payList.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { getPayInfo, getWalletInfo, createOrder, getCode } from './common/user';
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. pay_list: [],
  8. total_coin: 0,
  9. rectHeight: wx.getMenuButtonBoundingClientRect().height,
  10. appid: '',
  11. channel_name: "",
  12. session_key: "",
  13. source_name: "",
  14. version: "",
  15. openid: '',
  16. backAppid: '',
  17. backUrl: '',
  18. video_id: ''
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. onLoad: function (options) {
  24. var obj = wx.getLaunchOptionsSync()
  25. console.log(this.data, 'data', obj, obj.query.session_key);
  26. if (obj.query.appid) {
  27. this.setData({
  28. channel_name: obj.query.channel_name,
  29. session_key: obj.query.session_key,
  30. source_name: obj.query.source_name,
  31. version: obj.query.version,
  32. backAppid: obj.query.appid,
  33. backUrl: obj.query.url,
  34. video_id: obj.query.video_id ? obj.query.video_id : '',
  35. appid: wx.getAccountInfoSync().miniProgram.appId
  36. })
  37. this.toLogin()
  38. }
  39. },
  40. toLogin () {
  41. let that = this
  42. wx.login({
  43. success: function (res) {
  44. let _params = {
  45. appid: that.data.appid,
  46. channel_name: that.data.channel_name,
  47. session_key: that.data.session_key,
  48. source_name: that.data.source_name,
  49. version: that.data.version,
  50. }
  51. let params = {
  52. code: res.code,
  53. ..._params
  54. }
  55. console.log(params, 'params');
  56. if (res.code) {
  57. getCode(params).then(item => {
  58. console.log(item, 'login', params);
  59. that.setData({
  60. openid: item.data.data.open_id
  61. })
  62. that.getWalletData()
  63. that.loadWalletInfo()
  64. console.log(item.data.data.open_id, 'userinfo');
  65. })
  66. } else {
  67. }
  68. }
  69. })
  70. },
  71. //加载充值列表
  72. getWalletData () {
  73. let params = {
  74. appid: this.data.appid,
  75. channel_name: this.data.channel_name,
  76. session_key: this.data.session_key,
  77. source_name: this.data.source_name,
  78. version: this.data.version,
  79. }
  80. getPayInfo(params).then(res => {
  81. this.setData({
  82. pay_list: res.data.data
  83. })
  84. })
  85. },
  86. //加载钱包余额
  87. loadWalletInfo () {
  88. let params = {
  89. appid: this.data.appid,
  90. channel_name: this.data.channel_name,
  91. session_key: this.data.session_key,
  92. source_name: this.data.source_name,
  93. version: this.data.version,
  94. }
  95. getWalletInfo(params).then(res => {
  96. if (res.data.code == 200) {
  97. this.setData({
  98. total_coin: res.data.data.total_coin
  99. })
  100. }
  101. });
  102. },
  103. back () {
  104. let that = this
  105. console.log('1111', that.data.backAppid, that.data.backUrl);
  106. wx.navigateToMiniProgram({
  107. appId: that.data.backAppid,
  108. path: `${that.data.backUrl}?video_id=${that.data.video_id}`,
  109. extraData: {
  110. foo: 'bar'
  111. },
  112. envVersion: 'release',
  113. success (res) {
  114. // 打开成功
  115. }
  116. })
  117. return
  118. },
  119. //创建支付订单
  120. toOrder (e) {
  121. let item = e.currentTarget.dataset.item
  122. let params = {
  123. appid: this.data.appid,
  124. channel_name: this.data.channel_name,
  125. session_key: this.data.session_key,
  126. source_name: this.data.source_name,
  127. version: this.data.version,
  128. }
  129. let that = this
  130. createOrder({ pay_info_id: item.id, ...params, open_id: this.data.openid }).then(res => {
  131. console.log(res.data.code, 'creat');
  132. if (res.data.code == 200) {
  133. wx.requestPayment({
  134. timeStamp: res.data.data.timeStamp,
  135. nonceStr: res.data.data.nonceStr,
  136. package: res.data.data.package,
  137. signType: res.data.data.signType,
  138. paySign: res.data.data.paySign,
  139. success: function (res) {
  140. console.log('success');
  141. that.loadWalletInfo()
  142. that.back()
  143. },
  144. fail: function (res) { }
  145. })
  146. }
  147. })
  148. },
  149. // 监听页面隐藏
  150. onHide () {
  151. },
  152. /**
  153. * 生命周期函数--监听页面初次渲染完成
  154. */
  155. onReady: function () { },
  156. /**
  157. * 生命周期函数--监听页面显示
  158. */
  159. onShow: function () {
  160. },
  161. })