LoginController.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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\controller;
  15. use app\common\enum\LoginEnum;
  16. use app\common\enum\user\UserTerminalEnum;
  17. use app\common\model\user\User;
  18. use app\api\validate\{LoginAccountValidate, RegisterValidate, WebScanLoginValidate, WechatLoginValidate};
  19. use app\api\logic\LoginLogic;
  20. /**
  21. * 登录注册
  22. * Class LoginController
  23. * @package app\api\controller
  24. */
  25. class LoginController extends BaseApiController
  26. {
  27. public array $notNeedLogin = ['account', 'logout', 'codeUrl','getOpenIdByCode', 'oaLogin', 'mnpLogin', 'getScanCode', 'scanLogin', 'firmLogin','getMnpSessionKey','mnpAuthPhone'];
  28. /**
  29. * @notes 账号密码/手机号密码/手机号验证码登录
  30. * @return \think\response\Json
  31. * @author 段誉
  32. * @date 2022/9/16 10:42
  33. */
  34. public function account()
  35. {
  36. $params = (new LoginAccountValidate())->post()->goCheck();
  37. $result = LoginLogic::login($params);
  38. //验证是否传微信小程序的CODE,如果传入进行绑定
  39. if(!empty($params['wx_code']) and $params['terminal']==1){
  40. $params['code'] = $params['wx_code'];
  41. $params['user_id'] = User::where('sn',$result['sn'])->value('id');
  42. $wx_result = LoginLogic::mnpAuthLogin($params);
  43. if ($wx_result === false) {
  44. return $this->fail(LoginLogic::getError());
  45. }
  46. }
  47. if (false === $result) {
  48. return $this->fail(LoginLogic::getError());
  49. }
  50. return $this->data($result);
  51. }
  52. /**
  53. * @notes 退出登录
  54. * @return \think\response\Json
  55. * @throws \think\db\exception\DataNotFoundException
  56. * @throws \think\db\exception\DbException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. * @author 段誉
  59. * @date 2022/9/16 10:42
  60. */
  61. public function logout()
  62. {
  63. LoginLogic::logout($this->userInfo);
  64. return $this->success();
  65. }
  66. /**
  67. * @notes 获取微信请求code的链接
  68. * @return \think\response\Json
  69. * @author 段誉
  70. * @date 2022/9/15 18:27
  71. */
  72. public function codeUrl()
  73. {
  74. $url = $this->request->get('url');
  75. $result = ['url' => LoginLogic::codeUrl($url)];
  76. return $this->success('获取成功', $result);
  77. }
  78. /**
  79. * 获取微信用户openid信息
  80. */
  81. public function getOpenIdByCode()
  82. {
  83. $code = $this->request->get('code');
  84. $res = LoginLogic::getOpenIdByCode(['code'=>$code]);
  85. if (false === $res) {
  86. return $this->fail(LoginLogic::getError());
  87. }
  88. return $this->success('', $res);
  89. }
  90. /**
  91. * @notes 公众号登录
  92. * @return \think\response\Json
  93. * @throws \GuzzleHttp\Exception\GuzzleException
  94. * @author 段誉
  95. * @date 2022/9/20 19:48
  96. */
  97. public function oaLogin()
  98. {
  99. $params = (new WechatLoginValidate())->post()->goCheck('oa');
  100. $res = LoginLogic::oaLogin($params);
  101. if (false === $res) {
  102. return $this->fail(LoginLogic::getError());
  103. }
  104. return $this->success('', $res);
  105. }
  106. /**
  107. * @notes 小程序-登录接口
  108. * @return \think\response\Json
  109. * @author 段誉
  110. * @date 2022/9/20 19:48
  111. */
  112. public function mnpLogin()
  113. {
  114. $params = (new WechatLoginValidate())->post()->goCheck('mnpLogin');
  115. $res = LoginLogic::mnpLogin($params);
  116. if (false === $res) {
  117. return $this->fail(LoginLogic::getError());
  118. }
  119. return $this->success('', $res);
  120. }
  121. /**
  122. *
  123. * 手机号一键登录获取key
  124. * @return \think\response\Json
  125. */
  126. public function getMnpSessionKey()
  127. {
  128. $params = (new WechatLoginValidate())->post()->goCheck('wechatAuth');
  129. $res = LoginLogic::mnpSessionKey($params);
  130. if (false === $res) {
  131. return $this->fail(LoginLogic::getError());
  132. }
  133. return $this->success('', $res);
  134. }
  135. /**
  136. *
  137. * 微信小程序手机号一键登录
  138. * @return \think\response\Json
  139. */
  140. public function mnpAuthPhone()
  141. {
  142. $params = (new WechatLoginValidate())->post()->goCheck('mnpAuthPhone');
  143. $result = LoginLogic::mnpPhoneLogin($params);
  144. if (false === $result) {
  145. return $this->fail(LoginLogic::getError());
  146. }
  147. return $this->data($result);
  148. }
  149. /**
  150. * @notes 小程序绑定微信
  151. * @return \think\response\Json
  152. * @author 段誉
  153. * @date 2022/9/20 19:48
  154. */
  155. public function mnpAuthBind()
  156. {
  157. $params = (new WechatLoginValidate())->post()->goCheck("wechatAuth");
  158. $params['user_id'] = $this->userId;
  159. $result = LoginLogic::mnpAuthLogin($params);
  160. if ($result === false) {
  161. return $this->fail(LoginLogic::getError());
  162. }
  163. return $this->success('绑定成功', [], 1, 1);
  164. }
  165. /**
  166. * @notes 公众号绑定微信
  167. * @return \think\response\Json
  168. * @throws \GuzzleHttp\Exception\GuzzleException
  169. * @author 段誉
  170. * @date 2022/9/20 19:48
  171. */
  172. public function oaAuthBind()
  173. {
  174. $params = (new WechatLoginValidate())->post()->goCheck("wechatAuth");
  175. $params['user_id'] = $this->userId;
  176. $result = LoginLogic::oaAuthLogin($params);
  177. if ($result === false) {
  178. return $this->fail(LoginLogic::getError());
  179. }
  180. return $this->success('绑定成功', [], 1, 1);
  181. }
  182. /**
  183. * @notes 获取扫码地址
  184. * @return \think\response\Json
  185. * @author 段誉
  186. * @date 2022/10/20 18:25
  187. */
  188. public function getScanCode()
  189. {
  190. $redirectUri = $this->request->get('url/s');
  191. $result = LoginLogic::getScanCode($redirectUri);
  192. if (false === $result) {
  193. return $this->fail(LoginLogic::getError() ?? '未知错误');
  194. }
  195. return $this->success('', $result);
  196. }
  197. /**
  198. * @notes 网站扫码登录
  199. * @return \think\response\Json
  200. * @author 段誉
  201. * @date 2022/10/21 10:28
  202. */
  203. public function scanLogin()
  204. {
  205. $params = (new WebScanLoginValidate())->post()->goCheck();
  206. $result = LoginLogic::scanLogin($params);
  207. if (false === $result) {
  208. return $this->fail(LoginLogic::getError() ?? '登录失败');
  209. }
  210. return $this->success('', $result);
  211. }
  212. /**
  213. * @notes 更新用户头像昵称
  214. * @return \think\response\Json
  215. */
  216. public function updateUser()
  217. {
  218. $params = (new WechatLoginValidate())->post()->goCheck("updateUser");
  219. LoginLogic::updateUser($params, $this->userId);
  220. return $this->success('操作成功', [], 1, 1);
  221. }
  222. /**
  223. * @notes 注销用户
  224. * @return \think\response\Json
  225. */
  226. public function deleteAccount()
  227. {
  228. $user = User::find($this->userId);
  229. $user->is_disable = 1;
  230. $user->save();
  231. LoginLogic::logout($this->userInfo);
  232. return $this->success();
  233. }
  234. public function firmLogin()
  235. {
  236. $params = (new LoginAccountValidate())->post()->goCheck("firmLogin");
  237. $result = LoginLogic::firmLogin($params);
  238. if (false === $result) {
  239. return $this->fail(LoginLogic::getError());
  240. }
  241. return $this->data($result);
  242. }
  243. }