做小程序的流程总结(基本篇)
生活随笔
收集整理的這篇文章主要介紹了
做小程序的流程总结(基本篇)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、首先當我們借助小程序實現我們的網站搭建時,就需要使用小程序自帶的一些功能;且需要根據該小程序獲取到的一些參數存儲到對應的數據庫中。
? ? ? ?openID:每個微信用戶使用該小程序時都會產生一個openID,且該openID是唯一標識,因此可將其存儲在用戶表中。
? ? ? ?token:這個token值是我們自己在內部生成的,主要是為了安全獲取UID,UID在數據庫中存儲中的是數據記錄的唯一ID,如果單純的傳入UID值來獲取
? ? ? ? ? ? ? ? ? ? 該UID的信息值的話,會不安全,因此需要通過token來獲取,如何獲取我會提供一系列代碼;
//隨意生成的TOKEN值public static function generateToken(){$randChar = getRandChar(32);$timestamp = $_SERVER['REQUEST_TIME_FLOAT'];$tokenSalt = config('secure.salt');return md5($randChar . $timestamp . $tokenSalt);} public static function verifyToken($token){$exist = Cache::get($token);if($exist){return true;}else{return false;}}//獲取以token為鍵值的部分,獲取對應的值public static function getCurrentTokenVar($key){$request=Request::instance();$token = input("token");if($token==""){$token=$request->header("token");}$vars = Cache::get($token);if (!$vars){throw new TokenException();}else {if(!is_array($vars)){$vars = json_decode($vars, true);}if (array_key_exists($key, $vars)) {return $vars[$key];}else{ // throw new Exception('嘗試獲取的Token變量并不存在');return false;}}} function __construct($code){$this->code=$code;$this->wxAppId=config("wx.app_id");$this->wxAppSecret=config("wx.app_secret");$this->wxLoginUrl=sprintf(config("wx.login_url"),$this->wxAppId,$this->wxAppSecret,$this->code);}/** 登錄*/public function get(){$result = curl_get($this->wxLoginUrl);// 注意json_decode的第一個參數true// 這將使字符串被轉化為數組而非對象$wxResult = json_decode($result, true);if (empty($wxResult)) {// 為什么以empty判斷是否錯誤,這是根據微信返回// 規則摸索出來的// 這種情況通常是由于傳入不合法的codethrow new Exception('獲取session_key及openID時異常,微信內部錯誤');}else {// 建議用明確的變量來表示是否成功// 微信服務器并不會將錯誤標記為400,無論成功還是失敗都標記成200// 這樣非常不好判斷,只能使用errcode是否存在來判斷$loginFail = array_key_exists('errcode', $wxResult);if ($loginFail) {$this->processLoginError($wxResult);}else {return $this->grantToken($wxResult);}}}//頒發令牌private function grantToken($wxResult){// 此處生成令牌使用的是TP5自帶的令牌// 如果想要更加安全可以考慮自己生成更復雜的令牌// 比如使用JWT并加入鹽,如果不加入鹽有一定的幾率偽造令牌// $token = Request::instance()->token('token', 'md5');$openid = $wxResult['openid'];$user = User::getByOpenID($openid);if (!$user)// 借助微信的openid作為用戶標識// 但在系統中的相關查詢還是使用自己的uid {$uid = $this->newUser($openid);$type=0;}else {$uid = $user->id;$type=$user->type;}$cachedValue = $this->prepareCachedValue($wxResult, $uid,$type);$token = $this->saveToCache($cachedValue);return $token;}private function processLoginError($wxResult){throw new WeChatException(['msg' => $wxResult['errmsg'],'errorCode' => $wxResult['errcode']]);}/**創建一個新用戶* @param $openid* @return mixed*/private function newUser($openid){// 有可能會有異常,如果沒有特別處理// 這里不需要try——catch// 全局異常處理會記錄日志// 并且這樣的異常屬于服務器異常// 也不應該定義BaseException返回到客戶端$user = User::create(['openid' => $openid]);return $user->id;}/**準備存入緩存的數據* @param $wxResult* @param $uid* @return mixed*/private function prepareCachedValue($wxResult, $uid,$type){$cachedValue = $wxResult;$cachedValue['uid'] = $uid;$cachedValue['type']=$type;$cachedValue['scope'] =config("secure.user");;return $cachedValue ;}/*** 存入緩存*/private function saveToCache($wxResult){$key = self::generateToken();$value = json_encode($wxResult);$expire_in = config('secure.time');$result = cache($key, $value, $expire_in);if (!$result){throw new TokenException(['msg' => '服務器緩存異常','errorCode' => 10005]);}return $key;}
二、當獲取到token時,需要在小程序的緩存中也存入該token值;
wx.setStorageSync('token', res.data.token);使用小程序請求數據時,將token的值存入Header中,后臺獲取到Token的值,會進行相應的處理,獲取到對應的UID
?
轉載于:https://www.cnblogs.com/baiyangLI/p/9300147.html
總結
以上是生活随笔為你收集整理的做小程序的流程总结(基本篇)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux 文本编辑器vi常用命令
- 下一篇: html星空代码在线,怎么操作html星