live.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. "use strict";
  2. var common_vendor = require("../../common/vendor.js");
  3. var api_home = require("../../api/home.js");
  4. require("../../common/request.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: "\u516C\u544A:",
  13. icon2: "\u5E7F\u64AD:",
  14. text1: "\u7EC4\u4EF6\u529F\u80FD\u4E30\u5BCC\u591A\u7AEF\u517C\u5BB9\u8BA9\u60A8\u5FEB\u901F\u96C6\u6210\u5F00\u7BB1\u5373\u7528",
  15. text2: "\u4E30\u5BCC\u591A\u7AEF\u517C\u5BB9\u8BA9\u60A8\u5FEB\u901F\u96C6\u6210\u5F00\u7BB1\u5373\u7528",
  16. srcAvatar: "",
  17. acttab: 0,
  18. value: "",
  19. talkdisabled: false,
  20. placeholderText: "\u8BF7\u8F93\u5165\u5185\u5BB9",
  21. showadd: false,
  22. autoplay: true,
  23. userinfo: "",
  24. path: "http://192.168.10.170/dev-api",
  25. livedata: {},
  26. bufferRate: 0,
  27. playDuration: 0,
  28. videoContext: "",
  29. thistime: common_vendor.index.$u.timeFormat(new Date(), "yyyy-mm-dd hh:MM:ss"),
  30. codeimg: "",
  31. userid: "4",
  32. liveId: "5",
  33. upDown: true,
  34. isLongPress: false,
  35. timeout: null,
  36. list1: [
  37. {
  38. name: "\u8BA8\u8BBA"
  39. },
  40. {
  41. name: "\u8D44\u6599"
  42. }
  43. ],
  44. talklist: [],
  45. scrollIntoView: "",
  46. showWelcomeMessage: false,
  47. messageContent: ""
  48. };
  49. },
  50. mounted() {
  51. this.initSocket();
  52. var that = this;
  53. common_vendor.index.$on("initSocket", () => {
  54. that.initSocket();
  55. });
  56. common_vendor.index.$on("sendMsg", (item) => {
  57. that.sendMsg(item);
  58. });
  59. common_vendor.index.$on("closeWebSocket", () => {
  60. that.closeWebSocket();
  61. });
  62. this.getEWechatSdk();
  63. this.userinfo = JSON.parse(common_vendor.index.getStorageSync("userInfo"));
  64. this.getliving();
  65. this.gettalklist();
  66. },
  67. onReady: function(res) {
  68. this.videoContext = common_vendor.index.createVideoContext("myVideo");
  69. },
  70. onLoad() {
  71. },
  72. methods: {
  73. gettalklist() {
  74. const param = {
  75. id: this.liveId
  76. };
  77. api_home.gettextlist(param).then((res) => {
  78. if (res.code == 200) {
  79. this.talklist = res.data;
  80. this.$nextTick(() => {
  81. this.scrollIntoView = `list_${this.talklist.length - 1}`;
  82. });
  83. }
  84. });
  85. },
  86. longPress() {
  87. this.timeout = setTimeout(() => {
  88. this.isLongPress = true;
  89. common_vendor.index.saveImageToPhotosAlbum({
  90. filePath: this.livedata.qwQrCode,
  91. success: () => {
  92. common_vendor.index.showToast({
  93. title: "\u4FDD\u5B58\u6210\u529F"
  94. });
  95. },
  96. fail: () => {
  97. common_vendor.index.showToast({
  98. title: "\u4FDD\u5B58\u5931\u8D25",
  99. icon: "none"
  100. });
  101. }
  102. });
  103. }, 500);
  104. },
  105. cancelLongPress() {
  106. clearTimeout(this.timeout);
  107. this.isLongPress = false;
  108. },
  109. getliving() {
  110. this.gettalklist();
  111. const param = {
  112. id: this.liveId
  113. };
  114. api_home.getlive(param).then((res) => {
  115. if (res.code == 200) {
  116. this.livedata = res.data;
  117. this.codeimg = res.data.qwQrCode;
  118. if (this.livedata.status == 2) {
  119. this.autoplay = true;
  120. this.videoContext.seek(this.livedata.nowDuration);
  121. } else {
  122. this.autoplay = false;
  123. this.placeholderText = "\u76F4\u64AD\u5F00\u59CB\u624D\u80FD\u53D1\u8A00\u8BA8\u8BBA";
  124. this.talkdisabled = true;
  125. }
  126. } else {
  127. common_vendor.index.showToast({
  128. title: res.msg,
  129. icon: "none",
  130. duration: 2e3
  131. });
  132. }
  133. });
  134. },
  135. addwechat() {
  136. this.showadd = !this.showadd;
  137. },
  138. open() {
  139. },
  140. close() {
  141. this.showadd = !this.showadd;
  142. },
  143. tabClick(e) {
  144. this.acttab = e.index;
  145. if (e.index == 0) {
  146. this.$nextTick(() => {
  147. this.gettalklist();
  148. });
  149. }
  150. },
  151. getEWechatSdk() {
  152. let eWechatSdk = "";
  153. if (/(Android)/i.test(navigator.userAgent)) {
  154. eWechatSdk = "jWeixin";
  155. } else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
  156. eWechatSdk = "wx";
  157. } else {
  158. eWechatSdk = "jWeixin";
  159. }
  160. common_vendor.index.setStorageSync("wxSdk", eWechatSdk);
  161. },
  162. closeWebSocket() {
  163. if (socket != null) {
  164. common_vendor.index.closeSocket();
  165. }
  166. clearInterval(pingpangTimes);
  167. },
  168. reConnect() {
  169. var that = this;
  170. try {
  171. common_vendor.index.closeSocket();
  172. } catch (e) {
  173. }
  174. setTimeout(function() {
  175. that.initSocket();
  176. }, 1e4);
  177. },
  178. initSocket() {
  179. var that = this;
  180. socket = common_vendor.index.connectSocket({
  181. url: wsUrl + "?userId=666&liveId=777&AppToken=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI2NjYiLCJpYXQiOjE3NTE4NzQ0NzEsImV4cCI6MTc4MzQxMDQ3MX0.3uxTTb0qXygmaY9ItovMclxJCNhNEi6kFEqmfLGg4lP2PYzCPODsVjW4PjXNu6EYsl5eYyESltHWcwBnaNkilQ&signature=ff21bfb41ddd5f2e31d6f5bf32ec565aab9c518614d139fa26727468ce701237&userType=0&timestamp=666",
  182. multiple: true,
  183. success: (res) => {
  184. clearInterval(pingpangTimes);
  185. common_vendor.index.onSocketMessage((res2) => {
  186. const redata = JSON.parse(res2.data);
  187. this.talklist.push(redata.data);
  188. this.$nextTick(() => {
  189. this.scrollIntoView = `list_${this.talklist.length - 1}`;
  190. });
  191. if (redata.cmd == "deleteId") {
  192. common_vendor.index.$emit("deleteId");
  193. } else if (redata.cmd == "init") {
  194. common_vendor.index.$emit("init", redata.data);
  195. } else if (redata.cmd == "reload") {
  196. common_vendor.index.$emit("reload");
  197. } else if (redata.cmd == "sendStatus") {
  198. common_vendor.index.$emit("sendStatus", redata.data);
  199. } else if (redata.data.cmd == "entry") {
  200. this.showWelcomeMessage = true;
  201. common_vendor.index.$emit("entry", redata.data);
  202. console.log(redata.data);
  203. }
  204. });
  205. },
  206. error: (res) => {
  207. common_vendor.index.$emit("websocket", 0);
  208. }
  209. });
  210. common_vendor.index.onSocketOpen(() => {
  211. isSocketOpen = true;
  212. console.log("WebSocket\u8FDE\u63A5\u5DF2\u6253\u5F00\uFF01!");
  213. common_vendor.index.showToast({
  214. title: "\u63D2\u4EF6\u5DF2\u6253\u5F00",
  215. icon: "none"
  216. });
  217. });
  218. common_vendor.index.onSocketClose(() => {
  219. isSocketOpen = false;
  220. clearInterval(pingpangTimes);
  221. console.log("WebSocket\u8FDE\u63A5\u5DF2\u5173\u95ED\uFF01");
  222. common_vendor.index.showToast({
  223. title: "\u63D2\u4EF6\u79BB\u7EBF",
  224. icon: "none"
  225. });
  226. that.reConnect();
  227. });
  228. common_vendor.index.onSocketError(() => {
  229. isSocketOpen = false;
  230. clearInterval(pingpangTimes);
  231. console.log("WebSocket\u8FDE\u63A5\u6253\u5F00\u5931\u8D25");
  232. common_vendor.index.showToast({
  233. title: "\u63D2\u4EF6\u79BB\u7EBF",
  234. icon: "none"
  235. });
  236. common_vendor.index.showModal({
  237. content: "\u804A\u5929\u8FDE\u63A5\u5931\u8D25\u662F\u5426\u91CD\u65B0\u5C1D\u8BD5\u8FDE\u63A5",
  238. success() {
  239. that.reConnect();
  240. }
  241. });
  242. });
  243. },
  244. sendMsg() {
  245. if (isSocketOpen) {
  246. const data = {
  247. liveId: this.livedata.liveId,
  248. userId: this.userinfo.userId,
  249. userType: 0,
  250. cmd: "sendMsg",
  251. msg: this.value,
  252. nickName: this.userinfo.nickName,
  253. avatar: this.userinfo.avatar
  254. };
  255. if (this.value == "") {
  256. common_vendor.index.showToast({
  257. title: "\u4E0D\u80FD\u53D1\u9001\u7A7A\u6D88\u606F",
  258. icon: "none"
  259. });
  260. } else {
  261. socket.send({
  262. data: JSON.stringify(data),
  263. success: () => {
  264. console.log("\u53D1\u9001\u6210\u529F");
  265. this.value = "";
  266. },
  267. fail: () => {
  268. console.log("\u53D1\u9001\u5931\u8D25");
  269. }
  270. });
  271. }
  272. }
  273. }
  274. }
  275. };
  276. if (!Array) {
  277. const _easycom_u_tabs2 = common_vendor.resolveComponent("u-tabs");
  278. const _easycom_u_avatar2 = common_vendor.resolveComponent("u-avatar");
  279. const _easycom_u_input2 = common_vendor.resolveComponent("u-input");
  280. const _easycom_u_icon2 = common_vendor.resolveComponent("u-icon");
  281. const _easycom_u_popup2 = common_vendor.resolveComponent("u-popup");
  282. (_easycom_u_tabs2 + _easycom_u_avatar2 + _easycom_u_input2 + _easycom_u_icon2 + _easycom_u_popup2)();
  283. }
  284. const _easycom_u_tabs = () => "../../uni_modules/uview-plus/components/u-tabs/u-tabs.js";
  285. const _easycom_u_avatar = () => "../../uni_modules/uview-plus/components/u-avatar/u-avatar.js";
  286. const _easycom_u_input = () => "../../uni_modules/uview-plus/components/u-input/u-input.js";
  287. const _easycom_u_icon = () => "../../uni_modules/uview-plus/components/u-icon/u-icon.js";
  288. const _easycom_u_popup = () => "../../uni_modules/uview-plus/components/u-popup/u-popup.js";
  289. if (!Math) {
  290. (_easycom_u_tabs + _easycom_u_avatar + _easycom_u_input + _easycom_u_icon + _easycom_u_popup)();
  291. }
  292. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  293. return common_vendor.e({
  294. a: $data.upDown
  295. }, $data.upDown ? common_vendor.e({
  296. b: $data.path + $data.livedata.videoUrl,
  297. c: $data.autoplay,
  298. d: $data.livedata.liveImgUrl,
  299. e: !$data.autoplay
  300. }, !$data.autoplay ? common_vendor.e({
  301. f: $data.livedata.status == 1
  302. }, $data.livedata.status == 1 ? {} : {}, {
  303. g: $data.livedata.status == 3
  304. }, $data.livedata.status == 3 ? {} : {}, {
  305. h: common_vendor.o(($event) => $data.showadd = !$data.showadd)
  306. }) : {}) : {}, {
  307. i: common_vendor.o($options.tabClick),
  308. j: common_vendor.p({
  309. list: $data.list1,
  310. inactiveStyle: "color:#888",
  311. itemStyle: "width:120rpx;height: 80rpx;"
  312. }),
  313. k: common_vendor.o(($event) => $options.getliving()),
  314. l: $data.upDown,
  315. m: common_vendor.o(($event) => $data.upDown = !$data.upDown),
  316. n: !$data.upDown,
  317. o: common_vendor.o(($event) => $data.upDown = !$data.upDown),
  318. p: $data.acttab == 0
  319. }, $data.acttab == 0 ? common_vendor.e({
  320. q: common_vendor.f($data.talklist, (item, index, i0) => {
  321. return common_vendor.e({
  322. a: "281d25f3-1-" + i0,
  323. b: common_vendor.p({
  324. src: item.avatar,
  325. size: "30"
  326. }),
  327. c: item.userId == $data.userinfo.userId && item.cmd == "sendMsg"
  328. }, item.userId == $data.userinfo.userId && item.cmd == "sendMsg" ? {} : {}, {
  329. d: item.userType == 1
  330. }, item.userType == 1 ? {} : {}, {
  331. e: common_vendor.t(item.nickName),
  332. f: common_vendor.t(item.msg),
  333. g: item.index,
  334. h: `list_${index}`,
  335. i: item.cmd == "sendMsg"
  336. });
  337. }),
  338. r: $data.showWelcomeMessage
  339. }, $data.showWelcomeMessage ? {
  340. s: common_vendor.f($data.talklist, (item, index, i0) => {
  341. return common_vendor.e({
  342. a: item.userType == 1
  343. }, item.userType == 1 ? {} : {}, {
  344. b: common_vendor.t(item.nickName),
  345. c: common_vendor.t(item.msg),
  346. d: item.index,
  347. e: `list_${index}`,
  348. f: item.cmd == "entry" || item.cmd == "out"
  349. });
  350. })
  351. } : {}, {
  352. t: $data.scrollIntoView,
  353. v: common_vendor.o(($event) => $data.value = $event),
  354. w: common_vendor.p({
  355. placeholder: $data.placeholderText,
  356. border: "none",
  357. customStyle: "background:#f1f7f7;padding:12rpx;padding-left:40rpx",
  358. shape: "circle",
  359. modelValue: $data.value
  360. }),
  361. x: common_vendor.o((...args) => $options.sendMsg && $options.sendMsg(...args))
  362. }) : {}, {
  363. y: $data.acttab == 1
  364. }, $data.acttab == 1 ? {
  365. z: $data.livedata.liveDesc,
  366. A: !$data.livedata.liveDesc
  367. } : {}, {
  368. B: common_vendor.o((...args) => $options.addwechat && $options.addwechat(...args)),
  369. C: common_vendor.o(($event) => $data.showadd = !$data.showadd),
  370. D: common_vendor.p({
  371. name: "close",
  372. size: "18"
  373. }),
  374. E: $data.codeimg,
  375. F: common_vendor.o((...args) => $options.longPress && $options.longPress(...args)),
  376. G: common_vendor.o((...args) => $options.cancelLongPress && $options.cancelLongPress(...args)),
  377. H: common_vendor.o($options.close),
  378. I: common_vendor.o($options.open),
  379. J: common_vendor.p({
  380. show: $data.showadd,
  381. round: "20rpx",
  382. bgColor: "#fffee1"
  383. })
  384. });
  385. }
  386. var MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-281d25f3"], ["__file", "C:/Users/Administrator/Desktop/\u9879\u76EE/\u76F4\u64AD/liveH5-v3/pages/home/live.vue"]]);
  387. wx.createPage(MiniProgramPage);