common.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import Vue from 'vue'
  2. import Cookies from 'js-cookie'
  3. // import http from '@/plugins/http'
  4. // mixin
  5. Vue.mixin({
  6. async fetch({
  7. $axios,
  8. store,
  9. req
  10. }) {
  11. if (process.client && !Cookies.get('x_access_token')) {
  12. return
  13. }
  14. let headers = req && req.headers
  15. let res = await $axios.$get('/api/user/getInfo', {
  16. headers
  17. }, {
  18. neverLogout: true
  19. });
  20. if (res && res.data) {
  21. store.commit('updateUserinfo', {
  22. userinfo: res.data || {}
  23. })
  24. }
  25. },
  26. components: {},
  27. data() {
  28. return {}
  29. },
  30. mounted() {},
  31. computed: {
  32. userinfo() {
  33. return this.$store.state.userinfo
  34. },
  35. hasLogined() {
  36. return !!this.userinfo.uid
  37. },
  38. },
  39. methods: {
  40. async needLogin() {
  41. const userInfo = await this.getUserInfo();
  42. if (!userInfo || !userInfo.nickname) {
  43. this.goLogin();
  44. }
  45. },
  46. async needLoginQrcode() {
  47. const userInfo = await this.getUserInfo();
  48. if (!userInfo || !userInfo.nickname) {
  49. location.href = this.$store.state.domainConfig.siteUrl + '/?loginbox=show&scan=1&next=' + encodeURIComponent(location.href)
  50. }
  51. },
  52. async checkLogin(goLogin = false) {
  53. const userInfo = await this.getUserInfo();
  54. if (!userInfo || !userInfo.nickname) {
  55. this.$message.error('请先登录!')
  56. if (goLogin) {
  57. const {
  58. app
  59. } = this.$deviceType
  60. if (app) {
  61. location.href = 'proginn://login?backToPage=true'
  62. } else if (location.origin.indexOf('local') !== 1 || location.origin.indexOf('dev') !== 1) {
  63. location.href = this.$store.state.domainConfig.siteUrl + '/?loginbox=show&next=' + encodeURIComponent(location.href)
  64. } else {
  65. location.href = this.$store.state.domainConfig.siteUrl + '/?loginbox=show&next=' + encodeURIComponent(location.href)
  66. }
  67. }
  68. return false
  69. }
  70. return true
  71. },
  72. async needVerify() {
  73. const userInfo = await this.getUserInfo();
  74. // 1是待审核,2审核通过,3是拒绝
  75. if (userInfo.realname_verify_status !== '2') {
  76. this.$message.error('根据互联网相关法规要求,请先完成实名认证');
  77. this.goVerify();
  78. }
  79. },
  80. async getUserInfo() {
  81. let res = this.$store.state.userinfo;
  82. if (!res) {
  83. const result = await this.$axios.$get(
  84. `/api/user/getInfo`
  85. );
  86. res = result.data;
  87. }
  88. return res;
  89. },
  90. goVerify() {
  91. location.href = this.$store.state.domainConfig.siteUrl + '/setting/user';
  92. },
  93. goHome() {
  94. location.href = this.$store.state.domainConfig.siteUrl;
  95. },
  96. goLogin(e, noAlert) {
  97. if (noAlert) {
  98. if (this.$deviceType.app) {
  99. location.href = "proginn://login?backToPage=true";
  100. } else {
  101. location.href = this.$store.state.domainConfig.siteUrl + '/?loginbox=show&next=' + encodeURIComponent(location.href)
  102. }
  103. } else {
  104. this.$message.closeAll()
  105. let that = this;
  106. this.$alert('未登录, 前往登录', '提示', {
  107. confirmButtonText: '确定',
  108. center: true,
  109. callback: action => {
  110. if (that.$deviceType.app) {
  111. location.href = "proginn://login?backToPage=true";
  112. } else {
  113. location.href = that.$store.state.domainConfig.siteUrl + '/?loginbox=show&next=' + encodeURIComponent(location.href)
  114. }
  115. }
  116. })
  117. }
  118. },
  119. noCompetence(title = "没有权限") {
  120. this.$alert(title, '提示', {
  121. confirmButtonText: '确定',
  122. center: true,
  123. callback: action => {
  124. location.go(-1)
  125. }
  126. })
  127. },
  128. async updateUserInfo() {
  129. let res = await this.$axios.$get('/api/user/getInfo');
  130. if (res && res.data) {
  131. this.$store.commit('updateUserinfo', {
  132. userinfo: res.data || {}
  133. })
  134. }
  135. },
  136. _toast(msg, type) {
  137. if (this.$deviceType.isMobile()) {
  138. this.$toast(msg)
  139. return
  140. }
  141. if (this.$message[type||'success']) {
  142. this.$message[type||'success'](msg||'')
  143. }
  144. }
  145. }
  146. })