1 Star 0 Fork 3

zped/YYShop

forked from 苦笑/YYShop 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
common.js 8.76 KB
一键复制 编辑 原始数据 按行查看 历史
苦笑 提交于 2020-07-06 23:02 . 一大波更新
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
import Vue from 'vue'
import env from './.env.js'
Vue.prototype.isDev = function() {
if (process.env.NODE_ENV === 'development') return true;
return false;
}
Vue.prototype.getEnv = function(key, defaultValue = '') {
if (Vue.prototype.isDev()) {
let config = env.dev[key];
if (!config) return defaultValue;
return config;
}
let config = env.pro[key];
if (!config) return defaultValue;
return config;
}
Vue.prototype.getBaseUrl = function() {
return Vue.prototype.getEnv('host');
}
Vue.prototype.httpCommon = function(param) {
let baseUrl = Vue.prototype.getBaseUrl();
if (param.loading === true) uni.showLoading({
mask: true,
title: "加载中..."
})
param.url = baseUrl + param.url;
// //登录标识
param.header = {
token: Vue.prototype.localGet('token')
};
return new Promise((resolve, reject) => {
param.success = function(res) {
if (param.loading === true) uni.hideLoading()
// console.log(res);
if(res.data.code>=10&&res.data.code<=30){
Vue.prototype.localRemove('token')
uni.navigateTo({
url:'/pages/login/login'
})
return reject(res);
}else if(res.data.code>=31&&res.data.code<=40){
getApp().$vm.getSession(true);
}
resolve(res);
}
param.fail = function(res) {
if (param.loading === true) uni.hideLoading()
reject(res);
}
uni.request(param)
});
}
Vue.prototype.httpGet = function(param) {
param.method = 'GET';
return new Promise((resolve, reject) => {
Vue.prototype.httpCommon(param).then((re) => {
resolve(re.data);
}).catch((re) => {
reject(re);
});
});
}
Vue.prototype.httpPost = function(param) {
param.method = 'POST';
return new Promise((resolve, reject) => {
Vue.prototype.httpCommon(param).then((re) => {
resolve(re.data);
}).catch((re) => {
reject(re);
});
});
}
Vue.prototype.getImgPrefix = function() {
return Vue.prototype.getEnv('host') + "/uploads/";
}
Vue.prototype.localSave = function(key, value, expire = 0, prefix = 'weapp_') {
let expire_time = 0;
if (expire !== 0) expire_time = (Date.parse(new Date()) / 1000) + expire;
uni.setStorageSync(prefix + key, {
value: value,
expire: expire_time
})
}
Vue.prototype.localGet = function(key, defaultValue = '', prefix = 'weapp_') {
// console.log(prefix + key);
let v = uni.getStorageSync(prefix + key)
// console.log(v);
if (v === '') return defaultValue
let expire_time = v.expire;
if (expire_time == 0) return v.value;
let now = Date.parse(new Date()) / 1000;
if (now > expire_time) {
Vue.prototype.localRemove(key)
return defaultValue;
}
return v.value;
}
Vue.prototype.localRemove = function(key, prefix = 'weapp_') {
uni.removeStorageSync(prefix + key)
}
Vue.prototype.getObj = function(obj, attr, defaultValue = '') {
try {
// console.log(obj);
// return false;
let tempObj = Vue.prototype.cloneObj(obj)
// console.log(tempObj);
// return false;
let arr = attr.split('.');
// console.log(arr);
for (let i in arr) {
let isHave= tempObj.hasOwnProperty(arr[i]);
if(isHave){
tempObj = tempObj[arr[i]]
}else{
return defaultValue;
}
// console.log(tempObj);
}
return tempObj;
} catch (e) {
// console.log(e);
return defaultValue;
}
}
/**
* rule={
phone:{rule:'required',msg:'手机号未授权'}
};
*
* @param {Object} formItem
* @param {Object} rule
*/
Vue.prototype.checkForm = function(formItem, rule) {
for (let i in rule) {
let rule_check = rule[i]['rule'];
let msg = rule[i]['msg'];
let item = formItem[i];
// if(rule_check)
switch (rule_check) {
case "required":
if (item == '') return {
result: false,
msg: msg
}
break;
case "email":
let reg = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
if (!reg.test(item)) return {
result: false,
msg: msg
}
break;
}
}
return {
result: true
}
}
/**
let rule={
phone:[{rule:'required',msg:'手机号未授权'}],
company_name:[{rule:'required',msg:'公司名未填写'}],
name:[{rule:'required',msg:'联系人未填写'}],
work:[{rule:'required',msg:'职位未填写'}],
email:[{rule:'required',msg:'邮箱未填写'},{rule:'email',msg:'邮箱格式不正确'}]
};
* @param {Object} formItem
* @param {Object} rule
*/
Vue.prototype.checkFormPro = function(formItem, rule) {
for (let i in rule) {
for (let j in rule[i]) {
let rule_check = rule[i][j]['rule'];
let msg = rule[i][j]['msg'];
let item = formItem[i];
// if(rule_check)
// console.log(rule_check);
// console.log(item);
switch (rule_check) {
case "required":
if (item == '') return {
result: false,
msg: msg
}
break;
case "email":
let reg = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
if (!reg.test(item)) return {
result: false,
msg: msg
}
break;
}
}
}
return {
result: true
}
}
/**
* 克隆对象
* @param {Object} object
*/
Vue.prototype.cloneObj = function(object) {
return JSON.parse(JSON.stringify(object))
}
/**
* item赋值
* @param {Object} defaultItem
* @param {Object} item
*/
Vue.prototype.setItem = function(defaultItem, item) {
// console.log(item);
for (let key in defaultItem) {
// console.log(item[key]);
// console.log(typeof(item[key]));
if (!(typeof(item[key]) == 'undefined')) {
// console.log(item[key]);
defaultItem[key] = item[key];
}
}
}
Vue.prototype.goUrl = function(url) {
if (url.substring(0, 4) == 'http') {
uni.navigateTo({
url: "/pages/web/web?url=" + encodeURIComponent(url)
})
} else {
uni.navigateTo({
url: url
})
}
}
/**
* 显示弹窗
* @param {Object} title
* @param {Object} content
*/
Vue.prototype.showModal = function(title, content) {
return new Promise((success, fail) => {
uni.showModal({
title: title,
content: content,
success: function(res) {
if (res.confirm) {
// console.log('用户点击确定');
success();
} else if (res.cancel) {
// console.log('用户点击取消');
fail();
}
}
});
});
}
/**
* 获取前几页,页面对象,1代表前一页,2代表前两页
*
* 例: this.lastPage.route=='pages/confirm/confirm'
*
*/
Vue.prototype.lastPage=function(page=1){
return getCurrentPages()[getCurrentPages().length-(page+1)];
}
Vue.prototype.setSearchParam=function(type,keyword,inputText){
let str='keyword=' + encodeURI(keyword)+ "&type="+encodeURI(type)+"&inputText="+ encodeURI(inputText)
return str;
}
Vue.prototype.getSearchParam=function(option){
let obj={};
for(let i in option){
let item=option[i];
item=decodeURI(item)
obj[i]=item;
}
return obj;
}
/**
* 获取当前页面的完整路由,包括参数
* @param {Object} context
*/
Vue.prototype.getCompleteRoute=function(context){
let route=context.$mp.page.route;
route='/'+route;
let options=context.$mp.page.options;
if(Object.keys(options).length === 0) return route;
route+='?';
let index=0;
for(let i in options){
if(index!==0) route+="&";
route+=(i+"="+options[i]);
index++;
}
return route;
// console.log(Object.keys(options).length === 0);
// console.log(options);
// return
}
Vue.prototype.convertTime=function(second,format='H:i:s'){
if(second<=0||!second) return [0,0,0,0];
switch(format){
case'H:i:s':
let hour=parseInt(second/(60*60));
let min=parseInt((second-hour*3600)/60);
let sec=parseInt(second-hour*3600-min*60);
// return {[0,hour,min,sec];}
return {d:0,hour:hour,min:min,sec:sec}
break;
case'i:s':
let min_=parseInt((second)/60);
let sec_=parseInt(second-min_*60);
return {d:0,hour:0,min:min_,sec:sec_}
break;
}
}
//-----------------------------------------------------------------------------------------------
Vue.filter('saveOne', function(value) {
// console.log(typeof value);
if (value === '') return '';
return value.substr(0, value.length - 1);
})
/**
* php的join
* @param {Object} value
*/
Vue.filter('join', function(value) {
if (value === '') return '';
let temp = '';
for (let i in value) {
temp += (value[i] + ',')
}
return temp;
})
Vue.filter('int',(value)=>{
return Number(value);
})
Vue.filter('ceil',(value)=>{
return Math.ceil(value)
})
/**
* @param arg1 下标位置
* @param arg2 分隔符
*/
Vue.filter('split',(value,arg1,arg2='.')=>{
try{
if(!value) return '';
return value.split(arg2)[arg1];
}catch(e){
return '';
}
})
Vue.filter('maxLength',(value,length=10)=>{
let l=value.length;
if(l<=length) return value;
return value.substr(0,length)+'...'
})
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/zpedt/YYShop.git
git@gitee.com:zpedt/YYShop.git
zpedt
YYShop
YYShop
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385