shareBonus.js 9.3 KB

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