123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- // api接口请求封装
- var useApi = (url, data = {}, method = "get") => {
- var that=this;
- var requrestTask=wx.request
- wx.showLoading({
- title: '数据加载中...',
- })
- return new Promise((resolve, reject) => {
- requrestTask ({
- url: `${url}`, // 云端正式服
- data,
- method,
- header: {
- 'Authorization': 'Bearer '+wx.getStorageSync('token') || '',
- 'content-type': 'application/json',
- },
- dataType: 'json', // 添加这个配置
- success(res) {
- resolve(res.data)
-
- },
- fail(err) {
- // wx.hideLoading()
- wx.showToast({
- title: '加载失败',
- icon: 'none'
- })
- reject(err)
- },
- complete() {
- wx.hideLoading(333)
- wx.hideLoading({ //因为showToast、hideLoading不能同时使用
- fail() {}
- })
- }
- }).onHeadersReceived(res=>{
- if(res && res.header && res.header.Authorization) {
- console.log("获取requestTask",res)
- wx.setStorageSync("token", res.header.Authorization);
- }
- })
-
- })
-
- }
-
-
- module.exports = {
- useApi,
- getBanner(data) {
- console.log(1);
- return useApi('', data)
-
- },
-
- }
|