index.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // pages/mall/payment/payment.js
  2. import { getUserInfo, rechargeRoutine, getRechargeApi } from '../../api/user.js'
  3. var app = getApp()
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. picList: [],
  10. parameter: {
  11. navbar: '1',
  12. return: '1',
  13. title: '余额充值',
  14. color: false
  15. },
  16. navRecharge: ['账户充值', '佣金转入'],
  17. active: 0,
  18. number: '',
  19. focus: true,
  20. userinfo: {},
  21. placeholder: '0.00',
  22. placeholderOther: '其他',
  23. activePic: 0,
  24. numberPic: '',
  25. rechar_id: 0,
  26. recharge_attention: []
  27. },
  28. /**
  29. * 登录授权回调
  30. */
  31. onLoadFun: function () {
  32. this.getUserInfo()
  33. this.getRecharge()
  34. },
  35. setPlaceholderStatus: function (event) {
  36. if (event.detail.value.length == 0)
  37. this.setData({
  38. placeholder: '0.00'
  39. })
  40. },
  41. setPlaceholder: function () {
  42. this.setData({
  43. placeholder: ''
  44. })
  45. },
  46. setOtherPlaceholder: function () {
  47. this.setData({
  48. placeholderOther: ''
  49. })
  50. },
  51. setOtherPlaceholderStatus: function (event) {
  52. if (event.detail.value.length == 0)
  53. this.setData({
  54. placeholderOther: '其他'
  55. })
  56. },
  57. navRecharge: function (e) {
  58. this.setData({
  59. active: e.currentTarget.dataset.id,
  60. activePic: 0
  61. })
  62. if (this.data.picList.length) {
  63. this.setData({
  64. rechar_id: this.data.picList[0].id,
  65. numberPic: this.data.picList[0].price
  66. })
  67. }
  68. },
  69. /**
  70. * 选择金额
  71. */
  72. picCharge: function (e) {
  73. if (e.currentTarget.dataset.id == 0) {
  74. this.setData({
  75. activePic: e.currentTarget.dataset.index,
  76. rechar_id: 0,
  77. numberPic: ''
  78. })
  79. } else {
  80. this.setData({
  81. number: '',
  82. activePic: e.currentTarget.dataset.index,
  83. rechar_id: e.currentTarget.dataset.id,
  84. numberPic: e.currentTarget.dataset.quota,
  85. placeholderOther: '其他'
  86. })
  87. }
  88. },
  89. /**
  90. * 生命周期函数--监听页面加载
  91. */
  92. onLoad: function (options) {},
  93. /**
  94. * 充值额度选择
  95. */
  96. getRecharge: function () {
  97. var that = this
  98. getRechargeApi().then(res => {
  99. that.setData({
  100. picList: res.data.recharge_quota,
  101. rechar_id: res.data.recharge_quota[0].id,
  102. numberPic: res.data.recharge_quota[0].price,
  103. recharge_attention: res.data.recharge_attention || []
  104. })
  105. })
  106. },
  107. /**
  108. * 获取用户信息
  109. */
  110. getUserInfo: function () {
  111. var that = this
  112. getUserInfo().then(res => {
  113. that.setData({
  114. userinfo: res.data
  115. })
  116. // app.globalData.unread = res.data.notice
  117. app.setUnread(res.data.notice)
  118. })
  119. },
  120. /*
  121. * 用户充值
  122. */
  123. submitSub: function (e) {
  124. let that = this,
  125. value = e.detail.value.number,
  126. commissionCount = that.data.userinfo.commissionCount
  127. if (that.data.active) {
  128. if (parseFloat(value) < 0 || !value)
  129. return app.Tips({
  130. title: '请输入金额'
  131. })
  132. if (Number(value) > Number(commissionCount))
  133. return app.Tips({
  134. title: '转入金额不能大于' + commissionCount
  135. })
  136. wx.showModal({
  137. title: '转入余额',
  138. content: '转入余额后无法再次转出,确认是否转入余额',
  139. success(res) {
  140. if (res.confirm) {
  141. rechargeRoutine({
  142. price: value,
  143. type: 1
  144. })
  145. .then(res => {
  146. that.setData({
  147. 'userinfo.now_money': app.help().Add(value, that.data.userinfo.now_money)
  148. })
  149. return app.Tips(
  150. {
  151. title: '转入成功',
  152. icon: 'success'
  153. },
  154. {
  155. tab: 5,
  156. url: '/pages/user_money/index'
  157. }
  158. )
  159. })
  160. .catch(err => {
  161. return app.Tips({
  162. title: err
  163. })
  164. })
  165. } else if (res.cancel) {
  166. return app.Tips({
  167. title: '已取消'
  168. })
  169. }
  170. }
  171. })
  172. } else {
  173. if (this.data.picList.length == this.data.activePic && !value)
  174. return app.Tips({
  175. title: '请输入金额'
  176. })
  177. wx.showLoading({
  178. title: '正在支付'
  179. })
  180. rechargeRoutine({
  181. price: that.data.rechar_id == 0 ? value : that.data.numberPic,
  182. type: 0,
  183. rechar_id: that.data.rechar_id
  184. })
  185. .then(res => {
  186. wx.hideLoading()
  187. let jsConfig = res.data
  188. wx.requestPayment({
  189. timeStamp: jsConfig.timestamp,
  190. nonceStr: jsConfig.nonceStr,
  191. package: jsConfig.package,
  192. signType: jsConfig.signType,
  193. paySign: jsConfig.paySign,
  194. success: function (res) {
  195. that.setData({
  196. 'userinfo.now_money': app.help().Add(value, that.data.userinfo.now_money)
  197. })
  198. return app.Tips(
  199. {
  200. title: '支付成功',
  201. icon: 'success'
  202. },
  203. {
  204. tab: 5,
  205. url: '/pages/user_money/index'
  206. }
  207. )
  208. },
  209. fail: function () {
  210. return app.Tips({
  211. title: '支付失败'
  212. })
  213. },
  214. complete: function (res) {
  215. if (res.errMsg == 'requestPayment:cancel')
  216. return app.Tips({
  217. title: '取消支付'
  218. })
  219. }
  220. })
  221. })
  222. .catch(err => {
  223. wx.hideLoading()
  224. return app.Tips({
  225. title: err
  226. })
  227. })
  228. }
  229. }
  230. })