preload/index.js
import Vue from ‘vue‘ import router from ‘../router‘; import urlUtils from ‘../utils/urlutils‘ import { updateAxiosC, updateAxiosB, queryAxiosC, queryAxiosB} from ‘../axios‘; /** * @param {*} code */ function getOpenId( code ) { return queryAxiosB.get(`/wechat/getOpenIdByCode/${code}`).then(resp => { if( resp.data.retCode === ‘0000‘ ){ const data = resp.data.retData[0]; if (data.openId) { for(let key in data){ Vue.prototype[key] = data[key]; } return { msg: ‘获取openId成功‘,status: true }; } return Promise.reject(‘无效code‘); } return { msg: ‘后台接口异常‘,status: true }; }); } function getCode() { const code = urlUtils.getSearchParam(‘code‘); return code; } function getShopUserId( openId ) { return queryAxiosB.get(`/wechat/getShopAccountByOpenId/${openId}`); } function getQrCode( qrcode ) { const qrCode = urlUtils.getSearchParam(‘qr‘); Vue.prototype.qrCode = qrCode; return qrCode; } function isBind( qrCode ) { return queryAxiosB.get(`/myQrcodeTApi/checkBindQrCode/${qrCode}`).then(resp => { const status = resp.data.retData[0].status; if(status !== ‘02‘){ return true; } else { return Promise.reject(); } }); } function hashReceiptCount() { return new Promise((resolve, reject) => { if(Vue.prototype.shopAccountFlag === ‘1‘){ resolve(); } else { reject(); } }); } function openIdIsBind() { return new Promise((resolve, reject) => { if(Vue.prototype.shopUserId){ resolve(); } else { reject(); } }); } function toCPage() { const customerUserId = localStorage.getItem(‘customerUserId‘); const token = localStorage.getItem(‘token‘); const customerLoginPhone = localStorage.getItem(‘customerLoginPhone‘); if (customerUserId&&token&&customerLoginPhone) { Vue.prototype.customerUserId = customerUserId; Vue.prototype.token = token; Vue.prototype.customerLoginPhone = customerLoginPhone; } else { location.hash = ‘/c/navigate‘; } } function fromOther() { const qrCode = getQrCode(); return new Promise((resolve, reject) => { if (qrCode) { isBind(qrCode).then(data => { toCPage(); resolve(); }).catch(error => { location.hash = ‘/b/shoplogin‘; resolve(); }); } else { toCPage(); resolve(); } }); } function fromWeChat(){ return openIdIsBind().then(_ => { return hashReceiptCount().catch(_ => { location.hash = ‘/b/gatheredCards‘; }); }).catch(_ => { Vue.prototype.curRoute = location.hash.slice(1); location.hash = ‘/b/shoplogin‘; }); } function preload() { const code = getCode(); if (code) { return getOpenId(code).then(info => { console.info(info.msg); return fromWeChat(); }).catch(error => { console.error(error); return fromOther(); }); } else { return fromOther(); } } export default preload;
urlutils/index.js
function getHashParam(name) { const reg = new RegExp(‘[\\?&]‘ + name + ‘=(\\w+)‘); const hash = location.hash; if(reg.test(hash)){ return RegExp.$1; } return null; } function getSearchParam(name) { const reg = new RegExp(‘[\\?&]‘ + name + ‘=(\\w+)‘); const search = location.search; if(reg.test(search)){ console.log(RegExp.$1); return RegExp.$1; } return null; } export default { getHashParam, getSearchParam }
main.js
preLoad().then(_ => { /* eslint-disable no-new */ new Vue({ el: ‘#app‘, router, store, template: ‘<App/>‘, components: { App } }); });