pc.vue 3.1 KB

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