审查视图

application/api/controller/User.php 20.8 KB
jinglong authored
1 2 3 4
<?php

namespace app\api\controller;
jinglong authored
5
use app\admin\model\Collection;
jinglong authored
6
use app\admin\model\Rcoupon;
jinglong authored
7
use app\common\controller\Api;
8
use fast\Http;
jinglong authored
9
use think\db\Query;
jinglong authored
10
use think\Validate;
11
use think\Db;
jinglong authored
12
use think\db\Expression;
jinglong authored
13 14

/**
15
 * 个人中心接口
jinglong authored
16 17 18
 */
class User extends Api
{
19 20 21
    //无需登录,*表都不需要
//    protected $noNeedLogin = ['login', 'mobilelogin'];
    protected  $noNeedLogin = ['login'];
jinglong authored
22
    protected $noNeedRight = '*';
23
    protected $uid = '';
jinglong authored
24 25 26
    public function _initialize()
    {
        parent::_initialize();
27
        $this->uid = $this->auth->getUserId();
jinglong authored
28 29 30
    }

    /**
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
     * @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
            })
jinglong authored
50
     */
51 52 53 54 55 56 57 58 59 60 61
    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());
jinglong authored
62
            }
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
            $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();
jinglong authored
81
                        $data['nickname'] = $this->auth->emoji_decode($data['nickname']);
jinglong authored
82 83 84

                        //增加访客数
                        $type = config('verify.browse_type')[0];
jinglong authored
85
                        Common::statistics($type,$data['user_id']);
86 87 88 89 90 91 92
                        $this->success('登录成功', $data);
                    }else {
                        $this->error($this->auth->getError());
                    }
                } else {
                    $this->error("登录失败",$json);
                }
jinglong authored
93
            }
94 95
        }else{
            $this->error('请求方式错误');
jinglong authored
96 97
        }
    }
jinglong authored
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

    /**
     * @ApiTitle    (获取个人信息(新人/旧人))
     * @ApiSummary  (获取个人信息(新人/旧人))
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/user/info)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1575188985",
        "data": {
            "id": 2,//用户id
            "openid": "orhy25PeYJsTeN70aoTL_Hvfvz20",//openid
            "nickname": "风起时",//昵称
            "mobile": "",
            "avatar": "",//头像
            "token": "2794e6d4-14d5-4725-ba79-8b863d3ef8c6",//token
            "user_id": 2,
            "createtime": 1574927973,
            "expiretime": 1577519973,
            "expires_in": 2330988,
            "is_news": 0//是否为新人(0:是,1:否)
        }
    })
     */
    public function info(){
        if($this->request->isGet()){
            $data = $this->auth->getUserinfo();
            if($data){
jinglong authored
128
                $res = Common::findSoftWhereData('order',['uid'=>$data['id'],'status'=>config('verify.status')[8]],'id');
jinglong authored
129 130 131 132 133 134
                if($res){
                    $data['is_news'] = 1;//旧人
                }else{
                    $data['is_news'] = 0;//新人
                }
            }
jinglong authored
135
            $data['nickname'] = $this->auth->emoji_decode($data['nickname']);
jinglong authored
136 137 138 139 140 141 142 143 144 145 146
            $this->success('成功', $data);
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (获取二维码)
     * @ApiSummary  (获取二维码)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/user/getWxCode)
147 148 149
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="goods_id", type="inter", required=true, description="商品id")
jinglong authored
150 151 152 153 154 155
     *
     * @ApiReturn ({
        "code": 1,
        "msg": "成功",
        "time": "1572602867",
        "data": {
156
            "code_url": "http://feifangu.w.brotop.cn/wx_code_img/code_6.jpg"//二维码路径
jinglong authored
157 158 159 160 161
        }
    })
     */
    public function getWxCode(){
        if($this->request->isGet()){
162
            $goods_id = $this->request->get('goods_id');
163 164 165 166 167 168
            $rule = config('verify.goods_detail');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['goods_id'=>$goods_id])) {
                $this->error($validate->getError());
            }
