handOutBonus.js 7.1 KB

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