pc.vue 4.0 KB

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