common.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. let home_page_type=0;
  21. if (res && res.data) {
  22. home_page_type=res.data.home_page_type;
  23. Cookies.set("home_page_type",home_page_type);
  24. store.commit('updateUserinfo', {
  25. userinfo: res.data || {}
  26. })
  27. }
  28. else
  29. {
  30. Cookies.set("home_page_type",home_page_type);
  31. }
  32. },
  33. components: {},
  34. data() {
  35. return {}
  36. },
  37. mounted() {},
  38. computed: {
  39. userinfo() {
  40. return this.$store.state.userinfo
  41. },
  42. hasLogined() {
  43. return !!this.userinfo.uid
  44. },
  45. },
  46. methods: {
  47. async needLogin() {
  48. const userInfo = await this.getUserInfo();
  49. if (!userInfo || !userInfo.nickname) {
  50. this.goLogin();
  51. }
  52. },
  53. async cnzz(category,action,label) {
  54. let home_page_type="";
  55. if(Cookies.get('home_page_type')==1)
  56. {
  57. home_page_type="企业方";
  58. }
  59. if(Cookies.get('home_page_type')==2)
  60. {
  61. home_page_type="开发者";
  62. }
  63. else
  64. {
  65. home_page_type="游客";
  66. }
  67. label=label+""+home_page_type;
  68. this.$pushCNZZ.event(category,action,label);
  69. },
  70. async needLoginQrcode() {
  71. const userInfo = await this.getUserInfo();
  72. if (!userInfo || !userInfo.nickname) {
  73. location.href = this.$store.state.domainConfig.siteUrl + '/?loginbox=show&scan=1&next=' + encodeURIComponent(location.href)
  74. }
  75. },
  76. async checkLogin(goLogin = false) {
  77. const userInfo = await this.getUserInfo();
  78. if (!userInfo || !userInfo.nickname) {
  79. this.$message.error('请先登录!')
  80. if (goLogin) {
  81. const {
  82. app
  83. } = this.$deviceType
  84. if (app) {
  85. location.href = 'proginn://login?backToPage=true'
  86. } else if (location.origin.indexOf('local') !== 1 || location.origin.indexOf('dev') !== 1) {
  87. location.href = this.$store.state.domainConfig.siteUrl + '/?loginbox=show&next=' + encodeURIComponent(location.href)
  88. } else {
  89. location.href = this.$store.state.domainConfig.siteUrl + '/?loginbox=show&next=' + encodeURIComponent(location.href)
  90. }
  91. }
  92. return false
  93. }
  94. return true
  95. },
  96. async needVerify() {
  97. const userInfo = await this.getUserInfo();
  98. // 1是待审核,2审核通过,3是拒绝
  99. if (userInfo.realname_verify_status !== '2') {
  100. this.$message.error('根据互联网相关法规要求,请先完成实名认证');
  101. this.goVerify();
  102. }
  103. },
  104. async getUserInfo() {
  105. let res = this.$store.state.userinfo;
  106. if (!res) {
  107. const result = await this.$axios.$get(
  108. `/api/user/getInfo`
  109. );
  110. res = result.data;
  111. }
  112. console.log("用户登录",res);
  113. return res;
  114. },
  115. goVerify() {
  116. location.href = this.$store.state.domainConfig.siteUrl + '/setting/user';
  117. },
  118. goHome() {
  119. location.href = this.$store.state.domainConfig.siteUrl;
  120. },
  121. goLogin(e, noAlert) {
  122. if (noAlert) {
  123. if (this.$deviceType.app) {
  124. location.href = "proginn://login?backToPage=true";
  125. } else {
  126. location.href = this.$store.state.domainConfig.siteUrl + '/?loginbox=show&next=' + encodeURIComponent(location.href)
  127. }
  128. } else {
  129. this.$message.closeAll()
  130. let that = this;
  131. this.$alert('未登录, 前往登录', '提示', {
  132. confirmButtonText: '确定',
  133. center: true,
  134. callback: action => {
  135. if (that.$deviceType.app) {
  136. location.href = "proginn://login?backToPage=true";
  137. } else {
  138. location.href = that.$store.state.domainConfig.siteUrl + '/?loginbox=show&next=' + encodeURIComponent(location.href)
  139. }
  140. }
  141. })
  142. }
  143. },
  144. noCompetence(title = "没有权限") {
  145. this.$alert(title, '提示', {
  146. confirmButtonText: '确定',
  147. center: true,
  148. callback: action => {
  149. location.go(-1)
  150. }
  151. })
  152. },
  153. async updateUserInfo() {
  154. let res = await this.$axios.$get('/api/user/getInfo');
  155. if (res && res.data) {
  156. this.$store.commit('updateUserinfo', {
  157. userinfo: res.data || {}
  158. })
  159. }
  160. },
  161. _toast(msg, type) {
  162. if (this.$deviceType.isMobile()) {
  163. this.$toast(msg)
  164. return
  165. }
  166. if (this.$message[type||'success']) {
  167. this.$message[type||'success'](msg||'')
  168. }
  169. }
  170. }
  171. })