小程序异步请求

ES6构建基类

import { Config } from 'config.js'
class Base{
constructor(){
// this.baseRequestUrl = Config.restUrl;
this.baseRequestUrl = Config.restUrl;
}

request(params){
var url = this.baseRequestUrl + params.url;

if(!params.type){
params.type = 'GET';
}
wx.request({
url: url,
data: params.data,
method: params.type,
header: {
'content-type': params.header
// 'content-type': 'application/x-www-form-urlencoded' // 默认值
// 'content-type': 'application/json'
},
success: function(res){
params.sCallBack&¶ms.sCallBack(res.data)
},
fail: function(err){
console.log(err)
}
})
}
}

export {Base}


Config.js文件
class Config{
constructor(){

}
}
Config.restUrl = 'https://XXXXXXXXX';

export{Config}



app.js调用

wx.login({
success: function (loginRes) {
console.log( 'app.js页面微信登录')
console.log(loginRes)
console.log( 'code:' + loginRes.code)
if (loginRes.code) {

var js_code = loginRes.code; //调用登录接口获得的用户的登录凭证code

var params = {
url: 'yg_openId.aspx',
type: 'GET',
header: 'application/x-www-form-urlencoded',
data: {
code: loginRes.code,
Action: 'ActionLogin',
applet: '一个红扣'
},
sCallBack: function (res) {
console.log(res)
try {
wx.setStorageSync( 'openId', res.result)
} catch (e) {
}

}
}

base.request(params);

} else {
//console.log('获取用户登录态失败!' + res.errMsg)
}
},
fail: function (res) {
console.log( "微信登陆 fail")
},
complete: function (res) {
console.log( "微信登陆 complete")
}

})





你可能感兴趣的:(小程序异步请求)