postage.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. currentIndex: 0, //页签索引
  12. rechargeGearActiveIndex: 0, //挡位项目索引
  13. tips:'', //顶部tips
  14. goods:[], //挡位数据
  15. goods_id:'', //当前选中挡位的goods_id
  16. card_type: 'sinopec', //油卡类型 (中石化/中石油)
  17. card_no: '', //油卡号
  18. cards: [],
  19. sinopec: {},
  20. petrochina: {},
  21. firstLoad: true,
  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. const { cards } = this.data
  33. this.setData({
  34. currentIndex: dataset.activeindex,
  35. card_no: this.getValueBykey(cardtype ,cards).card_no
  36. });
  37. },
  38. //充值档位切换
  39. handlerClickRechargeGear(e) {
  40. var dataset = e.currentTarget.dataset
  41. this.setData({
  42. rechargeGearActiveIndex: dataset.activeindex,
  43. goods_id: dataset.currentvalue
  44. });
  45. },
  46. //跳转到油卡管理页面
  47. handlerAddCard(e){
  48. const { cardtype } = e.currentTarget.dataset
  49. wx.navigateTo({
  50. url: `/pages/postageManage/postageManage?card_type=${cardtype}`,
  51. })
  52. },
  53. /**
  54. * 生命周期函数--监听页面加载
  55. */
  56. onLoad: function (options) {
  57. let fromSource = recordSource(app, `act=index&op=card_goods`)
  58. this.setData({
  59. fromSource: app.globalData.fromSource,
  60. // card_no: this.data.tabsData[0].card_no
  61. });
  62. getReq({
  63. act: 'index',
  64. op: 'card_goods',
  65. page_type: 'oil',
  66. from: fromSource
  67. }, (res) => {
  68. this.setData({
  69. firstLoad: false
  70. });
  71. if (res.code == 200) {
  72. if(res.datas && res.datas.goods){
  73. const datas = res.datas
  74. const { tips = '', goods = [], cards = [], } = datas || {}
  75. const sinopec = this.getValueBykey('sinopec', cards)
  76. const petrochina = this.getValueBykey('petrochina', cards)
  77. console.log('sinopec',sinopec)
  78. console.log('petrochina',petrochina)
  79. this.setData({ tips, goods, cards, sinopec, petrochina, card_no: sinopec.card_no, goods_id: datas.goods[0].goods_id });
  80. }
  81. } else {
  82. app.showToast(res.message)
  83. }
  84. })
  85. },
  86. // 通过键取值
  87. getValueBykey(key = '', data = []){
  88. if(!data.length){
  89. return {}
  90. }
  91. return data.filter(val => val.card_type === key)[0]
  92. },
  93. // 立即充值
  94. handlerRecharge(){
  95. const { goods_id, card_no, card_type } = this.data
  96. if(!card_no){
  97. app.showToast('卡号不能为空')
  98. return
  99. }
  100. buyVGoods(goods_id,1,{ card_no,card_type }, (res) => {
  101. console.table({card_no,card_type})
  102. // console.log('res',res) 成功回调
  103. }, (err) => {
  104. // 失败回调
  105. })
  106. },
  107. /**
  108. * 生命周期函数--监听页面初次渲染完成
  109. */
  110. onReady: function () {
  111. },
  112. /**
  113. * 生命周期函数--监听页面显示
  114. */
  115. onShow: function () {
  116. if(!this.data.firstLoad) {
  117. const record = app.globalData.record || {}
  118. const { card_no = '', card_type = '' } = record
  119. app.setFromSource(this.data.fromSource)
  120. if (record) {
  121. this.setData({ card_no, card_type })
  122. }
  123. }
  124. },
  125. /**
  126. * 生命周期函数--监听页面隐藏
  127. */
  128. onHide: function () {
  129. },
  130. /**
  131. * 生命周期函数--监听页面卸载
  132. */
  133. onUnload: function () {
  134. },
  135. /**
  136. * 页面相关事件处理函数--监听用户下拉动作
  137. */
  138. onPullDownRefresh: function () {
  139. },
  140. /**
  141. * 页面上拉触底事件的处理函数
  142. */
  143. onReachBottom: function () {
  144. }
  145. })