审查视图

application/mobile/controller/Secret.php 8.1 KB
何书鹏 authored
1 2 3 4 5 6
<?php
namespace app\mobile\controller;

use think\Validate;
use think\Db;
use app\common\controller\Api;
何书鹏 authored
7 8 9 10
use app\mobile\model\SecretSpec;
use app\mobile\model\SecretOrder;
use app\mobile\model\Company;
use app\mobile\model\CompanyUser;
何书鹏 authored
11 12 13 14 15 16 17 18 19 20 21 22 23
use addons\epay\library\Service;

/**
 * 密卷接口
 */
class Secret extends Api
{
	protected $noNeedLogin = ['index','info'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
何书鹏 authored
24
        $this->model = model('app\mobile\model\Secret');
何书鹏 authored
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    }

    /**
     * @ApiTitle    (首页)
     * @ApiSummary  (首页)
     * @ApiMethod   (POST)
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1599201246",
        "data": [{
            "id": 1, //密卷名称
            "title": "测试密卷", //密卷标题
            "do_num": 20, //做过人数
            "current_price": "10.00", //现价
            "original_price": "10000.00" //原价
        }]
    })
     */
    public function index()
    {
何书鹏 authored
47 48 49 50
        $list = SecretModel::order('createtime desc')->select();
        foreach ($list as $v) {
            $v->visible(['id','title','do_num','current_price','original_price']);
        }
何书鹏 authored
51 52 53 54 55 56 57 58 59 60 61 62 63
        $this->success('成功',$list);
    }

    /**
     * @ApiTitle    (详情)
     * @ApiSummary  (详情)
     * @ApiMethod   (POST)
     *
     * @ApiParams (name="secret_id", type="int", required=true, description="密卷ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
何书鹏 authored
64
        "time": "1599803465",
何书鹏 authored
65
        "data": {
何书鹏 authored
66 67 68 69 70 71
            "id": 1, //密卷ID
            "title": "测试密卷", //密卷标题
            "current_price": "10.00", // 当前价格
            "original_price": "10000.00", //原价
            "description": "测试密卷", //密卷介绍
            "do_num": 20 //做过人数
何书鹏 authored
72 73 74 75 76 77 78 79 80
        }
    })
     */
    public function info()
    {
        $secret_id = $this->request->param('secret_id');
        empty($secret_id) && $this->error('缺少必要参数');
        $info = SecretModel::get($secret_id);
        empty($info) && $this->error('密卷信息不存在');
何书鹏 authored
81
        $info = $info->visible(['id','title','current_price','original_price','do_num','description']);
何书鹏 authored
82 83 84 85
        $this->success('成功',$info);
    }

    /**
何书鹏 authored
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
     * @ApiTitle    (选择规格)
     * @ApiSummary  (选择规格)
     * @ApiMethod   (POST)
     *
     * @ApiParams (name="secret_id", type="int", required=true, description="密卷ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1599739316",
        "data": [{
            "id": 1, //规格ID
            "secret_id": 1, //密卷ID
            "name": "基础版", //密卷名称
            "current_price": "500.00", //当前价格
            "original_price": "1000.00", //原价
            "people_num": 20, //限制人数
            "is_top": "0", //是否顶配:0=否,1=是
            "createtime": null,
            "updatetime": null,
            "is_pay": 1 //是否可以购买:0=否,1=是
        }]
    })
     */
    public function spec()
    {
        $company = Company::get(['user_id'=>$this->auth->id]);
        $secret_id = $this->request->param('secret_id');
        empty($secret_id) && $this->error('缺少必要参数');
        $info = SecretModel::get($secret_id);
        empty($info) && $this->error('密卷信息不存在');
        $list = SecretSpec::where('secret_id',$secret_id)->select();
        foreach ($list as &$v) {
            // 是否可购买
            $people_num = CompanyUser::where('company_id',$company['id'])
                ->where('status','1')
                ->count();
            if($v['is_top'] == '1'){
                $v['is_pay'] = 1;
            }else{
                $v['is_pay'] = $v['people_num'] < $people_num ? 0 : 1;
            }
            // 显示价格
            $order = SecretOrder::where('company_id',$company['id'])
                ->where('secret_id',$secret_id)
                ->where('pay_status','1')
                ->order('people_num desc')
                ->find();
            $v['current_price'] = !empty($order) ? $v['current_price']-$order['total_price'] : $v['current_price'];
        }
        $this->success('成功',$list);
    }

