shareBonus.js 9.8 KB

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