1
0

LoginController.php 7.1 KB

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