living.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const api_live = require("../../api/live.js");
  4. require("../../api/order.js");
  5. const api_home = require("../../api/home.js");
  6. const common_assets = require("../../common/assets.js");
  7. var wsUrl = "ws://192.168.10.166:7114/app/webSocket";
  8. var isSocketOpen = false;
  9. var socket = null;
  10. const _sfc_main = {
  11. data() {
  12. return {
  13. videoUrl: null,
  14. showType: 1,
  15. //横屏1 竖屏2
  16. boxHeight: 300,
  17. //小黄车高度
  18. isFollow: true,
  19. liveViewData: {},
  20. liveViewers: [],
  21. //观众
  22. likeName: 0,
  23. hlsPlayer: null,
  24. // HLS播放器实例,
  25. livingUrl: "",
  26. products: {},
  27. store: {},
  28. orderUser: {},
  29. //正在购买
  30. userType: 0,
  31. timestamp: "",
  32. liveId: null,
  33. // userId: uni.getStorageSync("userInfo.userId"),
  34. livedata: {},
  35. codeimg: "",
  36. placeholderText: "说点什么...",
  37. isZoom: false,
  38. //点赞按钮控制是否放大
  39. userinfo: "",
  40. //用户信息
  41. path: "http://192.168.10.166/dev-api",
  42. //余红奇
  43. // path: 'http://v56c9b8e.natappfree.cc', //余红奇
  44. // path: 'live.test.ylrztop.com/prod-api', //余红奇
  45. // path: 'http://192.168.10.170/dev-api', //陈果
  46. value: "",
  47. talkdisabled: false,
  48. //输入框是否禁用
  49. autoplay: false,
  50. //视频自动播放
  51. showadd: false,
  52. talklist: [],
  53. scrollIntoView: "",
  54. bufferRate: 0,
  55. //视频缓冲时间
  56. playDuration: 0,
  57. //视频播放时间
  58. videoContext: "",
  59. thistime: common_vendor.index.$u.timeFormat(/* @__PURE__ */ new Date(), "yyyy-mm-dd hh:MM:ss"),
  60. upDown: true,
  61. //是否视频显示隐藏
  62. isLongPress: false,
  63. // 是否长按
  64. timeout: null,
  65. // 计时器
  66. showWelcomeMessage: false,
  67. isSubmit: false,
  68. messageContent: "",
  69. showziliao: false,
  70. isScreen: true,
  71. showAnswer: false,
  72. //展示答题
  73. Answerlistall: {},
  74. //所有题目
  75. answerlist: {},
  76. //当前题目
  77. answerfrist: 0,
  78. //当前选择
  79. checkboxValue: [],
  80. //多选数据
  81. checkboxFormValue: "",
  82. //多选数据
  83. allAnswerLists: [],
  84. // 新增:存储所有题目列表
  85. showAnswerred: false,
  86. //展示红包答题
  87. answerbtn: false,
  88. //答题按钮弹窗
  89. redallAnswerLists: [],
  90. //储存所有红包答题列表
  91. redanswerAll: [],
  92. //红包当前题目
  93. redanswerList: [],
  94. //红包答题列表
  95. redanswertips: [],
  96. //红包答题提示
  97. shopping: false,
  98. //小黄车弹窗
  99. scrollTop: 0,
  100. //弹幕
  101. old: {
  102. scrollTop: 0
  103. }
  104. // liveData: {}, //直播间点赞、关注、在线人数数据
  105. };
  106. },
  107. onLoad(options) {
  108. if (options.liveId) {
  109. this.liveId = options.liveId;
  110. console.log("接收到的liveId:", this.liveId);
  111. }
  112. },
  113. computed: {
  114. filteredViewers() {
  115. return this.liveViewers.slice(0, 3);
  116. }
  117. },
  118. mounted() {
  119. this.getliveViewData();
  120. this.intervalId = setInterval(() => {
  121. this.getliveViewData();
  122. }, 60 * 1e3);
  123. this.getliveOrder();
  124. this.getliveStore();
  125. this.getliveUser();
  126. this.initTime();
  127. this.initSocket();
  128. var that = this;
  129. common_vendor.index.$on("initSocket", () => {
  130. that.initSocket();
  131. });
  132. common_vendor.index.$on("sendMsg", (item) => {
  133. that.sendMsg(item);
  134. });
  135. common_vendor.index.$on("closeWebSocket", () => {
  136. that.closeWebSocket();
  137. });
  138. this.getEWechatSdk();
  139. this.getliving();
  140. this.gettalklist();
  141. this.userinfo = JSON.parse(common_vendor.index.getStorageSync("userInfo"));
  142. },
  143. onReady: function(res) {
  144. this.videoContext = common_vendor.index.createVideoContext("myVideo");
  145. },
  146. onUnload() {
  147. this.closeWebSocket();
  148. if (this.hlsPlayer) {
  149. this.hlsPlayer.destroy();
  150. }
  151. if (this.pingpangTimes) {
  152. clearInterval(this.pingpangTimes);
  153. }
  154. },
  155. methods: {
  156. goStore() {
  157. console.log("带过去storeId", this.products[0].storeId);
  158. common_vendor.index.navigateTo({
  159. url: "/pages_shop/store?liveId=" + this.liveId + "&storeId=" + this.products[0].storeId
  160. });
  161. },
  162. // 去订单列表
  163. goOrderList() {
  164. common_vendor.index.navigateTo({
  165. url: "/pages_shop/order"
  166. });
  167. },
  168. // 初始化HLS播放器
  169. initHlsPlayer() {
  170. if (common_vendor.Hls.isSupported() && this.livingUrl) {
  171. const video = document.getElementById("myVideo");
  172. if (video) {
  173. this.hlsPlayer = new common_vendor.Hls();
  174. this.hlsPlayer.loadSource(this.livingUrl);
  175. this.hlsPlayer.attachMedia(video);
  176. this.hlsPlayer.on(common_vendor.Hls.Events.MANIFEST_PARSED, () => {
  177. video.play();
  178. });
  179. }
  180. }
  181. },
  182. // 视频错误处理
  183. videoError(e) {
  184. console.error("视频播放错误:", e.detail.errMsg);
  185. if (this.livingUrl) {
  186. setTimeout(() => {
  187. this.videoContext.play();
  188. }, 2e3);
  189. }
  190. },
  191. getliving() {
  192. console.log("获取直播信息");
  193. const param = {
  194. id: this.liveId
  195. };
  196. api_home.getlive(param).then((res) => {
  197. if (res.code !== 200) {
  198. common_vendor.index.showToast({
  199. title: res.msg,
  200. icon: "none",
  201. duration: 2e3
  202. });
  203. return;
  204. }
  205. this.livedata = res.data;
  206. this.showType = res.showType;
  207. if (res.data.liveType === 1) {
  208. this.videoUrl = res.data.videoUrl;
  209. this.autoplay = true;
  210. this.placeholderText = "观看回放中";
  211. this.talkdisabled = false;
  212. } else if (res.data.liveType === 2) {
  213. this.livingUrl = res.data.livingUrl;
  214. this.autoplay = true;
  215. this.placeholderText = "说点什么...";
  216. this.talkdisabled = false;
  217. } else {
  218. this.livingUrl = "";
  219. this.autoplay = false;
  220. this.placeholderText = "直播未开始,暂时无法发言";
  221. this.talkdisabled = true;
  222. return;
  223. }
  224. this.$nextTick(() => {
  225. if (!this.livingUrl)
  226. return;
  227. if (!this.videoContext) {
  228. this.videoContext = common_vendor.index.createVideoContext("myVideo", this);
  229. }
  230. if (res.data.liveType === 1) {
  231. this.videoContext.seek(res.data.nowDuration || 0);
  232. } else {
  233. this.videoContext.play();
  234. }
  235. });
  236. }).catch((err) => {
  237. console.error("获取直播信息失败:", err);
  238. common_vendor.index.showToast({
  239. title: "获取直播信息失败",
  240. icon: "none"
  241. });
  242. });
  243. },
  244. maskString(str, maskChar = "*") {
  245. if (!str)
  246. return "";
  247. const strVal = String(str);
  248. return strVal.split("").map((char, index) => index === 0 ? char : maskChar).join("");
  249. },
  250. getPureDecimal(num, precision = 6) {
  251. const decimalPart = Math.abs(num).toFixed(precision).split(".")[1];
  252. return (decimalPart == null ? void 0 : decimalPart.replace(/0+$/, "")) || "";
  253. },
  254. // 返回上一个页面并关闭WebSocket
  255. goBack() {
  256. common_vendor.index.navigateBack();
  257. this.closeWebSocket();
  258. },
  259. //直播间点赞、关注、在线人数数据
  260. getliveViewData() {
  261. api_live.getLiveViewData(this.liveId).then(
  262. (res) => {
  263. if (res.code == 200) {
  264. this.liveViewData = res;
  265. } else {
  266. common_vendor.index.showToast({
  267. title: res.msg,
  268. icon: "none"
  269. });
  270. }
  271. },
  272. (rej) => {
  273. }
  274. );
  275. },
  276. //正在购买
  277. getliveOrder() {
  278. api_live.liveOrderUser(this.liveId).then(
  279. (res) => {
  280. if (res.code == 200) {
  281. console.log("正在购买>>>>", res);
  282. this.orderUser = res;
  283. } else {
  284. common_vendor.index.showToast({
  285. title: res.msg,
  286. icon: "none"
  287. });
  288. }
  289. },
  290. (rej) => {
  291. }
  292. );
  293. },
  294. //小黄车 店铺展示
  295. getliveStore() {
  296. let data = {
  297. pageSize: 10,
  298. page: 1
  299. };
  300. api_live.liveStore(this.liveId, data).then(
  301. (res) => {
  302. if (res.code == 200) {
  303. console.log("小黄车 店铺展示>>>>", res);
  304. this.products = res.data;
  305. } else {
  306. common_vendor.index.showToast({
  307. title: res.msg,
  308. icon: "none"
  309. });
  310. }
  311. },
  312. (rej) => {
  313. }
  314. );
  315. },
  316. // 获取直播间信息接口
  317. getLiveinformation() {
  318. api_live.getLiveInfo(this.liveId).then(
  319. (res) => {
  320. if (res.code == 200) {
  321. console.log("获取直播间信息接口>>>>", res);
  322. this.livingUrl = res.livingUrl;
  323. } else {
  324. common_vendor.index.showToast({
  325. title: res.msg,
  326. icon: "none"
  327. });
  328. }
  329. },
  330. (rej) => {
  331. }
  332. );
  333. },
  334. // 获取直播间用户
  335. getliveUser() {
  336. api_live.getRecentLiveViewers(this.liveId).then(
  337. (res) => {
  338. if (res.code == 200) {
  339. console.log("获取直播间用户>>>>", res);
  340. this.liveViewers = res.recentLiveViewers;
  341. } else {
  342. common_vendor.index.showToast({
  343. title: res.msg,
  344. icon: "none"
  345. });
  346. }
  347. },
  348. (rej) => {
  349. }
  350. );
  351. },
  352. // 点赞
  353. onLike() {
  354. api_live.liveDataLike(this.liveId).then(
  355. (res) => {
  356. if (res.code == 200) {
  357. if (res.like) {
  358. this.liveViewData.like++;
  359. } else {
  360. common_vendor.index.showToast({
  361. title: res.msg,
  362. icon: "none"
  363. });
  364. }
  365. } else {
  366. common_vendor.index.showToast({
  367. title: res.msg,
  368. icon: "none"
  369. });
  370. }
  371. },
  372. (rej) => {
  373. }
  374. );
  375. },
  376. // 去购买,跳商品详情
  377. goShop(productId, goodsId) {
  378. common_vendor.index.navigateTo({
  379. url: "/pages_shop/goods?productId=" + productId + "&liveId=" + this.liveId + "&goodsId=" + goodsId
  380. });
  381. },
  382. // 收藏
  383. onCollect(item) {
  384. let data = {
  385. liveId: this.liveId,
  386. productId: item.productId,
  387. storeId: this.products.storeId
  388. };
  389. api_live.collectStore(data).then(
  390. (res) => {
  391. if (res.code == 200) {
  392. console.log("收藏lcollectStore>>>>", res);
  393. } else {
  394. common_vendor.index.showToast({
  395. title: res.msg,
  396. icon: "none"
  397. });
  398. }
  399. },
  400. (rej) => {
  401. }
  402. );
  403. },
  404. // 关注
  405. onFollow() {
  406. api_live.follow(this.liveId).then(
  407. (res) => {
  408. this.isFollow = !this.isFollow;
  409. common_vendor.index.showToast({
  410. title: res.msg,
  411. icon: "none"
  412. });
  413. },
  414. (rej) => {
  415. }
  416. );
  417. },
  418. initTime() {
  419. const now = /* @__PURE__ */ new Date();
  420. this.timestamp = now.getTime();
  421. },
  422. // initWebSocket() {
  423. // const liveWS = new LiveWS('ws://your-server.com', 123, 456);
  424. // // 从 URL 中解析 timestamp
  425. // const urlParams = new URL(liveWS.url).searchParams;
  426. // this.timestamp = urlParams.get('timestamp');
  427. // // console.log('Timestamp:', timestamp);
  428. // },
  429. // 弹幕滚动
  430. lowerChat: function(e) {
  431. console.log(e);
  432. },
  433. scroll: function(e) {
  434. console.log(e);
  435. this.old.scrollTop = e.detail.scrollTop;
  436. },
  437. loadmore() {
  438. },
  439. // 弹幕
  440. scrollchat() {
  441. },
  442. noredanswer() {
  443. if (this.Answerlistall > 0) {
  444. this.showAnswer = !this.this.showAnswer;
  445. } else {
  446. common_vendor.index.showToast({
  447. title: "暂无题目",
  448. icon: "none"
  449. });
  450. }
  451. },
  452. redbagAnswer() {
  453. this.showAnswerred = !this.showAnswerred;
  454. this.answerbtn = !this.answerbtn;
  455. },
  456. submitAnswers() {
  457. if (this.isSubmit)
  458. return;
  459. this.isSubmit = true;
  460. const data = {
  461. questionId: this.answerlist.id,
  462. answer: this.checkboxFormValue
  463. };
  464. api_home.submitAnswer(data).then((res) => {
  465. if (res.code == 200) {
  466. common_vendor.index.showToast({
  467. title: res.msg,
  468. icon: "none"
  469. });
  470. }
  471. if (this.answerfrist < this.Answerlistall - 1) {
  472. this.answerfrist++;
  473. this.answerlist = this.allAnswerLists[this.answerfrist];
  474. this.answerlist.content = JSON.parse(this.answerlist.content);
  475. } else {
  476. common_vendor.index.showToast({
  477. title: "已是最后一题",
  478. icon: "none"
  479. });
  480. this.showAnswer = false;
  481. }
  482. this.checkboxValue = [];
  483. this.checkboxFormValue = "";
  484. common_vendor.index.showToast({
  485. title: res.msg,
  486. icon: "none"
  487. });
  488. }).finally((e) => {
  489. this.isSubmit = false;
  490. });
  491. },
  492. // handleCheckboxSelect(value) {
  493. // const index = this.checkboxValue.indexOf(value)
  494. // console.log(value)
  495. // if (this.answerlist.type == 1) {
  496. // this.checkboxValue = [value]
  497. // this.checkboxFormValue = this.checkboxValue.join(',')
  498. // setTimeout(() => {
  499. // uni.showToast({
  500. // title: '准备下一题',
  501. // icon: 'none',
  502. // duration: 2000
  503. // });
  504. // }, 1000);
  505. // this.submitAnswers()
  506. // console.log(this.checkboxValue)
  507. // } else if (this.answerlist.type == 2) {
  508. // if (index > -1) {
  509. // this.checkboxValue.splice(index, 1)
  510. // this.checkboxFormValue = this.checkboxValue.join(',')
  511. // } else {
  512. // this.checkboxValue.push(value)
  513. // this.checkboxFormValue = this.checkboxValue.join(',')
  514. // }
  515. // console.log(this.checkboxFormValue)
  516. // }
  517. // },
  518. getAnswerlists() {
  519. console.log(123);
  520. const data = {
  521. liveId: this.liveId
  522. };
  523. api_home.getAnswerlist(data).then((res) => {
  524. if (res.code == 200) {
  525. if (res.data.length > 0) {
  526. this.allAnswerLists = res.data;
  527. this.Answerlistall = res.data.length;
  528. if (this.allAnswerLists.length > 0) {
  529. this.answerlist = this.allAnswerLists[0];
  530. this.answerlist.content = JSON.parse(this.allAnswerLists[0].content);
  531. }
  532. this.showAnswer = true;
  533. } else {
  534. this.showAnswer = false;
  535. }
  536. }
  537. });
  538. },
  539. gettalklist() {
  540. const param = {
  541. id: this.liveId
  542. };
  543. api_home.gettextlist(param).then((res) => {
  544. if (res.code == 200) {
  545. this.talklist = res.data;
  546. this.$nextTick(() => {
  547. this.scrollIntoView = `list_${this.talklist.length - 1}`;
  548. });
  549. }
  550. });
  551. },
  552. open() {
  553. },
  554. close() {
  555. this.showadd = !this.showadd;
  556. },
  557. closes() {
  558. this.showziliao = !this.showziliao;
  559. },
  560. closest() {
  561. this.showAnswer = !this.showAnswer;
  562. },
  563. closestred() {
  564. this.showAnswerred = !this.showAnswerred;
  565. },
  566. closeanswer() {
  567. this.answerbtn = !this.answerbtn;
  568. },
  569. // 关闭小黄车
  570. closeshop() {
  571. this.shopping = !this.shopping;
  572. },
  573. longPress() {
  574. this.timeout = setTimeout(() => {
  575. this.isLongPress = true;
  576. common_vendor.index.saveImageToPhotosAlbum({
  577. filePath: this.livedata.qwQrCode,
  578. // 图片的本地路径或网络路径
  579. success: () => {
  580. common_vendor.index.showToast({
  581. title: "保存成功"
  582. });
  583. },
  584. fail: () => {
  585. common_vendor.index.showToast({
  586. title: "",
  587. icon: "none"
  588. });
  589. }
  590. });
  591. }, 500);
  592. },
  593. cancelLongPress() {
  594. clearTimeout(this.timeout);
  595. this.isLongPress = false;
  596. },
  597. // 触摸开始
  598. handleTouchStart() {
  599. this.isZoom = true;
  600. },
  601. // 触摸结束
  602. handleTouchEnd() {
  603. this.isZoom = false;
  604. },
  605. getEWechatSdk() {
  606. },
  607. closeWebSocket() {
  608. clearInterval(this.pingpangTimes);
  609. clearTimeout(this.reconnectTimer);
  610. if (socket && isSocketOpen) {
  611. try {
  612. common_vendor.index.closeSocket();
  613. isSocketOpen = false;
  614. console.log("WebSocket已主动关闭");
  615. } catch (e) {
  616. console.error("关闭WebSocket时出错:", e);
  617. }
  618. }
  619. },
  620. reConnect() {
  621. var that = this;
  622. try {
  623. common_vendor.index.closeSocket();
  624. } catch (e) {
  625. }
  626. setTimeout(function() {
  627. that.initSocket();
  628. }, 1e4);
  629. },
  630. startHeartbeat() {
  631. clearInterval(this.pingpangTimes);
  632. console.log("开始心跳检测");
  633. this.pingpangTimes = setInterval(() => {
  634. if (isSocketOpen) {
  635. const pingData = {
  636. cmd: "ping",
  637. timestamp: (/* @__PURE__ */ new Date()).getTime()
  638. };
  639. socket.send({
  640. data: JSON.stringify(pingData),
  641. success: () => {
  642. console.log("心跳发送成功");
  643. },
  644. fail: (err) => {
  645. console.error("心跳发送失败:", err);
  646. this.scheduleReconnect();
  647. }
  648. });
  649. }
  650. }, 3e4);
  651. },
  652. initSocket() {
  653. if (this.reconnectCount >= this.maxReconnectAttempts) {
  654. console.log("已达到最大重连次数,不再尝试连接");
  655. common_vendor.index.showToast({
  656. title: "连接失败,请稍后再试",
  657. icon: "none"
  658. });
  659. return;
  660. }
  661. this.userinfo = JSON.parse(common_vendor.index.getStorageSync("userInfo"));
  662. let signature = common_vendor.CryptoJS.HmacSHA256(
  663. common_vendor.CryptoJS.enc.Utf8.parse(this.liveId.toString() + this.userinfo.userId + this.userType + this.timestamp),
  664. common_vendor.CryptoJS.enc.Utf8.parse(this.timestamp)
  665. ).toString(common_vendor.CryptoJS.enc.Hex);
  666. this.closeWebSocket();
  667. socket = common_vendor.index.connectSocket({
  668. url: wsUrl + "?userId=" + this.userinfo.userId + "&liveId=" + this.liveId + "&userType=" + this.userType + "&timestamp=" + this.timestamp + "&signature=" + signature,
  669. multiple: true,
  670. success: (res) => {
  671. this.reconnectCount++;
  672. clearInterval(this.pingpangTimes);
  673. common_vendor.index.onSocketMessage((res2) => {
  674. if (res2.data.code == 500) {
  675. common_vendor.index.showToast({
  676. title: res2.data.msg,
  677. icon: "none",
  678. duration: 2e3
  679. });
  680. }
  681. const redata = JSON.parse(res2.data);
  682. console.log("WebSocket拿到的东西", redata);
  683. this.talklist.push(redata.data);
  684. this.$nextTick(() => {
  685. this.scrollIntoView = `list_${this.talklist.length - 1}`;
  686. });
  687. if (redata.cmd == "deleteId") {
  688. common_vendor.index.$emit("deleteId");
  689. } else if (redata.cmd == "init") {
  690. common_vendor.index.$emit("init", redata.data);
  691. } else if (redata.cmd == "reload") {
  692. common_vendor.index.$emit("reload");
  693. } else if (redata.data.cmd == "sendRedPacketQuestion") {
  694. const list = JSON.parse(redata.data.data);
  695. this.redanswerAll = [...this.redanswerAll, ...list];
  696. if (this.redanswerAll[1].randomAmount !== null) {
  697. this.redanswertips = JSON.parse(this.redanswerAll[0].randomAmount);
  698. }
  699. console.log(this.redanswertips);
  700. console.log(this.redanswerAll);
  701. } else if (redata.data.cmd == "entry") {
  702. this.showWelcomeMessage = true;
  703. common_vendor.index.$emit("entry", redata.data);
  704. }
  705. });
  706. },
  707. fail: (res) => {
  708. common_vendor.index.$emit("websocket", 0);
  709. console.log(res);
  710. this.scheduleReconnect();
  711. }
  712. });
  713. common_vendor.index.onSocketOpen(() => {
  714. isSocketOpen = true;
  715. this.reconnectCount = 0;
  716. console.log("WebSocket 连接成功");
  717. this.startHeartbeat();
  718. });
  719. common_vendor.index.onSocketClose(() => {
  720. isSocketOpen = false;
  721. console.log("WebSocket连接已关闭!");
  722. this.scheduleReconnect();
  723. });
  724. common_vendor.index.onSocketError((err) => {
  725. isSocketOpen = false;
  726. console.error("WebSocket 连接错误:", err);
  727. this.scheduleReconnect();
  728. });
  729. },
  730. scheduleReconnect() {
  731. if (this.reconnectCount >= this.maxReconnectAttempts) {
  732. console.log("已达到最大重连次数,不再尝试连接");
  733. common_vendor.index.showToast({
  734. title: "连接失败,请刷新页面重试",
  735. icon: "none"
  736. });
  737. return;
  738. }
  739. clearTimeout(this.reconnectTimer);
  740. this.closeWebSocket();
  741. const delay = Math.min(1e3 * Math.pow(2, this.reconnectCount), 3e4);
  742. console.log(`将在 ${delay}ms 后尝试第 ${this.reconnectCount + 1} 次重连`);
  743. this.reconnectTimer = setTimeout(() => {
  744. this.initSocket();
  745. }, delay);
  746. },
  747. sendMsg() {
  748. if (isSocketOpen) {
  749. const data = {
  750. liveId: this.liveId,
  751. userId: this.userinfo.userId,
  752. userType: 0,
  753. cmd: "sendMsg",
  754. msg: this.value,
  755. nickName: this.userinfo.nickName,
  756. avatar: this.userinfo.avatar
  757. };
  758. if (this.value == "") {
  759. common_vendor.index.showToast({
  760. title: "不能发送空消息",
  761. icon: "none"
  762. });
  763. } else {
  764. socket.send({
  765. data: JSON.stringify(data),
  766. success: () => {
  767. console.log("发送成功");
  768. this.value = "";
  769. },
  770. fail: () => {
  771. console.log("发送失败");
  772. }
  773. });
  774. }
  775. }
  776. }
  777. }
  778. };
  779. if (!Array) {
  780. const _easycom_u_avatar2 = common_vendor.resolveComponent("u-avatar");
  781. const _easycom_u_input2 = common_vendor.resolveComponent("u-input");
  782. const _easycom_u_icon2 = common_vendor.resolveComponent("u-icon");
  783. const _easycom_u_popup2 = common_vendor.resolveComponent("u-popup");
  784. (_easycom_u_avatar2 + _easycom_u_input2 + _easycom_u_icon2 + _easycom_u_popup2)();
  785. }
  786. const _easycom_u_avatar = () => "../../uni_modules/uview-plus/components/u-avatar/u-avatar.js";
  787. const _easycom_u_input = () => "../../uni_modules/uview-plus/components/u-input/u-input.js";
  788. const _easycom_u_icon = () => "../../uni_modules/uview-plus/components/u-icon/u-icon.js";
  789. const _easycom_u_popup = () => "../../uni_modules/uview-plus/components/u-popup/u-popup.js";
  790. if (!Math) {
  791. (_easycom_u_avatar + _easycom_u_input + _easycom_u_icon + _easycom_u_popup)();
  792. }
  793. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  794. return common_vendor.e({
  795. a: common_vendor.o((...args) => $options.goBack && $options.goBack(...args)),
  796. b: common_assets._imports_0$1,
  797. c: common_vendor.p({
  798. src: $data.livedata.liveImgUrl,
  799. size: "32"
  800. }),
  801. d: common_vendor.t($data.livedata.liveName ? $data.livedata.liveName : "芳华未来"),
  802. e: common_vendor.t($data.liveViewData.follow || $data.liveViewData.follow === 0 ? $data.liveViewData.follow : 0),
  803. f: $data.isFollow
  804. }, $data.isFollow ? {
  805. g: common_vendor.o((...args) => $options.onFollow && $options.onFollow(...args))
  806. } : {
  807. h: common_vendor.o((...args) => $options.onFollow && $options.onFollow(...args))
  808. }, {
  809. i: common_vendor.f($options.filteredViewers, (item, index, i0) => {
  810. return {
  811. a: index,
  812. b: item.avatar
  813. };
  814. }),
  815. j: common_vendor.t($data.liveViewData.online),
  816. k: common_vendor.o(($event) => $options.onLike()),
  817. l: common_assets._imports_1$1,
  818. m: common_vendor.t($data.liveViewData.like),
  819. n: common_assets._imports_2,
  820. o: common_vendor.o(($event) => $options.goStore()),
  821. p: common_assets._imports_3,
  822. q: common_assets._imports_4,
  823. r: common_vendor.t($data.orderUser.userName ? $options.maskString($data.orderUser.userName) : ""),
  824. s: common_vendor.t($data.orderUser.count || 0),
  825. t: $data.livingUrl
  826. }, $data.livingUrl ? {
  827. v: $data.livingUrl,
  828. w: $data.autoplay,
  829. x: common_vendor.o((...args) => $options.videoError && $options.videoError(...args))
  830. } : {}, {
  831. y: $data.videoUrl
  832. }, $data.videoUrl ? {
  833. z: $data.videoUrl,
  834. A: $data.autoplay,
  835. B: common_vendor.o((...args) => $options.videoError && $options.videoError(...args))
  836. } : {}, {
  837. C: $data.isScreen ? "100vh" : "",
  838. D: common_vendor.f($data.talklist, (item, index, i0) => {
  839. return {
  840. a: common_vendor.t(item.msg),
  841. b: item.index,
  842. c: `list_${index}`,
  843. d: item.cmd == "announcement"
  844. };
  845. }),
  846. E: common_vendor.t($data.messageContent),
  847. F: common_vendor.f($data.talklist, (item, index, i0) => {
  848. return {
  849. a: common_vendor.t(item.nickName),
  850. b: common_vendor.t(item.msg),
  851. c: item.index,
  852. d: `list_${index}`,
  853. e: item.cmd == "sendMsg"
  854. };
  855. }),
  856. G: $data.showWelcomeMessage
  857. }, $data.showWelcomeMessage ? {
  858. H: common_vendor.f($data.talklist, (item, index, i0) => {
  859. return {
  860. a: common_vendor.t(item.nickName),
  861. b: common_vendor.t(item.msg),
  862. c: item.index,
  863. d: `list_${index}`,
  864. e: item.cmd == "entry" || item.cmd == "out"
  865. };
  866. }),
  867. I: common_vendor.t($data.messageContent)
  868. } : {}, {
  869. J: $data.scrollIntoView,
  870. K: common_vendor.o($options.sendMsg),
  871. L: common_vendor.o($options.sendMsg),
  872. M: common_vendor.o(($event) => $data.value = $event),
  873. N: common_vendor.p({
  874. placeholder: $data.placeholderText,
  875. border: "none",
  876. customStyle: "font-size:24rpx;",
  877. shape: "circle",
  878. color: "#fff",
  879. placeholderStyle: "color:#e7e7e7",
  880. modelValue: $data.value
  881. }),
  882. O: common_assets._imports_5,
  883. P: common_vendor.o(($event) => $data.shopping = !$data.shopping),
  884. Q: common_assets._imports_6,
  885. R: common_vendor.o(($event) => $data.showziliao = !$data.showziliao),
  886. S: common_vendor.o(($event) => $data.showziliao = !$data.showziliao),
  887. T: common_vendor.p({
  888. name: "close",
  889. size: "18"
  890. }),
  891. U: $data.livedata.liveDesc,
  892. V: common_vendor.o($options.closes),
  893. W: common_vendor.p({
  894. show: $data.showziliao,
  895. round: "20",
  896. bgColor: "#fffee1"
  897. }),
  898. X: common_vendor.p({
  899. src: $data.livedata.liveImgUrl,
  900. size: "36"
  901. }),
  902. Y: common_assets._imports_1,
  903. Z: common_assets._imports_10,
  904. aa: common_vendor.o((...args) => $options.onCollect && $options.onCollect(...args)),
  905. ab: common_assets._imports_9,
  906. ac: common_vendor.o((...args) => $options.goOrderList && $options.goOrderList(...args)),
  907. ad: common_vendor.f($data.products, (item, index, i0) => {
  908. return common_vendor.e({
  909. a: item.imgUrl,
  910. b: common_vendor.t(index + 1),
  911. c: common_vendor.t(item.productName),
  912. d: common_vendor.t(item.productIntroduce),
  913. e: common_vendor.t(item.sales),
  914. f: common_vendor.t(Math.trunc(item.price)),
  915. g: common_vendor.t($options.getPureDecimal(item.price) ? $options.getPureDecimal(item.price) : "00"),
  916. h: common_vendor.o(($event) => $options.onCollect(item), index),
  917. i: item.status == 1
  918. }, item.status == 1 ? {
  919. j: common_vendor.o(($event) => $options.goShop(item.productId, item.goodsId), index)
  920. } : item.status == 0 ? {
  921. l: common_vendor.o(($event) => $options.goShop(item.productId), index)
  922. } : {}, {
  923. k: item.status == 0,
  924. m: index
  925. });
  926. }),
  927. ae: common_assets._imports_10,
  928. af: $data.boxHeight + "px",
  929. ag: common_vendor.o($options.closeshop),
  930. ah: common_vendor.o($options.open),
  931. ai: common_vendor.p({
  932. show: $data.shopping,
  933. round: "20rpx",
  934. bgColor: "#f3f5f9"
  935. })
  936. });
  937. }
  938. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-471975a0"]]);
  939. wx.createPage(MiniProgramPage);