1. 常量模块化
(1)url.js中定义常量集合
var ip = "http://192.168.**.*8:7088/MVNFHM_war";
module.exports={
"get_ticket":ip+"/pmsAppWork/getTicket.do",
"get_process":ip+"/pmsAppWork/getPurchaseStyleProcessList.do",
"upload_work_info":ip+"/pmsAppWork/UploadWorkInfo.do",
"login":ip+"/pmsAppMy/UserLogin.do",
"getDepartList":ip+"/pmsAppWork/getDepartList.do"
}
(2)index.vue中调用常量
<script>
export default {
data() {
return {
urls: require("@/common/url.js"),
};
},
methods: {
login: function(e) {
var _this = this;
uni.request({
url: this.urls.login,
method: "POST",
header: {
"content-type": "application/x-www-form-urlencoded"
},
data: e.detail.value,
success(res) {
},
fail(res) {},
complete() {}
});
},
};
</script>
2. 固定方法模块化
(1)sign.js中定义方法
var md5 = require('./md5.js');
/**
* 签名函数:与服务器进行交互与md5加密
*/
module.exports = {
sign : function(apiServer){
// 环境判断非uni环境不支持(get、post模拟工具模拟不出来uni变量,实现了接口安全)
if(!uni){return '...';}
console.log('开始签名');
// 连接服务器获取一个临时的accessToken
uni.request({
url: apiServer+'getAccessToken',
method: 'GET',
success: res => {
console.log(res);
if(res.data.status != 'ok'){return ;}
var data = res.data.data;
// 对 accessToken 进行md5加密(token和当前时间戳组合后加密)
var accessToken = md5.hex_md5(data.token + data.time);
// 签名 = md5(accessToekn + time) + '-' + 'accessToekn';
var sign = accessToken + '-' + data.token;
//console.log(sign);
// 记录在本地
uni.setStorage({
key:"sign",
data:sign
});
console.log('签名完成');
},
fail:function(e){
console.log(JSON.stringify(e));
}
});
}
}
(2)index.vue中调用方法
<script>
var sign = require('../../commons/sign.js');
export default {
data() {
return {
};
},
methods:{
},
//options为登录验证时若需进行登录时传的参数
onLoad:function(options){
//console.log(options)
sign.sign(this.apiServer);//预签名:获取签名并保存在本地
}
}
</script>
备注
:apiServer为定义在main.js中的全局变量
//定义全局api接口地址和token
var APITOKEN = 'api2018'
Vue.prototype.apiServer = 'http://localhost/index.php?token=' + APITOKEN + '&c=';
网友评论