LoginLogic.php 14 KB

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