AuthController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace api\modules\v1\controllers\userwages;
  3. header('Access-Control-Allow-Origin:*');
  4. use addons\Salary\common\logic\UserLogic;
  5. use addons\Salary\common\services\WyaServices;
  6. use api\controllers\OnAuthController;
  7. use common\enums\StatusEnum;
  8. use common\helpers\sms\Sms;
  9. use common\models\common\ServiceAgreement;
  10. use common\models\mall\Mall;
  11. use common\models\mall\MallAddons;
  12. use common\models\user\User;
  13. use Yii;
  14. use Exception;
  15. class AuthController extends OnAuthController
  16. {
  17. /**
  18. * @var string
  19. */
  20. public $modelClass = '';
  21. /**
  22. * @var array
  23. */
  24. protected $authOptional = ['login', 'logout', 'sms-code', 'service-agreement-detail', 'up-password'];
  25. /**
  26. * @var string|null
  27. */
  28. public $withdraw_income = null;
  29. public function actionLogin()
  30. {
  31. try {
  32. if (Yii::$app->request->isPost) {
  33. $params = Yii::$app->request->post();
  34. $res = Yii::$app->services->validate->validate($params, [
  35. [['username'], 'required'],
  36. [['captcha', 'password'], 'safe'],
  37. ]);
  38. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  39. $where[] = ['mall_id' => $this->mall_id];
  40. $where[] = ['status' => StatusEnum::ENABLED];
  41. $where[] = ['mobile' => $params['username']];
  42. $mall_user = User::getUser($where, '*', [], 0);
  43. if (!empty($mall_user)) $user = $mall_user->toArray();
  44. if (empty($user)) {
  45. // 用户不存在,检查是否启用对外部接口用户数据获取
  46. if (MallAddons::isInstall($this->mall_id, 'Salary') && Yii::$app->services->setting->addons->get($this->mall_id, 'Salary', 'basic.is_external')) {
  47. $user = UserLogic::register($this->mall_id, $params['username']);
  48. } else {
  49. throw new \yii\db\Exception('账号不存在');
  50. }
  51. } else {
  52. // 用户已存在,更新用户最新的账号金额信息
  53. if (MallAddons::isInstall($this->mall_id, 'Salary') && Yii::$app->services->setting->addons->get($this->mall_id, 'Salary', 'basic.is_external')) {
  54. UserLogic::updateUser($user);
  55. }
  56. }
  57. if (empty($params['password']) && empty($params['captcha'])) throw new \yii\db\Exception('必须选择一种登录方式');
  58. if (!empty($params['password'])) {
  59. if (!\Yii::$app->getSecurity()->validatePassword(trim($params['password']), $user['password'])) throw new Exception('密码错误');
  60. } else {
  61. if (YII_ENV == 'prod') {
  62. if (!Sms::checkValidateCode($params['username'], $params['captcha'], Sms::SMS_TYPE_USER_LOGIN)) throw new Exception('验证码不正确');
  63. }
  64. }
  65. $token = Yii::$app->services->apiAccessToken->getAccessToken($mall_user, 'api');
  66. $token = $token['access_token'];
  67. return $this->success('登录成功', ['userwagesToken' => $token]);
  68. }
  69. if (Yii::$app->request->isGet) {
  70. $service_agreement_list = [];
  71. $agreements = Yii::$app->services->setting->addons->get($this->mall_id, 'Salary', 'basic.agreements');
  72. if (!empty($agreements)) {
  73. $service_agreement_ids = json_decode($agreements, true);
  74. $service_agreement_list = ServiceAgreement::lists([
  75. 'where' => [
  76. ['id' => $service_agreement_ids],
  77. ['mall_id' => $this->mall_id],
  78. ['status' => StatusEnum::ENABLED]
  79. ],
  80. 'select' => 'id,name',
  81. 'order' => 'sort asc,id desc'
  82. ]);
  83. }
  84. return $this->success('登录成功', ['service_agreement_list' => $service_agreement_list]);
  85. }
  86. } catch (\Exception $e) {
  87. return $this->error($e->getMessage());
  88. }
  89. }
  90. public function actionSmsCode()
  91. {
  92. $params = Yii::$app->request->post();
  93. $res = Yii::$app->services->validate->validate($params, [
  94. [['mobile'], 'required', 'message' => '请输入手机号码'],
  95. [['mobile', 'area_code'], 'integer'],
  96. [['sms_type'], 'string'],
  97. ]);
  98. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  99. $area_code = '';// 初始化区号
  100. $gateways = 'domestic';// 默认国内短信
  101. // 检测是登录并且区号不为空则使用国际短信
  102. if (!empty($this->user) && !empty($this->user->area_code)) {
  103. $gateways = 'international';
  104. $area_code = $this->user->area_code;
  105. }
  106. // 判断是否有区号
  107. if (!empty($params['area_code']) && $params['area_code'] != 86) {
  108. $gateways = 'international';
  109. $area_code = $params['area_code'];
  110. }
  111. $params['sms_type'] = $params['sms_type'] ?: '';
  112. $sms = new Sms($this->mall_id, $gateways);
  113. try {
  114. if ($sms->sendCaptcha($params['mobile'], $area_code, Sms::SMS_TYPE_USER_LOGIN) === false) return $this->error('发送失败');
  115. }catch (\Exception $e){
  116. return $this->error($e->getMessage());
  117. }
  118. return $this->success('发送成功');
  119. }
  120. public function actionLogout()
  121. {
  122. if (Yii::$app->services->apiAccessToken->disableByAccessToken(Yii::$app->user->identity->access_token)) return $this->success('退出成功');
  123. return $this->error('退出失败');
  124. }
  125. public function actionServiceAgreementDetail()
  126. {
  127. $params = Yii::$app->request->get();
  128. $res = Yii::$app->services->validate->validate($params, [
  129. [['id'], 'required', 'message' => '请输入id'],
  130. [['id'], 'integer'],
  131. ]);
  132. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  133. $service_agreement = ServiceAgreement::getOne([
  134. ['id' => $params['id']],
  135. ['mall_id' => $this->mall_id],
  136. ['status' => StatusEnum::ENABLED]
  137. ], true, [], false, 'id,name,content');
  138. return $this->success('成功', $service_agreement ?? []);
  139. }
  140. public function actionUpPassword()
  141. {
  142. try {
  143. if (\Yii::$app->request->isPost) {
  144. $params = Yii::$app->request->post();
  145. $res = Yii::$app->services->validate->validate($params, [
  146. [['mobile', 'captcha', 'password'], 'required'],
  147. ['mobile', 'match', 'pattern' => '/^1\d{10}$/', 'message' => '请输入正确的手机号码'],
  148. [['mobile'], 'integer'],
  149. ['password', 'string', 'length' => [6, 11]],
  150. ]);
  151. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  152. $user = User::find()->where(['mobile' => $params['mobile'], 'mall_id' => $this->mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])->one();
  153. //短信验证码
  154. if (YII_ENV == 'prod') {
  155. if (!Sms::checkValidateCode($params['mobile'], $params['captcha'])) return $this->error('验证码不正确');
  156. }
  157. $user->password = Yii::$app->getSecurity()->generatePasswordHash(trim($params['password']));
  158. if ($user->save() === false) return $this->error(User::getStaticError());
  159. return $this->success('设置成功');
  160. }
  161. } catch (\Exception $e) {
  162. return $this->error($e->getMessage());
  163. }
  164. }
  165. }