// 这里的vm，就是我们在vue文件里面的this，所以我们能在这里获取vuex的变量，比如存放在里面的token
// 同时，我们也可以在此使用getApp().globalData，如果你把token放在getApp().globalData的话，也是可以使用的

const install = (Vue, vm) => {
	Vue.prototype.current_url=function(){
		   let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
		   let curRoute = routes[routes.length - 1].route //获取当前页面路由
		   let curParam = routes[routes.length - 1].options; //获取路由参数
		   // 拼接参数
		   let param = ''
		   for (let key in curParam) {
		       param += '&' + key + '=' + curParam[key]
		   }
		   if (param==''){
			   return "/"+curRoute;
		   }else{
			   return "/"+curRoute+"?"+param
		   }
	}
	
	Vue.prototype.check_ysmsg = function() {
		//console.log(store.state.webset.msg.msgys,222)
		return new Promise((resolve, reject) => {
			uni.requestSubscribeMessage({
				tmplIds: store.state.set.webset.xcx_msgs,
				success(res) {
					//发送订阅消息
					Reflect.ownKeys(res).forEach(function(key) {
						if (res[key] == "accept") {
							resolve(1)
						}
					});
					resolve(0)
	
				}
			})
		})
	
	}
  	Vue.prototype.msg=function(title,duration=2000){
		uni.showToast({
				title:title,
				duration: duration,
					icon:"none"	
		})
	}
	Vue.prototype.jump=function(url){
		uni.navigateTo({
			url:url
		})
	}
	Vue.prototype.phone=function(phone){
		uni.makePhoneCall({
		    phoneNumber: phone //仅为示例
		});
	}
	Vue.prototype.copy=function(str){
		uni.setClipboardData({
			data: str,
			success: function () {
				vm.msg("复制成功")
			}
		});
	}
	Vue.prototype.jumpbar=function(url){
		uni.switchTab({
		    url: url
		});
	}
   Vue.prototype.arr=function(str){
     let temp= str.split(',');
     var that=this;
   	temp.forEach(function(val, index, arr){
   	      temp[index]=vm.xcx_url(temp[index])
   });
   	 
   	 return temp;
   }
   Vue.prototype.xcx_url=function(url){
	   
	  if (url.indexOf("http") != -1){
		  
		   return url;
	  }
	   else
	   return vm.rooturl+"/"+url;;
   }
	Vue.prototype.previewImage = function(urls,current) {
	
		uni.previewImage({
			urls: urls,
			current:current,
			longPressActions: {
				success: function(data) {
					//console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
				},
				fail: function(err) {
					console.log(err.errMsg);
				}
			}
		});
	}

}

export default {
	install
}
