live.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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. console.log(data)
  171. if(data.data.live_room.live_status === "已开始") {
  172. _this.setData({
  173. auction : false
  174. })
  175. }
  176. _this.setData({
  177. liveUrl: data.data.live_room.live_url, //设置拉流地址
  178. nowTime: data.data.nowtime, //设置服务器时间
  179. chatTime: data.data.nowtime, //设置聊天时间戳
  180. listData: data.data.data, //设置左侧列表数据
  181. contentText : data.data.live_room.content,
  182. sumPage : data.data.count
  183. }, () => {
  184. let auction = [], wait = [], end = [],start = [];
  185. for (let i = 0; i < _this.data.listData.length; i++) {
  186. if (_this.data.listData[i].bid_opening === '已开拍') {
  187. auction.push(_this.data.listData[i]);
  188. start.push(i);
  189. } else if (_this.data.listData[i].bid_opening === '未开拍') {
  190. wait.push(_this.data.listData[i]);
  191. } else if (_this.data.listData[i].bid_opening === '已结束') {
  192. end.push(_this.data.listData[i]);
  193. };
  194. }
  195. _this.setData({
  196. selectIndex : start[0]
  197. })
  198. if (auction.length > 0) {
  199. _this.setData({
  200. goodsKid: auction[0].kid
  201. }, ( ) => {
  202. _this.initData( _this.data.goodsKid);
  203. });
  204. } else if (auction.length === 0 && wait.length > 0) {
  205. _this.setData({
  206. goodsKid: wait[0].kid
  207. }, () => {
  208. _this.initData(_this.data.goodsKid);
  209. });
  210. };
  211. // _this.data.timerContr = setInterval(()=>{
  212. // if (_this.data.openData.price_bidder) {
  213. _this.data.setInter = setInterval(function () {
  214. _this.setServerTime(_this.data.nowTime); //设置服务器时间
  215. if (_this.data.openData !== {}) {
  216. _this.checkState(_this.data.goodsKid, _this.data.openData.price_bidder);
  217. }
  218. }, 1000);
  219. // }
  220. // })
  221. })
  222. },
  223. fail: function(res) {},
  224. complete: function(res) {},
  225. })
  226. },
  227. //设置服务器时间
  228. setServerTime: function (nowTime) {
  229. let _this = this;
  230. _this.setData({
  231. nowTime: nowTime + 1000
  232. });
  233. },
  234. //加载更多
  235. loadMore() {
  236. var _this = this;
  237. var page = this.data.page + 1;
  238. var sumPage = this.data.sumPage / 8 > Math.floor(this.data.sumPage / 8) ? Math.floor(this.data.sumPage / 8) + 1 : this.data.sumPage / 8;
  239. _this.setData({
  240. page
  241. },function() {
  242. if (page <= sumPage) {
  243. _this.listData(_this.data.liveid);
  244. } else {
  245. _this.setData({
  246. textTitle : "已经到底了,无更多商品了!!!"
  247. })
  248. }
  249. })
  250. },
  251. //左侧列表数据
  252. listData: function (liveid) {
  253. let _this = this;
  254. wx.request({
  255. url: app.globalData.prodUrl + 'web-pm/wx/pm/live_item_detail',
  256. data: { live_room_id: liveid, limit: _this.data.limit, page: _this.data.page},
  257. header: { 'ydw-token': wx.getStorageSync('key').ydw_token },
  258. method: 'GET',
  259. dataType: 'json',
  260. responseType: 'text',
  261. success: function(data) {
  262. _this.setData({
  263. listData: _this.data.listData.concat(data.data.data)
  264. },function() {
  265. var select = _this.data.SelectKid || _this.data.goodsKid
  266. _this.initData(select);
  267. })
  268. // var ind = null;
  269. // for(var i = 0;i< _this.data.listData.length ; i ++) {
  270. // if (data.data.data[i].price_bidder !== _this.data.listData[i].price_bidder) {
  271. // ind = i;
  272. // console.log(ind)
  273. // }
  274. // }
  275. // if (ind) {
  276. // var list = 'listData[' + ind + ']';
  277. // }
  278. // _this.setData({
  279. // [list] : data.data.data[ind] //设置左侧列表数据
  280. // }, () => {
  281. // let auction = [], wait = [], end = [];
  282. // for (let i = 0; i < _this.data.listData.length; i++) {
  283. // if (_this.data.listData[i].bid_opening === '已开拍') {
  284. // auction.push(_this.data.listData[i]);
  285. // } else if (_this.data.listData[i].bid_opening === '未开拍') {
  286. // wait.push(_this.data.listData[i]);
  287. // } else if (_this.data.listData[i].bid_opening === '已结束') {
  288. // end.push(_this.data.listData[i]);
  289. // };
  290. // }
  291. // var select = _this.data.SelectKid || _this.data.goodsKid
  292. //_this.initData(select);
  293. // if (auction.length > 0) {
  294. // _this.setData({
  295. // goodsKid: auction[0].kid
  296. // }, () => {
  297. // _this.initData(_this.data.goodsKid);
  298. // });
  299. // } else if (auction.length === 0 && wait.length > 0) {
  300. // _this.setData({
  301. // goodsKid: wait[0].kid
  302. // }, () => {
  303. // _this.initData(_this.data.goodsKid);
  304. // });
  305. // } else if (auction.length === 0 && wait.length === 0) {
  306. // _this.setData({
  307. // goodsKid: end[end.length-1].kid
  308. // }, () => {
  309. // _this.initData(_this.data.goodsKid);
  310. // });
  311. // };
  312. //});
  313. },
  314. fail: function(res) {},
  315. complete: function(res) {},
  316. });
  317. },
  318. //右侧商品数据
  319. initData: function (goodsKid) {
  320. let _this = this;
  321. wx.request({
  322. url: app.globalData.prodUrl + 'web-pm/wx/pm/get_item_detail',
  323. data: { item_kid: goodsKid},
  324. header: { 'ydw-token': wx.getStorageSync('key').ydw_token },
  325. method: 'GET',
  326. dataType: 'json',
  327. responseType: 'text',
  328. success: function (data) {
  329. //console.log(data)
  330. _this.setData({
  331. openData: data.data.data[0],
  332. code: data.data.code,
  333. payDeposit: data.data.item_submit,
  334. apply: data.data.submit_count,
  335. record: data.data.data[0].record,
  336. // onlineCount: data.data.online_count
  337. });
  338. if (data.data.data[0].price_bidder === 0) {
  339. _this.setData({
  340. new_bidder: data.data.data[0].price_start + data.data.data[0].price_add
  341. })
  342. } else {
  343. _this.setData({
  344. new_bidder: data.data.data[0].price_bidder + data.data.data[0].price_add
  345. })
  346. }
  347. },
  348. fail: function (res) {},
  349. complete: function (res) {},
  350. })
  351. },
  352. selectGood(e){
  353. var _this = this;
  354. var price = this.data.bidder;
  355. var kid = e.currentTarget.dataset.kid;
  356. var index = e.currentTarget.dataset.ind;
  357. this.setData({
  358. selectIndex : index,
  359. SelectKid : kid,
  360. goodsKid : kid,
  361. })
  362. this.initData(kid)
  363. wx.showToast({
  364. title: '已切换商品,请到右侧“我要出价”查看',
  365. icon: 'none',
  366. duration: 2000
  367. });
  368. },
  369. //刷新商品状态
  370. checkState: function (goodsKid, price) {
  371. let _this = this;
  372. if (price){
  373. wx.request({
  374. url: app.globalData.prodUrl + 'web-pm/wx/pm/check_live_item_status',
  375. data: {
  376. pm_item_id: goodsKid,
  377. price: price,
  378. live_room_id: _this.data.liveid,
  379. chat_time: _this.data.chatTime
  380. },
  381. header: { 'ydw-token': wx.getStorageSync('key').ydw_token },
  382. method: 'GET',
  383. dataType: 'json',
  384. responseType: 'text',
  385. success: function (data) {
  386. // if (data.data.list.length !== 0) {
  387. // let chatlist = _this.data.chatList;
  388. // _this.setData({
  389. // chatTime: data.data.list[data.data.list.length - 1].created
  390. // })
  391. // for (var i = 0; i < data.data.list.length; i++) {
  392. // chatlist.unshift(data.data.list[i])
  393. // };
  394. // _this.setData({
  395. // chatList: chatlist
  396. // })
  397. // };
  398. if (data.data.offer_status) {
  399. _this.listData(_this.data.liveid);
  400. if (data.data.offer_price_persion !== wx.getStorageSync('persion').persion.kid)
  401. wx.showToast({
  402. title: '有新的报价!',
  403. icon: 'none',
  404. duration: 3000
  405. });
  406. };
  407. if (data.data.archive_status && _this.data.flag) {
  408. _this.setData({
  409. flag: false
  410. }, () => {
  411. _this.endTime(data.data.close_time)
  412. });
  413. };
  414. if (!data.data.archive_status && !_this.data.flag) {
  415. clearInterval(_this.data.countDownInter);
  416. _this.setData({
  417. endTimeShow: false,
  418. flag: true
  419. });
  420. };
  421. if (data.data.bid_opening != _this.data.openData.bid_opening) {
  422. if (data.data.bid_opening === '已结束' && data.data.bidding_man === wx.getStorageSync('persion').persion.kid) {
  423. _this.setData({
  424. mask: true
  425. })
  426. // wx.showToast({
  427. // title: '恭喜您竞价成功!',
  428. // icon: 'success',
  429. // duration: 3000
  430. // });
  431. clearInterval(_this.data.countDownInter);
  432. } else if (data.data.bid_opening === '已结束' && data.data.bidding_man !== wx.getStorageSync('persion').persion.kid) {
  433. wx.showToast({
  434. title: '很遗憾,竞价失败!',
  435. icon: 'none',
  436. duration: 3000
  437. });
  438. }
  439. _this.listData(_this.data.liveid);
  440. };
  441. _this.setData({
  442. onlineCount: data.data.online_count,
  443. })
  444. },
  445. fail: function (res) { },
  446. complete: function (res) { },
  447. });
  448. }
  449. },
  450. //结束倒计时
  451. endTime: function (closeTime) {
  452. let _this = this;
  453. var time = parseInt(((closeTime + 15000) - _this.data.nowTime)/1000 );
  454. _this.data.countDownInter = setInterval(function(){
  455. if ( time > 0 ) {
  456. time -= 1;
  457. _this.setData({
  458. endTimeText: time,
  459. endTimeShow: true
  460. })
  461. } else if (time <= 0) {
  462. clearInterval(_this.data.countDownInter);
  463. _this.setData({
  464. endTimeText: time,
  465. endTimeShow: false,
  466. flag: true
  467. });
  468. };
  469. },1000);
  470. },
  471. //减价
  472. reducePrice: function (e) {
  473. let _this = this;
  474. let new_bidder = _this.data.new_bidder - _this.data.openData.price_add;
  475. if (new_bidder <= _this.data.openData.price_bidder ){
  476. wx.showToast({
  477. title: '出价不得低于最低加价幅度',
  478. icon: 'none',
  479. duration: 3000
  480. })
  481. } else {
  482. _this.setData({
  483. new_bidder: new_bidder
  484. })
  485. }
  486. },
  487. //加价
  488. reduceIncrease: function (e) {
  489. let _this = this;
  490. let new_bidder = (_this.data.new_bidder + _this.data.openData.price_add);
  491. _this.setData({
  492. new_bidder: new_bidder
  493. })
  494. },
  495. //出价
  496. bid:function (e) {
  497. let _this = this;
  498. wx.request({
  499. url: app.globalData.prodUrl + 'web-pm/wx/pm/compete_price',
  500. data: {
  501. pm_item_id: _this.data.openData.kid,
  502. price_bidder: _this.data.new_bidder === 0 ? 1 : _this.data.new_bidder
  503. },
  504. header: { 'ydw-token': wx.getStorageSync('key').ydw_token },
  505. method: 'GET',
  506. dataType: 'json',
  507. responseType: 'text',
  508. success: function(data) {
  509. if(data.data.success) {
  510. _this.setData({
  511. record: data.data.record,
  512. bidder: data.data.pm_item_submit_offer.price_bidder,
  513. new_bidder: data.data.pm_item_submit_offer.price_bidder + _this.data.openData.price_add
  514. },function() {
  515. })
  516. var selectkid = _this.data.SelectKid || _this.data.goodsKid;
  517. new Promise((resolve,reject) => {
  518. _this.initData(selectkid);
  519. resolve();
  520. }).then(() => {
  521. _this.listData(_this.data.liveid);
  522. })
  523. wx.showToast({
  524. title: data.data.tip.msg,
  525. icon: 'none',
  526. duration: 3000
  527. })
  528. } else {
  529. wx.showToast({
  530. title: data.data.tip.msg,
  531. icon: 'none',
  532. duration: 3000
  533. })
  534. }
  535. },
  536. fail: function(res) {
  537. },
  538. complete: function(res) {
  539. },
  540. })
  541. },
  542. //打开关闭商品详请
  543. openDetail: function (e) {
  544. let move = this.data.pageWidth * 0.9;
  545. if(this.data.detailOpen) {
  546. this.animation.translate(0).step()
  547. this.setData({
  548. detailOpen: false,
  549. chatBool: true,
  550. notPlay: true,
  551. animationData: this.animation.export()
  552. })
  553. } else {
  554. this.animation.translate(move).step()
  555. this.setData({
  556. detailOpen: true,
  557. chatBool: false,
  558. notPlay: false,
  559. animationData: this.animation.export()
  560. })
  561. }
  562. },
  563. //打开关闭出价页面
  564. openBidding: function (e) {
  565. let move = this.data.pageWidth * 0.9;
  566. if (this.data.biddingOpen) {
  567. this.animation.translate(0).step()
  568. this.setData({
  569. biddingOpen: false,
  570. chatBool : true,
  571. notPlay : true,
  572. animationData: this.animation.export()
  573. })
  574. } else {
  575. this.animation.translate(-move).step()
  576. this.setData({
  577. biddingOpen: true,
  578. chatBool: false,
  579. notPlay: false,
  580. animationData: this.animation.export()
  581. })
  582. }
  583. },
  584. //交保证金
  585. subDeposit: function (e) {
  586. let _this = this;
  587. wx.navigateTo({
  588. 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,
  589. success: function (res) {
  590. clearInterval(_this.data.setInter);
  591. clearInterval(_this.data.countDownInter);
  592. }
  593. })
  594. },
  595. //保存聊天文本
  596. chatText: function (e) {
  597. this.setData({
  598. chatText: e.detail.value
  599. });
  600. },
  601. //发送聊天文本
  602. send: function (e) {
  603. let _this = this;
  604. if ( _this.data.chatText !== '' ) {
  605. wx.request({
  606. url: app.globalData.prodUrl + 'web-pm/wx/pm/add_chitchat',
  607. data: {
  608. content: _this.data.chatText,
  609. liveroom_id: _this.data.liveid
  610. },
  611. header: { 'ydw-token': wx.getStorageSync('key').ydw_token },
  612. method: 'GET',
  613. dataType: 'json',
  614. responseType: 'text',
  615. success: function (data) {
  616. _this.setData({
  617. chatText: ''
  618. },function() {
  619. _this.getChatList()
  620. })
  621. }
  622. })
  623. } else {
  624. }
  625. },
  626. //检测拍卖状态
  627. getLiveState: function (e) {
  628. },
  629. getNetworkState: function (e) {
  630. let _this = this;
  631. if(e.detail.info.netSpeed === 0) {
  632. _this.setData({
  633. notPlay: true
  634. })
  635. } else {
  636. _this.setData({
  637. notPlay: false
  638. })
  639. }
  640. },
  641. //跳转首页
  642. backHome: function () {
  643. wx.reLaunch({
  644. url: '/pages/index/index'
  645. })
  646. },
  647. getChatList() {
  648. var _this = this;
  649. wx.request({
  650. url: app.globalData.prodUrl + 'web-pm/wx/pm/get_chatting_records',
  651. data: {
  652. live_room_id: _this.data.liveid,
  653. chat_time: _this.data.time
  654. },
  655. header: {
  656. 'ydw-token': wx.getStorageSync('key').ydw_token
  657. },
  658. method: 'GET',
  659. dataType: 'json',
  660. responseType: 'text',
  661. success: function (data) {
  662. var chatList = data.data.data;
  663. if (chatList.length!=0) {
  664. var time = data.data.data[0].created;
  665. _this.setData({
  666. chatList: chatList,
  667. time
  668. }, function () {
  669. _this.pageScrollToBottom();
  670. })
  671. }
  672. }
  673. })
  674. },
  675. pageScrollToBottom: function () {
  676. var _this = this;
  677. this.setData({
  678. scrollTop: this.data.scrollTop + 40
  679. }, function () {
  680. }
  681. )
  682. },
  683. lookDetail() {
  684. this.setData({
  685. detailBool: !this.data.detailBool
  686. })
  687. },
  688. gopay: function (e) {
  689. wx.navigateTo({
  690. url: '/pages/mine/wxml/beingphotographed/beingphotographed'
  691. })
  692. },
  693. closePay: function (e) {
  694. this.setData({
  695. mask : false
  696. })
  697. },
  698. opDetail : function(e) {
  699. var a = e.currentTarget.dataset.ind;
  700. var arr = this.data.listData;
  701. for (var i = 0; i < arr.length ; i ++) {
  702. if(a === i) {
  703. arr[i].bool = true
  704. }
  705. }
  706. this.setData({
  707. // productBool : true,
  708. listData : arr
  709. })
  710. },
  711. closeDetail : function(e) {
  712. var a = e.currentTarget.dataset.ind;
  713. var arr = this.data.listData;
  714. for (var i = 0; i < arr.length; i++) {
  715. if (a === i) {
  716. arr[i].bool = false
  717. }
  718. }
  719. this.setData({
  720. // productBool: true,
  721. listData: arr
  722. })
  723. }
  724. })