shareBonus.js 9.7 KB

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