ajax封装
const ajax = (url,method,data)=>{
return new Promise((resolve,reject)=>{
// 显示loading
wx.showLoading({
title: '加载中',
})
wx.request({
url: url, //仅为示例,并非真实的接口地址
method:method,
data: data,
header: {
'content-type': 'application/json' // 默认值
},
success (res) {
resolve(res.data)
// 关闭loading
wx.hideLoading()
}
})
})
}
export default ajax
//配置公共域名,导出方法,方便页面中引用
import ajax from '../utils/request.js'
// 公共的基础域名地址
const baseUrl= 'https://mpapi.iynn.cn/api/v1';
//所有的请求集合
// banner图列表
export function getBanner(){
return ajax(baseUrl+'/news/hot','get')
}
//新闻列表
export function getNewsList(params){
return ajax(baseUrl+'/news','get',params)
}
缓存
<!-- 设置缓存 -->
<button type="primary" bindtap="setCache">设置</button>
<!-- 获取缓存 -->
<button type="primary" bindtap="getCache">获取</button>
setCache: function () {
// 同步存储
wx.setStorageSync('key', 'value')
// 异步存储
wx.setStorage({
key:"key",
data:"value"
})
},
getCache: function () {
// 同步获取
var value = wx.getStorageSync('key')
// 异步获取
wx.getStorage({
key: 'key',
success (res) {
console.log(res.data)
}
})
},
储存形式: