Coupon.php 1.9 KB
<?php

namespace Coupon;

use Api\Sign;
use Api\Yohobuy;
use LibModels\Wap\Cuxiao\ActivityData;
use Plugin\Helpers;

/**
 * 发券相关的数据处理模型
 *
 * @name CouponModel
 * @package Models/Coupon
 * @copyright yoho.inc
 * @version 1.0 (2016-04-19 13:52:44)
 * @author Gtskk<2330416537@qq.com>
 */
class CouponModel
{
    /**
     * 发放优惠券返回的接口数据处理
     *
     * @param int $uid 用户ID
     * @param string $token 发券标记
     * @param array $app url中传递的app有关的参数
     * @return mixed
     */
    public static function couponSend($uid, $token, $app)
    {
        $result = array('code' => 403, 'message' => '参数错误', 'data' => '');

        do {
            // APP时用参数中的ID
            if (self::checkApp($app)) {
                $uid = isset($app['uid']) ? $app['uid'] : 0;
            }

            // 用户ID或者发券标记为空时
            if (empty($uid) || empty($token)) {
                break;
            }

            $couponResult = ActivityData::couponSend($uid, $token);
            // 接口返回错误时
            if (empty($couponResult)) {
                $result['code'] = 404;
                $result['message'] = '出错啦~';
                break;
            }

            $result = $couponResult;
        } while (false);

        return $result;
    }

    /**
     * 校验是否为app
     *
     * @param array $app url中传递的app有关的参数
     * @return bool
     */
    private static function checkApp($app)
    {
        $isApp = false;

        // APP时用参数中的ID
        if (!empty($app) && isset($app['client_secret'])) {
            $params = $app;
            unset($params['client_secret']);
            $params['private_key'] = Yohobuy::$privateKeyList[$params['client_type']];

            $isApp = ($app['client_secret'] === Sign::getSign($params));
        }

        return $isApp;
    }

}