buyVip.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // pages/buyVip/buyVip.js
  2. var http = require('../../../utils/http.js');
  3. var config = require('../../../utils/config.js');
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. duration: 1000,
  10. userInfo: {},
  11. userLevelInfo: {}, // 当前会员信息
  12. premiumVipList: [], // 付费会员等级列表
  13. currentLevelId: 0,
  14. selectPremiumVip: {},
  15. },
  16. /**
  17. * 生命周期函数--监听页面加载
  18. */
  19. onLoad: function(options) {
  20. // 获取会员信息
  21. this.getUserLevelInfo()
  22. // 获取年费会员列表
  23. this.getPayMemberList()
  24. this.setData({
  25. userInfo: wx.getStorageSync('loginResult')
  26. })
  27. },
  28. /**
  29. * 生命周期函数--监听页面初次渲染完成
  30. */
  31. onReady: function() {
  32. this.setData({
  33. })
  34. },
  35. /**
  36. * 生命周期函数--监听页面显示
  37. */
  38. onShow: function() {
  39. },
  40. /**
  41. * 获取当前会员信息
  42. */
  43. getUserLevelInfo() {
  44. var params = {
  45. url: '/p/score/scoreLevel/page',
  46. method: 'GET',
  47. data: {
  48. levelType: 0
  49. },
  50. callBack: res => {
  51. this.setData({
  52. userLevelInfo: res,
  53. currentLevelId: res.userLevel.id,
  54. currentGrowth: res.growth
  55. })
  56. }
  57. }
  58. http.request(params)
  59. },
  60. /**
  61. * 获取年费会员列表
  62. */
  63. getPayMemberList() {
  64. var params = {
  65. url: '/p/score/scoreLevel/page',
  66. method: 'GET',
  67. data: {
  68. levelType: 1
  69. },
  70. callBack: res => {
  71. this.setData({
  72. premiumVipList: res.userLevels,
  73. selectPremiumVip: res.userLevels[0],
  74. })
  75. }
  76. }
  77. http.request(params)
  78. },
  79. /**
  80. * 立即购买- 根据当前付费会员id
  81. */
  82. buyNow() {
  83. if (this.data.currentGrowth < this.data.selectPremiumVip.needGrowth){
  84. return wx.showToast({
  85. title: '成长值不足,无法购买',
  86. icon: 'none'
  87. })
  88. }
  89. if (this.data.userLevelInfo.levelType != 0 && this.data.userLevelInfo.userLevel.level < this.data.selectPremiumVip.level) {
  90. // 已有付费会员时购买付费会员
  91. wx.showModal({
  92. title: '提示',
  93. content: `您当前为${this.data.userLevelInfo.userLevel.levelName},即将购买${this.data.selectPremiumVip.levelName},升级会员将覆盖当前会员时限,是否确认购买?`,
  94. success: res => {
  95. if (res.confirm) {
  96. this.buyVip()
  97. } else {
  98. }
  99. }
  100. })
  101. return
  102. }
  103. this.buyVip()
  104. },
  105. /**
  106. * 购买会员
  107. */
  108. buyVip(){
  109. var ths = this
  110. var params = {
  111. url: '/p/level/payLevel',
  112. method: 'POST',
  113. data: {
  114. id: this.data.selectPremiumVip.id,
  115. scene: '',
  116. payType: 1, //支付方式 (1:微信小程序支付 2:支付宝 3微信扫码支付 4 微信h5支付)
  117. },
  118. callBack: res => {
  119. // 微信支付返回参数
  120. wx.requestPayment({
  121. timeStamp: res.timeStamp,
  122. nonceStr: res.nonceStr,
  123. package: res.packageValue,
  124. signType: res.signType,
  125. paySign: res.paySign,
  126. appId: res.appId,
  127. success: e => {
  128. console.log("支付成功");
  129. wx.showModal({
  130. title: '提示',
  131. content: '购买成功',
  132. showCancel: false,
  133. success(res) {
  134. if (res.confirm) {
  135. console.log('用户点击确定')
  136. ths.getUserLevelInfo()
  137. }
  138. }
  139. })
  140. },
  141. fail: err => {
  142. console.log("支付失败");
  143. wx.showModal({
  144. title: '提示',
  145. content: '支付失败,请重新支付',
  146. showCancel: false,
  147. success(res) {
  148. if (res.confirm) {
  149. console.log('用户点击确定')
  150. ths.getUserLevelInfo()
  151. }
  152. }
  153. })
  154. }
  155. })
  156. }
  157. }
  158. http.request(params)
  159. },
  160. /**
  161. * 切换轮播图
  162. */
  163. selectVip(e) {
  164. this.setData({
  165. selectPremiumVip: this.data.premiumVipList[e.detail.current]
  166. })
  167. },
  168. /**
  169. * 生命周期函数--监听页面隐藏
  170. */
  171. onHide: function() {
  172. },
  173. /**
  174. * 生命周期函数--监听页面卸载
  175. */
  176. onUnload: function() {
  177. },
  178. /**
  179. * 页面相关事件处理函数--监听用户下拉动作
  180. */
  181. onPullDownRefresh: function() {
  182. },
  183. /**
  184. * 页面上拉触底事件的处理函数
  185. */
  186. onReachBottom: function() {
  187. },
  188. /**
  189. * 用户点击右上角分享
  190. */
  191. onShareAppMessage: function() {
  192. }
  193. })