LoginController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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\api\validate\{LoginAccountValidate, RegisterValidate, WebScanLoginValidate, WechatLoginValidate};
  18. use app\api\logic\LoginLogic;
  19. /**
  20. * 登录注册
  21. * Class LoginController
  22. * @package app\api\controller
  23. */
  24. class LoginController extends BaseApiController
  25. {
  26. public array $notNeedLogin = ['register', 'account', 'logout', 'codeUrl', 'oaLogin', 'mnpLogin', 'getScanCode', 'scanLogin', 'firmLogin'];
  27. /**
  28. * @notes 注册账号
  29. * @return \think\response\Json
  30. * @author 段誉
  31. * @date 2022/9/7 15:38
  32. */
  33. public function register()
  34. {
  35. $params = (new RegisterValidate())->post()->goCheck('register');
  36. $result = LoginLogic::register($params);
  37. if (true === $result) {
  38. return $this->success('注册成功', [], 1, 1);
  39. }
  40. return $this->fail(LoginLogic::getError());
  41. }
  42. /**
  43. * @notes 账号密码/手机号密码/手机号验证码登录
  44. * @return \think\response\Json
  45. * @author 段誉
  46. * @date 2022/9/16 10:42
  47. */
  48. public function account()
  49. {
  50. $params = (new LoginAccountValidate())->post()->goCheck();
  51. $result = LoginLogic::login($params);
  52. if (false === $result) {
  53. return $this->fail(LoginLogic::getError());
  54. }
  55. return $this->data($result);
  56. }
  57. /**
  58. * @notes 退出登录
  59. * @return \think\response\Json
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\DbException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. * @author 段誉
  64. * @date 2022/9/16 10:42
  65. */
  66. public function logout()
  67. {
  68. LoginLogic::logout($this->userInfo);
  69. return $this->success();
  70. }
  71. /**
  72. * @notes 获取微信请求code的链接
  73. * @return \think\response\Json
  74. * @author 段誉
  75. * @date 2022/9/15 18:27
  76. */
  77. public function codeUrl()
  78. {
  79. $url = $this->request->get('url');
  80. $result = ['url' => LoginLogic::codeUrl($url)];
  81. return $this->success('获取成功', $result);
  82. }
  83. /**
  84. * @notes 公众号登录
  85. * @return \think\response\Json
  86. * @throws \GuzzleHttp\Exception\GuzzleException
  87. * @author 段誉
  88. * @date 2022/9/20 19:48
  89. */
  90. public function oaLogin()
  91. {
  92. $params = (new WechatLoginValidate())->post()->goCheck('oa');
  93. $res = LoginLogic::oaLogin($params);
  94. if (false === $res) {
  95. return $this->fail(LoginLogic::getError());
  96. }
  97. return $this->success('', $res);
  98. }
  99. /**
  100. * @notes 小程序-登录接口
  101. * @return \think\response\Json
  102. * @author 段誉
  103. * @date 2022/9/20 19:48
  104. */
  105. public function mnpLogin()
  106. {
  107. $params = (new WechatLoginValidate())->post()->goCheck('mnpLogin');
  108. $res = LoginLogic::mnpLogin($params);
  109. if (false === $res) {
  110. return $this->fail(LoginLogic::getError());
  111. }
  112. return $this->success('', $res);
  113. }
  114. /**
  115. * @notes 小程序绑定微信
  116. * @return \think\response\Json
  117. * @author 段誉
  118. * @date 2022/9/20 19:48
  119. */
  120. public function mnpAuthBind()
  121. {
  122. $params = (new WechatLoginValidate())->post()->goCheck("wechatAuth");
  123. $params['user_id'] = $this->userId;
  124. $result = LoginLogic::mnpAuthLogin($params);
  125. if ($result === false) {
  126. return $this->fail(LoginLogic::getError());
  127. }
  128. return $this->success('绑定成功', [], 1, 1);
  129. }
  130. /**
  131. * @notes 公众号绑定微信
  132. * @return \think\response\Json
  133. * @throws \GuzzleHttp\Exception\GuzzleException
  134. * @author 段誉
  135. * @date 2022/9/20 19:48
  136. */
  137. public function oaAuthBind()
  138. {
  139. $params = (new WechatLoginValidate())->post()->goCheck("wechatAuth");
  140. $params['user_id'] = $this->userId;
  141. $result = LoginLogic::oaAuthLogin($params);
  142. if ($result === false) {
  143. return $this->fail(LoginLogic::getError());
  144. }
  145. return $this->success('绑定成功', [], 1, 1);
  146. }
  147. /**
  148. * @notes 获取扫码地址
  149. * @return \think\response\Json
  150. * @author 段誉
  151. * @date 2022/10/20 18:25
  152. */
  153. public function getScanCode()
  154. {
  155. $redirectUri = $this->request->get('url/s');
  156. $result = LoginLogic::getScanCode($redirectUri);
  157. if (false === $result) {
  158. return $this->fail(LoginLogic::getError() ?? '未知错误');
  159. }
  160. return $this->success('', $result);
  161. }
  162. /**
  163. * @notes 网站扫码登录
  164. * @return \think\response\Json
  165. * @author 段誉
  166. * @date 2022/10/21 10:28
  167. */
  168. public function scanLogin()
  169. {
  170. $params = (new WebScanLoginValidate())->post()->goCheck();
  171. $result = LoginLogic::scanLogin($params);
  172. if (false === $result) {
  173. return $this->fail(LoginLogic::getError() ?? '登录失败');
  174. }
  175. return $this->success('', $result);
  176. }
  177. /**
  178. * @notes 更新用户头像昵称
  179. * @return \think\response\Json
  180. * @author 段誉
  181. * @date 2023/2/22 11:15
  182. */
  183. public function updateUser()
  184. {
  185. $params = (new WechatLoginValidate())->post()->goCheck("updateUser");
  186. LoginLogic::updateUser($params, $this->userId);
  187. return $this->success('操作成功', [], 1, 1);
  188. }
  189. public function firmLogin()
  190. {
  191. $params = (new LoginAccountValidate())->post()->goCheck("firmLogin");
  192. $result = LoginLogic::firmLogin($params);
  193. if (false === $result) {
  194. return $this->fail(LoginLogic::getError());
  195. }
  196. return $this->data($result);
  197. }
  198. }