123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- namespace app\api\controller;
- use app\api\model\UsersModel;
- use app\common\controller\Api;
- use fast\Random;
- use think\Db;
- /**
- * 登录接口
- */
- class Login extends Api
- {
- protected $noNeedLogin = '*';
- protected $noNeedRight = '*';
- /**
- * 会员登录
- * @ApiMethod (POST)
- * @param string $user_tel 账号
- * @param string $user_pwd 密码
- * @param string $user_openid qq或者微信openID
- */
- public function Login()
- {
- $data = $this->request->post();
- $rules = [
- 'user_tel' => 'require|max:11|number|min:11',
- 'user_pwd' => 'require|max:25|min:6',
- 'token' => 'require'
- ];
- $msg = [
- 'user_tel.require' => '手机号不能为空',
- 'user_tel.max' => '手机号长度不正确',
- 'user_tel.min' => '手机号长度不正确',
- 'user_tel.number' => '手机号必须为数字',
- 'user_pwd.require' => '密码不能为空',
- 'token.require' => 'token不能为空',
- 'user_pwd.max' => '密码长度过长',
- 'user_pwd.min' => '密码长度不足',
- ];
- if (isset($data['token'])) { //先删除token
- $delete = \app\common\library\Token::delete($data['token']);
- }
- if (isset($data['user_tel']) && isset($data['user_pwd'])) {
- $validata = $this->validate($data, $rules, $msg); //验证数据规则
- if (is_string($validata)) {
- return $this->result($validata, [], 100);
- }
- $valdatatel = UsersModel::where('user_tel', $data['user_tel'])->find(); //判断手机号是否存在
- if ($valdatatel) {
- $data['user_pwd'] = sha1(md5($data['user_pwd'])); //加密验证密码
- $validatapwd = UsersModel::where('user_pwd', $data['user_pwd'])->where('user_tel', $data['user_tel'])->find(); //判断密码是否正确
- $validatapwd['token'] = self::settoken($validatapwd['user_id']);
- if ($validatapwd) {
- return $this->result('登录成功,欢迎回来', $validatapwd, 200);
- } else {
- return $this->result('密码错误', [], 100);
- }
- } else {
- return $this->result('手机号不存在', [], 100);
- }
- }
- if (isset($data['user_openid'])) {
- $validataopenid = UsersModel::where('user_openid', $data['user_openid'])->find(); //判断QQ或者微信登录的openid是否正确
- if ($validataopenid) {
- return $this->result('登陆成功,欢迎回来', $validataopenid, 200);
- } else {
- return $this->result('暂无该用户', [], 100);
- }
- }
- }
- /**
- * 验证手机号是否已存在
- * @ApiMethod (POST)
- * 可以通过@ApiInternal忽略请求的方法
- * @ApiInternal
- */
- public function validatatel($tel)
- {
- $tel = UsersModel::where('user_tel', $tel)->find();
- $num = count($tel);
- if ($num > 0) {
- return $this->result('手机号已存在', [], 100);
- }
- }
- /**
- * 服务协议
- * @ApiMethod (POST)
- */
- public function agreement()
- {
- $data = Db::name('agreement')->where('type', 0)->find();
- if ($data) {
- return $this->result('', $data, 200);
- } else {
- return $this->result('网络错误', [], 100);
- }
- }
- /**
- * 生成token
- *
- * 可以通过@ApiInternal忽略请求的方法
- * @ApiInternal
- */
- public function settoken($user_id)
- {
- $token = Random::uuid();
- \app\common\library\Token::set($token, $user_id, 0);
- return $tokenInfo = \app\common\library\Token::get($token);
- }
- }
|