common.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. if(!goLogin) 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 checkMobile() {
  105. const userInfo = await this.getUserInfo();
  106. // 1是待审核,2审核通过,3是拒绝
  107. if (userInfo.mobile == '') {
  108. this.$message.error('根据互联网相关法规要求,请先完成手机认证');
  109. this.goVerify();
  110. return false;
  111. }
  112. return true;
  113. },
  114. async getUserInfo() {
  115. let res = this.$store.state.userinfo;
  116. if (!res) {
  117. const result = await this.$axios.$get(
  118. `/api/user/getInfo`
  119. );
  120. res = result.data;
  121. }
  122. console.log("用户登录",res);
  123. return res;
  124. },
  125. goVerify() {
  126. location.href = this.$store.state.domainConfig.siteUrl + '/setting/user';
  127. },
  128. goHome() {
  129. location.href = this.$store.state.domainConfig.siteUrl;
  130. },
  131. goLogin(e, noAlert) {
  132. if (noAlert) {
  133. if (this.$deviceType.app) {
  134. location.href = "proginn://login?backToPage=true";
  135. } else {
  136. location.href = this.$store.state.domainConfig.siteUrl + '/?loginbox=show&next=' + encodeURIComponent(location.href)
  137. }
  138. } else {
  139. this.$message.closeAll()
  140. let that = this;
  141. this.$alert('未登录, 前往登录', '提示', {
  142. confirmButtonText: '确定',
  143. center: true,
  144. callback: action => {
  145. if (that.$deviceType.app) {
  146. location.href = "proginn://login?backToPage=true";
  147. } else {
  148. location.href = that.$store.state.domainConfig.siteUrl + '/?loginbox=show&next=' + encodeURIComponent(location.href)
  149. }
  150. }
  151. })
  152. }
  153. },
  154. noCompetence(title = "没有权限") {
  155. this.$alert(title, '提示', {
  156. confirmButtonText: '确定',
  157. center: true,
  158. callback: action => {
  159. location.go(-1)
  160. }
  161. })
  162. },
  163. async updateUserInfo() {
  164. let res = await this.$axios.$get('/api/user/getInfo');
  165. if (res && res.data) {
  166. this.$store.commit('updateUserinfo', {
  167. userinfo: res.data || {}
  168. })
  169. }
  170. },
  171. _toast(msg, type) {
  172. if (this.$deviceType.isMobile()) {
  173. this.$toast(msg)
  174. return
  175. }
  176. if (this.$message[type||'success']) {
  177. this.$message[type||'success'](msg||'')
  178. }
  179. }
  180. }
  181. })