LoginController.php 6.0 KB

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