User.php 40.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 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 467 468 469 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 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 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 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 756 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 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 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 839 840 841 842 843 844 845 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 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 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 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 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
<?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;
use fast\Http;

/**
 * 个人中心
 */
class User extends Api
{
    protected $noNeedLogin = ['login'];
    protected $noNeedRight = '*';
    protected $uid = '';

    public function _initialize()
    {
        parent::_initialize();
        $this->uid = $this->auth->getUserId();
    }

    /**
     * @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');
            $validate = new Validate($rule['rule'],$rule['msg']);
            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('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (首页视频、图片、精美大片入口)
     * @ApiSummary  (首页视频、图片、精美大片入口)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/index)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "time": "1571492001",
        "data": {
        "video_thumbnail": http://mapwww.baidu.com 视频封面图地址
        "pic_thumbnail": http://mapwww.baidu.com 图片地址
        "wonderful_thumbnail": http://mapwww.baidu.com 精美大片地址
        }
    })
     */
    public function index()
    {
        $rule = config('verify.path');
        $data = Db::name('video')
            ->field('id,thumbnail')
            ->order('id desc')
            ->limit('1')
            ->find();
        $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);
    }

    /**
     * @ApiTitle    (地区分类)
     * @ApiSummary  (地区分类)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/type)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
        "id": 4,//分类id
        "area_name": //分类名称
        "thumbnail": //图片
        }
    })
     */
    public function type()
    {
        $rule = config('verify.path');
        $data = Db::name('type')
            ->field('id,area_name,thumbnail')
            ->order('id desc')
            ->select();
        foreach ($data as &$v){
            $v['thumbnail'] = $rule.$v['thumbnail'];
        }
        $this->success('SUCCESS',$data);
    }

    /**
     * @ApiTitle    (搜索页)
     * @ApiSummary  (搜索页)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/search)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="type_id", type="inter", required=false, description="视频/图片  (视频为1 图片为2 精美大片为3)")
     * @ApiParams   (name="keyword", type="string", required=true, description="关键字")
     * @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/图片id,
            "thumbnail": //视频封面图地址/图片地址,
            "video": //视频路径/精美大片视频路径,
        }
    })
     */
    public function search()
    {
        $user_id = $this->uid;
        $page = $this->request->param('page',1,'intval');
        $pageNum = $this->request->param('pageNum',10,'intval');
        $rule = config('verify.path');
        $type_id = $this->request->post('type_id');
        if(empty($type_id)){
            $this->error(['code'=>2,'msg'=>'缺少必要参数']);
        }
        $keyword = $this->request->post('keyword');
        if(!empty($keyword)){
            $param['user_id'] = $user_id;
            $param['title'] = $keyword;
            Db::name('history')
                ->insert($param);
        }
        if(!empty($keyword)){
            if($type_id == 1){
                $data = Db::name('video')
                    ->where('keyword','like',"%$keyword%")
                    ->whereOr('title','like',"%$keyword%")
                    ->order('id desc')
                    ->page($page,$pageNum)
                    ->select();
                foreach ($data as &$v){
                    $v['thumbnail'] = $rule.$v['thumbnail'];
                    $v['video'] = $rule.$v['video'];
                }
                $this->success('SUCCESS',$data);
        }elseif($type_id == 2){
                $data = Db::name('pic')
                    ->where('keyword','like',"%$keyword%")
                    ->whereOr('title','like',"%$keyword%")
                    ->order('id desc')
                    ->page($page,$pageNum)
                    ->select();
                foreach ($data as &$v){
                    $v['thumbnail'] = $rule.$v['thumbnail'];
                }
                $this->success('SUCCESS',$data);
            }else{
                $data = Db::name('wonderful')
                    ->where('title','like',"%$keyword%")
                    ->order('id desc')
                    ->page($page,$pageNum)
                    ->select();
                foreach ($data as &$v){
                    $v['thumbnail'] = $rule.$v['thumbnail'];
                    $v['video'] = $rule.$v['video'];
                }
                $this->success('SUCCESS',$data);
            }
        }else{
            $this->error(['code'=>2,'msg'=>'请输入关键字']);
        }

    }

    /**
     * @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")
     * @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()
    {
        $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');
        if(empty($type_id)){
            $data = Db::name('pic')
                ->field('id,thumbnail')
                ->order('id desc')
                ->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('pic')
                ->where($where)
                ->field('id,thumbnail')
                ->order('id desc')
                ->page($page,$pageNum)
                ->select();
            foreach ($data as &$v){
                $v['thumbnail'] = $rule.$v['thumbnail'];
            }
            $this->success('SUCCESS',$data);
        }
    }

    /**
     * @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")
     * @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 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');
        if(empty($type_id)){
            $data = Db::name('video')
                ->field('id,thumbnail,video')
                ->order('id desc')
                ->page($page,$pageNum)
                ->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')
                ->page($page,$pageNum)
                ->select();
            foreach ($data as &$v){
                $v['thumbnail'] = $rule.$v['thumbnail'];
                $v['video'] = $rule.$v['video'];
            }
            $this->success('SUCCESS',$data);
        }
    }

    /**
     * @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');
        $data = Db::name('wonderful')
            ->field('id,thumbnail,video')
            ->order('id desc')
            ->page($page,$pageNum)
            ->select();
        foreach ($data as &$v){
            $v['thumbnail'] = $rule.$v['thumbnail'];
            $v['video'] = $rule.$v['video'];
        }
        $this->success('SUCCESS',$data);

    }

    /**
     * @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,
            "title"://标题
            "thumbnail": //缩略图,
            "water_url": //小样带水印的图片地址,
           "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')
                ->field('id,title,thumbnail,price,water_url,content')
                ->where('id',$pic_id)
                ->find();
            if(empty($data)){
                $this->error(['code'=>2,'msg'=>'参数错误']);
            }
            $data['water_url'] = $rule.$data['water_url'];
            $data['vip_price'] = $data['price'] * 0.75;
            $data['thumbnail'] = $rule.$data['thumbnail'];
            $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('pic')
                ->field('id,title,thumbnail,price,water_url,content')
                ->where('id',$pic_id)
                ->find();
            if(empty($data)){
                $this->error(['code'=>2,'msg'=>'参数错误']);
            }
            $data['is_vip'] = 1;
            $data['water_url'] = $rule.$data['water_url'];
            $data['vip_price'] = $data['price'] * 0.75;
            $data['thumbnail'] = $rule.$data['thumbnail'];
            $data['content'] = strip_tags($data['content']);
            $this->success('SUCCESS',$data);
        }
    }

    /**
     * @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价格,
            "four_price"://4K价格,
            "four_vipprice"://4Kvip价格,
            "eight_price"://8K价格,
            "eight_vipprice"://8Kvip价格,
            "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')
                ->field('id,title,thumbnail,video,price,four_price,eight_price,water_url,content')
                ->where('id',$pic_id)
                ->find();
            if(empty($data)){
                $this->error(['code'=>2,'msg'=>'参数错误']);
            }
            $data['vip_price'] = $data['price']*0.75;
            $data['four_vipprice'] = $data['four_price']*0.75;
            $data['eight_vipprice'] = $data['eight_price']*0.75;
            $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){
            $pic_id = $this->request->post('video_id');
            if(empty($pic_id)){
                $this->error(['code'=>2,'msg'=>'缺少必要参数']);
            }
            $data = Db::name('video')
                ->field('id,title,thumbnail,video,price,four_price,eight_price,water_url,content')
                ->where('id',$pic_id)
                ->find();
            if(empty($data)){
                $this->error(['code'=>2,'msg'=>'参数错误']);
            }
            $data['is_vip'] = 1;
            $data['vip_price'] = $data['price']*0.75;
            $data['four_vipprice'] = $data['four_price']*0.75;
            $data['eight_vipprice'] = $data['eight_price']*0.75;
            $data['thumbnail'] = $rule.$data['thumbnail'];
            $data['video'] = $rule.$data['video'];
            $data['content'] = strip_tags($data['content']);
            $this->success('SUCCESS',$data);
        }
    }

    /**
     * @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);
    }

    /**
     * @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/vipdesc)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
        "content":       //会员介绍内容
    }
    })
     */
    public function vipdesc()
    {
        $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')
            ->field('text')
            ->where('id',2)
            ->find();
        $this->success('SUCCESS',$data);
    }

    /**
     * @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;
        $rule = config('verify.path');
        //查询该用户是否为会员
        $res = Db::name('user')
            ->where('id',$user_id)
            ->field('id,is_vip')
            ->find();
        //判断
        if($res['is_vip'] == 0){
            //不是会员的情况
            $data = Db::name('car')
                ->where('user_id',$user_id)
                ->field('id,user_id,pic_id,video_id')
                ->order('createtime desc,id desc')
                ->select();
            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'];
                }
                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'];
                }
            }
            $this->success('SUCCESS',$data);
        }else{
            //是的情况
            $data = Db::name('car')
                ->where('user_id',$user_id)
                ->field('id,user_id,pic_id,video_id')
                ->order('createtime desc,id desc')
                ->select();
            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'];
                    $v['pic_id']['vipprice'] = $v['pic_id']['price']*0.75;
                }
                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;
                }
            }
            $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")
     *
     *
     * @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');
        if(empty($pic_id)){
            $param['video_id'] = $this->request->param('video_id');
            $param['createtime'] = time();
            $param['user_id'] = $user_id;
            $data = Db::name('car')
                ->insertGetId($param);
            if($data){
                $this->success('SUCCESS');
            }else{
                $this->error(['code'=>3,'msg'=>'加入购物车失败']);
            }
        }elseif (empty($video_id)){
            $param['pic_id'] = $this->request->param('pic_id');
            $param['createtime'] = time();
            $param['user_id'] = $user_id;
            $data = Db::name('car')
                ->insertGetId($param);
            if($data){
                $this->success('SUCCESS');
            }else{
                $this->error(['code'=>3,'msg'=>'加入购物车失败']);
            }
        }else{
            $this->error(['code'=>2,'msg'=>'缺少必要参数']);
        }
    }

    /**
     * @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{
            $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")
     * @ApiParams   (name="total", type="float", required=true, description="价格")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
    "order_id"://订单ID
    }
    })
     */
    public function v_order()
    {
        $param['user_id'] = $this->uid;
        $param['video_id'] = $this->request->post('video_id');
        if(empty($param['video_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{
            $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/orders)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="video_id", type="string", required=true, description="视频ID")
     * @ApiParams   (name="pic_id", type="string", required=true, description="图片ID")
     * @ApiParams   (name="total", type="float", required=true, description="价格")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        "data": {
            "order_id"://订单ID
        }
        })
     */
    public function orders()
    {
        $param['user_id'] = $this->uid;
        $param['video_id'] = $this->request->post('video_id');
        $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]);
        }
    }

    /**
     * @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为已完成)")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        "data": {
            "id":       //用户ID
            "pic_id": //图片,
            "video_id":   //视频,
            "status" :   //状态(1待支付 2已完成)
        }
        })
     */
    public function myorder()
    {
        $user_id = $this->uid;
        $rule = config('verify.path');
        $type = $this->request->param('type');
        if(empty($type)){
            $res = Db::name('order')
                ->where('user_id',$user_id)
                ->field('id,pic_id,video_id,status')
                ->order('createtime desc,id desc')
                ->select();
            foreach ($res as &$v){
                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,price')->where('id',$v1)->find();
                        $v1['thumbnail'] = $rule.$v1['thumbnail'];
                        $v1['vipprice'] = $v1['price']*0.75;
                    }
                }
                if(!empty($v['video_id'])){
                    $v['video_id'] = explode(',',$v['video_id']);
                    foreach ($v['video_id'] as &$v2){
                        $v2 = Db::name('video')->field('keyword,type_ids,content,is_recommend,buy_num,click_num,createtime,updatetime',true)->where('id',$v2)->find();
                        $v2['thumbnail'] = $rule.$v2['thumbnail'];
                        $v2['video'] = $rule.$v2['video'];
                        $v2['vipprice'] = $v2['price']*0.75;
                        $v2['four_vipprice'] = $v2['four_price'] * 0.75;
                        $v2['eight_vipprice'] = $v2['eight_price'] * 0.75;
                    }
                }
            }
            $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')
                ->order('createtime desc,id desc')
                ->select();
            foreach ($res as &$v){
                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,price')->where('id',$v1)->find();
                        $v1['thumbnail'] = $rule.$v1['thumbnail'];
                        $v1['vipprice'] = $v1['price']*0.75;
                    }
                }
                if(!empty($v['video_id'])){
                    $v['video_id'] = explode(',',$v['video_id']);
                    foreach ($v['video_id'] as &$v2){
                        $v2 = Db::name('video')->field('keyword,type_ids,content,is_recommend,buy_num,click_num,createtime,updatetime',true)->where('id',$v2)->find();
                        $v2['thumbnail'] = $rule.$v2['thumbnail'];
                        $v2['video'] = $rule.$v2['video'];
                        $v2['vipprice'] = $v2['price']*0.75;
                        $v2['four_vipprice'] = $v2['four_price'] * 0.75;
                        $v2['eight_vipprice'] = $v2['eight_price'] * 0.75;
                    }
                }
            }
            $this->success('SUCCESS',$res);
        }else{
            $res = Db::name('order')
                ->where('user_id',$user_id)
                ->where('status',2)
                ->field('id,pic_id,video_id,status')
                ->order('createtime desc,id desc')
                ->select();
            foreach ($res as &$v){
                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,price')->where('id',$v1)->find();
                        $v1['thumbnail'] = $rule.$v1['thumbnail'];
                        $v1['vipprice'] = $v1['price']*0.75;
                    }
                }
                if(!empty($v['video_id'])){
                    $v['video_id'] = explode(',',$v['video_id']);
                    foreach ($v['video_id'] as &$v2){
                        $v2 = Db::name('video')->field('keyword,type_ids,content,is_recommend,buy_num,click_num,createtime,updatetime',true)->where('id',$v2)->find();
                        $v2['thumbnail'] = $rule.$v2['thumbnail'];
                        $v2['video'] = $rule.$v2['video'];
                        $v2['vipprice'] = $v2['price']*0.75;
                        $v2['four_vipprice'] = $v2['four_price'] * 0.75;
                        $v2['eight_vipprice'] = $v2['eight_price'] * 0.75;
                    }
                }
            }
            $this->success('SUCCESS',$res);
        }

    }

    /**
     * @ApiTitle    (消费记录)
     * @ApiSummary  (消费记录)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/payhistory)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @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()
    {
        $rule = config('verify.path');
        $user_id = $this->uid;
        $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')
            ->select();
        foreach ($data as &$v){
            $v['createtime'] = date('Y-m-d',$v['createtime']);
        }
        foreach ($data as &$v){
            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,price')->where('id',$v1)->find();
                    $v1['thumbnail'] = $rule.$v1['thumbnail'];
                    $v1['vipprice'] = $v1['price']*0.75;
                }
            }
            if(!empty($v['video_id'])){
                $v['video_id'] = explode(',',$v['video_id']);
                foreach ($v['video_id'] as &$v2){
                    $v2 = Db::name('video')->field('keyword,type_ids,content,is_recommend,buy_num,click_num,createtime,updatetime',true)->where('id',$v2)->find();
                    $v2['thumbnail'] = $rule.$v2['thumbnail'];
                    $v2['video'] = $rule.$v2['video'];
                    $v2['vipprice'] = $v2['price']*0.75;
                    $v2['four_vipprice'] = $v2['four_price'] * 0.75;
                    $v2['eight_vipprice'] = $v2['eight_price'] * 0.75;
                }
            }
        }
        //将用户购买时间数据分段
        $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);
    }


}