const apiPlus = {
// 可以在install方法里做扩展
install: function(Vue, options) {
//自定义指令 关闭Win窗口
Vue.directive('close',{
bind(el){
el.addEventListener('click',()=>{
_avMethods._avCloseWin(binding.value)
})
}
})
// 全局消息监听
Vue.prototype.sendEvent = function(name,data){
_avMethods._avSendEvent(name,data||{})
}
//时间戳格式化
Vue.prototype.initTime = function(d){
if(d){
let _defineTime = _avMethods._avTimeEncode(d);
return _defineTime;
}else{
return '时间戳格式不合法'
}
}
//封装ajax Get方法
Vue.prototype.$get = function(){
var avHeaders;
var avCallback;
if(typeof arguments[1] == 'object'){
avHeaders = arguments[1];
avCallback = arguments[2];
}else if(typeof arguments[1] == 'function'){
avCallback = arguments[1];
}
api.ajax({
url: this.indexUrl+arguments[0],
method: 'get',
headers:avHeaders
}, function(ret,err){avCallback.call(Vue,ret,err)});
};
//封装ajax POST方法
Vue.prototype.$post = function(){
var postHeader = postData = {};
var postCb;
if(typeof arguments[1] == typeof arguments[2]){
postHeader = arguments[1];
postData = arguments[2];
postCb = arguments[3];
}else{
postData = arguments[1];
postCb = arguments[2];
}
api.ajax({
url: this.indexUrl+arguments[0],
method: 'post',
data: {values: postData},
headers:postHeader,
},function(ret, err){
postCb.call(Vue,ret,err)
});
}
//显示loading...
Vue.prototype.showLoading = function(stu){
if(stu){
api.showProgress({
title: '努力加载中...',
text: '请稍候...',
modal: true
});
}else{
api.hideProgress();
}
}
Vue.prototype.avStorage = function(){
var isAndroid = (/android/gi).test(navigator.appVersion);
var ls = window.localStorage;
if(isAndroid){
ls = os.localStorage();
}
return ls;
};
// 生命周期、公共数据可以在这里写
Vue.mixin({
data(){
return{
//测试路由入口
indexUrl:'http://172.16.6.84:9949/',
//测试数据获取
avToken:9999999,
//自动获取窗口传参
Param:api.pageParam,
_avBase:{},
//自动获取窗口安全边距
safeArea:api.safeArea
}
},
methods:{
//Storage 存储数据
setStorage(key,value){
if(arguments.length === 2){
var v = value;
if(typeof v == 'object'){
v = JSON.stringify(v);
v = 'obj-'+ v;
}else{
v = 'str-'+ v;
}
var ls = this.avStorage();
if(ls){
ls.setItem(key, v);
}
}
},
//Storage 获取数据
getStorage(key){
var ls = this.avStorage();
if(ls){
var v = ls.getItem(key);
if(!v){return;}
if(v.indexOf('obj-') === 0){
v = v.slice(4);
return JSON.parse(v);
}else if(v.indexOf('str-') === 0){
return v.slice(4);
}
}
},
//Storage 移出数据
rmStorage(key){
var ls = this.avStorage();
if(ls && key){
ls.removeItem(key);
}
},
//Storage 清空数据
clrStorage(){
var ls = this.avStorage();
if(ls){
ls.clear();
}
},
},
created: function () {
//实例化的时候绑定监听
var listener = this.$options.listener;
for(item in listener){
if(typeof listener[item] !== 'function'){
console.error('the listener option is not a function!');
return;
}
_avMethods._avEvent(item,listener[item])
}
},
mounted:function(){
//移动端300ms延迟
FastClick.attach(document.body)
}
})
}
}
// 也可以使用Vue.use在Vue上直接扩展,看需要
aVue.use(apiPlus);