import Vue from 'vue' import Cookies from 'js-cookie' // import http from '@/plugins/http' // mixin Vue.mixin({ async fetch({ $axios, store, req }) { if (process.client && !Cookies.get('x_access_token')) { return } let headers = req && req.headers let res = await $axios.$get('/api/user/getInfo', { headers }, { neverLogout: true }); let home_page_type=0; if (res && res.data) { home_page_type=res.data.home_page_type; Cookies.set("home_page_type",home_page_type); store.commit('updateUserinfo', { userinfo: res.data || {} }) } else { Cookies.set("home_page_type",home_page_type); } }, components: {}, data() { return {} }, mounted() {}, computed: { userinfo() { return this.$store.state.userinfo }, hasLogined() { return !!this.userinfo.uid }, }, methods: { async needLogin() { const userInfo = await this.getUserInfo(); if (!userInfo || !userInfo.nickname) { this.goLogin(); } }, async cnzz(category,action,label) { let home_page_type=""; if(Cookies.get('home_page_type')==1) { home_page_type="企业方"; } if(Cookies.get('home_page_type')==2) { home_page_type="开发者"; } else { home_page_type="游客"; } label=label+""+home_page_type; this.$pushCNZZ.event(category,action,label); }, async needLoginQrcode() { const userInfo = await this.getUserInfo(); if (!userInfo || !userInfo.nickname) { location.href = this.$store.state.domainConfig.siteUrl + '/?loginbox=show&scan=1&next=' + encodeURIComponent(location.href) } }, async checkLogin(goLogin = false) { const userInfo = await this.getUserInfo(); if (!userInfo || !userInfo.nickname) { if(!goLogin) this.$message.error('请先登录!') if (goLogin) { const { app } = this.$deviceType if (app) { location.href = 'proginn://login?backToPage=true' } else if (location.origin.indexOf('local') !== 1 || location.origin.indexOf('dev') !== 1) { location.href = this.$store.state.domainConfig.siteUrl + '/?loginbox=show&next=' + encodeURIComponent(location.href) } else { location.href = this.$store.state.domainConfig.siteUrl + '/?loginbox=show&next=' + encodeURIComponent(location.href) } } return false } return true }, async needVerify() { const userInfo = await this.getUserInfo(); // 1是待审核,2审核通过,3是拒绝 if (userInfo.realname_verify_status !== '2') { this.$message.error('根据互联网相关法规要求,请先完成实名认证'); this.goVerify(); } }, async getUserInfo() { let res = this.$store.state.userinfo; if (!res) { const result = await this.$axios.$get( `/api/user/getInfo` ); res = result.data; } console.log("用户登录",res); return res; }, goVerify() { location.href = this.$store.state.domainConfig.siteUrl + '/setting/user'; }, goHome() { location.href = this.$store.state.domainConfig.siteUrl; }, goLogin(e, noAlert) { if (noAlert) { if (this.$deviceType.app) { location.href = "proginn://login?backToPage=true"; } else { location.href = this.$store.state.domainConfig.siteUrl + '/?loginbox=show&next=' + encodeURIComponent(location.href) } } else { this.$message.closeAll() let that = this; this.$alert('未登录, 前往登录', '提示', { confirmButtonText: '确定', center: true, callback: action => { if (that.$deviceType.app) { location.href = "proginn://login?backToPage=true"; } else { location.href = that.$store.state.domainConfig.siteUrl + '/?loginbox=show&next=' + encodeURIComponent(location.href) } } }) } }, noCompetence(title = "没有权限") { this.$alert(title, '提示', { confirmButtonText: '确定', center: true, callback: action => { location.go(-1) } }) }, async updateUserInfo() { let res = await this.$axios.$get('/api/user/getInfo'); if (res && res.data) { this.$store.commit('updateUserinfo', { userinfo: res.data || {} }) } }, _toast(msg, type) { if (this.$deviceType.isMobile()) { this.$toast(msg) return } if (this.$message[type||'success']) { this.$message[type||'success'](msg||'') } } } })