Login.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\model\UsersModel;
  4. use app\common\controller\Api;
  5. use fast\Random;
  6. use think\Db;
  7. /**
  8. * 登录接口
  9. */
  10. class Login extends Api
  11. {
  12. protected $noNeedLogin = '*';
  13. protected $noNeedRight = '*';
  14. /**
  15. * 会员登录
  16. *
  17. * @param string $user_tel 账号
  18. * @param string $user_pwd 密码
  19. * @param string $user_openid qq或者微信openID
  20. */
  21. public function Login()
  22. {
  23. $data = $this->request->post();
  24. $rules = [
  25. 'user_tel' => 'require|max:11|number|min:11',
  26. 'user_pwd' => 'require|max:25|min:6',
  27. 'token' => 'require'
  28. ];
  29. $msg = [
  30. 'user_tel.require' => '手机号不能为空',
  31. 'user_tel.max' => '手机号长度不正确',
  32. 'user_tel.min' => '手机号长度不正确',
  33. 'user_tel.number' => '手机号必须为数字',
  34. 'user_pwd.require' => '密码不能为空',
  35. 'token.require' => 'token不能为空',
  36. 'user_pwd.max' => '密码长度过长',
  37. 'user_pwd.min' => '密码长度不足',
  38. ];
  39. if (isset($data['token'])) { //先删除token
  40. $delete = \app\common\library\Token::delete($data['token']);
  41. }
  42. if (isset($data['user_tel']) && isset($data['user_pwd'])) {
  43. $validata = $this->validate($data, $rules, $msg); //验证数据规则
  44. if (is_string($validata)) {
  45. return $this->result($validata, [], 100);
  46. }
  47. $valdatatel = UsersModel::where('user_tel', $data['user_tel'])->find(); //判断手机号是否存在
  48. if ($valdatatel) {
  49. $data['user_pwd'] = sha1(md5($data['user_pwd'])); //加密验证密码
  50. $validatapwd = UsersModel::where('user_pwd', $data['user_pwd'])->where('user_tel', $data['user_tel'])->find(); //判断密码是否正确
  51. $validatapwd['token'] = self::settoken($validatapwd['user_id']);
  52. if ($validatapwd) {
  53. return $this->result('登录成功,欢迎回来', $validatapwd, 200);
  54. } else {
  55. return $this->result('密码错误', [], 100);
  56. }
  57. } else {
  58. return $this->result('手机号不存在', [], 100);
  59. }
  60. }
  61. if (isset($data['user_openid'])) {
  62. $validataopenid = UsersModel::where('user_openid', $data['user_openid'])->find(); //判断QQ或者微信登录的openid是否正确
  63. if ($validataopenid) {
  64. return $this->result('登陆成功,欢迎回来', $validataopenid, 200);
  65. } else {
  66. return $this->result('暂无该用户', [], 100);
  67. }
  68. }
  69. }
  70. /**
  71. * 验证手机号是否已存在
  72. *
  73. * 可以通过@ApiInternal忽略请求的方法
  74. * @ApiInternal
  75. */
  76. public function validatatel($tel)
  77. {
  78. $tel = UsersModel::where('user_tel', $tel)->find();
  79. $num = count($tel);
  80. if ($num > 0) {
  81. return $this->result('手机号已存在', [], 100);
  82. }
  83. }
  84. /**
  85. * 服务协议
  86. */
  87. public function agreement()
  88. {
  89. $data = Db::name('agreement')->where('type', 0)->find();
  90. if ($data) {
  91. return $this->result('', $data, 200);
  92. } else {
  93. return $this->result('网络错误', [], 100);
  94. }
  95. }
  96. /**
  97. * 生成token
  98. *
  99. * 可以通过@ApiInternal忽略请求的方法
  100. * @ApiInternal
  101. */
  102. public function settoken($user_id)
  103. {
  104. $token = Random::uuid();
  105. \app\common\library\Token::set($token, $user_id, 0);
  106. return $tokenInfo = \app\common\library\Token::get($token);
  107. }
  108. }