import Vue from "vue";
|
import App from "./App.vue";
|
import router from "./router";
|
import store from "./store";
|
import service from "@/plugin/axios";
|
import webApi from "./assets/js/webApi";
|
|
import "./registerServiceWorker";
|
|
import Vant from "vant";
|
import "vant/lib/index.css";
|
// 基础css
|
import "@/assets/css/base.css";
|
// 公共css
|
import "@/assets/css/common.css";
|
// 配置项
|
import config from "@/assets/js/config";
|
// 工具类
|
import toolClass from "@/assets/js/toolClass";
|
import wx from "weixin-js-sdk";
|
import MG from "@/assets/js/middleGround/WebMiddleGroundApi";
|
// 时间处理
|
import moment from "moment";
|
|
import WeChat from "@/assets/js/weChat/weChat";
|
// import vConsole from "@/assets/js/vConsole"
|
Vue.prototype.webApi = webApi;
|
Vue.config.productionTip = false;
|
Vue.prototype.config = config;
|
Vue.prototype.tool = toolClass;
|
Vue.prototype.request = service;
|
Vue.use(Vant);
|
|
// Vue.use(vConsole);
|
// 微信
|
Vue.use(wx);
|
Vue.prototype.MG = MG;
|
Vue.prototype.moment = moment;
|
Vue.prototype.WeChat = WeChat;
|
|
// 路由拦截处理
|
router.beforeEach((to, from, next) => {
|
console.log(to.meta, "tometa");
|
console.log(to.meta.requiresAuth, "requiresAuth");
|
if (to.meta && to.meta && to.meta.requiresAuth) {
|
// const shareVal = getSharekey(location.href);
|
// if (shareVal && shareVal.key) {
|
// shareNewJobWithApiNewEvent(config.appRefCode, shareVal.productId);
|
// }
|
// 判断当前的token是否存在
|
if (toolClass.getCookie(config.tokenKey)) {
|
// 拦截如果是绑定code,静默绑定微信
|
let bindingCode = WeChat.getUrlInfo("bindingWeChat");
|
if (bindingCode.code) {
|
let query = {
|
code: bindingCode.code,
|
isSocial: true, // 公众号和开放平台是不同的主体,公众号绑定微信使用isSocial=true,开放平台(扫码绑定)使用isSocial=false;
|
};
|
MG.identity
|
.bindWeChat(query)
|
.then((res) => {
|
const url = window.location.href;
|
const redirectUrl = url.split("?")[0];
|
window.location.href = redirectUrl;
|
})
|
.catch((res) => {
|
console.log(res);
|
});
|
} else {
|
next();
|
}
|
} else {
|
// 拦截如果是登录code
|
let loginCode = WeChat.getUrlInfo("weChatLogin");
|
if (loginCode.code) {
|
WeChat.login(loginCode.code, (data) => {
|
toolClass.setCookie(config.tokenKey, data);
|
const url = window.location.href;
|
const domian = url.split("?")[0];
|
console.log(domian, "domian");
|
const routeCode = url.split("#")[1];
|
const redirectUrl = domian + "#" + routeCode;
|
window.location.href = redirectUrl;
|
});
|
return false;
|
}
|
// 检查是否拥有微信账户
|
let checkCode = WeChat.getUrlInfo("checkWeChat");
|
if (checkCode.code) {
|
/**
|
* identity/checkWeChatAccount
|
* 通过code 判断是否是新用户
|
* 新用户
|
* 选择登录方式
|
* 微信登录
|
* 个人中心 提示绑定手机号
|
* 手机号登录
|
* 登录成功后 调用wechat.login 静默绑定用户
|
* 已有账户
|
* 微信登录即可, 此时两个账户已经打通
|
*
|
*/
|
let query = {
|
code: checkCode.code,
|
appCode: config.appRefCode,
|
};
|
MG.identity
|
.chechWechatAccount(query)
|
.then((res) => {
|
if (res == true) {
|
const url = window.location.href;
|
const domian = url.split("?")[0];
|
const redirectUrl = domian + "#" + "/weChatLogin";
|
window.location.href = redirectUrl;
|
} else {
|
if (to.meta.title != "选择登录方式") {
|
const url = window.location.href;
|
const domian = url.split("?")[0];
|
const redirectUrl = domian + "#" + "/index";
|
window.location.href = redirectUrl;
|
} else {
|
const url = window.location.href;
|
const domian = url.split("?")[0];
|
const redirectUrl = domian + "#" + "/selectLoginMethod";
|
window.location.href = redirectUrl;
|
}
|
}
|
})
|
.catch((res) => {
|
console.log(res);
|
});
|
} else {
|
//判断是否是移动端
|
// if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
|
//如在是微信中打开
|
var ua = navigator.userAgent.toLowerCase(); //获取判断用的对象
|
if (ua.match(/MicroMessenger/i) == "micromessenger") {
|
toolClass.delCookie(config.tokenKey);
|
localStorage.fullPath = to.fullPath;
|
window.location.href =
|
config.requestCtx + "/mobile/textbooks/#" + to.fullPath;
|
WeChat.getCode("checkWeChat");
|
} else {
|
WeChat.getCode("weChatLogin");
|
}
|
}
|
}
|
} else {
|
console.log("不拦截");
|
next();
|
}
|
});
|
new Vue({
|
router,
|
store,
|
render: (h) => h(App),
|
}).$mount("#app");
|