process Each request interface carries a token for verification key value: random number + timestamp + salt DemoMini Program<view> <button bindtap="loadTokenData">Carry token request data</button> </view> Page({ data: { }, onLoad:function(){ // this._loadData(); }, //Generate token setToken:function(callback){ // wx.setStorageSync('token', "sfspx64w8x47w14x3zX4x4wf4") var that = this; wx.request({ url: 'http://2021xcx-api.com/api/gettoken', method: 'POST', success: function(res){ console.log(res.data); var data = res.data; if(data.code==1){ //Update the newly acquired token value wx.setStorageSync('token', data.token); // Execute callback function callback&&callback() }else{ that.setToken(); } } }) }, loadToeknData:function(){ var that = this; wx.request({ url: 'http://2021xcx-api.com/api/orders', method: 'POST', header: { 'content-type': 'application/json', 'token': wx.getStorageSync('token') }, success: function(res){ var data = res.data; console.log(data) if(data.code==903){ // Token expires, request to set it again // Carry a callback function and continue to execute this method after token is re-acquired. that.setToken(that.loadTokenData) } } }) }, }) Backend interfaceRouting Configuration <?php Route::post("api/orders", "api/index/getOrders"); //Token Route::post("api/gettoken", "api/token/createToken"); Index.php <?php namespace app\api\controller; use app\api\controller\Token; use think\Cache; class Index extends Token { public function getOrders(Token $token){ $token->verifyToken(); $data['orders'] = [ 'id' => 1, 'title' => 'apple', 'time' => time() ]; echo json_encode($data); } } Token.php <?php namespace app\api\controller; use think\Controller; class Token extends Controller { protected $returnParam = [ 'code' => 1, 'msg' => 'Request failed' ]; /** * [verifyToken verifies whether the Token is carried and exists] * @return [type] [description] */ public function verifyToken(){ $token = request()->header()['token']; $isSetToken = cache($token); // dump($isSetToken); die; if(!$isSetToken){ $this->returnParam['code'] = 903; $this->returnParam['msg'] = "Token verification failed"; echo json_encode( $this->returnParam );die; } } /** * [createToken Token generation] * tip: record user ID/session_kye/openid in token * @return [type] [description] */ public function createToken() { $randStr = rand(1,9999); $time = time(); $sale = "xixi2021"; // * Simulate here--WeChat interface is not requested to obtain sessionkey and openid $tokenValue = [ 'uid' => 1, 'session_key' => '84848aasa', 'openid' => '20oxl65wc4d4s5x7hwc', 'code' => 'sssaaeee' ]; $tokenKey = md5($randStr.$time.$sale); //Cache token data cache($tokenKey, json_encode($tokenValue), 1); $returnParam = [ 'code' => 1, 'token' => $tokenKey ]; echo json_encode($returnParam); } } This is the end of this article about mini program token generation and verification. For more relevant mini program token generation and verification content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: About debugging CSS cross-browser style bugs
>>: Detailed use cases of MySql escape
This article shares the installation and configur...
Preface Linux groups are organizational units use...
Table of contents 1. Demand 2. Effect 3. All code...
The methods and concepts of private filters and g...
Preface At first, I wanted to use wget to downloa...
I recently used a Mac system and was preparing to...
This article shares the specific code for impleme...
<br />I am very happy to participate in this...
First, take a look at Alibaba Cloud's officia...
Differences between Docker and Docker Machine Doc...
environment Hostname ip address Serve Jenkins 192...
What is a directive? Both Angular and Vue have th...
Table of contents 1. Shopping cart example 2. Cod...
This article is based on the Free Code Camp Basic...
1. Problem Description For security reasons, the ...