User.php 13.6 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
<?php

namespace app\api\controller;

use app\api\model\UserAddress;
use app\api\model\UserCoupon;
use app\common\controller\Api;
use fast\Http;
use think\Config;
use think\Db;

/**
 * 我的页面
 */
class User extends Api
{
    protected $noNeedLogin = ['*'];
//    protected $noNeedLogin = ['third'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    /**
     * 会员中心
     * @ApiReturn (
     *     data:{
     *      nickname 昵称
     *      avatar 头像
     *      pay 待支付
     *      wait_send 待发货
     *      wait_collect 待收货
     *      wait_comment 待评价
     *     }
     * )
     */
    public function index()
    {
        $data = [];
        $data['nickname'] = $this->auth->nickname;
        $data['avatar'] = cdnurl($this->auth->avatar,true);
        $data['mobile'] = $this->auth->mobile;
        $data['pay'] = Db::name('litestore_order')->where('user_id',$this->auth->id)->where('pay_status','10')->count();
        $data['wait_send'] = Db::name('litestore_order')->where('user_id',$this->auth->id)->where('freight_status','10')->count();
        $data['wait_collect'] = Db::name('litestore_order')->where('user_id',$this->auth->id)->where('receipt_status','10')->count();
        $data['wait_comment'] = Db::name('litestore_order')->where('user_id',$this->auth->id)->where('receipt_status','20')->count();
        $this->success('会员中心', ['welcome' => $this->auth->nickname]);
    }

    /**
     * 修改会员个人信息
     *
     * @ApiMethod (POST)
     * @param string $avatar   头像地址
     * @param string $mobile   联系方式
     * @param string $nickname 昵称
     */
    public function profile()
    {
        $user = $this->auth->getUser();
        $mobile = $this->request->post('mobile');
        $nickname = $this->request->post('nickname');
        $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars');
        if ($mobile) {
            $user->mobile = $mobile;
        }
        if ($nickname) {
            $user->nickname = $nickname;
        }
        if ($avatar) {
            $user->avatar = $avatar;
        }
        $user->save();
        $this->success();
    }


    /**
     * 第三方登录
     *
     * @ApiMethod (POST)
     * @param string $code     Code码
     * @param string $nickname 微信昵称
     * @param string $avatar   微信头像
     */
    public function third()
    {
        $code = $this->request->post('code');
        $nickname = $this->request->post('nickname');
        $avatar = $this->request->post('avatar');
        if (!$code || !$nickname || !$avatar){
            $this->error('后台所需参数缺失请完善参数');
        }
        $param = [];
        $param['js_code'] = $code;
        $param['grant_type'] = 'authorization_code';
        $param['secret'] = Config::get('site.secret');
        $param['appid'] = Config::get('site.appid');
        $wxapi = Http::get('https://api.weixin.qq.com/sns/jscode2session',$param);//请求openid
        $wxapi = json_decode($wxapi,true);
        if (isset($wxapi['errcode'])){
            $this->error($wxapi['errmsg']);
        }
        $third = new \app\api\model\Third();
        $userid = $third->where('openid',$wxapi['openid'])->value('user_id');
        if ($userid){
            $this->auth->direct($userid);
            $this->success('登录成功',['token'=>$this->auth->getToken(),'user_type'=>$this->auth->user_type]);
        }else{
            $userid = $this->auth->register($nickname,'','','',['avatar'=>$avatar]);
            if ($userid){
                $third->save(['openid'=>$wxapi['openid'],'user_id'=>$userid]);
                $this->success('登录成功',['token'=>$this->auth->getToken(),'user_type'=>$this->auth->user_type]);
            }else{
                $this->error('注册失败');
            }
        }
    }

