LoginController.php 6.1 KB

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