###1.新建https.js(网络请求)
//封装GET请求无验证参数
function _get({
url,
data
}) {
//为了用户体验,加一个loading效果
wx.showLoading({
title: '加载中',
mask: true
});  
return new Promise((resolved, rejected) => {
const obj = {
url,
data,
method: 'GET',
success: (res) => res.statusCode === 200 ? resolved(res.data) : rejected(res.data),
fail: (err) => rejected(err),
    complete: () => {
wx.hideLoading();
}
}
wx.request(obj)
})
}
//封装GET请求有Authorization验证参数
function _getByHeader({
url,
data
}) {
//为了用户体验,加一个loading效果
wx.showLoading({
title: '加载中',
mask: true
});
return new Promise((resolved, rejected) => {
const obj = {
url,
data,
method: 'GET',
header: {
"Authorization": wx.getStorageSync('authorization')
},
success: (res) => res.statusCode === 200 ? resolved(res.data) : rejected(res.data),
fail: (err) => rejected(err),
complete: () => {
wx.hideLoading();
}
}
wx.request(obj)
})
}
//封装POST请求
function _post({
url,
data
}) {
//为了用户体验,加一个loading效果
wx.showLoading({
title: '加载中',
mask: true
});  
return new Promise((resolved, rejected) => {
const obj = {
url,
data,
method: 'POST',
header: {
'Content-Type': 'application/x-www-form-urlencoded',
"Authorization": wx.getStorageSync('authorization')
},
success: (res) => res.statusCode === 200 ? resolved(res.data) : rejected(res.data),
fail: (err) => rejected(err),
    complete: () => {
wx.hideLoading();
}
}
wx.request(obj)
})
}
//导出封装的_post方法
export default {
_post,
_get,
_getByHeader
}
###2.新建base.js(生产或者开发环境下的服务器地址)
let host = '';
let env = 'production';
if (env == 'development') {
// 开发环境
host = ''
} else if (env == 'production') {
// 生产环境
host = "https://sdyfy.ngrok.do-it.cn"
}
export default {
host
}
###3.新建api.js(封装接口请求)
import base from './base.js';
import https from './https.js';
const ajax = {
//添加就诊卡
addJzCard:(data)=>{
return https._post({
url: base.host + '/api/patient/addJzCard',
data
})
},
//获取默认就诊卡
getActiveCard: () => {
return https._getByHeader({
url: base.host + '/api/patient/activeCard'
})
},
//切换默认就诊卡
switchToJzCard: (data) => {
return https._post({
url: base.host + '/api/patient/switchToJzCard',
data
})
},
//获取默认就诊卡信息
getCardInfo: (data) => {
return https._getByHeader({
url: base.host + '/api/patient/jzCard',
data
})
},
//根据患者身份证号查询个人信息
getIdCardInfo:(data) =>{
return https._getByHeader({
url: base.host + '/api/patient/idCardNo',
data
})
},
//获取就诊卡列表
getCards: (data) => {
return https._getByHeader({
url: base.host + '/api/patient/cards'
})
}
}
export default ajax;
###4.在全局app.js文件中引用
import api from './utils/api.js'
App({
data: {
},
onLaunch: function() {
},
globalData: {
},
api
})
###5.页面中调用
getApp().api.getCardInfo({
jzCardNo: cardno
}).then((res) => {
if (res.state == 'ok') {
this.setData({
balance: res.obj.menzhenBalance,
})
}
}).catch((reject) => {
console.error('出啥错了?' + reject);
});