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
Block element p - paragraph pre - format text tabl...
Table of contents plan Install Dependencies Intro...
Software Download Download software link: https:/...
Since its launch in 2009, flex has been supported...
mysql gets all dates or months in a time period 1...
Inserting Data insert into table name (column nam...
[Problem description] On the application side, th...
Table of contents CSS3 Box Model a. CSS3 filter b...
Table of contents Native JS How to send a get req...
The isnull() function cannot be used as a substit...
1. Call the parent component method directly thro...
Error message: user: 'root' host: `localh...
This article uses examples to illustrate the synt...
Docker queries or obtains images in a private reg...
Syntax: ROW_NUMBER() OVER(PARTITION BY COLUMN ORD...