jinglong authored
169 170 171 172 173 174 175 176 177 178 179 180 181
            //获取access_token
            $config = config('verify.raw');
            $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$config['app_id'].'&secret='.$config['secret'];
            $res = $this->auth->http_get($url);
            $json_arr = json_decode($res,true);
            if(!isset($json_arr['access_token'])&&empty($json_arr['access_token'])){
                //用户登录
                $this->error('失败');
            }
            $access_token = $json_arr['access_token'];
            //获取二维码链接
            $get_code_url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$access_token;
            $data = [
jinglong authored
182
                'scene' => 'share_uid='.$this->uid.'&id='.$goods_id,
jinglong authored
183
                'width' => 280,
jinglong authored
184
                'page' => 'pages/index/goodsDetail/goodsDetail',
jinglong authored
185 186 187
            ];
            $data = json_encode($data);
            $code_res = $this->auth->http_post($get_code_url,$data);
jinglong authored
188
            file_put_contents('wx_code_img/code_'.$this->uid.'_'.$goods_id.'.jpg',$code_res);
jinglong authored
189
            $code_url = config('verify.ffg_host').'/wx_code_img/code_'.$this->uid.'_'.$goods_id.'.jpg';
jinglong authored
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
            $this->success('成功', ['code_url'=>$code_url]);
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (收藏)
     * @ApiSummary  (收藏)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/collection)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="goods_id", type="inter", required=true, description="商品id")
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571037179",
        "data": null
    })
     */
    public function collection(){
        if($this->request->isPost()){
            $goods_id = $this->request->post('goods_id');
            $rule = config('verify.goods_detail');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['goods_id'=>$goods_id])) {
                $this->error($validate->getError());
            }

            $collectionModel = new Collection();
            $res1 = Common::findWhereData('collection',['uid'=>$this->uid,'g_id'=>$goods_id],'id');
            if($res1){
                $this->error('已经收藏过了');
            }else{
                $res = $collectionModel->create(['g_id'=>$goods_id,'uid'=>$this->uid]);
                if($res){
jinglong authored
227 228
                    $goodsModel = new \app\admin\model\Goods();
                    $goodsModel->where(['id'=>$goods_id])->setInc('collections',1);
jinglong authored
229 230 231 232

                    //增加商品收藏数
                    $type = config('verify.browse_type')[2];
                    Common::statistics($type,$this->uid,$goods_id);
jinglong authored
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
                    $this->success('成功');
                }else{
                    $this->error('失败');
                }
            }
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (取消收藏)
     * @ApiSummary  (取消收藏)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/cancelCollection)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="goods_id", type="inter", required=true, description="商品id")
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571037179",
        "data": null
    })
     */
    public function cancelCollection(){
        if($this->request->isPost()){
            $goods_id = $this->request->post('goods_id');
            $rule = config('verify.goods_detail');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['goods_id'=>$goods_id])) {
                $this->error($validate->getError());
            }

            $collectionModel = new Collection();
            $res = $collectionModel->where(['uid'=>$this->uid,'g_id'=>$goods_id])->delete();
            if($res){
jinglong authored
270 271
                $goodsModel = new \app\admin\model\Goods();
                $goodsModel->where(['id'=>$goods_id])->setDec('collections',1);
jinglong authored
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
                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (我的收藏)
     * @ApiSummary  (我的收藏)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/user/myCollectionList)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="page", type="inter", required=true, description="分页页码")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1574941706",
        "data": {
            "data": [
                {
                    "id": 7,//商品id
                    "image": "http://jinglong.springchunjia.cn/uploads/20191128/8a677f5a0418059bf1b974c50026af13.png",//图片路径
                    "name": "MONENT 动感系列",//商品名称
                    "tag": [//商品标签
                        "日式简约",
                        "隐秘乡奢",
                        "家庭情侣"
                    ],
                    "style": [//商品规格
                        "主餐匙,茶匙各1件",
                        "古堡灰"
                    ],
                    "sale_price": 2299//销售价格
                    "expense_price": //运费(0:显示包运费标签)
311
                    "is_new_tag": 0//新人价格标签(0:不显示,1:显示)
jinglong authored
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
                },
                {
                    "id": 4,
                    "image": "http://jinglong.springchunjia.cn/uploads/20191128/93971e55b83d1a09c1831f8197514305.png",
                    "name": "MONENT 动感系列",
                    "tag": [
                        "AB级",
                        "ABX级",
                        "ABN级"
                    ],
                    "new_price": 2499,
                    "sale_price": 2599
                },
            ],
            "total_page": 1
        }
    })
     */
    public function myCollectionList(){
        if($this->request->isGet()){
            $page = $this->request->get('page');
            $rule = config('verify.page');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['page'=>$page])) {
                $this->error($validate->getError());
            }
