live.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const api_home = require("../../api/home.js");
  4. const common_assets = require("../../common/assets.js");
  5. var wsUrl = "ws://192.168.10.170:7114/app/webSocket";
  6. var pingpangTimes = null;
  7. var isSocketOpen = false;
  8. var socket = null;
  9. const _sfc_main = {
  10. data() {
  11. return {
  12. icon: "公告:",
  13. icon2: "广播:",
  14. text1: "组件功能丰富多端兼容让您快速集成开箱即用",
  15. text2: "丰富多端兼容让您快速集成开箱即用",
  16. srcAvatar: "",
  17. acttab: 0,
  18. value: "",
  19. talkdisabled: false,
  20. //是否禁用
  21. placeholderText: "请输入内容",
  22. showadd: false,
  23. autoplay: true,
  24. //自动播放
  25. userinfo: "",
  26. //用户信息
  27. path: "http://192.168.10.170/dev-api",
  28. livedata: {},
  29. bufferRate: 0,
  30. // 缓冲时间
  31. playDuration: 0,
  32. //视频播放时间
  33. videoContext: "",
  34. thistime: common_vendor.index.$u.timeFormat(/* @__PURE__ */ new Date(), "yyyy-mm-dd hh:MM:ss"),
  35. codeimg: "",
  36. userid: "4",
  37. liveId: "5",
  38. upDown: true,
  39. //是否视频显示隐藏
  40. isLongPress: false,
  41. // 是否长按
  42. timeout: null,
  43. // 计时器
  44. list1: [
  45. {
  46. name: "讨论"
  47. },
  48. // {
  49. // name: '问答'
  50. // },
  51. {
  52. name: "资料"
  53. }
  54. ],
  55. talklist: [],
  56. scrollIntoView: "",
  57. showWelcomeMessage: false,
  58. messageContent: ""
  59. };
  60. },
  61. mounted() {
  62. this.initSocket();
  63. var that = this;
  64. common_vendor.index.$on("initSocket", () => {
  65. that.initSocket();
  66. });
  67. common_vendor.index.$on("sendMsg", (item) => {
  68. that.sendMsg(item);
  69. });
  70. common_vendor.index.$on("closeWebSocket", () => {
  71. that.closeWebSocket();
  72. });
  73. this.getEWechatSdk();
  74. this.userinfo = JSON.parse(common_vendor.index.getStorageSync("userInfo"));
  75. this.getliving();
  76. this.gettalklist();
  77. },
  78. onReady: function(res) {
  79. this.videoContext = common_vendor.index.createVideoContext("myVideo");
  80. },
  81. onLoad() {
  82. },
  83. methods: {
  84. gettalklist() {
  85. const param = {
  86. id: this.liveId
  87. };
  88. api_home.gettextlist(param).then((res) => {
  89. if (res.code == 200) {
  90. this.talklist = res.data;
  91. this.$nextTick(() => {
  92. this.scrollIntoView = `list_${this.talklist.length - 1}`;
  93. });
  94. }
  95. });
  96. },
  97. longPress() {
  98. this.timeout = setTimeout(() => {
  99. this.isLongPress = true;
  100. common_vendor.index.saveImageToPhotosAlbum({
  101. filePath: this.livedata.qwQrCode,
  102. // 图片的本地路径或网络路径
  103. success: () => {
  104. common_vendor.index.showToast({
  105. title: "保存成功"
  106. });
  107. },
  108. fail: () => {
  109. common_vendor.index.showToast({
  110. title: "保存失败",
  111. icon: "none"
  112. });
  113. }
  114. });
  115. }, 500);
  116. },
  117. cancelLongPress() {
  118. clearTimeout(this.timeout);
  119. this.isLongPress = false;
  120. },
  121. getliving() {
  122. this.gettalklist();
  123. const param = {
  124. id: this.liveId
  125. };
  126. api_home.getlive(param).then((res) => {
  127. if (res.code == 200) {
  128. this.livedata = res.data;
  129. this.codeimg = res.data.qwQrCode;
  130. if (this.livedata.status == 2) {
  131. this.autoplay = true;
  132. this.videoContext.seek(this.livedata.nowDuration);
  133. } else {
  134. this.autoplay = false;
  135. this.placeholderText = "直播开始才能发言讨论";
  136. this.talkdisabled = true;
  137. }
  138. } else {
  139. common_vendor.index.showToast({
  140. title: res.msg,
  141. icon: "none",
  142. duration: 2e3
  143. });
  144. }
  145. });
  146. },
  147. addwechat() {
  148. this.showadd = !this.showadd;
  149. },
  150. open() {
  151. },
  152. close() {
  153. this.showadd = !this.showadd;
  154. },
  155. tabClick(e) {
  156. this.acttab = e.index;
  157. if (e.index == 0) {
  158. this.$nextTick(() => {
  159. this.gettalklist();
  160. });
  161. }
  162. },
  163. getEWechatSdk() {
  164. let eWechatSdk = "";
  165. if (/(Android)/i.test(navigator.userAgent)) {
  166. eWechatSdk = "jWeixin";
  167. } else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
  168. eWechatSdk = "wx";
  169. } else {
  170. eWechatSdk = "jWeixin";
  171. }
  172. common_vendor.index.setStorageSync("wxSdk", eWechatSdk);
  173. },
  174. closeWebSocket() {
  175. if (socket != null) {
  176. common_vendor.index.closeSocket();
  177. }
  178. clearInterval(pingpangTimes);
  179. },
  180. reConnect() {
  181. var that = this;
  182. try {
  183. common_vendor.index.closeSocket();
  184. } catch (e) {
  185. }
  186. setTimeout(function() {
  187. that.initSocket();
  188. }, 1e4);
  189. },
  190. initSocket() {
  191. var that = this;
  192. socket = common_vendor.index.connectSocket({
  193. // url: wsUrl+"?userId=40486&liveId=2",
  194. // url: wsUrl + "?userId=" + this.userid + "&liveId=" +
  195. // this.liveId+"&AppToken="+'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI0IiwiaWF0IjoxNzQwNzIxMDQ1LCJleHAiOjE3NDEzMjU4NDV9.PgS83JTMSJgVFD6vGAhUsEPbS6Az4yMyX8wVug61TZNTB3092CtvANo-AB9ZG4NdSBLgLrf4litM3vvTk-FK0g',
  196. url: wsUrl + "?userId=666&liveId=777&AppToken=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI2NjYiLCJpYXQiOjE3NTE4NzQ0NzEsImV4cCI6MTc4MzQxMDQ3MX0.3uxTTb0qXygmaY9ItovMclxJCNhNEi6kFEqmfLGg4lP2PYzCPODsVjW4PjXNu6EYsl5eYyESltHWcwBnaNkilQ&signature=ff21bfb41ddd5f2e31d6f5bf32ec565aab9c518614d139fa26727468ce701237&userType=0&timestamp=666",
  197. multiple: true,
  198. success: (res) => {
  199. clearInterval(pingpangTimes);
  200. common_vendor.index.onSocketMessage((res2) => {
  201. const redata = JSON.parse(res2.data);
  202. this.talklist.push(redata.data);
  203. this.$nextTick(() => {
  204. this.scrollIntoView = `list_${this.talklist.length - 1}`;
  205. });
  206. if (redata.cmd == "deleteId") {
  207. common_vendor.index.$emit("deleteId");
  208. } else if (redata.cmd == "init") {
  209. common_vendor.index.$emit("init", redata.data);
  210. } else if (redata.cmd == "reload") {
  211. common_vendor.index.$emit("reload");
  212. } else if (redata.cmd == "sendStatus") {
  213. common_vendor.index.$emit("sendStatus", redata.data);
  214. } else if (redata.data.cmd == "entry") {
  215. this.showWelcomeMessage = true;
  216. common_vendor.index.$emit("entry", redata.data);
  217. console.log(redata.data);
  218. }
  219. });
  220. },
  221. error: (res) => {
  222. common_vendor.index.$emit("websocket", 0);
  223. }
  224. });
  225. common_vendor.index.onSocketOpen(() => {
  226. isSocketOpen = true;
  227. console.log("WebSocket连接已打开!!");
  228. common_vendor.index.showToast({
  229. title: "插件已打开",
  230. icon: "none"
  231. });
  232. });
  233. common_vendor.index.onSocketClose(() => {
  234. isSocketOpen = false;
  235. clearInterval(pingpangTimes);
  236. console.log("WebSocket连接已关闭!");
  237. common_vendor.index.showToast({
  238. title: "插件离线",
  239. icon: "none"
  240. });
  241. that.reConnect();
  242. });
  243. common_vendor.index.onSocketError(() => {
  244. isSocketOpen = false;
  245. clearInterval(pingpangTimes);
  246. console.log("WebSocket连接打开失败");
  247. common_vendor.index.showToast({
  248. title: "插件离线",
  249. icon: "none"
  250. });
  251. common_vendor.index.showModal({
  252. content: "聊天连接失败是否重新尝试连接",
  253. success() {
  254. that.reConnect();
  255. }
  256. });
  257. });
  258. },
  259. sendMsg() {
  260. if (isSocketOpen) {
  261. const data = {
  262. liveId: this.livedata.liveId,
  263. userId: this.userinfo.userId,
  264. userType: 0,
  265. cmd: "sendMsg",
  266. msg: this.value,
  267. nickName: this.userinfo.nickName,
  268. avatar: this.userinfo.avatar
  269. };
  270. if (this.value == "") {
  271. common_vendor.index.showToast({
  272. title: "不能发送空消息",
  273. icon: "none"
  274. });
  275. } else {
  276. socket.send({
  277. data: JSON.stringify(data),
  278. success: () => {
  279. console.log("发送成功");
  280. this.value = "";
  281. },
  282. fail: () => {
  283. console.log("发送失败");
  284. }
  285. });
  286. }
  287. }
  288. }
  289. }
  290. };
  291. if (!Array) {
  292. const _easycom_u_tabs2 = common_vendor.resolveComponent("u-tabs");
  293. const _easycom_u_avatar2 = common_vendor.resolveComponent("u-avatar");
  294. const _easycom_u_input2 = common_vendor.resolveComponent("u-input");
  295. const _easycom_u_icon2 = common_vendor.resolveComponent("u-icon");
  296. const _easycom_u_popup2 = common_vendor.resolveComponent("u-popup");
  297. (_easycom_u_tabs2 + _easycom_u_avatar2 + _easycom_u_input2 + _easycom_u_icon2 + _easycom_u_popup2)();
  298. }
  299. const _easycom_u_tabs = () => "../../uni_modules/uview-plus/components/u-tabs/u-tabs.js";
  300. const _easycom_u_avatar = () => "../../uni_modules/uview-plus/components/u-avatar/u-avatar.js";
  301. const _easycom_u_input = () => "../../uni_modules/uview-plus/components/u-input/u-input.js";
  302. const _easycom_u_icon = () => "../../uni_modules/uview-plus/components/u-icon/u-icon.js";
  303. const _easycom_u_popup = () => "../../uni_modules/uview-plus/components/u-popup/u-popup.js";
  304. if (!Math) {
  305. (_easycom_u_tabs + _easycom_u_avatar + _easycom_u_input + _easycom_u_icon + _easycom_u_popup)();
  306. }
  307. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  308. return common_vendor.e({
  309. a: $data.upDown
  310. }, $data.upDown ? common_vendor.e({
  311. b: $data.path + $data.livedata.videoUrl,
  312. c: $data.autoplay,
  313. d: $data.livedata.liveImgUrl,
  314. e: !$data.autoplay
  315. }, !$data.autoplay ? common_vendor.e({
  316. f: $data.livedata.status == 1
  317. }, $data.livedata.status == 1 ? {} : {}, {
  318. g: $data.livedata.status == 3
  319. }, $data.livedata.status == 3 ? {} : {}, {
  320. h: common_vendor.o(($event) => $data.showadd = !$data.showadd)
  321. }) : {}) : {}, {
  322. i: common_vendor.o($options.tabClick),
  323. j: common_vendor.p({
  324. list: $data.list1,
  325. inactiveStyle: "color:#888",
  326. itemStyle: "width:120rpx;height: 80rpx;"
  327. }),
  328. k: common_vendor.o(($event) => $options.getliving()),
  329. l: common_assets._imports_0$3,
  330. m: $data.upDown,
  331. n: common_vendor.o(($event) => $data.upDown = !$data.upDown),
  332. o: common_assets._imports_1$1,
  333. p: !$data.upDown,
  334. q: common_vendor.o(($event) => $data.upDown = !$data.upDown),
  335. r: $data.acttab == 0
  336. }, $data.acttab == 0 ? common_vendor.e({
  337. s: common_vendor.f($data.talklist, (item, index, i0) => {
  338. return common_vendor.e({
  339. a: "81fcd0c9-1-" + i0,
  340. b: common_vendor.p({
  341. src: item.avatar,
  342. size: "30"
  343. }),
  344. c: item.userId == $data.userinfo.userId && item.cmd == "sendMsg"
  345. }, item.userId == $data.userinfo.userId && item.cmd == "sendMsg" ? {} : {}, {
  346. d: item.userType == 1
  347. }, item.userType == 1 ? {} : {}, {
  348. e: common_vendor.t(item.nickName),
  349. f: common_vendor.t(item.msg),
  350. g: item.index,
  351. h: `list_${index}`,
  352. i: item.cmd == "sendMsg"
  353. });
  354. }),
  355. t: $data.showWelcomeMessage
  356. }, $data.showWelcomeMessage ? {
  357. v: common_vendor.f($data.talklist, (item, index, i0) => {
  358. return common_vendor.e({
  359. a: item.userType == 1
  360. }, item.userType == 1 ? {} : {}, {
  361. b: common_vendor.t(item.nickName),
  362. c: common_vendor.t(item.msg),
  363. d: item.index,
  364. e: `list_${index}`,
  365. f: item.cmd == "entry" || item.cmd == "out"
  366. });
  367. })
  368. } : {}, {
  369. w: $data.scrollIntoView,
  370. x: common_vendor.o(($event) => $data.value = $event),
  371. y: common_vendor.p({
  372. placeholder: $data.placeholderText,
  373. border: "none",
  374. customStyle: "background:#f1f7f7;padding:12rpx;padding-left:40rpx",
  375. shape: "circle",
  376. modelValue: $data.value
  377. }),
  378. z: common_vendor.o((...args) => $options.sendMsg && $options.sendMsg(...args))
  379. }) : {}, {
  380. A: $data.acttab == 1
  381. }, $data.acttab == 1 ? {
  382. B: $data.livedata.liveDesc,
  383. C: !$data.livedata.liveDesc
  384. } : {}, {
  385. D: common_assets._imports_2$1,
  386. E: common_vendor.o((...args) => $options.addwechat && $options.addwechat(...args)),
  387. F: common_vendor.o(($event) => $data.showadd = !$data.showadd),
  388. G: common_vendor.p({
  389. name: "close",
  390. size: "18"
  391. }),
  392. H: $data.codeimg,
  393. I: common_vendor.o((...args) => $options.longPress && $options.longPress(...args)),
  394. J: common_vendor.o((...args) => $options.cancelLongPress && $options.cancelLongPress(...args)),
  395. K: common_vendor.o($options.close),
  396. L: common_vendor.o($options.open),
  397. M: common_vendor.p({
  398. show: $data.showadd,
  399. round: "20rpx",
  400. bgColor: "#fffee1"
  401. })
  402. });
  403. }
  404. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-81fcd0c9"]]);
  405. wx.createPage(MiniProgramPage);