LoginController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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'];
  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. * @notes 小程序绑定微信
  111. * @return \think\response\Json
  112. * @author 段誉
  113. * @date 2022/9/20 19:48
  114. */
  115. public function mnpAuthBind()
  116. {
  117. $params = (new WechatLoginValidate())->post()->goCheck("wechatAuth");
  118. $params['user_id'] = $this->userId;
  119. $result = LoginLogic::mnpAuthLogin($params);
  120. if ($result === false) {
  121. return $this->fail(LoginLogic::getError());
  122. }
  123. return $this->success('绑定成功', [], 1, 1);
  124. }
  125. /**
  126. * @notes 公众号绑定微信
  127. * @return \think\response\Json
  128. * @throws \GuzzleHttp\Exception\GuzzleException
  129. * @author 段誉
  130. * @date 2022/9/20 19:48
  131. */
  132. public function oaAuthBind()
  133. {
  134. $params = (new WechatLoginValidate())->post()->goCheck("wechatAuth");
  135. $params['user_id'] = $this->userId;
  136. $result = LoginLogic::oaAuthLogin($params);
  137. if ($result === false) {
  138. return $this->fail(LoginLogic::getError());
  139. }
  140. return $this->success('绑定成功', [], 1, 1);
  141. }
  142. /**
  143. * @notes 获取扫码地址
  144. * @return \think\response\Json
  145. * @author 段誉
  146. * @date 2022/10/20 18:25
  147. */
  148. public function getScanCode()
  149. {
  150. $redirectUri = $this->request->get('url/s');
  151. $result = LoginLogic::getScanCode($redirectUri);
  152. if (false === $result) {
  153. return $this->fail(LoginLogic::getError() ?? '未知错误');
  154. }
  155. return $this->success('', $result);
  156. }
  157. /**
  158. * @notes 网站扫码登录
  159. * @return \think\response\Json
  160. * @author 段誉
  161. * @date 2022/10/21 10:28
  162. */
  163. public function scanLogin()
  164. {
  165. $params = (new WebScanLoginValidate())->post()->goCheck();
  166. $result = LoginLogic::scanLogin($params);
  167. if (false === $result) {
  168. return $this->fail(LoginLogic::getError() ?? '登录失败');
  169. }
  170. return $this->success('', $result);
  171. }
  172. /**
  173. * @notes 更新用户头像昵称
  174. * @return \think\response\Json
  175. * @author 段誉
  176. * @date 2023/2/22 11:15
  177. */
  178. public function updateUser()
  179. {
  180. $params = (new WechatLoginValidate())->post()->goCheck("updateUser");
  181. LoginLogic::updateUser($params, $this->userId);
  182. return $this->success('操作成功', [], 1, 1);
  183. }
  184. public function firmLogin()
  185. {
  186. $params = (new LoginAccountValidate())->post()->goCheck("firmLogin");
  187. $result = LoginLogic::firmLogin($params);
  188. if (false === $result) {
  189. return $this->fail(LoginLogic::getError());
  190. }
  191. return $this->data($result);
  192. }
  193. }