jinglong authored
338
            
jinglong authored
339 340
            $collection = Common::selectWhereData('collection',['uid'=>$this->uid],'id,g_id');
            $g_ids = array_column($collection,'g_id');
jinglong authored
341
jinglong authored
342 343 344
            //按照指定的顺序排序
            $g_id_str = implode(',',$g_ids);
            $exp = new Expression('field(id,'.$g_id_str.')');
jinglong authored
345
jinglong authored
346 347
            $limit = config('verify.limit');
            $arr = Common::goodsList(['id'=>['in',$g_ids]],$page,$this->uid,$limit,$exp);
jinglong authored
348
            $this->success('成功',$arr);
jinglong authored
349 350 351 352 353
        }else{
            $this->error('请求方式错误');
        }
    }
354 355 356 357 358
    /**
     * @ApiTitle    (我的优惠券)
     * @ApiSummary  (我的优惠券)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/user/myCouponList)
jinglong authored
359 360
     *
     * @ApiParams   (name="is_flag", type="inter", required=true, description="优惠券标识(0:未使用,1:已使用,2:已过期)")
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
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1575372162",
        "data": [
            {
                "id": 2,//优惠券id
                "coupon_tag": "无门槛",//优惠券(无门槛,折扣券,满减券)
                "coupon_price": "¥300",//(折扣或减少金额)
                "coupon_tag1": "无门槛",优惠券(无门槛,满多少可用)
                "coupon_name": "全场优惠券",//优惠券名称
                "end_time": "2020.1.31",//优惠券有效期
                "type": "全场通用"//优惠券用途
            },
            {
                "id": 10,
                "coupon_tag": "折扣券",
                "coupon_price": "9.5折",
                "coupon_tag1": "满2000可用",
                "coupon_name": "商品优惠券",
                "end_time": "2020.1.31",
                "type": "商品可用"
            }
        ]
    })
     */
    public function myCouponList(){
        if($this->request->isGet()){
jinglong authored
390 391 392 393 394 395 396
            $is_flag = $this->request->get('is_flag');
            $rule = config('verify.my_coupon');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['is_flag'=>$is_flag])) {
                $this->error($validate->getError());
            }
397
            $flag = config('verify.flag');
jinglong authored
398
            $where['uid'] = $this->uid;
399
            $current_time = time();
jinglong authored
400 401 402
            if($is_flag == 0){
                //未使用
                $where['is_use'] = $flag[0];
403 404
                $where1['start_time'] = ['<',$current_time];
                $where1['end_time'] = ['>',$current_time];
jinglong authored
405 406 407
            }else if($is_flag == 1){
                //已使用
                $where['is_use'] = $flag[1];
408 409
                $where1['start_time'] = ['<',$current_time];
                $where1['end_time'] = ['>',$current_time];
jinglong authored
410 411
            }else{
                //已过期
412
                $where1['end_time'] = ['<',$current_time];
jinglong authored
413
            }
414
jinglong authored
415 416
            //查询已经领取过
            $receive = Common::selectWhereData('rcoupon',$where,'id,c_id');
417
            $receive_s = array_column($receive,'c_id');
jinglong authored
418
            $where1['id'] = ['in',$receive_s];