    /**
何书鹏 authored
140 141
     * @ApiTitle    (购买预览)
     * @ApiSummary  (购买预览)
何书鹏 authored
142 143 144 145
     * @ApiMethod   (POST)
     *
     * @ApiParams (name="secret_id", type="int", required=true, description="密卷ID")
     * @ApiParams (name="spec_id", type="int", required=true, description="密卷规格ID")
何书鹏 authored
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
     * @ApiParams (name="score_switch", type="int", description="积分开关:0=关,1=开")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1599046220",
        "data": {
            "id": 1, //试卷ID
            "title": "测试试卷", //试卷标题
            "year": 2015, //年费(单位:年)
            "time": 100, //答题时间(单位:分)
            "pass_score": 80, //合格分数
            "description": "这个还行", //试卷描述
            "do_num": 10, //回答人数
            "full_score": 100 //试卷分数(单位:分)
        }
    })
     */
    public function payView()
    {
        $param = $this->request->param();
        if(!$order = $this->model->payView($this->auth->getUser(),$param)){
            $this->error($this->model->getError(),null,$this->model->getCode());
        }
        $this->success(__('成功'),$order);
    }

    /**
     * @ApiTitle    (购买)
     * @ApiSummary  (购买)
     * @ApiMethod   (POST)
     *
     * @ApiParams (name="secret_id", type="int", required=true, description="密卷ID")
     * @ApiParams (name="spec_id", type="int", required=true, description="密卷规格ID")
     * @ApiParams (name="score_switch", type="int", description="积分开关:0=关,1=开")
何书鹏 authored
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
     * @ApiParams (name="pay_type", type="string", required=true, description="支付方式:wechat=微信,alipay=支付宝")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1599046220",
        "data": {
            "id": 1, //试卷ID
            "title": "测试试卷", //试卷标题
            "year": 2015, //年费(单位:年)
            "time": 100, //答题时间(单位:分)
            "pass_score": 80, //合格分数
            "description": "这个还行", //试卷描述
            "do_num": 10, //回答人数
            "full_score": 100 //试卷分数(单位:分)
        }
    })
     */
    public function pay()
    {
何书鹏 authored
201 202 203
        $param = $this->request->param();
        if(!$order = $this->model->payView($this->auth->getUser(),$param)){
            $this->error($this->model->getError(),null,$this->model->getCode());
何书鹏 authored
204
        }
何书鹏 authored
205 206
        if (!$param['pay_type'] || !in_array($param['pay_type'], ['alipay', 'wechat'])) {
            $this->error("请选择支付方式");
何书鹏 authored
207
        }
何书鹏 authored
208 209 210
        // 创建订单
        $model = new SecretOrder;
        $model->add($this->auth->getUser(), $order, $param['pay_type']);
何书鹏 authored
211
        //回调链接
何书鹏 authored
212 213
        $notifyurl = $this->request->root(true) . '/mobile/secret/notifyx/paytype/' . $param['pay_type'];
        $payment = Service::submitOrder($model['pay_price'], $model['order_sn'], $param['pay_type'], '密卷', $notifyurl, null, 'app');
何书鹏 authored
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
        $this->success('成功',$payment);
    }

    /**
     * 支付成功
     */
    public function notifyx()
    {
        $paytype = $this->request->param('paytype');
        $pay = \addons\epay\library\Service::checkNotify($paytype);
        if (!$pay) {
            echo '签名错误';
            return;
        }
        $data = $pay->verify();
        try {
            $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
            $out_trade_no = $data['out_trade_no'];

            // 处理订单逻辑
何书鹏 authored
234
            $order = SecretOrder::get(['order_sn'=>$out_trade_no,'pay_price'=>$payamount,'pay_type'=>$paytype]);
何书鹏 authored
235 236 237 238 239 240 241 242
            if($order && $order['pay_status'] != '1'){
                $order->save(['pay_status'=>'1','pay_time'=>time()]);
            }
        } catch (Exception $e) {
        }
        echo $pay->success();
    }
}