pc.vue 5.4 KB

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