jinglong authored
419
            $data = Common::selectWhereData('coupon',$where1,'id,type,coupon_name,c_type,coupon_type,full_reduce,reduce,discount,coupon_number,end_time','sort desc,id desc');
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
            $res = [];
            foreach($data as $key=>$value){

                $res[$key]['id'] = $value['id'];
                //无门槛,有门槛
                if($value['c_type'] == 0){
                    //无门槛
                    if($value['coupon_type'] == 0){
                        //减少券,去掉满减金额,折扣字段,
                        unset($value['full_reduce']);
                        unset($value['discount']);
                        $res[$key]['coupon_tag'] = '无门槛';
                        $res[$key]['coupon_price'] = '¥'.$value['reduce'];
                        $res[$key]['coupon_tag1'] = '无门槛';
                    }else{
                        //折扣券,去掉满减金额,减少金额字段,
                        unset($value['full_reduce']);
                        unset($value['reduce']);
                        $res[$key]['coupon_tag'] = '折扣券';
                        $res[$key]['coupon_price'] = $value['discount'].'折';
                        $res[$key]['coupon_tag1'] = '无门槛';
                    }
                }else{
                    //有门槛
                    if($value['coupon_type'] == 0){
                        //减少券,去掉折扣字段,
                        unset($value['discount']);
                        $res[$key]['coupon_tag'] = '满减券';
                        $res[$key]['coupon_price'] = '¥'.$value['reduce'];
                        $res[$key]['coupon_tag1'] = '满'.$value['full_reduce'].'可用';
                    }else{
                        //折扣券,去掉减少金额字段,
                        unset($value['reduce']);
                        $res[$key]['coupon_tag'] = '折扣券';
                        $res[$key]['coupon_price'] = $value['discount'].'折';
                        $res[$key]['coupon_tag1'] = '满'.$value['full_reduce'].'可用';
                    }
                }

                $res[$key]['coupon_name'] = $value['coupon_name'];//优惠券名称
                $res[$key]['end_time'] = date('Y.n.j',$value['end_time']);//优惠券有效期
                //全场,品牌,商品
                if($value['type'] == 0){
                    $res[$key]['type'] = '全场通用';

                }else if($value['type'] == 1){
                    $res[$key]['type'] = '部分品牌可用';

                }else{
                    $res[$key]['type'] = '部分商品可用';

                }
            }
            $this->success('成功',$res);
        }else{
            $this->error('请求方式错误');
        }
    }
jinglong authored
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

    /**
     * @ApiTitle    (分享获取礼包)
     * @ApiSummary  (分享获取礼包)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user/share)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="share_uid", type="inter", required=true, description="分享人uid")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571037179",
        "data": null
    })
     */
    public function share(){
        if($this->request->isPost()){
            $share_uid = $this->request->post('share_uid');
            $rule = config('verify.share');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['share_uid'=>$share_uid])) {
                $this->error($validate->getError());
            }
jinglong authored
504 505 506 507 508
            if($share_uid == $this->uid){
                //携带参数错误
                $this->error('失败');
            }
jinglong authored
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
            $rCouponModel = new Rcoupon();
            //查询分享人已领取优惠券
            $receive = Common::selectWhereData('rcoupon',['uid'=>$share_uid],'id,c_id');
            $receive_s = array_column($receive,'c_id');
            //查询分享人优惠券
            $res_coupon = Common::selectWhereData('coupon',['gift'=>1,'end_time'=>['>',time()]],'id');
            $res_coupon = array_column($res_coupon,'id');
            $res = array_diff($res_coupon,$receive_s);//数组1不存在数组2中的数组
            $arr = [];
            foreach($res as $key=>$value){
                $arr[$key]['uid'] = $share_uid;
                $arr[$key]['c_id'] = $value;
                $arr[$key]['is_share'] = 1;
            }
            $rCouponModel->saveAll($arr);

            //查询被分享人已领取优惠券
            $receive = Common::selectWhereData('rcoupon',['uid'=>$this->uid],'id,c_id');
            $receive_s = array_column($receive,'c_id');
            //查询分享人优惠券
            $res_coupon = Common::selectWhereData('coupon',['gift'=>2,'end_time'=>['>',time()]],'id');
            $res_coupon = array_column($res_coupon,'id');
            $res = array_diff($res_coupon,$receive_s);//数组1不存在数组2中的数组
            $arr1 = [];
            foreach($res as $key=>$value){
                $arr1[$key]['uid'] = $this->uid;
                $arr1[$key]['c_id'] = $value;
                $arr1[$key]['is_share'] = 1;
            }
            $rCouponModel->saveAll($arr1);
            $this->success('成功');
        }else{
            $this->error('请求方式错误');
        }
    }
jinglong authored
544
}