Login.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace app\api\controller\dinner;
  3. use app\hander\HelpHander;
  4. use think\Controller;
  5. use think\Db;
  6. class Login extends Controller
  7. {
  8. protected $middleware = ['Cross'];
  9. public function login(){
  10. $phone = input('phone','','trim');
  11. $password = input('password','','trim');
  12. if(!$phone){
  13. HelpHander::error('请输入手机号');
  14. }
  15. if(!$password){
  16. HelpHander::error('请输入密码');
  17. }
  18. $ret = model('DinnerUser')->login($phone,$password);
  19. HelpHander::success($ret);
  20. }
  21. public function autologin(){
  22. $token = input('token');
  23. $userId = input('userId');
  24. if(empty($token) || empty($userId)){
  25. HelpHander::error('登录信息已失效',100200);
  26. }
  27. $tokeninfo = Db::name('token')
  28. ->where('code',$token)
  29. ->where('user_id',$userId)
  30. ->find();
  31. if(!$tokeninfo){
  32. HelpHander::error('登录信息已失效',100200);
  33. }
  34. // 检查用户是否被禁用
  35. $user = Db::name('user')->where('id',$userId)->find();
  36. if(!$user || $user['enable'] == 0 || $user['del'] == 1){
  37. HelpHander::error('登录信息已失效',100200);
  38. }
  39. // 检查点餐用户是否存在
  40. $duser = Db::name('dinner_user')->where('phone',$user['phone'])->where('del',0)->find();
  41. if(!$duser || $duser['enable'] != 1){
  42. HelpHander::error('未注册点餐系统,请联系管理员',100200);
  43. }
  44. $token = md5($user['id'].'-'.time().rand(1000,9999));
  45. $ret = Db::name('dinner_user')->where('id',$duser['id'])->update([
  46. 'login_time' => date('Y-m-d H:i:s'),
  47. 'token' => $token
  48. ]);
  49. if(!$ret){
  50. HelpHander::error('登录失败',100200);
  51. }
  52. HelpHander::success(['user_id' => $duser['id'], 'token' => $token]);
  53. }
  54. public function updatePwd(){
  55. $phone = input('mobile','','trim');
  56. $password = input('newPassword','','trim');
  57. $code = input('code','','trim');
  58. model('User')->forgetPassword($phone,$password,$code);
  59. HelpHander::success([],'修改成功');
  60. }
  61. }