封装接口
项目/utils/api. js
//将请求进行Promise封装
const fetch = ({url, data}) => {
//打印接口请求的信息
console. log( [step 1] API 接口: ${url});
console. log(" [step 2] data 传参:");
console. log(data);
// 返回 Promise
return new Promise((resolve, reject) => {
wx.request({
url: getApp(). globalData, api + url,
data: data,
success: res => {
//成功时的处理
第195页
console. log(" [step 3]请求成功:");
console. log (res. data);
return resolve (res. data);
} else {
wx. showModal({
title:请求失败’,
content: res. data, message,
showCancel: false
});
}
},
fail: err => {
//失败时的处理
console. log(err);
return reject(err);
}
})
})
}
/**
*code 换取 openId
*@data {
*jsCode – wx. login()返回的 code
*}
*/
export const wxLogin = data => {
return fetch({
url: "tbcUser/getWechatOpenld",
data: data
})
}
调用接口
项目/pages/login/login.js
import {
wxLogin,
} from ‘../../utils/api. js’
使用接口
项目/pages/login/login. js
wxLogin({
jsCode: this. data. code
}). then(
res => {
console. log(" [step 4]返回成功处理:");
console. log(res. data);
},
err => {
console. log(" [step 4]返回失败处理:");
console. log(err);
}
)

Was this helpful?

0 / 0

发表回复 0

Your email address will not be published.