index.js 6.8 KB

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