component-sanzhizhou 1.2.10 → 1.2.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -20,4 +20,5 @@ export { default as PayQr } from './pay/qr';
20
20
  export { default as PayChannel } from './pay/pay-channel';
21
21
  export { default as IdTag } from './UI/id-tag';
22
22
  export { default as CollectConfigPrice } from './collect-price';
23
- export { default as ProductDetailLink, getProductDetailUrl } from './lib/product-detail-link';
23
+ export { CommercePlatform, default as ProductDetailLink, getProductDetailUrl, } from './lib/product-detail-link';
24
+ export type { ProductDetailLinkProps, ProductDetailUrlParams, } from './lib/product-detail-link';
package/dist/index.js CHANGED
@@ -20,4 +20,4 @@ export { default as PayQr } from "./pay/qr";
20
20
  export { default as PayChannel } from "./pay/pay-channel";
21
21
  export { default as IdTag } from "./UI/id-tag";
22
22
  export { default as CollectConfigPrice } from "./collect-price";
23
- export { default as ProductDetailLink, getProductDetailUrl } from "./lib/product-detail-link";
23
+ export { CommercePlatform, default as ProductDetailLink, getProductDetailUrl } from "./lib/product-detail-link";
@@ -1,13 +1,29 @@
1
1
  import React from 'react';
