index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // pages/storedValue/index.js
  2. import {
  3. get,
  4. post
  5. } from '../../utils/http'
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. selectRe: 0,
  12. agree: true,
  13. // 自定义金额
  14. money: null,
  15. // 控制自定义金额显示
  16. isShow: 1,
  17. isOffline: 0, //是否线下充值,1是,0否
  18. store_id: '', //店铺ID
  19. },
  20. // 获取输入的自定义金额
  21. getInput(e) {
  22. this.setData({
  23. money: e.detail.value
  24. })
  25. },
  26. agree() {
  27. this.setData({
  28. agree: !this.data.agree
  29. })
  30. },
  31. /**
  32. * 生命周期函数--监听页面加载
  33. */
  34. onLoad: function (options) {
  35. this.setData({
  36. store_id: wx.getStorageSync('store_id')
  37. })
  38. if (options.q) {
  39. let ticket = decodeURIComponent(options.q)
  40. let store_id = 1
  41. console.log(ticket);
  42. var reg = new RegExp("(^|\\?|&)" + 'store_id' + "=([^&]*)(\\s|&|$)", "i");
  43. if (reg.test(ticket)) {
  44. store_id = unescape(RegExp.$2.replace(/\+/g, " "));
  45. console.log(store_id, 'store_id');
  46. };
  47. this.setData({
  48. isOffline: 1,
  49. store_id
  50. })
  51. }
  52. this.getRecharge()
  53. this.getAmount()
  54. },
  55. submitRe() {
  56. if (!this.data.agree) {
  57. wx.showToast({
  58. title: '请先勾选同意jolijoli充值协议',
  59. icon: 'none'
  60. })
  61. return
  62. }
  63. let that = this
  64. console.log('store_id:', this.data.store_id, 'offline:', this.data.isOffline, "amount:", this.data.money || this.data.reChargeList[this.data.selectRe].amount);
  65. post('api/recharge/add', {
  66. amount: this.data.money || this.data.reChargeList[this.data.selectRe].amount,
  67. store_id: this.data.store_id,
  68. offline: this.data.isOffline
  69. }, (res) => {
  70. if (res.data) {
  71. wx.requestPayment({
  72. timeStamp: res.data.pay_data.timeStamp,
  73. nonceStr: res.data.pay_data.nonceStr,
  74. package: res.data.pay_data.package,
  75. signType: res.data.pay_data.signType,
  76. paySign: res.data.pay_data.paySign,
  77. success(res) {
  78. if (res.errMsg == 'requestPayment:ok') {
  79. wx.showToast({
  80. title: '支付成功',
  81. icon: 'success'
  82. })
  83. that.getAmount()
  84. }
  85. },
  86. fail(res) {
  87. wx.showToast({
  88. title: '支付失败',
  89. icon: 'error'
  90. })
  91. },
  92. complete: (res) => {
  93. this.setData({
  94. money: null
  95. })
  96. }
  97. })
  98. }
  99. })
  100. },
  101. selectRe(e) {
  102. this.setData({
  103. selectRe: e.currentTarget.dataset.index
  104. })
  105. },
  106. /**
  107. * 获取我的储值
  108. * api/user
  109. */
  110. getAmount() {
  111. get('api/user/amount', {}, (res) => {
  112. if (res.data) {
  113. this.setData({
  114. inserllAmount: res.data.amount
  115. })
  116. }
  117. })
  118. },
  119. /**
  120. * 获取充值列表
  121. *
  122. */
  123. getRecharge() {
  124. let storeId = wx.getStorageSync('store_id')
  125. get('api/recharge/list', {
  126. store_id: storeId
  127. }, (res) => {
  128. if (res.data) {
  129. this.setData({
  130. reChargeList: res.data.list
  131. })
  132. //控制显示自定义金额
  133. this.setData({
  134. isShow: this.data.isShow == res.data.allow_custom ? true : false,
  135. })
  136. }
  137. })
  138. },
  139. /**
  140. * 生命周期函数--监听页面初次渲染完成
  141. */
  142. onReady: function () {
  143. },
  144. /**
  145. * 生命周期函数--监听页面显示
  146. */
  147. onShow: function () {
  148. },
  149. /**
  150. * 生命周期函数--监听页面隐藏
  151. */
  152. onHide: function () {
  153. },
  154. /**
  155. * 生命周期函数--监听页面卸载
  156. */
  157. onUnload: function () {
  158. },
  159. /**
  160. * 页面相关事件处理函数--监听用户下拉动作
  161. */
  162. onPullDownRefresh: function () {
  163. },
  164. /**
  165. * 页面上拉触底事件的处理函数
  166. */
  167. onReachBottom: function () {
  168. },
  169. /**
  170. * 用户点击右上角分享
  171. */
  172. onShareAppMessage: function () {
  173. }
  174. })