common.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import Vue from 'vue'
  2. // import http from '@/plugins/http'
  3. // mixin
  4. Vue.mixin({
  5. async fetch({ $axios, store, req }) {
  6. let headers = req && req.headers
  7. let res = await $axios.$get('/api/user/getInfo', { headers });
  8. if (res && res.data) {
  9. store.commit('updateUserinfo', { userinfo: res.data || {} })
  10. }
  11. },
  12. components: {},
  13. data() {
  14. return {}
  15. },
  16. mounted() {
  17. },
  18. computed: {
  19. userinfo() {
  20. return this.$store.state.userinfo
  21. },
  22. hasLogined() {
  23. return !!this.userinfo.uid
  24. },
  25. },
  26. methods: {
  27. async needLogin() {
  28. const userInfo = await this.getUserInfo();
  29. if (!userInfo || !userInfo.nickname) {
  30. this.goLogin();
  31. }
  32. },
  33. async checkLogin(goLogin = false) {
  34. const userInfo = await this.getUserInfo();
  35. if (!userInfo || !userInfo.nickname) {
  36. this.$message.error('请先登录!')
  37. if (goLogin) {
  38. const { app } = this.$deviceType
  39. if (app) {
  40. location.href = 'proginn://login'
  41. } else if (location.origin.indexOf('local') !== 1 || location.origin.indexOf('dev') !== 1 ) {
  42. location.href = 'https://dev.test.proginn.com/?loginbox=show'
  43. } else {
  44. location.href = 'https://www.proginn.com/?loginbox=show'
  45. }
  46. }
  47. return false
  48. }
  49. return true
  50. },
  51. async needVerify() {
  52. const userInfo = await this.getUserInfo();
  53. // 1是待审核,2审核通过,3是拒绝
  54. if (userInfo.realname_verify_status !== '2') {
  55. this.$message.error('根据互联网相关法规要求,请先完成实名认证')
  56. this.goVerify();
  57. }
  58. },
  59. async getUserInfo() {
  60. let res = this.$store.state.userinfo;
  61. if (!res) {
  62. const result = await this.$axios.$get(
  63. `/api/user/getInfo`
  64. );
  65. res = result.data;
  66. }
  67. return res;
  68. },
  69. goVerify() {
  70. location.href = 'https://www.proginn.com/setting/user';
  71. },
  72. goHome() {
  73. location.href = 'https://www.proginn.com/';
  74. },
  75. goLogin(e, noAlert) {
  76. if (noAlert) {
  77. location.href = `https://www.proginn.com/?loginbox=show`
  78. } else {
  79. this.$message.closeAll()
  80. this.$alert('未登录, 前往登录', '提示', {
  81. confirmButtonText: '确定',
  82. callback: action => {
  83. location.href = `/?loginbox=show`
  84. }
  85. })
  86. }
  87. },
  88. noCompetence(title = "没有权限") {
  89. this.$alert(title, '提示', {
  90. confirmButtonText: '确定',
  91. callback: action => {
  92. location.go(-1)
  93. }
  94. })
  95. },
  96. async updateUserInfo() {
  97. let res = await this.$axios.$get('/api/user/getInfo');
  98. console.log('res', res.data)
  99. if (res && res.data) {
  100. this.$store.commit('updateUserinfo', { userinfo: res.data || {} })
  101. }
  102. },
  103. }
  104. })