pc.vue 4.0 KB

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