diff --git a/control/index.php b/control/index.php index fe13886..e3de114 100644 --- a/control/index.php +++ b/control/index.php @@ -181,25 +181,10 @@ class index extends publicBase { if (!$code)$this->ajax_json(false, '非法请求'); $m_user = new mUser(); - $openid = $m_user->getOpenid($code); - if (!$openid)$this->ajax_json(false, $m_user->getError()); + $uid = $m_user->getUid($code); + $token = $m_user->getToken($uid); - $user = $m_user->getUserByOpenid($openid); - if ($user){ - $token = $m_user->getToken($user['uid']); - $this->ajax_json(true, '登录成功', array('uid' => $user['uid'], 'token'=> $token)); - } - - //获取唯一uid串 - $uid = $m_user->createUniqueUid($openid); - $user = $m_user->getUserByUid($uid); - if ($user)$this->ajax_json(false, 'uid生成失败'); - - $id = $m_user->createUser(array('uid'=>$uid,'openid'=>$openid)); - if (!$id)$this->ajax_json(false, '用户创建失败'); - - $token = $m_user->getToken($user['uid']); - $this->ajax_json(true, '登录成功', array('uid' => $user['uid'], 'token'=> $token)); + $this->ajax_json(true, '登录成功', array('uid' => $uid, 'token'=> $token)); } public function ajax_contact_us(){ diff --git a/model/mUser.php b/model/mUser.php index 859f600..0dcc1fd 100644 --- a/model/mUser.php +++ b/model/mUser.php @@ -14,6 +14,33 @@ class mUser extends mBase { $this->tbl = 'tcm_user'; } + public function getUid($code) { + $openid = $this->getOpenid($code); + if (!$openid) { + $this->setError('获取openid失败'); + return false; + } + + $user = $this->getUserByOpenid($openid); + if ($user) return $user['uid']; + + //获取唯一uid串 + $uid = $this->createUniqueUid($openid); + $user = $this->getUserByUid($uid); + if ($user) { + $this->setError('uid生成失败'); + return false; + } + + $id = $this->createUser(array('uid' => $uid, 'openid' => $openid)); + if (!$id) { + $this->setError('用户创建失败'); + return false; + } + + return $id; + } + public function getOpenid($code) { $appid = MP_APPID; $secret = MP_SECRET;