handOutBonus2.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // pages/handOutBonus/handOutBonus.js
  2. const getReq = require('./../../config.js').getReq;
  3. const app = getApp();
  4. import recordSource from '../../utils/recordSource';
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. bonusType: 'pin',
  11. canShare: false,
  12. datas: {},
  13. total: 0,
  14. count: 0,
  15. money: 0,
  16. tempCount: '',
  17. tempMoney: '',
  18. sendFlag: false,
  19. calcFlag: true,
  20. path: '',
  21. img_url: '',
  22. title: '',
  23. tempBless: '',
  24. bless: '',
  25. userInfo: app.globalData.userInfo
  26. },
  27. /**
  28. * 生命周期函数--监听页面加载
  29. */
  30. onLoad: function (options) {
  31. if (app.globalData.userInfo) {
  32. this.getDatas()
  33. this.setData({
  34. userInfo: app.globalData.userInfo
  35. })
  36. }
  37. },
  38. getAuth(e) {
  39. let { userInfo } = e.detail
  40. if (userInfo) {
  41. this.getDatas()
  42. this.setData({
  43. userInfo: app.globalData.userInfo
  44. })
  45. }
  46. },
  47. getDatas() {
  48. let params = {
  49. act: 'member_bonus',
  50. op: 'predepositex'
  51. }
  52. getReq(params, res => {
  53. if (res.code == 200) {
  54. let { datas } = res
  55. let tempBless = datas['send_bless']
  56. this.setData({
  57. datas,
  58. tempBless
  59. })
  60. }
  61. })
  62. },
  63. calcTotal(count, money) {
  64. if (count && money && this.data.bonusType == 'pin') {
  65. let single = money / count
  66. if (single < 0.01) {
  67. wx.showToast({
  68. icon: 'none',
  69. title: '单个红包金额小于0.01!',
  70. duration: 2000
  71. })
  72. this.setData({
  73. calcFlag: false
  74. })
  75. return false
  76. }
  77. }
  78. if (this.data.bonusType == 'pin') {
  79. let calcFlag = this.checkTotal(money)
  80. this.setData({
  81. total: money,
  82. calcFlag
  83. })
  84. }
  85. else {
  86. let bonus = count * money
  87. let total = parseInt(bonus * 100 + 0.5) / 100
  88. let calcFlag = this.checkTotal(total)
  89. this.setData({
  90. total,
  91. calcFlag
  92. })
  93. }
  94. },
  95. checkTotal(money) {
  96. return money < this.data.datas['usable']
  97. },
  98. bindKeyInput(e) {
  99. let { type } = e.currentTarget.dataset
  100. let value = e.detail.value
  101. let flag = this.checkInput(type, value)
  102. if (!flag) {
  103. return
  104. }
  105. this.setData({
  106. sendFlag: true
  107. })
  108. },
  109. checkInput(type, value) {
  110. if (type == 'count') {
  111. if (value[0] == 0) {
  112. this.setData({
  113. tempCount: ''
  114. })
  115. return false
  116. }
  117. else if (value > 500) {
  118. let tempCount = value.substring(0, value.length - 1)
  119. this.setData({
  120. tempCount,
  121. count: tempCount
  122. })
  123. wx.showToast({
  124. icon: 'none',
  125. title: '一次最多发送红包500个',
  126. duration: 2000
  127. })
  128. this.calcTotal(tempCount || 0, this.data.money || 0)
  129. }
  130. else {
  131. this.setData({
  132. count: value
  133. })
  134. this.calcTotal(value || 0, this.data.money || 0)
  135. }
  136. }
  137. else if (type == 'money') {
  138. if (value.indexOf('.') != -1) {
  139. let tempArr = value.split('.')
  140. if (tempArr[1].length > 2) {
  141. tempArr[1] = tempArr[1].substring(0, tempArr[1].length - 1)
  142. }
  143. value = tempArr[0] + '.' + tempArr[1] || 0
  144. this.setData({
  145. tempMoney: value,
  146. money: parseFloat(value)
  147. })
  148. this.calcTotal(this.data.count || 0, parseFloat(value) || 0)
  149. }
  150. else if (value > this.data.datas['usable']) {
  151. let tempMoney = value.substring(0, value.length - 1)
  152. this.setData({
  153. tempMoney: tempMoney,
  154. money: parseFloat(tempMoney)
  155. })
  156. this.calcTotal(this.data.count || 0, parseFloat(tempMoney) || 0)
  157. wx.showToast({
  158. icon: 'none',
  159. title: '不能大于可分享的最大金额!',
  160. duration: 2000
  161. })
  162. return false
  163. }
  164. else {
  165. this.setData({
  166. money: parseFloat(value)
  167. })
  168. this.calcTotal(this.data.count || 0, parseFloat(value) || 0)
  169. }
  170. }
  171. return true
  172. },
  173. secBonusType(e) {
  174. let { type } = e.currentTarget.dataset
  175. this.setData({
  176. bonusType: type,
  177. })
  178. this.calcTotal(this.data.count || 0, this.data.money || 0)
  179. },
  180. checkBonus() {
  181. if (!this.data.sendFlag || !this.data.calcFlag) {
  182. return
  183. }
  184. this.shareBonus()
  185. },
  186. shareBonus() {
  187. let params = {
  188. act: 'member_bonus',
  189. op: 'make',
  190. bonus_rate: this.data.datas['share_bonus_rate'][0]['rate'],
  191. total_num: this.data.count,
  192. type_bless: this.data.bless || this.data.datas['send_bless']
  193. }
  194. if (this.data.bonusType == "pin") {
  195. let data = {
  196. total_amount: this.data.money,
  197. send_type: 1
  198. }
  199. params = Object.assign({}, params, data)
  200. }
  201. else {
  202. let data = {
  203. fixed_money: this.data.money,
  204. send_type: 2
  205. }
  206. params = Object.assign({}, params, data)
  207. }
  208. getReq(params, res => {
  209. if (res.code == 200) {
  210. let { title, path } = res.datas
  211. this.setData({
  212. canShare: true,
  213. title,
  214. path
  215. })
  216. }
  217. else {
  218. wx.showToast({
  219. icon: 'none',
  220. title: res.message,
  221. duration: 2000
  222. })
  223. return false
  224. }
  225. })
  226. },
  227. close() {
  228. this.setData({
  229. canShare: false
  230. })
  231. },
  232. blessFocus(e) {
  233. let { value } = e.detail
  234. if (value == this.data.datas['send_bless']) {
  235. this.setData({
  236. tempBless: ''
  237. })
  238. }
  239. },
  240. blessBlur(e) {
  241. let { value } = e.detail
  242. if(!this.trim(value)) {
  243. this.setData({
  244. tempBless: this.data.datas['send_bless']
  245. })
  246. }
  247. },
  248. blessInput(e) {
  249. let { value } = e.detail
  250. this.setData({
  251. bless: value
  252. })
  253. },
  254. trim(str) {
  255. str = str.replace(/\s+/g, "")
  256. return str
  257. },
  258. testBonus() {
  259. wx.navigateTo({
  260. url: '/pages/shareBonus/shareBonus?type_sn=23641538992987220059'
  261. });
  262. },
  263. /**
  264. * 生命周期函数--监听页面初次渲染完成
  265. */
  266. onReady: function () {
  267. },
  268. /**
  269. * 生命周期函数--监听页面显示
  270. */
  271. onShow: function () {
  272. },
  273. /**
  274. * 生命周期函数--监听页面隐藏
  275. */
  276. onHide: function () {
  277. },
  278. /**
  279. * 生命周期函数--监听页面卸载
  280. */
  281. onUnload: function () {
  282. },
  283. /**
  284. * 页面相关事件处理函数--监听用户下拉动作
  285. */
  286. onPullDownRefresh: function () {
  287. },
  288. /**
  289. * 页面上拉触底事件的处理函数
  290. */
  291. onReachBottom: function () {
  292. },
  293. /**
  294. * 用户点击右上角分享
  295. */
  296. onShareAppMessage: function (res) {
  297. let self = this
  298. if (res.from == "button") {
  299. this.setData({
  300. canShare: false
  301. })
  302. let imageUrl = '../../image/share_bonus.png'
  303. let { title, path } = this.data
  304. return {
  305. title,
  306. imageUrl: imageUrl,
  307. path: `/${path}`,
  308. complete: function(res) {
  309. self.getDatas()
  310. }
  311. }
  312. }
  313. }
  314. })