pc.vue 4.2 KB

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