微信小程序中怎么實(shí)現(xiàn)本地數(shù)據(jù)緩存功能

本篇內(nèi)容主要講解“微信小程序中怎么實(shí)現(xiàn)本地數(shù)據(jù)緩存功能”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“微信小程序中怎么實(shí)現(xiàn)本地數(shù)據(jù)緩存功能”吧!

創(chuàng)新互聯(lián)憑借在網(wǎng)站建設(shè)、網(wǎng)站推廣領(lǐng)域領(lǐng)先的技術(shù)能力和多年的行業(yè)經(jīng)驗(yàn),為客戶提供超值的營銷型網(wǎng)站建設(shè)服務(wù),我們始終認(rèn)為:好的營銷型網(wǎng)站就是好的業(yè)務(wù)員。我們已成功為企業(yè)單位、個人等客戶提供了成都網(wǎng)站制作、成都網(wǎng)站設(shè)計、外貿(mào)營銷網(wǎng)站建設(shè)服務(wù),以良好的商業(yè)信譽(yù),完善的服務(wù)及深厚的技術(shù)力量處于同行領(lǐng)先地位。

【將數(shù)據(jù)存儲在本地緩存】wx.setStorage

【讀取本地緩存】wx.getStorage

以手機(jī)號+密碼登錄為例,把登錄成功返回的token值存儲在本地緩存中,然后讀取緩存中的token:

login.php:

<?php 
  header("Content-type:text/html;charset=utf-8");
  $arr = array("state"=>0,"data"=>array(),"msg"=>'');
  $phone = $_POST['phone'];
  $password = $_POST['password'];
  if($phone && $password){
	//省略驗(yàn)證......

	//返回登錄token
	$tokenstr = 'liweishan666';
	$token = $phone.time().$tokenstr;//省略加密

	$arr['state'] = 1;
	$arr['msg'] = '登錄成功';
	$arr['data']['token'] = $token;
  }else{
	$arr['msg'] = '參數(shù)錯誤';
  }
  echo json_encode($arr);
  die;	

login.wxml:

<form bindsubmit="formSubmit" bindreset="formReset">
 <view>
  手機(jī)號:<input type="text" name="phone" placeholder="請輸入賬號" confirm-type="done" />
  密碼:<input password type="number" name="password" placeholder="請輸入6位密碼" maxlength="6" />
 </view>
 <view class="btn-area">
  <button formType="submit">登錄</button>
 </view>

 <view class="btn-area">
  <button bindtap="gettoken">讀取緩存token</button>
 </view>

 <view class="btn-area">{{token}}</view>
</form>

login.js:

Page({
 formSubmit: function (e) {
  wx.request({
   url: 'https://www.msllws.top/login.php',
   data: {
    'phone': e.detail.value.phone,
    'password': e.detail.value.password
   },
   method: 'POST',
   header: {
    'Content-Type': 'application/x-www-form-urlencoded'
   },
   success: function (res) {
    console.log(res.data);
    //以鍵值對的形式存儲到本地緩存
    wx.setStorage({
     key: "token",
     data: res.data.data.token
    })
   },
   fail: function () { },
   complete: function () { }
  })
 },

 gettoken: function (e) {
  var that = this
  wx.getStorage({
   key: 'token',
   success: function (res) {
    that.setData({'token': res.data})
   },
   fail: function () { },
   complete: function () { }
  })
 }
})

實(shí)現(xiàn)緩存的存儲和讀取:

微信小程序中怎么實(shí)現(xiàn)本地數(shù)據(jù)緩存功能

【從緩存中移除指定數(shù)據(jù)】wx.removeStorage

wx.removeStorage({
 key: 'token',
 success (res) {
  console.log(res.data)
 } 
})

 【清除全部緩存數(shù)據(jù)】wx.clearStorage

wx.clearStorage()

到此,相信大家對“微信小程序中怎么實(shí)現(xiàn)本地數(shù)據(jù)緩存功能”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

網(wǎng)站標(biāo)題:微信小程序中怎么實(shí)現(xiàn)本地數(shù)據(jù)緩存功能
標(biāo)題URL:http://bm7419.com/article34/jjscpe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計、面包屑導(dǎo)航、動態(tài)網(wǎng)站、網(wǎng)站營銷、網(wǎng)站設(shè)計公司、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作