
App({
onLaunch: function () {
let App = this
// 设置api地址
App.setApiRoot()
},
globalData: {
userInfo: null
},
api_root: '', // api地址
appid:'',
/**
* 显示失败提示框
*/
showError(msg, callback) {
wx.showModal({
title: '友情提示',
content: msg,
showCancel: false,
success(res) {
callback && callback()
}
})
},
/**
* 设置api地址
*/
setApiRoot() {
let App = this
// App.api_root = config.config.host
let extConfig = wx.getExtConfigSync? wx.getExtConfigSync(): {}
console.log(extConfig)
App.appid = extConfig.attr.appid
App.api_root = extConfig.attr.host
},
/**
* get请求
*/
_get(url, data, success, fail, complete, check_login) {
let App = this
wx.showNavigationBarLoading()
// 构造请求参数
data = Object.assign({
token: wx.getStorageSync('token'),
appid:App.appid
}, data)
// if (typeof check_login === 'undefined')
// check_login = true
console.log(App.api_root)
// 构造get请求
let request = () => {
data.token = wx.getStorageSync('token')
wx.request({
url: App.api_root + url,
header: {
'content-type': 'application/json'
},
data,
success(res) {
if (res.statusCode !== 200 || typeof res.data !== 'object') {
console.log(res)
App.showError('网络请求出错')
return false
}
if (res.data.code === -1) {
// 登录态失效, 重新登录
wx.hideNavigationBarLoading()
App.doLogin(() => {
App._get(url, data, success, fail)
})
} else if (res.data.code === 0) {
App.showError(res.data.msg)
return false
} else {
success && success(res.data)
}
},
fail(res) {
// console.log(res)
App.showError(res.errMsg, () => {
fail && fail(res)
})
},
complete(res) {
wx.hideNavigationBarLoading()
complete && complete(res)
},
})
}
// 判断是否需要验证登录
check_login ? App.doLogin(request) : request()
},
/**
* post提交
*/
_post_form(url, data, success, fail, complete) {
wx.showNavigationBarLoading()
let App = this
// 构造请求参数
data = Object.assign({
token: wx.getStorageSync('token'),
appid:App.appid
}, data)
data.token = wx.getStorageSync('token')
wx.request({
url: App.api_root + url,
header: {
'content-type': 'application/x-www-form-urlencoded',
},
method: 'POST',
data,
success(res) {
if (res.statusCode !== 200 || typeof res.data !== 'object') {
App.showError('网络请求出错')
return false
}
if (res.data.code === -1) {
// 登录态失效, 重新登录
App.doLogin(() => {
App._post_form(url, data, success, fail)
})
return false
} else if (res.data.code === 0) {
App.showError(res.data.msg, () => {
fail && fail(res)
})
return false
}
success && success(res.data)
},
fail(res) {
// console.log(res)
App.showError(res.errMsg, () => {
fail && fail(res)
})
},
complete(res) {
wx.hideLoading()
wx.hideNavigationBarLoading()
complete && complete(res)
}
})
},
/**
* 验证登录
*/
checkIsLogin() {
return wx.getStorageSync('token') != ''
},
/**
* 授权登录
*/
doLogin(callback) {
let App = this
// if (e.detail.errMsg !== 'getUserInfo:ok') {
// return false
// }
wx.showLoading({
title: "加载数据中...",
mask: true
})
// 执行微信登录
wx.login({
success(res) {
// 发送用户信息
App._post_form('login', {
code: res.code,
}, result => {
// 记录token user_id
wx.setStorageSync('token', result.data.token,)
// 执行回调函数
callback && callback()
}, false, () => {
wx.hideLoading()
})
}
})
}
})
本地存储分为异步和同步。
设置:
异步:wx.setStorage
同步:wx.setStorageSync
获取:
异步:wx.getStorage
同步:wx.getStorageSync
移除:
异步:wx.removeStorage
同步:wx.removeStorageSync
清除所有:
异步:wx.clearStorage
同步:wx.clearStorageSync
这里给大家以同步为例,
wx.setStorageSync('key','value')
接下来,我们说一下本地收藏功能怎么实现的呢
这是一个列表渲染页面,每一个view点进去的详情页面其实是一个页面,只是传不同的id,来赋值不同的数据展示出来而已。
通过JS文件中带参数的跳转,把相应的数据传到详情页中,那接下来就是展示详情页
您好!很高兴能为您解答, 在微信小程序中,数据缓存其实就和localstorage 的原理差不多,所以理解起来并不难。下面我们来一起实现一下。效果图展示:
我们在index页面存入数字11,然后在跳转到新页面,在将缓存中的11取出渲染到当前页面。具体代码如下:
index页面:
<span style="font-size:24px">
<view class="btn-area">
<navigator url="../navigator/navigator?title=我是navi">跳转到新的页面post情求</navigator>
<navigator url="../redirect/redirect?title=我是red" redirect>跳转到当前页面</navigator>
</view>
</span>
<view>
<input style="border:2rpx solid red" placeholder="输入信息" bindinput="getInput" />
<button style="border:2rpx solid yellow" bindtap="saveInput">存入</button>
</view>1234567891012345678910
index的js:
//index.js
//获取应用实例
var app = getApp()
Page({
data: {
storage:''
},
onLoad: function () {
var that = this
//获取输入值
getInput:function(e){
this.setData({
storage:e.detail.value
})
},
//存储输入值
saveInput:function(){
wx.setStorageSync('storage', this.data.storage)
}
})
12345678910111213141516171819202122231234567891011121314151617181920212223
跳转页面:
<view>从存储中得到的数据:{{storage}}</view>11
跳转页面的js:
var app = getApp()
var that
Page( {
data: {
storage:''
},
onLoad: function(options) {
that = this
//获取存储信息
wx.getStorage({
key: 'storage',
success: function(res){
// success
that.setData({
storage:res.data
})
}
})
}
})
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)