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, '非法请求');
$m_user = new mUser();
$uid = $m_user->getUid($code);
$token = $m_user->getToken($uid);
$user = $m_user->getUserInfo($code);
$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() {

4
data/dUser.php

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

20
model/mUser.php

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

Loading…
Cancel
Save