App.vue 6.8 KB

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