pc.vue 4.1 KB

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