index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // index.js
  2. let myPro = require("../../../utils/wxRequest.js");
  3. let util = require("../../../utils/util.js");
  4. Page({
  5. data: {
  6. money: "",
  7. topBg: "/statics/img/cash_bg.png",
  8. loading: false // 全局loading
  9. },
  10. onLoad: function (options) {
  11. // Do some initialize when page load.
  12. let that = this;
  13. that.setData({
  14. topBg: "data:image/png;base64," + wx.getFileSystemManager().readFileSync(that.data.topBg, "base64")
  15. })
  16. },
  17. onShow: function () {
  18. // Do something when page show.
  19. let that = this;
  20. // 店铺
  21. wx.getStorage({
  22. key: "store",
  23. success: function(res){
  24. that.setData({
  25. store: JSON.parse(res.data)
  26. })
  27. }
  28. });
  29. that.getUserInfo();
  30. that.getRechargeCard();
  31. },
  32. onReady: function () {
  33. // Do something when page ready.
  34. },
  35. onHide: function () {
  36. // Do something when page hide.
  37. },
  38. onUnload: function () {
  39. // Do something when page close.
  40. },
  41. onPullDownRefresh: function () {
  42. // Do something when pull down.
  43. },
  44. onReachBottom: function () {
  45. // Do something when page reach bottom.
  46. },
  47. onPageScroll: function () {
  48. // Do something when page scroll
  49. },
  50. onResize: function () {
  51. // Do something when page resize
  52. },
  53. // 用户信息
  54. getUserInfo(){
  55. let that = this;
  56. myPro.wxRequest("user/v2/userinfo","GET",{}).then(res=>{
  57. that.setData({
  58. money: res.result.money
  59. });
  60. // 同步会员信息 is_member
  61. getApp().globalData.is_member = res.result.is_member;
  62. }).catch(err=>{
  63. console.log('报错信息',err);
  64. wx.showToast({
  65. title: err,
  66. icon: 'none'
  67. });
  68. });
  69. },
  70. // 获取充值卡
  71. getRechargeCard(){
  72. let that = this;
  73. let params = {
  74. type: 2,
  75. card_type: 1 // 1普通 2年卡(会员卡)
  76. };
  77. myPro.wxRequest("user/v2/rechargecardlist","GET",params).then(res=>{
  78. let list = res.result;
  79. for(let i in list){
  80. list[i].checked = false;
  81. // 卡+券的总金额 (都按代金券走,折扣券不考虑)
  82. if(list[i].coupon_rule){
  83. list[i].priceAddCoupons = ((list[i].coupon_rule.coupon.price * list[i].coupon_rule.num) + parseFloat(list[i].price)).toFixed(2)
  84. }else{
  85. list[i].priceAddCoupons = list[i].price
  86. }
  87. };
  88. that.setData({
  89. cardList: list
  90. });
  91. }).catch(err=>{
  92. console.log('报错信息',err);
  93. wx.showToast({
  94. title: err,
  95. icon: "none"
  96. })
  97. })
  98. },
  99. // 选择充值卡
  100. clickCard(event){
  101. let that = this;
  102. let id = event.currentTarget.dataset.id;
  103. let list = that.data.cardList;
  104. for(let i in list){
  105. if(list[i].id == id){
  106. list[i].checked = !list[i].checked
  107. if(list[i].checked){
  108. that.nextFun(list[i].id)
  109. };
  110. }else{
  111. list[i].checked = false
  112. }
  113. };
  114. that.setData({
  115. cardList: list
  116. });
  117. },
  118. // 充值
  119. nextFun(id){
  120. let that = this;
  121. let params = {
  122. recharge_card_id: id,
  123. store_id: that.data.store ? that.data.store.id : 1 // 门店id
  124. };
  125. myPro.wxRequest("user/recharge","POST",params).then(res=>{
  126. let config = res.result.config;
  127. that.wxPayCard(JSON.parse(config));
  128. }).catch(err=>{
  129. console.log('报错信息',err);
  130. wx.showToast({
  131. title: err,
  132. icon: "none"
  133. })
  134. })
  135. },
  136. // 微信付款
  137. wxPayCard: function(result) {
  138. let that = this;
  139. wx.requestPayment({
  140. timeStamp: result.timeStamp,
  141. nonceStr: result.nonceStr,
  142. package: result.package,
  143. signType: result.signType,
  144. paySign: result.paySign,
  145. success(res) {
  146. wx.showToast({
  147. title: "充值成功",
  148. icon: "none"
  149. });
  150. // 拉取余额和会员角色
  151. that.getUserInfo();
  152. },
  153. fail(res) {
  154. wx.showToast({
  155. title: "充值失败",
  156. icon: "none"
  157. });
  158. console.log("报错信息",res)
  159. }
  160. });
  161. },
  162. // 充值记录
  163. goCashLog(){
  164. let that = this;
  165. wx.navigateTo({
  166. url: "/pages/user/cashlog/index"
  167. })
  168. }
  169. });