request->isPost) { $params = Yii::$app->request->post(); $res = Yii::$app->services->validate->validate($params, [ [['username'], 'required'], [['captcha', 'password'], 'safe'], ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $where[] = ['mall_id' => $this->mall_id]; $where[] = ['status' => StatusEnum::ENABLED]; $where[] = ['mobile' => $params['username']]; $mall_user = User::getUser($where, '*', [], 0); if (!empty($mall_user)) $user = $mall_user->toArray(); if (empty($user)) { // 用户不存在,检查是否启用对外部接口用户数据获取 if (MallAddons::isInstall($this->mall_id, 'Salary') && Yii::$app->services->setting->addons->get($this->mall_id, 'Salary', 'basic.is_external')) { $user = UserLogic::register($this->mall_id, $params['username']); } else { throw new \yii\db\Exception('账号不存在'); } } else { // 用户已存在,更新用户最新的账号金额信息 if (MallAddons::isInstall($this->mall_id, 'Salary') && Yii::$app->services->setting->addons->get($this->mall_id, 'Salary', 'basic.is_external')) { UserLogic::updateUser($user); } } if (empty($params['password']) && empty($params['captcha'])) throw new \yii\db\Exception('必须选择一种登录方式'); if (!empty($params['password'])) { if (!\Yii::$app->getSecurity()->validatePassword(trim($params['password']), $user['password'])) throw new Exception('密码错误'); } else { if (YII_ENV == 'prod') { if (!Sms::checkValidateCode($params['username'], $params['captcha'], Sms::SMS_TYPE_USER_LOGIN)) throw new Exception('验证码不正确'); } } $token = Yii::$app->services->apiAccessToken->getAccessToken($mall_user, 'api'); $token = $token['access_token']; return $this->success('登录成功', ['userwagesToken' => $token]); } if (Yii::$app->request->isGet) { $service_agreement_list = []; $agreements = Yii::$app->services->setting->addons->get($this->mall_id, 'Salary', 'basic.agreements'); if (!empty($agreements)) { $service_agreement_ids = json_decode($agreements, true); $service_agreement_list = ServiceAgreement::lists([ 'where' => [ ['id' => $service_agreement_ids], ['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED] ], 'select' => 'id,name', 'order' => 'sort asc,id desc' ]); } return $this->success('登录成功', ['service_agreement_list' => $service_agreement_list]); } } catch (\Exception $e) { return $this->error($e->getMessage()); } } public function actionSmsCode() { $params = Yii::$app->request->post(); $res = Yii::$app->services->validate->validate($params, [ [['mobile'], 'required', 'message' => '请输入手机号码'], [['mobile', 'area_code'], 'integer'], [['sms_type'], 'string'], ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $area_code = '';// 初始化区号 $gateways = 'domestic';// 默认国内短信 // 检测是登录并且区号不为空则使用国际短信 if (!empty($this->user) && !empty($this->user->area_code)) { $gateways = 'international'; $area_code = $this->user->area_code; } // 判断是否有区号 if (!empty($params['area_code']) && $params['area_code'] != 86) { $gateways = 'international'; $area_code = $params['area_code']; } $params['sms_type'] = $params['sms_type'] ?: ''; $sms = new Sms($this->mall_id, $gateways); try { if ($sms->sendCaptcha($params['mobile'], $area_code, Sms::SMS_TYPE_USER_LOGIN) === false) return $this->error('发送失败'); }catch (\Exception $e){ return $this->error($e->getMessage()); } return $this->success('发送成功'); } public function actionLogout() { if (Yii::$app->services->apiAccessToken->disableByAccessToken(Yii::$app->user->identity->access_token)) return $this->success('退出成功'); return $this->error('退出失败'); } public function actionServiceAgreementDetail() { $params = Yii::$app->request->get(); $res = Yii::$app->services->validate->validate($params, [ [['id'], 'required', 'message' => '请输入id'], [['id'], 'integer'], ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $service_agreement = ServiceAgreement::getOne([ ['id' => $params['id']], ['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED] ], true, [], false, 'id,name,content'); return $this->success('成功', $service_agreement ?? []); } public function actionUpPassword() { try { if (\Yii::$app->request->isPost) { $params = Yii::$app->request->post(); $res = Yii::$app->services->validate->validate($params, [ [['mobile', 'captcha', 'password'], 'required'], ['mobile', 'match', 'pattern' => '/^1\d{10}$/', 'message' => '请输入正确的手机号码'], [['mobile'], 'integer'], ['password', 'string', 'length' => [6, 11]], ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $user = User::find()->where(['mobile' => $params['mobile'], 'mall_id' => $this->mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])->one(); //短信验证码 if (YII_ENV == 'prod') { if (!Sms::checkValidateCode($params['mobile'], $params['captcha'])) return $this->error('验证码不正确'); } $user->password = Yii::$app->getSecurity()->generatePasswordHash(trim($params['password'])); if ($user->save() === false) return $this->error(User::getStaticError()); return $this->success('设置成功'); } } catch (\Exception $e) { return $this->error($e->getMessage()); } } }