track-message.wpy 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <style lang="less">
  2. </style>
  3. <template>
  4. <div class="component-follow-btn" v-if="tracked !== null">
  5. <div v-if="tracked">
  6. <slot name="tracked-content"></slot>
  7. </div>
  8. <div v-else v-on:click="toTrack">
  9. <slot name="un-tracked-content"></slot>
  10. </div>
  11. </div>
  12. </template>
  13. <script>
  14. import wepy from '@wepy/core';
  15. import store from '@/store';
  16. import { mapState } from '@wepy/x';
  17. import { trackMessages } from '@/service/subscribeMessage';
  18. import { trackOrder, checkOrderSubscribed } from '@/api/subscribe';
  19. wepy.component({
  20. store,
  21. mame: 'FollowBtn',
  22. data: {
  23. tracked: null,
  24. linkUrl: ''
  25. },
  26. options: {
  27. multipleSlots: true
  28. },
  29. props: {
  30. templateId: String, //该订阅消息模版,
  31. businessId: String, //事务id
  32. businessType: String //业务类类型 1身份认证 ,2发票
  33. },
  34. created() {
  35. if (this.token) {
  36. this.getData();
  37. }
  38. },
  39. computed: {
  40. ...mapState({ token: state => state.user.token })
  41. },
  42. watch: {
  43. token(curVal, oldVal) {
  44. if (curVal && !oldVal) {
  45. this.getData();
  46. }
  47. }
  48. },
  49. methods: {
  50. getData() {
  51. // 判断当前id 是否跟踪过
  52. checkOrderSubscribed({
  53. businessId: this.businessId,
  54. msgSubscriptionType: Number(this.businessType) //业务类类型 1身份认证 ,2发票
  55. }).then(res => {
  56. let result = res.data;
  57. this.tracked = result;
  58. });
  59. },
  60. toTrack() {
  61. trackMessages(this.templateId).then(res => {
  62. // 如果返回的是订阅消息拒绝过,就不再订阅
  63. if (res && res.reject) {
  64. return;
  65. }
  66. setTimeout(()=> {
  67. //发请求跟踪消息
  68. trackOrder({
  69. businessId: this.businessId,
  70. msgSubscriptionType: Number(this.businessType) //业务类类型 1身份认证 ,2发票
  71. }).then(() => {
  72. this.tracked = true;
  73. });
  74. }, 3e3);
  75. });
  76. }
  77. }
  78. });
  79. </script>