index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import Vue from 'vue';
  2. import VueRouter from 'vue-router';
  3. import store from '@/store/index';
  4. const jwt = require('jsonwebtoken');
  5. Vue.use(VueRouter);
  6. const admin = [
  7. {
  8. path: '/admin/index',
  9. meta: { title: '统计报表', isleftarrow: false },
  10. component: () => import('../views/adminCenter/index.vue'),
  11. },
  12. {
  13. path: '/adminRefute',
  14. meta: { title: '科技文章', isleftarrow: false },
  15. component: () => import('../views/adminCenter/adminRefute.vue'),
  16. },
  17. {
  18. path: '/adminRefute/edit',
  19. meta: { title: '科技文章', isleftarrow: false },
  20. component: () => import('../views/adminCenter/refute/edit.vue'),
  21. },
  22. {
  23. path: '/adminCommunity',
  24. meta: { title: '社区话题', isleftarrow: false },
  25. component: () => import('../views/adminCenter/adminCommunity.vue'),
  26. },
  27. {
  28. path: '/adminCommunity/edit',
  29. meta: { title: '社区话题', isleftarrow: false },
  30. component: () => import('../views/adminCenter/topic/edit.vue'),
  31. },
  32. {
  33. path: '/adminServe',
  34. meta: { title: '咨询服务', isleftarrow: false },
  35. component: () => import('../views/adminCenter/adminServe.vue'),
  36. },
  37. {
  38. path: '/adminServe/edit',
  39. meta: { title: '咨询服务', isleftarrow: false },
  40. component: () => import('../views/adminCenter/serve/edit.vue'),
  41. },
  42. ];
  43. const routes = [
  44. {
  45. path: '/',
  46. meta: { title: '系统首页', isleftarrow: false },
  47. component: () => import('../views/index.vue'),
  48. },
  49. {
  50. path: '/login',
  51. meta: { title: '管理登陆', isleftarrow: false },
  52. component: () => import('../views/login.vue'),
  53. },
  54. // 科技文章
  55. {
  56. path: '/refute/index',
  57. meta: { title: '科技文章', isleftarrow: false },
  58. component: () => import('../views/refute/index.vue'),
  59. },
  60. {
  61. path: '/refute/detail',
  62. meta: { title: '文章正文', isleftarrow: false },
  63. component: () => import('../views/refute/detail.vue'),
  64. },
  65. // 社区话题
  66. {
  67. path: '/community/index',
  68. meta: { title: '社区话题', isleftarrow: false },
  69. component: () => import('../views/community/index.vue'),
  70. },
  71. {
  72. path: '/community/detail',
  73. meta: { title: '话题正文', isleftarrow: false },
  74. component: () => import('../views/community/detail.vue'),
  75. },
  76. // 咨询服务
  77. {
  78. path: '/service/index',
  79. meta: { title: '咨询服务', isleftarrow: false },
  80. component: () => import('../views/service/index.vue'),
  81. },
  82. {
  83. path: '/service/detail',
  84. meta: { title: '文章正文', isleftarrow: false },
  85. component: () => import('../views/service/detail.vue'),
  86. },
  87. // 引用管理员
  88. ...admin,
  89. ];
  90. const router = new VueRouter({
  91. mode: 'history',
  92. base: process.env.NODE_ENV === 'development' ? '' : process.env.VUE_APP_ROUTER,
  93. routes,
  94. });
  95. router.beforeEach(async (to, form, next) => {
  96. // 检查session中是否有user信息
  97. store.commit('getUser');
  98. const user = store.state.user;
  99. if (!user) {
  100. // 有没有openid
  101. const { openid } = to.query;
  102. if (openid) {
  103. // 使用openid换取用户信息
  104. const res = await store.dispatch('weixin/login', openid);
  105. if (!res) console.warn('未获取到用户信息');
  106. } else {
  107. // 没有openid,很可能是没用微信浏览器打开
  108. console.warn('未获取到用户信息');
  109. }
  110. }
  111. next();
  112. });
  113. router.afterEach(async (to, form) => {
  114. const title = to.meta.title;
  115. const img = 'http://broadcast.waityou24.cn/files/article/icon.png';
  116. const desc = '吉林省计算中心文章发布系统';
  117. const url = encodeURIComponent(location.href.split('#')[0]);
  118. const wx = Vue.prototype.$wx;
  119. const res = await store.dispatch('weixin/jsAuth', { url });
  120. if (res.errcode == '0') {
  121. const { data } = res;
  122. const shareUrl = 'http://broadcast.waityou24.cn/api/article/auth?redirect_uri=' + url;
  123. wx.config({
  124. debug: false,
  125. appId: data.appid,
  126. timestamp: data.timestamp,
  127. nonceStr: data.noncestr,
  128. signature: data.sign,
  129. jsApiList: ['updateAppMessageShareData', 'updateTimelineShareData'],
  130. });
  131. wx.ready(() => {
  132. wx.updateAppMessageShareData({
  133. title: title,
  134. desc: desc,
  135. imgUrl: img,
  136. link: shareUrl,
  137. success: res => {},
  138. error: err => {
  139. alert(JSON.stringify(err));
  140. },
  141. });
  142. wx.updateTimelineShareData({
  143. title: title,
  144. imgUrl: img,
  145. link: shareUrl,
  146. success: res => {},
  147. error: err => {
  148. alert(JSON.stringify(err));
  149. },
  150. });
  151. });
  152. }
  153. });
  154. const originalPush = VueRouter.prototype.push;
  155. VueRouter.prototype.push = function push(location, onResolve, onReject) {
  156. if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject);
  157. return originalPush.call(this, location).catch(err => err);
  158. };
  159. export default router;