123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- <?php
- namespace app\api\controller;
- use app\api\model\UsersModel;
- use app\common\controller\Api;
- use fast\Random;
- use think\Cache;
- use think\Controller;
- use think\Db;
- use think\Loader;
- /**
- * 登录接口
- */
- class Login extends Controller
- {
- protected $noNeedLogin = '*';
- protected $noNeedRight = '*';
- /**
- * 手机号登录
- * @ApiMethod (POST)
- * @param string $user_tel 账号
- * @param string $user_pwd 密码
- */
- public function Login()
- {
- $data = $this->request->post();
- $rules = [
- 'user_tel' => 'require|max:11|number|min:11',
- 'user_pwd' => 'require|max:25|min:6',
- ];
- $msg = [
- 'user_tel.require' => '手机号不能为空',
- 'user_tel.max' => '手机号长度不正确',
- 'user_tel.min' => '手机号长度不正确',
- 'user_tel.number' => '手机号必须为数字',
- 'user_pwd.require' => '密码不能为空',
- '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 json(['code' => 100, 'msg' => '', 'data' => $validata]);
- }
- $valdatatel = UsersModel::where('user_tel', $data['user_tel'])->find(); //判断手机号是否存在
- if ($valdatatel) {
- $data['user_pwd'] = sha1(md5($data['user_pwd'])); //加密验证密码
- $where = array(
- 'user_tel' => $data['user_tel'],
- 'user_pwd' => $data['user_pwd'],
- );
- $validatapwd = Db::name('users')->where($data)->field('user_id')->find(); //判断密码是否正确
- if ($valdatatel['status_switch'] == 0) {
- return json(['code' => 100, 'msg' => '此账号已被禁用', 'data' => []]);
- }
- if ($validatapwd) {
- $validatapwd['token'] = self::settoken($validatapwd['user_id']);
- return json(['code' => 200, 'msg' => '登陆成功,欢迎回来哦', 'data' => $validatapwd]);
- } else {
- return json(['code' => 100, 'msg' => '密码错误', 'data' => []]);
- }
- } else {
- return json(['code' => 100, 'msg' => '手机号不存在', 'data' => []]);
- }
- }
- }
- /**
- * 微信登录
- * @ApiMethod (POST)
- * @param string $user_nickname 昵称
- * @param string $user_avatar 头像
- * @param string $user_unionid unionID
- */
- public function wechatLogin()
- {
- $params = $this->request->post();
- $rules = [
- 'user_nickname' => 'require',
- 'user_avatar' => 'require',
- 'user_unionid' => 'require',
- ];
- $msg = [
- 'user_nickname.require' => '昵称未获取',
- 'user_avatar.require' => '头像未获取',
- 'user_unionid.require' => '开放id未获取',
- ];
- $validata = $this->validate($params, $rules, $msg);
- if (is_string($validata)) {
- return json(['code' => 100, 'msg' => '', 'data' => $validata]);
- }
- $validataopenid = UsersModel::where('user_unionid', $params['user_unionid'])->field('user_id,user_tel')->find(); //判断QQ或者微信登录的openid是否正确
- if ($validataopenid) {
- if ($validataopenid['user_tel'] == "") {
- return json(['code' => 10, 'msg' => '请先绑定手机号', 'data' => $validataopenid]);
- }
- $validataopenid['token'] = self::settoken($validataopenid['user_id']);
- return json(['code' => 200, 'msg' => '登陆成功,欢迎回来哦', 'data' => $validataopenid]);
- } else {
- return json(['code' => 10, 'msg' => '请先绑定手机号', 'data' => $params]);
- // $data = array(
- // 'user_nickname' => $params['user_nickname'],
- // 'user_avatar' => $params['user_avatar'],
- // 'create_time' => date('Y-m-d H:i:s', time()),
- // 'user_unionid' => $params['user_unionid'],
- // 'type' => 1,
- // );
- // $userId['user_id'] = Db::name('users')->insertGetId($data);
- // if ($userId) {
- // return json(['code' => 10, 'msg' => '请去绑定手机号', 'data' => []]);
- // } else {
- // return json(['code' => 100, 'msg' => '登录失败', 'data' => []]);
- // }
- }
- }
- /**
- * QQ登录
- * @ApiMethod (POST)
- * @param string $user_nickname 昵称
- * @param string $user_avatar 头像
- * @param string $user_unionid unionID
- */
- public function qqLogin()
- {
- $params = $this->request->post();
- $rules = [
- 'user_nickname' => 'require',
- 'user_avatar' => 'require',
- 'user_unionid' => 'require',
- ];
- $msg = [
- 'user_nickname.require' => '昵称未获取',
- 'user_avatar.require' => '头像未获取',
- 'user_unionid.require' => '开放id未获取',
- ];
- $validata = $this->validate($params, $rules, $msg);
- if (is_string($validata)) {
- return json(['code' => 100, 'msg' => '', 'data' => $validata]);
- }
- $validataopenid = UsersModel::where('user_unionid', $params['user_unionid'])->field('user_id,user_tel')->find(); //判断QQ或者微信登录的openid是否正确
- $validataopenid['token'] = self::settoken($validataopenid['user_id']);
- if ($validataopenid) {
- if ($validataopenid['user_tel'] == "") {
- return json(['code' => 10, 'msg' => '请先绑定手机号', 'data' => $validataopenid]);
- }
- return json(['code' => 200, 'msg' => '登陆成功,欢迎回来哦', 'data' => $validataopenid]);
- } else {
- return json(['code' => 10, 'msg' => '请先绑定手机号', 'data' => $params]);
- // $data = array(
- // 'user_nickname' => $params['user_nickname'],
- // 'user_avatar' => $params['user_avatar'],
- // 'create_time' => $params('Y-m-d H:i:s', time()),
- // 'user_tjtel' => $params['user_tjtel'],
- // 'user_unionid' => $params['user_openid'],
- // 'type' => 2,
- // );
- // $userId['user_id'] = Db::name('users')->insertGetId($params);
- // if ($userId) {
- //
- // return json(['code' => 10, 'msg' => '请先绑定手机号','data' => []]);
- // } else {
- // return json(['code' => 100, 'msg' => '登录失败','data' => []]);
- // }
- }
- }
- /**
- * 添加登录手机号登录
- * @ApiMethod (POST)
- * @param string $user_tel 用户手机号
- * @param string $code 验证码
- * @param string $user_tjtel 用户手机号
- * @param string $user_nickname 昵称
- * @param string $user_avatar 头像
- * @param string $user_unionid unionID
- * @param string $type 0手机号1微信2QQ
- */
- public function addUserTel()
- {
- $params = $this->request->post();
- $rules = [
- // 'user_id' => 'require|number',
- 'code' => 'require|number',
- 'user_tel' => 'require|number',
- 'user_tjtel' => 'require|number',
- //'token' => 'require',
- ];
- $msg = [
- //'user_id.require' => '网络错误1',
- 'code .require' => '网络错误2',
- 'user_tel.require' => '手机号不能为空',
- 'user_tjtel.require' => '推荐人手机号不能为空',
- //'token.require' => '网络错误3',
- // 'user_id.number' => '网络错误4',
- 'code.number' => '网络错误5',
- 'user_tel.number' => '网络错误6',
- 'user_tjtel.number' => '网络错误7',
- ];
- $validate = $this->validate($params, $rules, $msg);
- if (is_string($validate)) {
- return json(['code' => 100, 'msg' => $validate, 'data' => []]);
- }
- $check = '/^(1(([35789][0-9])|(47)))\d{8}$/';
- if (!preg_match($check, $params['user_tel'])) {
- return json(['code' => 100, 'msg' => '手机号不合法', 'data' => []]);
- }
- //检查数据库是否有此推荐人手机号
- $user_tjtel = UsersModel::where('user_tel', $params['user_tjtel'])->find();
- if (!$user_tjtel) {
- return json(['code' => 100, 'msg' => '无此推荐人', 'data' => []]);
- }
- //判断验证码是否正确
- if (!Cache::get($params['code'])) {
- return json(['code' => 100, 'msg' => '验证码不正确', 'data' => []]);
- }
- //Cache::rm($params['code']);
- // 判断是否有此手机号
- $user_tel = UsersModel::where('user_tel', $params['user_tel'])->find();
- if ($user_tel) {
- $upddata = array(
- 'user_tjtel' => $params['user_tjtel'],
- 'type' => $params['type'],
- 'create_time' => date('Y-m-d H:i:s', time()),
- 'user_unionid' => $params['user_unionid'],
- 'user_nickname' => $params['user_nickname'],
- 'user_avatar' => $params['user_avatar'],
- );
- $addUserMember = Db::name('Users')->where('user_tel', $params['user_tjtel'])->setInc('user_member_count', 1); // 给上机的下级成员总数加1
- $updUsertelAndwechat = Db::name('users')->where('user_id', $user_tel['user_id'])->update($upddata);
- if ($updUsertelAndwechat) {
- $token = self::settoken($user_tel['user_id']);
- $res = array(
- "user_id" => $user_tel['user_id'],
- "token" => $token,
- );
- return json(['code' => 200, 'msg' => '绑定成功', 'data' => $res]);
- } else {
- return json(['code' => 100, 'msg' => '绑定失败', 'data' => []]);
- }
- }
- $data = array(
- 'user_tel' => $params['user_tel'],
- 'user_tjtel' => $params['user_tjtel'],
- 'type' => $params['type'],
- 'create_time' => date('Y-m-d H:i:s', time()),
- 'user_unionid' => $params['user_unionid'],
- 'user_nickname' => $params['user_nickname'],
- 'user_avatar' => $params['user_avatar'],
- );
- $adduser = Db::name('users')->insertGetId($data);
- if ($adduser) {
- $token = self::settoken($adduser);
- $res = array(
- "user_id" => $adduser,
- "token" => $token,
- );
- return json(['code' => 200, 'msg' => '绑定成功', 'data' => $res]);
- } else {
- return json(['code' => 100, 'msg' => '绑定失败', 'data' => []]);
- }
- }
- /**
- * 验证手机号是否已存在
- * @ApiMethod (POST)
- * 可以通过@ApiInternal忽略请求的方法
- * @ApiInternal
- */
- public function validatatel($tel)
- {
- $tel = UsersModel::where('user_tel', $tel)->find();
- $num = count($tel);
- if ($num > 0) {
- return json(['code' => 100, 'msg' => '手机号已存在', 'data' => []]);
- }
- }
- /**
- * 服务协议
- * @ApiMethod (POST)
- */
- public function agreement()
- {
- $data = Db::name('agreement')->where('type', 0)->find();
- if ($data) {
- return json(['code' => 200, 'msg' => '绑定成功', 'data' => $data]);
- } else {
- return json(['code' => 100, 'msg' => '网络错误', 'data' => []]);
- }
- }
- /**
- * 生成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);
- }
- }
|