2
- interface Props {
3
- commercePlatformId?: number;
4
- skuId?: number;
5
- children?: React.ReactNode;
6
- className?: string;
7
- linkTitle?: string;
2
+ /**
3
+ * 电商平台枚举,取值与后端的 commercePlatformId 保持一致。
4
+ */
5
+ export declare enum CommercePlatform {
6
+ /** OZON */
7
+ OZON = 7,
8
+ /** Wildberries */
9
+ Wildberries = 15
10
+ }
11
+ /** 生成商品详情链接所需的参数 */
12
+ export interface ProductDetailUrlParams {
13
+ /** 电商平台 */
14
+ platform: CommercePlatform | number;
15
+ /** 商品 ID / SKU */
16
+ skuId: number | string;
17
+ /** 附加到链接上的查询参数 */
18
+ query?: Record<string, string | number | boolean | undefined>;
8
19
  }
9
- export interface ProductDetailUrlOptions {
20
+ /**
21
+ * 根据电商平台和商品 ID 生成商品详情页链接。
22
+ * 平台不支持或 skuId 为空时返回空字符串。
23
+ */
24
+ export declare const getProductDetailUrl: ({ platform, skuId, query, }: ProductDetailUrlParams) => string;
25
+ export interface ProductDetailLinkProps extends ProductDetailUrlParams, Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, 'href'> {
26
+ children?: React.ReactNode;
10
27
  }
11
- export declare const getProductDetailUrl: (commercePlatformId?: number, skuId?: number | string, options?: ProductDetailUrlOptions) => string;
12
- declare const ProductDetailLink: React.FC<Props>;
28
+ declare const ProductDetailLink: React.FC<ProductDetailLinkProps>;
13
29
  export default ProductDetailLink;
@@ -1,36 +1,93 @@
1
+ import _extends from "@babel/runtime/helpers/esm/extends";
2
+ import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
3
+ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
4
+ var _excluded = ["platform", "skuId", "query", "children", "target", "rel", "onClick"];
1
5
  import React from 'react';
2
- export var getProductDetailUrl = function getProductDetailUrl(commercePlatformId, skuId) {
3
- var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
4
- if (!skuId) {
6
+ import isInClient from "./isInClient";
7
+
8
+ /**
9
+ * 电商平台枚举,取值与后端的 commercePlatformId 保持一致。
10
+ */
11
+ export var CommercePlatform = /*#__PURE__*/function (CommercePlatform) {
12
+ CommercePlatform[CommercePlatform["OZON"] = 7] = "OZON";
13
+ CommercePlatform[CommercePlatform["Wildberries"] = 15] = "Wildberries";
14
+ return CommercePlatform;
15
+ }({});
16
+
17
+ /** 生成商品详情链接所需的参数 */
18
+
19
+ /** 各平台的商品详情页 URL 生成器,扩展新平台时在此登记即可 */
20
+ var PLATFORM_URL_BUILDERS = _defineProperty(_defineProperty({}, CommercePlatform.OZON, function (skuId) {
21
+ return "https://ozon.ru/product/".concat(skuId);
22
+ }), CommercePlatform.Wildberries, function (skuId) {
23
+ return "https://www.wildberries.ru/catalog/".concat(skuId, "/detail.aspx");
24
+ });
25
+ function buildQueryString(query) {
26
+ if (!query) {
5
27
  return '';
6
28
  }
7
- switch (commercePlatformId) {
8
- case 7:
9
- // OZON
10
- return "https://ozon.ru/product/".concat(skuId);
11
- case 15:
12
- // WB
13
- return "https://www.wildberries.ru/catalog/".concat(skuId, "/detail.aspx");
14
- default:
15
- return '';
29
+ var pairs = Object.keys(query).filter(function (key) {
30
+ var value = query[key];
31
+ return value !== undefined && value !== null && value !== '';
32
+ }).map(function (key) {
33
+ return "".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(String(query[key])));
34
+ });
35
+ return pairs.length ? "?".concat(pairs.join('&')) : '';
36
+ }
37
+
38
+ /**
39
+ * 根据电商平台和商品 ID 生成商品详情页链接。
40
+ * 平台不支持或 skuId 为空时返回空字符串。
41
+ */
42
+ export var getProductDetailUrl = function getProductDetailUrl(_ref) {
43
+ var platform = _ref.platform,
44
+ skuId = _ref.skuId,
45
+ query = _ref.query;
46
+ if (skuId === undefined || skuId === null || skuId === '') {
47
+ return '';
16
48
  }
49
+ var build = PLATFORM_URL_BUILDERS[platform];
50
+ if (!build) {
51
+ return '';
52
+ }
53
+ return "".concat(build(encodeURIComponent(String(skuId)))).concat(buildQueryString(query));
17
54
  };
18
- var ProductDetailLink = function ProductDetailLink(_ref) {
19
- var commercePlatformId = _ref.commercePlatformId,
20
- skuId = _ref.skuId,
21
- className = _ref.className,
22
- linkTitle = _ref.linkTitle,
23
- children = _ref.children;
24
- var href = getProductDetailUrl(commercePlatformId, skuId);
55
+ var ProductDetailLink = function ProductDetailLink(_ref2) {
56
+ var platform = _ref2.platform,
57
+ skuId = _ref2.skuId,
58
+ query = _ref2.query,
59
+ children = _ref2.children,
60
+ _ref2$target = _ref2.target,
61
+ target = _ref2$target === void 0 ? '_blank' : _ref2$target,
62
+ _ref2$rel = _ref2.rel,
63
+ rel = _ref2$rel === void 0 ? 'noopener noreferrer' : _ref2$rel,
64
+ onClick = _ref2.onClick,
65
+ rest = _objectWithoutProperties(_ref2, _excluded);
66
+ var href = getProductDetailUrl({
67
+ platform: platform,
68
+ skuId: skuId,
69
+ query: query
70
+ });
25
71
  if (!href) {
26
72
  return /*#__PURE__*/React.createElement(React.Fragment, null, children);
27
73
  }
28
- return /*#__PURE__*/React.createElement("a", {
74
+ var handleClick = function handleClick(event) {
75
+ // 客户端内通过桥接在本机浏览器打开,避免新开浏览器标签页
76
+ if (isInClient()) {
77
+ var _window$getBridge, _window;
78
+ var bridge = (_window$getBridge = (_window = window).getBridge) === null || _window$getBridge === void 0 ? void 0 : _window$getBridge.call(_window);
79
+ if (bridge !== null && bridge !== void 0 && bridge.openLocalBrowser) {
80
+ event.preventDefault();
81
+ bridge.openLocalBrowser(href);
82
+ }
83
+ }
84
+ onClick === null || onClick === void 0 || onClick(event);
85
+ };
86
+ return /*#__PURE__*/React.createElement("a", _extends({
29
87
  href: href,
30
- title: linkTitle,
31
- target: "_blank",
32
- rel: "noreferrer",
33
- className: className
34
- }, children);
88
+ target: target,
89
+ rel: rel,
90
+ onClick: handleClick
91
+ }, rest), children);
35
92
  };
36
93
  export default ProductDetailLink;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "component-sanzhizhou",
3
- "version": "1.2.10",
3
+ "version": "1.2.11",
4
4
  "description": "",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",