pc.vue 5.3 KB

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