pc.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <template>
  2. <view class="pc-page">
  3. <web-view :src="url" @message="handleGetMessage" v-if="url"/>
  4. </view>
  5. </template>
  6. <script>
  7. import {pcBaseUrl} from '../utils/config'
  8. const mapObj=new Map([
  9. ['pages/activity/activity','/activity/list'],
  10. ['pages/pricedriven/pricedriven','/pricedriven'],
  11. ['pages-activity/detail','/activity/detail'],
  12. ['pages/report/report','/report/index'],
  13. ['pages-report/classify','/report/classify'],
  14. ['pages-report/reportList','/report/list'],
  15. ['pages-report/reportDetail','/report/detail'],
  16. ['pages-report/chapterDetail','/report/chapterdetail'],
  17. ['pages-report/specialColumn/list','/report/specialcolumnlist'],
  18. ['pages-report/specialColumn/detail','/report/specialcolumndetail'],
  19. ['pages/video/videoList','/video/list'],
  20. ['pages-sandTable/sandTable','/sandBox/list'],
  21. ['pages/voice/voice','/voice/list'],
  22. ['pages-voice/voiceDetail','/voice/detail'],
  23. ['pages-roadShow/video/list','/roadshow/video/list'],
  24. ['pages-report/reportForVariety/list','/report/varietyreportlist']
  25. ])//map映射小程序页面路径对应h5页面路径
  26. import {apiUserInfo} from '@/api/user'
  27. import {apiGetSceneToParams} from '@/api/common'
  28. export default {
  29. data () {
  30. return {
  31. url:'',
  32. msgObj:{},//{path:小程序页面地址,params:页面参数,title:分享的标题,shareImg:分享的图片}
  33. times:0,//检查token次数
  34. }
  35. },
  36. onLoad(options) {
  37. this.init(options)
  38. },
  39. onShareAppMessage({webViewUrl}) {
  40. // console.log(webViewUrl);
  41. let paramsStr=''
  42. for(const key in this.msgObj.params){
  43. if(!paramsStr){
  44. paramsStr=`${key}=${this.msgObj.params[key]}`
  45. }else{
  46. paramsStr=`${paramsStr}&${key}=${this.msgObj.params[key]}`
  47. }
  48. }
  49. return {
  50. title: this.msgObj.title||'弘则研究',
  51. path: `${this.msgObj.path}?${paramsStr}`,
  52. imageUrl:this.msgObj.shareImg||''
  53. }
  54. },
  55. methods: {
  56. // 获取到用户点击转发时从h5页面传来的参数
  57. handleGetMessage(e){
  58. const data=e.detail.data[e.detail.data.length-1]
  59. console.log('h5传来的数据',data);
  60. this.msgObj=data
  61. },
  62. async init(options){
  63. // 检查token是否有效 超过十次不在检测,提示重启小程序
  64. const res=await apiUserInfo()
  65. this.times++
  66. if(res.code!==200){
  67. console.log('pc页面检查token次:',this.times);
  68. if(this.times<11){
  69. setTimeout(() => {
  70. this.init(options)
  71. }, 1000);
  72. }else{
  73. uni.showToast({
  74. title: '请重启小程序',
  75. icon: 'none'
  76. })
  77. }
  78. return
  79. }
  80. console.log('pc页面onload数据',options);
  81. // 如果是识别海报的则要解密
  82. if(options.scene){
  83. const resScene=await apiGetSceneToParams({scene_key:options.scene})
  84. if(resScene.code===200){
  85. const obj=JSON.parse(resScene.data)
  86. delete options.scene
  87. options={...options,...obj}
  88. console.log('pc页面解密scene数据',options);
  89. }
  90. }
  91. let paramsObj={
  92. ...options,
  93. token:this.$store.state.user.token||uni.getStorageSync("token"),
  94. timestamp:new Date().getTime(),//防止缓存
  95. }
  96. delete paramsObj.xcxPath
  97. console.log('要处理的参数',paramsObj);
  98. let paramsObjStr=''
  99. for (const key in paramsObj) {
  100. if(!paramsObjStr){
  101. paramsObjStr=`${key}=${paramsObj[key]}`
  102. }else{
  103. paramsObjStr=`${paramsObjStr}&${key}=${paramsObj[key]}`
  104. }
  105. }
  106. console.log('拼接字符串:',paramsObjStr);
  107. this.url=`${pcBaseUrl}${mapObj.get(decodeURIComponent(options.xcxPath))||'/'}?${paramsObjStr}#wechat_redirect`
  108. }
  109. },
  110. onShow() {
  111. uni.hideHomeButton({
  112. fail:(e)=>{
  113. console.log(e);
  114. }
  115. })
  116. }
  117. }
  118. </script>