AuthController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. namespace backend\modules\admin\controllers;
  3. use backend\controllers\AdminController;
  4. use common\enums\StatusEnum;
  5. use common\helpers\sms\Sms;
  6. use common\models\backend\Admin;
  7. use common\models\mall\Mall;
  8. use Yii;
  9. use yii\filters\AccessControl;
  10. use yii\filters\VerbFilter;
  11. use common\models\common\PlatformSetting;
  12. use common\models\common\MallSetting;
  13. /**
  14. *
  15. * 管理员
  16. * @Author bing
  17. * @DateTime 2021-08-11 17:22:39
  18. * @copyright: Copyright (c) 2020 广东七件事集团
  19. */
  20. class AuthController extends AdminController
  21. {
  22. public $layout = false;
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function behaviors()
  27. {
  28. return [
  29. 'access' => [
  30. 'class' => AccessControl::class,
  31. 'rules' => [
  32. [
  33. 'actions' => ['login','logout', 'error', 'captcha','register', 'expired', 'register-sms-code','login-sms-code'],
  34. 'allow' => true,
  35. ],
  36. ],
  37. ],
  38. ];
  39. }
  40. public function actions()
  41. {
  42. return [
  43. 'error' => [
  44. 'class' => 'yii\web\ErrorAction',
  45. ],
  46. 'captcha' => [
  47. 'class' => 'common\components\CaptchaAction',
  48. 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  49. 'maxLength' => 5, // 最大显示个数
  50. 'minLength' => 5, // 最少显示个数
  51. 'padding' => 5, // 间距
  52. 'height' => 32, // 高度
  53. 'width' => 100, // 宽度
  54. 'offset' => 4, // 设置字符偏移量
  55. 'backColor' => 0xffffff, // 背景颜色
  56. 'foreColor' => 0x2F4F4F, // 字体颜色
  57. ]
  58. ];
  59. }
  60. /**
  61. * 登录
  62. * @Author bing
  63. * @DateTime 2021-08-11 18:45:46
  64. * @copyright: Copyright (c) 2020 广东七件事集团
  65. * @return void
  66. */
  67. public function actionLogin()
  68. {
  69. $system = \Yii::$app->services->setting->platform->get('system',true);
  70. $open_mobile_login = 0;
  71. $mall_id = 0;
  72. if (!Yii::$app->user->isGuest) {
  73. // 记录行为日志
  74. // Yii::$app->services->actionLog->create('login', '自动登录', false);
  75. return $this->goHome();
  76. }
  77. $login_type = Yii::$app->request->get('login_type');
  78. $mall_sign = Yii::$app->request->get('mall_sign');
  79. if(!empty($login_type)){
  80. if($login_type!='mall')$login_type = base64_decode($login_type);
  81. }else{
  82. $login_type = 'mall';
  83. }
  84. if(!empty($mall_sign)){
  85. $mall = Mall::find()->where(['mall_sign'=>$mall_sign,'status'=>StatusEnum::ENABLED])->one();
  86. if(!$mall){
  87. return $this->error('登录网址有误');
  88. }
  89. $mall_id = $mall['id'];
  90. $login_setting = Yii::$app->services->setting->mall->get($mall['id'], 'login');
  91. $sms = MallSetting::getConfByGroup($mall_id, ['notice_sms'], true);
  92. if(isset($login_setting['open_sms_login']) && $login_setting['open_sms_login'] == 1 && isset($sms['notice_sms']['status']['value']) && $sms['notice_sms']['status']['value'] == 1){
  93. $open_mobile_login = 1;
  94. }
  95. $login_type = 'mall';
  96. }else{
  97. $sms = PlatformSetting::getConfByGroup('sms', true);
  98. if(isset($system['open_mobile_login_verify']) && $system['open_mobile_login_verify'] == 1 && isset($sms['status']['value']) && $sms['status']['value'] == 1){
  99. $open_mobile_login = 1;
  100. }
  101. }
  102. if(Yii::$app->request->isPost){
  103. $post = Yii::$app->request->post();
  104. if($open_mobile_login == 1){
  105. $requireParam = ['username','admin_type','is_remember','mobile','mobile_captcha'];
  106. }else{
  107. $requireParam = ['captcha','username','admin_type','is_remember'];
  108. }
  109. $res = self::checkRequireParam($post,$requireParam);
  110. if($res === false) return $this->error(self::getStaticError());
  111. if (YII_ENV != 'dev'){
  112. if($open_mobile_login == 0){
  113. if (!$this->createAction('captcha')->validate($post['captcha'], false)) return $this->error('验证码错误');
  114. }else{
  115. $mobile_captcha = \Yii::$app->cache->get($post['mobile'].'_AdminLogin_'. $mall_id);
  116. if ($mobile_captcha != $post['mobile_captcha']) return $this->error('验证码错误');
  117. }
  118. }
  119. if(!empty($post['mall_id'])) $post['mall_id'] = base64_decode($post['mall_id']);
  120. $data = Admin::login($post);
  121. if($data === false) return $this->error(Admin::getStaticError());
  122. return $this->success('登录成功',$data);
  123. }
  124. if(!in_array($login_type,['admin','agent','mall'])) return $this->error('登录类型错误');
  125. $system['open_mobile_login'] = $open_mobile_login;
  126. $system['mall_id'] = $mall_id;
  127. return $this->render($this->action->id,compact('login_type','system'));
  128. }
  129. /**
  130. * @desc:商家注册
  131. * @Author: hua
  132. * @Date: 2022/2/22
  133. * @Time: 15:46
  134. * @Copyright: copyright (c) 2021 广东七件事集团
  135. * @return mixed|string|void
  136. */
  137. public function actionRegister()
  138. {
  139. if(Yii::$app->request->isPost){
  140. $open_register = \Yii::$app->services->setting->platform->get('system.open_register',true);
  141. if(empty($open_register)) return $this->error('注册功能未开启');
  142. $post = Yii::$app->request->post();
  143. $res = Yii::$app->services->validate->validate($post, [
  144. [['username'], 'required', 'message' => '请输入手机号码'],
  145. [['password'], 'required', 'message' => '请输入密码'],
  146. [['confirm_password'], 'required', 'message' => '请输入确认密码'],
  147. [['name'], 'string'],
  148. [['mobile_captcha'], 'required', 'message' => '请输入验证码'],
  149. ['username', 'match', 'pattern' => '/^1\d{10}$/', 'message' => '请输入正确的手机号码'],
  150. [['reference_id'], 'safe'],
  151. ]);
  152. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  153. if($post['password']!=$post['confirm_password']) return $this->error('密码不一致');
  154. if (YII_ENV == 'prod') {
  155. if (!Sms::checkValidateCode($post['username'], $post['mobile_captcha'])) return $this->error('输入的手机验证码不正确');
  156. }
  157. $admin = Admin::getOne([
  158. ['username'=>$post['username']],
  159. ['status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]],
  160. ['<>','mall_id',0]
  161. ]);
  162. if(!empty($admin)) return $this->error('该手机号码已被使用');
  163. $register_manage_setting = \Yii::$app->services->setting->platform->get('register_manage');
  164. $expired_at = 0;
  165. if(!empty($register_manage_setting['expire'])){
  166. $expired_at = time()+$register_manage_setting['expire']*24*3600;
  167. }
  168. $params['name'] = empty($post['name']) ? '商城系统' : $post['name'];
  169. $params['reference_id'] = !empty($post['reference_id'])?$post['reference_id']:0;
  170. $params['mobile'] = $post['username'];
  171. $params['password'] = $post['password'];
  172. $params['expired_at'] = $expired_at;
  173. $params['source'] = 1;
  174. $res = Mall::setData($params);
  175. if ($res === false) return $this->error(Mall::getStaticError());
  176. $login_url = \Yii::$app->urlManager->createAbsoluteUrl('admin/auth/login');
  177. return $this->success('注册成功',['login_url'=>$login_url]);
  178. }
  179. $is_h5 = Yii::$app->request->get('h5','');
  180. $system = Yii::$app->services->setting->platform->get('system',true);
  181. return $is_h5? $this->renderPartial('register_h5'):$this->render($this->action->id,compact('system'));
  182. }
  183. /**
  184. * 获取注册短信验证码
  185. * @return mixed|void
  186. * @throws \Overtrue\EasySms\Exceptions\NoGatewayAvailableException
  187. */
  188. public function actionRegisterSmsCode()
  189. {
  190. $sms = new Sms(0, 'domestic', 'admin');
  191. $params = Yii::$app->request->post();
  192. $res = Yii::$app->services->validate->validate($params, [
  193. [['mobile'], 'required', 'message' => '请输入手机号码'],
  194. ['mobile', 'match', 'pattern' => '/^1\d{10}$/', 'message' => '请输入正确的手机号码'],
  195. ]);
  196. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  197. $cache_key = 'RegisterSmsCode_'.$params['mobile'];
  198. if (\Yii::$app->cache->get($cache_key)) {
  199. return $this->error('一分钟之后再试');
  200. }
  201. try {
  202. if ($sms->sendCaptcha($params['mobile']) === false) return $this->error('发送失败');
  203. }catch (\Exception $e){
  204. return $this->error($e->getMessage());
  205. }
  206. \Yii::$app->cache->set($cache_key, 1, 60);
  207. return $this->success('发送成功');
  208. }
  209. /**
  210. * 退出登录
  211. * @Author bing
  212. * @DateTime 2021-08-11 18:45:57
  213. * @copyright: Copyright (c) 2020 广东七件事集团
  214. * @return void
  215. */
  216. public function actionLogout()
  217. {
  218. Yii::$app->user->logout();
  219. $admin_type = $this->admin->admin_type;
  220. $mall_id = $this->admin->mall_id;
  221. $mall = Mall::find()->where(['id'=>$mall_id])->one();
  222. switch ($admin_type) {
  223. case Admin::ADMIN_TYPE_SUPER:
  224. $redirect_url = \Yii::$app->urlManager->createAbsoluteUrl(['admin/auth/login','login_type'=>Yii::$app->params['login_type']]);
  225. break;
  226. case Admin::ADMIN_TYPE_AGENT:
  227. $redirect_url = 'admin/auth/login';
  228. break;
  229. case Admin::ADMIN_TYPE_OPERATE:
  230. $mall_id = base64_encode($mall_id);
  231. $login_type = base64_encode('mall');
  232. $redirect_url = \Yii::$app->urlManager->createAbsoluteUrl(['admin/auth/login','mall_sign'=>$mall['mall_sign']]);
  233. break;
  234. default:
  235. $redirect_url = 'admin/auth/login';
  236. }
  237. return $this->success('操作成功',['url'=>$redirect_url]);
  238. }
  239. /**
  240. * 站点已过期
  241. *
  242. * @return mixed
  243. */
  244. public function actionExpired()
  245. {
  246. return $this->render($this->action->id);
  247. }
  248. public function actionLoginSmsCode()
  249. {
  250. $params = Yii::$app->request->post();
  251. $res = Yii::$app->services->validate->validate($params, [
  252. [['mobile'], 'required', 'message' => '请输入手机号码'],
  253. [['username'], 'required', 'message' => '请输入用户名'],
  254. ['mobile', 'match', 'pattern' => '/^1\d{10}$/', 'message' => '请输入正确的手机号码'],
  255. [['mall_id'], 'safe'],
  256. ]);
  257. $admin = Admin::find()->where(['mobile'=>$params['mobile'],'status'=>StatusEnum::ENABLED])->one();
  258. if(!$admin){
  259. return $this->error('手机号不存在');
  260. }
  261. if($admin->username!=$params['username'])
  262. {
  263. return $this->error('手机号与用户名不匹配');
  264. }
  265. if($params['mall_id'] > 0){
  266. $sms = new Sms($params['mall_id'],'domestic');
  267. }else{
  268. $sms = new Sms($admin['mall_id'],'domestic','platform');
  269. $params['mall_id']=0;
  270. }
  271. Sms::$validityMinutes =1;//一分钟有效
  272. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  273. $str = 'AdminLogin_'.$params['mall_id'];
  274. $cache_key = $params['mobile'].'_'.$str;
  275. if (\Yii::$app->cache->get($cache_key)) {
  276. return $this->error('一分钟之后再试');
  277. }
  278. try {
  279. if ($sms->sendCaptcha($params['mobile'],'',$str) === false) return $this->error('发送失败');
  280. }catch (\Exception $e){
  281. return $this->error($e->getMessage());
  282. }
  283. return $this->success('发送成功');
  284. }
  285. }