pc.vue 5.4 KB

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