瀏覽代碼

用户名生成规则

lizhen_gitee 1 年之前
父節點
當前提交
c7d4bdeff8
共有 2 個文件被更改,包括 4 次插入162 次删除
  1. 0 160
      application/api/controller/User.php
  2. 4 2
      application/common/library/Auth.php

+ 0 - 160
application/api/controller/User.php

@@ -394,167 +394,7 @@ class User extends Api
         }
     }
 
-    /**
-     * 获取用户openid
-     */
-    public function getUserOpenid()
-    {
-        $code = $this->request->param('code');// code值
-        if (!$code) {
-            $this->error(__('Invalid parameters'));
-        }
-        $config = config("wxMiniProgram");
-        $getopenid = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $config["appid"] . "&secret=" . $config["secret"] . "&js_code=" . $code . "&grant_type=authorization_code";
-        $openidInfo = $this->getJson($getopenid);
-        if (!isset($openidInfo["openid"])) {
-            $this->error("用户openid获取失败", $openidInfo);
-        }
-        //  获取的结果存入数据库
-        $sessionkeyModel = new \app\common\model\UserSessionkey();
-        if ($sessionkeyModel->where(["openid" => $openidInfo["openid"]])->find()) {
-            $update = [];
-            $update["sessionkey"] = $openidInfo["session_key"];
-            $res = $sessionkeyModel->update($update, ["openid" => $openidInfo["openid"]]);
-        } else {
-            $insert = [];
-            $insert["sessionkey"] = $openidInfo["session_key"];
-            $insert["openid"] = $openidInfo["openid"];
-            $insert["createtime"] = time();
-            $res = $sessionkeyModel->insert($insert);
-        }
-
-        if ($res) {
-            $this->success("获取成功!", $openidInfo);
-        } else {
-            $this->error("获取失败!");
-        }
-
-    }
-
-    /**
-     * 微信小程序登录
-     */
-    public function wxMiniProgramLogin()
-    {
-        $openid = $this->request->param('openid');// openid值
-        $encryptedData = $this->request->param('encryptedData');// 加密数据
-        $iv = $this->request->param('iv');// 加密算法
-        $signature = $this->request->param('signature');// 签名验证
-        $rawData = $this->request->param('rawData');// 签名验证
-        $logintype = $this->request->param('loginType', 1);// 登录方式:1=手机号,2=微信授权openid
-
-        if (!$openid || !$encryptedData || !$iv) {
-            $this->error(__('Invalid parameters'));
-        }
-        $encryptedData = urldecode($encryptedData);
-
-        $config = config("wxMiniProgram");
-        // 获取openid和sessionkey
-        $sessionkeyModel = new \app\common\model\UserSessionkey();
-        $openidInfo = $sessionkeyModel->where(["openid" => $openid])->find();
-        $openid = $openidInfo['openid'];
-        $session_key = $openidInfo['sessionkey'];
-
-//        // 数据签名校验
-//        $signature2 = sha1($rawData . $session_key);
-//        if ($signature != $signature2) {
-//            $this->error(__('数据签名验证失败'));
-//        }
-
-        // 根据加密数据和加密算法获取用户信息
-        $pc = new WXBizDataCrypt($config["appid"], $session_key);
-        $data = "";
-        $errCode = $pc->decryptData($encryptedData, $iv, $data);
-
-        if ($errCode == 0) {
-            $data = json_decode($data, true);
-            // 用户登录逻辑 === 开始
-            $userModel = new \app\common\model\User();
-            $auth = \app\common\library\Auth::instance();
-            if ($logintype == 1) { // 手机号登录
-                $userInfo = $userModel->where(["mobile" => $data["purePhoneNumber"]])->find();
-                // 用户信息不存在时使用
-                $extend = ["mobile" => $data["purePhoneNumber"]];
 
-            } else { // 微信授权openid登录
-                $userInfo = $userModel->where(["openid" => $openid])->find();
-                // 用户信息不存在时使用
-                $extend = [
-                    'openid'   => $data['openId'],
-                    'nickname' => $data['nickName'],
-                    'avatar'   => $data['avatarUrl'],
-                    'gender'   => $data['gender'],
-                ];
-            }
-            // 判断用户是否已经存在
-            if ($userInfo) { // 登录
-                $user = \app\common\model\User::get($userInfo["id"]);
-                if (!$user) {
-                    $this->error("网络错误!请稍后重试");
-                }
-                $user->save(["logintime" => time()]);
-                $res = $auth->direct($user->id);
-                $is_register = 0;
-            } else { // 注册
-                // 先随机一个用户名,随后再变更为u+数字id
-                $username = Random::alnum(20);
-                $password = Random::alnum(6);
-
-                Db::startTrans();
-                try {
-                    // 默认注册一个会员
-                    $result = $auth->register($username, $password, "", $extend);
-
-                    if (!$result) {
-                        return false;
-                    }
-                    $user = $auth->getUser();
-                    $fields = ['username' => 'u' . $user->id];
-
-                    // 更新会员资料
-                    $user = \app\common\model\User::get($user->id);
-                    $user->save($fields);
-
-                    Db::commit();
-                } catch (PDOException $e) {
-                    Db::rollback();
-                    $auth->logout();
-                    return false;
-                }
-
-                // 写入登录Cookies和Token
-                $res = $auth->direct($user->id);
-                $is_register = 1;
-            }
-            $userInfo = $auth->getUserinfo();
-            $userInfo["is_register"] = $is_register;
-            if ($res) {
-                $this->success("登录成功!", $userInfo);
-            } else {
-                $this->error("登录失败!");
-            }
-            // 用户登录逻辑 === 结束
-        } else {
-            $this->error("解密失败!", ["code" => $errCode]);
-        }
-    }
-
-    /**
-     * json 请求
-     * @param $url
-     * @return mixed
-     */
-    private function getJson($url)
-    {
-        $ch = curl_init();
-        curl_setopt($ch, CURLOPT_URL, $url);
-        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
-        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
-        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-        $output = curl_exec($ch);
-        curl_close($ch);
-        return json_decode($output, true);
-    }
 
     /**
      * 运营商一键登录

+ 4 - 2
application/common/library/Auth.php

@@ -166,7 +166,7 @@ class Auth
 //            $this->setError('Nickname already exist');
 //            return false;
 //        }
-        if (!isset($extend['openid'])) {
+        if (isset($extend['openid'])) {
             if (!empty($extend['openid']) && User::getByOpenid($extend['openid'])) {
                 $this->setError('微信账号已存在');
                 return false;
@@ -190,7 +190,7 @@ class Auth
         $data = [
             'u_id'      => $this->getUinqueId(8, [$ids]),
             'invite_no' => $this->getUinqueNo(8, $invite_no),
-            'username'  => $username,
+//            'username'  => $username,
 //            'password'  => $password,
             'mobile'    => $mobile,
             'level'     => 0,
@@ -220,6 +220,8 @@ class Auth
             $user = User::create($params, true);
 
             $this->_user = User::get($user->id);
+            $this->_user->username = 'u' . 10000 + $user->id;
+            $this->_user->save();
 
             //设置Token
             $this->_token = Random::uuid();