common.js 5.6 KB

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