index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. Component({
  2. /**
  3. * 组件的属性列表
  4. */
  5. properties: {
  6. params: {
  7. type: Object,
  8. value: null
  9. },
  10. envVersion: {
  11. type: String,
  12. value: "release"
  13. }
  14. },
  15. /**
  16. * 组件的初始数据
  17. */
  18. data: {
  19. appid:'wx13e5a2a68ae503c0',
  20. showPayModal:false,
  21. chickOnPay: false //标记是否支付中状态
  22. },
  23. /**
  24. * 组件的方法列表
  25. */
  26. methods: {
  27. //设置支付点击了打开云小程序
  28. setPaying(paystatus) {
  29. this.setData({
  30. chickOnPay: paystatus
  31. })
  32. this.triggerEvent('Change',{chickOnPay:paystatus})
  33. },
  34. //[跳转到【收银台】小程序失败-用户点击了支付组件外的地方(灰色地方)
  35. onTapCancel () {
  36. this.triggerEvent('Fail',{navigateSuccess:false})
  37. this.triggerEvent('Complete')
  38. },
  39. //跳转到【收银台】小程序成功
  40. navigateSuccess () {
  41. this.setPaying(true)
  42. },
  43. /**
  44. * 跳转失败
  45. * 如果下单成功但是用户取消支付则可在
  46. * 调用组件用 res.detail.event 查看原因
  47. */
  48. navigateFail (event) {
  49. this.triggerEvent('Fail',{navigateSuccess: false,event:event})
  50. this.triggerEvent('Complete')
  51. }
  52. },
  53. /**
  54. * 组件生命周期
  55. */
  56. lifetimes: {
  57. attached() {
  58. this.setPaying(false)
  59. if (!this.data.params) {
  60. console.error('跳转到【收银台】小程序失败 - 没有传递跳转参数')
  61. this.triggerEvent('Fail',{error: true,navigateSuccess: false})
  62. this.triggerEvent('Complete')
  63. }
  64. //监听app.onShow事件
  65. wx.onAppShow(appOptions => {
  66. if (!this.data.chickOnPay){
  67. return;
  68. }
  69. this.setPaying(false)
  70. if (appOptions.scene === 1038 && appOptions.referrerInfo.appId === 'wx13e5a2a68ae503c0') {
  71. console.log('确认来源于【收银台】回调返回')
  72. let extraData = appOptions.referrerInfo.extraData
  73. if (extraData.return_code =='SUCCESS') {
  74. this.triggerEvent('Success',extraData)
  75. this.triggerEvent('Complete')
  76. } else {
  77. this.triggerEvent('Fail',{navigateSuccess:true,event:extraData})
  78. this.triggerEvent('Complete')
  79. }
  80. }
  81. })
  82. var params = this.data.params;
  83. //尝试打开
  84. wx.navigateToMiniProgram({
  85. appId: this.data.appid,
  86. path: 'pages/dopay/index',
  87. extraData: params,
  88. envVersion: this.data.envVersion,
  89. success:yes => {
  90. console.log('跳转到【收银台】小程序成功')
  91. this.setPaying(true)
  92. },
  93. fail:no => {
  94. console.log('跳转到【收银台】小程序失败-弹窗提醒')
  95. this.setData({
  96. showPayModal: true,
  97. appid: this.data.appid
  98. })
  99. },
  100. })
  101. }
  102. }
  103. })