LoginController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. //验证是否传微信小程序的CODE,如果传入进行绑定
  38. if(!empty($params['wx_code']) and $params['channel']==1){
  39. $params['code'] = $params['wx_code'];
  40. $params['user_id'] = $this->userId;
  41. $wx_result = LoginLogic::mnpAuthLogin($params);
  42. if ($wx_result === false) {
  43. return $this->fail(LoginLogic::getError());
  44. }
  45. }
  46. if (true === $result) {
  47. return $this->success('注册成功', [], 1, 1);
  48. }
  49. return $this->fail(LoginLogic::getError());
  50. }
  51. /**
  52. * @notes 账号密码/手机号密码/手机号验证码登录
  53. * @return \think\response\Json
  54. * @author 段誉
  55. * @date 2022/9/16 10:42
  56. */
  57. public function account()
  58. {
  59. $params = (new LoginAccountValidate())->post()->goCheck();
  60. $result = LoginLogic::login($params);
  61. //验证是否传微信小程序的CODE,如果传入进行绑定
  62. if(!empty($params['wx_code']) and $params['terminal']==1){
  63. $params['code'] = $params['wx_code'];
  64. $params['user_id'] = $this->userId;
  65. $wx_result = LoginLogic::mnpAuthLogin($params);
  66. if ($wx_result === false) {
  67. return $this->fail(LoginLogic::getError());
  68. }
  69. }
  70. if (false === $result) {
  71. return $this->fail(LoginLogic::getError());
  72. }
  73. return $this->data($result);
  74. }
  75. /**
  76. * @notes 退出登录
  77. * @return \think\response\Json
  78. * @throws \think\db\exception\DataNotFoundException
  79. * @throws \think\db\exception\DbException
  80. * @throws \think\db\exception\ModelNotFoundException
  81. * @author 段誉
  82. * @date 2022/9/16 10:42
  83. */
  84. public function logout()
  85. {
  86. LoginLogic::logout($this->userInfo);
  87. return $this->success();
  88. }
  89. /**
  90. * @notes 获取微信请求code的链接
  91. * @return \think\response\Json
  92. * @author 段誉
  93. * @date 2022/9/15 18:27
  94. */
  95. public function codeUrl()
  96. {
  97. $url = $this->request->get('url');
  98. $result = ['url' => LoginLogic::codeUrl($url)];
  99. return $this->success('获取成功', $result);
  100. }
  101. /**
  102. * @notes 公众号登录
  103. * @return \think\response\Json
  104. * @throws \GuzzleHttp\Exception\GuzzleException
  105. * @author 段誉
  106. * @date 2022/9/20 19:48
  107. */
  108. public function oaLogin()
  109. {
  110. $params = (new WechatLoginValidate())->post()->goCheck('oa');
  111. $res = LoginLogic::oaLogin($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 mnpLogin()
  124. {
  125. $params = (new WechatLoginValidate())->post()->goCheck('mnpLogin');
  126. $res = LoginLogic::mnpLogin($params);
  127. if (false === $res) {
  128. return $this->fail(LoginLogic::getError());
  129. }
  130. return $this->success('', $res);
  131. }
  132. /**
  133. * @notes 小程序绑定微信
  134. * @return \think\response\Json
  135. * @author 段誉
  136. * @date 2022/9/20 19:48
  137. */
  138. public function mnpAuthBind()
  139. {
  140. $params = (new WechatLoginValidate())->post()->goCheck("wechatAuth");
  141. $params['user_id'] = $this->userId;
  142. $result = LoginLogic::mnpAuthLogin($params);
  143. if ($result === false) {
  144. return $this->fail(LoginLogic::getError());
  145. }
  146. return $this->success('绑定成功', [], 1, 1);
  147. }
  148. /**
  149. * @notes 公众号绑定微信
  150. * @return \think\response\Json
  151. * @throws \GuzzleHttp\Exception\GuzzleException
  152. * @author 段誉
  153. * @date 2022/9/20 19:48
  154. */
  155. public function oaAuthBind()
  156. {
  157. $params = (new WechatLoginValidate())->post()->goCheck("wechatAuth");
  158. $params['user_id'] = $this->userId;
  159. $result = LoginLogic::oaAuthLogin($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. * @author 段誉
  169. * @date 2022/10/20 18:25
  170. */
  171. public function getScanCode()
  172. {
  173. $redirectUri = $this->request->get('url/s');
  174. $result = LoginLogic::getScanCode($redirectUri);
  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 2022/10/21 10:28
  185. */
  186. public function scanLogin()
  187. {
  188. $params = (new WebScanLoginValidate())->post()->goCheck();
  189. $result = LoginLogic::scanLogin($params);
  190. if (false === $result) {
  191. return $this->fail(LoginLogic::getError() ?? '登录失败');
  192. }
  193. return $this->success('', $result);
  194. }
  195. /**
  196. * @notes 更新用户头像昵称
  197. * @return \think\response\Json
  198. * @author 段誉
  199. * @date 2023/2/22 11:15
  200. */
  201. public function updateUser()
  202. {
  203. $params = (new WechatLoginValidate())->post()->goCheck("updateUser");
  204. LoginLogic::updateUser($params, $this->userId);
  205. return $this->success('操作成功', [], 1, 1);
  206. }
  207. public function firmLogin()
  208. {
  209. $params = (new LoginAccountValidate())->post()->goCheck("firmLogin");
  210. $result = LoginLogic::firmLogin($params);
  211. if (false === $result) {
  212. return $this->fail(LoginLogic::getError());
  213. }
  214. return $this->data($result);
  215. }
  216. }