审查视图

application/api/controller/User.php 19.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'];
郭盛 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 114 115 116
        $data = Db::name('video')
            ->field('id,thumbnail')
            ->order('id desc')
            ->limit('1')
            ->find();
郭盛 authored
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
        $pic = Db::name('pic')
            ->field('id,thumbnail')
            ->order('id desc')
            ->limit('1')
            ->find();
        $wonderful = Db::name('wonderful')
            ->field('id,thumbnail')
            ->order('id desc')
            ->limit('1')
            ->find();
        $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
132 133
    }
郭盛 authored
134 135 136
    /**
     * @ApiTitle    (地区分类)
     * @ApiSummary  (地区分类)
郭盛 authored
137
     * @ApiMethod   (POST)
郭盛 authored
138 139 140 141 142 143 144 145
     * @ApiRoute    (/api/user/type)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
郭盛 authored
146 147 148 149
        "id": 4,//分类id
        "area_name": //分类名称
        "thumbnail": //图片
        }
郭盛 authored
150 151 152 153
    })
     */
    public function type()
    {
郭盛 authored
154
        $rule = config('verify.path');
郭盛 authored
155 156 157 158
        $data = Db::name('type')
            ->field('id,area_name,thumbnail')
            ->order('id desc')
            ->select();
郭盛 authored
159 160 161
        foreach ($data as &$v){
            $v['thumbnail'] = $rule.$v['thumbnail'];
        }
郭盛 authored
162 163 164
        $this->success('SUCCESS',$data);
    }
郭盛 authored
165 166 167 168 169 170
    /**
     * @ApiTitle    (搜索页)
     * @ApiSummary  (搜索页)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/search)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
郭盛 authored
171
     *
郭盛 authored
172
     * @ApiParams   (name="type_id", type="inter", required=false, description="视频/图片  (视频为1 图片为2 精美大片为3)")
郭盛 authored
173 174
     * @ApiParams   (name="keyword", type="string", required=true, description="关键字")
     * @ApiReturn({
郭盛 authored
175 176 177 178 179 180 181 182
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        "data": {
            "id": 4,//视频id/图片id,
            "thumbnail": //视频封面图地址/图片地址,
            "video": //视频路径/精美大片视频路径,
        }
郭盛 authored
183 184 185 186
    })
     */
    public function search()
    {
郭盛 authored
187
        $user_id = $this->uid;
郭盛 authored
188 189
        $rule = config('verify.path');
        $type_id = $this->request->post('type_id');
郭盛 authored
190 191 192
        if(empty($type_id)){
            $this->error(['code'=>2,'msg'=>'缺少必要参数']);
        }
郭盛 authored
193 194
        $keyword = $this->request->post('keyword');
        if(!empty($keyword)){
郭盛 authored
195 196 197 198 199 200
            $param['user_id'] = $user_id;
            $param['title'] = $keyword;
            Db::name('history')
                ->insert($param);
        }
        if(!empty($keyword)){
郭盛 authored
201 202
            if($type_id == 1){
                $data = Db::name('video')
郭盛 authored
203 204
                    ->where('keyword','like',"%$keyword%")
                    ->whereOr('title','like',"%$keyword%")
郭盛 authored
205 206 207 208 209 210
                    ->order('id desc')
                    ->select();
                foreach ($data as &$v){
                    $v['thumbnail'] = $rule.$v['thumbnail'];
                    $v['video'] = $rule.$v['video'];
                }
郭盛 authored
211
                $this->success('SUCCESS',$data);
郭盛 authored
212
        }elseif($type_id == 2){
郭盛 authored
213
                $data = Db::name('pic')
郭盛 authored
214 215
                    ->where('keyword','like',"%$keyword%")
                    ->whereOr('title','like',"%$keyword%")
郭盛 authored
216 217 218 219 220 221 222 223
                    ->order('id desc')
                    ->select();
                foreach ($data as &$v){
                    $v['thumbnail'] = $rule.$v['thumbnail'];
                }
                $this->success('SUCCESS',$data);
            }else{
                $data = Db::name('wonderful')
郭盛 authored
224
                    ->where('title','like',"%$keyword%")
郭盛 authored
225 226 227 228 229 230 231
                    ->order('id desc')
                    ->select();
                foreach ($data as &$v){
                    $v['thumbnail'] = $rule.$v['thumbnail'];
                    $v['video'] = $rule.$v['video'];
                }
                $this->success('SUCCESS',$data);
郭盛 authored
232 233 234 235 236 237 238
            }
        }else{
            $this->error(['code'=>2,'msg'=>'请输入关键字']);
        }

    }
郭盛 authored
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
    /**
     * @ApiTitle    (图片列表)
     * @ApiSummary  (图片列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/pic)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="type_id", type="inter", required=false, description="地区ID")
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
    "id": 4,//图片id,
    "thumbnail": //缩略图,
    }
    })
     */
    public function pic()
    {
        $rule = config('verify.path');
        $type_id = $this->request->post('type_id');
        if(empty($type_id)){
            $data = Db::name('pic')
                ->field('id,thumbnail')
                ->order('id desc')
                ->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('pic')
                ->where($where)
                ->field('id,thumbnail')
                ->order('id desc')
                ->select();
            foreach ($data as &$v){
                $v['thumbnail'] = $rule.$v['thumbnail'];
            }
            $this->success('SUCCESS',$data);
        }
    }
