LoginLogic.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\api\logic;
  15. use app\common\cache\WebScanLoginCache;
  16. use app\common\logic\BaseLogic;
  17. use app\api\service\{UserTokenService, WechatUserService};
  18. use app\common\enum\{LoginEnum, user\UserTerminalEnum, YesNoEnum};
  19. use app\common\service\{
  20. ConfigService,
  21. FileService,
  22. wechat\WeChatConfigService,
  23. wechat\WeChatMnpService,
  24. wechat\WeChatOaService,
  25. wechat\WeChatRequestService
  26. };
  27. use app\common\model\user\{User, UserAuth};
  28. use think\facade\{Db, Config};
  29. /**
  30. * 登录逻辑
  31. * Class LoginLogic
  32. * @package app\api\logic
  33. */
  34. class LoginLogic extends BaseLogic
  35. {
  36. /**
  37. * @notes 账号密码注册
  38. * @param array $params
  39. * @return bool
  40. * @author 段誉
  41. * @date 2022/9/7 15:37
  42. */
  43. public static function register(array $params)
  44. {
  45. try {
  46. $userSn = User::createUserSn();
  47. $passwordSalt = Config::get('project.unique_identification');
  48. $password = create_password($params['password'], $passwordSalt);
  49. $avatar = ConfigService::get('default_image', 'user_avatar');
  50. $user = User::create([
  51. 'sn' => $userSn,
  52. 'avatar' => $avatar,
  53. 'nickname' => '用户' . $userSn,
  54. 'account' => $params['account'],
  55. 'password' => $password,
  56. 'channel' => $params['channel'],
  57. ]);
  58. //验证是否传微信小程序的CODE,如果传入进行绑定
  59. if(!empty($params['wx_code']) and $params['channel']==1){
  60. $params['code'] = $params['wx_code'];
  61. $params['user_id'] = $user->id;
  62. self::mnpAuthLogin($params);
  63. }
  64. return true;
  65. } catch (\Exception $e) {
  66. self::setError($e->getMessage());
  67. return false;
  68. }
  69. }
  70. /**
  71. * @notes 账号/手机号登录,手机号验证码
  72. * @param $params
  73. * @return array|false
  74. * @author 段誉
  75. * @date 2022/9/6 19:26
  76. */
  77. public static function login($params)
  78. {
  79. try {
  80. // 账号/手机号 密码登录
  81. $where = ['account|mobile' => $params['account']];
  82. if ($params['scene'] == LoginEnum::MOBILE_CAPTCHA) {
  83. //手机验证码登录
  84. $where = ['mobile' => $params['account']];
  85. }
  86. $user = User::where($where)->findOrEmpty();
  87. if ($user->isEmpty()) {
  88. throw new \Exception('用户不存在');
  89. }
  90. //更新登录信息
  91. $user->login_time = time();
  92. $user->login_ip = request()->ip();
  93. $user->save();
  94. //设置token
  95. $userInfo = UserTokenService::setToken($user->id, $params['terminal']);
  96. //返回登录信息
  97. $avatar = $user->avatar ?: Config::get('project.default_image.user_avatar');
  98. $avatar = FileService::getFileUrl($avatar);
  99. //验证是否传微信小程序的CODE,如果传入进行绑定
  100. if(!empty($params['wx_code']) and $params['terminal']==1){
  101. $params['code'] = $params['wx_code'];
  102. $params['user_id'] = $user->id;
  103. self::mnpAuthLogin($params);
  104. }
  105. return [
  106. 'nickname' => $userInfo['nickname'],
  107. 'sn' => $userInfo['sn'],
  108. 'mobile' => $userInfo['mobile'],
  109. 'avatar' => $avatar,
  110. 'token' => $userInfo['token'],
  111. ];
  112. } catch (\Exception $e) {
  113. self::setError($e->getMessage());
  114. return false;
  115. }
  116. }
  117. /**
  118. * @notes 退出登录
  119. * @param $userInfo
  120. * @return bool
  121. * @throws \think\db\exception\DataNotFoundException
  122. * @throws \think\db\exception\DbException
  123. * @throws \think\db\exception\ModelNotFoundException
  124. * @author 段誉
  125. * @date 2022/9/16 17:56
  126. */
  127. public static function logout($userInfo)
  128. {
  129. //token不存在,不注销
  130. if (!isset($userInfo['token'])) {
  131. return false;
  132. }
  133. //设置token过期
  134. return UserTokenService::expireToken($userInfo['token']);
  135. }
  136. /**
  137. * @notes 获取微信请求code的链接
  138. * @param string $url
  139. * @return string
  140. * @author 段誉
  141. * @date 2022/9/20 19:47
  142. */
  143. public static function codeUrl(string $url)
  144. {
  145. return (new WeChatOaService())->getCodeUrl($url);
  146. }
  147. /**
  148. * @notes 公众号登录
  149. * @param array $params
  150. * @return array|false
  151. * @throws \GuzzleHttp\Exception\GuzzleException
  152. * @author 段誉
  153. * @date 2022/9/20 19:47
  154. */
  155. public static function oaLogin(array $params)
  156. {
  157. Db::startTrans();
  158. try {
  159. //通过code获取微信 openid
  160. $response = (new WeChatOaService())->getOaResByCode($params['code']);
  161. $userServer = new WechatUserService($response, UserTerminalEnum::WECHAT_OA);
  162. $userInfo = $userServer->getResopnseByUserInfo()->authUserLogin()->getUserInfo();
  163. // 更新登录信息
  164. self::updateLoginInfo($userInfo['id']);
  165. Db::commit();
  166. return $userInfo;
  167. } catch (\Exception $e) {
  168. Db::rollback();
  169. self::$error = $e->getMessage();
  170. return false;
  171. }
  172. }
  173. /**
  174. * @notes 小程序-静默登录
  175. * @param array $params
  176. * @return array|false
  177. * @author 段誉
  178. * @date 2022/9/20 19:47
  179. */
  180. public static function silentLogin(array $params)
  181. {
  182. try {
  183. //通过code获取微信 openid
  184. $response = (new WeChatMnpService())->getMnpResByCode($params['code']);
  185. $userServer = new WechatUserService($response, UserTerminalEnum::WECHAT_MMP);
  186. $userInfo = $userServer->getResopnseByUserInfo('silent')->getUserInfo();
  187. if (!empty($userInfo)) {
  188. // 更新登录信息
  189. self::updateLoginInfo($userInfo['id']);
  190. }
  191. return $userInfo;
  192. } catch (\Exception $e) {
  193. self::$error = $e->getMessage();
  194. return false;
  195. }
  196. }
  197. /**
  198. * @notes 小程序-授权登录
  199. * @param array $params
  200. * @return array|false
  201. * @author 段誉
  202. * @date 2022/9/20 19:47
  203. */
  204. public static function mnpLogin(array $params)
  205. {
  206. Db::startTrans();
  207. try {
  208. //通过code获取微信 openid
  209. $response = (new WeChatMnpService())->getMnpResByCode($params['code']);
  210. $userServer = new WechatUserService($response, UserTerminalEnum::WECHAT_MMP);
  211. $userInfo = $userServer->getResopnseByUserInfo()->authUserLogin()->getUserInfo();
  212. // 更新登录信息
  213. self::updateLoginInfo($userInfo['id']);
  214. Db::commit();
  215. return $userInfo;
  216. } catch (\Exception $e) {
  217. Db::rollback();
  218. self::$error = $e->getMessage();
  219. return false;
  220. }
  221. }
  222. /**
  223. * @notes 更新登录信息
  224. * @param $userId
  225. * @throws \Exception
  226. * @author 段誉
  227. * @date 2022/9/20 19:46
  228. */
  229. public static function updateLoginInfo($userId)
  230. {
  231. $user = User::findOrEmpty($userId);
  232. if ($user->isEmpty()) {
  233. throw new \Exception('用户不存在');
  234. }
  235. $time = time();
  236. $user->login_time = $time;
  237. $user->login_ip = request()->ip();
  238. $user->update_time = $time;
  239. $user->save();
  240. }
  241. /**
  242. * @notes 小程序端绑定微信
  243. * @param array $params
  244. * @return bool
  245. * @author 段誉
  246. * @date 2022/9/20 19:46
  247. */
  248. public static function mnpAuthLogin(array $params)
  249. {
  250. try {
  251. //通过code获取微信openid
  252. $response = (new WeChatMnpService())->getMnpResByCode($params['code']);
  253. $response['user_id'] = $params['user_id'];
  254. $response['terminal'] = UserTerminalEnum::WECHAT_MMP;
  255. return self::createAuth($response);
  256. } catch (\Exception $e) {
  257. self::$error = $e->getMessage();
  258. return false;
  259. }
  260. }
  261. /**
  262. * @notes 公众号端绑定微信
  263. * @param array $params
  264. * @return bool
  265. * @throws \GuzzleHttp\Exception\GuzzleException
  266. * @author 段誉
  267. * @date 2022/9/16 10:43
  268. */
  269. public static function oaAuthLogin(array $params)
  270. {
  271. try {
  272. //通过code获取微信openid
  273. $response = (new WeChatOaService())->getOaResByCode($params['code']);
  274. $response['user_id'] = $params['user_id'];
  275. $response['terminal'] = UserTerminalEnum::WECHAT_OA;
  276. return self::createAuth($response);
  277. } catch (\Exception $e) {
  278. self::$error = $e->getMessage();
  279. return false;
  280. }
  281. }
  282. /**
  283. * @notes 生成授权记录
  284. * @param $response
  285. * @return bool
  286. * @throws \Exception
  287. * @author 段誉
  288. * @date 2022/9/16 10:43
  289. */
  290. public static function createAuth($response)
  291. {
  292. //先检查openid是否有记录
  293. $isAuth = UserAuth::where('openid', '=', $response['openid'])->findOrEmpty();
  294. if (!$isAuth->isEmpty()) {
  295. throw new \Exception('该微信已被绑定');
  296. }
  297. if (isset($response['unionid']) && !empty($response['unionid'])) {
  298. //在用unionid找记录,防止生成两个账号,同个unionid的问题
  299. $userAuth = UserAuth::where(['unionid' => $response['unionid']])
  300. ->findOrEmpty();
  301. if (!$userAuth->isEmpty() && $userAuth->user_id != $response['user_id']) {
  302. throw new \Exception('该微信已被绑定');
  303. }
  304. }
  305. //如果没有授权,直接生成一条微信授权记录
  306. UserAuth::create([
  307. 'user_id' => $response['user_id'],
  308. 'openid' => $response['openid'],
  309. 'unionid' => $response['unionid'] ?? '',
  310. 'terminal' => $response['terminal'],
  311. ]);
  312. return true;
  313. }
  314. /**
  315. * @notes 获取扫码登录地址
  316. * @return array|false
  317. * @author 段誉
  318. * @date 2022/10/20 18:23
  319. */
  320. public static function getScanCode($redirectUri)
  321. {
  322. try {
  323. $config = WeChatConfigService::getOpConfig();
  324. $appId = $config['app_id'];
  325. $redirectUri = UrlEncode($redirectUri);
  326. // 设置有效时间标记状态, 超时扫码不可登录
  327. $state = MD5(time().rand(10000, 99999));
  328. (new WebScanLoginCache())->setScanLoginState($state);
  329. // 扫码地址
  330. $url = WeChatRequestService::getScanCodeUrl($appId, $redirectUri, $state);
  331. return ['url' => $url];
  332. } catch (\Exception $e) {
  333. self::$error = $e->getMessage();
  334. return false;
  335. }
  336. }
  337. /**
  338. * @notes 网站扫码登录
  339. * @param $params
  340. * @return array|false
  341. * @author 段誉
  342. * @date 2022/10/21 10:28
  343. */
  344. public static function scanLogin($params)
  345. {
  346. Db::startTrans();
  347. try {
  348. // 通过code 获取 access_token,openid,unionid等信息
  349. $userAuth = WeChatRequestService::getUserAuthByCode($params['code']);
  350. if (empty($userAuth['openid']) || empty($userAuth['access_token'])) {
  351. throw new \Exception('获取用户授权信息失败');
  352. }
  353. // 获取微信用户信息
  354. $response = WeChatRequestService::getUserInfoByAuth($userAuth['access_token'], $userAuth['openid']);
  355. // 生成用户或更新用户信息
  356. $userServer = new WechatUserService($response, UserTerminalEnum::PC);
  357. $userInfo = $userServer->getResopnseByUserInfo()->authUserLogin()->getUserInfo();
  358. // 更新登录信息
  359. self::updateLoginInfo($userInfo['id']);
  360. Db::commit();
  361. return $userInfo;
  362. } catch (\Exception $e) {
  363. Db::rollback();
  364. self::$error = $e->getMessage();
  365. return false;
  366. }
  367. }
  368. /**
  369. * @notes 更新用户信息
  370. * @param $params
  371. * @param $userId
  372. * @return User
  373. * @author 段誉
  374. * @date 2023/2/22 11:19
  375. */
  376. public static function updateUser($params, $userId)
  377. {
  378. return User::where(['id' => $userId])->update([
  379. 'nickname' => $params['nickname'],
  380. 'avatar' => FileService::setFileUrl($params['avatar']),
  381. 'is_new_user' => YesNoEnum::NO
  382. ]);
  383. }
  384. public static function firmLogin($params)
  385. {
  386. try {
  387. // 账号/手机号 密码登录
  388. $where = ['account|mobile' => $params['account']];
  389. $user = User::where($where)->findOrEmpty();
  390. if ($user->isEmpty()) {
  391. throw new \Exception('用户不存在');
  392. }
  393. if($user->user_type != YesNoEnum::YES){
  394. throw new \Exception('普通用户不能登录后台');
  395. }
  396. //更新登录信息
  397. $user->login_time = time();
  398. $user->login_ip = request()->ip();
  399. $user->save();
  400. //设置token
  401. $userInfo = UserTokenService::setToken($user->id, $params['terminal']);
  402. //返回登录信息
  403. $avatar = $user->avatar ?: Config::get('project.default_image.user_avatar');
  404. $avatar = FileService::getFileUrl($avatar);
  405. return [
  406. 'nickname' => $userInfo['nickname'],
  407. 'sn' => $userInfo['sn'],
  408. 'mobile' => $userInfo['mobile'],
  409. 'avatar' => $avatar,
  410. 'token' => $userInfo['token'],
  411. ];
  412. } catch (\Exception $e) {
  413. self::setError($e->getMessage());
  414. return false;
  415. }
  416. }
  417. }