tools
import thirdBrowser from '@/js/third_party/browser';
import thirdWX from '@/js/third_party/wx';
import alert from '@/js/alertView';
import constant from '@/js/const';
var thirdObj;
// 返回在vue模板中的调用接口
export default {
//--------------------工具型-------------------------
//去掉左右空格
trimStr:function(str) {
return str.replace(/(^\s+)|(\s+$)/g, "");
},
//是否是数字
isNumber(value) {
var patrn = /^(-)?\d+(\.\d+)?$/;
if (patrn.exec(value) == null || value == "" || value == "undefined" || value == null) {
return false;
} else {
return true;
}
},
//判断字符是否为空的方法
isEmpty(obj){
if(typeof obj == "undefined" || obj == null || obj == ""){
return true;
}else{
return false;
}
},
//检测图片大小
formatUpload:function(file){
if(file.type.split("/")[0] != "image"){
alert.eduToast('文件格式错误,请重新上传~',2000);
return false;
}
if(file.size>3072000){
alert.eduToast('文件太大了, 不允许超过3M~',2000);
return false;
}
return true;
},
//获取参数
getParam:function(name){
var search = decodeURIComponent(document.location.search);
var pattern = new RegExp("[?&]" + name + "=([^&]+)", "g");
var matcher = pattern.exec(search);
var items = null;
if (null != matcher) {
try {
items = decodeURIComponent(decodeURIComponent(matcher[1]));
} catch (e) {
try {
items = decodeURIComponent(matcher[1]);
} catch (e) {
items = matcher[1];
}
}
}
return items;
},
//是否是微信环境
is_weixin(){
var ua = navigator.userAgent.toLowerCase();
if(ua.match(/MicroMessenger/i)=="micromessenger") {
return true;
} else {
return false;
}
},
//--------------------需求型-------------------------
loadEnv:function(onReady, isLogin,callback){
var self = this;
if (self.is_weixin()) {
//微信相关
thirdObj = thirdWX;
}else{
//走浏览器。
thirdObj = thirdBrowser;
}
thirdObj.init(onReady, isLogin,callback);
},
//登陆事件
loginEvent:function(callback){
console.log("登录事件tools");
thirdObj.loginEvent(callback);
},
//获取图片验证码
getBindImageVerify:function(callback){
thirdObj.getBindImageVerify(callback);
},
//获取验证码
clickBindVerifica(bindInfo,callback){
console.log("获取验证码tools");
thirdObj.clickBindVerifica(bindInfo,callback);
},
//验证登陆按钮
executeNextEvent(bindInfo,loginOrRegist,callback){
thirdObj.executeNextEvent(bindInfo,loginOrRegist,callback);
},
//获取主打色(示例)
mainColor:function() {
thirdObj.mainColor();
},
//获取商城地址
getAddressCode(){
//获取商城地址
var self = this;
if(self.getParam('addressCode')){
return self.getParam('addressCode');
}else if(!self.isEmpty(localStorage.getItem('addressCode'))){
return localStorage.getItem('addressCode');
}else{
return constant.addressCode();
}
},
//防止iphone手机,在点击input框,失去焦点后,输入法把内容顶起来
checkWxScroll(){
var currentPosition,timer;
var speed=1;//页面滚动距离
timer=setInterval(function(){
currentPosition=document.documentElement.scrollTop || document.body.scrollTop;
currentPosition-=speed;
window.scrollTo(0,0);//页面向上滚动
currentPosition+=speed; //speed变量
window.scrollTo(0,currentPosition);//页面向下滚动
console.log('我滚动了')
clearInterval(timer);
},1);
},
/**
* 拼接图片路径
* imageFile:image路径
* size:要多大尺寸
* cmId:商城的id(如果是0定制这个参数不传)
* customId:定制信息id用于详情页修改定制信息后图片缓存问题
* */
joinImageUrl(imageFile,size,cmId,customId){
var imageFileBase = imageFile.split("tzd")[0];
var imageFilePath = imageFile.split("tzd")[1];
var imageFileName = imageFilePath.split(".")[0];
var imageFileSurfix = imageFilePath.split(".")[1];
var sizeStr = '';
var url;
//如果有尺寸要求的拼接,没有要求传0即可
if(size > 0){
sizeStr = "_" + size;
}
if(cmId){
url = imageFileBase + "cm/" + cmId + imageFileName + sizeStr + "." + imageFileSurfix;
if(customId){
url = url + '?customId=' + customId;
}
}else{
url = imageFileBase + 'tzd' + imageFileName + sizeStr + "." + imageFileSurfix;
}
return url;
},
//所有中文都算2个字符
getByteForUTF(s){
var a,c;
a=s.replace(/[\u0000-\u007f]/g, "\u0061");
// b=a.replace(/[\u0080-\u07ff]/g, "\u0061\u0061");
// c=b.replace(/[\u0800-\uffff]/g, "\u0061\u0061\u0061");
c=a.replace(/[\u0800-\uffff]/g, "\u0061\u0061");
return c.length;
},
/**
* 跳转页面的方法 采用query传参
* 有三个参数:vueObject、name、query
* vueObject:vue实例
* name:是路由name名称(字符串)//必须的参数
* query:是要传的参数,是一个对象 例如:{property1:value1,property2:value2,...}
* */
goToUrl(vueObject,name,query){
if(query){
if(query.addressCode){
vueObject.$router.push({name:name,query:query});
}else{
query.addressCode = this.getAddressCode();
vueObject.$router.push({name:name,query:query});
}
}else{
var queryObj = {};
queryObj.addressCode = this.getAddressCode();
vueObject.$router.push({name:name,query:queryObj});
}
}