Login.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. namespace App\Http\Controllers\Manager;
  3. use App\Models\Manager\AdminUser;
  4. use App\Http\Requests\Manager\Login as Request;
  5. use App\Models\Manager\AuthRule;
  6. use App\Facades\Servers\Encrypts\AccessToken;
  7. use App\Models\Manager\Personnel\Employee as EmployeeModel;
  8. use App\Facades\Servers\Sms\VerifyCode as Sms;
  9. use App\Models\Manager\Personnel\EmployeeOpenid as EmployeeOpenidModel;
  10. use App\Servers\Wechat\WeChatWebApp;
  11. use App\Models\Manager\Personnel\RolesAuthRule as RolesAuthRuleModel;
  12. /**
  13. * 管理后台登录控制器
  14. * @author 唐远望
  15. * @version 1.0
  16. * @date 2025-12-02
  17. *
  18. * */
  19. class Login extends Manager
  20. {
  21. /**
  22. * 登录方法 /manager/login/index
  23. * @author 唐远望
  24. * @version 1.0
  25. * @date 2025-12-02
  26. * @param string username 登录账号
  27. * @param string password 登录密码
  28. *
  29. * */
  30. public function index(Request $Request, AdminUser $AdminUser, AuthRule $AuthRule, EmployeeModel $EmployeeModel,RolesAuthRuleModel $RolesAuthRuleModel)
  31. {
  32. // 验证规则
  33. $Request->scene('login')->validate();
  34. // 接收数据
  35. $username = $Request->input('username', '');
  36. // 接收数据
  37. $password = $Request->input('password', '');
  38. if (strtolower($username) == 'admin') {
  39. // 查询用户
  40. $admin = $AdminUser->orWhere('username', $username)->first(['uid', 'username', 'phone', 'status', 'password', 'insert_time', 'update_time']);
  41. // 用户不存在
  42. if (!$admin) return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
  43. // 用户不存在
  44. if ($admin['status']) return json_send(['code' => 'error', 'msg' => '该账号已停用']);
  45. // 转数组
  46. $admin = $admin->toArray();
  47. // 比对密码
  48. if (md5($password) != $admin['password']) return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
  49. // 登录
  50. $accessToken = $AdminUser->Login($admin['uid'], 'manager');
  51. // 比对密码
  52. if (isset($accessToken['error'])) return json_send(['code' => 'error', 'msg' => '登录失败', 'data' => $accessToken['data']]);
  53. // 获取权限列表
  54. $accessToken['username'] = $admin['username'];
  55. // 获取权限列表
  56. } else {
  57. $admin = $EmployeeModel->where('employee_code', $username)->first(['company_id','company_id','id as uid', 'name as username', 'mobile as phone', 'status', 'password', 'insert_time', 'update_time']);
  58. // 用户不存在
  59. if (!$admin) return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
  60. // 用户不存在
  61. if ($admin['status']) return json_send(['code' => 'error', 'msg' => '该账号已停用']);
  62. // 转数组
  63. $admin = $admin->toArray();
  64. // 比对密码
  65. if (md5($password) != $admin['password']) return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
  66. // 登录
  67. $accessToken = $EmployeeModel->Login($admin['uid'],$admin['company_id'], 'manager');
  68. // 比对密码
  69. if (isset($accessToken['error'])) return json_send(['code' => 'error', 'msg' => '登录失败', 'data' => $accessToken['data']]);
  70. // 获取权限列表
  71. $accessToken['username'] = $admin['username'];
  72. }
  73. // 表单令牌
  74. return json_send(['code' => 'success', 'msg' => '登录成功', 'data' => $accessToken]);
  75. }
  76. /**
  77. * 获取用户页面权限 /manager/login/auth_rules'
  78. * @author 唐远望
  79. * @version 1.0
  80. * @date 2026-01-30
  81. * @param string username 登录账号
  82. * @param string password 登录密码
  83. *
  84. */
  85. public function auth_rules(Request $Request, AuthRule $AuthRule, RolesAuthRuleModel $RolesAuthRuleModel)
  86. {
  87. $access_token = $Request->input('access_token', '');
  88. if (!isset($access_token)) return json_send(['code' => 'error', 'msg' => '缺少参数']);
  89. $auth_rules = [];
  90. if ($access_token['is_admin'] == 0) {
  91. $auth_rules = $RolesAuthRuleModel->getAuthList($access_token['uid'], '0', 'manager');
  92. } else {
  93. $auth_rules = $AuthRule->getAuthList($access_token['uid'], '1', 'manager');
  94. }
  95. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $auth_rules]);
  96. }
  97. /**
  98. * 登录方法 /manager/login/out
  99. * @author 唐远望
  100. * @version 1.0
  101. * @date 2025-12-02
  102. * @param string username 登录账号
  103. * @param string password 登录密码
  104. *
  105. * */
  106. public function out(Request $Request, AdminUser $AdminUser, EmployeeModel $EmployeeModel)
  107. {
  108. $token = $Request->input('access_token_manager', '');
  109. // 解码
  110. $userInfo = AccessToken::decode($token);
  111. // 验证规则
  112. $uid = $userInfo['uid'];
  113. $is_admin = $userInfo['is_admin'];
  114. if ($is_admin == '1') {
  115. // 退出登录
  116. $AdminUser->LoginOut($uid, 'manager');
  117. } else {
  118. $EmployeeModel->LoginOut($uid, 'manager');
  119. }
  120. // 表单令牌
  121. return json_send(['code' => 'success', 'msg' => '退出成功', 'data' => '']);
  122. }
  123. /**
  124. * 手机号码登录 /manager/login/mobile
  125. * @author 唐远望
  126. * @version 1.0
  127. * @date 2025-12-04
  128. * @param string mobile 手机号码
  129. * @param string password 登录密码
  130. *
  131. */
  132. public function mobile(Request $Request, AuthRule $AuthRule, EmployeeModel $EmployeeModel)
  133. {
  134. // 验证规则
  135. $Request->scene('mobile')->validate();
  136. // 接收数据
  137. $phone = $Request->input('phone', '');
  138. // 接收数据
  139. $password = $Request->input('password', '');
  140. // 查询用户
  141. $admin = $EmployeeModel->where('mobile', $phone)->first(['company_id','id as uid', 'name as username', 'mobile as phone', 'status', 'password', 'insert_time', 'update_time']);
  142. // 用户不存在
  143. if (!$admin) return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
  144. // 用户不存在
  145. if ($admin['status']) return json_send(['code' => 'error', 'msg' => '该账号已停用']);
  146. // 转数组
  147. $admin = $admin->toArray();
  148. // 比对密码
  149. if (md5($password) != $admin['password']) return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
  150. // 登录
  151. $accessToken = $EmployeeModel->Login($admin['uid'],$admin['company_id'], 'manager');
  152. // 比对密码
  153. if (isset($accessToken['error'])) return json_send(['code' => 'error', 'msg' => '登录失败', 'data' => $accessToken['data']]);
  154. // 获取权限列表
  155. $accessToken['username'] = $admin['username'];
  156. // 表单令牌
  157. return json_send(['code' => 'success', 'msg' => '登录成功', 'data' => $accessToken]);
  158. }
  159. /**
  160. * 发送验证码
  161. * @author 唐远望
  162. * @version 1.0
  163. * @date 2026-01-15
  164. * @param string phone 手机号码
  165. *
  166. */
  167. public function send_code(Request $Request, EmployeeModel $EmployeeModel)
  168. {
  169. // 验证规则
  170. $Request->scene('send_code')->validate();
  171. // 接收数据
  172. $mobile = request('phone', '');
  173. if (!$mobile) return json_send(['code' => 'error', 'msg' => '请先填写手机号']);
  174. // 获取数据
  175. $session = session('loginSmsCode');
  176. // 如果有数据,并且验证码创建的时间在一分钟之内
  177. if ($session && time() - $session['create_time'] < 60) return json_send(['code' => 'error', 'msg' => '请稍后再试']);
  178. // 查询用户
  179. $admin = $EmployeeModel->query()->where('mobile', $mobile)->first(['status']);
  180. if ($admin && $admin['status']) return json_send(['code' => 'error', 'msg' => '用户已被停用']);
  181. $code = strval(rand(100000, 999999));
  182. $result = Sms::sendCode($mobile, $code);
  183. if (isset($result['error'])) return json_send(['code' => 'error', 'msg' => $result['error']]);
  184. $session = ['code' => $code, 'mobile' => $mobile, 'create_time' => time()];
  185. session(['loginSmsCode' => $session]);
  186. return json_send(['code' => 'success', 'msg' => '发送成功', 'data' => $code]);
  187. }
  188. /**
  189. * 邮箱登录 /manager/login/email
  190. * @author 唐远望
  191. * @version 1.0
  192. * @date 2025-12-04
  193. * @param string email 邮箱号码
  194. * @param string password 登录密码
  195. *
  196. */
  197. public function email(Request $Request, AuthRule $AuthRule, EmployeeModel $EmployeeModel)
  198. {
  199. // 验证规则
  200. $Request->scene('email')->validate();
  201. // 接收数据
  202. $email = $Request->input('email', '');
  203. // 接收数据
  204. $password = $Request->input('password', '');
  205. // 查询用户
  206. $admin = $EmployeeModel->where('email', $email)->first(['company_id','company_id','id as uid', 'name as username', 'mobile as phone', 'status', 'password', 'insert_time', 'update_time']);
  207. // 用户不存在
  208. if (!$admin) return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
  209. // 用户不存在
  210. if ($admin['status']) return json_send(['code' => 'error', 'msg' => '该账号已停用']);
  211. // 转数组
  212. $admin = $admin->toArray();
  213. // 比对密码
  214. if (md5($password) != $admin['password']) return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
  215. // 登录
  216. $accessToken = $EmployeeModel->Login($admin['uid'],$admin['company_id'], 'manager');
  217. // 比对密码
  218. if (isset($accessToken['error'])) return json_send(['code' => 'error', 'msg' => '登录失败', 'data' => $accessToken['data']]);
  219. // 获取权限列表
  220. $accessToken['username'] = $admin['username'];
  221. // 表单令牌
  222. return json_send(['code' => 'success', 'msg' => '登录成功', 'data' => $accessToken]);
  223. }
  224. /**
  225. * 手机验证码登录 /manager/login/mobile_code
  226. * @author 唐远望
  227. * @version 1.0
  228. * @date 2026-01-15
  229. * @param string mobile 手机号码
  230. * @param string code 验证码
  231. *
  232. */
  233. public function mobile_code(Request $Request, AuthRule $AuthRule, EmployeeModel $EmployeeModel)
  234. {
  235. // 验证规则
  236. $Request->scene('mobile_code')->validate();
  237. // 接收数据
  238. $phone = $Request->input('phone', '');
  239. // 接收数据
  240. $code = $Request->input('code', '');
  241. // 获取数据
  242. $session = session('loginSmsCode');
  243. if (!$session) return json_send(['code' => 'error', 'msg' => '请先获取手机号验证码']);
  244. if ($session['code'] != $code || $session['phone'] != $phone) return json_send(['code' => 'error', 'msg' => '验证码错误']);
  245. // 查询用户
  246. $admin = $EmployeeModel->where('mobile', $phone)->first(['company_id','id as uid', 'name as username', 'mobile as phone', 'status', 'password', 'insert_time', 'update_time']);
  247. // 用户不存在
  248. if (!$admin) return json_send(['code' => 'error', 'msg' => '账号不存在']);
  249. // 用户不存在
  250. if ($admin['status']) return json_send(['code' => 'error', 'msg' => '该账号已停用']);
  251. // 转数组
  252. $admin = $admin->toArray();
  253. // 登录
  254. $accessToken = $EmployeeModel->Login($admin['uid'],$admin['company_id'], 'manager');
  255. // 比对密码
  256. if (isset($accessToken['error'])) return json_send(['code' => 'error', 'msg' => '登录失败', 'data' => $accessToken['data']]);
  257. // 获取权限列表
  258. $accessToken['username'] = $admin['username'];
  259. // 表单令牌
  260. return json_send(['code' => 'success', 'msg' => '登录成功', 'data' => $accessToken]);
  261. }
  262. /**
  263. * 微信扫码登录 /manager/login/wechat
  264. * @author 唐远望
  265. * @version 1.0
  266. * @date 2026-01-19
  267. * @param string open_code 微信扫码登录的code
  268. *
  269. */
  270. public function wechat(Request $Request, AuthRule $AuthRule, EmployeeModel $EmployeeModel, EmployeeOpenidModel $EmployeeOpenidModel)
  271. {
  272. // 验证规则
  273. $Request->scene('wechat')->validate();
  274. // 接收数据
  275. $open_code = $Request->input('open_code', '');
  276. $wechatApp = new WeChatWebApp();
  277. $tokenData = $wechatApp->getAccessTokenByCode($open_code);
  278. if (!$tokenData) return json_send(['code' => 'error', 'msg' => '获取微信用户信息失败']);
  279. $user_open_data = $EmployeeOpenidModel->where(['openid' => $tokenData['openid']])->first();
  280. if (!$user_open_data) return json_send(['code' => 'error', 'msg' => '未绑定账号,请登录后在绑定']);
  281. // 查询用户
  282. $admin = $EmployeeModel->where('id', $user_open_data->employee_id)->first(['company_id','company_id','id as uid', 'name as username', 'mobile as phone', 'status', 'password', 'insert_time', 'update_time']);
  283. // 用户不存在
  284. if (!$admin) return json_send(['code' => 'error', 'msg' => '账号不存在']);
  285. // 用户不存在
  286. if ($admin['status']) return json_send(['code' => 'error', 'msg' => '该账号已停用']);
  287. // 转数组
  288. $admin = $admin->toArray();
  289. // 登录
  290. $accessToken = $EmployeeModel->Login($admin['uid'],$admin['company_id'], 'manager');
  291. // 比对密码
  292. if (isset($accessToken['error'])) return json_send(['code' => 'error', 'msg' => '登录失败', 'data' => $accessToken['data']]);
  293. // 获取权限列表
  294. $accessToken['username'] = $admin['username'];
  295. // 表单令牌
  296. return json_send(['code' => 'success', 'msg' => '登录成功', 'data' => $accessToken]);
  297. }
  298. /**
  299. * 微信扫码授权绑定 /manager/login/wechat_bind
  300. * @author 唐远望
  301. * @version 1.0
  302. * @date 2026-01-19
  303. * @param string open_code 微信扫码登录的code
  304. *
  305. */
  306. public function wechat_bind(Request $Request,EmployeeOpenidModel $EmployeeOpenidModel)
  307. {
  308. // 验证规则
  309. $Request->scene('wechat_bind')->validate();
  310. $uid = request('access_token.uid', 0);
  311. // 接收数据
  312. $open_code = $Request->input('open_code', '');
  313. $wechatApp = new WeChatWebApp();
  314. $tokenData = $wechatApp->getAccessTokenByCode($open_code);
  315. if (!$tokenData) return json_send(['code' => 'error', 'msg' => '获取微信用户信息失败']);
  316. $user_open_data = $EmployeeOpenidModel->where(['openid' => $tokenData['openid'],'employee_id'=> $uid])->first();
  317. if ($user_open_data) return json_send(['code' => 'error', 'msg' => '微信已绑定,无需重复绑定']);
  318. //新增绑定记录
  319. $EmployeeOpenidModel->create([
  320. 'openid' => $tokenData['openid'],
  321. 'unionid' => isset($tokenData['unionid']) ? $tokenData['unionid'] : '',
  322. 'employee_id' => $uid,
  323. 'insert_time' => time(),
  324. ]);
  325. return json_send(['code' => 'success', 'msg' => '绑定成功','data'=>'']);
  326. }
  327. }