审查视图

application/api/controller/User.php 65.7 KB
郭盛 authored
1 2 3 4 5 6 7 8 9 10
<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Db;
use think\Validate;
郭盛 authored
11
use fast\Http;
郭盛 authored
12 13 14 15 16 17

/**
 * 个人中心
 */
class User extends Api
{
郭盛 authored
18
    protected $noNeedLogin = ['login','index','type','mine','car'];
郭盛 authored
19
    protected $noNeedRight = '*';
郭盛 authored
20
    protected $uid = '';
郭盛 authored
21 22 23 24

    public function _initialize()
    {
        parent::_initialize();
郭盛 authored
25
        $this->uid = $this->auth->getUserId();
郭盛 authored
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
    }

    /**
     * @ApiTitle    (小程序登录)
     * @ApiSummary  (小程序登录)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/login)
     * @ApiParams   (name="code", type="string", required=true, description="小程序code")
     * @ApiParams   (name="nickname", type="string", required=true, description="小程序昵称")
     * @ApiParams   (name="avatar", type="string", required=true, description="小程序头像")
     * @ApiReturn({
    "code": 1,
    "msg": "登录成功",
    "time": "1553839125",
    "data": {
    "token": "677afb39-1a4f-4492-84d3-0bcf32016b8a",//token
    "user_id": 27,//用户id
    "createtime": 1553839125,//登录时间
    "expiretime": 1556431125,//token失效时间
    "expires_in": 2592000//token失效剩余时间(单位s)
    "openid": 1485212522522//openid
    })
     */
    public function login(){
        if($this->request->isPost()){
            //小程序配置
            $config =  config('verify.raw');
            //小程序传递数据,包含昵称,头像,code
            $raw_data = $this->request->post();
            //验证表数据
            $rule = config('verify.user');
郭盛 authored
57
            $validate = new Validate($rule['rule'],$rule['msg']);
郭盛 authored
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
            if (!$validate->check($raw_data)) {
                $this->error($validate->getError());
            }
            $params = [
                'appid'      => $config['app_id'],
                'secret'     => $config['secret'],
                'js_code'    => $raw_data['code'],
                'grant_type' => 'authorization_code'
            ];
            $result = Http::sendRequest("https://api.weixin.qq.com/sns/jscode2session", $params, 'GET');
            if ($result['ret']) {
                $json = (array)json_decode($result['msg'], true);
                if (isset($json['openid'])) {
                    $result = [
                        'openid' => $json['openid'],
                        'nickname' => $raw_data['nickname'],
                        'avatar' => $raw_data['avatar']
                    ];
                    $ret = $this->auth->login($result);
                    if ($ret) {
                        $data = $this->auth->getUserinfo();
                        $this->success('登录成功', $data);
                    }else {
                        $this->error($this->auth->getError());
                    }
                } else {
                    $this->error("登录失败",$json);
                }
            }
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
郭盛 authored
93 94
     * @ApiTitle    (首页视频、图片、精美大片入口)
     * @ApiSummary  (首页视频、图片、精美大片入口)
郭盛 authored
95 96 97 98 99
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/index)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturn   ({
    'code':'1',
郭盛 authored
100 101 102 103 104 105 106
    'msg':'返回成功',
    "time": "1571492001",
        "data": {
        "video_thumbnail": http://mapwww.baidu.com 视频封面图地址
        "pic_thumbnail": http://mapwww.baidu.com 图片地址
        "wonderful_thumbnail": http://mapwww.baidu.com 精美大片地址
        }
郭盛 authored
107 108 109 110
    })
     */
    public function index()
    {
郭盛 authored
111
        $rule = config('verify.path');
郭盛 authored
112 113
        $data = Db::name('video')
            ->field('id,thumbnail')
郭盛 authored
114
            ->where('is_top',1)
115
            ->order('createtime desc,id desc')
郭盛 authored
116 117
            ->limit('1')
            ->find();
郭盛 authored
118 119
        $pic = Db::name('pic')
            ->field('id,thumbnail')
郭盛 authored
120
            ->where('is_top',1)
121
            ->order('createtime desc,id desc')
郭盛 authored
122 123 124 125
            ->limit('1')
            ->find();
        $wonderful = Db::name('wonderful')
            ->field('id,thumbnail')
126 127
            ->where('is_top',1)
            ->order('createtime desc,id desc')
郭盛 authored
128
            ->limit('1')
郭盛 authored
129
            ->find();
130 131 132 133 134
//        $res['wonderful_thumbnail'] = [];
//        foreach ($wonderful as &$v){
//            $v['thumbnail'] = $rule.$v['thumbnail'];
//            array_push($res['wonderful_thumbnail'],$v['thumbnail']);
//        }
郭盛 authored
135 136 137 138 139
        $this->auth;
        $res['video_thumbnail'] = $rule.$data['thumbnail'];
        $res['pic_thumbnail'] = $rule.$pic['thumbnail'];
        $res['wonderful_thumbnail'] = $rule.$wonderful['thumbnail'];
        $this->success('SUCCESS',$res);
郭盛 authored
140 141
    }
郭盛 authored
142 143 144
    /**
     * @ApiTitle    (地区分类)
     * @ApiSummary  (地区分类)
郭盛 authored
145
     * @ApiMethod   (POST)
郭盛 authored
146 147 148 149 150 151 152 153
     * @ApiRoute    (/api/user/type)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
郭盛 authored
154 155 156 157
        "id": 4,//分类id
        "area_name": //分类名称
        "thumbnail": //图片
        }
郭盛 authored
158 159 160 161
    })
     */
    public function type()
    {
郭盛 authored
162
        $rule = config('verify.path');
郭盛 authored
163 164 165 166
        $data = Db::name('type')
            ->field('id,area_name,thumbnail')
            ->order('id desc')
            ->select();
郭盛 authored
167 168 169
        foreach ($data as &$v){
            $v['thumbnail'] = $rule.$v['thumbnail'];
        }
郭盛 authored
170 171 172
        $this->success('SUCCESS',$data);
    }
郭盛 authored
173
    /**
郭盛 authored
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
     * @ApiTitle    (图片分类(类型))
     * @ApiSummary  (图片分类(类型))
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/pictype)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
        "id": 4,//分类id
        "typename": //分类名称
        "thumbnail": //图片
    }
    })
     */
    public function pictype()
    {
        $rule = config('verify.path');
        $data = Db::name('pictype')
            ->field('id,typename,thumbnail')
            ->order('id desc')
            ->select();
        foreach ($data as &$v){
            $v['thumbnail'] = $rule.$v['thumbnail'];
        }
        $this->success('SUCCESS',$data);
    }

    /**
郭盛 authored
205 206 207 208 209
     * @ApiTitle    (搜索页)
     * @ApiSummary  (搜索页)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/search)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
郭盛 authored
210
     *
郭盛 authored
211
     * @ApiParams   (name="type_id", type="inter", required=false, description="视频/图片  (视频为1 图片为2 精美大片为3)")
郭盛 authored
212
     * @ApiParams   (name="keyword", type="string", required=true, description="关键字")
郭盛 authored
213 214
     * @ApiParams   (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams   (name="pageNum", type="inter", required=false, description="每页显示数据个数(默认10)")
郭盛 authored
215
     * @ApiReturn({
郭盛 authored
216 217 218 219 220 221 222 223
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        "data": {
            "id": 4,//视频id/图片id,
            "thumbnail": //视频封面图地址/图片地址,
            "video": //视频路径/精美大片视频路径,
        }
郭盛 authored
224 225 226 227
    })
     */
    public function search()
    {
郭盛 authored
228
        $user_id = $this->uid;
郭盛 authored
229 230
        $page = $this->request->param('page',1,'intval');
        $pageNum = $this->request->param('pageNum',10,'intval');
郭盛 authored
231 232
        $rule = config('verify.path');
        $type_id = $this->request->post('type_id');
郭盛 authored
233 234 235
        if(empty($type_id)){
            $this->error(['code'=>2,'msg'=>'缺少必要参数']);
        }
郭盛 authored
236 237
        $keyword = $this->request->post('keyword');
        if(!empty($keyword)){
郭盛 authored
238 239 240 241 242 243
            $param['user_id'] = $user_id;
            $param['title'] = $keyword;
            Db::name('history')
                ->insert($param);
        }
        if(!empty($keyword)){
郭盛 authored
244 245
            if($type_id == 1){
                $data = Db::name('video')
郭盛 authored
246 247
                    ->where('keyword','like',"%$keyword%")
                    ->whereOr('title','like',"%$keyword%")
郭盛 authored
248
                    ->order('id desc')
郭盛 authored
249
                    ->page($page,$pageNum)
郭盛 authored
250 251 252 253 254
                    ->select();
                foreach ($data as &$v){
                    $v['thumbnail'] = $rule.$v['thumbnail'];
                    $v['video'] = $rule.$v['video'];
                }
郭盛 authored
255
                $this->success('SUCCESS',$data);
郭盛 authored
256
        }elseif($type_id == 2){
郭盛 authored
257
                $data = Db::name('pic')
郭盛 authored
258 259
                    ->where('keyword','like',"%$keyword%")
                    ->whereOr('title','like',"%$keyword%")
郭盛 authored
260
                    ->order('id desc')
郭盛 authored
261
                    ->page($page,$pageNum)
郭盛 authored
262 263 264 265 266 267 268
                    ->select();
                foreach ($data as &$v){
                    $v['thumbnail'] = $rule.$v['thumbnail'];
                }
                $this->success('SUCCESS',$data);
            }else{
                $data = Db::name('wonderful')
郭盛 authored
269
                    ->where('title','like',"%$keyword%")
郭盛 authored
270
                    ->order('id desc')
郭盛 authored
271
                    ->page($page,$pageNum)
郭盛 authored
272 273 274 275 276 277
                    ->select();
                foreach ($data as &$v){
                    $v['thumbnail'] = $rule.$v['thumbnail'];
                    $v['video'] = $rule.$v['video'];
                }
                $this->success('SUCCESS',$data);
郭盛 authored
278 279 280 281 282 283 284
            }
        }else{
            $this->error(['code'=>2,'msg'=>'请输入关键字']);
        }

    }
郭盛 authored
285 286 287 288 289 290
    /**
     * @ApiTitle    (图片列表)
     * @ApiSummary  (图片列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/pic)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
郭盛 authored
291
     *
郭盛 authored
292
     * @ApiParams   (name="pictype_id", type="inter", required=false, description="类型ID")
郭盛 authored
293 294
     * @ApiParams   (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams   (name="pageNum", type="inter", required=false, description="每页显示数据个数(默认10)")
郭盛 authored
295
     * @ApiReturn({
郭盛 authored
296 297 298 299 300 301 302 303
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        "data": {
            "id": 4,//图片id,
            "thumbnail": //缩略图,
        }
        })
郭盛 authored
304
 */
郭盛 authored
305 306 307
    public function pic()
    {
        $rule = config('verify.path');
郭盛 authored
308 309 310 311 312
        $page = $this->request->param('page', 1, 'intval');
        $pageNum = $this->request->param('pageNum', 10, 'intval');
        $pictype_id = $this->request->post('pictype_id');
        if (empty($pictype_id)) {
            $this->error(['code'=>2,'msg'=>'缺少必要参数']);
郭盛 authored
313
        }else{
郭盛 authored
314 315
            $pictype_id = ','.$pictype_id.',';
            $where['pictype_ids'] = ['like',"%$pictype_id%"];
郭盛 authored
316
            $data = Db::name('pic')
郭盛 authored
317
                ->field('id,thumbnail,title')
郭盛 authored
318
                ->where($where)
郭盛 authored
319
                ->order('id desc')
郭盛 authored
320
                ->page($page, $pageNum)
郭盛 authored
321
                ->select();
郭盛 authored
322 323
            foreach ($data as &$v) {
                $v['thumbnail'] = $rule . $v['thumbnail'];
郭盛 authored
324
            }
郭盛 authored
325
            $this->success('SUCCESS', $data);
郭盛 authored
326 327
        }
    }
郭盛 authored
328
郭盛 authored
329
    /**
郭盛 authored
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
     * @ApiTitle    (分类下的图片视频)
     * @ApiSummary  (分类下的图片视频)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/pic_video)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="type_id", type="inter", required=true, description="地区ID")
     * @ApiParams   (name="type", type="inter", required=false, description="类型ID(图片为1 视频为2)")
     * @ApiParams   (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams   (name="pageNum", type="inter", required=false, description="每页显示数据个数(默认10)")
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        "data": {
            "id": 4,//图片id,
            "thumbnail": //缩略图,
        }
        })
     */
    public function pic_video()
    {
        $rule = config('verify.path');
        $page = $this->request->param('page',1,'intval');
        $pageNum = $this->request->param('pageNum',10,'intval');
        $type_id = $this->request->post('type_id');
        $type = $this->request->post('type');
        if($type == 1){
            $type = ','.$type_id.',';
            $where['type_ids'] = ['like',"%$type%"];
            $data = Db::name('pic')
                ->where($where)
                ->field('id,thumbnail,title')
363
                ->order('is_top desc,id desc')
郭盛 authored
364 365 366 367 368 369 370 371 372 373 374
                ->page($page,$pageNum)
                ->select();
            foreach ($data as &$v){
                $v['thumbnail'] = $rule.$v['thumbnail'];
            }
            $this->success('SUCCESS',$data);
        }else{
            $type = ','.$type_id.',';
            $where['type_ids'] = ['like',"%$type%"];
            $data = Db::name('video')
                ->where($where)
郭盛 authored
375
                ->field('id,thumbnail,video,title')
376
                ->order('is_top desc,id desc')
郭盛 authored
377 378 379 380 381 382 383 384 385 386 387 388
                ->page($page,$pageNum)
                ->select();
            foreach ($data as &$v){
                $v['thumbnail'] = $rule.$v['thumbnail'];
                $v['video'] = $rule.$v['video'];
            }
            $this->success('SUCCESS',$data);
        }

    }

    /**
郭盛 authored
389 390 391 392 393 394
     * @ApiTitle    (视频列表)
     * @ApiSummary  (视频列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/video)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="type_id", type="inter", required=false, description="地区ID")
郭盛 authored
395 396
     * @ApiParams   (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams   (name="pageNum", type="inter", required=false, description="每页显示数据个数(默认10)")
郭盛 authored
397 398 399 400 401 402 403 404 405 406 407 408 409 410
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        "data": {
            "id": //图片id,
            "thumbnail": //视频封面,
            "video"://视频地址
        }
        })
     */
    public function video()
    {
        $rule = config('verify.path');
郭盛 authored
411 412
        $page = $this->request->param('page',1,'intval');
        $pageNum = $this->request->param('pageNum',10,'intval');
郭盛 authored
413 414 415 416 417
        $type_id = $this->request->post('type_id');
        if(empty($type_id)){
            $data = Db::name('video')
                ->field('id,thumbnail,video')
                ->order('id desc')
郭盛 authored
418
                ->page($page,$pageNum)
郭盛 authored
419 420 421 422 423 424 425 426 427 428 429 430 431
                ->select();
            foreach ($data as &$v){
                $v['thumbnail'] = $rule.$v['thumbnail'];
                $v['video'] = $rule.$v['video'];
            }
            $this->success('SUCCESS',$data);
        }else{
            $type = ','.$type_id.',';
            $where['type_ids'] = ['like',"%$type%"];
            $data = Db::name('video')
                ->where($where)
                ->field('id,thumbnail,video')
                ->order('id desc')
郭盛 authored
432
                ->page($page,$pageNum)
郭盛 authored
433 434 435 436 437 438 439 440 441
                ->select();
            foreach ($data as &$v){
                $v['thumbnail'] = $rule.$v['thumbnail'];
                $v['video'] = $rule.$v['video'];
            }
            $this->success('SUCCESS',$data);
        }
    }
郭盛 authored
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
    /**
     * @ApiTitle    (精美大片列表)
     * @ApiSummary  (精美大片列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/won)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams   (name="pageNum", type="inter", required=false, description="每页显示数据个数(默认10)")
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
    "id": //图片id,
    "thumbnail": //视频封面,
    "video"://视频地址
    }
    })
     */
    public function won()
    {
        $rule = config('verify.path');
        $page = $this->request->param('page',1,'intval');
        $pageNum = $this->request->param('pageNum',10,'intval');
郭盛 authored
467
        $data = Db::name('wonderful')
郭盛 authored
468
            ->field('id,thumbnail,video,title,content')
郭盛 authored
469 470 471 472 473 474
            ->order('id desc')
            ->page($page,$pageNum)
            ->select();
        foreach ($data as &$v){
            $v['thumbnail'] = $rule.$v['thumbnail'];
            $v['video'] = $rule.$v['video'];
郭盛 authored
475
        }
郭盛 authored
476
        $this->success('SUCCESS',$data);
郭盛 authored
477
郭盛 authored
478
    }
郭盛 authored
479
郭盛 authored
480
    /**
郭盛 authored
481 482 483 484 485 486 487 488 489 490 491 492 493
     * @ApiTitle    (图片详情)
     * @ApiSummary  (图片详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/picdetail)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="pic_id", type="inter", required=true, description="图片ID")
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        "data": {
            "id": 4,//图片id,
郭盛 authored
494
            "title"://标题
郭盛 authored
495
            "thumbnail": //缩略图,
郭盛 authored
496
            "accredit": //授权书图片
郭盛 authored
497
            "water_url": //小样带水印的图片地址,
郭盛 authored
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
           "price"://正常价格,
           "vip_price"://vip价格,
           "content"://图片介绍,
           "is_vip"://是否为会员(0否 1是)

        }
    })
     */
    public function picdetail()
    {
        $uid = $this->uid;
        $rule = config('verify.path');
        $res = Db::name('user')
            ->where('id',$uid)
            ->field('id,is_vip')
            ->find();
        if($res['is_vip'] == 0){
            $pic_id = $this->request->post('pic_id');
            if(empty($pic_id)){
                $this->error(['code'=>2,'msg'=>'缺少必要参数']);
            }
郭盛 authored
519 520 521
            $accredit = Db::name('accredit')
                ->where('id',1)
                ->find();
郭盛 authored
522
            $data = Db::name('pic')
郭盛 authored
523
                ->field('id,title,thumbnail,price,water_url,url,code,content,click_num')
郭盛 authored
524 525
                ->where('id',$pic_id)
                ->find();
郭盛 authored
526 527 528
            if(empty($data)){
                $this->error(['code'=>2,'msg'=>'参数错误']);
            }
郭盛 authored
529 530
            $click_num = $data['click_num'] + 1;
            Db::name('pic')->where('id',$pic_id)->update(['click_num'=>$click_num]);
郭盛 authored
531
            $data['accredit'] = $rule.$accredit['thumbnail'];
郭盛 authored
532
            $data['water_url'] = $rule.$data['water_url'];
郭盛 authored
533
            $data['vip_price'] = $data['price'] * 0.75;
郭盛 authored
534
            $data['thumbnail'] = $rule.$data['thumbnail'];
郭盛 authored
535
            $data['content'] = strip_tags($data['content']);
郭盛 authored
536 537 538 539 540 541 542 543
            $data['is_vip'] = 0;
            $this->success('SUCCESS',$data);
        }elseif($res['is_vip'] == 1){
            $pic_id = $this->request->post('pic_id');
            if(empty($pic_id)){
                $this->error(['code'=>2,'msg'=>'缺少必要参数']);
            }
            $data = Db::name('pic')
郭盛 authored
544
                ->field('id,title,thumbnail,price,water_url,url,code,content,click_num')
郭盛 authored
545 546
                ->where('id',$pic_id)
                ->find();
郭盛 authored
547 548 549
            $accredit = Db::name('accredit')
                ->where('id',1)
                ->find();
郭盛 authored
550 551 552
            if(empty($data)){
                $this->error(['code'=>2,'msg'=>'参数错误']);
            }
郭盛 authored
553 554
            $click_num = $data['click_num'] + 1;
            Db::name('pic')->where('id',$pic_id)->update(['click_num'=>$click_num]);
郭盛 authored
555
            $data['is_vip'] = 1;
郭盛 authored
556
            $data['accredit'] = $rule.$accredit['thumbnail'];
郭盛 authored
557
            $data['water_url'] = $rule.$data['water_url'];
郭盛 authored
558
            $data['vip_price'] = $data['price'] * 0.75;
郭盛 authored
559
            $data['thumbnail'] = $rule.$data['thumbnail'];
郭盛 authored
560
            $data['content'] = strip_tags($data['content']);
郭盛 authored
561 562 563 564
            $this->success('SUCCESS',$data);
        }
    }
郭盛 authored
565
    /**
郭盛 authored
566 567 568 569 570 571 572 573 574 575 576 577 578 579
     * @ApiTitle    (视频详情)
     * @ApiSummary  (视频详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/videodetail)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="video_id", type="inter", required=true, description="视频ID")
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        "data": {
            "id": 4,//视频id,
            "title"://标题
郭盛 authored
580
            "accredit": //授权书图片
郭盛 authored
581 582 583 584 585
            "thumbnail": //视频封面图,
            "video": //2K视频地址,
            "water_url"://小样视频网址
            "price"://2K价格,
            "vip_price"://2Kvip价格,
郭盛 authored
586 587 588 589
            "four_price"://4K价格,
            "four_vipprice"://4Kvip价格,
            "eight_price"://8K价格,
            "eight_vipprice"://8Kvip价格,
郭盛 authored
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
            "content"://图片介绍,
            "is_vip"://是否为会员(0否 1是)
        }
        })
     */
    public function videodetail()
    {
        $uid = $this->uid;
        $rule = config('verify.path');
        $res = Db::name('user')
            ->where('id',$uid)
            ->field('id,is_vip')
            ->find();
        if($res['is_vip'] == 0){
            $pic_id = $this->request->post('video_id');
            if(empty($pic_id)){
                $this->error(['code'=>2,'msg'=>'缺少必要参数']);
            }
            $data = Db::name('video')
郭盛 authored
609
                ->field('id,click_num,title,thumbnail,video,price,four_price,eight_price,water_url,two_url,four_url,two_code,four_code,eight_code,eight_url,content')
郭盛 authored
610 611
                ->where('id',$pic_id)
                ->find();
郭盛 authored
612 613 614
            if(empty($data)){
                $this->error(['code'=>2,'msg'=>'参数错误']);
            }
郭盛 authored
615 616 617
            $accredit = Db::name('accredit')
                ->where('id',1)
                ->find();
郭盛 authored
618 619
            $click_num = $data['click_num']+1;
            Db::name('video')->where('id',$pic_id)->update(['click_num'=>$click_num]);
郭盛 authored
620
            $data['vip_price'] = $data['price']*0.75;
郭盛 authored
621
            $data['accredit'] = $rule.$accredit['thumbnail'];
郭盛 authored
622 623
            $data['four_vipprice'] = $data['four_price']*0.75;
            $data['eight_vipprice'] = $data['eight_price']*0.75;
郭盛 authored
624 625 626 627 628 629
            $data['thumbnail'] = $rule.$data['thumbnail'];
            $data['video'] = $rule.$data['video'];
            $data['content'] = strip_tags($data['content']);
            $data['is_vip'] = 0;
            $this->success('SUCCESS',$data);
        }elseif($res['is_vip'] == 1){
郭盛 authored
630
            $pic_id = $this->request->post('video_id');
郭盛 authored
631 632 633 634
            if(empty($pic_id)){
                $this->error(['code'=>2,'msg'=>'缺少必要参数']);
            }
            $data = Db::name('video')
郭盛 authored
635
                ->field('id,click_num,title,thumbnail,video,price,four_price,eight_price,water_url,two_url,two_code,four_code,eight_code,four_url,eight_url,content')
郭盛 authored
636 637
                ->where('id',$pic_id)
                ->find();
郭盛 authored
638 639 640
            if(empty($data)){
                $this->error(['code'=>2,'msg'=>'参数错误']);
            }
郭盛 authored
641 642 643
            $accredit = Db::name('accredit')
                ->where('id',1)
                ->find();
郭盛 authored
644 645
            $click_num = $data['click_num']+1;
            Db::name('video')->where('id',$pic_id)->update(['click_num'=>$click_num]);
郭盛 authored
646
            $data['is_vip'] = 1;
郭盛 authored
647
            $data['accredit'] = $rule.$accredit['thumbnail'];
郭盛 authored
648 649 650
            $data['vip_price'] = $data['price']*0.75;
            $data['four_vipprice'] = $data['four_price']*0.75;
            $data['eight_vipprice'] = $data['eight_price']*0.75;
郭盛 authored
651 652 653 654 655 656 657 658
            $data['thumbnail'] = $rule.$data['thumbnail'];
            $data['video'] = $rule.$data['video'];
            $data['content'] = strip_tags($data['content']);
            $this->success('SUCCESS',$data);
        }
    }

    /**
郭盛 authored
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
     * @ApiTitle    (历史记录)
     * @ApiSummary  (历史记录)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/history)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
        "id": 4,//记录id,
        "title"://搜索的内容
    }
    })
     */
    public function history()
    {
        $user_id = $this->uid;
        $data = Db::name('history')
            ->where('user_id',$user_id)
            ->order('id desc')
郭盛 authored
681 682 683 684
            ->column('title');
        $res = array_unique($data);
        $res = array_slice($res,0,10);
        $this->success('SUCCESS',$res);
郭盛 authored
685 686
    }
郭盛 authored
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
    /**
     * @ApiTitle    (我的)
     * @ApiSummary  (我的)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/mine)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        "data": {
            "id":       //用户ID
            "nickname": //昵称,
            "avatar":   //头像,
            "money" :   //余额
            "is_vip":   //是否为会员(0否 1是)
        }
        })
     */
    public function mine()
    {
        $user_id = $this->uid;
        $data = Db::name('user')
            ->where('id',$user_id)
            ->field('id,nickname,avatar,money,is_vip')
            ->find();
        $this->success('SUCCESS',$data);
    }

    /**
     * @ApiTitle    (创建充值订单)
     * @ApiSummary  (创建充值订单)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/toporder)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="money", type="float", required=true, description="充值金额")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        "data": {
            "order_id"://订单ID
        }
        })
     */
    public function toporder()
    {
        $param['user_id'] = $this->uid;
        $param['money'] = $this->request->post('money');
        if(empty( $param['money'])){
            $this->error(['code'=>2,'msg'=>'缺少必要参数']);
        }elseif ( $param['money']<=0){
            $this->error(['code'=>3,'msg'=>'非法操作']);
        }else{
            $param['createtime'] = time();
            $param['num'] = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
            $data = Db::name('toporder')
                ->insertGetId($param);
            if(empty($data)){
                $this->error(['code'=>2,'msg'=>'sql执行失败']);
            }
            $this->success('SUCCESS',['order_id'=>$data]);
        }

    }
郭盛 authored
756
    /**
郭盛 authored
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
     * @ApiTitle    (创建开通会员订单)
     * @ApiSummary  (创建开通会员订单)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/viporder)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="total", type="float", required=true, description="金额")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        "data": {
            "order_id"://订单ID
        }
        })
     */
    public function viporder()
    {
        $param['user_id'] = $this->uid;
        $param['total'] = $this->request->post('total');
        if(empty( $param['total'])){
            $this->error(['code'=>2,'msg'=>'缺少必要参数']);
        }elseif ( $param['total']<=8000){
            $this->error(['code'=>3,'msg'=>'开通会员至少需要8000元哦']);
        }else{
            $param['createtime'] = time();
            $param['num'] = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
            $data = Db::name('viporder')
                ->insertGetId($param);
            if(empty($data)){
                $this->error(['code'=>2,'msg'=>'sql执行失败']);
            }
            $this->success('SUCCESS',['order_id'=>$data]);
        }

    }

    /**
郭盛 authored
796 797
     * @ApiTitle    (会员介绍)
     * @ApiSummary  (会员介绍)
郭盛 authored
798 799 800 801 802 803 804 805 806
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/vipdesc)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
郭盛 authored
807
        "content":       //会员介绍内容
郭盛 authored
808 809 810 811 812
    }
    })
     */
    public function vipdesc()
    {
郭盛 authored
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
        $data = Db::name('vip')
            ->field('content')
            ->where('id',2)
            ->find();
        $this->success('SUCCESS',$data);
    }

    /**
     * @ApiTitle    (会员须知)
     * @ApiSummary  (会员须知)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/vipdetail)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
    "text":       //会员须知内容
    }
    })
     */
    public function vipdetail()
    {
        $data = Db::name('vip')
郭盛 authored
839
            ->field('desc')
郭盛 authored
840 841
            ->where('id',2)
            ->find();
郭盛 authored
842
郭盛 authored
843 844 845
        $this->success('SUCCESS',$data);
    }
郭盛 authored
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
    /**
     * @ApiTitle    (购物车列表)
     * @ApiSummary  (购物车列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/car)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
        "id"://购物车ID
          "user_id"://用户ID
          "pic_id"://图片的ID
          "video_id"://视频的ID
          "pic":[
                "id"://图片ID
               "title"://图片标题
               "thumbnail"://缩略图
               "url"://高清百度云盘地址
               "price"://普通价格
               "vip_price"://vip价格
          ],
          "video":[
               "id"://视频ID
               "title"://视频标题
               "thumbnail"://视频封面图片
               "video"://视频地址
               "two_url"://2K视频云盘地址
               "four_url"://4k视频云盘地址
               "eight_url"://8k视频云盘地址
               "price"://2k视频原价格
               "four_price"://4k视频原价格
               "eight_price"://8k视频原价格
               "vip_price"://2k会员价格
               "four_vipprice"://4k会员价格
               "eight_vipprice"://8k会员价格
          ]
    }
    })
     */
    public function car()
    {
        $user_id = $this->uid;
郭盛 authored
891
        $rule = config('verify.path');
郭盛 authored
892 893 894 895 896 897 898
        //查询该用户是否为会员
        $res = Db::name('user')
            ->where('id',$user_id)
            ->field('id,is_vip')
            ->find();
        //判断
        if($res['is_vip'] == 0){
郭盛 authored
899
            //不是会员的情况
郭盛 authored
900 901 902
            $data = Db::name('car')
                ->where('user_id',$user_id)
                ->field('id,user_id,pic_id,video_id')
郭盛 authored
903
                ->order('createtime desc,id desc')
郭盛 authored
904
                ->select();
郭盛 authored
905 906 907 908
            foreach ($data as &$v) {
                if(!empty($v['pic_id'])){
                    $v['pic_id'] = Db::name('pic')->field('id,title,thumbnail,url,price')->where('id',$v['pic_id'])->find();
                    $v['pic_id']['thumbnail'] = $rule.$v['pic_id']['thumbnail'];
郭盛 authored
909
                }
郭盛 authored
910 911 912 913
                if(!empty($v['video_id'])){
                    $v['video_id'] = Db::name('video')->field('keyword,type_ids,content,is_recommend,buy_num,click_num,createtime,updatetime',true)->where('id',$v['video_id'])->find();
                    $v['video_id']['thumbnail'] = $rule.$v['video_id']['thumbnail'];
                    $v['video_id']['video'] = $rule.$v['video_id']['video'];
郭盛 authored
914
                }
郭盛 authored
915 916 917 918 919 920 921
            }
            $this->success('SUCCESS',$data);
        }else{
            //是的情况
            $data = Db::name('car')
                ->where('user_id',$user_id)
                ->field('id,user_id,pic_id,video_id')
郭盛 authored
922
                ->order('createtime desc,id desc')
郭盛 authored
923 924
                ->select();
            foreach ($data as &$v){
郭盛 authored
925 926 927 928
                if(!empty($v['pic_id'])){
                    $v['pic_id'] = Db::name('pic')->field('id,title,thumbnail,url,price')->where('id',$v['pic_id'])->find();
                    $v['pic_id']['thumbnail'] = $rule.$v['pic_id']['thumbnail'];
                    $v['pic_id']['vipprice'] = $v['pic_id']['price']*0.75;
郭盛 authored
929
                }
郭盛 authored
930 931 932 933 934 935 936
                if(!empty($v['video_id'])){
                    $v['video_id'] = Db::name('video')->field('keyword,type_ids,content,is_recommend,buy_num,click_num,createtime,updatetime',true)->where('id',$v['video_id'])->find();
                    $v['video_id']['thumbnail'] = $rule.$v['video_id']['thumbnail'];
                    $v['video_id']['video'] = $rule.$v['video_id']['video'];
                    $v['video_id']['vipprice'] = $v['video_id']['price'] * 0.75;
                    $v['video_id']['four_vipprice'] = $v['video_id']['four_price'] * 0.75;
                    $v['video_id']['eight_vipprice'] = $v['video_id']['eight_price'] * 0.75;
郭盛 authored
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
                }
            }
            $this->success('SUCCESS',$data);
        }
    }

    /**
     * @ApiTitle    (加入购物车)
     * @ApiSummary  (加入购物车)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/addcar)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="pic_id", type="inter", required=false, description="图片ID")
     * @ApiParams   (name="video_id", type="inter", required=false, description="视频ID")
郭盛 authored
952
     *
郭盛 authored
953 954 955 956 957 958 959 960 961 962 963 964
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    })
     */
    public function addcar()
    {
        $user_id = $this->uid;
        $pic_id = $this->request->param('pic_id');
        $video_id = $this->request->param('video_id');
郭盛 authored
965 966 967
        if(empty($pic_id) && empty($video_id)){
            $this->error(['code'=>2,'msg'=>'缺少必要参数']);
        }
郭盛 authored
968
        if(empty($pic_id)){
郭盛 authored
969
            //接受视频ID并判断
970 971 972 973 974 975 976 977
            $data = Db::name('car')
                ->field('id')
                ->where('user_id',$user_id)
                ->where('video_id',$video_id)
                ->find();
            if(!empty($data)){
                $this->error(['code'=>2,'msg'=>'您已经添加过了']);
            }
郭盛 authored
978
            $param['video_id'] = $video_id;
郭盛 authored
979
            $param['createtime'] = time();
郭盛 authored
980
            $param['user_id'] = $user_id;
郭盛 authored
981 982 983 984 985
            $data = Db::name('car')
                ->insertGetId($param);
            if($data){
                $this->success('SUCCESS');
            }else{
郭盛 authored
986
                $this->error(['code'=>3,'msg'=>'加入购物车失败']);
郭盛 authored
987
            }
郭盛 authored
988
        }elseif (empty($video_id)){
989 990 991
            $data = Db::name('car')
                ->field('id')
                ->where('user_id',$user_id)
郭盛 authored
992
                ->where('pic_id',$pic_id)
993 994 995 996
                ->find();
            if(!empty($data)){
                $this->error(['code'=>2,'msg'=>'您已经添加过了']);
            }
郭盛 authored
997
            $param['pic_id'] = $pic_id;
郭盛 authored
998 999
            $param['createtime'] = time();
            $param['user_id'] = $user_id;
郭盛 authored
1000
            $data = Db::name('car')
郭盛 authored
1001
                ->insertGetId($param);
郭盛 authored
1002 1003 1004
            if($data){
                $this->success('SUCCESS');
            }else{
郭盛 authored
1005
                $this->error(['code'=>3,'msg'=>'加入购物车失败']);
郭盛 authored
1006
            }
郭盛 authored
1007 1008
        }else{
            $this->error(['code'=>2,'msg'=>'缺少必要参数']);
郭盛 authored
1009 1010 1011
        }
    }
郭盛 authored
1012
    /**
郭盛 authored
1013
     * @ApiTitle    (删除购物车商品)
郭盛 authored
1014
     * @ApiSummary  (删除购物车商品)
郭盛 authored
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/delcar)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="ids[]", type="inter", required=true, description="购物车ID")
     *
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    })
     */
    public function delcar()
    {
        $user_id = $this->uid;
        $ids = $this->request->param('ids');
        if(empty($ids)){
            $this->error(['code'=>2,'msg'=>'缺少必要参数']);
        }
        $data = Db::name('car')
            ->where('user_id',$user_id)
            ->whereIn('id',$ids)
            ->delete();
        if($data){
            $this->success('SUCCESS','删除成功');
        }else{
            $this->error(['code'=>2,'msg'=>'sql运行失败']);
        }


    }

    /**
郭盛 authored
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
     * @ApiTitle    (创建图片订单)
     * @ApiSummary  (创建图片订单)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/picorder)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="pic_id", type="inter", required=true, description="图片ID")
     * @ApiParams   (name="total", type="float", required=true, description="价格")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
        "order_id"://订单ID
    }
    })
     */
    public function picorder()
    {
        $param['user_id'] = $this->uid;
        $param['pic_id'] = $this->request->post('pic_id');
        if(empty($param['pic_id'])){
            $this->error(['code'=>2,'msg'=>'缺少必要参数']);
        }
        $param['total'] = $this->request->post('total');
        if(empty( $param['total'])){
            $this->error(['code'=>2,'msg'=>'缺少必要参数']);
        }elseif ( $param['total']<=0){
            $this->error(['code'=>3,'msg'=>'非法操作']);
        }else{
郭盛 authored
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
            $where['status'] = 2;
            $where['user_id'] = $param['user_id'];
            $arr = Db::name('order')
                ->field('id,pic_id')
                ->where($where)
                ->select();
            foreach ($arr as &$v){
                $v['pic_id'] = explode(',',$v['pic_id']);
                if(in_array($param['pic_id'],$v['pic_id'])){
                    $this->error(['code'=>2,'msg'=>'该图片已经购买过了']);
                }
            }
郭盛 authored
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
            $param['createtime'] = time();
            $param['num'] = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
            $data = Db::name('order')
                ->insertGetId($param);
            if(empty($data)){
                $this->error(['code'=>2,'msg'=>'sql执行失败']);
            }
            $this->success('SUCCESS',['order_id'=>$data]);
        }
    }

    /**
     * @ApiTitle    (创建视频订单)
     * @ApiSummary  (创建视频订单)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/v_order)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="video_id", type="inter", required=true, description="视频ID")
郭盛 authored
1111
     * @ApiParams   (name="video_attr", type="inter", required=true, description="视频品质属性")
郭盛 authored
1112 1113 1114
     * @ApiParams   (name="total", type="float", required=true, description="价格")
     *
     * @ApiReturn({
郭盛 authored
1115 1116 1117 1118 1119 1120 1121
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        "data": {
            "order_id"://订单ID
        }
        })
郭盛 authored
1122 1123 1124 1125
     */
    public function v_order()
    {
        $param['user_id'] = $this->uid;
郭盛 authored
1126 1127 1128 1129 1130 1131 1132 1133
        //接受视频ID并判断
        $video_id = $this->request->post('video_id');
        if(empty($video_id)){
            $this->error(['code'=>2,'msg'=>'缺少必要参数']);
        }
        //接受视频品质属性并判断
        $video_attr = $this->request->post('video_attr');
        if(empty($video_attr)){
郭盛 authored
1134 1135
            $this->error(['code'=>2,'msg'=>'缺少必要参数']);
        }
郭盛 authored
1136
        //接受视频价格并判断
郭盛 authored
1137 1138 1139 1140 1141 1142
        $param['total'] = $this->request->post('total');
        if(empty( $param['total'])){
            $this->error(['code'=>2,'msg'=>'缺少必要参数']);
        }elseif ( $param['total']<=0){
            $this->error(['code'=>3,'msg'=>'非法操作']);
        }else{
郭盛 authored
1143
            $a = [];
郭盛 authored
1144 1145
            $a[0]['id'] = $video_id;
            $a[0]['attr'] = $video_attr;
郭盛 authored
1146 1147
            $where['user_id'] = ['eq',$param['user_id']];
            $where['status'] = ['eq',2];
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157

            $res = Db::name('order')
                ->field('video_id')
                ->where($where)
                ->select();
            foreach ($res as &$v){
                $v['video_id'] = unserialize($v['video_id']);
                if(!empty($v['video_id'])){
                    foreach ($v['video_id'] as $v1){
                        if($v1['id'] == $video_id && $v1['attr'] == $video_attr){
郭盛 authored
1158
                            $this->error(['code'=>2,'msg'=>'该属性的视频已经购买过了','video_id'=>$v1['id'],'attr'=>$v1['attr']]);
1159 1160 1161
                        }
                    }
                }
郭盛 authored
1162
            }
郭盛 authored
1163
            $param['video_id'] = serialize($a);
郭盛 authored
1164 1165
            $param['createtime'] = time();
            $param['num'] = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
郭盛 authored
1166
            $data = Db::name('order')
郭盛 authored
1167 1168 1169 1170
                ->insertGetId($param);
            if(empty($data)){
                $this->error(['code'=>2,'msg'=>'sql执行失败']);
            }
郭盛 authored
1171
            $this->success('SUCCESS',['order_id'=>$data,'video_id'=>$video_id,'attr'=>$video_attr]);
郭盛 authored
1172 1173 1174
        }
    }
郭盛 authored
1175
    /**
郭盛 authored
1176 1177 1178 1179 1180 1181
     * @ApiTitle    (创建组合订单)
     * @ApiSummary  (创建组合订单)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/orders)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
郭盛 authored
1182 1183
     * @ApiParams   (name="video_id", type="string", required=true, description="视频ID(多个以逗号隔开)")
     * @ApiParams   (name="video_attr", type="string", required=true, description="视频品质属性(多个以逗号隔开)")
郭盛 authored
1184
     * @ApiParams   (name="pic_id", type="string", required=true, description="图片ID")
郭盛 authored
1185
     * @ApiParams   (name="total", type="float", required=true, description="总价格")
郭盛 authored
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        "data": {
            "order_id"://订单ID
        }
        })
     */
    public function orders()
    {
        $param['user_id'] = $this->uid;
郭盛 authored
1199
郭盛 authored
1200 1201 1202 1203 1204
        //接收视频ID加视频属性
        $video_ids = $this->request->post('video_id');
        $video_attrs = $this->request->post('video_attr');
        $video_id = explode(',',$video_ids);
        $video_attr = explode(',',$video_attrs);
郭盛 authored
1205
        $key = ['id','attr'];
郭盛 authored
1206 1207 1208

        //接收图片的ID
        $pic_id = explode(',',$this->request->post('pic_id'));
郭盛 authored
1209 1210 1211 1212

        $where['user_id'] = ['eq',$param['user_id']];
        $where['status'] = ['eq',2];
郭盛 authored
1213
        if(!empty($video_ids) && !empty($video_attrs)){
郭盛 authored
1214
            $a = $this->array_merge_more($key,$video_id,$video_attr);
郭盛 authored
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
            //查询视频ID
            $res = Db::name('order')
                ->field('video_id')
                ->where($where)
                ->select();
            foreach ($res as &$v){
                $v['video_id'] = unserialize($v['video_id']);
                if(!empty($v['video_id']) && !empty($a)){
                    foreach ($v['video_id'] as $v1){
                        foreach ($a as &$v2){
                            if($v1['id'] == $v2['id'] && $v1['attr'] == $v2['attr']){
                                $this->error(['code'=>2,'msg'=>'该属性的视频已经购买过了','video_id'=>$v2['id']]);
                            }
郭盛 authored
1228 1229 1230
                        }
                    }
                }
郭盛 authored
1231
            }
郭盛 authored
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
            $param['video_id'] = serialize($a);
        }else{
            //查询图片ID
            $arr = Db::name('order')
                ->field('pic_id')
                ->where($where)
                ->select();
            foreach ($arr as &$v){
                $v['pic_id'] = explode(',',$v['pic_id']);
                if(!empty($v['pic_id'])){
                    foreach ($pic_id as $v3){
                        if(in_array($v3,$v['pic_id'])){
                            $this->error(['code'=>2,'msg'=>'该图片已经购买过了','pic_id'=>$v3]);
                        }
郭盛 authored
1246 1247 1248 1249
                    }
                }
            }
        }
郭盛 authored
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
        $param['pic_id'] = $this->request->post('pic_id');
        $param['total'] = $this->request->post('total');
        if(empty( $param['total'])){
            $this->error(['code'=>2,'msg'=>'缺少必要参数']);
        }elseif ( $param['total']<=0){
            $this->error(['code'=>3,'msg'=>'非法操作']);
        }else{
            $param['createtime'] = time();
            $param['num'] = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
            $data = Db::name('order')
                ->insertGetId($param);
            if(empty($data)){
                $this->error(['code'=>2,'msg'=>'sql执行失败']);
            }
            $this->success('SUCCESS',['order_id'=>$data]);
        }
    }
郭盛 authored
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
    //将多个一维数组转为二维数组
    public function array_merge_more($keys, ...$arrs){
        // 检查参数是否正确
        if(!$keys || !is_array($keys) || !$arrs || !is_array($arrs) || count($keys)!=count($arrs)){
            return array();
        }
        // 一维数组中最大长度
        $max_len = 0;
        // 整理数据,把所有一维数组转重新索引
        for($i=0,$len=count($arrs); $i<$len; $i++){
            $arrs[$i] = array_values($arrs[$i]);

            if(count($arrs[$i])>$max_len){
                $max_len = count($arrs[$i]);
            }
        }
        // 合拼数据
        $result = array();
        for($i=0; $i<$max_len; $i++){
            $tmp = array();
            foreach($keys as $k=>$v){
                if(isset($arrs[$k][$i])){
                    $tmp[$v] = $arrs[$k][$i];
                }
            }
            $result[] = $tmp;
        }
        return $result;
    }
郭盛 authored
1298
    /**
郭盛 authored
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
     * @ApiTitle    (删除订单)
     * @ApiSummary  (删除订单)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/delorder)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="ids[]", type="inter", required=true, description="订单ID")
     *
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
    })
     */
    public function delorder()
    {
        $user_id = $this->uid;
        $ids = $this->request->param('ids');
        if(empty($ids)){
            $this->error(['code'=>2,'msg'=>'缺少必要参数']);
        }
        $data = Db::name('order')
            ->where('user_id',$user_id)
            ->whereIn('id',$ids)
            ->delete();
        if($data){
            $this->success('SUCCESS','删除成功');
        }else{
            $this->error(['code'=>2,'msg'=>'sql运行失败']);
        }


    }

    /**
郭盛 authored
1335 1336 1337 1338 1339 1340 1341
     * @ApiTitle    (我的订单)
     * @ApiSummary  (我的订单)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/myorder)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="type", type="inter", required=false, description="状态(不传默认为全部 1为待支付 2为已完成)")
郭盛 authored
1342 1343
     * @ApiParams   (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams   (name="pageNum", type="inter", required=false, description="每页显示数据个数(默认10)")
郭盛 authored
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        "data": {
            "id":       //用户ID
            "pic_id": //图片,
            "video_id":   //视频,
            "status" :   //状态(1待支付 2已完成)
郭盛 authored
1354
            "total"://总价格
郭盛 authored
1355 1356 1357 1358 1359 1360
        }
        })
     */
    public function myorder()
    {
        $user_id = $this->uid;
郭盛 authored
1361 1362
        $page = $this->request->param('page',1,'intval');
        $pageNum = $this->request->param('pageNum',10,'intval');
郭盛 authored
1363
        $type = $this->request->param('type');
郭盛 authored
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
        $res = Db::name('user')
            ->where('id',$user_id)
            ->field('id,is_vip')
            ->find();
        //确认为会员
        if($res['is_vip'] == 1){
            //判断状态
            if(empty($type)){
                //全部
                $res = Db::name('order')
                    ->where('user_id',$user_id)
                    ->field('id,pic_id,video_id,status,total')
                    ->order('createtime desc,id desc')
                    ->page($page,$pageNum)
                    ->select();
                $res = $this->fetch($res);
                $this->success('SUCCESS',$res);
            }elseif ($type == 1){
                //待支付
                $res = Db::name('order')
                    ->where('user_id',$user_id)
                    ->where('status',1)
                    ->field('id,pic_id,video_id,status,total')
                    ->order('createtime desc,id desc')
                    ->page($page,$pageNum)
                    ->select();
                $res = $this->fetch($res);
                $this->success('SUCCESS',$res);
            }else{
                //已完成
                $res = Db::name('order')
                    ->where('user_id',$user_id)
                    ->where('status',2)
                    ->field('id,pic_id,video_id,status,total')
                    ->order('createtime desc,id desc')
                    ->page($page,$pageNum)
                    ->select();
                $res = $this->fetch($res);
                $this->success('SUCCESS',$res);
            }
        }else{
            //不是会员的状态
            //判断状态
            if(empty($type)){
                $res = Db::name('order')
                    ->where('user_id',$user_id)
                    ->field('id,pic_id,video_id,status,total')
                    ->order('createtime desc,id desc')
                    ->page($page,$pageNum)
                    ->select();
                $res = $this->fetch1($res);
                $this->success('SUCCESS',$res);
            }elseif ($type == 1){
                $res = Db::name('order')
                    ->where('user_id',$user_id)
                    ->where('status',1)
                    ->field('id,pic_id,video_id,status,total')
                    ->order('createtime desc,id desc')
                    ->page($page,$pageNum)
                    ->select();
                $res = $this->fetch1($res);
                $this->success('SUCCESS',$res);
            }else{
                $res = Db::name('order')
                    ->where('user_id',$user_id)
                    ->where('status',2)
                    ->field('id,pic_id,video_id,status,total')
                    ->order('createtime desc,id desc')
                    ->page($page,$pageNum)
                    ->select();
                $res = $this->fetch1($res);
                $this->success('SUCCESS',$res);
            }
        }
    }
郭盛 authored
1439
郭盛 authored
1440 1441 1442 1443 1444 1445 1446 1447
    //会员
    public function fetch($res)
    {
        $rule = config('verify.path');
        foreach ($res as &$v){
            if(!empty($v['pic_id'])){
                $v['pic_id'] = explode(',',$v['pic_id']);
                foreach ($v['pic_id'] as &$v1){
郭盛 authored
1448
                    $v1 = Db::name('pic')->field('id,title,thumbnail,url,code,price')->where('id',$v1)->find();
郭盛 authored
1449 1450
                    $v1['thumbnail'] = $rule.$v1['thumbnail'];
                    $v1['vipprice'] = $v1['price']*0.75;
郭盛 authored
1451 1452
                }
            }
郭盛 authored
1453 1454
            if(!empty($v['video_id'])){
                $v['video_id'] = unserialize($v['video_id']);
郭盛 authored
1455 1456 1457 1458 1459 1460 1461 1462 1463
                foreach ($v['video_id'] as &$v2){
                    $v2['info'] = Db::name('video')->field('keyword,type_ids,content,is_recommend,buy_num,click_num,createtime,updatetime',true)->where('id',$v2['id'])->find();
                    $v2['info']['attr'] = $v2['attr'];
                    unset($v2['attr']);
                    $v2['info']['thumbnail'] = $rule.$v2['info']['thumbnail'];
                    $v2['info']['vipprice'] = $v2['info']['price']*0.75;
                    $v2['info']['four_vipprice'] = $v2['info']['four_price'] * 0.75;
                    $v2['info']['eight_vipprice'] = $v2['info']['eight_price'] * 0.75;
                }
郭盛 authored
1464
            }
郭盛 authored
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
        }
        return $res;
    }

    //非会员
    public function fetch1($res)
    {
        $rule = config('verify.path');
        foreach ($res as &$v){
            if(!empty($v['pic_id'])){
                $v['pic_id'] = explode(',',$v['pic_id']);
                foreach ($v['pic_id'] as &$v1){
郭盛 authored
1477
                    $v1 = Db::name('pic')->field('id,title,thumbnail,url,code,price')->where('id',$v1)->find();
郭盛 authored
1478
                    $v1['thumbnail'] = $rule.$v1['thumbnail'];
郭盛 authored
1479
                }
郭盛 authored
1480 1481 1482 1483 1484 1485 1486 1487
            }
            if(!empty($v['video_id'])){
                $v['video_id'] = unserialize($v['video_id']);
                foreach ($v['video_id'] as &$v2){
                    $v2['info'] = Db::name('video')->field('keyword,type_ids,content,is_recommend,buy_num,click_num,createtime,updatetime',true)->where('id',$v2['id'])->find();
                    $v2['info']['attr'] = $v2['attr'];
                    unset($v2['attr']);
                    $v2['info']['thumbnail'] = $rule.$v2['info']['thumbnail'];
郭盛 authored
1488 1489 1490
                }
            }
        }
郭盛 authored
1491
        return $res;
郭盛 authored
1492 1493
    }
郭盛 authored
1494 1495 1496 1497 1498 1499 1500
    /**
     * @ApiTitle    (消费记录)
     * @ApiSummary  (消费记录)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/payhistory)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
郭盛 authored
1501 1502 1503
     * @ApiParams   (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams   (name="pageNum", type="inter", required=false, description="每页显示数据个数(默认10)")
     *
郭盛 authored
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        "data": {
          "times"://时间分段,
          "info":[
                "id": 4,//id,
                "user_id"://用户ID
                "pic_id"://图片ID
                "video_id"://视频ID
          ],
          "total"://总价
        }
        })
     */
    public function payhistory()
    {
郭盛 authored
1522 1523
        $page = $this->request->param('page',1,'intval');
        $pageNum = $this->request->param('pageNum',10,'intval');
郭盛 authored
1524 1525
        $rule = config('verify.path');
        $user_id = $this->uid;
郭盛 authored
1526 1527 1528 1529
        $res = Db::name('user')
            ->where('id',$user_id)
            ->field('id,is_vip')
            ->find();
郭盛 authored
1530 1531 1532 1533 1534
        $data = Db::name('order')
            ->field('id,user_id,pic_id,video_id,total,createtime')
            ->where('user_id',$user_id)
            ->where('status',2)
            ->order('createtime desc,id desc')
郭盛 authored
1535
            ->page($page,$pageNum)
郭盛 authored
1536
            ->select();
郭盛 authored
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
        //判断该用户是否为会员
        if($res['is_vip'] == 1){
            //是的情况
            foreach ($data as &$v){
                $v['createtime'] = date('Y-m-d',$v['createtime']);
                if(!empty($v['pic_id'])){
                    $v['pic_id'] = explode(',',$v['pic_id']);
                    foreach ($v['pic_id'] as &$v1){
                        $v1 = Db::name('pic')->field('id,title,thumbnail,url,code,price')->where('id',$v1)->find();
                        $v1['thumbnail'] = $rule.$v1['thumbnail'];
                        $v1['vipprice'] = $v1['price']*0.75;
                    }
                }
                if(!empty($v['video_id'])){
                    $v['video_id'] = unserialize($v['video_id']);
                    foreach ($v['video_id'] as &$v2){
                        $v2['info'] = Db::name('video')->field('keyword,type_ids,content,is_recommend,buy_num,click_num,createtime,updatetime',true)->where('id',$v2['id'])->find();
                        $v2['info']['attr'] = $v2['attr'];
                        unset($v2['attr']);
                        $v2['info']['thumbnail'] = $rule.$v2['info']['thumbnail'];
                        $v2['info']['vipprice'] = $v2['info']['price']*0.75;
                        $v2['info']['four_vipprice'] = $v2['info']['four_price'] * 0.75;
                        $v2['info']['eight_vipprice'] = $v2['info']['eight_price'] * 0.75;
                    }
郭盛 authored
1561 1562
                }
            }
郭盛 authored
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581
        }else{
            //否的情况
            foreach ($data as &$v){
                $v['createtime'] = date('Y-m-d',$v['createtime']);
                if(!empty($v['pic_id'])){
                    $v['pic_id'] = explode(',',$v['pic_id']);
                    foreach ($v['pic_id'] as &$v1){
                        $v1 = Db::name('pic')->field('id,title,thumbnail,url,code,price')->where('id',$v1)->find();
                        $v1['thumbnail'] = $rule.$v1['thumbnail'];
                    }
                }
                if(!empty($v['video_id'])){
                    $v['video_id'] = unserialize($v['video_id']);
                    foreach ($v['video_id'] as &$v2){
                        $v2['info'] = Db::name('video')->field('keyword,type_ids,content,is_recommend,buy_num,click_num,createtime,updatetime',true)->where('id',$v2['id'])->find();
                        $v2['info']['attr'] = $v2['attr'];
                        unset($v2['attr']);
                        $v2['info']['thumbnail'] = $rule.$v2['info']['thumbnail'];
                    }
郭盛 authored
1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
                }
            }
        }
        //将用户购买时间数据分段
        $times = array_values(array_unique(array_column($data,'createtime')));
        rsort($times);
        //将数据放到对应的时间分段
        $arr = [];
        foreach ($times as $t_k=> $t_v){
            $arr[$t_k]['times'] = $t_v;
            $k = 0;
            foreach ($data as $value){
                $k+=0;
                if($t_v == $value['createtime']){
                    $arr[$t_k]['info'][$k] = $value;
                    $k++;
                }
            }
        }
        $this->success('SUCCESS',$arr);
    }
郭盛 authored
1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621
    /**
     * @ApiTitle    (获取sessionkey)
     * @ApiSummary  (获取sessionkey)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/getSessionKey)
     *
     * @ApiParams   (name="code", type="string", required=true, description="code")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data":[
        "session_key"://session_key
    ]
    })
     */
    public function getSessionKey(){
郭盛 authored
1622
        $rule = config('verify.raw');
郭盛 authored
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
        $validate = new Validate([
            'code'           => 'require',
        ]);

        $validate->message([
            'code.require'           => '缺少参数code!',
        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error(['code'=>'40003','msg'=>$validate->getError()]);
        }

        $code      = $data['code'];
        $appId = $rule['app_id'];
郭盛 authored
1638
郭盛 authored
1639 1640 1641
        $appSecret = $rule['secret'];

        $response = "https://api.weixin.qq.com/sns/jscode2session?appid=$appId&secret=$appSecret&js_code=$code&grant_type=authorization_code";
郭盛 authored
1642
        $response = $this->http_get($response);
郭盛 authored
1643 1644 1645 1646 1647 1648 1649
        $response = json_decode($response, true);
        if (!empty($response['errcode'])) {
            $this->error(['code'=>'2','msg'=>'操作失败:'.$response['errcode']]);
        }
        $this->success('获取成功',$response);
    }
郭盛 authored
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
    //curl  get请求
    public function http_get($url){
        $curl = curl_init();//启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL,$url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, false);//不开启header
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
        $result = curl_exec($curl); //执行操作
        curl_close($curl);
        return $result;
    }
郭盛 authored
1664 1665 1666 1667 1668 1669 1670
    /**
     * @ApiTitle    (获取用户手机号)
     * @ApiSummary  (获取用户手机号)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/getMobile)
     *
     * @ApiParams   (name="sessionKey", type="string", required=true, description="sessionkey")
郭盛 authored
1671
     * @ApiParams   (name="encryptedData", type="string", required=true, description="encryptedDate")
郭盛 authored
1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684
     * @ApiParams   (name="iv", type="string", required=true, description="iv")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
          "data":[
               "phoneNumber"://手机号
          ]
        })
     */
    public function getMobile()
    {
郭盛 authored
1685
        $appid = 'wx4300d933e5abd5ba';
郭盛 authored
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702
        $sessionKey = $this->request->param('sessionKey');
        $encryptedData = $this->request->param('encryptedData');
        $iv = $this->request->param('iv');
        $errCode = $this->decryptData($appid,$sessionKey,$encryptedData, $iv, $data );
        if ($errCode == 0) {
            $return['tel'] = $data->phoneNumber;
            $return['tel'] = json_encode($return['tel']);
            $return['tel'] = json_decode($return['tel'],true);
        } else {
            $return['code'] = 301;
            $return['data'] = $errCode;
            $return['code'] = json_encode($return['code']);
            $return['code'] = json_decode($return['code'],true);
            $return['data'] = json_encode($return['data']);
            $return['data'] = json_decode($return['data'],true);
        }
郭盛 authored
1703
        $this->success('SUCCESS',$return);
郭盛 authored
1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734
    }

    private function decryptData( $appid,$sessionKey,$encryptedData, $iv, &$data )
    {
        if (strlen($sessionKey) != 24) {
            return -41001;
        }
        $aesKey=base64_decode($sessionKey);

        if (strlen($iv) != 24) {
            return -41002;
        }
        $aesIV=base64_decode($iv);

        $aesCipher=base64_decode($encryptedData);

        $result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);

        $dataObj=json_decode( $result );
        if( $dataObj  == NULL )
        {
            return -41003;
        }
        if( $dataObj->watermark->appid != $appid )
        {
            return -41004;
        }
        $data = $dataObj;
        return 0;
    }
郭盛 authored
1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753
    /**
     * @ApiTitle    (绑定用户手机号)
     * @ApiSummary  (绑定用户手机号)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/phone)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="phone", type="string", required=true, description="手机号")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        })
     */
    public function phone()
    {
        $user_id = $this->uid;
郭盛 authored
1754 1755 1756 1757
        $phone = $this->request->post('phone');
        if(empty($phone)){
            $this->error(['code'=>2,'msg'=>'缺少必要参数']);
        }
郭盛 authored
1758 1759 1760
        $data = Db::name('user')
            ->where('id',$user_id)
            ->update(['mobile'=>$phone]);
郭盛 authored
1761 1762 1763 1764 1765
        if(!empty($data)){
            $this->success('SUCCESS',$data);
        }else{
            $this->error(['code'=>2,'msg'=>'sql运行失败']);
        }
郭盛 authored
1766
    }
郭盛 authored
1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778


    /**
     * @ApiTitle    (填写发票信息)
     * @ApiSummary  (填写发票信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/invoice)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="order_id", type="inter", required=true, description="订单ID")
     * @ApiParams   (name="name", type="string", required=true, description="公司名称")
郭盛 authored
1779 1780
     * @ApiParams   (name="tel", type="string", required=true, description="公司电话")
     * @ApiParams   (name="adress", type="string", required=true, description="公司地址")
郭盛 authored
1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
     * @ApiParams   (name="duty", type="string", required=true, description="纳税人识别号")
     * @ApiParams   (name="bank", type="string", required=true, description="银行名称")
     * @ApiParams   (name="card", type="string", required=true, description="银行账号")
     * @ApiParams   (name="linkname", type="string", required=true, description="收件人名字")
     * @ApiParams   (name="linktel", type="string", required=true, description="收件人电话")
     * @ApiParams   (name="linkadress", type="string", required=true, description="收件人地址")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        })
     */
    public function invoice()
    {
        $param = $this->request->param();
郭盛 authored
1797
        $param['user_id'] = $this->uid;
郭盛 authored
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824
        $validate = new Validate([
            'order_id'=>'require',
            'name'=>'require',
            'tel'=>'require',
            'adress'=>'require',
            'duty'=>'require',
            'bank'=>'require',
            'card'=>'require',
            'linkname'=>'require',
            'linktel'=>'require',
            'linkadress'=>'require',
        ]);
        $validate->message([
            'order_id'=>'订单ID不能为空',
            'name'=>'公司名称不能为空',
            'tel'=>'公司电话不能为空',
            'adress'=>'公司地址不能为空',
            'duty'=>'纳税人识别号不能为空',
            'bank'=>'银行名称不能为空',
            'card'=>'银行账号不能为空',
            'linkname'=>'收件人姓名不能为空',
            'linktel'=>'收件人电话不能为空',
            'linkadress'=>'收件人地址不能为空',
        ]);
        if (!$validate->check($param)) {
            $this->error($validate->getError());
        }
郭盛 authored
1825
        $info = Db::name('invoice')
郭盛 authored
1826
            ->whereLike('order_id',"%$param[order_id]%")
郭盛 authored
1827 1828 1829 1830
            ->find();
        if(!empty($info)){
            $this->error(['code'=>2,'msg'=>'您已经提交过发票了']);
        }
郭盛 authored
1831 1832 1833 1834
        $param['createtime'] = time();
        $data = Db::name('invoice')
            ->insertGetId($param);
        if(!empty($data)){
郭盛 authored
1835 1836 1837 1838 1839 1840 1841
            $res = Db::name('invoice')
                ->where('id',$data)
                ->find();
            $order_id = explode(',',$res['order_id']);
            Db::name('order')
                ->whereIn('id',$order_id)
                ->update(['invoice_status'=>1]);
郭盛 authored
1842 1843 1844 1845 1846
            $this->success('success');
        }else{
            $this->error('sql运行失败');
        }
    }
郭盛 authored
1847
}