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

namespace addons\shopro\controller;

use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Validate;
use addons\shopro\library\Wechat;
use think\Db;
use addons\shopro\model\UserOauth;
use addons\shopro\model\User as UserModel;
use addons\shopro\model\UserStore;

/**
 * 会员接口
 */
class User extends Base
{
    protected $noNeedLogin = ['login', 'mobilelogin', 'accountLogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third', 'wxMiniProgramLogin', 'getWxMiniProgramSessionKey', 'wxOfficialAccountLogin', 'wxOfficialAccountBaseLogin', 'wxOpenPlatformLogin', 'getUserDefaultFields'];
    protected $noNeedRight = '*';

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

    /**
     * 会员中心
     */
    public function index()
    {
        $auth = \app\common\library\Auth::instance();
        $auth->setAllowFields(['id', 'username', 'nickname', 'mobile', 'avatar', 'score', 'birthday', 'money', 'group', 'group_id']);
        $data = $auth->getUserinfo();
        if (!isset($data['group'])) {
            $data['group'] = \addons\shopro\model\UserGroup::get($data['group_id']);
        }

        // 查询用户优惠券数量
        $userCoupons = \addons\shopro\model\Coupons::getCouponsList(1);
        $data['coupons_num'] = count($userCoupons);

        // 查询用户是否是门店管理员
        $userStores = UserStore::where('user_id', $data['id'])->select();
        $data['is_store'] = $userStores ? 1 : 0;
        $data['store_id'] = 0;
        if (count($userStores) == 1) {
            // 只有一个店铺 直接进入店铺
            $data['store_id'] = $userStores[0]['store_id'];
        }

        $this->success('用户信息', $data);
    }

