living.js 37 KB

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