living.js 34 KB

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