living.js 31 KB

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