web-africa-h5/main.js

756 lines
17 KiB
JavaScript

import Vue from 'vue'
import App from './App'
import directive from './common/directive.js'
import utils from './common/utils.js'
import config from './config.js'
import onfire from './common/onfire.js'
import messages from './locale/index'
import html2canvas from 'html2canvas'
import {
gotopage
} from '@/common/gotopage.js'
/* i18n */
console.log(uni.getLocale())
let i18nConfig = {
locale: uni.getLocale() || 'en-US',
messages: {
'en-US': messages['en-US'],
'zh-CN': messages['zh-CN'],
// 'zh-Hant': messages['zh-Hant'],
'ru-RU': messages['ru-RU'],
'fr-FR': messages['fr-FR'],
'pt-PT': messages['pt-PT'],
// 'en': '',
// 'zh-CN': '',
// 'zh-Hant': '',
// 'ru-RU': '',
// 'fr-FR': '',
}
}
import VueI18n from 'vue-i18n'
Vue.use(VueI18n)
var i18n = new VueI18n(i18nConfig);
/* uView */
import uView from '@/uni_modules/uview-ui'
Vue.use(uView)
/* Vuex */
import store from "./store/index.js"
Vue.prototype.$store = store;
// 公共组件
import headerBar from './components/header.vue'
Vue.component('header-bar', headerBar)
Vue.prototype.$fire = new onfire()
Vue.config.productionTip = false
App.mpType = 'app'
Vue.prototype.config = config
Vue.config.productionTip = false
Vue.prototype.toJSON = () => {
return this
}
const app = new Vue({
i18n,
...App
})
app.$mount()
Vue.prototype.websiteUrl = function() {
let host = config.app_url || '';
// #ifndef APP-PLUS
host = config.app_url
// #endif
return host
// return 'http://p1.hzs413.com'
}
Vue.prototype.app_id = config.app_id;
//h5发布路径
Vue.prototype.h5_addr = config.h5_addr;
/*页面跳转*/
Vue.prototype.gotoPage = gotopage;
//#ifdef H5
app.$router.afterEach((to, from) => {
let loginType = uni.getStorageSync('loginType');
if (loginType == 1) {
if (to.path != '/pages/special/goods/index') {
uni.removeStorageSync('Admin-Token');
uni.removeStorageSync('expires_in');
uni.removeStorageSync('loginType');
}
}
const u = navigator.userAgent.toLowerCase()
if (u.indexOf("like mac os x") < 0 || u.match(/MicroMessenger/i) != 'micromessenger') return
if (to.path !== global.location.pathname) {
location.assign(config.h5_addr + to.fullPath);
}
})
//#endif
Vue.prototype.testFunc = function(e) {
console.log(e)
};
//是否是ios
Vue.prototype.ios = function() {
const u = navigator.userAgent.toLowerCase();
if (u.indexOf("like mac os x") < 0 || u.match(/MicroMessenger/i) != 'micromessenger') {
return false;
}
return true;
};
Vue.prototype.getAppId8n = function(name) {
console.log(name)
console.log(this.$i18n.translate('S_C_67'))
return name
}
//get请求
Vue.prototype._get = function(path, data, success, fail, complete) {
let self = this;
data = data || {};
let host = self.websiteUrl();
// #ifndef APP-PLUS
host = config.app_url
// #endif
let callback = function() {
uni.request({
url: host + '/inter-api/' + path,
data: data,
dataType: 'json',
header: {
'content-type': 'application/json',
'Authorization': uni.getStorageSync('Admin-Token') || '',
'Accept-Language': self.getLanguage()
// 'Accept-Language': 'en-US'
},
method: 'GET',
success: (res) => {
if (res.statusCode !== 200 || typeof res.data !== 'object') {
return false;
}
if (res.data.code === -2) {
console.log('用户已删除');
console.log(res.data.msg)
self.showError(res.data.msg, function() {
uni.removeStorageSync('Admin-Token');
self.gotoPage('/login', 'reLaunch');
})
} else if (res.data.code === 401) {
// 登录态失效, 重新登录
console.log('登录态失效, 重新登录');
self.doLogin();
} else if (res.data.code === 500) {
self.showError(res.data.msg, function() {
fail && fail(res);
});
return false;
} else {
if(res.data.msg === '操作成功'){
res.data.msg = self.$t('fn_056');
}
success && success(res.data);
}
},
fail: (res) => {
fail && fail(res);
},
complete: (res) => {
uni.hideLoading();
complete && complete(res);
},
});
}
// console.log(config.app_url)
// callback();
// #ifndef APP-PLUS
callback();
// #endif
// #ifdef APP-PLUS
if (host) {
callback();
} else {
setTimeout(function() {
console.log('请求host中-----');
self._get(path, data, success, fail, complete)
}, 300);
}
// #endif
};
//post请求
Vue.prototype._post = function(path, data, success, fail, complete) {
let self = this;
data = data || {};
let host = self.websiteUrl();
// #ifndef APP-PLUS
host = config.app_url
// #endif
let Source = '2';
// #ifndef APP-PLUS
Source = '3'
// #endif
uni.request({
url: host + '/inter-api/' + path,
data: data,
dataType: 'json',
method: 'POST',
header: {
'content-type': 'application/json',
'Authorization': uni.getStorageSync('Admin-Token') || '',
'Accept-Language': self.getLanguage(),
'token': uni.getStorageSync('token') || '',
'Source': Source
},
success: (res) => {
if (res.data.code === -1) {
// 登录态失效, 重新登录
console.log('登录态失效, 重新登录');
this.doLogin();
} else if (res.data.code === 500) {
this.showError(res.data.msg, function() {
fail && fail(res);
});
return false;
} else {
if(res.data.msg === '操作成功'){
res.data.msg = self.$t('fn_056');
}
success && success(res.data);
}
},
fail: (res) => {
fail && fail(res);
},
complete: (res) => {
uni.hideLoading();
complete && complete(res);
},
});
};
//PUT请求
Vue.prototype._put = function(path, data, success, fail, complete) {
let self = this;
let host = self.websiteUrl();
// #ifndef APP-PLUS
host = config.app_url
// #endif
uni.request({
url: host + '/inter-api/' + path + '/' + data,
dataType: 'json',
method: 'PUT',
header: {
'content-type': 'application/json',
'Authorization': uni.getStorageSync('Admin-Token') || '',
'Accept-Language': self.getLanguage()
},
success: (res) => {
if (res.data.code === 500) {
this.showError(res.data.msg, function() {
fail && fail(res);
});
return false;
} else {
if(res.data.msg === '操作成功'){
res.data.msg = self.$t('fn_056');
}
success && success(res.data);
}
},
fail: (res) => {
fail && fail(res);
},
complete: (res) => {
uni.hideLoading();
complete && complete(res);
},
});
};
//PUT请求
Vue.prototype._putjson = function(path, data, success, fail, complete) {
let self = this;
data = data || {};
let host = self.websiteUrl();
// #ifndef APP-PLUS
host = config.app_url
// #endif
uni.request({
url: host + '/inter-api/' + path,
data: data,
dataType: 'json',
method: 'PUT',
header: {
'content-type': 'application/json',
'Authorization': uni.getStorageSync('Admin-Token') || '',
'Accept-Language': self.getLanguage()
},
success: (res) => {
if (res.data.code === 500) {
this.showError(res.data.msg, function() {
fail && fail(res);
});
return false;
} else {
if(res.data.msg === '操作成功'){
res.data.msg = self.$t('fn_056');
}
success && success(res.data);
}
},
fail: (res) => {
fail && fail(res);
},
complete: (res) => {
uni.hideLoading();
complete && complete(res);
},
});
};
//DELETE请求
Vue.prototype._delete = function(path, data, success, fail, complete) {
let self = this;
let host = self.websiteUrl();
// #ifndef APP-PLUS
host = config.app_url
// #endif
uni.request({
url: host + '/inter-api/' + path,
dataType: 'json',
method: 'DELETE',
header: {
'content-type': 'application/json',
'Authorization': uni.getStorageSync('Admin-Token') || '',
'Accept-Language': self.getLanguage()
},
success: (res) => {
if (res.data.code === 500) {
this.showError(res.data.msg, function() {
fail && fail(res);
});
return false;
} else {
if(res.data.msg === '操作成功'){
res.data.msg = self.$t('fn_056');
}
success && success(res.data);
}
},
fail: (res) => {
fail && fail(res);
},
complete: (res) => {
uni.hideLoading();
complete && complete(res);
},
});
};
Vue.prototype.doLogin = function() {
let pages = getCurrentPages();
if (pages.length) {
let currentPage = pages[pages.length - 1];
if ("login" != currentPage.route) {
uni.setStorageSync("currentPage", currentPage.route);
uni.setStorageSync("currentPageOptions", currentPage.options);
}
}
this.gotoPage("/login", 'reLaunch');
};
/**
* 显示失败提示框
*/
Vue.prototype.showError = function(msg, callback) {
let self = this;
uni.showModal({
title: self.$t('w_0034'),
content: msg,
showCancel: false,
success: function(res) {
callback && callback();
}
});
};
/**
* 显示失败提示框
*/
Vue.prototype.showSuccess = function(msg, callback) {
let self = this;
uni.showModal({
title: self.$t('w_0034'),
content: msg,
showCancel: false,
success: function(res) {
callback && callback();
}
});
};
/**
* 获取应用ID
*/
Vue.prototype.getAppId = function() {
return this.app_id || 10001;
};
Vue.prototype.compareVersion = function(v1, v2) {
v1 = v1.split('.')
v2 = v2.split('.')
const len = Math.max(v1.length, v2.length)
while (v1.length < len) {
v1.push('0')
}
while (v2.length < len) {
v2.push('0')
}
for (let i = 0; i < len; i++) {
const num1 = parseInt(v1[i])
const num2 = parseInt(v2[i])
if (num1 > num2) {
return 1
} else if (num1 < num2) {
return -1
}
}
return 0
};
/**
* 生成转发的url参数
*/
Vue.prototype.getShareUrlParams = function(params) {
let self = this;
return utils.urlEncode(Object.assign({
referee_id: self.getUserId(),
}, params));
};
/**
* 当前用户id
*/
Vue.prototype.getUserId = function() {
return uni.getStorageSync('userCode');
};
//#ifdef H5
var jweixin = require('jweixin-module');
Vue.prototype.configWx = function(signPackage, shareParams, params) {
if (signPackage == '') {
return;
}
let self = this;
jweixin.config(JSON.parse(signPackage));
let url_params = self.getShareUrlParams(params);
jweixin.ready(function(res) {
jweixin.updateAppMessageShareData({
title: shareParams.title,
desc: shareParams.desc,
link: self.websiteUrl() + self.h5_addr + shareParams.link + '?' + url_params,
imgUrl: shareParams.imgUrl,
success: function() {
}
});
jweixin.updateTimelineShareData({
title: shareParams.title,
desc: shareParams.desc,
link: self.websiteUrl() + self.h5_addr + shareParams.link + '?' + url_params,
imgUrl: shareParams.imgUrl,
success: function() {
}
});
});
};
Vue.prototype.configWxScan = function(signPackage) {
if (signPackage == '') {
return;
}
jweixin.config(JSON.parse(signPackage));
return jweixin;
};
//#endif
Vue.prototype.toThousands = function(num, cent, isThousand) {
num = num.toString().replace(/\$|\,/g, '');
// 检查传入数值为数值类型
if (isNaN(num))
num = "0";
// 获取符号(正/负数)
let sign = (num == (num = Math.abs(num)));
num = Math.floor(num * Math.pow(10, cent) + 0.50000000001); // 把指定的小数位先转换成整数.多余的小数位四舍五入
let cents = num % Math.pow(10, cent); // 求出小数位数值
num = Math.floor(num / Math.pow(10, cent)).toString(); // 求出整数位数值
cents = cents.toString(); // 把小数位转换成字符串,以便求小数位长度
// 补足小数位到指定的位数
while (cents.length < cent)
cents = "0" + cents;
if (isThousand) {
// 对整数部分进行千分位格式化.
for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
}
if (cent > 0)
return (((sign) ? '' : '-') + num + '.' + cents);
else
return (((sign) ? '' : '-') + num);
};
Vue.prototype.formatNum = function(floatNum) {
let flag = true;
if (floatNum < 0) {
flag = false;
}
floatNum = Math.abs(floatNum * 1)
let n = (floatNum * 1).toFixed(2);
var st1 = String(n); // 将浮点数转换为字符串
var index = st1.indexOf('.'); // 取小数点下标
var st2 = st1.slice(0, index); // 将小数点前面的字符串截取出来
var len = st2.length;
if (len < 3 && flag) return st2.concat(st1.slice(index));
if (len < 3 && !flag) return '-' + st2.concat(st1.slice(index));
var r = len % 3;
let num = (r > 0 ?
st2.slice(0, r) + ',' + st2.slice(r, len).match(/\d{3}/g).join() :
st2.slice(r, len).match(/\d{3}/g).join())
.concat(st1.slice(index));
if (!flag) {
num = '-' + num
}
return num
}
/**
* 获取当前平台
*/
Vue.prototype.getPlatform = function(params) {
let platform = 'wx';
// #ifdef APP-PLUS
if (uni.getSystemInfoSync().platform == 'android') {
platform = 'android';
} else {
platform = 'ios';
}
// #endif
// #ifdef H5
if (this.isWeixin()) {
platform = 'mp';
} else {
platform = 'h5';
}
// #endif
return platform;
};
/**
* 订阅通知,目前仅小程序
*/
Vue.prototype.subMessage = function(temlIds, callback) {
let self = this;
// #ifdef MP-WEIXIN
//小程序订阅消息
const version = wx.getSystemInfoSync().SDKVersion;
if (temlIds && temlIds.length != 0 && self.compareVersion(version, '2.8.2') >= 0) {
wx.requestSubscribeMessage({
tmplIds: temlIds,
success(res) {},
fail(res) {},
complete(res) {
callback();
},
});
} else {
callback();
}
// #endif
// #ifndef MP-WEIXIN
callback();
// #endif
};
Vue.prototype.isWeixin = function() {
var ua = navigator.userAgent.toLowerCase();
if (ua.match(/MicroMessenger/i) == "micromessenger") {
return true;
} else {
return false;
}
};
Vue.prototype.topBarTop = function() {
// #ifdef MP-WEIXIN
return uni.getMenuButtonBoundingClientRect().top;
// #endif
// #ifndef MP-WEIXIN
const SystemInfo = uni.getSystemInfoSync();
return SystemInfo.statusBarHeight;
// #endif
};
Vue.prototype.topBarHeight = function() {
// #ifdef MP-WEIXIN
return uni.getMenuButtonBoundingClientRect().height;
// #endif
// #ifndef MP-WEIXIN
return 0
// #endif
};
Vue.prototype.callPhone = function(n) {
if (n == '') {
this.showError('拨打号码为空')
return
}
uni.makePhoneCall({
phoneNumber: n
});
}
Vue.prototype.yulan = function(e, i) {
console.log(e)
let image_path_arr = [];
if (!Array.isArray(e)) {
image_path_arr = [e];
} else {
if (e[0].file_path) {
e.forEach((item, index) => {
image_path_arr.push(item.file_path)
})
} else {
image_path_arr = e
}
}
let picnum = i * 1;
console.log(image_path_arr)
uni.previewImage({
urls: image_path_arr,
current: picnum,
indicator: 'default',
});
}
Vue.prototype.mobileHidden = function(num) {
return num.toString().slice(0, 3) + num.toString().slice(-4).padStart(7, '*');
}
Vue.prototype.currencyIcon = function() {
return uni.getStorageSync('currencyIcon') || '¥'
}
Vue.prototype.performanceCurrency = function() {
let icon = uni.getStorageSync('currencyIcon') || '¥';
if (icon != '¥') {
icon = '$'
}
return icon
}
Vue.prototype.changelang = async function() {
let self = this;
let itemList = [];
let list = this.$store.state.languages;
if (list == '') {
await this.getlangList();
list = this.$store.state.languages;
}
list.forEach(item => {
itemList.push(item.label);
})
uni.showActionSheet({
itemList: itemList,
success: function(res) {
let index = res.tapIndex;
uni.setLocale(list[index].field);
self.$i18n.locale = list[index].field;
// #ifdef H5
history.go(0);
// #endif
},
fail: function(res) {
console.log(res.errMsg);
}
});
}
Vue.prototype.getlangList = async function() {
let self = this;
console.log(uni.getLocale())
return new Promise((resolve, reject) => {
self._get('system/pub/enums/get-languages', {}, res => {
self.$store.commit('changeLanguages', res.data);
resolve( res.data);
})
});
}
Vue.prototype.getLocale = function() {
let l = uni.getLocale() || 'en-US';
if (l == 'zh-CN') {
return 'zh-CN'
} else if (l == 'zh-Hant') {
return 'zh-TC'
} else if (l == 'fr-FR') {
return 'fr-FR'
} else if (l == 'ru-RU') {
return 'ru-RU'
} else if (l == 'pt-PT') {
return 'pt-PT'
} else {
return 'en-US'
}
}
Vue.prototype.getLanguage = function() {
let l = uni.getLocale() || 'en-US';
if (l == 'zh-CN') {
return 'zh-CN'
} else if (l == 'zh-Hant') {
return 'zh-TC'
} else if (l == 'fr-FR') {
return 'fr-FR'
} else if (l == 'ru-RU') {
return 'ru-RU'
} else if (l == 'pt-PT') {
return 'pt-PT'
} else {
return 'en-US'
}
}
Vue.prototype.getlangfunc = async function() {
let self = this;
let list = this.$store.state.languages;
if (list == '') {
await this.getlangList();
list = this.$store.state.languages;
}
let lang = self.getLocale();
let e = '';
list.forEach(item => {
if (item.field == lang) {
e = item.label
}
})
self.$store.commit('changeLocal', e);
}
Vue.prototype.getlang = function() {
let self = this;
this.getlangfunc();
return this.$store.state.localLang;
}
Vue.prototype.test = function(e) {
console.log(e);
}