pc.vue 5.0 KB

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