在外部app.js中
App({
globaldata: {
serverurl:'实际地址',
authToken:'token'
// serverurl: 'http://172.16.1.47:7001',
authCode: '',
},
// 封装网络请求
https(httpstype, url, data) {
dd.showLoading();
let endurl = encodeURI(getApp().globaldata.serverurl + url); //全局里边不能用this getApp()代表this
return new Promise((resolve, reject) => {
dd.httpRequest({
headers: {
"Content-Type": 'application/json;charset=utf-8',
"token": getApp().globalData.authToken
},
url: endurl,
method: httpstype,
data: data,
dataType: 'json',
success: (res) => {
resolve(res)
},
fail: (res) => {
reject(res)
},
complete: (res) => {
dd.hideLoading()
}
})
})
},
})
在index.js中引入
Page({
data: {},
onLoad() {
let app=getApp()
let dada= {pageNum: '1',pageSize:'99999',firstDept:app.globalData.firstDept}
dada=JSON.stringify(dada)
app.https('post','dingapi/pri/xttCRM/babyInfo/getfirstDept',dada)
.then((res)=>{console.log('对了',res)}
)
},
});