Auth.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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\api;
  12. use app\common\repositories\user\UserRepository;
  13. use app\common\repositories\wechat\RoutineQrcodeRepository;
  14. use app\common\repositories\wechat\WechatUserRepository;
  15. use app\validate\api\ChangePasswordValidate;
  16. use app\validate\api\UserAuthValidate;
  17. use crmeb\basic\BaseController;
  18. use crmeb\services\MiniProgramService;
  19. use crmeb\services\WechatService;
  20. use crmeb\services\YunxinSmsService;
  21. use Exception;
  22. use Gregwar\Captcha\CaptchaBuilder;
  23. use Gregwar\Captcha\PhraseBuilder;
  24. use Symfony\Component\HttpFoundation\Request;
  25. use think\db\exception\DataNotFoundException;
  26. use think\db\exception\DbException;
  27. use think\db\exception\ModelNotFoundException;
  28. use think\facade\Cache;
  29. /**
  30. * Class Auth
  31. * @package app\controller\api
  32. * @author xaboy
  33. * @day 2020-05-06
  34. */
  35. class Auth extends BaseController
  36. {
  37. public function test()
  38. {
  39. Cache::set('_scan_login1cb532170a4bcc80938a13e9d41a567e', 7233);
  40. var_dump(Cache::get('_scan_login1cb532170a4bcc80938a13e9d41a567e'));
  41. }
  42. /**
  43. * @param UserRepository $repository
  44. * @return mixed
  45. * @throws DbException
  46. * @author xaboy
  47. * @day 2020/6/1
  48. */
  49. public function login(UserRepository $repository)
  50. {
  51. $account = $this->request->param('account');
  52. if (!$account)
  53. return app('json')->fail('请输入账号');
  54. $user = $repository->accountByUser($this->request->param('account'));
  55. if (!$user)
  56. return app('json')->fail('账号或密码错误');
  57. if (!password_verify($pwd = (string)$this->request->param('password'), $user['pwd']))
  58. return app('json')->fail('账号或密码错误');
  59. $user = $repository->mainUser($user);
  60. $pid = $this->request->param('spread', 0);
  61. $repository->bindSpread($user, intval($pid));
  62. $tokenInfo = $repository->createToken($user);
  63. $repository->loginAfter($user);
  64. return app('json')->success($repository->returnToken($user, $tokenInfo));
  65. }
  66. /**
  67. * @return mixed
  68. * @author xaboy
  69. * @day 2020/6/1
  70. */
  71. public function userInfo()
  72. {
  73. $user = $this->request->userInfo()->hidden(['label_id', 'group_id', 'pwd', 'addres', 'card_id', 'last_time', 'last_ip', 'create_time', 'mark', 'status', 'spread_uid', 'spread_time', 'real_name', 'birthday', 'brokerage_price']);
  74. $user->append(['service', 'total_collect_product', 'total_collect_store', 'total_coupon', 'total_visit_product', 'total_unread', 'total_recharge']);
  75. $data = $user->toArray();
  76. $data['total_consume'] = $user['pay_price'];
  77. $data['extension_status'] = systemConfig('extension_status');
  78. return app('json')->success($data);
  79. }
  80. /**
  81. * @param UserRepository $repository
  82. * @return mixed
  83. * @author xaboy
  84. * @day 2020/6/1
  85. */
  86. public function logout(UserRepository $repository)
  87. {
  88. $repository->clearToken($this->request->token());
  89. return app('json')->success('退出登录');
  90. }
  91. /**
  92. * @return mixed
  93. * @throws DataNotFoundException
  94. * @throws DbException
  95. * @throws ModelNotFoundException
  96. * @author xaboy
  97. * @day 2020-05-11
  98. */
  99. public function auth()
  100. {
  101. $request = $this->request;
  102. $oauth = WechatService::create()->getApplication()->oauth;
  103. $oauth->setRequest(new Request($request->get(), $request->post(), [], [], [], $request->server(), $request->getContent()));
  104. try {
  105. $wechatInfo = $oauth->user()->getOriginal();
  106. } catch (Exception $e) {
  107. return app('json')->fail('授权失败[001]', ['message' => $e->getMessage()]);
  108. }
  109. if (!isset($wechatInfo['nickname'])) {
  110. return app('json')->fail('授权失败[002]');
  111. }
  112. /** @var WechatUserRepository $make */
  113. $make = app()->make(WechatUserRepository::class);
  114. $user = $make->syncUser($wechatInfo['openid'], $wechatInfo);
  115. if (!$user)
  116. return app('json')->fail('授权失败[003]');
  117. /** @var UserRepository $make */
  118. $userRepository = app()->make(UserRepository::class);
  119. $user[1] = $userRepository->mainUser($user[1]);
  120. $pid = $this->request->param('spread', 0);
  121. $userRepository->bindSpread($user[1], intval($pid));
  122. $tokenInfo = $userRepository->createToken($user[1]);
  123. $userRepository->loginAfter($user[1]);
  124. return app('json')->success($userRepository->returnToken($user[1], $tokenInfo));
  125. }
  126. /**
  127. * @return mixed
  128. * @throws DataNotFoundException
  129. * @throws DbException
  130. * @throws ModelNotFoundException
  131. * @author xaboy
  132. * @day 2020-05-11
  133. */
  134. public function mpAuth()
  135. {
  136. list($code, $post_cache_key) = $this->request->params([
  137. 'code',
  138. 'cache_key',
  139. ], true);
  140. $session_key = Cache::get('eb_api_code_' . $post_cache_key);
  141. if (!$code && !$session_key)
  142. return app('json')->fail('授权失败,参数有误');
  143. $miniProgramService = MiniProgramService::create();
  144. if ($code && !$session_key) {
  145. try {
  146. $userInfoCong = $miniProgramService->getUserInfo($code);
  147. $session_key = $userInfoCong['session_key'];
  148. $cache_key = md5(time() . $code);
  149. Cache::set('eb_api_code_' . $cache_key, $session_key, 86400);
  150. } catch (Exception $e) {
  151. return app('json')->fail('获取session_key失败,请检查您的配置!', ['line' => $e->getLine(), 'message' => $e->getMessage()]);
  152. }
  153. }
  154. $data = $this->request->params([
  155. ['spread_spid', 0],
  156. ['spread_code', ''],
  157. ['iv', ''],
  158. ['encryptedData', ''],
  159. ]);
  160. try {
  161. //解密获取用户信息
  162. $userInfo = $miniProgramService->encryptor($session_key, $data['iv'], $data['encryptedData']);
  163. } catch (Exception $e) {
  164. if ($e->getCode() == '-41003') return app('json')->fail('获取会话密匙失败', ['line' => $e->getLine(), 'message' => $e->getMessage()]);
  165. throw $e;
  166. }
  167. if (!$userInfo) return app('json')->fail('openid获取失败');
  168. if (!isset($userInfo['openId'])) $userInfo['openId'] = $userInfoCong['openid'] ?? '';
  169. $userInfo['unionId'] = $userInfoCong['unionid'] ?? $userInfo['unionId'] ?? '';
  170. if (!$userInfo['openId']) return app('json')->fail('openid获取失败');
  171. /** @var WechatUserRepository $make */
  172. $make = app()->make(WechatUserRepository::class);
  173. $user = $make->syncRoutineUser($userInfo['openId'], $userInfo);
  174. if (!$user)
  175. return app('json')->fail('授权失败');
  176. /** @var UserRepository $make */
  177. $userRepository = app()->make(UserRepository::class);
  178. $user[1] = $userRepository->mainUser($user[1]);
  179. $code = intval($data['spread_code']['id'] ?? $data['spread_code']);
  180. //获取是否有扫码进小程序
  181. if ($code && ($info = app()->make(RoutineQrcodeRepository::class)->getRoutineQrcodeFindType($code))) {
  182. $data['spread_spid'] = $info['third_id'];
  183. }
  184. $userRepository->bindSpread($user[1], intval($data['spread_spid']));
  185. $tokenInfo = $userRepository->createToken($user[1]);
  186. $userRepository->loginAfter($user[1]);
  187. return app('json')->success($userRepository->returnToken($user[1], $tokenInfo));
  188. }
  189. public function getCaptcha()
  190. {
  191. $codeBuilder = new CaptchaBuilder(null, new PhraseBuilder(4));
  192. $key = uniqid(microtime(true), true);
  193. Cache::set('api_captche' . $key, $codeBuilder->getPhrase(), 300);
  194. $captcha = $codeBuilder->build()->inline();
  195. $code = $codeBuilder->getPhrase();
  196. return app('json')->success(compact('key', 'code', 'captcha'));
  197. }
  198. protected function checkCaptcha($uni, string $code): bool
  199. {
  200. $cacheName = 'api_captche' . $uni;
  201. if (!Cache::has($cacheName)) return false;
  202. $key = Cache::get($cacheName);
  203. $code = mb_strtolower($code, 'UTF-8');
  204. $res = password_verify($code, $key);
  205. if ($res) Cache::delete($cacheName);
  206. return $res;
  207. }
  208. public function verify(UserAuthValidate $validate)
  209. {
  210. $data = $this->request->params(['phone', 'code', 'key', ['type', 'login']]);
  211. $validate->sceneVerify()->check($data);
  212. $sms_num_key = 'api.auth.num.' . $data['phone'];
  213. $num = Cache::get($sms_num_key) ? Cache::get($sms_num_key) : 0;
  214. if ($num > 2) {
  215. if (!$data['code'])
  216. return app('json')->make(402, '请输入验证码');
  217. if (!$this->checkCaptcha($data['key'], $data['code']))
  218. return app('json')->fail('验证码输入有误');
  219. }
  220. $sms = (YunxinSmsService::create());
  221. // if(!env('APP_DEBUG', false)){
  222. try {
  223. $sms_code = str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT);
  224. $sms_time = systemConfig('sms_time') ? systemConfig('sms_time') : 30;
  225. $sms->send($data['phone'], 'VERIFICATION_CODE', ['code' => $sms_code, 'time' => $sms_time]);
  226. } catch (Exception $e) {
  227. return app('json')->fail($e->getMessage());
  228. }
  229. // }else{
  230. // $sms_code = 1234;
  231. // $sms_time = 5;
  232. // }
  233. $sms_key = $sms->sendSmsKey($data['phone'], $data['type']);
  234. Cache::set($sms_key, $sms_code, $sms_time * 60);
  235. Cache::set($sms_num_key, $num + 1, 300);
  236. //'短信发送成功'
  237. return app('json')->success('短信发送成功');
  238. }
  239. public function smsLogin(UserAuthValidate $validate, UserRepository $repository)
  240. {
  241. $data = $this->request->params(['phone', 'sms_code', 'spread']);
  242. $validate->sceneSmslogin()->check($data);
  243. // if (!(YunxinSmsService::create())->checkSmsCode($data['phone'], $data['sms_code'], 'login'))
  244. // return app('json')->fail('验证码不正确');
  245. $user = $repository->accountByUser($data['phone']);
  246. if (!$user) $user = $repository->registr($data['phone'], null);
  247. $user = $repository->mainUser($user);
  248. $repository->bindSpread($user, intval($data['spread']));
  249. $tokenInfo = $repository->createToken($user);
  250. $repository->loginAfter($user);
  251. return app('json')->success($repository->returnToken($user, $tokenInfo));
  252. }
  253. public function changePassword(ChangePasswordValidate $validate, UserRepository $repository)
  254. {
  255. $data = $this->request->params(['phone', 'sms_code', 'pwd']);
  256. $validate->check($data);
  257. $user = $repository->accountByUser($data['phone']);
  258. if (!$user) return app('json')->fail('用户不存在');
  259. if (!(YunxinSmsService::create())->checkSmsCode($data['phone'], $data['sms_code'], 'change_pwd'))
  260. return app('json')->fail('验证码不正确');
  261. $user->pwd = $repository->encodePassword($data['pwd']);
  262. $user->save();
  263. return app('json')->success('修改成功');
  264. }
  265. public function spread(UserRepository $userRepository)
  266. {
  267. $data = $this->request->params([
  268. ['spread_spid', 0],
  269. ['spread_code', null],
  270. ]);
  271. if (isset($data['spread_code']['id']) && ($info = app()->make(RoutineQrcodeRepository::class)->getRoutineQrcodeFindType($data['spread_code']['id']))) {
  272. $data['spread_spid'] = $info['third_id'];
  273. }
  274. $userRepository->bindSpread($this->request->userInfo(), intval($data['spread_spid']));
  275. return app('json')->success();
  276. }
  277. }