LoginLogic.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\shopapi\logic;
  20. use app\shopapi\service\{
  21. UserTokenService,
  22. WechatUserService
  23. };
  24. use app\common\{enum\LoginEnum,
  25. model\User,
  26. logic\BaseLogic,
  27. model\UserAuth,
  28. service\FileService,
  29. enum\UserTerminalEnum,
  30. service\WeChatService,
  31. };
  32. use think\facade\{
  33. Db,
  34. Config,
  35. };
  36. use Requests;
  37. class LoginLogic extends BaseLogic
  38. {
  39. static function check_mobile_user($mobile) : bool
  40. {
  41. return User::where('mobile', $mobile)->value('id') > 0;
  42. }
  43. /**
  44. * @notes 用户账号登录
  45. * @param $params
  46. * @return false|mixed
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\DbException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. * @author 令狐冲
  51. * @date 2021/6/30 17:00
  52. */
  53. public function login($params)
  54. {
  55. try {
  56. if ($params['scene'] == LoginEnum::MOBILE_PASSWORD || $params['scene'] == LoginEnum::MOBILE_CAPTCHA) {
  57. // 手机号密码、手机验证码登录
  58. $where = ['mobile' => $params['account']];
  59. } else {
  60. // 账号密码登录
  61. $where = ['account' => $params['account']];
  62. }
  63. $user = User::where($where)->findOrEmpty();
  64. if ($user->isEmpty()) {
  65. throw new \Exception('用户不存在');
  66. }
  67. //更新登录信息
  68. $user->login_time = time();
  69. $user->login_ip = request()->ip();
  70. $user->save();
  71. //设置token
  72. $userInfo = UserTokenService::setToken($user->id, $params['terminal']);
  73. //返回登录信息
  74. $avatar = $user->avatar ? $user->avatar : Config::get('project.default_image.user_avatar');
  75. $avatar = FileService::getFileUrl($avatar);
  76. return [
  77. 'nickname' => $userInfo['nickname'],
  78. 'sn' => $userInfo['sn'],
  79. 'mobile' => $userInfo['mobile'],
  80. 'avatar' => $avatar,
  81. 'token' => $userInfo['token'],
  82. ];
  83. } catch (\Exception $e) {
  84. self::setError($e->getMessage());
  85. return false;
  86. }
  87. }
  88. /**
  89. * @notes 退出登录
  90. * @param $userInfo
  91. * @return bool
  92. * @throws \think\db\exception\DataNotFoundException
  93. * @throws \think\db\exception\DbException
  94. * @throws \think\db\exception\ModelNotFoundException
  95. * @author 令狐冲
  96. * @date 2021/7/5 14:34
  97. */
  98. public function logout($userInfo)
  99. {
  100. //token不存在,不注销
  101. if (!isset($userInfo['token'])) {
  102. return false;
  103. }
  104. //设置token过期
  105. return UserTokenService::expireToken($userInfo['token']);
  106. }
  107. /**
  108. * @notes 获取微信请求code的链接
  109. * @param $url
  110. * @return string
  111. * @author cjhao
  112. * @date 2021/7/31 14:28
  113. */
  114. public function codeUrl(string $url)
  115. {
  116. return WeChatService::getCodeUrl($url);
  117. }
  118. /**
  119. * @notes 公众号登录
  120. * @param $params
  121. * @return array|bool
  122. * @throws \GuzzleHttp\Exception\GuzzleException
  123. * @author cjhao
  124. * @date 2021/8/2 17:54
  125. */
  126. public function oaLogin(array $params)
  127. {
  128. //微信调用
  129. try {
  130. Db::startTrans();
  131. //通过code获取微信 openid
  132. $response = WeChatService::getOaResByCode($params);
  133. $userServer = new WechatUserService($response, UserTerminalEnum::WECHAT_OA);
  134. $userInfo = $userServer->getResopnseByUserInfo()->authUserLogin()->getUserInfo();
  135. // 更新登录信息
  136. $this->updateLoginInfo($userInfo['id']);
  137. Db::commit();
  138. return $userInfo;
  139. } catch (\Exception $e) {
  140. Db::rollback();
  141. self::$error = $e->getMessage();
  142. return false;
  143. }
  144. }
  145. /**
  146. * @notes 小程序-静默登录
  147. * @param $post
  148. * @return array|bool
  149. * @author cjhao
  150. * @date 2021/8/2 17:00
  151. */
  152. public function silentLogin(array $params)
  153. {
  154. try {
  155. //通过code获取微信 openid
  156. $response = WeChatService::getMnpResByCode($params);
  157. $userServer = new WechatUserService($response, UserTerminalEnum::WECHAT_MMP);
  158. $userInfo = $userServer->getResopnseByUserInfo('silent')->getUserInfo();
  159. if (!empty($userInfo)) {
  160. // 更新登录信息
  161. $this->updateLoginInfo($userInfo['id']);
  162. }
  163. return $userInfo;
  164. } catch (\Exception $e) {
  165. self::$error = $e->getMessage();
  166. return false;
  167. }
  168. }
  169. /**
  170. * @notes 小程序-授权登录
  171. * @param $params
  172. * @return array|bool
  173. * @author cjhao
  174. * @date 2021/7/30 19:00
  175. */
  176. public function authLogin(array $params)
  177. {
  178. try {
  179. Db::startTrans();
  180. //通过code获取微信 openid
  181. $response = WeChatService::getMnpResByCode($params);
  182. $response['headimgurl'] = $params['headimgurl'];
  183. $response['nickname'] = $params['nickname'];
  184. $userServer = new WechatUserService($response, UserTerminalEnum::WECHAT_MMP);
  185. $userInfo = $userServer->getResopnseByUserInfo()->authUserLogin()->getUserInfo();
  186. // 更新登录信息
  187. $this->updateLoginInfo($userInfo['id']);
  188. Db::commit();
  189. return $userInfo;
  190. } catch (\Exception $e) {
  191. Db::rollback();
  192. self::$error = $e->getMessage();
  193. return false;
  194. }
  195. }
  196. /**
  197. * @notes uniApp微信登录
  198. * @param array $params
  199. * @return array|bool
  200. * @author cjhao
  201. * @date 2021/9/2 14:17
  202. */
  203. public function uninAppLogin(array $params)
  204. {
  205. try {
  206. Db::startTrans();
  207. //sdk不支持app登录,直接调用微信接口
  208. $requests = Requests::get('https://api.weixin.qq.com/sns/userinfo?openid=' . 'openid=' . $params['openid'] . '&access_token=' . $params['access_token']);
  209. $response = json_decode($requests->body, true);
  210. $userServer = new WechatUserService($response, $params['terminal']);
  211. $userInfo = $userServer->getResopnseByUserInfo()->authUserLogin()->getUserInfo();
  212. // 更新登录信息
  213. $this->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. * @author Tab
  225. * @date 2021/11/10 16:58
  226. */
  227. public function toutiaoSilentLogin($params)
  228. {
  229. Db::startTrans();
  230. try {
  231. $userInfo = (new TouTiaoLogic())->silentLogin($params);
  232. if (!empty($userInfo)) {
  233. // 更新登录信息
  234. $this->updateLoginInfo($userInfo['id']);
  235. }
  236. Db::commit();
  237. return $userInfo;
  238. } catch (\Exception $e) {
  239. Db::rollback();
  240. self::$error = $e->getMessage();
  241. return false;
  242. }
  243. }
  244. /**
  245. * @notes 字节小程序-授权登录
  246. * @author Tab
  247. * @date 2021/11/10 16:58
  248. */
  249. public function toutiaoAuthLogin($params)
  250. {
  251. Db::startTrans();
  252. try {
  253. $userInfo = (new TouTiaoLogic())->authLogin($params);
  254. // 更新登录信息
  255. $this->updateLoginInfo($userInfo['id']);
  256. Db::commit();
  257. return $userInfo;
  258. } catch (\Exception $e) {
  259. Db::rollback();
  260. self::$error = $e->getMessage();
  261. return false;
  262. }
  263. }
  264. /**
  265. * @notes 更新登录信息
  266. * @param $userId
  267. * @throws \Exception
  268. * @author Tab
  269. * @date 2021/12/7 14:12
  270. */
  271. public function updateLoginInfo($userId)
  272. {
  273. $user = User::findOrEmpty($userId);
  274. if ($user->isEmpty()) {
  275. throw new \Exception('用户不存在');
  276. }
  277. $time = time();
  278. $user->login_time = $time;
  279. $user->login_ip = request()->ip();
  280. $user->update_time = $time;
  281. $user->save();
  282. }
  283. /**
  284. * @notes 小程序端绑定微信
  285. * @param array $params
  286. * @return bool
  287. * @author cjhao
  288. * @date 2022/5/17 15:47
  289. */
  290. public function mnpAuthLogin(array $params)
  291. {
  292. try {
  293. //通过code获取微信openid
  294. $response = WeChatService::getMnpResByCode($params);
  295. $response['user_id'] = $params['user_id'];
  296. $response['terminal'] = UserTerminalEnum::WECHAT_MMP;
  297. return $this->createAuth($response);
  298. } catch (\Exception $e) {
  299. self::$error = $e->getMessage();
  300. return false;
  301. }
  302. }
  303. /**
  304. * @notes 公众号端绑定微信
  305. * @param array $params
  306. * @return bool
  307. * @throws AuthorizeFailedException
  308. * @throws Exception
  309. * @throws \GuzzleHttp\Exception\GuzzleException
  310. * @throws \think\db\exception\DataNotFoundException
  311. * @throws \think\db\exception\DbException
  312. * @throws \think\db\exception\ModelNotFoundException
  313. * @author cjhao
  314. * @date 2022/5/17 16:20
  315. */
  316. public function oaAuthLogin(array $params)
  317. {
  318. try {
  319. //通过code获取微信openid
  320. $response = WeChatService::getOaResByCode($params);
  321. $response['user_id'] = $params['user_id'];
  322. $response['terminal'] = UserTerminalEnum::WECHAT_OA;
  323. return $this->createAuth($response);
  324. } catch (\Exception $e) {
  325. self::$error = $e->getMessage();
  326. return false;
  327. }
  328. }
  329. /**
  330. * @notes 生成一条授权记录
  331. * @param $response
  332. * @return bool
  333. * @throws Exception
  334. * @throws \think\db\exception\DataNotFoundException
  335. * @throws \think\db\exception\DbException
  336. * @throws \think\db\exception\ModelNotFoundException
  337. * @author cjhao
  338. * @date 2022/5/17 16:18
  339. */
  340. public function createAuth($response)
  341. {
  342. //验证该微信是否授权过,用户open_id或者ounion_id,防止生成两个账号
  343. // $isAuth = UserAuth::where('openid','=',$response['openid'])
  344. // ->whereOr(function ($query) use($response) {
  345. // if(isset($response['unionid']) && !empty($response['unionid'])){
  346. // $query->where('unionid', $response['unionid']);
  347. // }
  348. // })->find();
  349. //先检查openid是否有记录
  350. $isAuth = UserAuth::where('openid', '=', $response['openid'])->findOrEmpty();
  351. if(!$isAuth->isEmpty()){
  352. throw new \Exception('该微信已经绑定,请切换微信授权登录');
  353. }
  354. if(isset($response['unionid']) && !empty($response['unionid'])) {
  355. //在用unionid找记录,防止生成两个账号,同个unionid的问题
  356. $userAuth = UserAuth::where(['unionid'=>$response['unionid']])
  357. ->findOrEmpty();
  358. if (!$userAuth->isEmpty() && $userAuth->user_id != $response['user_id']) {
  359. throw new \Exception('该微信已经绑定,请切换微信授权登录');
  360. }
  361. }
  362. //如果没有授权,直接生成一条微信授权记录
  363. UserAuth::create([
  364. 'user_id' => $response['user_id'],
  365. 'openid' => $response['openid'],
  366. 'unionid' => $response['unionid'] ?? '',
  367. 'terminal' => $response['terminal'],
  368. ]);
  369. return true;
  370. }
  371. /**
  372. * @notes 更新用户头像昵称
  373. * @param $post
  374. * @param $user_id
  375. * @return bool
  376. * @throws \think\db\exception\DbException
  377. * @author ljj
  378. * @date 2023/2/2 6:36 下午
  379. */
  380. public static function updateUser($post,$user_id)
  381. {
  382. Db::name('user')->where(['id'=>$user_id])->update(['nickname'=>$post['nickname'],'avatar'=>FileService::setFileUrl($post['avatar']),'is_new_user'=>0]);
  383. return true;
  384. }
  385. }