Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize local data storage by WeChat Mini Programs

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

这篇"微信小程序怎么实现本地数据存储"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇"微信小程序怎么实现本地数据存储"文章吧。

微信小程序 本地数据存储实例详解

前言

如果您在看此文章之前有过其他程序的开发经验,那一定会知道一般例如安卓或者苹果的原生APP都提供了本地的存储功能,甚至可以使用sqlite数据库来做存储。可是微信的小程序框架基于微信本身,其实际运行环境只是在浏览器里面,所以不会提供那么丰富的数据存储实力。但html5开始已经可以在浏览器里面存储数据,好在微信的小程序给这个功能封装好了,这样我们可以使用数据存储。

每个微信小程序都可以有自己的本地缓存,可以通过 wx.setStorage(wx.setStorageSync)

wx.getStorage(wx.getStorageSync)、 wx.clearStorage(wx.clearStorageSync)

可以对本地缓存进行设置、获取和清理。本地缓存最大为10MB。

上面的set和get都有对应的Sync方法,带Sync的方法为同步方法、不带Sync的方法为异步方法。

设置缓存都需要设置一个key和对应的data值,我们在《微信web开发者工具》中的调试状态下可以点击调试窗口的Storage 栏来查看我们缓存在本地的数据。

缓存可以保存数组、数值、字符串、对象。

设置缓存

提供setStorage和setStorageSync两个接口,并且在使用设置存储方法时,如果小程序的存储值当中已经存在对应的key的值,那么会使用新的值替换原来的值。

setSotrage接口

wx.setStorage({ key:"key", data:"value", success:function(res){console.log(res)}, fail:function(res){console.log(res)}, complete:function(res){console.log(res)},})//Object {errMsg: "setStorage:ok"}//Object {errMsg: "setStorage:ok"}

setStorageSync接口

因为该方法为同步接口,所以直接设置key和data:

wx.setStorageSync('key', 'value')

上面两个demo中我们都使用了字符串缓存,当然我们也可以缓存一个对象,例如:

wx.setStorage({key:"ob",data:{name:'smallerpig',sex:1,age:18}})

获取缓存

getSotrage接口

异步接口,所以我们可以定义几个回调:

wx.getStorage({ key:'key', success:function(res){ console.log(res)//Object {errMsg: "getStorage:ok", data: "value1"} }, fail:function(res){console.log(res)}, complete:function(res){console.log(res)}})

其中,我们可以看出来,微信小程序的很多异步接口的回调都会给出三个回调:success、fail、complete,在执行成功的时候回执行success、complete回调;在执行失败之后会分别执行fail、complete回调。

getSotrageSync接口

该接口为同步接口,所以只需传递对应的key值就可以了。如下列代码:

wx.getStorageSync('ob')//Object {name: "smallerpig", sex: 1, age: 18}

获取当前存储总结

使用wx.getStorageInfo接口

例如下列代码:

wx.getStorageInfo({ success: function(res) { console.log(res.keys) console.log(res.currentSize) console.log(res.limitSize) }//["logs", "r3session", "key"]0: "logs"1: "r3session"2: "key"]//1//10240})

也可以使用同步接口wx.getStorageInfoSync。

缓存的使用

在上一篇文章中,小猪介绍了如何解密通过wx.getUserInfo接口获取到的cryptedData数据。其中的session_key小猪是写在flask的缓存中,缓存的键是写死为:xcx_session_key。在真实环境中我们不能够这样写,因为这样不同的用户获取到的session_key是相同的,所以我们需要给不同的用户附上不同的缓存key。把flask的缓存key返回给微信小程序,小程序中可以固定一个key值,data值保持flask后台给到的值。

说的比较绕,这里需要读者好好理解下。

在flask的代码的使用code获取session_key中,使用下列代码:

@app.route('/user/getuserinfo', methods=['GET', 'POST'])def getuserinfo(): code = request.data url = 'https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code' % (appid, secret, code) r = requests.get(url) j = json.loads(r.text) r3session_key = binascii.hexlify(os.urandom(64)) cache.set(r3session_key, j['session_key']) return r3session_key

接下来,在wx.request的回调中把flask中返回的r3session_key保存起来:

wx.request({ url: 'https://***.smallerpig.com/user/getuserinfo', data: r.code, method: 'POST', success: function(res){ wx.setStorageSync('r3session', res.data) }})

再接下来,在调用wx.getUserInfo时将返回的数据加上从微信小程序本地缓存中取到的r3session丢给flask来处理,flask根据来的r3session从本地服务器的flask缓存中取到微信的值解密cryptedData。这才是一个完整的过程。

小程序的getUserInfo代码:

wx.getUserInfo({ success: function (res) { that.globalData.userInfo = res.userInfo typeof cb == "function" && cb(that.globalData.userInfo) //将本地存储中的r3session值也同样传递到我的服务器,在服务器中找出微信给到我们的session_key var r3session = wx.getStorageSync('r3session') res.r3session = r3session wx.request({ url: 'https://***.smallerpig.com/user/getuserunionid', data: res, success: function(res){ // success console.log(res) }, }) }})

对应的flask代码:

@app.route('/user/getuserunionid', methods=['GET', 'POST'])def getuserid(): r = json.loads(request.data) encryptedData = r['encryptedData'] iv = r['iv'] xcx_session_key = r['r3session'] session_key = cache.get(xcx_session_key) # 从缓存中取出对应r3session对应的session_key pc = WXBizDataCrypt(appid, session_key) return pc.decrypt(encryptedData, iv)以上就是关于"微信小程序怎么实现本地数据存储"这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注行业资讯频道。

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report