LoginLogic.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. 'inviter_id' => $user['inviter_id'],
  83. ];
  84. } catch (\Exception $e) {
  85. self::setError($e->getMessage());
  86. return false;
  87. }
  88. }
  89. /**
  90. * @notes 退出登录
  91. * @param $userInfo
  92. * @return bool
  93. * @throws \think\db\exception\DataNotFoundException
  94. * @throws \think\db\exception\DbException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. * @author 令狐冲
  97. * @date 2021/7/5 14:34
  98. */
  99. public function logout($userInfo)
  100. {
  101. //token不存在,不注销
  102. if (!isset($userInfo['token'])) {
  103. return false;
  104. }
  105. //设置token过期
  106. return UserTokenService::expireToken($userInfo['token']);
  107. }
  108. /**
  109. * @notes 获取微信请求code的链接
  110. * @param $url
  111. * @return string
  112. * @author cjhao
  113. * @date 2021/7/31 14:28
  114. */
  115. public function codeUrl(string $url)
  116. {
  117. return WeChatService::getCodeUrl($url);
  118. }
  119. /**
  120. * @notes 公众号登录
  121. * @param $params
  122. * @return array|bool
  123. * @throws \GuzzleHttp\Exception\GuzzleException
  124. * @author cjhao
  125. * @date 2021/8/2 17:54
  126. */
  127. public function oaLogin(array $params)
  128. {
  129. //微信调用
  130. try {
  131. Db::startTrans();
  132. //通过code获取微信 openid
  133. $response = WeChatService::getOaResByCode($params);
  134. $userServer = new WechatUserService($response, UserTerminalEnum::WECHAT_OA);
  135. $userInfo = $userServer->getResopnseByUserInfo()->authUserLogin()->getUserInfo();
  136. // 更新登录信息
  137. $this->updateLoginInfo($userInfo['id']);
  138. Db::commit();
  139. return $userInfo;
  140. } catch (\Exception $e) {
  141. Db::rollback();
  142. self::$error = $e->getMessage();
  143. return false;
  144. }
  145. }
  146. /**
  147. * @notes 小程序-静默登录
  148. * @param $post
  149. * @return array|bool
  150. * @author cjhao
  151. * @date 2021/8/2 17:00
  152. */
  153. public function silentLogin(array $params)
  154. {
  155. try {
  156. //通过code获取微信 openid
  157. $response = WeChatService::getMnpResByCode($params);
  158. $userServer = new WechatUserService($response, UserTerminalEnum::WECHAT_MMP);
  159. $userInfo = $userServer->getResopnseByUserInfo('silent')->getUserInfo();
  160. if (!empty($userInfo)) {
  161. // 更新登录信息
  162. $this->updateLoginInfo($userInfo['id']);
  163. }
  164. return $userInfo;
  165. } catch (\Exception $e) {
  166. self::$error = $e->getMessage();
  167. return false;
  168. }
  169. }
  170. /**
  171. * @notes 小程序-授权登录
  172. * @param $params
  173. * @return array|bool
  174. * @author cjhao
  175. * @date 2021/7/30 19:00
  176. */
  177. public function authLogin(array $params)
  178. {
  179. try {
  180. Db::startTrans();
  181. //通过code获取微信 openid
  182. $response = WeChatService::getMnpResByCode($params);
  183. $response['headimgurl'] = $params['headimgurl'];
  184. $response['nickname'] = $params['nickname'];
  185. $userServer = new WechatUserService($response, UserTerminalEnum::WECHAT_MMP);
  186. $userInfo = $userServer->getResopnseByUserInfo()->authUserLogin()->getUserInfo();
  187. // 更新登录信息
  188. $this->updateLoginInfo($userInfo['id']);
  189. Db::commit();
  190. return $userInfo;
  191. } catch (\Exception $e) {
  192. Db::rollback();
  193. self::$error = $e->getMessage();
  194. return false;
  195. }
  196. }
  197. /**
  198. * @notes uniApp微信登录
  199. * @param array $params
  200. * @return array|bool
  201. * @author cjhao
  202. * @date 2021/9/2 14:17
  203. */
  204. public function uninAppLogin(array $params)
  205. {
  206. try {
  207. Db::startTrans();
  208. //sdk不支持app登录,直接调用微信接口
  209. $requests = Requests::get('https://api.weixin.qq.com/sns/userinfo?openid=' . 'openid=' . $params['openid'] . '&access_token=' . $params['access_token']);
  210. $response = json_decode($requests->body, true);
  211. $userServer = new WechatUserService($response, $params['terminal']);
  212. $userInfo = $userServer->getResopnseByUserInfo()->authUserLogin()->getUserInfo();
  213. // 更新登录信息
  214. $this->updateLoginInfo($userInfo['id']);
  215. Db::commit();
  216. return $userInfo;
  217. } catch (\Exception $e) {
  218. Db::rollback();
  219. self::$error = $e->getMessage();
  220. return false;
  221. }
  222. }
  223. /**
  224. * @notes 字节小程序-静默登录
  225. * @author Tab
  226. * @date 2021/11/10 16:58
  227. */
  228. public function toutiaoSilentLogin($params)
  229. {
  230. Db::startTrans();
  231. try {
  232. $userInfo = (new TouTiaoLogic())->silentLogin($params);
  233. if (!empty($userInfo)) {
  234. // 更新登录信息
  235. $this->updateLoginInfo($userInfo['id']);
  236. }
  237. Db::commit();
  238. return $userInfo;
  239. } catch (\Exception $e) {
  240. Db::rollback();
  241. self::$error = $e->getMessage();
  242. return false;
  243. }
  244. }
  245. /**
  246. * @notes 字节小程序-授权登录
  247. * @author Tab
  248. * @date 2021/11/10 16:58
  249. */
  250. public function toutiaoAuthLogin($params)
  251. {
  252. Db::startTrans();
  253. try {
  254. $userInfo = (new TouTiaoLogic())->authLogin($params);
  255. // 更新登录信息
  256. $this->updateLoginInfo($userInfo['id']);
  257. Db::commit();
  258. return $userInfo;
  259. } catch (\Exception $e) {
  260. Db::rollback();
  261. self::$error = $e->getMessage();
  262. return false;
  263. }
  264. }
  265. /**
  266. * @notes 更新登录信息
  267. * @param $userId
  268. * @throws \Exception
  269. * @author Tab
  270. * @date 2021/12/7 14:12
  271. */
  272. public function updateLoginInfo($userId)
  273. {
  274. $user = User::findOrEmpty($userId);
  275. if ($user->isEmpty()) {
  276. throw new \Exception('用户不存在');
  277. }
  278. $time = time();
  279. $user->login_time = $time;
  280. $user->login_ip = request()->ip();
  281. $user->update_time = $time;
  282. $user->save();
  283. }
  284. /**
  285. * @notes 小程序端绑定微信
  286. * @param array $params
  287. * @return bool
  288. * @author cjhao
  289. * @date 2022/5/17 15:47
  290. */
  291. public function mnpAuthLogin(array $params)
  292. {
  293. try {
  294. //通过code获取微信openid
  295. $response = WeChatService::getMnpResByCode($params);
  296. $response['user_id'] = $params['user_id'];
  297. $response['terminal'] = UserTerminalEnum::WECHAT_MMP;
  298. return $this->createAuth($response);
  299. } catch (\Exception $e) {
  300. self::$error = $e->getMessage();
  301. return false;
  302. }
  303. }
  304. /**
  305. * @notes 公众号端绑定微信
  306. * @param array $params
  307. * @return bool
  308. * @throws AuthorizeFailedException
  309. * @throws Exception
  310. * @throws \GuzzleHttp\Exception\GuzzleException
  311. * @throws \think\db\exception\DataNotFoundException
  312. * @throws \think\db\exception\DbException
  313. * @throws \think\db\exception\ModelNotFoundException
  314. * @author cjhao
  315. * @date 2022/5/17 16:20
  316. */
  317. public function oaAuthLogin(array $params)
  318. {
  319. try {
  320. //通过code获取微信openid
  321. $response = WeChatService::getOaResByCode($params);
  322. $response['user_id'] = $params['user_id'];
  323. $response['terminal'] = UserTerminalEnum::WECHAT_OA;
  324. return $this->createAuth($response);
  325. } catch (\Exception $e) {
  326. self::$error = $e->getMessage();
  327. return false;
  328. }
  329. }
  330. /**
  331. * @notes 生成一条授权记录
  332. * @param $response
  333. * @return bool
  334. * @throws Exception
  335. * @throws \think\db\exception\DataNotFoundException
  336. * @throws \think\db\exception\DbException
  337. * @throws \think\db\exception\ModelNotFoundException
  338. * @author cjhao
  339. * @date 2022/5/17 16:18
  340. */
  341. public function createAuth($response)
  342. {
  343. //验证该微信是否授权过,用户open_id或者ounion_id,防止生成两个账号
  344. // $isAuth = UserAuth::where('openid','=',$response['openid'])
  345. // ->whereOr(function ($query) use($response) {
  346. // if(isset($response['unionid']) && !empty($response['unionid'])){
  347. // $query->where('unionid', $response['unionid']);
  348. // }
  349. // })->find();
  350. //先检查openid是否有记录
  351. $isAuth = UserAuth::where('openid', '=', $response['openid'])->findOrEmpty();
  352. if(!$isAuth->isEmpty()){
  353. throw new \Exception('该微信已经绑定,请切换微信授权登录');
  354. }
  355. if(isset($response['unionid']) && !empty($response['unionid'])) {
  356. //在用unionid找记录,防止生成两个账号,同个unionid的问题
  357. $userAuth = UserAuth::where(['unionid'=>$response['unionid']])
  358. ->findOrEmpty();
  359. if (!$userAuth->isEmpty() && $userAuth->user_id != $response['user_id']) {
  360. throw new \Exception('该微信已经绑定,请切换微信授权登录');
  361. }
  362. }
  363. //如果没有授权,直接生成一条微信授权记录
  364. UserAuth::create([
  365. 'user_id' => $response['user_id'],
  366. 'openid' => $response['openid'],
  367. 'unionid' => $response['unionid'] ?? '',
  368. 'terminal' => $response['terminal'],
  369. ]);
  370. return true;
  371. }
  372. /**
  373. * @notes 更新用户头像昵称
  374. * @param $post
  375. * @param $user_id
  376. * @return bool
  377. * @throws \think\db\exception\DbException
  378. * @author ljj
  379. * @date 2023/2/2 6:36 下午
  380. */
  381. public static function updateUser($post,$user_id)
  382. {
  383. Db::name('user')->where(['id'=>$user_id])->update(['nickname'=>$post['nickname'],'avatar'=>FileService::setFileUrl($post['avatar']),'is_new_user'=>0]);
  384. return true;
  385. }
  386. }