playpusher.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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(wx.getStorageSync('key').ydw_token);
  115. console.log(live_room_kid);
  116. console.log(data);
  117. if ( !data.data.msg ) {
  118. wx.showModal({
  119. title: '提示',
  120. content: '您没有上传拍卖竞买商品,请先上传商品!',
  121. success(res) {
  122. if (res.confirm) {
  123. wx.navigateBack({
  124. delta: 1
  125. })
  126. } else if (res.cancel) {
  127. wx.navigateBack({
  128. delta: 1
  129. })
  130. }
  131. }
  132. });
  133. } else {
  134. _this.setData({
  135. liveRoomData: data.data.data, //设置拍卖会列表
  136. pushUrl: data.data.pushUrl, //设置推流地址
  137. nowTime: data.data.nowtime, //设置服务器时间
  138. chatTime: data.data.nowtime //设置服务器时间
  139. },() => {
  140. //调用请求列表
  141. _this.listData(live_room_kid);
  142. //调用刷新页面状态
  143. _this.data.setInter = setInterval(function () {
  144. _this.setServerTime(_this.data.nowTime); //设置服务器时间
  145. if (_this.data.openData !== {}) {
  146. _this.checkState(_this.data.goodsKid, _this.data.openData.price_bidder);
  147. }
  148. }, 1000);
  149. });
  150. }
  151. // if ( _this.data.kid === null ) {
  152. // var goodsState = false;
  153. // var item_kid;
  154. // for (let i = 0; i<data.data.data.length; i++) {
  155. // if (data.data.data[i].bid_opening === "已开拍" ) {
  156. // goodsState = true;
  157. // item_kid = data.data.data[i].kid;
  158. // break;
  159. // }
  160. // }
  161. // if (goodsState) {
  162. // _this.setData({
  163. // kid: item_kid
  164. // })
  165. // } else {
  166. // _this.setData({
  167. // kid: data.data.data[0].kid
  168. // })
  169. // }
  170. // };
  171. //调用同步后台时间
  172. // _this.getTime(nowTime);
  173. },
  174. fail: function (res) { },
  175. complete: function (res) { },
  176. })
  177. },
  178. //请求列表数据
  179. listData: function (liveid) {
  180. let _this = this;
  181. wx.request({
  182. url: app.globalData.prodUrl + 'web-pm/wx/pm/live_room_detail',
  183. data: {
  184. live_room_id: liveid
  185. },
  186. header: { 'ydw-token': wx.getStorageSync('key').ydw_token },
  187. method: 'GET',
  188. dataType: 'json',
  189. responseType: 'text',
  190. success: function(data) {
  191. //console.log(data)
  192. _this.setData({
  193. liveRoomData: data.data.data //设置左侧列表数据
  194. }, () => {
  195. let auction = [], wait = [], end = [];
  196. for (let i = 0; i < _this.data.liveRoomData.length; i++) {
  197. if (_this.data.liveRoomData[i].bid_opening === '已开拍') {
  198. auction.push(_this.data.liveRoomData[i]);
  199. } else if (_this.data.liveRoomData[i].bid_opening === '未开拍') {
  200. wait.push(_this.data.liveRoomData[i]);
  201. } else if (_this.data.liveRoomData[i].bid_opening === '已结束') {
  202. end.push(_this.data.liveRoomData[i]);
  203. };
  204. }
  205. if (auction.length > 0) {
  206. _this.setData({
  207. goodsKid: auction[0].kid
  208. }, () => {
  209. _this.initData(_this.data.goodsKid);
  210. });
  211. } else if (auction.length === 0 && wait.length > 0) {
  212. _this.setData({
  213. goodsKid: wait[0].kid
  214. }, () => {
  215. _this.initData(_this.data.goodsKid);
  216. });
  217. } else if (auction.length === 0 && wait.length === 0) {
  218. _this.initData(_this.data.goodsKid);
  219. };
  220. });
  221. },
  222. fail: function(res) {},
  223. complete: function(res) {},
  224. })
  225. },
  226. //同步右侧商品信息
  227. initData: function (goodsKid) {
  228. let _this = this;
  229. wx.request({
  230. url: app.globalData.prodUrl + 'web-pm/wx/pm/get_item_detail',
  231. data: { item_kid: goodsKid },
  232. header: { 'ydw-token': wx.getStorageSync('key').ydw_token },
  233. method: 'GET',
  234. dataType: 'json',
  235. responseType: 'text',
  236. success: function(data) {
  237. //console.log(data);
  238. _this.setData({
  239. openData: data.data.data[0],
  240. record: data.data.data[0].record,
  241. submitNum: data.data.submit_count,
  242. onlineCount: data.data.online_count
  243. }, () => {
  244. })
  245. },
  246. fail: function(res) {},
  247. complete: function(res) {},
  248. })
  249. },
  250. //同步后台时间
  251. setServerTime: function (nowTime) {
  252. let _this = this;
  253. _this.setData({
  254. nowTime: nowTime + 1000
  255. });
  256. },
  257. //每秒刷新页面状态
  258. checkState: function (goodsKid, price) {
  259. console.log("id"+goodsKid)
  260. console.log("price"+price)
  261. let _this = this;
  262. wx.request({
  263. url: app.globalData.prodUrl + 'web-pm/wx/pm/check_item_status',
  264. data: {
  265. pm_item_id: goodsKid,
  266. price: price,
  267. live_room_id: _this.data.liveid,
  268. chat_time: _this.data.chatTime
  269. },
  270. header: { 'ydw-token': wx.getStorageSync('key').ydw_token },
  271. method: 'GET',
  272. dataType: 'json',
  273. responseType: 'text',
  274. success: function(data) {
  275. console.log("data"+data)
  276. // if (data.data.list.length !== 0) {
  277. // let chatlist = _this.data.chatList;
  278. // _this.setData({
  279. // chatTime: data.data.list[data.data.list.length - 1].created
  280. // })
  281. // for (var i = 0; i < data.data.list.length; i++) {
  282. // chatlist.unshift(data.data.list[i])
  283. // };
  284. // _this.setData({
  285. // chatList: chatlist
  286. // })
  287. // }
  288. if (data.data.offer_status) {
  289. _this.listData(_this.data.liveid);
  290. wx.showToast({
  291. title: '有新的报价!',
  292. icon: 'none',
  293. duration: 3000
  294. });
  295. }
  296. },
  297. fail: function(res) {},
  298. complete: function(res) {},
  299. })
  300. },
  301. //选择商品
  302. selectGoods: function (e) {
  303. let _this = this;
  304. for (let i = 0; i < this.data.liveRoomData.length; i++) {
  305. if (this.data.liveRoomData[i].kid === e.currentTarget.dataset.kid) {
  306. _this.setData({
  307. goodsKid: this.data.liveRoomData[i].kid,
  308. thisId: i
  309. },() => {
  310. _this.initData(_this.data.goodsKid);
  311. });
  312. wx.showToast({
  313. title: '已切换商品,请到右侧商品详情中查看。',
  314. icon: 'none',
  315. duration: 3000
  316. });
  317. //console.log(_this.data.thisId);
  318. }
  319. }
  320. },
  321. //打开关闭商品列表
  322. openDetail: function (e) {
  323. let move = this.data.pageWidth * 0.9;
  324. if (this.data.detailOpen) {
  325. this.animation.translate(0).step()
  326. this.setData({
  327. detailOpen: false,
  328. animationData: this.animation.export()
  329. })
  330. } else {
  331. this.animation.translate(move).step()
  332. this.setData({
  333. detailOpen: true,
  334. animationData: this.animation.export()
  335. })
  336. }
  337. },
  338. //打开关闭商品详情
  339. openBidding: function (e) {
  340. let _this = this;
  341. //动画效果
  342. let move = this.data.pageWidth * 0.9;
  343. if (this.data.biddingOpen) {
  344. this.animation.translate(0).step();
  345. this.setData({
  346. biddingOpen: false,
  347. animationData: this.animation.export()
  348. });
  349. } else {
  350. this.animation.translate(-move).step();
  351. this.setData({
  352. biddingOpen: true,
  353. animationData: this.animation.export()
  354. });
  355. };
  356. },
  357. //开拍
  358. startAuction: function (e) {
  359. let _this = this;
  360. wx.request({
  361. url: app.globalData.prodUrl + 'web-pm/wx/pm/bidding_status',
  362. data: {
  363. item_kid: _this.data.openData.kid,
  364. is_open: true
  365. },
  366. header: { 'ydw-token': wx.getStorageSync('key').ydw_token },
  367. method: 'GET',
  368. dataType: 'json',
  369. responseType: 'text',
  370. success: function(data) {
  371. if (data.data.success) {
  372. _this.listData(_this.data.liveid);
  373. wx.showToast({
  374. title: data.data.tip.msg,
  375. icon: 'none',
  376. duration: 3000
  377. });
  378. }
  379. }
  380. })
  381. },
  382. //关拍
  383. endAuction:function(e) {
  384. //console.log('点击关拍')
  385. let _this = this;
  386. if( _this.data.submit ) {
  387. wx.request({
  388. url: app.globalData.prodUrl + 'web-pm/wx/pm/start_countdown',
  389. data: {
  390. item_kid: _this.data.openData.kid
  391. },
  392. header: { 'ydw-token': wx.getStorageSync('key').ydw_token },
  393. method: 'GET',
  394. dataType: 'json',
  395. responseType: 'text',
  396. success: function (data) {
  397. //console.log(data)
  398. //调用倒计时
  399. _this.endCountDown(data.data.nowtime, _this.data.nowTime);
  400. _this.setData({
  401. submit: false
  402. })
  403. },
  404. fail: function (res) { },
  405. complete: function (res) { },
  406. })
  407. } else {
  408. return false;
  409. }
  410. },
  411. //关拍倒计时
  412. endCountDown: function ( closeTime, nowTime ) {
  413. let _this = this;
  414. let time = parseInt((closeTime + 15000 - nowTime)/1000);
  415. //console.log(closeTime)
  416. let countdown = setInterval(function () {
  417. time -= 1;
  418. if(time >= 0) {
  419. _this.setData({
  420. btnText: '成交倒计时' + time + '秒'
  421. });
  422. wx.request({
  423. url: app.globalData.prodUrl + 'web-pm/wx/pm/get_countdown',
  424. data: {
  425. item_kid: _this.data.openData.kid,
  426. start_time: closeTime
  427. },
  428. header: { 'ydw-token': wx.getStorageSync('key').ydw_token },
  429. method: 'GET',
  430. dataType: 'json',
  431. responseType: 'text',
  432. success: function(data) {
  433. //console.log(data);
  434. if (data.data.statuz) {
  435. wx.showToast({
  436. title: '有新的报价!',
  437. icon: 'none',
  438. duration: 3000,
  439. success: function(res) {}
  440. })
  441. _this.setData({
  442. btnText: '竞价倒计时',
  443. submit: true
  444. });
  445. clearInterval(countdown);
  446. }
  447. },
  448. fail: function(res) {},
  449. complete: function(res) {},
  450. })
  451. } else {
  452. wx.request({
  453. url: app.globalData.prodUrl + 'web-pm/wx/pm/bidding_status',
  454. data: {
  455. item_kid: _this.data.openData.kid,
  456. is_open: false
  457. },
  458. header: { 'ydw-token': wx.getStorageSync('key').ydw_token },
  459. method: 'GET',
  460. dataType: 'json',
  461. responseType: 'text',
  462. success: function (data) {
  463. //console.log(data);
  464. if (data.data.success) {
  465. _this.setData({
  466. btnText: '竞价倒计时',
  467. submit: true
  468. })
  469. _this.listData(_this.data.liveid);
  470. wx.showToast({
  471. title: data.data.tip.msg,
  472. icon: 'none',
  473. duration: 3000
  474. });
  475. }
  476. }
  477. });
  478. clearInterval(countdown);
  479. }
  480. }, 1000)
  481. //console.log(time);
  482. },
  483. //切换后置摄像头
  484. rotateCamera:function (e) {
  485. this.ctx.switchCamera({
  486. success: res => {
  487. //console.log('switchCamera success')
  488. },
  489. fail: res => {
  490. //console.log('switchCamera fail')
  491. }
  492. });
  493. },
  494. getChatList() {
  495. var _this = this;
  496. wx.request({
  497. url: app.globalData.prodUrl + 'web-pm/wx/pm/get_chatting_records',
  498. data: {
  499. live_room_id: _this.data.liveid,
  500. chat_time: _this.data.timear
  501. },
  502. header: {
  503. 'ydw-token': wx.getStorageSync('key').ydw_token
  504. },
  505. method: 'GET',
  506. dataType: 'json',
  507. responseType: 'text',
  508. success: function (data) {
  509. var chatList = data.data.data;
  510. if (chatList.length != 0) {
  511. var time = data.data.data[0].created;
  512. _this.setData({
  513. chatList: chatList,
  514. timear: time
  515. }, function () {
  516. _this.pageScrollToBottom();
  517. })
  518. }
  519. }
  520. })
  521. },
  522. pageScrollToBottom: function () {
  523. var _this = this;
  524. this.setData({
  525. scrollTop: this.data.scrollTop + 40
  526. }, function () {
  527. }
  528. )
  529. }
  530. })