Browse Source

登录优化

pull/1/head
pengda 9 months ago
parent
commit
6cfc885ed2
  1. 6
      control/index.php
  2. 4
      data/dUser.php
  3. 20
      model/mUser.php

6
control/index.php

@ -178,10 +178,10 @@ class index extends publicBase {
if (!$code) $this->ajax_json(false, '非法请求'); if (!$code) $this->ajax_json(false, '非法请求');
$m_user = new mUser(); $m_user = new mUser();
$uid = $m_user->getUid($code); $user = $m_user->getUserInfo($code);
$token = $m_user->getToken($uid); $token = $m_user->getToken($user['uid']);
$this->ajax_json(true, '登录成功', array('uid' => $uid, 'token' => $token)); $this->ajax_json(true, '登录成功', array('uid' => $user['uid'], 'identifier' => $user['identifier'], 'token' => $token));
} }
public function ajax_contact_us() { public function ajax_contact_us() {

4
data/dUser.php

@ -7,9 +7,9 @@ include_once SERVER_ROOT . '/data/dBase.php';
class dUser extends dBase { class dUser extends dBase {
protected $fieldlist = array( protected $fieldlist = array(
'tcm_user' => array( 'tcm_user' => array(
'id',
'openid',
'uid', 'uid',
'openid',
'identifier',
'status', 'status',
'create_time', 'create_time',
), ),

20
model/mUser.php

@ -14,7 +14,7 @@ class mUser extends mBase {
$this->tbl = 'tcm_user'; $this->tbl = 'tcm_user';
} }
public function getUid($code) { public function getUserInfo($code) {
$openid = $this->getOpenid($code); $openid = $this->getOpenid($code);
if (!$openid) { if (!$openid) {
$this->setError('获取openid失败'); $this->setError('获取openid失败');
@ -22,23 +22,23 @@ class mUser extends mBase {
} }
$user = $this->getUserByOpenid($openid); $user = $this->getUserByOpenid($openid);
if ($user) return $user['uid']; if ($user) return $user;
//获取唯一uid串 //获取唯一uid串
$uid = $this->createUniqueUid($openid); $identifier = $this->createUniqueID($openid);
$user = $this->getUserByUid($uid); $user = $this->getUserByIdentifier($identifier);
if ($user) { if ($user) {
$this->setError('uid生成失败'); $this->setError('uid生成失败');
return false; return false;
} }
$id = $this->createUser(array('uid' => $uid, 'openid' => $openid)); $uid = $this->createUser(array('identifier' => $identifier, 'openid' => $openid));
if (!$id) { if (!$uid) {
$this->setError('用户创建失败'); $this->setError('用户创建失败');
return false; return false;
} }
return $id; return $this->getUserByUid($uid);
} }
public function getOpenid($code) { public function getOpenid($code) {
@ -63,13 +63,17 @@ class mUser extends mBase {
return $this->obj->select($this->tbl, array('sql' => '`openid`=?', 'vals' => array($openid))); return $this->obj->select($this->tbl, array('sql' => '`openid`=?', 'vals' => array($openid)));
} }
function createUniqueUid($openid) { function createUniqueID($openid) {
$uuid = uniqid($openid, true); $uuid = uniqid($openid, true);
$hash = hash('sha256', $uuid); $hash = hash('sha256', $uuid);
$decimal = base_convert(substr($hash, 0, 16), 16, 10); $decimal = base_convert(substr($hash, 0, 16), 16, 10);
return substr($decimal, 0, 10); return substr($decimal, 0, 10);
} }
public function getUserByIdentifier($identifier) {
return $this->obj->select($this->tbl, array('sql' => '`identifier`=?', 'vals' => array($identifier)));
}
public function getUserByUid($uid) { public function getUserByUid($uid) {
return $this->obj->select($this->tbl, array('sql' => '`uid`=?', 'vals' => array($uid))); return $this->obj->select($this->tbl, array('sql' => '`uid`=?', 'vals' => array($uid)));
} }

Loading…
Cancel
Save