'状态', 'code' => '微信code', 'iv' => 'iv', 'encryptedData' => 'encryptedData', 'mobile' => '手机号', 'smsCode' => '验证码', ]; } public function userlogin() { if (!$this->validate()) { throw new Exception($this->getErrorMsg()); } switch ($this->status) { case 1;//微信一键登录 if (!$this->code) { throw new \yii\db\Exception('code不能为空'); } return $this->login(); case 3://手机号登录 if (!$this->mobile) { throw new \yii\db\Exception('mobile不能为空'); } if (!$this->smsCode) throw new \yii\db\Exception('验证码不能为空'); return $this->phoneLogin(); case 4://手机号注册 if (!$this->mobile) throw new \yii\db\Exception('mobile不能为空'); if (!$this->smsCode) throw new \yii\db\Exception('验证码不能为空'); return $this->PhoneRegister(); case 5://开发登录 if (!YII_DEBUG) { die(); } $user = User::findOne(['mobile' => $this->mobile]); if (!$user) { throw new \yii\db\Exception('用户不存在'); } $t = Yii::$app->db->beginTransaction(); try { $user->mobile = $this->mobile; $user->nickname = $user->nickname ?? '微信用户'; $token = Yii::$app->security->generateRandomString() . '_' . time(); $user->token = $token; $user->saveOrFail(); $storeUser = StoreUser::find()->where([ 'user_id' => $user->id, 'store_id' => Yii::$app->store, ])->one(); if (!$storeUser) { //用户关联门店 $StoreUser = new StoreUser(); $StoreUser->store_id = Yii::$app->store; $StoreUser->user_id = $user->id; $StoreUser->is_online = 1; $StoreUser->last_login_time = time(); $StoreUser->saveOrFail(); } else { StoreUser::updateAll(['last_login_time' => time()], [ 'user_id' => $user->id, 'store_id' => Yii::$app->store, 'is_delete' => 0 ]); } $t->commit(); return [ 'token' => $token, 'data' => $storeUser, ]; } catch (\Exception $e) { $t->rollBack(); throw new \yii\db\Exception($e->getMessage()); } break; } } /** * 微信登录 */ public function login() { $transaction = Yii::$app->db->beginTransaction(); try { $login = WechatService::getInstance()->app->auth->session($this->code); if (isset($login['errmsg'])) { throw new Exception($login['errmsg']); } $user = User::find()->where(['openid' => $login['openid'], 'is_delete' => 0])->one(); if (!$user) { $user = new User(); } $user->openid = $login['openid']; $user->session_key = $login['session_key']; $user->unionid = isset($login['unionid']) ? $login['unionid'] : ''; $token = Yii::$app->security->generateRandomString() . '_' . time(); $user->token = $token; $user->nickname = $user['nickname'] ?? '微信用户'; $user->current_store_id = Yii::$app->store;//当前门店 $user->saveOrFail(); $storeUser = StoreUser::find()->where([ 'user_id' => $user->id, 'store_id' => Yii::$app->store, ])->one(); if (!$storeUser) { //用户关联门店 $StoreUser = new StoreUser(); $StoreUser->store_id = Yii::$app->store; $StoreUser->user_id = $user->id; $StoreUser->is_online = 1; $StoreUser->last_login_time = time(); $StoreUser->saveOrFail(); } else { StoreUser::updateAll(['last_login_time' => time()], [ 'user_id' => $user->id, 'store_id' => Yii::$app->store, 'is_delete' => 0 ]); } $transaction->commit(); } catch (Exception $exception) { $transaction->rollBack(); throw new Exception('登录失败:' . $exception->getMessage() . ' APPID:' . WechatService::getInstance()->app->getConfig()['app_id']); } $StoreUser = StoreUser::find()->where([ 'user_id' => $user->id, 'is_online' => 1 ])->with(['user'])->orderBy(['last_login_time' => SORT_DESC]) ->asArray()->one(); return [ 'token' => $token, 'data' => $StoreUser, 'platform_token' => "Bearer ".Yii::$app->params['platform']['token'], 'user_id' => $user->id ]; } /** * 手机号登录 */ public function phoneLogin() { $cache = Yii::$app->cache; if ($cache->get('login_sms_code_' . $this->mobile)) { if ($cache->get('login_sms_code_' . $this->mobile) != $this->smsCode) { throw new Exception('手机验证码错误'); } $t = Yii::$app->db->beginTransaction(); try { $user = User::findOne(['mobile' => $this->mobile]); if (!$user) { throw new \yii\db\Exception('您还没有注册,请先注册'); } $user->mobile = $this->mobile; $user->nickname = $user->nickname ?? '微信用户'; $token = Yii::$app->security->generateRandomString() . '_' . time(); $user->token = $token; $user->saveOrFail(); $storeUser = StoreUser::find()->where([ 'user_id' => $user->id, 'store_id' => Yii::$app->store, ])->one(); if (!$storeUser) { //用户关联门店 $StoreUser = new StoreUser(); $StoreUser->store_id = Yii::$app->store; $StoreUser->user_id = $user->id; $StoreUser->is_online = 1; $StoreUser->last_login_time = time(); $StoreUser->saveOrFail(); } else { StoreUser::updateAll(['last_login_time' => time()], [ 'user_id' => $user->id, 'store_id' => Yii::$app->store, 'is_delete' => 0 ]); } $t->commit(); return [ 'token' => $token, 'data' => $storeUser, 'platform_token' => "Bearer ".Yii::$app->params['platform']['token'], 'user_id' => $user->id ]; } catch (\Exception $e) { $t->rollBack(); throw new \yii\db\Exception($e->getMessage()); } } else { throw new Exception('验证码已过期,请从新获取验证码'); } } /** * 手机号注册 */ public function PhoneRegister() { $cache = Yii::$app->cache; if ($cache->get('login_sms_code_' . $this->mobile)) { if ($cache->get('login_sms_code_' . $this->mobile) != $this->smsCode) { throw new Exception('手机验证码错误'); } try { $t = Yii::$app->db->beginTransaction(); $user = User::findOne(['mobile' => $this->mobile]); if ($user) { throw new \yii\db\Exception('您已注册,请登录'); } $addUsers = new User(); $addUsers->mobile = $this->mobile; $addUsers->nickname = $user->nickname ?? '微信用户'; $addUsers->avatarurl = $user->avatarurl ?? 'https://tenfei03.cfp.cn/creative/vcg/veer/1600water/veer-105516317.jpg'; $token = Yii::$app->security->generateRandomString() . '_' . time(); $addUsers->token = $token; $addUsers->saveOrFail(); //用户门店 $storeUser = StoreUser::find()->where([ 'user_id' => $addUsers->id, 'store_id' => Yii::$app->store, ])->one(); if (!$storeUser) { //用户关联门店 $StoreUser = new StoreUser(); $StoreUser->store_id = Yii::$app->store; $StoreUser->user_id = $addUsers->id; $StoreUser->is_online = 1; $StoreUser->last_login_time = time(); $StoreUser->saveOrFail(); } else { //更新登录时间 StoreUser::updateAll(['last_login_time' => time()], [ 'user_id' => $addUsers->id, 'store_id' => Yii::$app->store, 'is_delete' => 0 ]); } // $response = (new Client(['http_errors' => false]))->post(\Yii::$app->params['platform']['url']."/platform/v1/sync/user", [ // 'headers' => ['Authorization' =>"Bearer ".\Yii::$app->params['platform']['token']], // 'form_params' => [ // 'id' => $addUsers->id, // 'mobile' => $this->mobile, // 'nickname' => '微信用户', // 'avatarurl' => $addUsers->avatarurl // ], // ]); // $result = json_decode($response->getBody(),true); // if ($result['errcode'] != 0) { // throw new Exception($result['msg']); // } $t->commit(); return [ 'StoreUser' => $StoreUser, 'addUsers' => $addUsers, 'platform_token' => "Bearer ".Yii::$app->params['platform']['token'], ]; } catch (\Exception $e) { $t->rollBack(); throw new \yii\db\Exception($e->getMessage()); } } else { throw new Exception('验证码已过期,请从新获取验证码'); } } }