LoginController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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', '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. * @notes 公众号登录
  80. * @return \think\response\Json
  81. * @throws \GuzzleHttp\Exception\GuzzleException
  82. * @author 段誉
  83. * @date 2022/9/20 19:48
  84. */
  85. public function oaLogin()
  86. {
  87. $params = (new WechatLoginValidate())->post()->goCheck('oa');
  88. $res = LoginLogic::oaLogin($params);
  89. if (false === $res) {
  90. return $this->fail(LoginLogic::getError());
  91. }
  92. return $this->success('', $res);
  93. }
  94. /**
  95. * @notes 小程序-登录接口
  96. * @return \think\response\Json
  97. * @author 段誉
  98. * @date 2022/9/20 19:48
  99. */
  100. public function mnpLogin()
  101. {
  102. $params = (new WechatLoginValidate())->post()->goCheck('mnpLogin');
  103. $res = LoginLogic::mnpLogin($params);
  104. if (false === $res) {
  105. return $this->fail(LoginLogic::getError());
  106. }
  107. return $this->success('', $res);
  108. }
  109. /**
  110. *
  111. * 手机号一键登录获取key
  112. * @return \think\response\Json
  113. */
  114. public function getMnpSessionKey()
  115. {
  116. $params = (new WechatLoginValidate())->post()->goCheck('wechatAuth');
  117. $res = LoginLogic::mnpSessionKey($params);
  118. if (false === $res) {
  119. return $this->fail(LoginLogic::getError());
  120. }
  121. return $this->success('', $res);
  122. }
  123. /**
  124. *
  125. * 微信小程序手机号一键登录
  126. * @return \think\response\Json
  127. */
  128. public function mnpAuthPhone()
  129. {
  130. $params = (new WechatLoginValidate())->post()->goCheck('mnpAuthPhone');
  131. $result = LoginLogic::mnpPhoneLogin($params);
  132. if (false === $result) {
  133. return $this->fail(LoginLogic::getError());
  134. }
  135. return $this->data($result);
  136. }
  137. /**
  138. * @notes 小程序绑定微信
  139. * @return \think\response\Json
  140. * @author 段誉
  141. * @date 2022/9/20 19:48
  142. */
  143. public function mnpAuthBind()
  144. {
  145. $params = (new WechatLoginValidate())->post()->goCheck("wechatAuth");
  146. $params['user_id'] = $this->userId;
  147. $result = LoginLogic::mnpAuthLogin($params);
  148. if ($result === false) {
  149. return $this->fail(LoginLogic::getError());
  150. }
  151. return $this->success('绑定成功', [], 1, 1);
  152. }
  153. /**
  154. * @notes 公众号绑定微信
  155. * @return \think\response\Json
  156. * @throws \GuzzleHttp\Exception\GuzzleException
  157. * @author 段誉
  158. * @date 2022/9/20 19:48
  159. */
  160. public function oaAuthBind()
  161. {
  162. $params = (new WechatLoginValidate())->post()->goCheck("wechatAuth");
  163. $params['user_id'] = $this->userId;
  164. $result = LoginLogic::oaAuthLogin($params);
  165. if ($result === false) {
  166. return $this->fail(LoginLogic::getError());
  167. }
  168. return $this->success('绑定成功', [], 1, 1);
  169. }
  170. /**
  171. * @notes 获取扫码地址
  172. * @return \think\response\Json
  173. * @author 段誉
  174. * @date 2022/10/20 18:25
  175. */
  176. public function getScanCode()
  177. {
  178. $redirectUri = $this->request->get('url/s');
  179. $result = LoginLogic::getScanCode($redirectUri);
  180. if (false === $result) {
  181. return $this->fail(LoginLogic::getError() ?? '未知错误');
  182. }
  183. return $this->success('', $result);
  184. }
  185. /**
  186. * @notes 网站扫码登录
  187. * @return \think\response\Json
  188. * @author 段誉
  189. * @date 2022/10/21 10:28
  190. */
  191. public function scanLogin()
  192. {
  193. $params = (new WebScanLoginValidate())->post()->goCheck();
  194. $result = LoginLogic::scanLogin($params);
  195. if (false === $result) {
  196. return $this->fail(LoginLogic::getError() ?? '登录失败');
  197. }
  198. return $this->success('', $result);
  199. }
  200. /**
  201. * @notes 更新用户头像昵称
  202. * @return \think\response\Json
  203. */
  204. public function updateUser()
  205. {
  206. $params = (new WechatLoginValidate())->post()->goCheck("updateUser");
  207. LoginLogic::updateUser($params, $this->userId);
  208. return $this->success('操作成功', [], 1, 1);
  209. }
  210. /**
  211. * @notes 注销用户
  212. * @return \think\response\Json
  213. */
  214. public function deleteAccount()
  215. {
  216. $user = User::find($this->userId);
  217. $user->is_disable = 1;
  218. $user->save();
  219. LoginLogic::logout($this->userInfo);
  220. return $this->success();
  221. }
  222. public function firmLogin()
  223. {
  224. $params = (new LoginAccountValidate())->post()->goCheck("firmLogin");
  225. $result = LoginLogic::firmLogin($params);
  226. if (false === $result) {
  227. return $this->fail(LoginLogic::getError());
  228. }
  229. return $this->data($result);
  230. }
  231. }