<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ``` ~~~ <?php namespace app\program\controller;  //命名空間 use think\App; use think\facade\Db; class ProgramService  //類名(可自定義) { private $table = "數據庫表名";  //我的數據庫表名 取你自己的表名或者表名寫在SQL語句中 ~~~ ~~~ private $authorization_uri = "https://api.weixin.qq.com/sns/jscode2session";    //微信授權獲取openID以及session_key的地址   //自定義變量 private $AppID; private $Secret; private $session_key; private $openid; private $unionid; public function __construct() {     //構造方法,用于變量賦值 也可以在定義變量時直接賦值 注, 類變量不能直接調用方法 $this -> AppID = sysconf("program.app_id"); $this -> Secret = sysconf("program.secret"); } /** * 小程序授權入口    * 接收小程序傳過來的code值獲取openID及sessionkey * @param $code * @param $rawData * @param $session_key * @param $signature * @param $iv * @param $encryptedData * @return \think\response\Json * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function authorization($code,$rawData,$signature,$iv,$encryptedData,$from_id = ''){ if(empty($code)) return json(returnData(1,'小程序授權參數code不存在'));     //拼接url地址 $url = $this -> authorization_uri . "?appid=" . $this -> AppID . "&secret=" . $this -> Secret . "&js_code=" . $code . "&grant_type=authorization_code";     //自己封裝的請求 composer 安裝php-curl 直接調用curl 請求即可 封裝是為了簡化代碼 $res = json_decode(requestGetData($url),true); // dump($res);die; if(isset($res['errcode'])) return json(returnData(1,'授權失敗',$res['errmsg']));     //變量賦值 $this -> session_key = $res['session_key']; $this -> openid = $res['openid']; // $this -> unionid = $res['unionid'];     //驗證簽名。判斷數據真實性 $signature2 = sha1(htmlspecialchars_decode($rawData) . $this -> session_key); if($signature != $signature2) return json(returnData(1,'授權失敗',"簽名認證失敗"));     //檢查用戶登錄狀態 return $this -> changeLoginStatus($this -> session_key,$iv,$encryptedData,$from_id); } /** * 檢查用戶登錄狀態 * @param $session_key * @param $iv * @param $encryptedData * @return \think\response\Json * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function changeLoginStatus($session_key,$iv,$encryptedData,$from_id){     //根據openID判斷數據庫是否存在該用戶。存在即直接返回用戶數據 $user = Db::name($this -> table) -> where("openid",$this -> openid) -> find(); if($user) {         //是否被人邀請 如無需此功能可刪除 if($from_id != '' && $user['from_id'] == 0){ Db::name($this -> table) -> where("id",$user['id']) -> update(['form_id' => $from_id]); } return json(returnData(99,'登錄成功',$user['openid'])); }       //用戶未授權,調用微信授權解密參數并存入數據庫 return $result = $this -> decryptionEncryptedData($session_key,$iv,$encryptedData,$from_id); } /** * 解密參數,保存用戶數據 * @param $session_key * @param $iv * @param $encryptedData * @return \think\response\Json */ public function decryptionEncryptedData($session_key,$iv,$encryptedData,$from_id){ if (strlen($session_key) != 24) return json(returnData(1,'授權失敗',"解密后得到的sessionKey非法")); if (strlen($iv) != 24) return json(returnData(1,'授權失敗',"解密后得到的iv非法")); $aesKey = base64_decode($session_key); $aesIV = base64_decode($iv); $aesCipher = base64_decode($encryptedData); $result = openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV); $result = json_decode($result,true); // dump($result);die; if(empty($result)) return json(returnData(1,'授權失敗',"解密后得到的Buffer非法"));     //確認小程序AppID是否為同一個 if($result['watermark']['appid'] != $this -> AppID ) return json(returnData(1,'授權失敗',"解密后得到的AppID非法"));       //需保存的用戶數組 請改成你自己的數據庫字段 $customer['openid'] = $this -> openid;     //開放平臺標識,如無需開放平臺 請注釋或刪除 $customer['unionid'] = $result['unionId'];     //用戶昵稱可能包含特殊字符,需處理 $customer['nickname'] = $this->removeEmoji($result['nickName']); $customer['sex'] = $result['gender']; $customer['city'] = $result['city']; $customer['level'] = $this -> setCustomerLevel(); $customer['from_id'] = $from_id; $customer['province'] = $result['province']; $customer['country'] = $result['country']; $customer['headimgurl'] = $result['avatarUrl']; $customer['appid'] = $result['watermark']['appid']; $customer['authorization_time'] = $result['watermark']['timestamp']; $id = Db::name($this -> table) -> insertGetId($customer); if($id) return json(returnData(99,'授權成功',$this -> openid)); return json(returnData(1,'授權失敗',"保存用戶信息出錯".Db::name($this -> table) -> getLastSql())); } /** * 處理特殊字符 * @param $text * @return string|string[]|null */ public function removeEmoji($text) { $clean_text = ""; preg_match_all("/[\x{4e00}-\x{9fa5}|0-9|a-z|A-Z|_]/u", $text, $matches); $clean_text = isset($matches[0]) ? implode('', $matches[0]) : ''; // Match Emoticons $regexEmoticons = '/[\x{1F600}-\x{1F64F}]/u'; $clean_text = preg_replace($regexEmoticons, '', $text); // Match Miscellaneous Symbols and Pictographs $regexSymbols = '/[\x{1F300}-\x{1F5FF}]/u'; $clean_text = preg_replace($regexSymbols, '', $clean_text); // Match Transport And Map Symbols $regexTransport = '/[\x{1F680}-\x{1F6FF}]/u'; $clean_text = preg_replace($regexTransport, '', $clean_text); // Match Miscellaneous Symbols $regexMisc = '/[\x{2600}-\x{26FF}]/u'; $clean_text = preg_replace($regexMisc, '', $clean_text); // Match Dingbats $regexDingbats = '/[\x{2700}-\x{27BF}]/u'; $clean_text = preg_replace($regexDingbats, '', $clean_text); return $clean_text; } ?> ~~~ ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看