    /**
     * @ApiTitle    (用户优惠券)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="type", type="integer", required=true, description="0未使用1已使用2已过期")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": [
        {
        "id": 1,
        "user_id": 1,
        "coupon_id": 1,
        "name": "手动阀手动阀",
        "price": "1.00", 优惠券金额
        "full_price": "10.00", 满减金额
        "createtime": 111122244,
        "endtime": 1641869388,
        "status": "0",
        "endtime_text": "2022年01月11日到期"
        }
    ]
    })
     */
    public function userCoupon()
    {
        $type = $this->request->post('type');
        if (!in_array($type,[0,1,2])) $this->error('type参数不合法');
        $model = new UserCoupon();
        $list = $model->where('status',$type)->where('user_id',$this->auth->id)->select();
        $this->success('用户优惠券列表',$list);
    }

    /**
     * @ApiTitle    (用户地址列表)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": [
        {
        "id": 1,
        "user_id": 1,
        "username": "1", 收件人
        "address": "阿松大", 地址
        "address_detail": "阿松大", 详细地址
        "normal_status": "0", 0不默认1默认
        "lng": null, 经度
        "lat": null, 纬度
        "createtime": null,
        "mobile_hide": "135****9988" 电话号码
        "mobile": "13549059988" 电话号码
        }
    ]
    })
     */
    public function userAddressList()
    {
        $model = new UserAddress();
        $list = $model->where('user_id',1)->order('normal_status','desc')->select();
        $this->success('用户地址列表',$list);
    }


    /**
     * @ApiTitle    (用户修改(删除)地址)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name=id, type=integer, required=true, description="地址id")
     * @ApiParams   (name=type, type=integer, required=true, description="1修改2删除")
     * @ApiParams   (name=username, type=string, required=false, description="收件人")
     * @ApiParams   (name=mobile, type=string, required=flase, description="手机号")
     * @ApiParams   (name=address, type=string, required=false, description="地址")
     * @ApiParams   (name=address_detail, type=string, required=false, description="详细地址")
     * @ApiParams   (name=normal, type=integer, required=false, description="默认状态 0不默认或1默认")
     * @ApiParams   (name=lat, type=float, required=false, description="纬度")
     * @ApiParams   (name=lng, type=float, required=false, description="经度")
     * @ApiReturn   ({
    'code':'1',
    'msg':'SUCCESS'
    "data":
    })
     */
    public function userAddressEdit()
    {
        $id = $this->request->post('id');
        $type = $this->request->post('type');
        $username = $this->request->post('username');
        $mobile = $this->request->post('mobile');
        $address = $this->request->post('address');
        $address_detail = $this->request->post('address_detail');
        $normal = $this->request->post('normal');
        $lat = $this->request->post('lat');
        $lng = $this->request->post('lng');
        $data = [
            'type' => $type,
            'mobile' => $mobile,
            'id' => $id,
            'normal' => $normal,
            'lat' => $lat,
            'lng' => $lng,
        ];
        $rule = [
            'type' => 'require|in:1,2',
            'mobile' => 'regex:^1\d{10}$',
            'id' => 'require|integer',
            'normal' => 'in:0,1',
            'lat' => 'float',
            'lng' => 'float',
        ];
        $msg = [
            'type' => 'type参数不合法',
            'mobile' => '电话号码格式不正确',
            'id' => '地址id不合法',
            'normal' => '默认状态参数不合法',
            'lat' => '纬度请为浮点数格式',
            'lng' => '经度请为浮点数格式',
        ];
        $validate = new \think\Validate();
        $validate->rule($rule);
        $validate->message($msg);
        if (!$validate->check($data)) $this->error($validate->getError());
        $model = new UserAddress();
        if ($type == 1){
            $data = [
                'mobile' => $mobile,
                'normal_status' => $normal,
                'lat' => $lat,
                'lng' => $lng,
                'username' => $username,
                'address' => $address,
                'address_detail' => $address_detail,
            ];
            $model->where('id',$id)->isUpdate()->save($data,['id'=>$id]);
        }else{
            $model->where('id',$id)->delete();
        }
        $this->success('SUCCESS');
    }


