From feaada8618d63cce0c97c156303e434892bdab37 Mon Sep 17 00:00:00 2001 From: pengda <10266652509@qq.com> Date: Fri, 30 Aug 2024 16:42:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/mUser.php | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/model/mUser.php b/model/mUser.php index b943075..a94be83 100644 --- a/model/mUser.php +++ b/model/mUser.php @@ -13,4 +13,65 @@ class mUser extends mBase { $this->obj = new dUser(); $this->tbl = 'tcm_user'; } + + 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); + if(empty($get_openid_res['openid'])){ + $this->setError('获取openid失败'); + $tool_obj = new qTool(); + $tool_obj->trackLog('tcm', $get_openid_url . "|" .$jsonres, sprintf(LOG_TRACK_SAVE_PATH, date('Y-m-d'), 'tcm_get_openid')); + return false; + } + + return $get_openid_res['openid']; + } + + public function getUserByOpenid($openid) { + return $this->obj->select($this->tbl, array('sql'=>'`openid`=?','vals'=>array($openid))); + } + + function createUniqueUid($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 getUserByUid($uid) { + return $this->obj->select($this->tbl, array('sql'=>'`uid`=?','vals'=>array($uid))); + } + + public function createUser($info){ + return $this->obj->insert($this->tbl, $info); + } + + public function getToken($uid){ + $secretKey = JWT_KEY; + $timestamp = time(); + $data = $uid . '|' . $timestamp; + $token = hash_hmac('sha256', $data, $secretKey); + return base64_encode($data . '|' . $token); + } + + public function validateToken($uid, $token){ + $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)) { + if (time() - $timestamp < 7200) { + return true; + } + } + return false; + } } \ No newline at end of file