Login.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\cashier;
  12. use app\Request;
  13. use app\services\store\SystemStoreStaffServices;
  14. use crmeb\services\wechat\config\WorkConfig;
  15. use crmeb\services\wechat\OfficialAccount;
  16. use crmeb\services\wechat\Work;
  17. use crmeb\utils\Captcha;
  18. use crmeb\services\CacheService;
  19. use app\services\cashier\LoginServices;
  20. use think\exception\ValidateException;
  21. use app\validate\api\user\RegisterValidates;
  22. use think\facade\Cache;
  23. use think\facade\Config;
  24. /**
  25. * 登录
  26. * Class AuthController
  27. * @package app\api\controller
  28. */
  29. class Login
  30. {
  31. /**
  32. * 扫码登录缓存前缀
  33. * @var string
  34. */
  35. protected $scan_cache_prefix = '_scan_login:';
  36. /**
  37. * 二维码类型
  38. * @var string[]
  39. */
  40. protected $scan_type = [
  41. 1 => 'wechat',
  42. 2 => 'work'
  43. ];
  44. /**
  45. * @var LoginServices|null
  46. */
  47. protected $services = NUll;
  48. /**
  49. * LoginController constructor.
  50. * @param LoginServices $services
  51. */
  52. public function __construct(LoginServices $services)
  53. {
  54. $this->services = $services;
  55. }
  56. /**
  57. * @param Request $request
  58. * @return mixed
  59. * @author 等风来
  60. * @email 136327134@qq.com
  61. * @date 2022/10/11
  62. */
  63. public function getAjCaptcha(Request $request)
  64. {
  65. [$account,] = $request->postMore([
  66. 'account',
  67. ], true);
  68. $key = 'cashier_login_captcha_' . $account;
  69. return app('json')->success(['is_captcha' => Cache::get($key) > 2]);
  70. }
  71. /**
  72. * @return mixed
  73. */
  74. public function ajcaptcha(Request $request)
  75. {
  76. $captchaType = $request->get('captchaType');
  77. return app('json')->success(aj_captcha_create($captchaType));
  78. }
  79. /**
  80. * 一次验证
  81. * @return mixed
  82. */
  83. public function ajcheck(Request $request)
  84. {
  85. [$token, $pointJson, $captchaType] = $request->postMore([
  86. ['token', ''],
  87. ['pointJson', ''],
  88. ['captchaType', ''],
  89. ], true);
  90. try {
  91. aj_captcha_check_one($captchaType, $token, $pointJson);
  92. return app('json')->success();
  93. } catch (\Throwable $e) {
  94. return app('json')->fail(400336);
  95. }
  96. }
  97. /**
  98. * 获取后台登录页轮播图以及LOGO
  99. * @return mixed
  100. */
  101. public function info()
  102. {
  103. return app('json')->success($this->services->getLoginInfo());
  104. }
  105. /**
  106. * 验证码
  107. * @return \app\controller\admin\Login|\think\Response
  108. */
  109. public function captcha()
  110. {
  111. return app()->make(Captcha::class)->create();
  112. }
  113. /**
  114. * H5账号登陆
  115. * @param Request $request
  116. * @return mixed
  117. * @throws \think\db\exception\DataNotFoundException
  118. * @throws \think\db\exception\ModelNotFoundException
  119. * @throws \think\exception\DbException
  120. */
  121. public function login(Request $request)
  122. {
  123. [$account, $password, $captchaType, $captchaVerification] = $request->postMore([
  124. 'account',
  125. 'pwd',
  126. ['captchaType', ''],
  127. ['captchaVerification', '']
  128. ], true);
  129. validate(\app\validate\cashier\LoginValidate::class)->check(['account' => $account, 'pwd' => $password]);
  130. $key = 'cashier_login_captcha_' . $account;
  131. if (Cache::has($key) && Cache::get($key) > 2) {
  132. if (!$captchaType || !$captchaVerification) {
  133. return app('json')->fail('请拖动滑块验证');
  134. }
  135. //二次验证
  136. try {
  137. aj_captcha_check_two($captchaType, $captchaVerification);
  138. } catch (\Throwable $e) {
  139. return app('json')->fail($e->getError());
  140. }
  141. }
  142. $res = $this->services->login($account, $password, 'cashier');
  143. if ($res) {
  144. Cache::delete($key);
  145. }
  146. return app('json')->success($res);
  147. }
  148. /**
  149. * 微信扫码登录
  150. * @return mixed
  151. */
  152. public function wechatScanLogin()
  153. {
  154. $qrcode = OfficialAccount::qrcodeService();
  155. mt_srand();
  156. $key = md5(time() . uniqid(true, false) . mt_rand(1, 10000));
  157. $timeout = 600;
  158. CacheService::set('wechat' . $this->scan_cache_prefix . $key, 0, 600);
  159. $data = $qrcode->temporary('wechat_scan_login:' . $key, 30 * 24 * 3600);
  160. return app('json')->success(['timeout' => $timeout, 'key' => $key, 'qrcode' => $qrcode->url($data['ticket'])]);
  161. }
  162. /**
  163. * 获取配置
  164. * @param WorkConfig $config
  165. * @return mixed
  166. */
  167. public function getWechatConfig(WorkConfig $config)
  168. {
  169. $workCorpId = $config->get('corpId');
  170. $config = $config->getAppConfig(WorkConfig::TYPE_USER_APP);
  171. return app('json')->success([
  172. 'work_corp_id' => $workCorpId,
  173. 'work_agent_id' => $config['agent_id'] ?? '',
  174. ]);
  175. }
  176. /**
  177. * 企业微信扫码登录
  178. * @return mixed
  179. */
  180. public function workScanLogin()
  181. {
  182. $userInfo = Work::getAuthUserInfo();
  183. return app('json')->success($this->services->workScanLogin($userInfo));
  184. }
  185. /**
  186. * 检测获取用户信息
  187. * @return mixed
  188. */
  189. public function checkScanLogin(Request $request, SystemStoreStaffServices $systemStoreStaffServices)
  190. {
  191. [$key, $type] = $request->postMore([
  192. ['key', ''],
  193. ['type', '1']
  194. ], true);
  195. if ($key) {
  196. $type = $this->scan_type[$type] ?? 'wechat';
  197. $uid = CacheService::get($type . $this->scan_cache_prefix . $key);
  198. if ($uid) {
  199. CacheService::delete($type . $this->scan_cache_prefix . $key);
  200. try {
  201. $staffInfo = $systemStoreStaffServices->getStaffInfoByUid((int)$uid);
  202. } catch (\Throwable $e) {
  203. return app('json')->fail('未登录');
  204. }
  205. return app('json')->success($this->services->getLoginResult($staffInfo['id'], 'cashier', $staffInfo));
  206. }
  207. }
  208. return app('json')->fail('未登录');
  209. }
  210. /**
  211. * 退出登录
  212. * @param Request $request
  213. * @return mixed
  214. * @throws \Psr\SimpleCache\InvalidArgumentException
  215. */
  216. public function logout(Request $request)
  217. {
  218. $key = trim(ltrim($request->header(Config::get('cookie.token_name')), 'Bearer'));
  219. CacheService::redisHandler()->delete(md5($key));
  220. CacheService::redisHandler(CacheService::CASHIER_AUX_SCREEN_TAG . '_' . $request->storeId())->clear();
  221. return app('json')->success();
  222. }
  223. /**
  224. * 密码修改
  225. * @param Request $request
  226. * @return mixed
  227. */
  228. public function reset(Request $request)
  229. {
  230. [$account, $captcha, $password] = $request->postMore([['account', ''], ['captcha', ''], ['password', '']], true);
  231. try {
  232. validate(RegisterValidates::class)->scene('register')->check(['account' => $account, 'captcha' => $captcha, 'password' => $password]);
  233. } catch (ValidateException $e) {
  234. return app('json')->fail($e->getError());
  235. }
  236. $verifyCode = CacheService::get('code_' . $account);
  237. if (!$verifyCode)
  238. return app('json')->fail('请先获取验证码');
  239. $verifyCode = substr($verifyCode, 0, 6);
  240. if ($verifyCode != $captcha) {
  241. return app('json')->fail('验证码错误');
  242. }
  243. if (strlen(trim($password)) < 4 || strlen(trim($password)) > 64)
  244. return app('json')->fail('密码必须是在4到64位之间');
  245. if ($password == '123456') return app('json')->fail('密码太过简单,请输入较为复杂的密码');
  246. $resetStatus = $this->services->reset($account, $password);
  247. if ($resetStatus) {
  248. CacheService::delete('code_' . $account);
  249. return app('json')->success('修改成功');
  250. }
  251. return app('json')->fail('修改失败');
  252. }
  253. }