live.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. // pages/live/live.js
  2. var time;
  3. const app = getApp();
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. liveUrl: '', //拉流地址
  10. detailOpen: false,//左抽屉开关
  11. setInter: '', //秒刷函数
  12. goodsKid: '', //当前商品ID
  13. openData: {}, //openData
  14. biddingOpen: false,//右抽屉开关
  15. animationData: {},//动画函数
  16. nowTime: null, //服务器时间
  17. endAuction: false,
  18. endTimeText: 9,
  19. endTimeShow: false,
  20. flag: true,
  21. new_bidder: 0, //需要操作的价格
  22. apply: null, //报名人数
  23. payDeposit: false, //是否支付保证金
  24. code: '', //支付保证金后显示的号牌
  25. countDownInter: '',
  26. notPlay: true,
  27. chatText: '',
  28. chatTime: null,
  29. chatList: [],
  30. prodUrl: app.globalData.prodUrl,
  31. auction : true,
  32. time : 0,
  33. scrollTop : 300,
  34. contentText : "",
  35. onlineCount : 0,
  36. chatBool : true,
  37. timer : null,
  38. detailBool : false,
  39. mask : false,
  40. productBool : false,
  41. selectIndex : 0,
  42. SelectKid : "",
  43. timerContr: null,
  44. setInter1 :null,
  45. limit : 8,
  46. page : 1,
  47. sumPage : null,
  48. textTitle : "点击加载更多..."
  49. },
  50. /**
  51. * 生命周期函数--监听页面加载
  52. */
  53. onLoad: function (options) {
  54. let _this = this;
  55. this.setData({
  56. liveid: options.liveid,
  57. title: options.title
  58. },function() {
  59. _this.data.timer = setInterval(() => {
  60. _this.getChatList();
  61. },1000)
  62. });
  63. //设置动态标题
  64. wx.setNavigationBarTitle({
  65. title: options.title
  66. });
  67. //创建animation实例
  68. let animation = wx.createAnimation({
  69. duration: 300,
  70. timingFunction: 'linear'
  71. });
  72. this.animation = animation;
  73. //动态设置页面高度
  74. wx.getSystemInfo({
  75. success: function(res) {
  76. _this.setData({
  77. pageWidth: res.windowWidth,
  78. pageHeight: res.windowHeight - 50
  79. })
  80. }
  81. });
  82. //初始化页面数据
  83. if (options.isshare) {
  84. //console.log('是分享进入');
  85. let getlogin = app.getLogs(app);
  86. getlogin.then(function () {
  87. _this.getNewData(options.liveid);
  88. }).catch(function () {
  89. //console.log('错误')
  90. })
  91. this.setData({
  92. 'isshare': options.isshare
  93. })
  94. } else {
  95. _this.getNewData(options.liveid);
  96. }
  97. // setInterval(() => {
  98. // var a = Math.floor(Math.random() * (100 - 20 + 1) + 20);
  99. // _this.setData({
  100. // onlineCount : a
  101. // })
  102. // },3000)
  103. },
  104. /**
  105. * 生命周期函数--监听页面初次渲染完成
  106. */
  107. onReady: function () {
  108. },
  109. /**
  110. * 生命周期函数--监听页面显示
  111. */
  112. onShow: function () {
  113. },
  114. /**
  115. * 生命周期函数--监听页面隐藏
  116. */
  117. onHide: function () {
  118. },
  119. /**
  120. * 生命周期函数--监听页面卸载
  121. */
  122. onUnload: function () {
  123. clearInterval(this.data.setInter);
  124. clearInterval(this.data.countDownInter);
  125. clearInterval(this.data.timer);
  126. },
  127. /**
  128. * 页面相关事件处理函数--监听用户下拉动作
  129. */
  130. onPullDownRefresh: function () {
  131. },
  132. /**
  133. * 页面上拉触底事件的处理函数
  134. */
  135. onReachBottom: function () {
  136. },
  137. /**
  138. * 用户点击右上角分享
  139. */
  140. onShareAppMessage: function () {
  141. //console.log('123132')
  142. var _this = this;
  143. return {
  144. title: '和我一起来玩拍~',
  145. path: '/pages/live/live?isshare=true&liveid=' + _this.data.liveid + "&title=" + _this.data.title,
  146. success: function (res) {
  147. // 转发成功
  148. },
  149. fail: function (res) {
  150. // 转发失败
  151. }
  152. }
  153. },
  154. playstate: function (e) {
  155. },
  156. btn:function (e) {
  157. //console.log(e)
  158. },
  159. //初始化页面数据
  160. getNewData: function (liveid) {
  161. let _this = this;
  162. wx.request({
  163. url: app.globalData.prodUrl + 'web-pm/wx/pm/live_item_detail',
  164. data: { live_room_id: liveid, limit: _this.data.limit, page: _this.data.page },
  165. header: { 'ydw-token': wx.getStorageSync('key').ydw_token },
  166. method: 'GET',
  167. dataType: 'json',
  168. responseType: 'text',
  169. success: function(data) {
  170. if(data.data.live_room.live_status === "已开始") {
  171. _this.setData({
  172. auction : false
  173. })
  174. }
  175. _this.setData({
  176. liveUrl: data.data.live_room.live_url, //设置拉流地址
  177. nowTime: data.data.nowtime, //设置服务器时间
  178. chatTime: data.data.nowtime, //设置聊天时间戳
  179. listData: data.data.data, //设置左侧列表数据
  180. contentText : data.data.live_room.content,
  181. sumPage : data.data.count
  182. }, () => {
  183. let auction = [], wait = [], end = [],start = [];
  184. for (let i = 0; i < _this.data.listData.length; i++) {
  185. if (_this.data.listData[i].bid_opening === '已开拍') {
  186. auction.push(_this.data.listData[i]);
  187. start.push(i);
  188. } else if (_this.data.listData[i].bid_opening === '未开拍') {
  189. wait.push(_this.data.listData[i]);
  190. } else if (_this.data.listData[i].bid_opening === '已结束') {
  191. end.push(_this.data.listData[i]);
  192. };
  193. }
  194. _this.setData({
  195. selectIndex : start[0]
  196. })
  197. if (auction.length > 0) {
  198. _this.setData({
  199. goodsKid: auction[0].kid
  200. }, ( ) => {
  201. _this.initData( _this.data.goodsKid);
  202. });
  203. } else if (auction.length === 0 && wait.length > 0) {
  204. _this.setData({
  205. goodsKid: wait[0].kid
  206. }, () => {
  207. _this.initData(_this.data.goodsKid);
  208. });
  209. };
  210. // _this.data.timerContr = setInterval(()=>{
  211. // if (_this.data.openData.price_bidder) {
  212. _this.data.setInter = setInterval(function () {
  213. _this.setServerTime(_this.data.nowTime); //设置服务器时间
  214. if (_this.data.openData !== {}) {
  215. _this.checkState(_this.data.goodsKid, _this.data.openData.price_bidder);
  216. }
  217. }, 1000);
  218. // }
  219. // })
  220. })
  221. },
  222. fail: function(res) {},
  223. complete: function(res) {},
  224. })
  225. },
  226. //设置服务器时间
  227. setServerTime: function (nowTime) {
  228. let _this = this;
  229. _this.setData({
  230. nowTime: nowTime + 1000
  231. });
  232. },
  233. //加载更多
  234. loadMore() {
  235. var _this = this;
  236. var page = this.data.page + 1;
  237. var sumPage = this.data.sumPage / 8 > Math.floor(this.data.sumPage / 8) ? Math.floor(this.data.sumPage / 8) + 1 : this.data.sumPage / 8;
  238. _this.setData({
  239. page
  240. },function() {
  241. if (page <= sumPage) {
  242. _this.listData(_this.data.liveid);
  243. } else {
  244. _this.setData({
  245. textTitle : "已经到底了,无更多商品了!!!"
  246. })
  247. }
  248. })
  249. },
  250. //左侧列表数据
  251. listData: function (liveid) {
  252. let _this = this;
  253. wx.request({
  254. url: app.globalData.prodUrl + 'web-pm/wx/pm/live_item_detail',
  255. data: { live_room_id: liveid, limit: _this.data.limit, page: _this.data.page},
  256. header: { 'ydw-token': wx.getStorageSync('key').ydw_token },
  257. method: 'GET',
  258. dataType: 'json',
  259. responseType: 'text',
  260. success: function(data) {
  261. _this.setData({
  262. listData: _this.data.listData.concat(data.data.data)
  263. },function() {
  264. var select = _this.data.SelectKid || _this.data.goodsKid
  265. _this.initData(select);
  266. })
  267. // var ind = null;
  268. // for(var i = 0;i< _this.data.listData.length ; i ++) {
  269. // if (data.data.data[i].price_bidder !== _this.data.listData[i].price_bidder) {
  270. // ind = i;
  271. // console.log(ind)
  272. // }
  273. // }
  274. // if (ind) {
  275. // var list = 'listData[' + ind + ']';
  276. // }
  277. // _this.setData({
  278. // [list] : data.data.data[ind] //设置左侧列表数据
  279. // }, () => {
  280. // let auction = [], wait = [], end = [];
  281. // for (let i = 0; i < _this.data.listData.length; i++) {
  282. // if (_this.data.listData[i].bid_opening === '已开拍') {
  283. // auction.push(_this.data.listData[i]);
  284. // } else if (_this.data.listData[i].bid_opening === '未开拍') {
  285. // wait.push(_this.data.listData[i]);
  286. // } else if (_this.data.listData[i].bid_opening === '已结束') {
  287. // end.push(_this.data.listData[i]);
  288. // };
  289. // }
  290. // var select = _this.data.SelectKid || _this.data.goodsKid
  291. //_this.initData(select);
  292. // if (auction.length > 0) {
  293. // _this.setData({
  294. // goodsKid: auction[0].kid
  295. // }, () => {
  296. // _this.initData(_this.data.goodsKid);
  297. // });
  298. // } else if (auction.length === 0 && wait.length > 0) {
  299. // _this.setData({
  300. // goodsKid: wait[0].kid
  301. // }, () => {
  302. // _this.initData(_this.data.goodsKid);
  303. // });
  304. // } else if (auction.length === 0 && wait.length === 0) {
  305. // _this.setData({
  306. // goodsKid: end[end.length-1].kid
  307. // }, () => {
  308. // _this.initData(_this.data.goodsKid);
  309. // });
  310. // };
  311. //});
  312. },
  313. fail: function(res) {},
  314. complete: function(res) {},
  315. });
  316. },
  317. //右侧商品数据
  318. initData: function (goodsKid) {
  319. let _this = this;
  320. wx.request({
  321. url: app.globalData.prodUrl + 'web-pm/wx/pm/get_item_detail',
  322. data: { item_kid: goodsKid},
  323. header: { 'ydw-token': wx.getStorageSync('key').ydw_token },
  324. method: 'GET',
  325. dataType: 'json',
  326. responseType: 'text',
  327. success: function (data) {
  328. //console.log(data)
  329. _this.setData({
  330. openData: data.data.data[0],
  331. code: data.data.code,
  332. payDeposit: data.data.item_submit,
  333. apply: data.data.submit_count,
  334. record: data.data.data[0].record,
  335. // onlineCount: data.data.online_count
  336. });
  337. if (data.data.data[0].price_bidder === 0) {
  338. _this.setData({
  339. new_bidder: data.data.data[0].price_start + data.data.data[0].price_add
  340. })
  341. } else {
  342. _this.setData({
  343. new_bidder: data.data.data[0].price_bidder + data.data.data[0].price_add
  344. })
  345. }
  346. },
  347. fail: function (res) {},
  348. complete: function (res) {},
  349. })
  350. },
  351. selectGood(e){
  352. var _this = this;
  353. var price = this.data.bidder;
  354. var kid = e.currentTarget.dataset.kid;
  355. var index = e.currentTarget.dataset.ind;
  356. this.setData({
  357. selectIndex : index,
  358. SelectKid : kid,
  359. goodsKid : kid,
  360. })
  361. this.initData(kid)
  362. wx.showToast({
  363. title: '已切换商品,请到右侧“我要出价”查看',
  364. icon: 'none',
  365. duration: 2000
  366. });
  367. },
  368. //刷新商品状态
  369. checkState: function (goodsKid, price) {
  370. let _this = this;
  371. if (price){
  372. wx.request({
  373. url: app.globalData.prodUrl + 'web-pm/wx/pm/check_live_item_status',
  374. data: {
  375. pm_item_id: goodsKid,
  376. price: price,
  377. live_room_id: _this.data.liveid,
  378. chat_time: _this.data.chatTime
  379. },
  380. header: { 'ydw-token': wx.getStorageSync('key').ydw_token },
  381. method: 'GET',
  382. dataType: 'json',
  383. responseType: 'text',
  384. success: function (data) {
  385. // if (data.data.list.length !== 0) {
  386. // let chatlist = _this.data.chatList;
  387. // _this.setData({
  388. // chatTime: data.data.list[data.data.list.length - 1].created
  389. // })
  390. // for (var i = 0; i < data.data.list.length; i++) {
  391. // chatlist.unshift(data.data.list[i])
  392. // };
  393. // _this.setData({
  394. // chatList: chatlist
  395. // })
  396. // };
  397. if (data.data.offer_status) {
  398. _this.listData(_this.data.liveid);
  399. if (data.data.offer_price_persion !== wx.getStorageSync('persion').persion.kid)
  400. wx.showToast({
  401. title: '有新的报价!',
  402. icon: 'none',
  403. duration: 3000
  404. });
  405. };
  406. if (data.data.archive_status && _this.data.flag) {
  407. _this.setData({
  408. flag: false
  409. }, () => {
  410. _this.endTime(data.data.close_time)
  411. });
  412. };
  413. if (!data.data.archive_status && !_this.data.flag) {
  414. clearInterval(_this.data.countDownInter);
  415. _this.setData({
  416. endTimeShow: false,
  417. flag: true
  418. });
  419. };
  420. if (data.data.bid_opening != _this.data.openData.bid_opening) {
  421. if (data.data.bid_opening === '已结束' && data.data.bidding_man === wx.getStorageSync('persion').persion.kid) {
  422. _this.setData({
  423. mask: true
  424. })
  425. // wx.showToast({
  426. // title: '恭喜您竞价成功!',
  427. // icon: 'success',
  428. // duration: 3000
  429. // });
  430. clearInterval(_this.data.countDownInter);
  431. } else if (data.data.bid_opening === '已结束' && data.data.bidding_man !== wx.getStorageSync('persion').persion.kid) {
  432. wx.showToast({
  433. title: '很遗憾,竞价失败!',
  434. icon: 'none',
  435. duration: 3000
  436. });
  437. }
  438. _this.listData(_this.data.liveid);
  439. };
  440. _this.setData({
  441. onlineCount: data.data.online_count,
  442. })
  443. },
  444. fail: function (res) { },
  445. complete: function (res) { },
  446. });
  447. }
  448. },
  449. //结束倒计时
  450. endTime: function (closeTime) {
  451. let _this = this;
  452. var time = parseInt(((closeTime + 15000) - _this.data.nowTime)/1000 );
  453. _this.data.countDownInter = setInterval(function(){
  454. if ( time > 0 ) {
  455. time -= 1;
  456. _this.setData({
  457. endTimeText: time,
  458. endTimeShow: true
  459. })
  460. } else if (time <= 0) {
  461. clearInterval(_this.data.countDownInter);
  462. _this.setData({
  463. endTimeText: time,
  464. endTimeShow: false,
  465. flag: true
  466. });
  467. };
  468. },1000);
  469. },
  470. //减价
  471. reducePrice: function (e) {
  472. let _this = this;
  473. let new_bidder = _this.data.new_bidder - _this.data.openData.price_add;
  474. if (new_bidder <= _this.data.openData.price_bidder ){
  475. wx.showToast({
  476. title: '出价不得低于最低加价幅度',
  477. icon: 'none',
  478. duration: 3000
  479. })
  480. } else {
  481. _this.setData({
  482. new_bidder: new_bidder
  483. })
  484. }
  485. },
  486. //加价
  487. reduceIncrease: function (e) {
  488. let _this = this;
  489. let new_bidder = (_this.data.new_bidder + _this.data.openData.price_add);
  490. _this.setData({
  491. new_bidder: new_bidder
  492. })
  493. },
  494. //出价
  495. bid:function (e) {
  496. let _this = this;
  497. wx.request({
  498. url: app.globalData.prodUrl + 'web-pm/wx/pm/compete_price',
  499. data: {
  500. pm_item_id: _this.data.openData.kid,
  501. price_bidder: _this.data.new_bidder === 0 ? 1 : _this.data.new_bidder
  502. },
  503. header: { 'ydw-token': wx.getStorageSync('key').ydw_token },
  504. method: 'GET',
  505. dataType: 'json',
  506. responseType: 'text',
  507. success: function(data) {
  508. if(data.data.success) {
  509. _this.setData({
  510. record: data.data.record,
  511. bidder: data.data.pm_item_submit_offer.price_bidder,
  512. new_bidder: data.data.pm_item_submit_offer.price_bidder + _this.data.openData.price_add
  513. },function() {
  514. })
  515. var selectkid = _this.data.SelectKid || _this.data.goodsKid;
  516. new Promise((resolve,reject) => {
  517. _this.initData(selectkid);
  518. resolve();
  519. }).then(() => {
  520. _this.listData(_this.data.liveid);
  521. })
  522. wx.showToast({
  523. title: data.data.tip.msg,
  524. icon: 'none',
  525. duration: 3000
  526. })
  527. } else {
  528. wx.showToast({
  529. title: data.data.tip.msg,
  530. icon: 'none',
  531. duration: 3000
  532. })
  533. }
  534. },
  535. fail: function(res) {
  536. },
  537. complete: function(res) {
  538. },
  539. })
  540. },
  541. //打开关闭商品详请
  542. openDetail: function (e) {
  543. let move = this.data.pageWidth * 0.9;
  544. if(this.data.detailOpen) {
  545. this.animation.translate(0).step()
  546. this.setData({
  547. detailOpen: false,
  548. chatBool: true,
  549. notPlay: true,
  550. animationData: this.animation.export()
  551. })
  552. } else {
  553. this.animation.translate(move).step()
  554. this.setData({
  555. detailOpen: true,
  556. chatBool: false,
  557. notPlay: false,
  558. animationData: this.animation.export()
  559. })
  560. }
  561. },
  562. //打开关闭出价页面
  563. openBidding: function (e) {
  564. let move = this.data.pageWidth * 0.9;
  565. if (this.data.biddingOpen) {
  566. this.animation.translate(0).step()
  567. this.setData({
  568. biddingOpen: false,
  569. chatBool : true,
  570. notPlay : true,
  571. animationData: this.animation.export()
  572. })
  573. } else {
  574. this.animation.translate(-move).step()
  575. this.setData({
  576. biddingOpen: true,
  577. chatBool: false,
  578. notPlay: false,
  579. animationData: this.animation.export()
  580. })
  581. }
  582. },
  583. //交保证金
  584. subDeposit: function (e) {
  585. let _this = this;
  586. wx.navigateTo({
  587. url: '../payDeposit/payDeposit?kid=' + this.data.openData.kid + '&deposit=' + _this.data.openData.price_guarantee + '&auction_way=' + _this.data.openData.auction_way + "&time=" + _this.data.openData.time_opening,
  588. success: function (res) {
  589. clearInterval(_this.data.setInter);
  590. clearInterval(_this.data.countDownInter);
  591. }
  592. })
  593. },
  594. //保存聊天文本
  595. chatText: function (e) {
  596. this.setData({
  597. chatText: e.detail.value
  598. });
  599. },
  600. //发送聊天文本
  601. send: function (e) {
  602. let _this = this;
  603. if ( _this.data.chatText !== '' ) {
  604. wx.request({
  605. url: app.globalData.prodUrl + 'web-pm/wx/pm/add_chitchat',
  606. data: {
  607. content: _this.data.chatText,
  608. liveroom_id: _this.data.liveid
  609. },
  610. header: { 'ydw-token': wx.getStorageSync('key').ydw_token },
  611. method: 'GET',
  612. dataType: 'json',
  613. responseType: 'text',
  614. success: function (data) {
  615. _this.setData({
  616. chatText: ''
  617. },function() {
  618. _this.getChatList()
  619. })
  620. }
  621. })
  622. } else {
  623. }
  624. },
  625. //检测拍卖状态
  626. getLiveState: function (e) {
  627. },
  628. getNetworkState: function (e) {
  629. let _this = this;
  630. if(e.detail.info.netSpeed === 0) {
  631. _this.setData({
  632. notPlay: true
  633. })
  634. } else {
  635. _this.setData({
  636. notPlay: false
  637. })
  638. }
  639. },
  640. //跳转首页
  641. backHome: function () {
  642. wx.reLaunch({
  643. url: '/pages/index/index'
  644. })
  645. },
  646. getChatList() {
  647. var _this = this;
  648. wx.request({
  649. url: app.globalData.prodUrl + 'web-pm/wx/pm/get_chatting_records',
  650. data: {
  651. live_room_id: _this.data.liveid,
  652. chat_time: _this.data.time
  653. },
  654. header: {
  655. 'ydw-token': wx.getStorageSync('key').ydw_token
  656. },
  657. method: 'GET',
  658. dataType: 'json',
  659. responseType: 'text',
  660. success: function (data) {
  661. var chatList = data.data.data;
  662. if (chatList.length!=0) {
  663. var time = data.data.data[0].created;
  664. _this.setData({
  665. chatList: chatList,
  666. time
  667. }, function () {
  668. _this.pageScrollToBottom();
  669. })
  670. }
  671. }
  672. })
  673. },
  674. pageScrollToBottom: function () {
  675. var _this = this;
  676. this.setData({
  677. scrollTop: this.data.scrollTop + 40
  678. }, function () {
  679. }
  680. )
  681. },
  682. lookDetail() {
  683. this.setData({
  684. detailBool: !this.data.detailBool
  685. })
  686. },
  687. gopay: function (e) {
  688. wx.navigateTo({
  689. url: '/pages/mine/wxml/beingphotographed/beingphotographed'
  690. })
  691. },
  692. closePay: function (e) {
  693. this.setData({
  694. mask : false
  695. })
  696. },
  697. opDetail : function(e) {
  698. var a = e.currentTarget.dataset.ind;
  699. var arr = this.data.listData;
  700. for (var i = 0; i < arr.length ; i ++) {
  701. if(a === i) {
  702. arr[i].bool = true
  703. }
  704. }
  705. this.setData({
  706. // productBool : true,
  707. listData : arr
  708. })
  709. },
  710. closeDetail : function(e) {
  711. var a = e.currentTarget.dataset.ind;
  712. var arr = this.data.listData;
  713. for (var i = 0; i < arr.length; i++) {
  714. if (a === i) {
  715. arr[i].bool = false
  716. }
  717. }
  718. this.setData({
  719. // productBool: true,
  720. listData: arr
  721. })
  722. }
  723. })