common.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. return userInfo;
  53. },
  54. async cnzz(category,action,status="") {
  55. let home_page_type="";
  56. if(Cookies.get('home_page_type')==1)
  57. {
  58. home_page_type="企业方";
  59. }
  60. if(Cookies.get('home_page_type')==2)
  61. {
  62. home_page_type="开发者";
  63. }
  64. else
  65. {
  66. home_page_type="游客";
  67. }
  68. if(process.env.NODE_ENV=="production" && this.$store.state.domainConfig.siteUrl=="https://www.proginn.com")
  69. {
  70. this.$axios.$get("https://proginn-click.cn-hangzhou.log.aliyuncs.com/logstores/click/track_ua.gif?APIVersion=0.6.0&type=1&action="+action+"&category="+category+"&home_page_type="+home_page_type+"&result="+status);
  71. }
  72. },
  73. async needLoginQrcode() {
  74. const userInfo = await this.getUserInfo();
  75. if (!userInfo || !userInfo.nickname) {
  76. location.href = this.$store.state.domainConfig.siteUrl + '/?loginbox=show&scan=1&next=' + encodeURIComponent(location.href)
  77. }
  78. },
  79. async checkLogin(goLogin = false) {
  80. const userInfo = await this.getUserInfo();
  81. if (!userInfo || !userInfo.nickname) {
  82. if(!goLogin) this.$message.error('请先登录!')
  83. if (goLogin) {
  84. const {
  85. app
  86. } = this.$deviceType
  87. if (app) {
  88. location.href = 'proginn://login?backToPage=true'
  89. } else if (location.origin.indexOf('local') !== 1 || location.origin.indexOf('dev') !== 1) {
  90. location.href = this.$store.state.domainConfig.siteUrl + '/?loginbox=show&next=' + encodeURIComponent(location.href)
  91. } else {
  92. location.href = this.$store.state.domainConfig.siteUrl + '/?loginbox=show&next=' + encodeURIComponent(location.href)
  93. }
  94. }
  95. return false
  96. }
  97. return true
  98. },
  99. async needVerify() {
  100. const userInfo = await this.getUserInfo();
  101. // 1是待审核,2审核通过,3是拒绝
  102. if (userInfo.realname_verify_status !== '2') {
  103. this.$message.error('根据互联网相关法规要求,请先完成实名认证');
  104. this.goVerify();
  105. }
  106. },
  107. async checkMobile() {
  108. const userInfo = await this.getUserInfo();
  109. // 1是待审核,2审核通过,3是拒绝
  110. if (userInfo.mobile == '') {
  111. this.$message.error('根据互联网相关法规要求,请先完成手机认证');
  112. this.goVerify();
  113. return false;
  114. }
  115. return true;
  116. },
  117. async getUserInfo() {
  118. let res = this.$store.state.userinfo;
  119. if (!res) {
  120. const result = await this.$axios.$get(
  121. `/api/user/getInfo`
  122. );
  123. res = result.data;
  124. }
  125. console.log("用户登录",res);
  126. return res;
  127. },
  128. goVerify() {
  129. location.href = this.$store.state.domainConfig.siteUrl + '/setting/user';
  130. },
  131. goHome() {
  132. location.href = this.$store.state.domainConfig.siteUrl;
  133. },
  134. goLogin(e, noAlert) {
  135. if (noAlert) {
  136. if (this.$deviceType.app) {
  137. location.href = "proginn://login?backToPage=true";
  138. } else {
  139. location.href = this.$store.state.domainConfig.siteUrl + '/?loginbox=show&next=' + encodeURIComponent(location.href)
  140. }
  141. } else {
  142. this.$message.closeAll()
  143. let that = this;
  144. this.$alert('未登录, 前往登录', '提示', {
  145. confirmButtonText: '确定',
  146. center: true,
  147. callback: action => {
  148. if (that.$deviceType.app) {
  149. location.href = "proginn://login?backToPage=true";
  150. } else {
  151. location.href = that.$store.state.domainConfig.siteUrl + '/?loginbox=show&next=' + encodeURIComponent(location.href)
  152. }
  153. }
  154. })
  155. }
  156. },
  157. noCompetence(title = "没有权限") {
  158. this.$alert(title, '提示', {
  159. confirmButtonText: '确定',
  160. center: true,
  161. callback: action => {
  162. location.go(-1)
  163. }
  164. })
  165. },
  166. async updateUserInfo() {
  167. let res = await this.$axios.$get('/api/user/getInfo');
  168. if (res && res.data) {
  169. this.$store.commit('updateUserinfo', {
  170. userinfo: res.data || {}
  171. })
  172. }
  173. },
  174. _toast(msg, type) {
  175. if (this.$deviceType.isMobile()) {
  176. this.$toast(msg)
  177. return
  178. }
  179. if (this.$message[type||'success']) {
  180. this.$message[type||'success'](msg||'')
  181. }
  182. }
  183. }
  184. })