living.js 33 KB

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