postage.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import recordSource from '../../utils/recordSource';
  2. const config = require('../../config.js')
  3. const getReq = config.getReq
  4. const buyVGoods = config.buyVGoods
  5. let app = getApp();
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. fGoodsRequesting:false,
  12. currentIndex: 0, //页签索引
  13. rechargeGearActiveIndex: 0, //挡位项目索引
  14. tips:'', //顶部tips
  15. inviter_tip:'',
  16. goods_inviter_tips: new Map(),
  17. goods:[], //挡位数据
  18. goods_id:'', //当前选中挡位的goods_id
  19. sinopec: { card_no: '', card_type: 'sinopec' },
  20. petrochina: { card_no: '', card_type: 'petrochina' },
  21. phone: { card_no: '', card_type: 'phone' },
  22. tabsData: [
  23. { id: 0, icon: '/image/postage/sinopec.png', title: '中国石化', tips: '选择油卡', card_type: 'sinopec', card_no: '' },
  24. { id: 1, icon: '/image/postage/petrochina.png', title: '中国石油', tips: '选择油卡', card_type: 'petrochina', card_no: '' }
  25. ],
  26. fromSource: ''
  27. },
  28. // 页签切换
  29. handlerClickTabItem(e) {
  30. var dataset = e.currentTarget.dataset
  31. const { activeindex, cardtype } = dataset
  32. console.log('handlerClickTabItem activeindex=', activeindex,'cart_type=', cardtype)
  33. this.setData({currentIndex: activeindex});
  34. },
  35. //充值档位切换
  36. handlerClickRechargeGear(e) {
  37. var dataset = e.currentTarget.dataset
  38. console.log('handlerClickRechargeGear dataset:', e.currentTarget.dataset)
  39. let goods_id = dataset.currentvalue
  40. this.setData({
  41. rechargeGearActiveIndex: dataset.activeindex,
  42. goods_id: goods_id,
  43. inviter_tip: this.data.goods_inviter_tips.get(goods_id)
  44. });
  45. },
  46. //跳转到油卡管理页面
  47. handlerAddCard(e){
  48. const { cardtype } = e.currentTarget.dataset
  49. app.navigateto(`/pages/postageManage/postageManage?card_type=${cardtype}`)
  50. },
  51. /**
  52. * 生命周期函数--监听页面加载
  53. */
  54. onLoad: function (options)
  55. {
  56. this.requestGoods();
  57. },
  58. requestGoods()
  59. {
  60. if(this.fGoodsRequesting) return;
  61. this.fGoodsRequesting = true
  62. let fromSource = recordSource(app, `act=index&op=card_goods`)
  63. this.setData({
  64. fromSource: app.globalData.fromSource,
  65. });
  66. getReq(app, {
  67. act: 'index',
  68. op: 'card_goods',
  69. page_type: 'oil',
  70. from: fromSource
  71. }, (res) => {
  72. wx.stopPullDownRefresh()
  73. if (res.code == 200) {
  74. if (res.datas && res.datas.goods) {
  75. const datas = res.datas
  76. let goods_inviter_tips = this.formGoodsInviterTips(res.datas.goods_inviter_tips)
  77. const { tips = '', goods = [], cards = [], } = datas
  78. let phone = app.getValueByKey('phone', cards)
  79. let petrochina = app.getValueByKey('petrochina', cards)
  80. let sinopec = app.getValueByKey('sinopec', cards)
  81. phone = app.isNullObject(phone) ? { card_no: '', card_type: 'phone' } : phone
  82. petrochina = app.isNullObject(petrochina) ? { card_no: '', card_type: 'petrochina' } : petrochina
  83. sinopec = app.isNullObject(sinopec) ? { card_no: '', card_type: 'sinopec' } : sinopec
  84. let goods_id = datas.goods[0].goods_id
  85. let inviter_tip = goods_inviter_tips.get(goods_id)
  86. this.setData({ tips, goods_inviter_tips, inviter_tip, goods, sinopec, petrochina, phone, goods_id });
  87. }
  88. }
  89. else {
  90. app.showToast(res.message)
  91. }
  92. this.fGoodsRequesting = false
  93. })
  94. },
  95. formGoodsInviterTips(godosInviterTips)
  96. {
  97. let ret = new Map();
  98. godosInviterTips.forEach(item => {
  99. let goods_id = item.goods_id
  100. let tip = item.tip
  101. ret.set(goods_id,tip)
  102. });
  103. return ret;
  104. },
  105. // 立即充值
  106. handlerRecharge()
  107. {
  108. console.log('handlerRecharge:',this.data)
  109. const { goods_id } = this.data
  110. let card_type = ''
  111. let card_no = ''
  112. if (this.data.currentIndex == 0) {
  113. card_type = 'sinopec'
  114. card_no = this.data.sinopec.card_no
  115. } else {
  116. card_type = 'petrochina'
  117. card_no = this.data.petrochina.card_no
  118. }
  119. if(!card_no){
  120. app.showToast('卡号不能为空')
  121. return
  122. }
  123. buyVGoods(app,goods_id,1,{ card_no,card_type }, (res) => {
  124. console.table({card_no,card_type})
  125. console.log('res:',res)
  126. let page = getCurrentPages().pop();
  127. page.onLoad()
  128. }, (err) => {
  129. wx.showToast({
  130. icon: 'none',
  131. title: '支付未完成,订单将在10分钟后取消...',
  132. duration: 5000
  133. })
  134. let page = getCurrentPages().pop();
  135. page.onLoad()
  136. })
  137. },
  138. /**
  139. * 生命周期函数--监听页面初次渲染完成
  140. */
  141. onReady: function () {
  142. },
  143. /**
  144. * 生命周期函数--监听页面显示
  145. */
  146. onShow: function ()
  147. {
  148. const record = app.globalData.record || {}
  149. const { card_no = '', card_type = '' } = record
  150. app.setFromSource(this.data.fromSource)
  151. console.log('onShow ')
  152. if (!app.isNullObject(record) && card_no && card_type)
  153. {
  154. if (card_type === 'sinopec'){
  155. this.setData({ sinopec: { card_no: card_no, card_type:'sinopec'} })
  156. } else if(card_type === 'petrochina') {
  157. this.setData({ petrochina: { card_no: card_no, card_type: 'petrochina' }})
  158. }
  159. console.log(this.sinopec, this.petrochina)
  160. }
  161. },
  162. /**
  163. * 生命周期函数--监听页面隐藏
  164. */
  165. onHide: function () {
  166. },
  167. /**
  168. * 生命周期函数--监听页面卸载
  169. */
  170. onUnload: function () {
  171. },
  172. /**
  173. * 页面相关事件处理函数--监听用户下拉动作
  174. */
  175. onPullDownRefresh: function () {
  176. this.requestGoods();
  177. },
  178. /**
  179. * 页面上拉触底事件的处理函数
  180. */
  181. onReachBottom: function () {
  182. },
  183. onShareAppMessage: function () {
  184. return app.cardShareInfo();
  185. }
  186. })