1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- // api接口请求封装
- var useApi = (url, data = {}, method = "get") => {
- var that = this;
- var requrestTask = wx.request
- wx.showLoading({
- title: '数据加载中...',
- })
- return new Promise((resolve, reject) => {
- //测试专用,非必要不要打开
- // data.user_id=738;
- // data.test="future";
- requrestTask({
- url: `${url}`, // 云端正式服
- data,
- method,
- header: {
- 'Authorization': 'Bearer ' + wx.getStorageSync('token') || '',
- 'content-type': 'application/json',
- },
- dataType: 'json', // 添加这个配置
- success(res) {
- wx.hideLoading({ //因为showToast、hideLoading不能同时使用
- fail() { }
- })
- resolve(res.data)
- },
- fail(err) {
- wx.hideLoading()
- wx.showToast({
- title: '加载失败',
- icon: 'none'
- })
- reject(err)
- },
- complete() {
- 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)
- },
- }
|