postage.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. this.setData({ tips, goods, cards, sinopec, petrochina, card_no: sinopec.card_no, goods_id: datas.goods[0].goods_id });
  78. }
  79. } else {
  80. app.showToast(res.message)
  81. }
  82. })
  83. },
  84. // 通过键取值
  85. getValueBykey(key = '', data = []){
  86. return data.filter(val => val.card_type === key)[0]
  87. },
  88. // 立即充值
  89. handlerRecharge(){
  90. const { goods_id, card_no, card_type } = this.data
  91. if(!card_no){
  92. app.showToast('卡号不能为空')
  93. return
  94. }
  95. buyVGoods(goods_id,1,{ card_no,card_type }, (res) => {
  96. console.table({card_no,card_type})
  97. // console.log('res',res) 成功回调
  98. }, (err) => {
  99. // 失败回调
  100. })
  101. },
  102. /**
  103. * 生命周期函数--监听页面初次渲染完成
  104. */
  105. onReady: function () {
  106. },
  107. /**
  108. * 生命周期函数--监听页面显示
  109. */
  110. onShow: function () {
  111. if(!this.data.firstLoad) {
  112. const record = app.globalData.record || {}
  113. const { card_no = '', card_type = '' } = record
  114. app.setFromSource(this.data.fromSource)
  115. if (record) {
  116. this.setData({ card_no, card_type })
  117. }
  118. }
  119. },
  120. /**
  121. * 生命周期函数--监听页面隐藏
  122. */
  123. onHide: function () {
  124. },
  125. /**
  126. * 生命周期函数--监听页面卸载
  127. */
  128. onUnload: function () {
  129. },
  130. /**
  131. * 页面相关事件处理函数--监听用户下拉动作
  132. */
  133. onPullDownRefresh: function () {
  134. },
  135. /**
  136. * 页面上拉触底事件的处理函数
  137. */
  138. onReachBottom: function () {
  139. }
  140. })