wx-app/utils/api.js
2019-06-27 20:32:07 +08:00

113 lines
2.8 KiB
JavaScript

const API_URL = 'http://127.0.0.1:8000';
const getOpenId = () => {
return getApp().globalData.openId;
}
const requestApi = (api, data = {}, method = "GET", showError) => {
return new Promise((resolve, reject) => {
if (typeof(showError) == "undefined") {
showError = true;
}
if (!data['open_id']) {
data['open_id'] = getOpenId();
}
wx.showLoading({
title: '请求数据中...',
});
// let headers = {};
// if(method.toLocaleLowerCase() == 'post'){
// headers['content-type']
// }
wx.request({
url: API_URL + api,
data,
header: {
'content-type': 'application/x-www-form-urlencoded' // 默认值
},
method,
dataType: 'json',
responseType: 'text',
success: function(res) {
wx.hideLoading();
if (res.statusCode != 200) {
reject(res);
} else {
if (res.data.code && res.data.code != 0) {
if (showError) {
wx.showModal({
title: res.data.message,
showCancel: false
})
} else {
reject(res);
}
} else {
resolve(res.data);
}
}
},
fail: function(res) {
wx.hideLoading();
reject(res);
}
});
});
}
/**
* 使用用户登录code获取用户session及open_id
*/
export function userLogin(code) {
return requestApi('/user/login', {
code
});
}
/**
* 查询用户基本信息
*/
export function userInfo(openid, showError) {
return requestApi('/user/info', {
open_id: openid
}, 'GET', showError);
}
export function createUserInfo(data) {
return requestApi('/user/create', data, 'POST', false);
}
/**
* 查询用户详细信息
*/
export function userDetail() {
return requestApi('/user/detail');
}
/**
* 更新用户详细信息
*/
export function updateUserDetail(data) {
return requestApi('/user/update', data, 'POST');
}
/**
* 查询用户所有的评估数据
*/
export function queryAllEvaluation() {
return requestApi('/evaluation/all');
}
export function queryEvaluationById(id) {
return requestApi('/evaluation/detail', {
id
});
}
/**
* 创建用户评估数据
*/
export function createEvaluation(data) {
return requestApi('/evaluation/create', data, 'POST');
}
export default {
userInfo,
updateUserDetail,
createUserInfo,
userDetail,
queryAllEvaluation,
createEvaluation,
userLogin
}