shareBonus.js 9.4 KB

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