    /**
     * @ApiTitle    (加入我们)
     * @ApiMethod   (POST)
     * @ApiParams   (name=work, type=string, required=true, description="从事行业")
     * @ApiParams   (name=years, type=string, required=true, description="从业时间")
     * @ApiParams   (name=username, type=string, required=true, description="姓名")
     * @ApiParams   (name=mobile, type=string, required=true, description="手机号")
     * @ApiParams   (name=money, type=string, required=true, description="预计可投入资金")
     * @ApiParams   (name=team, type=integer, required=true, description="团队拥有0没有1有")
     * @ApiParams   (name=city, type=string, required=true, description="意向城市")
     * @ApiParams   (name=content, type=string, required=true, description="自身优势")
     * @ApiReturn   ({
    'code':'1',
    'msg':'SUCCESS'
    "data":
    })
     */
    public function joinUs()
    {
        $work = $this->request->post('work');
        $years = $this->request->post('years');
        $username = $this->request->post('username');
        $mobile = $this->request->post('mobile');
        $money = $this->request->post('money');
        $team = $this->request->post('team');
        $city = $this->request->post('city');
        $content = $this->request->post('content');
        $data = [
            'work' => $work,
            'mobile' => $mobile,
            'username' => $username,
            'years' => $years,
            'money' => $money,
            'team' => $team,
            'city' => $city,
            'content' => $content,
        ];
        $rule = [
            'work' => 'require',
            'mobile' => 'require|regex:^1\d{10}$',
            'years' => 'require',
            'username' => 'require',
            'money' => 'require',
            'team' => 'require|in:0,1',
            'city' => 'require',
            'content' => 'require|max:100',
        ];
        $msg = [
            'work' => '请填写从事行业',
            'mobile' => '请正确填写电话号码',
            'years' => '请填写从业时间',
            'username' => '请输入姓名',
            'money' => '请输入可投入资金',
            'team' => '请选择是否拥有团队',
            'city' => '请填写意向城市',
            'content' => '请填写描述控制在100字以内',
        ];
        $validate = new \think\Validate();
        $validate->rule($rule);
        $validate->message($msg);
        if (!$validate->check($data)) $this->error($validate->getError());
        $data['createtime'] = time();
        Db::name('join_us')->insert($data);
        $this->success('SUCCESS');
    }


    /**
     * @ApiTitle    (帮助反馈列表)
     * @ApiMethod   (POST)
     * @ApiHeaders    (name=token, type=string, required=true, description="token")
     * @ApiParams   (name=page, type=integer, required=true, description="页数")
     * @ApiReturn   ({
    'code':'1',
    'msg':'SUCCESS'
    "data":
        "list": {
            "total": 1,
            "per_page": 15,
            "current_page": 1,
            "last_page": 1,
            "data": [
                {
                "id": 1,
                "title": "123", 标题
                "content": "啊实打实大苏打", 内容
                }
            ]
        },
        "rider_status": 0 申请骑手状态:0=无,1=申请中,2=成功,3=拒绝
    })
     */
    public function helpsList()
    {
        $page = $this->request->post('page');
        if (!is_numeric($page)) $this->error('页数参数不合法');
        $list = Db::name('helps')
            ->order('id','desc')
            ->paginate(15,false,['page'=>$page]);
        $this->success('SUCCESS',['list'=>$list,'rider_status'=>0]);
    }

    /**
     * @ApiTitle    (申请成为骑手)
     * @ApiMethod   (POST)
     * @ApiHeaders    (name=token, type=string, required=true, description="token")
     * @ApiReturn   ({
    'code':'1',
    'msg':'SUCCESS'
    "data":
    })
     */
    public function riderApply()
    {
        if ($this->auth->rider > 0){
            $this->error('您已申请,不可重复申请');
        }
        $user = $this->auth->getUser();
        $user->rider = '1';
        $user->save();
        $this->success('SUCCESS');
    }

    /**
     * @ApiTitle    (邀请有奖)
     * @ApiMethod   (POST)
     * @ApiHeaders    (name=token, type=string, required=true, description="token")
     * @ApiReturn   ({
    'code':'1',
    'msg':'SUCCESS'
    "data":
    })
     */
    public function inviteReward()
    {
        if ($this->auth->rider > 0){
            $this->error('您已申请,不可重复申请');
        }
        $user = $this->auth->getUser();
        $user->rider = '1';
        $user->save();
        $this->success('SUCCESS');
    }
}