shareBonus.js 9.3 KB

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