request.js
import {
config
} from './config.js'
export const apiResquest = (prams) => { //prams 为我们需要调用的接口API的参数 下面会贴具体代码
return new Promise((resolve, reject) => {
let url = config.base_url + prams.url; //请求的网络地址和局地的api地址组合
let type = prams.method;
let query = prams.query;
let AuthorizationToken = uni.getStorageSync('token') ||''
let headerData = {query,AuthorizationToken};
uni.showLoading({
title: '加载中',
mask: true
})
console.log(111)
uni.request({
method: type,
url: url,
data:query,
header: headerData,
dataType: 'json',
}).then((response) => {
console.log(response)
setTimeout(function() {
uni.hideLoading();
}, 200);
let [error, res] = response;
resolve(res.data);
}).catch(error => {
let [err, res] = error;
reject(err)
})
});
}
sestting.js
import { apiResquest } from '../../request.js'
export const userUpdate = (query) => {
return apiResquest({
url: '/user/api/user/update',
method: 'POST',
query: {...query}
})
}
methods:
userUpdate() {
const query = {
id:this.userInfo.id,
headImg: this.userInfo.headUrl,
nickname:this.userInfo.realName
}
console.log(query)
userUpdate(query).then((res) => {
console.log(res)
let code = res.code
if (code == 0) {
uni.showToast({
title: '修改成功',
duration: 2000,
icon: 'success'
})
} else {
let msg = res.msg
uni.showToast({
title: msg,
duration: 2000,
icon: 'none'
})
}
}).catch(err => {
console.log(err)
})
},