| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- 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
- });
- if (res && res.data) {
- store.commit('updateUserinfo', {
- userinfo: res.data || {}
- })
- }
- },
- 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 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) {
- 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;
- }
- 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||'')
- }
- }
- }
- })
|