call-center.vue 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <template>
  2. <view class="call-center">
  3. <view v-if="contacts.length" class="contact-list">
  4. <view class="contact-card" v-for="contact in contacts" :key="contact.id">
  5. <view class="contact-info">
  6. <image class="contact-avatar" :src="contact.avatar" mode="aspectFill" />
  7. <view class="contact-meta">
  8. <text class="contact-name">{{ contact.username }}</text>
  9. <text class="contact-desc">{{ contact.className }}</text>
  10. </view>
  11. </view>
  12. <view class="action-buttons">
  13. <view class="action-btn" @click="startVoiceCall(contact)">
  14. <view class="action-btn-icon">
  15. <image src="/static/icon/audio.png" mode="aspectFit" />
  16. </view>
  17. <text>音频</text>
  18. </view>
  19. <view class="action-btn" @click="startVideoCall(contact)">
  20. <view class="action-btn-icon">
  21. <image src="/static/icon/video.png" mode="aspectFit" />
  22. </view>
  23. <text>视频</text>
  24. </view>
  25. <view class="action-btn" :class="{ 'message-point': contact.hasPoints }" @click="goToMessage(contact)">
  26. <view class="action-btn-icon">
  27. <image src="/static/icon/message.png" mode="aspectFit" />
  28. </view>
  29. <text>留言</text>
  30. </view>
  31. </view>
  32. </view>
  33. </view>
  34. <view v-else class="empty-tip">
  35. <text>暂未关联可呼叫的设备</text>
  36. </view>
  37. </view>
  38. </template>
  39. <script>
  40. import { getImageUrl } from '@/utils/util'
  41. const CALL_END_STORAGE_KEY = 'parentLastCallInfo'
  42. const CALL_END_PAGE_URL = '/pages/parent/call-center' // 通话结束返回通话中心页
  43. let wmpfVoip = null
  44. try {
  45. if (typeof requirePlugin === 'function') {
  46. wmpfVoip = requirePlugin('wmpf-voip').default
  47. }
  48. } catch (error) {
  49. console.warn('wmpf-voip 插件暂不可用', error)
  50. }
  51. const miniprogramState = (() => {
  52. if (typeof wx === 'undefined' || typeof wx.getAccountInfoSync !== 'function') {
  53. return 'formal'
  54. }
  55. const accountInfo = wx.getAccountInfoSync()
  56. if (accountInfo && accountInfo.miniProgram) {
  57. const platform = { develop: 'developer', trial: 'trial', release: 'formal' }
  58. return platform[accountInfo.miniProgram.envVersion]
  59. }
  60. return 'formal'
  61. })()
  62. const DEFAULT_AVATAR = '/static/logo.png'
  63. const DEFAULT_CLASS_NAME = '所属班级'
  64. export default {
  65. data() {
  66. return {
  67. parentInfo: {
  68. xm: '张小同',
  69. openid: 'oKFvD4pG_iKBJR0wfjiDicm7fjaA'
  70. // openid: 'oKFvD4jogQe9DTDRNgFjW5ybobEU'
  71. },
  72. contacts: [
  73. {
  74. id: 'stu-001',
  75. username: '张小小同',
  76. className: '三年级二班',
  77. avatar: '/static/images/yishuzhao_nan.svg',
  78. deviceSn: 'A100006B623E86',
  79. pushToken: 'BgAADLbeQ1s4jDjapuGe4HKnrI-I-5D57-TLABuqXrB0saDv-fuzNlT-32rTadDMrLrpNLxcUMe1y6FlESz7-EVGC4kjHaK_P7WZQUfEhHzxP55ndPPrYOSzvq6aez115InzFIOM1PZhdNS5U6pItMBid7zqVX4',
  80. }
  81. ],
  82. currentContact: null,
  83. _voipEndPathSet: false,
  84. _voipEventRegistered: false,
  85. defaultAvatar: DEFAULT_AVATAR,
  86. defaultClassName: DEFAULT_CLASS_NAME
  87. }
  88. },
  89. onLoad() {
  90. this.initParentInfo()
  91. this.registerVoipEvent()
  92. },
  93. methods: {
  94. initParentInfo() {
  95. let info = uni.getStorageSync('userInfo')
  96. if (typeof info === 'string') {
  97. try {
  98. info = JSON.parse(info)
  99. } catch (error) {
  100. info = null
  101. }
  102. }
  103. if (!info) {
  104. this.loadContactList()
  105. return
  106. }
  107. this.parentInfo = {
  108. ...this.parentInfo,
  109. ...info
  110. }
  111. this.loadContactList()
  112. },
  113. loadContactList() {
  114. // 统一处理联系人来源:仅格式化 data 中已有的联系人,保持展示/拨号结构一致
  115. const formatContact = (item = {}, index = 0) => ({
  116. id: item.id || `contact-${index}`,
  117. username: item.username || item.name || '学生',
  118. avatar: this.formatAvatar(item.avatar || item.yszwj),
  119. className: item.className || item.bjmc || item.bmmc || item.bjmcName || this.defaultClassName,
  120. deviceSn: item.deviceSn || item.sn || item.devId || '',
  121. pushToken: item.pushToken || item.voipToken || '',
  122. deviceName: item.deviceName || item.username || item.name || '家庭设备',
  123. hasPoints: !!item.hasPoints,
  124. })
  125. this.contacts = (this.contacts || []).map(formatContact)
  126. },
  127. formatAvatar(value) {
  128. if (!value) return this.defaultAvatar
  129. if (typeof value === 'string' && (value.startsWith('http') || value.startsWith('https') || value.startsWith('data:'))) {
  130. return value
  131. }
  132. return getImageUrl(value)
  133. },
  134. async startVoiceCall(contact) {
  135. await this.startCall(contact, 'voice')
  136. },
  137. async startVideoCall(contact) {
  138. await this.startCall(contact, 'video')
  139. },
  140. async startCall(contact, roomType) {
  141. if (!wmpfVoip) {
  142. uni.showToast({ title: '通话能力暂不可用', icon: 'none' })
  143. return
  144. }
  145. const callerId = this.parentInfo?.openid
  146. if (!callerId) {
  147. uni.showToast({ title: '缺少家长身份标识', icon: 'none' })
  148. return
  149. }
  150. if (!contact?.deviceSn) {
  151. uni.showToast({ title: '未找到可呼叫的设备编号', icon: 'none' })
  152. return
  153. }
  154. if (!contact?.pushToken) {
  155. uni.showToast({ title: '设备未上报通话凭证', icon: 'none' })
  156. return
  157. }
  158. uni.showLoading({ title: '呼叫中...', mask: true })
  159. this.currentContact = contact
  160. const callInfo = {
  161. name: contact.username || '家庭设备',
  162. avatar: contact.avatar || '/static/logo.png',
  163. duration: 0,
  164. time: new Date().toLocaleString(),
  165. type: roomType,
  166. status: '呼叫中'
  167. }
  168. uni.setStorageSync(CALL_END_STORAGE_KEY, callInfo)
  169. this.setVoipEndPagePath()
  170. console.log(callerId)
  171. console.log(this.parentInfo?.xm)
  172. console.log(contact.deviceSn)
  173. console.log(contact.username)
  174. console.log(contact.pushToken)
  175. console.log(miniprogramState)
  176. try {
  177. const res = await wmpfVoip.initByCaller({
  178. roomType,
  179. caller: {
  180. id: callerId,
  181. name: this.parentInfo?.xm || '家长'
  182. },
  183. listener: {
  184. id: contact.deviceSn,
  185. name: contact.username
  186. },
  187. businessType: 2,
  188. voipToken: contact.pushToken,
  189. miniprogramState: "formal"
  190. })
  191. if (res.isSuccess) {
  192. uni.hideLoading()
  193. uni.redirectTo({ url: wmpfVoip.CALL_PAGE_PATH })
  194. return
  195. }
  196. uni.hideLoading()
  197. console.error('呼叫失败', res)
  198. uni.showToast({ title: '呼叫失败', icon: 'error' })
  199. } catch (error) {
  200. uni.hideLoading()
  201. console.error('通话异常:', error)
  202. uni.showToast({ title: '发起通话失败', icon: 'none' })
  203. }
  204. },
  205. setVoipEndPagePath(forceUpdate = false) {
  206. if (this._voipEndPathSet && !forceUpdate) return
  207. if (!wmpfVoip) return
  208. const callInfo = uni.getStorageSync(CALL_END_STORAGE_KEY) || {}
  209. const options = callInfo.name ?
  210. `name=${encodeURIComponent(callInfo.name)}&avatar=${encodeURIComponent(callInfo.avatar)}&duration=${callInfo.duration || 0}&type=${callInfo.type || 'voice'}&status=${encodeURIComponent(callInfo.status || '通话已结束')}` : ''
  211. wmpfVoip.setVoipEndPagePath({
  212. url: CALL_END_PAGE_URL,
  213. key: 'Call',
  214. options,
  215. routeType: 'redirectTo'
  216. })
  217. this._voipEndPathSet = true
  218. },
  219. registerVoipEvent() {
  220. if (this._voipEventRegistered || !wmpfVoip) return
  221. wmpfVoip.onVoipEvent((event) => {
  222. const eventName = event.eventName
  223. const hangupEvent = ['hangUpVoip', 'endVoip']
  224. const cancelEvent = ['cancelVoip']
  225. const timeoutEvent = ['timeout']
  226. const rejectEvent = ['rejectVoip']
  227. const callInfo = uni.getStorageSync(CALL_END_STORAGE_KEY) || {}
  228. if (hangupEvent.includes(eventName)) {
  229. callInfo.duration = event.data?.keepTime || 0
  230. callInfo.status = event.data?.keepTime > 0 ? '通话已结束' : '未接通'
  231. } else if (cancelEvent.includes(eventName)) {
  232. callInfo.duration = 0
  233. callInfo.status = '已取消'
  234. } else if (timeoutEvent.includes(eventName)) {
  235. callInfo.duration = 0
  236. callInfo.status = '未接听'
  237. } else if (rejectEvent.includes(eventName)) {
  238. callInfo.duration = 0
  239. callInfo.status = '已拒绝'
  240. }
  241. if ([...hangupEvent, ...cancelEvent, ...timeoutEvent, ...rejectEvent].includes(eventName)) {
  242. callInfo.endType = eventName
  243. callInfo.endTime = Date.now()
  244. uni.hideLoading()
  245. uni.setStorageSync(CALL_END_STORAGE_KEY, callInfo)
  246. this.setVoipEndPagePath(true)
  247. }
  248. })
  249. this._voipEventRegistered = true
  250. },
  251. goToMessage(contact) {
  252. const params = {
  253. role: 'parent'
  254. }
  255. if (contact?.studentId) {
  256. params.studentId = contact.studentId
  257. }
  258. if (contact?.id) {
  259. params.contactId = contact.id
  260. }
  261. const query = Object.keys(params)
  262. .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
  263. .join('&')
  264. uni.navigateTo({
  265. url: `/pages/parent/message?${query}`
  266. })
  267. }
  268. }
  269. }
  270. </script>
  271. <style scoped>
  272. .call-center {
  273. min-height: 100vh;
  274. padding: 32rpx;
  275. background: #f5f5f5;
  276. box-sizing: border-box;
  277. }
  278. .contact-list {
  279. display: flex;
  280. flex-direction: column;
  281. gap: 24rpx;
  282. }
  283. .contact-card {
  284. background: #fff;
  285. border-radius: 12rpx;
  286. border: 2rpx solid #e4e7ed;
  287. padding: 24rpx;
  288. display: flex;
  289. justify-content: space-between;
  290. align-items: center;
  291. box-shadow: 0 6rpx 12rpx rgba(0, 0, 0, 0.05);
  292. }
  293. .contact-info {
  294. display: flex;
  295. align-items: center;
  296. }
  297. .contact-avatar {
  298. width: 96rpx;
  299. height: 96rpx;
  300. border-radius: 8rpx;
  301. margin-right: 16rpx;
  302. background: #f0f0f0;
  303. }
  304. .contact-meta {
  305. display: flex;
  306. flex-direction: column;
  307. gap: 8rpx;
  308. }
  309. .contact-name {
  310. font-size: 30rpx;
  311. color: #333;
  312. font-weight: 500;
  313. }
  314. .contact-desc {
  315. font-size: 26rpx;
  316. color: #8c8c8c;
  317. }
  318. .empty-tip {
  319. margin-top: 60rpx;
  320. text-align: center;
  321. color: #888;
  322. font-size: 28rpx;
  323. }
  324. .action-buttons {
  325. display: flex;
  326. gap: 18rpx;
  327. }
  328. .action-btn {
  329. display: flex;
  330. flex-direction: column;
  331. align-items: center;
  332. gap: 6rpx;
  333. }
  334. .action-btn-icon {
  335. width: 56rpx;
  336. height: 56rpx;
  337. display: flex;
  338. align-items: center;
  339. justify-content: center;
  340. background: #ffffff;
  341. border: 2rpx solid #e0e0e0;
  342. border-radius: 10rpx;
  343. }
  344. .action-btn-icon image {
  345. width: 32rpx;
  346. height: 32rpx;
  347. }
  348. .action-btn text {
  349. font-size: 24rpx;
  350. color: #333;
  351. }
  352. .message-point {
  353. position: relative;
  354. }
  355. .message-point::after {
  356. content: '';
  357. position: absolute;
  358. width: 12rpx;
  359. height: 12rpx;
  360. background-color: #eb6100;
  361. border-radius: 50%;
  362. top: -4rpx;
  363. right: -4rpx;
  364. }
  365. </style>