common.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. console.log('needLogin++++++++++++++++++');
  39. console.log(userInfo);
  40. if (!userInfo || !userInfo.nickname) {
  41. this.goLogin();
  42. }
  43. },
  44. async checkLogin(goLogin = false) {
  45. const userInfo = await this.getUserInfo();
  46. if (!userInfo || !userInfo.nickname) {
  47. this.$message.error('请先登录!')
  48. if (goLogin) {
  49. const {
  50. app
  51. } = this.$deviceType
  52. if (app) {
  53. location.href = 'proginn://login?backToPage=true'
  54. } else if (location.origin.indexOf('local') !== 1 || location.origin.indexOf('dev') !== 1) {
  55. location.href = this.$store.state.domainConfig.siteUrl + '/?loginbox=show'
  56. } else {
  57. location.href = this.$store.state.domainConfig.siteUrl + '/?loginbox=show'
  58. }
  59. }
  60. return false
  61. }
  62. return true
  63. },
  64. async needVerify() {
  65. const userInfo = await this.getUserInfo();
  66. // 1是待审核,2审核通过,3是拒绝
  67. if (userInfo.realname_verify_status !== '2') {
  68. this.$message.error('根据互联网相关法规要求,请先完成实名认证')
  69. this.goVerify();
  70. }
  71. },
  72. async getUserInfo() {
  73. let res = this.$store.state.userinfo;
  74. if (!res) {
  75. const result = await this.$axios.$get(
  76. `/api/user/getInfo`
  77. );
  78. res = result.data;
  79. }
  80. return res;
  81. },
  82. goVerify() {
  83. location.href = this.$store.state.domainConfig.siteUrl + '/setting/user';
  84. },
  85. goHome() {
  86. location.href = this.$store.state.domainConfig.siteUrl;
  87. },
  88. goLogin(e, noAlert) {
  89. if (noAlert) {
  90. if (this.$deviceType.app) {
  91. location.href = "proginn://login?backToPage=true";
  92. } else {
  93. location.href = this.$store.state.domainConfig.siteUrl + '/?loginbox=show'
  94. }
  95. } else {
  96. this.$message.closeAll()
  97. let that = this;
  98. this.$alert('未登录, 前往登录', '提示', {
  99. confirmButtonText: '确定',
  100. center: true,
  101. callback: action => {
  102. if (that.$deviceType.app) {
  103. location.href = "proginn://login?backToPage=true";
  104. } else {
  105. location.href = that.$store.state.domainConfig.siteUrl + '/?loginbox=show'
  106. }
  107. }
  108. })
  109. }
  110. },
  111. noCompetence(title = "没有权限") {
  112. this.$alert(title, '提示', {
  113. confirmButtonText: '确定',
  114. center: true,
  115. callback: action => {
  116. location.go(-1)
  117. }
  118. })
  119. },
  120. async updateUserInfo() {
  121. let res = await this.$axios.$get('/api/user/getInfo');
  122. console.log('res', res.data)
  123. if (res && res.data) {
  124. this.$store.commit('updateUserinfo', {
  125. userinfo: res.data || {}
  126. })
  127. }
  128. },
  129. _toast(msg, type) {
  130. if (this.$deviceType.isMobile()) {
  131. this.$toast(msg)
  132. return
  133. }
  134. if (this.$message[type||'success']) {
  135. this.$message[type||'success'](msg||'')
  136. }
  137. }
  138. }
  139. })