shareBonus.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. // pages/pandaBonus/pandaBonus.js
  2. const app = getApp();
  3. const getReq = require('./../../config.js').getReq;
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. openAnimation: true, // 点击开红包
  10. activityType: '', // 1 活动未开始
  11. renderCount: [],
  12. bonus_state: '',
  13. mine_bonus: '',
  14. reveive_info: '',
  15. binded_info: [], // 领取列表
  16. msg: '',
  17. mineid: '',
  18. type_sn: '',
  19. message_flag: false,
  20. input_msg: '',
  21. result_msg: '留言',
  22. summarys: false,
  23. userInfo: app.globalData.userInfo,
  24. options: ''
  25. },
  26. /**
  27. * 生命周期函数--监听页面加载
  28. */
  29. onLoad: function (options) {
  30. let self = this
  31. this.setData({
  32. options
  33. })
  34. if (app.globalData.userInfo) {
  35. self.init(options)
  36. self.setData({
  37. userInfo: app.globalData.userInfo
  38. })
  39. }
  40. },
  41. getAuth(e) {
  42. let { userInfo } = e.detail
  43. if (userInfo) {
  44. this.init(this.data.options)
  45. this.setData({
  46. userInfo
  47. })
  48. }
  49. },
  50. init(options) {
  51. let self = this
  52. let { type_sn, relay_id = '' } = options
  53. let params = {
  54. act: 'bonusex',
  55. op: 'open',
  56. type_sn
  57. }
  58. self.getDatas(params)
  59. self.setData({
  60. type_sn,
  61. relay_id
  62. })
  63. },
  64. detail() {
  65. let params = {
  66. act: 'bonusex',
  67. op: 'detail',
  68. type_sn: this.data.type_sn
  69. }
  70. this.getDatas(params)
  71. },
  72. grab() {
  73. let params = {
  74. act: 'bonusex',
  75. op: 'grab',
  76. type_sn: this.data.type_sn
  77. }
  78. this.getDatas(params)
  79. },
  80. getDatas(params) {
  81. let self = this
  82. getReq(params, function (res) {
  83. if (res.code == 200) {
  84. console.log('res:', res)
  85. let { type_info, bonus_state } = res.datas
  86. if (bonus_state == 'unstart') {
  87. // bonus_state: unstart 活动未开始
  88. let { send_start_date } = type_info
  89. self.countTime(params, send_start_date)
  90. self.setData({
  91. bonus_state
  92. })
  93. return
  94. }
  95. else if (bonus_state == 'open') {
  96. // open 活动开始, 可以点击, 红包金额有 固定, 随机
  97. self.setData({
  98. type_info,
  99. bonus_state
  100. })
  101. return
  102. }
  103. else if (bonus_state == 'detail') {
  104. // 已点开状态, 详情
  105. let { avatars, mine_bonus, mineid, summarys } = res.datas
  106. let { sender_id, relayer_id } = type_info
  107. let sender_head_src = self.senderHead(relayer_id, sender_id, avatars)
  108. let reveive_info = self.receiveInfo(type_info) // 展示领取信息
  109. if (summarys) {
  110. summarys = summarys.map(item => {
  111. item['goods_lowest_price'] = parseFloat(item['goods_lowest_price'])
  112. item['goods_price'] = parseFloat(item['goods_price'])
  113. return item
  114. })
  115. }
  116. //领取列表
  117. let binded_info = []
  118. if (res.datas['binded_info']) {
  119. binded_info = self.getBindedInfo(res.datas['binded_info'], avatars, type_info)
  120. }
  121. if (mine_bonus) {
  122. mine_bonus['bonus_value'] = parseInt(mine_bonus['bonus_value'] * 100 + 0.5) / 100
  123. }
  124. self.setData({
  125. sender_head_src,
  126. type_info,
  127. bonus_state,
  128. mine_bonus,
  129. reveive_info,
  130. binded_info,
  131. mineid: mineid.toString(),
  132. summarys
  133. })
  134. return
  135. }
  136. else if (bonus_state == 'over') {
  137. let { msg } = res.datas
  138. self.setData({
  139. msg,
  140. bonus_state
  141. })
  142. return
  143. }
  144. else if (bonus_state == 'end') {
  145. self.setData({
  146. type_info,
  147. bonus_state
  148. })
  149. return
  150. }
  151. else {
  152. wx.showModal({
  153. confirmText: '重试',
  154. content: '网络错误',
  155. success: function (res) {
  156. if (res.confirm) {
  157. wx.reLaunch({
  158. url: "/pages/index/index"
  159. })
  160. } else if (res.cancel) {
  161. console.log('用户点击取消')
  162. }
  163. }
  164. })
  165. }
  166. }
  167. });
  168. },
  169. getBindedInfo(binded_info, avatars, type_info) {
  170. let self = this
  171. return binded_info.map(item => {
  172. if (avatars[item['user_id']]) {
  173. let avatarItem = avatars[item['user_id']]
  174. item['avatar'] = avatarItem['avatar']
  175. let discount = parseInt(avatarItem['discount'] * 100 + 0.5)
  176. if (discount != 0) {
  177. discount = discount / 100
  178. }
  179. item['discount'] = discount
  180. }
  181. else {
  182. item['avatar'] = '../../image/bonus/female.png'
  183. item['discount'] = 0
  184. }
  185. item['is_king'] = self.isKing(type_info, item['bonus_value'])
  186. item['format_time'] = self.formatTime(item['get_time'])
  187. item['bonus_value'] = parseInt(item['bonus_value'] * 100 + 0.5) / 100
  188. return item
  189. })
  190. },
  191. // 判断手气最佳
  192. isKing(type_info, bonus_value) {
  193. let { total_num, binded_num, send_type, max_amount } = type_info
  194. if (send_type != 1) return false;
  195. if (parseInt(total_num) != parseInt(binded_num)) return false;
  196. if (parseInt(max_amount * 100) != parseInt(bonus_value * 100)) return false;
  197. return true
  198. },
  199. formatTime(time) {
  200. let d = new Date(time * 1000)
  201. let month = this.zero(d.getMonth() + 1)
  202. let date = this.zero(d.getDate())
  203. let h = this.zero(d.getHours())
  204. let m = this.zero(d.getMinutes())
  205. return `${month}-${date} ${h}:${m}`
  206. },
  207. senderHead(relayer_id, sender_id, avatars) {
  208. let sender_head_src = '../../image/bonus/female.png'
  209. if (relayer_id > 0) {
  210. sender_head_src = avatars[relayer_id]['avatar']
  211. }
  212. else if (sender_id > 0) {
  213. sender_head_src = avatars[sender_id]['avatar']
  214. }
  215. return sender_head_src
  216. },
  217. receiveInfo(type_info) {
  218. let { total_num, binded_num } = type_info
  219. if (total_num == binded_num) {
  220. // 红包领完了
  221. let timestr = `${total_num}个红包,${this.bindedTime(type_info['binded_period'])}`
  222. return timestr
  223. }
  224. else {
  225. let timestr = `领取 ${binded_num}/${total_num}`
  226. let isEndTime = this.isEnd(type_info['send_end_date'])
  227. if (isEndTime == false) {
  228. timestr += ',点击右上角继续分享此红包'
  229. }
  230. return timestr
  231. }
  232. },
  233. isEnd(send_end_date) {
  234. let sendDate = parseInt(send_end_date)
  235. if (sendDate === 0) {
  236. return false
  237. }
  238. else {
  239. return new Date().getTime >= sendDate * 1000
  240. }
  241. },
  242. bindedTime(period) {
  243. let min = 60
  244. let hour = 3600
  245. let day = 24 * 3600
  246. let days = parseInt(period / day)
  247. period = period % day
  248. let hours = parseInt(period / hour)
  249. period = period % hour
  250. let mins = parseInt(period / min)
  251. period = period % min
  252. if (period > 0) {
  253. mins += 1
  254. }
  255. let timestr = ''
  256. if (days > 0) {
  257. timestr += `${days}天`
  258. }
  259. if (hours > 0) {
  260. timestr += `${hours}小时`
  261. }
  262. if (mins > 0) {
  263. timestr += `${mins}分钟`
  264. }
  265. timestr += ',领完'
  266. return timestr
  267. },
  268. countTime(params, send_start_date) {
  269. let times = parseInt(send_start_date - new Date().getTime() / 1000)
  270. if (times <= 0) {
  271. this.getDatas(params) // 活动时间到了,重新请求
  272. return
  273. }
  274. let h = Math.floor(times / 3600)
  275. h = this.zero(h)
  276. let m = Math.floor((times / 60) % 60)
  277. m = this.zero(m)
  278. let s = times % 60
  279. s = this.zero(s)
  280. let renderCount = [h.split(''), m.split(''), s.split('')]
  281. this.setData({
  282. renderCount
  283. })
  284. setTimeout(() => {
  285. this.countTime(send_start_date)
  286. }, 1000);
  287. },
  288. zero(n) {
  289. n = n >= 10 ? n.toString() : '0' + n
  290. return n
  291. },
  292. open() {
  293. this.setData({
  294. openAnimation: false
  295. })
  296. this.grab()
  297. },
  298. bindKeyInput(e) {
  299. let value = e.detail.value
  300. this.setData({
  301. input_msg: value
  302. })
  303. },
  304. message_tap(e) {
  305. let { flag } = e.currentTarget.dataset
  306. this.setData({
  307. message_flag: flag
  308. })
  309. },
  310. send_msg() {
  311. let self = this
  312. let result_msg = this.trim(this.data.input_msg)
  313. if (!result_msg) return;
  314. let params = {
  315. act: 'bonusex',
  316. op: 'comment',
  317. comment: result_msg,
  318. bonus_sn: this.data.mine_bonus['bonus_sn']
  319. }
  320. getReq(params, function (res) {
  321. if (res.code == 200) {
  322. self.setData({
  323. message_flag: false,
  324. result_msg
  325. })
  326. }
  327. else {
  328. wx.showToast({
  329. icon: 'none',
  330. title: 'error',
  331. duration: 2000
  332. })
  333. self.setData({
  334. message_flag: false,
  335. input_msg: ''
  336. })
  337. }
  338. }, 'POST')
  339. },
  340. trim(str) {
  341. str = str.replace(/\s+/g, "")
  342. return str
  343. },
  344. backHome: function () {
  345. wx.reLaunch({
  346. url: '/pages/index/index',
  347. })
  348. },
  349. /**
  350. * 生命周期函数--监听页面初次渲染完成
  351. */
  352. onReady: function () {
  353. },
  354. /**
  355. * 生命周期函数--监听页面显示
  356. */
  357. onShow: function () {
  358. },
  359. /**
  360. * 生命周期函数--监听页面隐藏
  361. */
  362. onHide: function () {
  363. },
  364. /**
  365. * 生命周期函数--监听页面卸载
  366. */
  367. onUnload: function () {
  368. },
  369. /**
  370. * 页面相关事件处理函数--监听用户下拉动作
  371. */
  372. onPullDownRefresh: function () {
  373. },
  374. /**
  375. * 页面上拉触底事件的处理函数
  376. */
  377. onReachBottom: function () {
  378. },
  379. /**
  380. * 用户点击右上角分享
  381. */
  382. onShareAppMessage: function () {
  383. let imageUrl = '../../image/share_bonus.png'
  384. let { type_sn, relay_id } = this.data
  385. return {
  386. title: `点一下,你和TA都有红包!`,
  387. path: `/pages/shareBonus/shareBonus?type_sn=${type_sn}&relay_id=${relay_id}`,
  388. imageUrl: imageUrl
  389. }
  390. }
  391. })