playpusher.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. // pages/mine/wxml/playpusher/playpusher.js
  2. var time;
  3. const app = getApp();
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. kid: null,
  10. detailOpen: false,
  11. biddingOpen: false,
  12. animationData: {},
  13. liveRoomData: [],
  14. openData:{},
  15. thisId: null,
  16. record:[],
  17. nowTime: null,
  18. btnText: '竞价倒计时',
  19. submit: true,
  20. setInter: '',
  21. chatTime: null,
  22. chatList: [],
  23. prodUrl:app.globalData.prodUrl,
  24. timer : null,
  25. timear : 0,
  26. scrollTop : 300
  27. },
  28. /**
  29. * 生命周期函数--监听页面加载
  30. */
  31. onLoad: function (options) {
  32. let _this = this;
  33. //console.log(options.live_room_kid)
  34. this.setData({
  35. liveid: options.live_room_kid
  36. },function() {
  37. _this.data.timer = setInterval(() => {
  38. _this.getChatList();
  39. }, 1000)
  40. })
  41. //设置动态标题
  42. // wx.setNavigationBarTitle({
  43. // title: options.title
  44. // });
  45. //创建animation实例
  46. let animation = wx.createAnimation({
  47. duration: 200,
  48. timingFunction: 'linear'
  49. })
  50. this.animation = animation;
  51. //动态设置页面高度
  52. wx.getSystemInfo({
  53. success: function (res) {
  54. _this.setData({
  55. pageWidth: res.windowWidth,
  56. pageHeight: res.windowHeight
  57. });
  58. }
  59. });
  60. //初始化拍卖会数据
  61. _this.getLiveRoomData(options.live_room_kid);
  62. //创建LivePusherContext实例
  63. _this.ctx = wx.createLivePusherContext('pusher');
  64. },
  65. /**
  66. * 生命周期函数--监听页面初次渲染完成
  67. */
  68. onReady: function () {
  69. },
  70. /**
  71. * 生命周期函数--监听页面显示
  72. */
  73. onShow: function () {
  74. },
  75. /**
  76. * 生命周期函数--监听页面隐藏
  77. */
  78. onHide: function () {
  79. },
  80. /**
  81. * 生命周期函数--监听页面卸载
  82. */
  83. onUnload: function () {
  84. clearInterval(time);
  85. clearInterval(this.data.setInter);
  86. clearInterval(this.data.timer);
  87. },
  88. /**
  89. * 页面相关事件处理函数--监听用户下拉动作
  90. */
  91. onPullDownRefresh: function () {
  92. },
  93. /**
  94. * 页面上拉触底事件的处理函数
  95. */
  96. onReachBottom: function () {
  97. },
  98. onState: function (e) {
  99. //console.log(e.detail.code)
  100. },
  101. //初始化拍卖会数据
  102. getLiveRoomData: function ( live_room_kid ) {
  103. let _this = this;
  104. wx.request({
  105. url: app.globalData.prodUrl + 'web-pm/wx/pm/live_room_detail',
  106. data: {
  107. live_room_id: live_room_kid
  108. },
  109. header: { 'ydw-token': wx.getStorageSync('key').ydw_token },
  110. method: 'GET',
  111. dataType: 'json',
  112. responseType: 'text',
  113. success: function (data) {
  114. //console.log(data);
  115. if ( !data.data.msg ) {
  116. wx.showModal({
  117. title: '提示',
  118. content: '您没有上传拍卖竞买商品,请先上传商品!',
  119. success(res) {
  120. if (res.confirm) {
  121. wx.navigateBack({
  122. delta: 1
  123. })
  124. } else if (res.cancel) {
  125. wx.navigateBack({
  126. delta: 1
  127. })
  128. }
  129. }
  130. });
  131. } else {
  132. _this.setData({
  133. liveRoomData: data.data.data, //设置拍卖会列表
  134. pushUrl: data.data.pushUrl, //设置推流地址
  135. nowTime: data.data.nowtime, //设置服务器时间
  136. chatTime: data.data.nowtime //设置服务器时间
  137. },() => {
  138. //调用请求列表
  139. _this.listData(live_room_kid);
  140. //调用刷新页面状态
  141. _this.data.setInter = setInterval(function () {
  142. _this.setServerTime(_this.data.nowTime); //设置服务器时间
  143. if (_this.data.openData !== {}) {
  144. _this.checkState(_this.data.goodsKid, _this.data.openData.price_bidder);
  145. }
  146. }, 1000);
  147. });
  148. }
  149. // if ( _this.data.kid === null ) {
  150. // var goodsState = false;
  151. // var item_kid;
  152. // for (let i = 0; i<data.data.data.length; i++) {
  153. // if (data.data.data[i].bid_opening === "已开拍" ) {
  154. // goodsState = true;
  155. // item_kid = data.data.data[i].kid;
  156. // break;
  157. // }
  158. // }
  159. // if (goodsState) {
  160. // _this.setData({
  161. // kid: item_kid
  162. // })
  163. // } else {
  164. // _this.setData({
  165. // kid: data.data.data[0].kid
  166. // })
  167. // }
  168. // };
  169. //调用同步后台时间
  170. // _this.getTime(nowTime);
  171. },
  172. fail: function (res) { },
  173. complete: function (res) { },
  174. })
  175. },
  176. //请求列表数据
  177. listData: function (liveid) {
  178. let _this = this;
  179. wx.request({
  180. url: app.globalData.prodUrl + 'web-pm/wx/pm/live_room_detail',
  181. data: {
  182. live_room_id: liveid
  183. },
  184. header: { 'ydw-token': wx.getStorageSync('key').ydw_token },
  185. method: 'GET',
  186. dataType: 'json',
  187. responseType: 'text',
  188. success: function(data) {
  189. //console.log(data)
  190. _this.setData({
  191. liveRoomData: data.data.data //设置左侧列表数据
  192. }, () => {
  193. let auction = [], wait = [], end = [];
  194. for (let i = 0; i < _this.data.liveRoomData.length; i++) {
  195. if (_this.data.liveRoomData[i].bid_opening === '已开拍') {
  196. auction.push(_this.data.liveRoomData[i]);
  197. } else if (_this.data.liveRoomData[i].bid_opening === '未开拍') {
  198. wait.push(_this.data.liveRoomData[i]);
  199. } else if (_this.data.liveRoomData[i].bid_opening === '已结束') {
  200. end.push(_this.data.liveRoomData[i]);
  201. };
  202. }
  203. if (auction.length > 0) {
  204. _this.setData({
  205. goodsKid: auction[0].kid
  206. }, () => {
  207. _this.initData(_this.data.goodsKid);
  208. });
  209. } else if (auction.length === 0 && wait.length > 0) {
  210. _this.setData({
  211. goodsKid: wait[0].kid
  212. }, () => {
  213. _this.initData(_this.data.goodsKid);
  214. });
  215. } else if (auction.length === 0 && wait.length === 0) {
  216. _this.initData(_this.data.goodsKid);
  217. };
  218. });
  219. },
  220. fail: function(res) {},
  221. complete: function(res) {},
  222. })
  223. },
  224. //同步右侧商品信息
  225. initData: function (goodsKid) {
  226. let _this = this;
  227. wx.request({
  228. url: app.globalData.prodUrl + 'web-pm/wx/pm/get_item_detail',
  229. data: { item_kid: goodsKid },
  230. header: { 'ydw-token': wx.getStorageSync('key').ydw_token },
  231. method: 'GET',
  232. dataType: 'json',
  233. responseType: 'text',
  234. success: function(data) {
  235. //console.log(data);
  236. _this.setData({
  237. openData: data.data.data[0],
  238. record: data.data.data[0].record,
  239. submitNum: data.data.submit_count,
  240. onlineCount: data.data.online_count
  241. }, () => {
  242. })
  243. },
  244. fail: function(res) {},
  245. complete: function(res) {},
  246. })
  247. },
  248. //同步后台时间
  249. setServerTime: function (nowTime) {
  250. let _this = this;
  251. _this.setData({
  252. nowTime: nowTime + 1000
  253. });
  254. },
  255. //每秒刷新页面状态
  256. checkState: function (goodsKid, price) {
  257. let _this = this;
  258. wx.request({
  259. url: app.globalData.prodUrl + 'web-pm/wx/pm/check_item_status',
  260. data: {
  261. pm_item_id: goodsKid,
  262. price: price,
  263. live_room_id: _this.data.liveid,
  264. chat_time: _this.data.chatTime
  265. },
  266. header: { 'ydw-token': wx.getStorageSync('key').ydw_token },
  267. method: 'GET',
  268. dataType: 'json',
  269. responseType: 'text',
  270. success: function(data) {
  271. // if (data.data.list.length !== 0) {
  272. // let chatlist = _this.data.chatList;
  273. // _this.setData({
  274. // chatTime: data.data.list[data.data.list.length - 1].created
  275. // })
  276. // for (var i = 0; i < data.data.list.length; i++) {
  277. // chatlist.unshift(data.data.list[i])
  278. // };
  279. // _this.setData({
  280. // chatList: chatlist
  281. // })
  282. // }
  283. if (data.data.offer_status) {
  284. _this.listData(_this.data.liveid);
  285. wx.showToast({
  286. title: '有新的报价!',
  287. icon: 'none',
  288. duration: 3000
  289. });
  290. }
  291. },
  292. fail: function(res) {},
  293. complete: function(res) {},
  294. })
  295. },
  296. //选择商品
  297. selectGoods: function (e) {
  298. let _this = this;
  299. for (let i = 0; i < this.data.liveRoomData.length; i++) {
  300. if (this.data.liveRoomData[i].kid === e.currentTarget.dataset.kid) {
  301. _this.setData({
  302. goodsKid: this.data.liveRoomData[i].kid,
  303. thisId: i
  304. },() => {
  305. _this.initData(_this.data.goodsKid);
  306. });
  307. wx.showToast({
  308. title: '已切换商品,请到右侧商品详情中查看。',
  309. icon: 'none',
  310. duration: 3000
  311. });
  312. //console.log(_this.data.thisId);
  313. }
  314. }
  315. },
  316. //打开关闭商品列表
  317. openDetail: function (e) {
  318. let move = this.data.pageWidth * 0.9;
  319. if (this.data.detailOpen) {
  320. this.animation.translate(0).step()
  321. this.setData({
  322. detailOpen: false,
  323. animationData: this.animation.export()
  324. })
  325. } else {
  326. this.animation.translate(move).step()
  327. this.setData({
  328. detailOpen: true,
  329. animationData: this.animation.export()
  330. })
  331. }
  332. },
  333. //打开关闭商品详情
  334. openBidding: function (e) {
  335. let _this = this;
  336. //动画效果
  337. let move = this.data.pageWidth * 0.9;
  338. if (this.data.biddingOpen) {
  339. this.animation.translate(0).step();
  340. this.setData({
  341. biddingOpen: false,
  342. animationData: this.animation.export()
  343. });
  344. } else {
  345. this.animation.translate(-move).step();
  346. this.setData({
  347. biddingOpen: true,
  348. animationData: this.animation.export()
  349. });
  350. };
  351. },
  352. //开拍
  353. startAuction: function (e) {
  354. let _this = this;
  355. wx.request({
  356. url: app.globalData.prodUrl + 'web-pm/wx/pm/bidding_status',
  357. data: {
  358. item_kid: _this.data.openData.kid,
  359. is_open: true
  360. },
  361. header: { 'ydw-token': wx.getStorageSync('key').ydw_token },
  362. method: 'GET',
  363. dataType: 'json',
  364. responseType: 'text',
  365. success: function(data) {
  366. if (data.data.success) {
  367. _this.listData(_this.data.liveid);
  368. wx.showToast({
  369. title: data.data.tip.msg,
  370. icon: 'none',
  371. duration: 3000
  372. });
  373. }
  374. }
  375. })
  376. },
  377. //关拍
  378. endAuction:function(e) {
  379. //console.log('点击关拍')
  380. let _this = this;
  381. if( _this.data.submit ) {
  382. wx.request({
  383. url: app.globalData.prodUrl + 'web-pm/wx/pm/start_countdown',
  384. data: {
  385. item_kid: _this.data.openData.kid
  386. },
  387. header: { 'ydw-token': wx.getStorageSync('key').ydw_token },
  388. method: 'GET',
  389. dataType: 'json',
  390. responseType: 'text',
  391. success: function (data) {
  392. //console.log(data)
  393. //调用倒计时
  394. _this.endCountDown(data.data.nowtime, _this.data.nowTime);
  395. _this.setData({
  396. submit: false
  397. })
  398. },
  399. fail: function (res) { },
  400. complete: function (res) { },
  401. })
  402. } else {
  403. return false;
  404. }
  405. },
  406. //关拍倒计时
  407. endCountDown: function ( closeTime, nowTime ) {
  408. let _this = this;
  409. let time = parseInt((closeTime + 15000 - nowTime)/1000);
  410. //console.log(closeTime)
  411. let countdown = setInterval(function () {
  412. time -= 1;
  413. if(time >= 0) {
  414. _this.setData({
  415. btnText: '成交倒计时' + time + '秒'
  416. });
  417. wx.request({
  418. url: app.globalData.prodUrl + 'web-pm/wx/pm/get_countdown',
  419. data: {
  420. item_kid: _this.data.openData.kid,
  421. start_time: closeTime
  422. },
  423. header: { 'ydw-token': wx.getStorageSync('key').ydw_token },
  424. method: 'GET',
  425. dataType: 'json',
  426. responseType: 'text',
  427. success: function(data) {
  428. //console.log(data);
  429. if (data.data.statuz) {
  430. wx.showToast({
  431. title: '有新的报价!',
  432. icon: 'none',
  433. duration: 3000,
  434. success: function(res) {}
  435. })
  436. _this.setData({
  437. btnText: '竞价倒计时',
  438. submit: true
  439. });
  440. clearInterval(countdown);
  441. }
  442. },
  443. fail: function(res) {},
  444. complete: function(res) {},
  445. })
  446. } else {
  447. wx.request({
  448. url: app.globalData.prodUrl + 'web-pm/wx/pm/bidding_status',
  449. data: {
  450. item_kid: _this.data.openData.kid,
  451. is_open: false
  452. },
  453. header: { 'ydw-token': wx.getStorageSync('key').ydw_token },
  454. method: 'GET',
  455. dataType: 'json',
  456. responseType: 'text',
  457. success: function (data) {
  458. //console.log(data);
  459. if (data.data.success) {
  460. _this.setData({
  461. btnText: '竞价倒计时',
  462. submit: true
  463. })
  464. _this.listData(_this.data.liveid);
  465. wx.showToast({
  466. title: data.data.tip.msg,
  467. icon: 'none',
  468. duration: 3000
  469. });
  470. }
  471. }
  472. });
  473. clearInterval(countdown);
  474. }
  475. }, 1000)
  476. //console.log(time);
  477. },
  478. //切换后置摄像头
  479. rotateCamera:function (e) {
  480. this.ctx.switchCamera({
  481. success: res => {
  482. //console.log('switchCamera success')
  483. },
  484. fail: res => {
  485. //console.log('switchCamera fail')
  486. }
  487. });
  488. },
  489. getChatList() {
  490. var _this = this;
  491. wx.request({
  492. url: app.globalData.prodUrl + 'web-pm/wx/pm/get_chatting_records',
  493. data: {
  494. live_room_id: _this.data.liveid,
  495. chat_time: _this.data.timear
  496. },
  497. header: {
  498. 'ydw-token': wx.getStorageSync('key').ydw_token
  499. },
  500. method: 'GET',
  501. dataType: 'json',
  502. responseType: 'text',
  503. success: function (data) {
  504. var chatList = data.data.data;
  505. if (chatList.length != 0) {
  506. var time = data.data.data[0].created;
  507. _this.setData({
  508. chatList: chatList,
  509. timear: time
  510. }, function () {
  511. _this.pageScrollToBottom();
  512. })
  513. }
  514. }
  515. })
  516. },
  517. pageScrollToBottom: function () {
  518. var _this = this;
  519. this.setData({
  520. scrollTop: this.data.scrollTop + 40
  521. }, function () {
  522. }
  523. )
  524. }
  525. })