common.js 4.0 KB

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