add.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // pages/add/add.js
  2. const app = getApp()
  3. const cell = [0, 288, 100, 1000, 10000]
  4. var lock = require('../../utils/lock.js')
  5. var net = require('../../utils/net.js')
  6. var url = require('../../utils/url.js')
  7. var common = require('../../utils/common.js')
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. getPhoneCell: '200万',
  14. canPay: true,
  15. canNotPay: '',
  16. options: [],
  17. selected: '',
  18. contact: {
  19. sessionFrom: ''
  20. }
  21. },
  22. buyAction: function () {
  23. // app.log(this.data.selected)
  24. wx.showLoading({
  25. title: '正在打赏',
  26. })
  27. this.getOrder({
  28. url: url.host + url.recharge,
  29. data: {
  30. cellProductId: this.data.selected
  31. },
  32. success: res => {
  33. wx.hideLoading()
  34. // app.globalData.user.cell += cell[Number(this.data.selected)]
  35. var len = this.data.options.length || 0
  36. for (var i = 0; i < len; i++) {
  37. if (this.data.options[i].cellProductId == this.data.selected) {
  38. var title = '+' + common.unit(this.data.options[i].cell)
  39. if (this.data.options[i].type == 1) {
  40. this.data.options.splice(i, 1)
  41. this.data.options[0].selected = true
  42. var selected = this.data.options[0].cellProductId || 0
  43. this.setData({
  44. selected: selected,
  45. options: this.data.options
  46. })
  47. }
  48. break
  49. }
  50. }
  51. wx.showToast({
  52. duration: 2000,
  53. icon: 'none',
  54. title: title
  55. })
  56. }
  57. })
  58. },
  59. //获取订单接口
  60. getOrder: function (obj) {
  61. net.connect({
  62. url: obj.url,
  63. data: obj.data,
  64. method: 'POST',
  65. success: res => {
  66. this.order(obj, res.data.data)
  67. }
  68. })
  69. },
  70. //微信支付统一调用接口
  71. order: function (obj, order) {
  72. if (app.globalData.debug) {
  73. net.connect({
  74. url: url.host + url.testPay,
  75. data: { out_trade_no: order },
  76. success: res => {
  77. obj.success(res)
  78. }
  79. })
  80. return
  81. }
  82. net.connect({
  83. url: url.host + url.unified,
  84. data: { out_trade_no: order },
  85. method: 'POST',
  86. success: res => {
  87. this.pay(obj, order, res.data.data)
  88. }
  89. })
  90. },
  91. //微信支付
  92. pay: function (obj, no, order) {
  93. wx.requestPayment({
  94. timeStamp: order.timeStamp,
  95. nonceStr: order.nonceStr,
  96. package: order.package,
  97. signType: 'MD5',
  98. paySign: order.paySign,
  99. success: res => {
  100. obj.success(res)
  101. },
  102. fail: e => {
  103. wx.hideLoading()
  104. wx.showModal({
  105. title: '',
  106. content: '支付失败',
  107. })
  108. }
  109. })
  110. },
  111. selectAction: function (o) {
  112. app.log(o.currentTarget.id)
  113. var id = o.currentTarget.id
  114. var len = this.data.options.length || 0
  115. for (var i = 0; i < len; i++) {
  116. if (this.data.options[i].cellProductId == id) {
  117. var selected = id
  118. this.data.options[i].selected = true
  119. } else {
  120. this.data.options[i].selected = false
  121. }
  122. }
  123. this.setData({
  124. options: this.data.options,
  125. selected: selected
  126. })
  127. },
  128. /**
  129. * 生命周期函数--监听页面加载
  130. */
  131. onLoad: function (options) {
  132. app.log(app.globalData.user)
  133. var options = app.globalData.user.cellProductList
  134. var len = options.length || 0
  135. for (var i = 0; i < len; i++) {
  136. if (i == 0) {
  137. options[i].selected = true
  138. } else {
  139. options[i].selected = false
  140. }
  141. }
  142. var selected = options[0].cellProductId || 0
  143. if (app.server.config.cellBindPhone) {
  144. this.data.getPhoneCell = common.unit(app.server.config.cellBindPhone)
  145. }
  146. if (app.server.messageRuleMap.buy && app.server.messageRuleMap.buy.sessionFrom) {
  147. this.data.contact = common.messageRuleMapToObject(app.server.messageRuleMap.buy)
  148. }
  149. this.setData({
  150. contact: this.data.contact,
  151. getPhoneCell: this.data.getPhoneCell,
  152. canPay: !app.globalData.isIOS,
  153. canNotPay: app.server.config.noSupportPayMsg,
  154. options: options,
  155. selected: selected
  156. })
  157. },
  158. /**
  159. * 生命周期函数--监听页面初次渲染完成
  160. */
  161. onReady: function () {
  162. },
  163. /**
  164. * 生命周期函数--监听页面显示
  165. */
  166. onShow: function () {
  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. })