App.vue 6.9 KB

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