LoginController.php 7.4 KB

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