main.js 4.31 KB
Newer Older
王建威's avatar
王建威 committed
1 2 3 4 5
import Vue from 'vue'
import App from './App'
import store from '@/store/store.js';
import {
	php,
王建威's avatar
王建威 committed
6
	go
王建威's avatar
王建威 committed
7 8
} from '@/common/host.js';
import {
王建威's avatar
王建威 committed
9
	getNavigationBarTitle,
郑秀明's avatar
郑秀明 committed
10
	getCache
王建威's avatar
王建威 committed
11 12 13 14
} from './common/util.js';

// 微信分享公共接口
// #ifdef H5  
王建威's avatar
王建威 committed
15
import wechat from './common/wechat.js';
16 17 18 19
Vue.prototype.$wechat = wechat;
// if (wechat.isWechat() || wechat.isMini()) {
// 	Vue.prototype.$wechat = wechat;
// }
王建威's avatar
王建威 committed
20 21 22
// #endif

Vue.config.productionTip = false;
王建威's avatar
王建威 committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
const {
	origin
} = window.location;
let url = '';
// 测试环境
if (origin.indexOf('local') > -1 || origin.indexOf('test') > -1) {
	if (origin.indexOf('test') > -1) {
		url = origin + '/mobile/index.php?';
	} else {
		url = 'https://test.pet-dbc.cn/mobile/index.php?';
	}
} else if (origin.indexOf('jingxiang') > -1) {
	url = 'https://jingxiang.pet-dbc.cn/mobile/index.php?';
} else {
	url = 'https://shop.pet-dbc.cn/mobile/index.php?';
}
var search = window.location.href.split('?');
if (search[1]) {
	var arr = search[1].split('&');
	arr.every((item, index) => {
		var kv = item.split('=');
		if (kv[0] === 'openId') {
45 46 47
			localStorage.openId = kv[1];
			var exp = new Date();
		    exp.setTime(exp.getTime() + 3650*24*60*60*1000);
王建威's avatar
王建威 committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
		    document.cookie = 'openId='+kv[1]+';expires='+exp.toGMTString();
			$.ajax({
				url: url + 'app=weixin&act=autoLogin',
				type: 'get',
				data: {
					openid: kv[1]
				},
				dataType: 'json',
				success: function(res) {
					alert(JSON.stringify(res))
				}
			})
		}
		if (kv[0] === 'inionid') {
			localStorage.unionid = kv[1];
		}
	});
}
王建威's avatar
王建威 committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

// 加入购物车
function addCart(spec_id, quantity, isLogin) {
	if (!isLogin) {
		uni.showToast({
			title: '请登录后加入购物车',
			duration: 2000,
			icon: 'none'
		});
		setTimeout(() => {
			window.location.href = php + 'app=member&act=login';
		}, 2000);
		return
	}
	uni.request({
		url: '/uni/api//credit_goods/add_cart',
		method: 'post',
		dataType: 'json',
		data: {
			spec_id: spec_id,
			quantity: quantity
		},
		success: (res) => {
			let msg = '';
			if (res.data.code == 0) {
				msg = '已加入购物车'
			} else {
				msg = res.data.messages
			}
			uni.showToast({
				title: msg,
				duration: 2000,
				icon: 'none'
			});
		}
	})
}
// 默认图片加载失败的情况
function __reloadResource(_this) {
	_this._source.default_image =
		'https://dbc-static.oss-cn-beijing.aliyuncs.com/credit_shop/20190605/goods/5cf78b96601be.png?x-oss-process=image/resize,m_lfit,w_300,h_300/auto-orient,0/quality,Q_85/format,jpg';
}
王建威's avatar
王建威 committed
108
// 毫秒数转时间
王建威's avatar
王建威 committed
109 110 111 112 113 114 115

// 跳转商品详情页,只传一个商品ID,方便以后全局修改

function jumpGoodDetail(goods_id) {
	window.location.href = `${php}app=goods&id=${goods_id}`;
}

116 117
function jumpOrderDetail(order_id) {
	window.location.href = `${php}app=buyer_order&act=view&order_id=${order_id}`;
王建威's avatar
王建威 committed
118 119 120 121 122 123 124 125 126 127 128 129
}

function backup() {
	const routes = getCurrentPages();
	if (routes.length > 1) {
		uni.navigateBack();
	} else {
		history.back();
	}
}

// 跳转外部链接,使用webview
郑秀明's avatar
郑秀明 committed
130
function jump(url, type) {
王建威's avatar
王建威 committed
131
	// #ifdef H5
王建威's avatar
王建威 committed
132
	if (type === 1) {
王建威's avatar
王建威 committed
133 134
		// 重定向到指定地址,防止回退
		window.location.replace(url);
王建威's avatar
王建威 committed
135 136 137
	} else if (type === 2) {
		uni.navigateTo({
			url: url
王建威's avatar
王建威 committed
138
		})
王建威's avatar
王建威 committed
139 140
	} else {
		window.location.href = url;
郑秀明's avatar
郑秀明 committed
141
	}
王建威's avatar
王建威 committed
142 143 144 145 146 147 148 149 150 151 152 153
	// #endif

	// #ifndef H5
	const link = url
	store.dispatch('changeWebviewUrl', link)
	uni.redirectTo({
		url: `/pages/web/web`,
	});
	// #endif
}

Vue.prototype.$jumpGoodDetail = jumpGoodDetail;
王建威's avatar
王建威 committed
154
Vue.prototype.$jump = jump;
155
Vue.prototype.$jumpOrderDetail = jumpOrderDetail;
郑秀明's avatar
郑秀明 committed
156
Vue.prototype.$getCache = getCache;
王建威's avatar
王建威 committed
157
Vue.prototype.$addCart = addCart;
王建威's avatar
王建威 committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
Vue.prototype.$backup = backup;
Vue.prototype.$__reloadResource = __reloadResource;
Vue.prototype.$getNavigationBarTitle = getNavigationBarTitle;
Vue.prototype.$noGoodsImg =
	'https://dbc-static.oss-cn-beijing.aliyuncs.com/credit_shop/20190605/goods/5cf78b96601be.png?x-oss-process=image/resize,m_lfit,w_300,h_300/auto-orient,0/quality,Q_85/format,jpg';
Vue.prototype.$defaultPortrait = 'https://dbc-static.oss-cn-beijing.aliyuncs.com/credit/default_user_portrait.gif';
Vue.prototype.$defaultClassImg =
	'https://dbc-static.oss-cn-beijing.aliyuncs.com/credit_shop/20190520/img/5ce2636edf912.png';

App.mpType = 'app'

const app = new Vue({
	...App,
	store
})
王建威's avatar
王建威 committed
173
app.$mount()