Secret.php 16.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 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 140 141 142 143 144 145 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
<?php
namespace app\mobile\controller;

use think\Validate;
use think\Db;
use app\common\controller\Api;
use app\mobile\model\SecretSpec;
use app\mobile\model\SecretOrder;
use app\mobile\model\Company;
use app\mobile\model\CompanyUser;
use addons\epay\library\Service;
use app\mobile\model\Question;
use app\mobile\model\QuestionAnswer;
use app\mobile\model\User;

/**
 * 密卷接口
 * @ApiWeigh (94)
 */
class Secret extends Api
{
	protected $noNeedLogin = ['index','info'];
    protected $noNeedRight = ['*'];
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('app\mobile\model\Secret');
    }

    /**
     * @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", //原价
            "is_buy": 0 //是否已购买:0=否,1=是
        }]
    })
     */
    public function index()
    {
        // 我加入的企业购买的密卷
        $company_secret_list = SecretOrder::alias('a')
            ->join('mobile_company_user b','b.company_id = a.company_id')
            ->join('user c','c.id = b.user_id')
            ->where('b.user_id',$this->auth->id)
            ->where('b.status','1')
            ->where('c.group_id',1)
            ->field('a.id,a.company_id,a.people_num,a.is_top')
            ->select();
        // 查询我是否可以享受企业密卷(按企业审核时间排队,没在队伍里就无法享受企业密卷)
        $order_id_arr = [];
        foreach ($company_secret_list as $v) {
            if($v['is_top'] == '1'){
                $order_id_arr[] = $v['id'];
                continue;
            }
            $user_id_arr = CompanyUser::where('company_id',$v['company_id'])
                ->where('status','1')
                ->order('updatetime asc')
                ->limit($v['people_num'])
                ->column('user_id');
            if(in_array($this->auth->id,$user_id_arr)){
                $order_id_arr[] = $v['id'];
            }
        }
        // 查找所有我购买的和企业购买的密卷
        $secret_order = SecretOrder::where(function($query)use($order_id_arr){
                $query->where('user_id', $this->auth->id)->whereor('id', 'in', $order_id_arr);
            })
            ->where('pay_status','1')
            ->limit(1)
            ->buildSql();
        $list = $this->model
            ->alias('s')
            ->join([$secret_order => 'so'],'so.secret_id = s.id','left')
            ->field("
                s.*,
                if(so.id > 0,1,0) is_buy
            ")
            ->order('createtime desc')
            ->select();
        foreach ($list as $v) {
            $v->visible([
                'id',
                'title',
                'do_num',
                'current_price',
                'original_price'
            ])->append([
                'is_buy'
            ]);
        }
        $this->success('成功',$list);
    }

    /**
     * @ApiTitle    (详情)
     * @ApiSummary  (详情)
     * @ApiMethod   (POST)
     *
     * @ApiParams (name="secret_id", type="int", required=true, description="密卷ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1599803465",
        "data": {
            "id": 1, //密卷ID
            "title": "测试密卷", //密卷标题
            "current_price": "10.00", // 当前价格
            "original_price": "10000.00", //原价
            "description": "测试密卷", //密卷介绍
            "do_num": 20, //做过人数
            "description": '密卷介绍', //密卷介绍
            "is_buy": 0, //是否已购买:0=否,1=是
            "have_spec": 0, //是否有规格:0=否,1=是
        }
    })
     */
    public function info()
    {
        $secret_id = $this->request->param('secret_id');
        empty($secret_id) && $this->error('缺少必要参数');
        $info = $this->model->get($secret_id);
        empty($info) && $this->error('密卷信息不存在');
        // 是否已购买
        // 我加入的企业购买的密卷
        $company_secret_list = SecretOrder::alias('a')
            ->join('mobile_company_user b','b.company_id = a.company_id')
            ->join('user c','c.id = b.user_id')
            ->where('b.user_id',$this->auth->id)
            ->where('b.status','1')
            ->where('c.group_id',1)
            ->field('a.id,a.company_id,a.people_num,a.is_top')
            ->select();
        // 查询我是否可以享受企业密卷(按企业审核时间排队,没在队伍里就无法享受企业密卷)
        $order_id_arr = [];
        foreach ($company_secret_list as $v) {
            if($v['is_top'] == '1'){
                $order_id_arr[] = $v['id'];
                continue;
            }
            $user_id_arr = CompanyUser::where('company_id',$v['company_id'])
                ->where('status','1')
                ->order('updatetime asc')
                ->limit($v['people_num'])
                ->column('user_id');
            if(in_array($this->auth->id,$user_id_arr)){
                $order_id_arr[] = $v['id'];
            }
        }
        $order = SecretOrder::where(function($query)use($order_id_arr){
                $query->where('user_id', $this->auth->id)->whereor('id', 'in', $order_id_arr);
            })
            ->where('secret_id',$secret_id)
            ->where('pay_status','1')
            ->field('id')
            ->find();
        $info->is_buy = !empty($order) ? 1 : 0;
        // 是否有规格
        $have_spec = SecretSpec::where('secret_id',$info['id'])->find();
        $info->have_spec = $have_spec ? 1 : 0;
        $info = $info->visible(['id','title','current_price','original_price','do_num','description'])->append(['is_buy','have_spec']);
        $this->success('成功',$info);
    }

    /**
     * @ApiTitle    (获取用户身份)
     * @ApiSummary  (获取用户身份)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1599739316",
        "data": {
            "group_id": 1, //0=普通用户,1=企业用户
        }
    })
     */
    public function getUserIdentity()
    {
        $group_id = User::where('id',$this->auth->id)->value('group_id');
        $this->success('成功',compact('group_id'));
    }

    /**
     * @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 = $this->model->get($secret_id);
        empty($info) && $this->error('密卷信息不存在');
        $list = SecretSpec::where('secret_id',$secret_id)->select();
        foreach ($list as &$v) {
            // 是否可购买
            $order = SecretOrder::where('company_id',$company['id'])
                ->where('secret_id',$secret_id)
                ->where('pay_status','1')
                ->order(['is_top'=>'desc','people_num'=>'desc'])
                ->find();
            $people_num = CompanyUser::where('company_id',$company['id'])
                ->where('status','1')
                ->count();
            if(!empty($order)){
                // 已购买顶配套餐,直接跳出本次循环
                if($order['is_top'] == '1'){
                    $v['is_pay'] = 0;
                    continue;
                }
                if($order['people_num'] > $people_num){
                    $people_num = $order['people_num'];
                }
                $v['current_price'] = $v['current_price']-$order['secret_price'];
            }
            if($v['is_top'] == '1'){
                $v['is_pay'] = 1;
            }else{
                $v['is_pay'] = $v['people_num'] <= $people_num ? 0 : 1;
            }
        }
        $this->success('成功',$list);
    }

    /**
     * @ApiTitle    (套餐说明)
     * @ApiSummary  (套餐说明)
     * @ApiMethod   (POST)
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1599017563",
        "data": "套餐说明" //套餐说明内容
    })
     */
    public function spec_intro()
    {
        $content = Db::name('mobile_config')->where('id',1)->value('secret_spec_intro');
        $this->success('成功', $content);
    }

    /**
     * @ApiTitle    (购买预览)
     * @ApiSummary  (购买预览)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     * @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=开")
     *
     * @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)
     *
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     * @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=开")
     * @ApiParams (name="pay_type", type="string", required=true, description="支付方式:wechat=微信,alipay=支付宝,money=余额")
     *
     * @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()
    {
        $param = $this->request->param();
        if(!$order = $this->model->payView($this->auth->getUser(),$param)){
            $this->error($this->model->getError(),null,$this->model->getCode());
        }
        if (!$param['pay_type'] || !in_array($param['pay_type'], ['alipay', 'wechat', 'money'])) {
            $this->error("请选择支付方式");
        }
        // 创建订单
        $model = new SecretOrder;
        $model->add($this->auth->getUser(), $order, $param['pay_type']);
        // 零元直接支付成功
        if($model['pay_price'] <= 0 || $param['pay_type'] == 'money'){
            (new Notify)->notifySecretZero($model['order_sn'],$model['pay_price'],$param['pay_type']);
            $this->success('成功',[]);
        }
        //回调链接
        $notifyurl = $this->request->root(true) . '/mobile/notify/notifySecret/paytype/' . $param['pay_type'];
        // $model['pay_price'] = 0.01; //测试金额
        $payment = Service::submitOrder($model['pay_price'], $model['order_sn'], $param['pay_type'], '密卷', $notifyurl, null, 'app');
        $this->success('成功',$payment);
    }

    /**
     * @ApiTitle    (通关密卷-题目详情)
     * @ApiSummary  (通关密卷-题目详情)
     * @ApiMethod   (POST)
     *
     * @ApiParams (name="secret_id", type="int", required=true, description="密卷ID")
     * @ApiParams (name="answer_type", type="string", required=false, description="作答类型:0=全部,1=已答题,2=未答题,3=错题")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1599046220",
        "data": {
            "total": 1, //题目总数
            "list": [{ //题目列表
                "id": 1, //题目ID
                "title": "测定混凝土立方体抗压强度时,标准试件的尺寸是(      )㎜。", //题目
                "option": "[{\"name\":\"A\",\"gender\":\"100\\u00d7100\\u00d7100\"},{\"name\":\"B\",\"gender\":\"150\\u00d7150\\u00d7150\"},{\"name\":\"C\",\"gender\":\"200\\u00d7200\\u00d7200\"},{\"name\":\"D\",\"gender\":\"70.7\\u00d770.7\\u00d770.7\"}]", //题目选项
                "type": "1", //题目类型:1=单选题,2=多选题,3=判断题,4=简答题
                "answer": "A", //答案
                "note": "45678910", //笔记
                "is_collect": "1" //是否收藏:0=否,1=是
            }]
        }
    })
     */
    public function questionList()
    {
        $user_id = $this->auth->id;
        $secret_id = $this->request->param('secret_id');
        $answer_type = $this->request->param('answer_type');
        empty($secret_id) && $this->error('缺少必要参数');
        $info = $this->model->get($secret_id);
        empty($info) && $this->error('密卷信息不存在');
        $where['q.target_id'] = $secret_id;
        $where['q.target_type'] = '5';
        switch ($answer_type) {
            case '1':
                $question_id_arr = QuestionAnswer::where('user_id',$user_id)->column('question_id');
                $where['q.id'] = !empty($question_id_arr) ? ['in',$question_id_arr] : 0;
                break;
            case '2':
                $question_id_arr = QuestionAnswer::where('user_id',$user_id)->column('question_id');
                if(!empty($question_id_arr)){
                    $where['q.id'] = ['notin',$question_id_arr];
                }
                break;
            case '3':
                $question_id_arr = QuestionAnswer::where('user_id',$user_id)->where('is_wrong','1')->column('question_id');
                $where['q.id'] = !empty($question_id_arr) ? ['in',$question_id_arr] : 0;
                break;
        }
        $list = Question::alias('q')
            ->join('mobile_question_note qn','q.id = qn.question_id and qn.user_id='.$user_id,'left')
            ->join('mobile_question_collect qc','q.id = qc.question_id and qn.user_id='.$user_id,'left')
            ->where($where)
            ->field('
                q.id,
                q.title,
                q.option,
                q.type,
                q.answer,
                qn.content note,
                if(qc.id > 0,1,0) is_collect
            ')->select();
        $total = count($list);
        // 删除原来的做题记录
        QuestionAnswer::where('question_id','in',array_column($list, 'id'))
            ->where('user_id',$user_id)
            ->delete();
        $this->success('成功',compact('total','list'));
    }
}