App.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <script>
  2. import { eventBus } from '@/utils/event-bus'
  3. import { isPolicyConsented, requestShowPrivacyPopup } from '@/utils/policy-consent-manager.js'
  4. export default {
  5. globalData: {
  6. // 胶囊距上距离
  7. menuTop: 0,
  8. // 导航栏高度
  9. navBarHeight: 0,
  10. // 胶囊距右方间距(方保持左、右间距一致)
  11. menuRight: 0,
  12. // 胶囊距底部间距(保持底部间距一致)
  13. menuBotton: 0,
  14. // 胶囊高度(自定义内容可与胶囊高度保证一致)
  15. menuHeight: 0,
  16. // 状态栏高度
  17. statusBarHeight: 0,
  18. // 安全距离
  19. safeAreaHeight: 0,
  20. // 胶囊宽度
  21. menuWidth: 0,
  22. // 窗口宽度
  23. windowWidth: 0,
  24. // 窗口宽度高度
  25. windowHeight: 0,
  26. // 常规子页面可操作区域高度
  27. pageContentHeight: 0,
  28. // 是否是冷启动
  29. isColdLaunch: false,
  30. needPolicyConsent: false,
  31. needPrivacyPopup: false,
  32. pendingLaunchOptions: null,
  33. },
  34. onLaunch(options) {
  35. this.globalData.isColdLaunch = true;
  36. console.log('冷启动onLaunch:', this.globalData.isColdLaunch)
  37. this.initPrivacyAuthorization()
  38. this.handleLaunchWithPolicy(options)
  39. },
  40. onShow(options) {
  41. this.$updateCartBadge();
  42. this.handleAppShowOptions(options);
  43. },
  44. onUnload() {
  45. uni.removeStorageSync('loginType')
  46. },
  47. methods: {
  48. initPrivacyAuthorization() {
  49. // #ifdef MP-WEIXIN
  50. if (typeof wx !== 'undefined' && wx.onNeedPrivacyAuthorization) {
  51. wx.onNeedPrivacyAuthorization((resolve) => {
  52. requestShowPrivacyPopup(resolve)
  53. })
  54. }
  55. // #endif
  56. },
  57. handleLaunchWithPolicy(options = {}) {
  58. this.globalData.pendingLaunchOptions = options
  59. if (!isPolicyConsented()) {
  60. this.globalData.needPolicyConsent = true
  61. return
  62. }
  63. this.runPendingLaunchLogin()
  64. },
  65. runPendingLaunchLogin() {
  66. const options = this.globalData.pendingLaunchOptions || {}
  67. let params = {};
  68. if (options.query) {
  69. if (options.query.scene) {
  70. const decodeScene = decodeURIComponent(options.query.scene);
  71. const paramPairs = decodeScene.split('&');
  72. paramPairs.forEach((pair) => {
  73. const [key, value] = pair.split('=');
  74. params[key] = value;
  75. });
  76. let keys = Object.keys(params)
  77. if (keys.length > 0) {
  78. console.log(params, "params");
  79. if (params.upsellCode) {
  80. uni.setStorageSync('upsellCode', params.upsellCode)
  81. }
  82. if (params.reduceCode) {
  83. uni.setStorageSync('reduceCode', params.reduceCode)
  84. }
  85. if (params.isbn) {
  86. uni.setStorageSync('scene_isbn', params.isbn);
  87. }
  88. if (params.bianhao) {
  89. uni.setStorageSync('scene_bianhao', params.bianhao);
  90. }
  91. this.slientLogin(params);
  92. } else {
  93. this.slientLogin();
  94. }
  95. } else {
  96. this.slientLogin(options.query);
  97. if (options.query.upsellCode) {
  98. uni.setStorageSync('upsellCode', options.query.upsellCode)
  99. }
  100. if (options.query.reduceCode) {
  101. uni.setStorageSync('reduceCode', options.query.reduceCode)
  102. }
  103. }
  104. } else {
  105. this.slientLogin()
  106. }
  107. },
  108. parseEntryOptions(options = {}) {
  109. let params = {};
  110. if (!options || !options.query) return params;
  111. if (options.query.scene) {
  112. const decodeScene = decodeURIComponent(options.query.scene);
  113. const paramPairs = decodeScene.split('&');
  114. paramPairs.forEach((pair) => {
  115. const [key, value] = pair.split('=');
  116. params[key] = value;
  117. });
  118. return params;
  119. }
  120. return options.query || {};
  121. },
  122. cacheEntryParams(params = {}) {
  123. if (params.upsellCode) {
  124. uni.setStorageSync('upsellCode', params.upsellCode)
  125. }
  126. if (params.reduceCode) {
  127. uni.setStorageSync('reduceCode', params.reduceCode)
  128. }
  129. if (params.isbn) {
  130. uni.setStorageSync('scene_isbn', params.isbn);
  131. }
  132. if (params.bianhao) {
  133. uni.setStorageSync('scene_bianhao', params.bianhao);
  134. }
  135. },
  136. handleAppShowOptions(options = {}) {
  137. const params = this.parseEntryOptions(options);
  138. if (!Object.keys(params).length) return;
  139. this.cacheEntryParams(params);
  140. if (this.globalData.isColdLaunch) return;
  141. if (params.upsellCode || params.reduceCode) {
  142. eventBus.emit('appShowActivity', { params });
  143. }
  144. },
  145. slientLogin(params = {}) {
  146. let that = this;
  147. uni.login({
  148. success(res) {
  149. //plat integer($int32)平台 1-微信 2-支付宝
  150. let platform = uni.getSystemInfoSync().uniPlatform
  151. let data = { code: res.code, plat: platform === 'mp-alipay' ? 2 : 1 }
  152. data = { ...data, ...params }
  153. uni.setStorageSync('loginType', data)
  154. console.log('登录参数:', data)
  155. uni.$u.http
  156. .post("/user/miniLogin", data)
  157. .then((response) => {
  158. if (response.code == 200) {
  159. uni.setStorageSync("loginSuccess", response.data);
  160. uni.setStorageSync("token", response.data.token);
  161. console.log("登录成功返回参数", response.data, data);
  162. eventBus.emit('loginSuccess', { response: response.data, params: data })
  163. that.globalData.isColdLaunch = false;
  164. that.$updateCartBadge();
  165. }
  166. });
  167. },
  168. fail: (err) => {
  169. console.log(err, "wx.login登录失败");
  170. },
  171. });
  172. },
  173. },
  174. };
  175. </script>
  176. <style lang="scss">
  177. // ===
  178. // === 注意:此处导入的css,会作用于全部.vue文件,请适量导入
  179. // ===
  180. body {
  181. font-family: PingFang-SC-Regular, PingFang-SC;
  182. }
  183. page {
  184. background-color: $app-theme-bg-gray-deep-color;
  185. }
  186. @import "uview-ui/index.scss";
  187. @import "./static/css/common.scss";
  188. /* 解决小程序和app滚动条的问题 */
  189. /* #ifdef MP-WEIXIN || APP-PLUS */
  190. ::-webkit-scrollbar {
  191. display: none;
  192. width: 0 !important;
  193. height: 0 !important;
  194. -webkit-appearance: none;
  195. background: transparent;
  196. color: transparent;
  197. }
  198. /* #endif */
  199. /* 解决H5 的问题 */
  200. /* #ifdef H5 */
  201. uni-scroll-view .uni-scroll-view::-webkit-scrollbar {
  202. /* 隐藏滚动条,但依旧具备可以滚动的功能 */
  203. display: none;
  204. width: 0 !important;
  205. height: 0 !important;
  206. -webkit-appearance: none;
  207. background: transparent;
  208. color: transparent;
  209. }
  210. /* #endif */
  211. .shu-elip-1 {
  212. overflow: hidden;
  213. text-overflow: ellipsis;
  214. white-space: nowrap;
  215. }
  216. .shu-elip-2 {
  217. display: -webkit-box;
  218. overflow: hidden;
  219. text-overflow: ellipsis;
  220. word-wrap: break-word;
  221. white-space: normal !important;
  222. -webkit-line-clamp: 2;
  223. -webkit-box-orient: vertical;
  224. }
  225. // 导出 scss 变量用于在 script 下使用
  226. .price_color {
  227. color: $app-theme-text-money-color;
  228. }
  229. .color_blue {
  230. color: $app-theme-blue;
  231. }
  232. .btnGroup {
  233. display: flex;
  234. align-items: center;
  235. .btn {
  236. margin: 0 10rpx;
  237. }
  238. }
  239. .mallbtn {
  240. min-width: 200rpx;
  241. line-height: 66rpx;
  242. padding: 0 20rpx;
  243. border-radius: 36rpx;
  244. color: #ffffff;
  245. margin-right: 20rpx;
  246. text-align: center;
  247. flex: 1;
  248. padding: 0 30rpx;
  249. }
  250. .soldOutBtn {
  251. @extend .mallbtn;
  252. background-color: $app-theme-nobuy-bg-color;
  253. }
  254. .joinCartBtn {
  255. @extend .mallbtn;
  256. background-color: $app-theme-joincart-bg-color;
  257. }
  258. .buyBtn {
  259. @extend .mallbtn;
  260. background-color: $app-theme-buybtn-bg-color;
  261. }
  262. </style>