activity-host.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <view>
  3. <UpsellShare v-if="enableUpsell" ref="shareRef" @viewRules="emitViewRules" />
  4. <ReduceShare v-if="enableReduce" ref="reduceShareRef" @viewRules="emitViewRules" />
  5. </view>
  6. </template>
  7. <script>
  8. import UpsellShare from "@/pages/home/components/upsell-share.vue";
  9. import ReduceShare from "@/pages/home/components/reduce-share.vue";
  10. import { eventBus } from "@/utils/event-bus";
  11. export default {
  12. name: "ActivityHost",
  13. props: {
  14. enableUpsell: {
  15. type: Boolean,
  16. default: true,
  17. },
  18. enableReduce: {
  19. type: Boolean,
  20. default: true,
  21. },
  22. },
  23. components: {
  24. UpsellShare,
  25. ReduceShare,
  26. },
  27. data() {
  28. return {
  29. loginSuccessHandler: null,
  30. appShowActivityHandler: null,
  31. };
  32. },
  33. mounted() {
  34. this.bindLoginSuccess();
  35. this.bindAppShowActivity();
  36. this.tryOpenPendingActivity();
  37. },
  38. beforeDestroy() {
  39. this.unbindLoginSuccess();
  40. this.unbindAppShowActivity();
  41. },
  42. methods: {
  43. bindLoginSuccess() {
  44. if (this.loginSuccessHandler) return;
  45. this.loginSuccessHandler = (data) => {
  46. this.openByLoginSuccess(data);
  47. const app = getApp();
  48. if (app && app.globalData) {
  49. app.globalData.isColdLaunch = false;
  50. }
  51. };
  52. eventBus.on("loginSuccess", this.loginSuccessHandler);
  53. },
  54. bindAppShowActivity() {
  55. if (this.appShowActivityHandler) return;
  56. this.appShowActivityHandler = (data) => {
  57. this.openByLoginSuccess(data);
  58. };
  59. eventBus.on("appShowActivity", this.appShowActivityHandler);
  60. },
  61. unbindLoginSuccess() {
  62. if (!this.loginSuccessHandler) return;
  63. eventBus.off("loginSuccess", this.loginSuccessHandler);
  64. this.loginSuccessHandler = null;
  65. },
  66. unbindAppShowActivity() {
  67. if (!this.appShowActivityHandler) return;
  68. eventBus.off("appShowActivity", this.appShowActivityHandler);
  69. this.appShowActivityHandler = null;
  70. },
  71. tryOpenPendingActivity() {
  72. const token = uni.getStorageSync("token");
  73. const upsellCode = uni.getStorageSync("upsellCode");
  74. const reduceCode = uni.getStorageSync("reduceCode");
  75. if (!token || (!upsellCode && !reduceCode)) return;
  76. this.openByLoginSuccess();
  77. },
  78. openByLoginSuccess(data) {
  79. const upsellCode = uni.getStorageSync("upsellCode") || data?.params?.upsellCode;
  80. const reduceCode = uni.getStorageSync("reduceCode") || data?.params?.reduceCode;
  81. if (this.enableUpsell && upsellCode) {
  82. this.$refs.shareRef?.open(upsellCode);
  83. }
  84. if (this.enableReduce && reduceCode) {
  85. this.$refs.reduceShareRef?.open(reduceCode);
  86. }
  87. this.$emit("refresh-order");
  88. },
  89. handlePageOptions(options = {}, isColdLaunch = false) {
  90. if (isColdLaunch) return;
  91. const params = this.parseEntryParams(options);
  92. if (this.enableUpsell && params.upsellCode) {
  93. this.$refs.shareRef?.open(params.upsellCode);
  94. this.$emit("refresh-order");
  95. return;
  96. }
  97. if (this.enableReduce && params.reduceCode) {
  98. this.$refs.reduceShareRef?.open(params.reduceCode);
  99. return;
  100. }
  101. this.$emit("refresh-order");
  102. },
  103. parseEntryParams(options = {}) {
  104. if (!options.scene) return options;
  105. const params = {};
  106. const decodeScene = decodeURIComponent(options.scene);
  107. const paramPairs = decodeScene.split("&");
  108. paramPairs.forEach((pair) => {
  109. const [key, value] = pair.split("=");
  110. params[key] = value;
  111. });
  112. return params;
  113. },
  114. emitViewRules() {
  115. this.$emit("view-rules");
  116. },
  117. getShareAppMessage(res) {
  118. if (this.enableUpsell && res.from === "button" && res.target && res.target.dataset && res.target.dataset.shareType === "upsell") {
  119. const upsellCode = uni.getStorageSync("upsellCodeShare");
  120. uni.$u.http.get("/token/order/goToShare?upsellCode=" + upsellCode);
  121. return {
  122. title: "书嗨",
  123. path: "/pages/home/index?upsellCode=" + upsellCode,
  124. imageUrl: "https://shuhi.oss-cn-qingdao.aliyuncs.com/mini/share.jpg",
  125. desc: "书嗨,专注于书籍交易的平台,提供新书和二手书的买卖服务",
  126. };
  127. }
  128. if (this.enableReduce && res.from === "button" && res.target && res.target.dataset && res.target.dataset.shareType === "reduce") {
  129. const reduceCode = uni.getStorageSync("reduceCodeShare");
  130. if (uni.$u && uni.$u.api && uni.$u.api.goToReduceShareAjax) {
  131. uni.$u.api.goToReduceShareAjax({ reduceCode: reduceCode });
  132. } else {
  133. uni.$u && uni.$u.http && uni.$u.http.get("/token/shop/order/goToShare", { reduceCode: reduceCode });
  134. }
  135. return {
  136. title: "快来帮我减钱买书吧!",
  137. path: "/pages/sell/index?reduceCode=" + reduceCode,
  138. imageUrl: "https://shuhi.oss-cn-qingdao.aliyuncs.com/mini/share.jpg",
  139. desc: "书嗨,专注于书籍交易的平台,提供新书和二手书的买卖服务",
  140. };
  141. }
  142. return null;
  143. },
  144. getShareTimeline(res) {
  145. if (this.enableUpsell && res.from === "button" && res.target && res.target.dataset && res.target.dataset.shareType === "upsell") {
  146. const upsellCode = uni.getStorageSync("upsellCodeShare");
  147. uni.$u.http.get("/token/order/goToShare?upsellCode=" + upsellCode);
  148. return {
  149. title: "书嗨",
  150. path: "/pages/home/index?upsellCode=" + upsellCode,
  151. imageUrl: "https://shuhi.oss-cn-qingdao.aliyuncs.com/mini/share.jpg",
  152. desc: "书嗨,专注于书籍交易的平台,提供新书和二手书的买卖服务",
  153. };
  154. }
  155. if (this.enableReduce && res.from === "button" && res.target && res.target.dataset && res.target.dataset.shareType === "reduce") {
  156. const reduceCode = uni.getStorageSync("reduceCodeShare");
  157. if (uni.$u && uni.$u.api && uni.$u.api.goToReduceShareAjax) {
  158. uni.$u.api.goToReduceShareAjax({ reduceCode: reduceCode });
  159. } else {
  160. uni.$u && uni.$u.http && uni.$u.http.get("/token/shop/order/goToShare", { reduceCode: reduceCode });
  161. }
  162. return {
  163. title: "快来帮我减钱买书吧!",
  164. path: "/pages/sell/index?reduceCode=" + reduceCode,
  165. imageUrl: "https://shuhi.oss-cn-qingdao.aliyuncs.com/mini/share.jpg",
  166. desc: "书嗨,专注于书籍交易的平台,提供新书和二手书的买卖服务",
  167. };
  168. }
  169. return null;
  170. },
  171. },
  172. };
  173. </script>