living.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  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.data.cmd == "deleteId") {
  849. common_vendor.index.$emit("deleteId");
  850. } else if (redata.data.cmd == "init") {
  851. common_vendor.index.$emit("init", redata.data);
  852. } else if (redata.data.cmd == "reload") {
  853. common_vendor.index.$emit("reload");
  854. } else if (redata.data.cmd == "red") {
  855. console.log(" 领红包>>", redata);
  856. this.redInfo = JSON.parse(redata.data.data);
  857. } else if (redata.data.cmd == "sendRedPacketQuestion") {
  858. const list = JSON.parse(redata.data.data);
  859. this.redanswerAll = [...this.redanswerAll, ...list];
  860. if (this.redanswerAll[1].randomAmount !== null) {
  861. this.redanswertips = JSON.parse(this.redanswerAll[0].randomAmount);
  862. }
  863. console.log(this.redanswertips);
  864. console.log(this.redanswerAll);
  865. } else if (redata.data.cmd == "entry") {
  866. this.showWelcomeMessage = true;
  867. common_vendor.index.$emit("entry", redata.data);
  868. }
  869. });
  870. },
  871. fail: (res) => {
  872. common_vendor.index.$emit("websocket", 0);
  873. console.log(res);
  874. this.scheduleReconnect();
  875. }
  876. });
  877. common_vendor.index.onSocketOpen(() => {
  878. isSocketOpen = true;
  879. this.reconnectCount = 0;
  880. console.log("WebSocket 连接成功");
  881. this.startHeartbeat();
  882. });
  883. common_vendor.index.onSocketClose(() => {
  884. isSocketOpen = false;
  885. console.log("WebSocket连接已关闭!");
  886. this.scheduleReconnect();
  887. });
  888. common_vendor.index.onSocketError((err) => {
  889. isSocketOpen = false;
  890. console.error("WebSocket 连接错误:", err);
  891. this.scheduleReconnect();
  892. });
  893. },
  894. scheduleReconnect() {
  895. if (this.reconnectCount >= this.maxReconnectAttempts) {
  896. console.log("已达到最大重连次数,不再尝试连接");
  897. common_vendor.index.showToast({
  898. title: "连接失败,请刷新页面重试",
  899. icon: "none"
  900. });
  901. return;
  902. }
  903. clearTimeout(this.reconnectTimer);
  904. this.closeWebSocket();
  905. const delay = Math.min(1e3 * Math.pow(2, this.reconnectCount), 3e4);
  906. console.log(`将在 ${delay}ms 后尝试第 ${this.reconnectCount + 1} 次重连`);
  907. this.reconnectTimer = setTimeout(() => {
  908. this.initSocket();
  909. }, delay);
  910. },
  911. sendMsg() {
  912. if (isSocketOpen) {
  913. const data = {
  914. liveId: this.liveId,
  915. userId: this.userinfo.userId,
  916. userType: 0,
  917. cmd: "sendMsg",
  918. msg: this.value,
  919. nickName: this.userinfo.nickName,
  920. avatar: this.userinfo.avatar
  921. };
  922. if (this.value == "") {
  923. common_vendor.index.showToast({
  924. title: "不能发送空消息",
  925. icon: "none"
  926. });
  927. } else {
  928. socket.send({
  929. data: JSON.stringify(data),
  930. success: () => {
  931. console.log("发送成功");
  932. this.value = "";
  933. },
  934. fail: () => {
  935. console.log("发送失败");
  936. }
  937. });
  938. }
  939. }
  940. }
  941. }
  942. };
  943. if (!Array) {
  944. const _easycom_u_avatar2 = common_vendor.resolveComponent("u-avatar");
  945. const _easycom_u_input2 = common_vendor.resolveComponent("u-input");
  946. const _easycom_u_icon2 = common_vendor.resolveComponent("u-icon");
  947. const _easycom_u_popup2 = common_vendor.resolveComponent("u-popup");
  948. (_easycom_u_avatar2 + _easycom_u_input2 + _easycom_u_icon2 + _easycom_u_popup2)();
  949. }
  950. const _easycom_u_avatar = () => "../../uni_modules/uview-plus/components/u-avatar/u-avatar.js";
  951. const _easycom_u_input = () => "../../uni_modules/uview-plus/components/u-input/u-input.js";
  952. const _easycom_u_icon = () => "../../uni_modules/uview-plus/components/u-icon/u-icon.js";
  953. const _easycom_u_popup = () => "../../uni_modules/uview-plus/components/u-popup/u-popup.js";
  954. if (!Math) {
  955. (_easycom_u_avatar + _easycom_u_input + _easycom_u_icon + _easycom_u_popup)();
  956. }
  957. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  958. var _a, _b;
  959. return common_vendor.e({
  960. a: common_vendor.o((...args) => $options.goBack && $options.goBack(...args)),
  961. b: common_assets._imports_0$1,
  962. c: common_vendor.p({
  963. src: $data.livedata.liveImgUrl,
  964. size: "32"
  965. }),
  966. d: common_vendor.t($data.livedata.liveName ? $options.truncateString($data.livedata.liveName, 5) : "未命名"),
  967. e: common_vendor.t($data.liveViewData.follow || $data.liveViewData.follow === 0 ? $data.liveViewData.follow : 0),
  968. f: $data.isFollow
  969. }, $data.isFollow ? {
  970. g: common_vendor.o((...args) => $options.onFollow && $options.onFollow(...args))
  971. } : {
  972. h: common_vendor.o((...args) => $options.onFollow && $options.onFollow(...args))
  973. }, {
  974. i: common_vendor.f($options.filteredViewers, (item, index, i0) => {
  975. return {
  976. a: index,
  977. b: item.avatar
  978. };
  979. }),
  980. j: common_vendor.t($data.liveViewData.online || 0),
  981. k: ((_a = $data.redInfo) == null ? void 0 : _a.status) == 1
  982. }, ((_b = $data.redInfo) == null ? void 0 : _b.status) == 1 ? {
  983. l: common_vendor.o((...args) => $options.onRed && $options.onRed(...args)),
  984. m: common_assets._imports_1$1
  985. } : {}, {
  986. n: common_vendor.o((...args) => $options.onLike && $options.onLike(...args)),
  987. o: common_assets._imports_2,
  988. p: common_vendor.t($data.liveViewData.like),
  989. q: common_vendor.o(($event) => $options.goStore()),
  990. r: common_assets._imports_3,
  991. s: common_assets._imports_4,
  992. t: common_vendor.n($data.showType == 1 ? "siderow-group" : "side-group"),
  993. v: $data.showPurchasePrompt
  994. }, $data.showPurchasePrompt ? {
  995. w: common_assets._imports_5,
  996. x: common_vendor.t($data.orderUser.userName ? $options.maskString($data.orderUser.userName) : ""),
  997. y: common_vendor.t($data.orderUser.count || 0)
  998. } : {}, {
  999. z: $data.livingUrl
  1000. }, $data.livingUrl ? {
  1001. A: common_vendor.n($data.showType == 1 ? "video_row" : "videotop"),
  1002. B: $data.livingUrl,
  1003. C: $data.autoplay,
  1004. D: common_vendor.o((...args) => $options.videoError && $options.videoError(...args))
  1005. } : {}, {
  1006. E: $data.videoUrl
  1007. }, $data.videoUrl ? {
  1008. F: common_vendor.n($data.showType == 1 ? "video_row" : "videotop"),
  1009. G: $data.videoUrl,
  1010. H: $data.autoplay,
  1011. I: common_vendor.o((...args) => $options.videoError && $options.videoError(...args))
  1012. } : {}, {
  1013. J: common_vendor.f($data.talklist, (item, index, i0) => {
  1014. return {
  1015. a: common_vendor.t(item.msg),
  1016. b: item.index,
  1017. c: `list_${index}`,
  1018. d: item.cmd == "announcement"
  1019. };
  1020. }),
  1021. K: common_vendor.t($data.messageContent),
  1022. L: common_vendor.f($data.talklist, (item, index, i0) => {
  1023. return {
  1024. a: common_vendor.t(item.nickName),
  1025. b: common_vendor.t(item.msg),
  1026. c: item.index,
  1027. d: `list_${index}`,
  1028. e: item.cmd == "sendMsg"
  1029. };
  1030. }),
  1031. M: $data.showWelcomeMessage
  1032. }, $data.showWelcomeMessage ? {
  1033. N: common_vendor.f($data.talklist, (item, index, i0) => {
  1034. return {
  1035. a: common_vendor.t(item.nickName),
  1036. b: common_vendor.t(item.msg),
  1037. c: item.index,
  1038. d: `list_${index}`,
  1039. e: item.cmd == "entry" || item.cmd == "out"
  1040. };
  1041. }),
  1042. O: common_vendor.t($data.messageContent)
  1043. } : {}, {
  1044. P: $data.scrollIntoView,
  1045. Q: common_vendor.o($options.sendMsg),
  1046. R: common_vendor.o($options.sendMsg),
  1047. S: common_vendor.o(($event) => $data.value = $event),
  1048. T: common_vendor.p({
  1049. placeholder: $data.placeholderText,
  1050. border: "none",
  1051. customStyle: "font-size:24rpx;",
  1052. shape: "circle",
  1053. color: "#fff",
  1054. placeholderStyle: "color:#e7e7e7",
  1055. modelValue: $data.value
  1056. }),
  1057. U: common_assets._imports_6,
  1058. V: common_vendor.o(($event) => $data.shopping = !$data.shopping),
  1059. W: common_assets._imports_7,
  1060. X: common_vendor.o(($event) => $data.showziliao = !$data.showziliao),
  1061. Y: common_vendor.o(($event) => $data.showziliao = !$data.showziliao),
  1062. Z: common_vendor.p({
  1063. name: "close",
  1064. size: "18"
  1065. }),
  1066. aa: $data.livedata.liveDesc,
  1067. ab: common_vendor.o($options.closes),
  1068. ac: common_vendor.p({
  1069. show: $data.showziliao,
  1070. round: "20",
  1071. bgColor: "#fffee1"
  1072. }),
  1073. ad: common_vendor.p({
  1074. src: $data.store.logoUrl,
  1075. size: "36"
  1076. }),
  1077. ae: common_assets._imports_1,
  1078. af: common_vendor.o([($event) => $data.inputInfo = $event.detail.value, (...args) => $options.handleSearchInput && $options.handleSearchInput(...args)]),
  1079. ag: $data.inputInfo,
  1080. ah: $data.store.isFavorite
  1081. }, $data.store.isFavorite ? {
  1082. ai: common_vendor.o((...args) => $options.onStoreCollect && $options.onStoreCollect(...args)),
  1083. aj: common_assets._imports_12
  1084. } : {
  1085. ak: common_vendor.o((...args) => $options.onStoreCollect && $options.onStoreCollect(...args)),
  1086. al: common_assets._imports_13
  1087. }, {
  1088. am: common_assets._imports_11,
  1089. an: common_vendor.o((...args) => $options.goOrderList && $options.goOrderList(...args)),
  1090. ao: common_vendor.f($data.products, (item, index, i0) => {
  1091. return common_vendor.e({
  1092. a: item.imgUrl,
  1093. b: common_vendor.t(index + 1),
  1094. c: common_vendor.t(item.productName),
  1095. d: common_vendor.t(item.sales),
  1096. e: common_vendor.t(Math.trunc(item.price)),
  1097. f: common_vendor.t($options.getPureDecimal(item.price) ? $options.getPureDecimal(item.price) : "00"),
  1098. g: item.isFavorite
  1099. }, item.isFavorite ? {
  1100. h: common_vendor.o(($event) => $options.onGoodsCollect(item), index),
  1101. i: common_assets._imports_12
  1102. } : {
  1103. j: common_vendor.o(($event) => $options.onGoodsCollect(item), index),
  1104. k: common_assets._imports_13
  1105. }, {
  1106. l: item.status == 1
  1107. }, item.status == 1 ? {
  1108. m: common_vendor.o(($event) => $options.goShop(item.productId, item.goodsId), index)
  1109. } : item.status == 0 ? {
  1110. o: common_vendor.o(($event) => $options.goShop(item.productId), index)
  1111. } : {}, {
  1112. n: item.status == 0,
  1113. p: index
  1114. });
  1115. }),
  1116. ap: $data.boxHeight + "px",
  1117. aq: common_vendor.o($options.closeshop),
  1118. ar: common_vendor.o($options.open),
  1119. as: common_vendor.p({
  1120. show: $data.shopping,
  1121. round: "20rpx",
  1122. bgColor: "#f3f5f9"
  1123. })
  1124. });
  1125. }
  1126. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-471975a0"]]);
  1127. _sfc_main.__runtimeHooks = 2;
  1128. wx.createPage(MiniProgramPage);