录医案
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.0 KiB

9 months ago
<?php
/**
*
*/
include_once(SERVER_ROOT . "/model/mBase.php");
9 months ago
class mUser extends mBase {
private $obj;
private $tbl;
public function __construct() {
$this->obj = new dUser();
$this->tbl = 'tcm_user';
}
9 months ago
9 months ago
public function getUserInfo($code) {
$openid = $this->getOpenid($code);
if (!$openid) {
$this->setError('登录失败');
return false;
}
$user = $this->getUserByOpenid($openid);
9 months ago
if ($user) return $user;
//获取唯一uid串
9 months ago
$identifier = $this->createUniqueID($openid);
$user = $this->getUserByIdentifier($identifier);
if ($user) {
$this->setError('登录失败,用户已存在');
return false;
}
9 months ago
$uid = $this->createUser(array('identifier' => $identifier, 'openid' => $openid));
if (!$uid) {
$this->setError('登录失败,用户不存在');
return false;
}
9 months ago
return $this->getUserByUid($uid);
}
9 months ago
public function getOpenid($code) {
$appid = MP_APPID;
$secret = MP_SECRET;
$get_openid_url = sprintf(MP_GET_OPENID_HREF, $appid, $secret, $code);
$jsonres = $this->getCUrl($get_openid_url);
$get_openid_res = json_decode($jsonres, true);
9 months ago
if (empty($get_openid_res['openid'])) {
$this->writeLog('user', 'get_openid_error_log', $get_openid_url . "|" . $jsonres);
9 months ago
return false;
}
return $get_openid_res['openid'];
}
public function getUserByOpenid($openid) {
return $this->obj->select($this->tbl, array('sql' => '`openid`=?', 'vals' => array($openid)));
9 months ago
}
9 months ago
function createUniqueID($openid) {
9 months ago
$uuid = uniqid($openid, true);
$hash = hash('sha256', $uuid);
$decimal = base_convert(substr($hash, 0, 16), 16, 10);
return substr($decimal, 0, 10);
}
9 months ago
public function getUserByIdentifier($identifier) {
return $this->obj->select($this->tbl, array('sql' => '`identifier`=?', 'vals' => array($identifier)));
}
9 months ago
public function getUserByUid($uid) {
return $this->obj->select($this->tbl, array('sql' => '`uid`=?', 'vals' => array($uid)));
9 months ago
}
9 months ago
public function createUser($info) {
9 months ago
return $this->obj->insert($this->tbl, $info);
}
9 months ago
public function getToken($uid) {
9 months ago
$secretKey = JWT_KEY;
$timestamp = time();
$data = $uid . '|' . $timestamp;
$token = hash_hmac('sha256', $data, $secretKey);
return base64_encode($data . '|' . $token);
}
9 months ago
public function validateToken($uid, $token) {
9 months ago
$secretKey = JWT_KEY;
$decodedToken = base64_decode($token);
list($valid_uid, $timestamp, $tokenHash) = explode('|', $decodedToken);
$data = $uid . '|' . $timestamp;
$validToken = hash_hmac('sha256', $data, $secretKey);
if (hash_equals($validToken, $tokenHash) && time() - $timestamp < 7200) {
return true;
9 months ago
}
return false;
}
9 months ago
}