pc.vue 4.1 KB

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