WXtrialInterface.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. import request from './request.js'
  2. import { clickCopyText, pasteText } from './common.js'
  3. // 禁止双击缩放
  4. document.addEventListener('dblclick', function (e) {
  5. e.preventDefault();
  6. });
  7. const { Dialog, Toast } = vant
  8. Toast.setDefaultOptions({ duration: 2000 });
  9. // 从 CLOUD_GAME_SDK 结构中解构必要的函数和常量
  10. const RtcEngineSDK = window.rtc_sdk.CloudGameSdk
  11. // 业务通道定时标识
  12. let doConnectDirectivesIntervalerPing = null
  13. // 获取云机数据定时标识
  14. let getUserCardInfoTimerInterval = null
  15. let getUserCardInfoRequestNum = 1
  16. let doConnectDirectivesRequestNum = 1
  17. let doConnectDirectivesTimerInterval = null
  18. // 触碰间隔定时标识
  19. let noOperationSetTimeoutTimeInterval = null
  20. let noOperationSetIntervalTimeInterval = null
  21. // 倒计时定时标识
  22. let countdownTimeInterval = null
  23. // let vc = new VConsole()
  24. const app = new Vue({
  25. el: '#app',
  26. data: {
  27. // 底部按钮
  28. footerBtn: [{
  29. key: 'task',
  30. img: '../static/img/wx/gengduo_icon.png'
  31. }, {
  32. key: 'home',
  33. img: '../static/img/wx/home_icon.png'
  34. }, {
  35. key: 'onBack',
  36. img: '../static/img/wx/fanhui_icon.png'
  37. }],
  38. // 宽高
  39. width: 0,
  40. height: 0,
  41. // webRtc实例
  42. engine: {},
  43. // 横竖屏幕 false是竖
  44. isLandscape: false,
  45. // 悬浮球位置
  46. levitatedSpherePositionData: {},
  47. // 右侧弹窗
  48. levitatedSphereVisible: false,
  49. // 清晰度数据
  50. definitionList: [{
  51. name: '高清',
  52. value: 2800
  53. }, {
  54. name: '标清',
  55. value: 2200,
  56. }, {
  57. name: '极速',
  58. value: 1800,
  59. }],
  60. // 选中的清晰度
  61. definitionValue: '',
  62. // 分辨率
  63. resolutionRatioVisible: false,
  64. resolutionRatioList: [],
  65. // 需要用到的参数
  66. parametersData: {},
  67. // 屏幕分辨率
  68. phoneSize: {},
  69. // 业务指令通道实例
  70. doConnectDirectivesWs: null,
  71. // 粘贴版
  72. pasteVersionVisible: false,
  73. pasteVersionList: [],
  74. // 复制内容
  75. copyTextValue: '',
  76. copyTextVisible: false,
  77. // 卡数据
  78. userCardInfoData: {},
  79. // 是否显示计时
  80. timingVisible: false,
  81. // 计费规则
  82. billingRulesVisible: false,
  83. applyRecommendVisible: false,
  84. // 是否是启动不操作自动退出云机功能
  85. isFiringNoOperationSetTimeout: false,
  86. // 超过指定触碰时间的弹窗
  87. noOperationSetTimeoutTimeVisible: false,
  88. // 超过指定触碰时间的弹窗文案
  89. confirmButtonText: '',
  90. // 云机剩余时长
  91. countdownTime: 0,
  92. // 是否支持webRTC
  93. isSupportRtc: !!(
  94. typeof RTCPeerConnection !== 'undefined' &&
  95. typeof RTCIceCandidate !== 'undefined' &&
  96. typeof RTCSessionDescription !== 'undefined'
  97. ),
  98. // 推荐列表
  99. recommendList: [],
  100. layoutViewWidth: null,
  101. layoutViewHeight: null,
  102. // 是否显示video
  103. isShowVideo: false,
  104. plugFlowStartTime: null,
  105. obtainCardInfoStartTime: null
  106. },
  107. created() {
  108. this.initConfig()
  109. },
  110. mounted() {
  111. this.getUserCardInfo()
  112. },
  113. computed: {
  114. // 右侧弹框退出相关按钮
  115. exitList() {
  116. let arr = [{
  117. name: '剪贴版',
  118. key: "shearplate",
  119. img: '../static/img/wx/jianqieban_icon.png'
  120. }, {
  121. name: '退出',
  122. key: 'signout',
  123. img: '../static/img/wx/tuichu_icon.png'
  124. }]
  125. if ([1, 2, 3].includes(+this.parametersData.userCardType)) {
  126. arr.push({
  127. name: '退出并下机',
  128. key: 'dormant',
  129. img: '../static/img/wx/tuichu_icon.png'
  130. })
  131. }
  132. return arr
  133. },
  134. rtcMediaPlayerStyle() {
  135. let obj = {
  136. objectFit: "fill"
  137. }
  138. if (this.isLandscape) {
  139. obj = {
  140. width: `${this.layoutViewHeight}px`,
  141. height: `${this.layoutViewWidth}px`,
  142. left: '50%',
  143. top: '50%',
  144. transform: 'translate(-50%, -50%) rotate(90deg)'
  145. }
  146. }
  147. return obj
  148. }
  149. },
  150. methods: {
  151. // 初始化
  152. initConfig() {
  153. // 获取窗口尺寸
  154. this.getInitSize()
  155. let levitatedSpherePositionData = localStorage.getItem('levitatedSpherePositionData')
  156. let definitionValue = localStorage.getItem('definitionValue')
  157. // 悬浮球位置
  158. this.levitatedSpherePositionData = levitatedSpherePositionData ? JSON.parse(levitatedSpherePositionData) : { right: '15px', top: '15px' }
  159. // 清晰度
  160. this.definitionValue = definitionValue ? +definitionValue : 2200
  161. // 获取参数
  162. this.parametersData = getParameters()
  163. let { token, validTime } = this.parametersData
  164. // 给api增加需要的参数
  165. request.defaults.headers.Authorization = token
  166. request.defaults.headers.versionname = '5.9.0'
  167. window.onresize = () => {
  168. this.getInitSize()
  169. }
  170. },
  171. // 连接webRTC
  172. connectWebRtc() {
  173. // console.time('获取推流响应消耗时间:')
  174. this.plugFlowStartTime = +new Date()
  175. const { sn: topic, cardToken: authToken, internetHttps, internetHttp, webrtcNetwork, webrtcTransferCmnet, webrtcTransferTelecom, webrtcTransferUnicom } = this.userCardInfoData;
  176. const isWss = location.protocol === 'https:'
  177. const url = `${isWss ? 'wss://' : 'ws://'}${isWss ? internetHttps : internetHttp}/nats`;
  178. const ICEServerUrl = [
  179. // { "CMNET": webrtcTransferCmnet || '' }, // 移动
  180. // { 'CHINANET-GD': webrtcTransferTelecom || '' }, // 电信
  181. // { 'UNICOM-GD': webrtcTransferUnicom || '' }, // 联通
  182. // webrtc三网地址遗弃, 统一使用三网解析地址
  183. { "CMNET": webrtcNetwork }, // 移动
  184. { 'CHINANET-GD': webrtcNetwork }, // 电信
  185. { 'UNICOM-GD': webrtcNetwork }, // 联通
  186. ];
  187. const connection = {
  188. name: "猪猪令是猪",
  189. topic, // 云机ID 必填
  190. url, //信令服务地址 必填
  191. ICEServerUrl,
  192. width: 720, // 推流视频宽度 必填
  193. height: 1280, // 推流视频高度 必填
  194. // cardWidth: this.phoneSize.width || 1080, // 云机系统分辨率 宽 必填
  195. // cardHeight: this.phoneSize.height || 1920, // 云机系统分辨率 高 必填
  196. // cardDensity: this.phoneSize.dpi || 480, // 云机系统显示 密度 必填
  197. cardWidth: 0, // 云机系统分辨率 宽 必填
  198. cardHeight: 0, // 云机系统分辨率 高 必填
  199. cardDensity: 0, // 云机系统显示 密度 必填
  200. authToken, //拉流鉴权 token 必填
  201. bitrate: 6000, //码率 必填
  202. fps: 30, //必填
  203. callback: this.statusCallBack,//回调函数 必填
  204. };
  205. // 初始化 SDK
  206. this.engine = new RtcEngineSDK(connection);
  207. this.engine.RtcEngine()
  208. },
  209. // webRTC状态回调
  210. statusCallBack(event) {
  211. if (event.type !== 'StreamStates') {
  212. // console.log("链接的状态", event, event.val);
  213. }
  214. switch (event.type) {
  215. case "screenChange":
  216. // 0:横屏 1:竖屏
  217. // console.log("屏幕方向变化事件:" + event.val);
  218. this.isLandscape = event.val === 0;
  219. break;
  220. case "wsState":
  221. // “TIMEOUT”:nats链接超时
  222. // success 链接成功
  223. const status = ["TIMEOUT", "failed"]
  224. if (status.includes(event.val)) {
  225. Dialog.alert({
  226. title: '提示',
  227. message: '链接超时',
  228. confirmButtonText: '确定',
  229. confirmButtonColor: '#3cc51f',
  230. beforeClose: (action, done) => {
  231. this.exit()
  232. done()
  233. }
  234. })
  235. }
  236. break;
  237. case "rtcState":
  238. // “connected”:rtc链接成功 “failed”:rtc链接失败 “closed”:rtc链接关闭 “disconnected”:rtc链接超时
  239. // console.log(event.val, 'rtc链接成功状态')
  240. if (event.val === "connected") {
  241. // Toast.clear();
  242. // this.doConnectDirectives()
  243. // this.isShowVideo = true
  244. // this.$refs.rtcMediaPlayer.play()
  245. // let ms = +new Date - this.plugFlowStartTime
  246. // console.timeEnd('获取推流响应消耗时间:')
  247. // console.log(`获取推流响应消耗时间:${ms / 1000}秒`)
  248. // this.definitionFun(this.definitionValue)
  249. // this.pushflowPopup()
  250. // this.getResidueTime()
  251. playOnBtn.bind(this)()
  252. return
  253. }
  254. if (event.val === "connecting") return
  255. this.exit()
  256. break;
  257. case "AuthenticationStatus":
  258. // console.log(`鉴权${event.val === "success" ? '成功' : '失败'}`);
  259. break;
  260. case "StreamStates":
  261. // “currentRoundTripTime”:延迟 “lostRate”:丢包率 “seconds_KBytes”:带宽 “framesPerSecond”:帧率
  262. // ballPosition.value = event.val
  263. // console.log(event.val)
  264. break;
  265. case 'videoResolution': // 云机视频分辨率
  266. // console.log(event.val, '分辨率')
  267. break
  268. case 'networkService': // 获取三网信息
  269. // console.log("三网信息:", event.val);
  270. break
  271. case 'networkServiceURL': // 获取三网信息
  272. // console.log("三网IP地址:", event.val);
  273. break
  274. }
  275. },
  276. // 悬浮球移动
  277. touchmoveLevitatedSphere(e) {
  278. let { pageX, pageY } = e.targetTouches[0]
  279. let min = 20
  280. let MaxPageX = this.width - 20
  281. let MaxPageY = this.height - 20
  282. pageX = pageX <= min ? min : (pageX >= MaxPageX ? MaxPageX : pageX)
  283. pageY = pageY <= min ? min : (pageY >= MaxPageY ? MaxPageY : pageY)
  284. this.levitatedSpherePositionData = {
  285. left: `${pageX}px`,
  286. top: `${pageY}px`,
  287. transform: 'translate(-50%, -50%)'
  288. }
  289. },
  290. touchendLevitatedSphere(e) {
  291. localStorage.setItem('levitatedSpherePositionData', JSON.stringify(this.levitatedSpherePositionData))
  292. },
  293. // 清晰度
  294. definitionFun(value) {
  295. this.definitionValue = value
  296. this.engine.makeBitrate && this.engine.makeBitrate(value)
  297. localStorage.setItem('definitionValue', value)
  298. this.levitatedSphereVisible = false
  299. },
  300. // 修改分辨率
  301. resolutionRatio() {
  302. request.get('/api/resources/v5/machine/resolution/getResolvingPower', { params: { userCardId: this.parametersData.userCardId } }).then(res => {
  303. if (res.success) {
  304. this.resolutionRatioList = res.data.map(item => {
  305. item.height = item.high
  306. return item
  307. })
  308. this.levitatedSphereVisible = false
  309. this.resolutionRatioVisible = true
  310. }
  311. })
  312. },
  313. // 确定修改分辨率
  314. confirmResolution() {
  315. let { width, height, dpi: density } = this.phoneSize
  316. this.engine.makeResolution && this.engine.makeResolution({ width, height, density })
  317. this.resolutionRatioVisible = false
  318. },
  319. // 退出相关按钮操作
  320. exitFun(key) {
  321. switch (key) {
  322. case 'dormant':
  323. Dialog.alert({
  324. title: '提示',
  325. message: '确定退出云手机并下机',
  326. confirmButtonText: '确定',
  327. confirmButtonColor: '#3cc51f',
  328. showCancelButton: true,
  329. beforeClose: (action, done) => {
  330. if (action === 'cancel') done()
  331. if (action === 'confirm') {
  332. this.downline(done)
  333. }
  334. }
  335. })
  336. break;
  337. case 'shearplate':
  338. this.copyTextValue = ''
  339. pasteText().then(content => {
  340. typeof content !== 'boolean' ? this.openPasteboard(content) : this.copyTextVisible = true
  341. }, err => {
  342. this.copyTextVisible = true
  343. })
  344. break;
  345. case 'signout':
  346. this.exit()
  347. break;
  348. }
  349. },
  350. // 业务指令
  351. doConnectDirectives() {
  352. let { internetHttps, localIp, cardToken } = this.userCardInfoData
  353. let cUrl = `wss://${internetHttps}/businessChannel?cardIp=${localIp}&token=${cardToken}&type=directives`
  354. this.doConnectDirectivesWs = new WebSocket(cUrl);
  355. this.doConnectDirectivesWs.binaryType = 'arraybuffer'
  356. clearInterval(doConnectDirectivesIntervalerPing)
  357. // 链接成功
  358. this.doConnectDirectivesWs.onopen = (e) => {
  359. doConnectDirectivesIntervalerPing = setInterval(() => {
  360. if (this.doConnectDirectivesWs.readyState === 1) {
  361. this.doConnectDirectivesWs.send(JSON.stringify({ type: 'ping' }));
  362. } else {
  363. clearInterval(doConnectDirectivesIntervalerPing);
  364. }
  365. }, 3000)
  366. this.doConnectDirectivesWs.send(JSON.stringify({ type: 'getVsStatus' }))
  367. this.doConnectDirectivesWs.send(JSON.stringify({ type: 'bitRate', data: { bitRate: 1243000 } }))
  368. this.doConnectDirectivesWs.send(JSON.stringify({ type: 'InputMethod', data: { type: 2 } }))
  369. this.doConnectDirectivesWs.send(JSON.stringify({ type: 'getPhoneSize' }))
  370. }
  371. // 接受到的消息
  372. this.doConnectDirectivesWs.onmessage = res => {
  373. const result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data;
  374. switch (result.type) {
  375. // 分辨率
  376. case 'getPhoneSize':
  377. case 'setPhoneSize':
  378. let data = JSON.parse(JSON.stringify(result.data))
  379. let { width, height } = data
  380. if (width > height) {
  381. data.width = height
  382. data.height = width
  383. }
  384. this.phoneSize = data
  385. break
  386. // 云机复制过来的文本
  387. case 'reProduceText':
  388. if (navigator.clipboard) {
  389. navigator.clipboard.writeText(result.data.text);
  390. }
  391. break
  392. case 'downAdnInstallRep':
  393. Toast(result.data.msg)
  394. break
  395. // 接受到这个消息就自动退出云机
  396. case 'exitPhone':
  397. this.exit()
  398. break
  399. }
  400. }
  401. // 链接报错的回调
  402. this.doConnectDirectivesWs.onerror = res => {
  403. clearInterval(doConnectDirectivesTimerInterval)
  404. if (doConnectDirectivesRequestNum > 6) {
  405. this.exit()
  406. return
  407. }
  408. doConnectDirectivesRequestNum++
  409. this.doConnectDirectives()
  410. }
  411. },
  412. // 粘贴版相关接口
  413. shearContent({ type, params, queryStr }) {
  414. let url = '/api/public/v5/shear/content'
  415. if (queryStr) url += queryStr
  416. return request[type](url, params)
  417. },
  418. // 清空全部、清除某条
  419. deletePasteVersion(ids) {
  420. if (!ids) {
  421. Dialog.alert({
  422. title: '提示',
  423. message: '确定清空剪贴板?',
  424. confirmButtonText: '确定',
  425. confirmButtonColor: '#3cc51f',
  426. showCancelButton: true,
  427. beforeClose: (action, done) => {
  428. if (action === 'cancel') done()
  429. if (action === 'confirm') {
  430. fun.bind(this)(done)
  431. }
  432. }
  433. })
  434. return
  435. }
  436. fun.bind(this)()
  437. function fun(callBack = () => { }) {
  438. this.shearContent({
  439. type: 'delete', queryStr: Qs.stringify(
  440. {
  441. ids: ids ? [ids] : this.pasteVersionList.map((v) => v.id),
  442. },
  443. { arrayFormat: 'repeat', addQueryPrefix: true },
  444. )
  445. }).then(res => {
  446. if (res.status === 0) {
  447. this.getPasteVersion()
  448. callBack(true)
  449. } else {
  450. callBack(false)
  451. Toast(res.msg)
  452. }
  453. }).catch(() => {
  454. callBack(false)
  455. })
  456. }
  457. },
  458. // 获取粘贴版数据
  459. getPasteVersion(callBack = () => { }) {
  460. this.shearContent({ type: 'get' }).then(res => {
  461. this.pasteVersionList = res.data
  462. callBack(true)
  463. }).catch(() => {
  464. callBack(false)
  465. }).finally(() => { })
  466. },
  467. // 复制弹窗是否关闭
  468. beforeCloseCopy(action, done) {
  469. if (action !== 'confirm') {
  470. // 获取剪切板
  471. this.getPasteVersion(() => {
  472. this.pasteVersionVisible = true
  473. this.levitatedSphereVisible = false
  474. done()
  475. })
  476. return
  477. }
  478. if (!this.copyTextValue) {
  479. Toast('请输入复制到剪切板的内容')
  480. done(false)
  481. return
  482. }
  483. this.openPasteboard(this.copyTextValue, done)
  484. },
  485. // 打开粘贴板
  486. async openPasteboard(content, callBack = () => { }) {
  487. this.shearContent({ type: 'post', params: { content } }).then().finally(() => {
  488. callBack()
  489. // 获取剪切板
  490. this.getPasteVersion(() => {
  491. this.pasteVersionVisible = true
  492. this.levitatedSphereVisible = false
  493. })
  494. })
  495. },
  496. // 复制粘贴某条数据
  497. copyPasteVersiontext(e) {
  498. clickCopyText(e, (event) => {
  499. this.doConnectDirectivesWs.send(JSON.stringify({
  500. type: 'cutting',
  501. data: {
  502. str: event.text,
  503. },
  504. }))
  505. Toast('复制成功')
  506. }, () => {
  507. Toast('复制失败')
  508. })
  509. },
  510. // 获取卡信息
  511. getUserCardInfo() {
  512. Toast.loading({
  513. duration: 0, // 持续展示 toast
  514. message: '数据加载中...',
  515. });
  516. this.obtainCardInfoStartTime = + new Date()
  517. let { userCardId } = this.parametersData
  518. userCardId = +userCardId
  519. const statusTips = {
  520. 5200: '网络异常,请稍后重试',
  521. 5220: '云手机正在一键修复中',
  522. 5203: '正在排队中,请稍等',
  523. 5204: '云机异常,正在为你重新分配云机'
  524. }
  525. request.post('/api/resources/user/cloud/connect', { userCardId }).then(async res => {
  526. console.log('res', res);
  527. if (!res.success) { return }
  528. const { isWeixin } = this.parametersData;
  529. switch (res.status) {
  530. case 0:
  531. getUserCardInfoRequestNum = 1
  532. // 不支持webRTC跳转到指定的页面
  533. if (!res.data.isWebrtc) {
  534. location.replace(`${location.origin}/h5/webRtcYJ/WXtrialInterface.html${location.search}`)
  535. return
  536. }
  537. if (!this.isSupportRtc) {
  538. Dialog.alert({
  539. title: '提示',
  540. message: `${+isWeixin ? '微信小程序' : ''}当前版本暂不支持使用,可下载谷歌浏览器或双子星客户端进行使用`,
  541. confirmButtonText: '确定',
  542. confirmButtonColor: '#3cc51f',
  543. beforeClose: (action, done) => {
  544. this.exit()
  545. done()
  546. }
  547. })
  548. return
  549. }
  550. // webRtc连接,需获取连接地址,否则失败
  551. if (res.data.webrtcNetworkAnalysis) {
  552. // 如果有网络分析的请求地址, 则请求,否则失败
  553. const webrtcNetworkAnalysisReq = await request.get(res.data.webrtcNetworkAnalysis);
  554. console.log('webrtcNetworkAnalysisReq', webrtcNetworkAnalysisReq);
  555. if (webrtcNetworkAnalysisReq !== null && webrtcNetworkAnalysisReq.success) {
  556. if (webrtcNetworkAnalysisReq.data) {
  557. // 保存获取的连接地址到上个请求的响应中, 方便后面使用
  558. res.data.webrtcNetwork = webrtcNetworkAnalysisReq.data;
  559. }
  560. }
  561. }
  562. // 如果没有获取到连接地址, 则失败
  563. if(!res.data.webrtcNetwork) {
  564. Dialog.alert({
  565. title: '提示',
  566. message: '访问失败,请稍后重试',
  567. confirmButtonText: '确定',
  568. confirmButtonColor: '#3cc51f',
  569. beforeClose: (action, done) => {
  570. this.exit()
  571. done()
  572. }
  573. })
  574. return;
  575. }
  576. this.userCardInfoData = res.data
  577. // let ms = + new Date() - this.obtainCardInfoStartTime
  578. this.connectWebRtc()
  579. return
  580. case 5200:
  581. case 5220:
  582. case 5203:
  583. case 5204:
  584. if (res.status === 5200) {
  585. reconnect.bind(this)()
  586. return
  587. }
  588. Toast(statusTips[res.status])
  589. break
  590. default:
  591. Toast('画面异常,请重新进入')
  592. break
  593. }
  594. setTimeout(() => {
  595. this.exit()
  596. }, 3000)
  597. })
  598. function reconnect() {
  599. if (getUserCardInfoRequestNum > 6) {
  600. Toast('网络异常,请稍后重试')
  601. clearTimeout(getUserCardInfoTimerInterval)
  602. setTimeout(() => {
  603. this.exit()
  604. }, 3000)
  605. return
  606. }
  607. getUserCardInfoTimerInterval = setTimeout(() => {
  608. this.getUserCardInfo()
  609. getUserCardInfoRequestNum++
  610. }, 3000)
  611. }
  612. },
  613. // 超过指定触碰时间是否提示关闭链接
  614. pushflowPopup() {
  615. request.get('/api/public/v5/pushflow/popup').then(res => {
  616. if (res.success) {
  617. this.isFiringNoOperationSetTimeout = res.data
  618. this.noOperationSetTimeout()
  619. }
  620. })
  621. },
  622. // 退出功能
  623. exit() {
  624. this.engine.disconnect && this.engine.disconnect();
  625. this.doConnectDirectivesWs && this.doConnectDirectivesWs.close()
  626. uni.reLaunch({
  627. url: '/pages/index/index'
  628. });
  629. },
  630. // 不触碰屏幕显示退出链接弹窗
  631. noOperationSetTimeout(key) {
  632. if (!this.isFiringNoOperationSetTimeout) return
  633. clearTimeout(noOperationSetTimeoutTimeInterval)
  634. if (key === 'cancel') {
  635. clearInterval(noOperationSetIntervalTimeInterval)
  636. this.noOperationSetTimeoutTimeVisible = false
  637. this.noOperationSetTimeout()
  638. return
  639. }
  640. noOperationSetTimeoutTimeInterval = setTimeout(() => {
  641. let index = 9
  642. this.confirmButtonText = '退出(10秒)'
  643. this.noOperationSetTimeoutTimeVisible = true
  644. noOperationSetIntervalTimeInterval = setInterval(() => {
  645. this.confirmButtonText = `退出${index ? `(${index}秒)` : ''}`
  646. index--
  647. if (index < 0) {
  648. this.noOperationSetTimeout('cancel')
  649. this.exit()
  650. }
  651. }, 1000)
  652. }, 300000)
  653. },
  654. // 获取云机剩余时长
  655. async getResidueTime() {
  656. clearInterval(countdownTimeInterval)
  657. const { userCardType, isShowCountdown, isShowRule } = this.parametersData
  658. const { userCardId } = this.userCardInfoData
  659. if (![1, 2, 3].includes(+userCardType)) return
  660. const res = await request.get(`/api/resources/yearMember/getResidueTime?userCardId=${userCardId}`)
  661. let time = res.data;
  662. if (!res.status) {
  663. this.countdownTime = residueTimeStamp(time)
  664. await request.get(`/api/resources/yearMember/startTime?userCardId=${userCardId}`)
  665. if (+isShowCountdown) this.timingVisible = true
  666. if (+isShowRule) this.billingRulesVisible = true
  667. countdownTimeInterval = setInterval(() => {
  668. if (time <= 0) {
  669. clearInterval(countdownTimeInterval)
  670. this.downline()
  671. return
  672. }
  673. time--
  674. this.countdownTime = residueTimeStamp(time)
  675. }, 1000)
  676. }
  677. },
  678. // 关闭倒计时弹窗
  679. handlecountdownTimeClose() {
  680. const { userCardId } = this.userCardInfoData
  681. request.get(`/api/resources/yearMember/closeRemind?userCardId=${userCardId}`).then(res => {
  682. if (!res.status) {
  683. clearInterval(countdownTimeInterval)
  684. this.timingVisible = false
  685. return
  686. }
  687. Toast(res.msg);
  688. })
  689. },
  690. // 退出并下机
  691. downline(fun = () => { }) {
  692. const { userCardId } = this.userCardInfoData
  693. request.get(`/api/resources/yearMember/downline?userCardId=${userCardId}`).then(res => {
  694. if (!res.status) {
  695. fun(true)
  696. // 通信给h5项目告知是退出并下机
  697. parent.postMessage(
  698. {
  699. type: 'exit',
  700. },
  701. '*',
  702. );
  703. uni.postMessage({
  704. data: {
  705. type: 'exit'
  706. }
  707. });
  708. this.exit()
  709. return
  710. }
  711. fun(false)
  712. Toast(res.msg);
  713. })
  714. },
  715. // 获取推荐列表
  716. getRecommend() {
  717. const { userCardId } = this.userCardInfoData
  718. request.get(`/api/public/v1/market/get/recommend?userCardId=${userCardId}`).then(res => {
  719. if (!res.status) {
  720. this.billingRulesVisible = false
  721. this.recommendList = res.data
  722. this.recommendList.length && (this.applyRecommendVisible = true)
  723. }
  724. })
  725. },
  726. // 下载apk
  727. downAndInstallApk({ downloadUrl: apkUrl, id: taskUid }) {
  728. this.doConnectDirectivesWs.send(JSON.stringify({
  729. type: 'downAndInstallApk',
  730. data: {
  731. apkUrl,
  732. taskUid
  733. },
  734. }))
  735. },
  736. // 移开手指时会发生的回调
  737. touchendRtcMediaPlayer(event) {
  738. this.engine.touchClick && this.engine.touchClick(event, 1, true)
  739. this.noOperationSetTimeout()
  740. },
  741. // 返回、主页、任务器
  742. footerBtnFun(key) {
  743. this.engine[key] && this.engine[key]()
  744. this.noOperationSetTimeout()
  745. },
  746. // 获取初始化尺寸
  747. getInitSize() {
  748. // 高度、悬浮球相关配置
  749. this.height = window.innerHeight
  750. this.width = window.innerWidth
  751. this.$nextTick(() => {
  752. // 云机画面宽高
  753. let layoutView = document.querySelector('.layout-view')
  754. this.layoutViewWidth = layoutView.offsetWidth
  755. this.layoutViewHeight = layoutView.offsetHeight
  756. })
  757. },
  758. // 音量
  759. volumeControl(value) {
  760. this.engine.ExexuteKeyBoard && this.engine.ExexuteKeyBoard(value)
  761. this.$refs.rtcMediaPlayer && (this.$refs.rtcMediaPlayer.muted = false)
  762. }
  763. }
  764. })
  765. function playOnBtn() {
  766. const { isTips } = this.parametersData;
  767. Dialog.alert({
  768. title: '提示',
  769. message: `${+isTips ? '开始' : '继续'}使用云手机`,
  770. confirmButtonText: '确定',
  771. confirmButtonColor: '#3cc51f',
  772. beforeClose: (action, done) => {
  773. if (action === 'confirm') {
  774. this.isShowVideo = true
  775. Toast.clear();
  776. this.$refs.rtcMediaPlayer.play()
  777. setTimeout(() => {
  778. this.doConnectDirectives()
  779. this.definitionFun(this.definitionValue)
  780. this.pushflowPopup()
  781. this.getResidueTime()
  782. done()
  783. })
  784. }
  785. }
  786. });
  787. }
  788. // 获取URL参数
  789. function getParameters() {
  790. let arr = location.search.split('?')
  791. let obj = {}
  792. if (arr[1]) {
  793. arr = arr[1].split('&')
  794. arr.forEach(item => {
  795. let [key, value = ''] = item.split('=')
  796. obj[key] = value
  797. })
  798. }
  799. return obj
  800. }
  801. // 倒计时处理的时间
  802. function residueTimeStamp(value) {
  803. let theTime = value;//秒
  804. let middle = 0;//分
  805. let hour = 0;//小时
  806. if (theTime > 59) {
  807. middle = parseInt(theTime / 60);
  808. theTime = parseInt(theTime % 60);
  809. }
  810. if (middle > 59) {
  811. hour = parseInt(middle / 60);
  812. middle = parseInt(middle % 60);
  813. }
  814. theTime < 10 ? theTime = '0' + theTime : theTime = theTime
  815. middle < 10 ? middle = '0' + middle : middle = middle
  816. hour < 10 ? hour = '0' + hour : hour = hour
  817. return hour + ':' + middle + ':' + theTime
  818. }