不能正常的代码
<template>
<view class="container" :style="{height:pageHeight+'px'}">
<view v-if="userStore.userInfo.is_examine===0 || userStore.userInfo.status ===0" class="contentEx"
:style="{height:pageHeight+'px'}">
<image src="@/static/images/login.png" class="loginImage"></image>
<view class="loginText">
<text v-if="userStore.userInfo.status ===0" class="statusText">
你的账号已被禁用,无法看课</text>
<text v-else class="statusText">注册成功,等待群管审核</text>
</view>
</view>
<view v-else-if="msg" class="contentError" :style="{height:pageHeight+'px'}">
<image src="@/static/images/login.png" class="loginImage"></image>
<view class="loginText">
<text class="statusText">{{msg}}</text>
</view>
</view>
<view v-else class="content" :style="{height:pageHeight+'px'}">
<block v-if="courseInfo">
<!-- 1横屏 2竖屏 -->
<block v-if="courseInfo.video_type===1">
<video ref="videoContent" class="videoHorizontal" :src="courseInfo.video_file"
:poster="courseInfo.video_image"></video>
<view class="videoTab">
<view :class="['tab-item',tabAvtive===index?'tab-item-active':'']"
v-for="(item,index) in tabList" :key="index" @click="changeTabItem(index)">
<text
:class="['tab-item-text',tabAvtive===index?'tab-item-text-active':'']">{{item.name}}</text>
</view>
</view>
<swiper :current="tabAvtive" class="swiperContainer" :style="{height:swiperHeight+'px'}"
@change="changeSwiper">
<swiper-item v-for="(item,index) in tabList" :key="index" class="swiperContainer"
:style="{height:swiperHeight+'px'}">
<block v-if="item.value===1">
<answer :questions="courseInfo.question" @submit="onSubmit"></answer>
</block>
<block v-else>
<web-view ref="webview" :style="{height: swiperHeight + 'px',width: '690rpx'}"
src="/hybrid/html/index.html" @onPostMessage="handerMessage"
@message="handerMessage"></web-view>
<view v-if="!isWebviewInit" :style="{height: swiperHeight + 'px',width: '690rpx'}"
class="webLoading">
<view class="loading-spinner"></view>
<view class="loading-text">加载中...</view>
</view>
</block>
</swiper-item>
</swiper>
</block>
<block v-else></block>
</block>
</view>
</view>
</template>
<script>
import {
useUserStore
} from '@/store/userStore'
import {
useToast
} from 'wot-design-uni'
import {
getCourseDetail,
getAliIMParams
} from "@/api/course.js"
export default {
data() {
return {
pageHeight: 0,
swiperHeight: 0,
train_lesson_id: "",
rpxTopx: 0.5,
msg: "",
isWebviewInit: false,
courseInfo: null,
tabList: [{
name: "答题",
value: 1
}, {
name: "聊天",
value: 2
}],
tabAvtive: 0,
redMoney: 0,
initialTime: 0,
playEnd: false
}
},
computed: {
userStore() {
return useUserStore()
},
toast() {
return useToast()
}
},
onLoad(options) {
this.train_lesson_id = options.id
let system = uni.getSystemInfoSync()
this.pageHeight = system.windowHeight
this.rpxTopx = system.windowWidth / system.windowHeight
this.swiperHeight = this.pageHeight - 630 * this.rpxTopx
},
mounted() {
if (this.userStore.userInfo.is_examine == 0) {
this.updataUserInfo();
return;
}
this.getParamCourseInfo();
},
methods: {
changeTabItem(index) {
this.tabAvtive = index
},
changeSwiper(e) {
this.tabAvtive = e.detail.current
},
handerMessage(e) {
let obj = e.detail.data.length > 0 ? e.detail.data[0] : null
console.log("收到网页的消息", obj)
if (obj) {
switch (obj.type) {
case "init":
this.isWebviewInit = true
this.getImData()
break;
default:
break;
}
}
},
updataUserInfo(flag = true) {
this.userStore.refreshUserInfo((userInfo) => {
if (userInfo.is_examine != 0 && flag) {
this.getParamCourseInfo()
}
})
},
getParamCourseInfo() {
getCourseDetail({
train_lesson_id: this.train_lesson_id
}).then((res) => {
if (res.code == 1) {
res.data.question.sort((a, b) => {
return a.question_id - b.question_id;
});
res.data.question.forEach((q) => {
q.question_option_json = JSON.parse(q.question_option_json);
});
this.courseInfo = res.data;
this.redMoney = res.data.red_money
if (!this.playEnd) {
this.initialTime = parseInt(this.courseInfo.progress);
}
console.log(this.courseInfo)
} else {
this.msg = res.msg
if (res.msg.includes('审核')) {
this.updataUserInfo(false);
}
}
}).catch((error) => {
console.error(error);
console.log("获取课程信息错误", error.errMsg || error.msg);
})
},
getImData() {
getAliIMParams({
feature: "im_join_room",
room_id: this.courseInfo.train_lesson_id,
}).then((res) => {
if (res.code == 1) {
this.sendWebviewMessage({
type: "chatInit",
...res.data,
userInfo: this.userStore.userInfo,
room_id: String(this.courseInfo.train_lesson_id)
})
} else {
this.toast.error(res.msg)
}
}).catch((err) => {
console.log("获取IM数据错误", err);
});
},
sendWebviewMessage(data) {
console.log(this.$refs.webview)
this.$refs.webview.evalJS(`msgFromUniapp('${JSON.stringify(data)}')`)
},
onSubmit(e) {
}
}
}
</script>
能正常的代码
<template>
<view class="container" :style="{height:pageHeight+'px'}">
<web-view ref="webview" :style="{height: swiperHeight + 'px',width: '690rpx'}"
src="/hybrid/html/index.html" @onPostMessage="handerMessage"
@message="handerMessage"></web-view>
<view v-if="userStore.userInfo.is_examine===0 || userStore.userInfo.status ===0" class="contentEx"
:style="{height:pageHeight+'px'}">
<image src="@/static/images/login.png" class="loginImage"></image>
<view class="loginText">
<text v-if="userStore.userInfo.status ===0" class="statusText">
你的账号已被禁用,无法看课</text>
<text v-else class="statusText">注册成功,等待群管审核</text>
</view>
</view>
<view v-else-if="msg" class="contentError" :style="{height:pageHeight+'px'}">
<image src="@/static/images/login.png" class="loginImage"></image>
<view class="loginText">
<text class="statusText">{{msg}}</text>
</view>
</view>
<view v-else class="content" :style="{height:pageHeight+'px'}">
<block v-if="courseInfo">
<!-- 1横屏 2竖屏 -->
<block v-if="courseInfo.video_type===1">
<video ref="videoContent" class="videoHorizontal" :src="courseInfo.video_file"
:poster="courseInfo.video_image"></video>
<view class="videoTab">
<view :class="['tab-item',tabAvtive===index?'tab-item-active':'']"
v-for="(item,index) in tabList" :key="index" @click="changeTabItem(index)">
<text
:class="['tab-item-text',tabAvtive===index?'tab-item-text-active':'']">{{item.name}}</text>
</view>
</view>
<swiper :current="tabAvtive" class="swiperContainer" :style="{height:swiperHeight+'px'}"
@change="changeSwiper">
<swiper-item v-for="(item,index) in tabList" :key="index" class="swiperContainer"
:style="{height:swiperHeight+'px'}">
<block v-if="item.value===1">
<answer :questions="courseInfo.question" @submit="onSubmit"></answer>
</block>
<block v-else>
<!-- <web-view ref="webview" :style="{height: swiperHeight + 'px',width: '690rpx'}"
src="/hybrid/html/index.html" @onPostMessage="handerMessage"
@message="handerMessage"></web-view> -->
<view v-if="!isWebviewInit" :style="{height: swiperHeight + 'px',width: '690rpx'}"
class="webLoading">
<view class="loading-spinner"></view>
<view class="loading-text">加载中...</view>
</view>
</block>
</swiper-item>
</swiper>
</block>
<block v-else></block>
</block>
</view>
</view>
</template>
就把webview拿出来了就能正常了