    /**
     * 会员登录
     *
     * @param string $account 账号
     * @param string $password 密码
     */
    public function accountLogin()
    {
        $account = $this->request->post('account');
        $password = $this->request->post('password');
        if (!$account || !$password) {
            $this->error(__('Invalid parameters'));
        }
        $ret = $this->auth->login($account, $password);
        if ($ret) {
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success(__('Logged in successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }
    /**
     * 获取微信小程序session_key
     *
     * @param string $code 加密code
     */
    public function getWxMiniProgramSessionKey()
    {
        $post = $this->request->post();
        $wechat = new Wechat('wxMiniProgram');
        $decryptSession = $wechat->code($post['code']);
        $this->success('获取session_key', $decryptSession);
    }
    /**
     * 微信小程序登录
     *
     * @param string $code 加密code
     */
    public function wxMiniProgramLogin()
    {
        $post = $this->request->post();
        $wechat = new Wechat('wxMiniProgram');
        $decryptUserInfo = $wechat->decryptData($post['session_key'], $post['iv'], $post['encryptedData']);
//        array(10) {
//        ["openId"] => string(28) "oD9ko40ZEoru7bjzN2f0Wnm3uNmA"
//        ["nickName"] => string(12) "Civilization"
//        ["gender"] => int(1)
//        ["language"] => string(5) "zh_CN"
//        ["city"] => string(0) ""
//        ["province"] => string(0) ""
//        ["country"] => string(7) "Iceland"
//        ["avatarUrl"] => string(124) "https://wx.qlogo.cn/mmopen/vi_32/bKchg38ErVBnuJBb4NfCOYkMsxgzZ3vZGu7y7V0BQG8clImsyvZge2HzGGYfxXKBNv8dbQOvyvmjKPEtRyrniaA/132"
//        ["unionId"] => string(28) "odsYQ1MLsGSGL6Gp5f6y3at2j7Hg"
//        ["watermark"] => array(2) {
//            ["timestamp"] => int(1590344429)
//            ["appid"] => string(18) "wx39cd0799d4567dd0"
//             }
//        }
        //组装decryptData
        $decryptData = array_change_key_case($decryptUserInfo, CASE_LOWER);
        $decryptData['headimgurl'] = $decryptData['avatarurl'];
        $decryptData['sex'] = $decryptData['gender'];
        $decryptData['session_key'] = $post['session_key'];
        if (empty($decryptData['openid'])) {
            $this->error(__('code错误'), $decryptData);
        }

        $ret = $this->oauthLoginOrRegister($decryptData, 'wxMiniProgram', 'Wechat');
        if ($ret) {
            $data = $ret->getUserinfo();

            if (!isset($data['group'])) {
                $data['group'] = \addons\shopro\model\UserGroup::get($data['group_id']);
            }
            $this->success(__('Logged in successful'), $data);
        }

        $this->error($this->auth->getError());
    }

    /**
     * 微信APP登录
     *
     * @param string $authResult id信息
     * @param string $userInfo 用户信息
     */
    public function wxOpenPlatformLogin()
    {
        $params = $this->request->post();
        //App
//        'authResult' =>
//  array (
//      'access_token' => '32_WlLV5jxPoZuHWi7fnnxNPhun8Dj_igJPWOOxkP_WyEkHHc6pLpBz-13F3uu8KvS2HI0ei0l64Js8TD_W7A16JSr7PA16gwcDeU7RWd35gzQ',
//      'expires_in' => 7200,
//      'refresh_token' => '32_uvVpEiwt9q4ZmzDZgyF7jv8e4XxXValLOsuux4-GG7PIgywjnVL8Ovzzw-H1sKUd__X9NJko2lB49ZMo1ubYL4DT0Hv0C33yKkZDq8K0J9Y',
//      'openid' => 'oO01HuKtCFucLFr-Q9vwCLMGHO08',
//      'scope' => 'snsapi_userinfo',
//      'unionid' => 'odsYQ1IOJtHfVIK9KqPdy1U0uMBo',
//  ),
//  'userInfo' =>
//  array (
//      'openId' => 'oO01HuKtCFucLFr-Q9vwCLMGHO08',
//      'nickName' => 'RogerD',
//      'gender' => 1,
//      'city' => '中山',
//      'province' => '广东',
//      'country' => '中国',
//      'avatarUrl' => 'http://thirdwx.qlogo.cn/mmopen/vi_32/PiajxSqBRaEK5uWViaPyje54F39j2FZYgvVicB0sQV1zLQ4ia41DYm7bqLlygCYu5sH8Y4MvfFWicNRUP4by7l1yhkg/132',
//      'unionId' => 'odsYQ1IOJtHfVIK9KqPdy1U0uMBo',
//  ),
        //组装decryptData
        $userInfo = array_change_key_case($params['userInfo'], CASE_LOWER);
        $userInfo['headimgurl'] = $userInfo['avatarurl'];
        $userInfo['sex'] = $userInfo['gender'];
        $decryptData = array_merge($params['authResult'], $userInfo);

        $ret = $this->oauthLoginOrRegister($decryptData, 'App', 'Wechat');
        if ($ret) {
            $data = $ret->getUserinfo();
            if (!isset($data['group'])) {
                $data['group'] = \addons\shopro\model\UserGroup::get($data['group_id']);
            }
            $this->success(__('Logged in successful'), $data);
        }

        $this->error($this->auth->getError());
    }


    /**
     * 微信公众号登录
     *
     * @param string $code 加密code
     */
    public function wxOfficialAccountLogin()
    {
        $wechat = new Wechat('wxOfficialAccount');
        $oauth = $wechat->oauth();
        //解析来源页面
        $oUrl = input('get.state');
        $url = explode('/', $oUrl);
        $procotol = $url[0] . '//';
        $host = $url[2];
        $decryptData = $oauth->user()->getOriginal();
//        ["openid"] => string(28) "oUgIVxASHEWU0wHd_ypNhJAdH088"
//        ["nickname"] => string(6) "東浩"
//        ["sex"] => int(1)
//        ["language"] => string(5) "zh_CN"
//        ["city"] => string(6) "郑州"
//        ["province"] => string(6) "河南"
//        ["country"] => string(6) "中国"
//        ["headimgurl"] => string(133) "http://thirdwx.qlogo.cn/mmopen/vi_32/PiajxSqBRaEI3EldP4TQicjoF7utibMMRP08vfC9AOWia3WYmUWAGyI9GjWVkzMiahVYo2ef4KTNvVwLfhvKA3W0iccQ/132"
//        ["privilege"] => array(0) {
//        }
//        ["unionid"] => string(28) "odsYQ1CI3j4CK8_NmWdz0HuKsajY"

        $ret = $this->oauthLoginOrRegister($decryptData, 'wxOfficialAccount', 'Wechat');

        if ($ret) {
            //登录页接参Token
            header('Location:' . $procotol . $host . '/pages/public/login?token=' . $ret->getToken());
            exit;
        }
        $this->error($ret->getError());
    }

    /**
     * 微信公众号静默登录
     *
     * @param string $code 加密code
     */
    public function wxOfficialAccountBaseLogin()
    {
        $wechat = new Wechat('wxOfficialAccount');
        $oauth = $wechat->oauth();
        $oUrl = input('get.state');
        $url = explode('/', $oUrl);
        $decryptData = $oauth->user()->getOriginal();
        if ($decryptData) {
            header('Location:' . $oUrl . '&openid=' . $decryptData['openid']);
        } else {
            $this->error('未获取到OPENID');
        }


    }

    /**
     * 第三方登录或自动注册
     *
     * @param string $decryptData 解密参数
     * @param string $platform 平台名称
     * @param string $provider 厂商名称
     * @param string $keeptime 有效时长
     */
    private function oauthLoginOrRegister($decryptData, $platform, $provider, $keeptime = 0)
    {
        extract($decryptData);
        @$oauthData = compact('provider', 'unionid', 'platform', 'openid', 'nickname', 'sex', 'city', 'province', 'country', 'headimgurl', 'session_key', 'refresh_token', 'access_token');
        $oauthData['logintime'] = time();
        $oauthData['logincount'] = 1;
        if ($platform === 'wxMiniProgram' || $platform === 'App') {
            $oauthData['expire_in'] = 7200;
            $oauthData['expiretime'] = time() + 7200;
        }
        $auth = \app\common\library\Auth::instance();
        $auth->keeptime($keeptime);
        $auth->setAllowFields(['id', 'username', 'nickname', 'mobile', 'avatar', 'score', 'money', 'group', 'group_id']);

        $userOauth = UserOauth::get(['openid' => $openid]);
        if (isset($decryptData['nickname'])) {
            $fields['nickname'] = $decryptData['nickname'];
        }
        if (isset($decryptData['headimgurl'])) {
            $fields['avatar'] = $decryptData['headimgurl'];
        }
        //开启事务
        Db::startTrans();
        try {
            if ($userOauth) {
                //找到对应已注册用户,更新oauthData数据和用户数据直接发起登录
                $user_id = $userOauth->user_id;
                $user = UserModel::get($user_id);
                $oauthData['logincount'] = $userOauth->logincount + 1;
                $userOauth->save($oauthData);
            } else {
                //添加新的oauthData数据

                //默认创建新用户
                $createNewUser = true;
                // 判断是否有unionid 并且已存在oauth数据中
                if (isset($unionid)) {
                    //存在同厂商信息,添加oauthData数据,合并用户
                    $user_id = UserOauth::where(['unionid' => $unionid])->value('user_id');
                    $user = UserModel::get($user_id);
                    if ($user) {
                        $createNewUser = false;
                    }
                }
                if ($createNewUser) {
                    // 创建空用户
                    $username = Random::alnum(20);
                    $password = '';
                    $domain = request()->host();
                    $extend = $this->getUserDefaultFields();
                    $result = $auth->register($username, $password, $username . '@' . $domain, '', $extend, $keeptime);
                    if (!$result) {
                        return false;
                    }
                    $user = $auth->getUser();
                    if (!isset($fields['nickname'])) {
                        //默认昵称
                        $fields['nickname'] = $extend['nickname'] . $user->id;
                    }
                    // 更新会员资料
                    $user = UserModel::get($user->id);
                    // 保存第三方信息
                    $user_id = $user->id;
                }
                $oauthData['user_id'] = $user_id;
                UserOauth::create($oauthData);
            }
            if (isset($fields)) $user->save($fields);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $auth->logout();
            $this->error($e->getMessage());
            return false;
        }

        $auth->direct($user_id);

        return $auth;
    }

    /**
     * 手机验证码登录
     *
     * @param string $mobile 手机号
     * @param string $code 验证码
     */
    public function mobileLogin()
    {
        $mobile = $this->request->post('mobile');
        $code = $this->request->post('code');
        if (!$mobile || !$code) {
            $this->error(__('Invalid parameters'));
        }
        if (!Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        if (!Sms::check($mobile, $code, 'mobilelogin')) {
            $this->error(__('Captcha is incorrect'));
        }
        $user = \app\common\model\User::getByMobile($mobile);
        if ($user) {
            if ($user->status != 'normal') {
                $this->error(__('Account is locked'));
            }
            //如果已经有账号则直接登录
            $ret = $this->auth->direct($user->id);
        }
        if ($ret) {
            Sms::flush($mobile, 'mobilelogin');
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success(__('Logged in successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }

    /**
     * 注册会员
     *
     * @param string $username 用户名
     * @param string $password 密码
     * @param string $email 邮箱
     * @param string $mobile 手机号
     * @param string $code 验证码
     */
    public function register()
    {
        $username = $this->request->post('mobile');
        $password = $this->request->post('password');
        $email = $this->request->post('mobile') . '@' . request()->host();

        $mobile = $this->request->post('mobile');
        $code = $this->request->post('code');
        if (!$password) {
            $this->error(__('请填写密码')); //TODO:密码规则校验
        }
        if (strlen($password) < 6 || strlen($password) > 16) {
            $this->error(__('密码长度 6-16 位')); //TODO:密码规则校验
        }
        if ($email && !Validate::is($email, "email")) {
            $this->error(__('邮箱填写错误'));
        }
        if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('手机号填写错误'));
        }
        $ret = Sms::check($mobile, $code, 'register');
        if (!$ret) {
            $this->error(__('Captcha is incorrect'));
        }
        $extend = $this->getUserDefaultFields();
        $ret = $this->auth->register($username, $password, $email, $mobile, $extend);
        if ($ret) {
            $user = $this->auth->getUser();
            $user->nickname = $user->nickname . $user->id;
            $user->save();
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success(__('注册成功'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }

    /**
     * 注销登录
     */
    public function logout()
    {
        $this->auth->logout();
        $this->success(__('Logout successful'));
    }

    /**
     * 修改会员个人信息
     *
     * @param string $avatar 头像地址
     * @param string $username 用户名
     * @param string $nickname 昵称
     * @param string $birthday 生日
     * @param string $bio 个人简介
     */
    public function profile()
    {
        $user = $this->auth->getUser();
        $username = $this->request->post('username');
        $nickname = $this->request->post('nickname');
        $bio = $this->request->post('bio', '');
        $birthday = $this->request->post('birthday');
        $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars');
        if ($username) {
            $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
            if ($exists) {
                $this->error(__('Username already exists'));
            }
            $user->username = $username;
        }
        $user->nickname = $nickname;
        $user->bio = $bio;
        $user->birthday = $birthday;
        $user->avatar = $avatar;
        $user->save();
        $this->success();
    }

    /**
     * 修改邮箱
     *
     * @param string $email 邮箱
     * @param string $captcha 验证码
     */
    public function changeemail()
    {
        $user = $this->auth->getUser();
        $email = $this->request->post('email');
        $captcha = $this->request->request('captcha');
        if (!$email || !$captcha) {
            $this->error(__('Invalid parameters'));
        }
        if (!Validate::is($email, "email")) {
            $this->error(__('Email is incorrect'));
        }
        if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
            $this->error(__('Email already exists'));
        }
        $result = Ems::check($email, $captcha, 'changeemail');
        if (!$result) {
            $this->error(__('Captcha is incorrect'));
        }
        $verification = $user->verification;
        $verification->email = 1;
        $user->verification = $verification;
        $user->email = $email;
        $user->save();

        Ems::flush($email, 'changeemail');
        $this->success();
    }

    /**
     * 修改手机号
     *
     * @param string $mobile 手机号
     * @param string $captcha 验证码
     */
    public function changemobile()
    {
        $user = $this->auth->getUser();
        $mobile = $this->request->post('mobile');
        $captcha = $this->request->post('captcha');
        if (!$mobile || !$captcha) {
            $this->error(__('Invalid parameters'));
        }
        if (!Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
            $this->error(__('Mobile already exists'));
        }
        $result = Sms::check($mobile, $captcha, 'changemobile');
        if (!$result) {
            $this->error(__('Captcha is incorrect'));
        }
        $verification = $user->verification;
        $verification->mobile = 1;
        $user->verification = $verification;
        $user->mobile = $mobile;
        $user->save();

        Sms::flush($mobile, 'changemobile');
        $this->success();
    }


    /**
     * 重置密码
     *
     * @param string $mobile 手机号
     * @param string $newpassword 新密码
     * @param string $captcha 验证码
     */
    public function resetpwd()
    {
        $type = $this->request->post("type", 'mobile');
        $mobile = $this->request->post("mobile");
        $email = $this->request->post("email");
        $newpassword = $this->request->post("newpassword");
        $captcha = $this->request->post("captcha");
        if (!$newpassword || !$captcha) {
            $this->error(__('Invalid parameters'));
        }
        if (strlen($newpassword) < 6 || strlen($newpassword) > 16) {
            $this->error(__('密码长度 6-16 位')); //TODO:密码规则校验
        }
        if ($type == 'mobile') {
            if (!Validate::regex($mobile, "^1\d{10}$")) {
                $this->error(__('Mobile is incorrect'));
            }
            $user = \app\common\model\User::getByMobile($mobile);
            if (!$user) {
                $this->error(__('User not found'));
            }
            $ret = Sms::check($mobile, $captcha, 'resetpwd');
            if (!$ret) {
                $this->error(__('Captcha is incorrect'));
            }
            Sms::flush($mobile, 'resetpwd');
        } else {
            if (!Validate::is($email, "email")) {
                $this->error(__('Email is incorrect'));
            }
            $user = \app\common\model\User::getByEmail($email);
            if (!$user) {
                $this->error(__('User not found'));
            }
            $ret = Ems::check($email, $captcha, 'resetpwd');
            if (!$ret) {
                $this->error(__('Captcha is incorrect'));
            }
            Ems::flush($email, 'resetpwd');
        }
        //模拟一次登录
        $this->auth->direct($user->id);
        $ret = $this->auth->changepwd($newpassword, '', true);
        if ($ret) {
            $this->success(__('Reset password successful'));
        } else {
            $this->error($this->auth->getError());
        }
    }


    public function changepwd()
    {
        $user = $this->auth->getUser();

        $oldpassword = $this->request->post("oldpassword");
        $newpassword = $this->request->post("newpassword");

        if (!$newpassword || !$oldpassword) {
            $this->error(__('Invalid parameters'));
        }
        if (strlen($newpassword) < 6 || strlen($newpassword) > 16) {
            $this->error(__('密码长度 6-16 位')); //TODO:密码规则校验
        }

        $ret = $this->auth->changepwd($newpassword, $oldpassword);

        if ($ret) {
            $this->auth->direct($user->id);
            $data = ['userinfo' => $this->auth->getUserinfo()];

            $this->success(__('Change password successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }

    private function getUserDefaultFields()
    {
        $userConfig = json_decode(\addons\shopro\model\Config::get(['name' => 'user'])->value,true);
        return $userConfig;
    }
}