郭盛 authored
283
郭盛 authored
284
    /**
郭盛 authored
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
     * @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")
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        "data": {
            "id": //图片id,
            "thumbnail": //视频封面,
            "video"://视频地址
        }
        })
     */
    public function video()
    {
        $rule = config('verify.path');
        $type_id = $this->request->post('type_id');
        if(empty($type_id)){
            $data = Db::name('video')
                ->field('id,thumbnail,video')
                ->order('id desc')
                ->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')
                ->select();
            foreach ($data as &$v){
                $v['thumbnail'] = $rule.$v['thumbnail'];
                $v['video'] = $rule.$v['video'];
            }
            $this->success('SUCCESS',$data);
        }
    }

    /**
郭盛 authored
333 334 335 336 337 338 339 340 341 342 343 344 345
     * @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
346
            "title"://标题
郭盛 authored
347
            "thumbnail": //缩略图,
郭盛 authored
348
            "water_url": //小样带水印的图片地址,
郭盛 authored
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
           "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'=>'缺少必要参数']);
            }
            $data = Db::name('pic')
郭盛 authored
371
                ->field('id,title,thumbnail,price,vip_price,water_url,content')
郭盛 authored
372 373 374
                ->where('id',$pic_id)
                ->find();
            $data['thumbnail'] = $rule.$data['thumbnail'];
郭盛 authored
375
            $data['water_url'] = $rule.$data['water_url'];
郭盛 authored
376
            $data['content'] = strip_tags($data['content']);
郭盛 authored
377 378 379 380 381 382 383 384
            $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
385
                ->field('id,title,thumbnail,price,vip_price,water_url,content')
郭盛 authored
386 387 388 389
                ->where('id',$pic_id)
                ->find();
            $data['is_vip'] = 1;
            $data['thumbnail'] = $rule.$data['thumbnail'];
郭盛 authored
390
            $data['water_url'] = $rule.$data['water_url'];
郭盛 authored
391
            $data['content'] = strip_tags($data['content']);
郭盛 authored
392 393 394 395
            $this->success('SUCCESS',$data);
        }
    }
郭盛 authored
396
    /**
郭盛 authored
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
     * @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"://标题
            "thumbnail": //视频封面图,
            "video": //2K视频地址,
            "water_url"://小样视频网址
            "price"://2K价格,
            "vip_price"://2Kvip价格,
郭盛 authored
416 417 418 419
            "four_price"://4K价格,
            "four_vipprice"://4Kvip价格,
            "eight_price"://8K价格,
            "eight_vipprice"://8Kvip价格,
郭盛 authored
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
            "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
439
                ->field('id,title,thumbnail,video,price,four_price,eight_price,water_url,content')
郭盛 authored
440 441
                ->where('id',$pic_id)
                ->find();
郭盛 authored
442 443 444
            $data['vip_price'] = $data['price']*0.75;
            $data['four_vipprice'] = $data['four_price']*0.75;
            $data['eight_vipprice'] = $data['eight_price']*0.75;
郭盛 authored
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
            $data['thumbnail'] = $rule.$data['thumbnail'];
            $data['video'] = $rule.$data['video'];
            $data['water_url'] = $rule.$data['water_url'];
            $data['content'] = strip_tags($data['content']);
            $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('video')
                ->field('id,title,thumbnail,video,price,vip_price,water_url,content')
                ->where('id',$pic_id)
                ->find();
            $data['is_vip'] = 1;
            $data['thumbnail'] = $rule.$data['thumbnail'];
            $data['video'] = $rule.$data['video'];
            $data['water_url'] = $rule.$data['water_url'];
            $data['content'] = strip_tags($data['content']);
            $this->success('SUCCESS',$data);
        }
    }

    /**
郭盛 authored
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
     * @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')
            ->limit(10)
            ->select();
        $this->success('SUCCESS',$data);
    }
郭盛 authored
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
    /**
     * @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]);
        }

    }

    /**
     * @ApiTitle    (充值支付)
     * @ApiSummary  (充值支付)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/pay)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="order_id", type="inter", required=true, description="订单id")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        })
     */
    public function pay()
    {
        $user_id = $this->uid;
        $id = $this->request->post('order_id');
        if(empty($id)){
            $this->error(['code'=>2,'msg'=>'确实必要参数']);
        }
        $where['id'] = ['eq',$id];
        $where['user_id'] = ['eq',$user_id];
        $data = Db::name('toporder')->where($where)->find();
        if(empty($data)){
            $this->error(['code'=>41001,'msg'=>'数据错误']);
        }
        $openid = $this->getopenid();
        $this->success('SUCCESS');
    }

    public function getopenid()
    {
        $user_id = $this->uid;
        $openid = Db::name('user')
            ->where('id',$user_id)
            ->field('openid')
            ->find();

        return $openid;
    }
郭盛 authored
609
}