@polyv/utils 2.12.0 → 2.13.0-beta.2

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.
@@ -0,0 +1,170 @@
1
+ /**
2
+ * 本模块提供链接处理相关方法。
3
+ * @packageDocumentation
4
+ */
5
+ /**
6
+ * 保利威 WebView 桥接器接口
7
+ */
8
+ export interface WebViewBridge {
9
+ /**
10
+ * 向 WebView 发送数据
11
+ * @param event 事件
12
+ * @param data 数据对象
13
+ */
14
+ sendData: (event: string, data: Record<string, unknown>) => void;
15
+ }
16
+ /** 是否安卓 */
17
+ export declare const isAndroid: boolean;
18
+ /** 是否 iOS */
19
+ export declare const isIOS: boolean;
20
+ /** 是否微信(非企业微信) */
21
+ export declare const isWeixin: boolean;
22
+ /** 是否企业微信 */
23
+ export declare const isWorkWeixin: boolean;
24
+ /** 判断 PC 端微信小程序环境 */
25
+ export declare const isPcMiniProgram: boolean;
26
+ /** 是否移动端 */
27
+ export declare const isMobile: boolean;
28
+ type ToPointMallFunc = (params: string) => unknown;
29
+ declare global {
30
+ interface Window {
31
+ AndroidNative?: {
32
+ toPointMall?: ToPointMallFunc;
33
+ };
34
+ webkit?: {
35
+ messageHandlers?: {
36
+ gotoPointsMall?: {
37
+ postMessage?: ToPointMallFunc;
38
+ };
39
+ };
40
+ };
41
+ }
42
+ }
43
+ /**
44
+ * 链接类型
45
+ */
46
+ export declare enum LinkType {
47
+ /**
48
+ * 通用链接
49
+ */
50
+ Normal = 10,
51
+ /**
52
+ * 多平台链接
53
+ */
54
+ MultiPlatform = 11,
55
+ /**
56
+ * 原生方法跳转
57
+ */
58
+ Native = 12
59
+ }
60
+ /**
61
+ * 外链跳转方式
62
+ */
63
+ export declare enum LinkJumpWay {
64
+ /**
65
+ * iframe 弹框形式打开
66
+ */
67
+ PopUp = "POP_UP",
68
+ /**
69
+ * 新窗口打开
70
+ */
71
+ NewWindow = "NEW_WINDOW",
72
+ /**
73
+ * 当前窗口打开
74
+ */
75
+ CurrentWindow = "CURRENT_WINDOW"
76
+ }
77
+ /**
78
+ * 链接数据接口
79
+ */
80
+ export interface LinkData {
81
+ /**
82
+ * 链接类型
83
+ */
84
+ linkType: LinkType;
85
+ /**
86
+ * 跳转方式
87
+ */
88
+ jumpWay: LinkJumpWay;
89
+ /**
90
+ * 通用平台跳转链接
91
+ */
92
+ link: string;
93
+ /**
94
+ * PC 端跳转链接
95
+ */
96
+ pcLink: string;
97
+ /**
98
+ * 移动端跳转链接
99
+ */
100
+ mobileLink: string;
101
+ /**
102
+ * App 链接
103
+ */
104
+ mobileAppLink: string;
105
+ /**
106
+ * 安卓端 app 跳转链接
107
+ */
108
+ androidLink: string;
109
+ /**
110
+ * iOS app 跳转链接
111
+ */
112
+ iosLink: string;
113
+ /**
114
+ * 其他链接
115
+ */
116
+ otherLink: string;
117
+ /**
118
+ * 微信小程序原始 id
119
+ */
120
+ wxMiniprogramOriginalId: string;
121
+ /**
122
+ * 微信小程序应用 id
123
+ */
124
+ wxMiniprogramAppId: string;
125
+ /**
126
+ * 微信小程序内页面路径及参数
127
+ */
128
+ wxMiniprogramLink: string;
129
+ }
130
+ /**
131
+ * 跳转链接配置项
132
+ */
133
+ export interface NavigateToLinkOptions {
134
+ /** 链接数据 */
135
+ linkData: LinkData;
136
+ /** 通用链接打开处理器 */
137
+ openLink: (url: string, jumpWay: LinkJumpWay) => void;
138
+ /** 是否处于保利威 webview 中 */
139
+ isPlvWebview?: () => boolean;
140
+ /** 获取保利威 webview 桥接器 */
141
+ getPlvWebviewBridge?: () => Promise<WebViewBridge | undefined>;
142
+ /** 获取保利威 webview 小窗尺寸 */
143
+ getPlvWebviewSmallWindowSize?: () => {
144
+ width: number;
145
+ height: number;
146
+ };
147
+ isWxMiniProgramEnv?: () => Promise<boolean | undefined>;
148
+ /** 跳转微信小程序 */
149
+ toWxMiniProgram?: (link: string) => void;
150
+ }
151
+ /**
152
+ * 格式化链接地址
153
+ * @param url 需要格式化的链接地址
154
+ * @param getLinkParams 获取额外参数的函数,可选
155
+ * @returns 格式化后的链接地址
156
+ */
157
+ export declare function formatLink(url: string, getLinkParams?: (url: string) => Record<string, unknown>): string;
158
+ /**
159
+ * 根据链接类型和当前环境进行跳转处理
160
+ * @param options 跳转配置项
161
+ * @param options.linkData 链接数据,包含各种平台的链接和跳转方式
162
+ * @param options.openLink 通用链接打开处理器
163
+ * @param options.isPlvWebview 判断是否处于保利威 webview 中的函数
164
+ * @param options.getPlvWebviewSmallWindowSize 获取保利威 webview 小窗尺寸的函数
165
+ * @param options.getPlvWebviewBridge 获取保利威 webview 桥接器的函数
166
+ * @param options.isWxMiniProgramEnv 判断是否处于微信小程序环境的函数
167
+ * @param options.toWxMiniProgram 跳转微信小程序的函数
168
+ */
169
+ export declare function navigateToLink(options: NavigateToLinkOptions): void;
170
+ export {};
@@ -0,0 +1 @@
1
+ "use strict";var __awaiter=this&&this.__awaiter||function(i,n,e,t){return new(e||(e=Promise))((function(r,o){function a(i){try{s(t.next(i))}catch(i){o(i)}}function l(i){try{s(t.throw(i))}catch(i){o(i)}}function s(i){var n;i.done?r(i.value):(n=i.value,n instanceof e?n:new e((function(i){i(n)}))).then(a,l)}s((t=t.apply(i,n||[])).next())}))},__generator=this&&this.__generator||function(i,n){var e,t,r,o,a={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]};return o={next:l(0),throw:l(1),return:l(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function l(l){return function(s){return function(l){if(e)throw new TypeError("Generator is already executing.");for(;o&&(o=0,l[0]&&(a=0)),a;)try{if(e=1,t&&(r=2&l[0]?t.return:l[0]?t.throw||((r=t.return)&&r.call(t),0):t.next)&&!(r=r.call(t,l[1])).done)return r;switch(t=0,r&&(l=[2&l[0],r.value]),l[0]){case 0:case 1:r=l;break;case 4:return a.label++,{value:l[1],done:!1};case 5:a.label++,t=l[1],l=[0];continue;case 7:l=a.ops.pop(),a.trys.pop();continue;default:if(!(r=a.trys,(r=r.length>0&&r[r.length-1])||6!==l[0]&&2!==l[0])){a=0;continue}if(3===l[0]&&(!r||l[1]>r[0]&&l[1]<r[3])){a.label=l[1];break}if(6===l[0]&&a.label<r[1]){a.label=r[1],r=l;break}if(r&&a.label<r[2]){a.label=r[2],a.ops.push(l);break}r[2]&&a.ops.pop(),a.trys.pop();continue}l=n.call(i,a)}catch(i){l=[6,i],t=0}finally{e=r=0}if(5&l[0])throw l[1];return{value:l[0]?l[1]:void 0,done:!0}}([l,s])}}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.LinkJumpWay=exports.LinkType=exports.isMobile=exports.isPcMiniProgram=exports.isWorkWeixin=exports.isWeixin=exports.isIOS=exports.isAndroid=void 0,exports.formatLink=formatLink,exports.navigateToLink=navigateToLink;var LinkType,LinkJumpWay,ua_info_1=require("@just4/ua-info"),querystring_1=require("@just4/querystring"),ua=navigator.userAgent.toLowerCase(),uaInfo=(0,ua_info_1.getCurrentUAInfo)();function getNativeToPointMallFn(){var i,n,e,t;return exports.isAndroid?null===(i=window.AndroidNative)||void 0===i?void 0:i.toPointMall:exports.isIOS?null===(t=null===(e=null===(n=window.webkit)||void 0===n?void 0:n.messageHandlers)||void 0===e?void 0:e.gotoPointsMall)||void 0===t?void 0:t.postMessage:void 0}function toNativeLink(i){var n=i.androidLink,e=i.iosLink,t=i.otherLink,r=getNativeToPointMallFn();if(r){var o=exports.isAndroid?n:e,a=encodeURIComponent(location.href);return o=(0,querystring_1.concat)(o,{plt_back_uri:a}),void r(JSON.stringify({url:o}))}window.open(t,"_blank","noopener=yes")}function toPlvWebviewBridge(i){return __awaiter(this,void 0,void 0,(function(){var n,e,t,r;return __generator(this,(function(o){switch(o.label){case 0:return n=i.getPlvWebviewSmallWindowSize,e=i.getPlvWebviewBridge,t=(null==n?void 0:n())||{width:90,height:160},[4,null==e?void 0:e()];case 1:return(r=o.sent())?(r.sendData("clickProduct",{width:t.width,height:t.height,newPage:!0,link:i.link,data:i.data}),[2]):[2]}}))}))}function toMultiPlatformLink(i){return __awaiter(this,void 0,void 0,(function(){var n,e,t,r,o,a,l,s;return __generator(this,(function(u){switch(u.label){case 0:n=i.linkData,e=i.isWxMiniProgramEnv,t=i.toWxMiniProgram,r=i.openLink,o=n.wxMiniprogramLink,a=n.mobileLink,l=n.pcLink,s=!1,u.label=1;case 1:return u.trys.push([1,3,,4]),[4,null==e?void 0:e()];case 2:return s=u.sent()||!1,[3,4];case 3:return u.sent(),s=!1,[3,4];case 4:return s&&o&&t?(t(formatLink(o)),[2]):exports.isMobile?(r(formatLink(a),LinkJumpWay.NewWindow),[2]):(exports.isMobile||r(formatLink(l),LinkJumpWay.NewWindow),[2])}}))}))}function formatLink(i,n){var e={};if(n){var t=n(i);e=Object.assign({},e,t)}return(0,querystring_1.concat)(i,e)}function navigateToLink(i){var n=i.linkData,e=i.openLink,t=i.isPlvWebview,r=i.getPlvWebviewSmallWindowSize,o=i.getPlvWebviewBridge,a=i.isWxMiniProgramEnv,l=i.toWxMiniProgram,s=n.linkType,u=(null==t?void 0:t())||!1;if(s!==LinkType.Native){if(u){var p="",k=n.link,c=n.mobileLink,f=n.mobileAppLink,v=n.wxMiniprogramOriginalId,d=n.wxMiniprogramLink;switch(s){case LinkType.Normal:p=k;break;case LinkType.MultiPlatform:p=f||c}var m=null;return s!==LinkType.Normal&&(m={mobileLink:formatLink(c),wxMiniprogramOriginalId:v,wxMiniprogramLink:formatLink(d),mobileAppLink:formatLink(f)}),void toPlvWebviewBridge({link:formatLink(p),data:m,getPlvWebviewSmallWindowSize:r,getPlvWebviewBridge:o})}if(s!==LinkType.Normal)s===LinkType.MultiPlatform&&toMultiPlatformLink({linkData:n,isWxMiniProgramEnv:a,toWxMiniProgram:l,openLink:e});else{k=n.link;var L=n.jumpWay;e(formatLink(k),L)}}else toNativeLink({androidLink:formatLink(n.androidLink),iosLink:formatLink(n.iosLink),otherLink:formatLink(n.otherLink)})}exports.isAndroid=uaInfo.os.isAndroid,exports.isIOS=uaInfo.os.isIOS,exports.isWeixin=uaInfo.client.isWx,exports.isWorkWeixin=uaInfo.client.isWxWork,exports.isPcMiniProgram=(exports.isWeixin||exports.isWorkWeixin)&&/miniprogramenv/.test(ua),exports.isMobile=uaInfo.isPortable||exports.isPcMiniProgram,function(i){i[i.Normal=10]="Normal",i[i.MultiPlatform=11]="MultiPlatform",i[i.Native=12]="Native"}(LinkType||(exports.LinkType=LinkType={})),function(i){i.PopUp="POP_UP",i.NewWindow="NEW_WINDOW",i.CurrentWindow="CURRENT_WINDOW"}(LinkJumpWay||(exports.LinkJumpWay=LinkJumpWay={}));
package/cjs/image.js CHANGED
@@ -1 +1 @@
1
- "use strict";function getExtname(e){var A=(e||"").split("."),t=A[A.length-1];return t?t.toLowerCase():""}function handleCompressOptions(e){var A,t,r="number"==typeof e?{width:e}:e;return r.allowJPG=null===(A=r.allowJPG)||void 0===A||A,r.allowWebP=null!==(t=r.allowWebP)&&void 0!==t?t:"auto",r}function createURLObject(e){var A;if("undefined"!=typeof document){var t=document.createElement("a");t.href=e,A=t}else if("function"==typeof URL)try{A=new URL(/^\/\//.test(e)?"https:"+e:e)}catch(e){}return A}function genOSSCompressParams(e,A){var t="";return null==A.width&&null==A.height||(t+="/resize",A.width&&(t+=",w_"+A.width),A.height&&(t+=",h_"+A.height),t+=",limit_1"),A.allowAVIF&&"gif"!==e?t+="/format,avif":!0===A.allowWebP||"auto"===A.allowWebP&&(0,exports.supportWebP)()?t+="/format,webp/quality,Q_80":A.allowJPG&&"gif"!==e&&(t+="/format,jpg/quality,Q_80"),t}function setOSSCompressParams(e,A){var t=!1;return e=e.replace(/([?&]x-oss-process)(?:=([^&]*))?/,(function(e,r,o){return t=!0,/^image/.test(o)?r+"="+o.replace(/\/(?:resize|format|quality)[^/]*/gi,"")+A:e})),t?e:e+(-1===e.indexOf("?")?"?":"&")+"x-oss-process=image"+A}function ossCompress(e,A){var t=createURLObject(e);if(!t)return e;var r=getExtname(t.pathname.split("/").pop());if(!/\.videocc\.net$/i.test(t.hostname)||"svg"===r)return e;var o=genOSSCompressParams(r,handleCompressOptions(A));return o&&(t.search=setOSSCompressParams(t.search,o)),t.href}function genCOSCompressParams(e,A){var t="";return null!=A.width&&null!=A.height?t+="/thumbnail/".concat(A.width,"x").concat(A.height):null!=A.width?t+="/thumbnail/".concat(A.width,"x"):null!=A.height&&(t+="/thumbnail/x".concat(A.height)),A.allowAVIF&&"gif"!==e?t+="/format/avif":!0===A.allowWebP||"auto"===A.allowWebP&&(0,exports.supportWebP)()?t+="/format/webp/quality/80":A.allowJPG&&"gif"!==e&&(t+="/format/jpg/quality/80"),t}function setCOSCompressParams(e,A){var t=!1;return e=e.replace(/([?&]imageMogr2)(\/[^&]*)/,(function(e,r,o){return t=!0,r+"="+o.replace(/\/(?:thumbnail|format|quality)\/[^/]+/gi,"")+A})),t?e:e+(-1===e.indexOf("?")?"?":"&")+"imageMogr2"+A}function cosCompress(e,A){var t=createURLObject(e);if(!t)return e;var r=getExtname(t.pathname.split("/").pop());if(!/(?:\.videocc\.net|\.kingswayvideo\.com)$/i.test(t.hostname)||"svg"===r)return e;var o=genCOSCompressParams(r,handleCompressOptions(A));return o&&(t.search=setCOSCompressParams(t.search,o)),t.href}function compressHTMLImgs(e,A,t){return void 0===t&&(t=ossCompress),e?e.replace(/(<img.*?\ssrc=)(["']?)(.+?)\2(.*?>)/gi,(function(e,r,o,a,s){return r+'"'+t(a,A)+'" data-src="'+a+'"'+s})):""}Object.defineProperty(exports,"__esModule",{value:!0}),exports.supportAVIF=exports.supportWebP=void 0,exports.ossCompress=ossCompress,exports.cosCompress=cosCompress,exports.compressHTMLImgs=compressHTMLImgs,exports.supportWebP=function(){var e;return function(){if(null==e)try{e=function(){var e=document.createElement("canvas");if(e.getContext&&e.getContext("2d")){var A="image/webp";return 0===e.toDataURL(A).indexOf("data:"+A)}return!1}()}catch(A){e=!1}return e}}(),exports.supportAVIF=function(){var e;return function(){return e||(e=new Promise((function(e){var A=new Image;A.src="data:image/avif;base64,AAAAIGZ0eXBhdmlmAAAAAGF2aWZtaWYxbWlhZk1BMUIAAADybWV0YQAAAAAAAAAoaGRscgAAAAAAAAAAcGljdAAAAAAAAAAAAAAAAGxpYmF2aWYAAAAADnBpdG0AAAAAAAEAAAAeaWxvYwAAAABEAAABAAEAAAABAAABGgAAAB0AAAAoaWluZgAAAAAAAQAAABppbmZlAgAAAAABAABhdjAxQ29sb3IAAAAAamlwcnAAAABLaXBjbwAAABRpc3BlAAAAAAAAAAIAAAACAAAAEHBpeGkAAAAAAwgICAAAAAxhdjFDgQ0MAAAAABNjb2xybmNseAACAAIAAYAAAAAXaXBtYQAAAAAAAAABAAEEAQKDBAAAACVtZGF0EgAKCBgANogQEAwgMg8f8D///8WfhwB8+ErK42A=",A.onerror=function(){e(!1)},A.onload=function(){e(!0)},setTimeout((function(){e(!1)}),1e3)}))),e}}();
1
+ "use strict";function getExtname(e){var A=(e||"").split("."),t=A[A.length-1];return t?t.toLowerCase():""}function handleCompressOptions(e){var A,t,r="number"==typeof e?{width:e}:e;return r.allowJPG=null===(A=r.allowJPG)||void 0===A||A,r.allowWebP=null!==(t=r.allowWebP)&&void 0!==t?t:"auto",r}function createURLObject(e){var A;if("undefined"!=typeof document){var t=document.createElement("a");t.href=e,A=t}else if("function"==typeof URL)try{A=new URL(/^\/\//.test(e)?"https:"+e:e)}catch(e){}return A}function genOSSCompressParams(e,A){var t="";return null==A.width&&null==A.height||(t+="/resize",A.width&&(t+=",w_"+A.width),A.height&&(t+=",h_"+A.height),t+=",limit_1"),A.allowAVIF&&"gif"!==e?t+="/format,avif":!0===A.allowWebP||"auto"===A.allowWebP&&(0,exports.supportWebP)()?t+="/format,webp/quality,Q_80":A.allowJPG&&"gif"!==e&&(t+="/format,jpg/quality,Q_80"),t}function setOSSCompressParams(e,A){var t=!1;return e=e.replace(/([?&]x-oss-process)(?:=([^&]*))?/,(function(e,r,a){return t=!0,/^image/.test(a)?r+"="+a.replace(/\/(?:resize|format|quality)[^/]*/gi,"")+A:e})),t?e:e+(-1===e.indexOf("?")?"?":"&")+"x-oss-process=image"+A}function ossCompress(e,A){var t=createURLObject(e);if(!t)return e;var r=getExtname(t.pathname.split("/").pop());if(!/\.videocc\.net$/i.test(t.hostname)||"svg"===r)return e;var a=genOSSCompressParams(r,handleCompressOptions(A));return a&&(t.search=setOSSCompressParams(t.search,a)),t.href}function genCOSCompressParams(e,A){var t="";return null!=A.width&&null!=A.height?t+="/thumbnail/".concat(A.width,"x").concat(A.height):null!=A.width?t+="/thumbnail/".concat(A.width,"x"):null!=A.height&&(t+="/thumbnail/x".concat(A.height)),A.allowAVIF&&"gif"!==e?t+="/format/avif":!0===A.allowWebP||"auto"===A.allowWebP&&(0,exports.supportWebP)()?t+="/format/webp/quality/80":A.allowJPG&&"gif"!==e&&(t+="/format/jpg/quality/80"),t}function setCOSCompressParams(e,A){var t=!1;return e=e.replace(/([?&]imageMogr2)(\/[^&]*)/,(function(e,r,a){return t=!0,r+"="+a.replace(/\/(?:thumbnail|format|quality)\/[^/]+/gi,"")+A})),t?e:e+(-1===e.indexOf("?")?"?":"&")+"imageMogr2"+A}function cosCompress(e,A){var t=createURLObject(e);if(!t)return e;var r=getExtname(t.pathname.split("/").pop());if(!/(?:\.videocc\.net|\.kingswayvideo\.com)$/i.test(t.hostname)||"svg"===r)return e;var a=genCOSCompressParams(r,handleCompressOptions(A));return a&&(t.search=setCOSCompressParams(t.search,a)),t.href}function compressHTMLImgs(e,A,t){return void 0===t&&(t=ossCompress),e?e.replace(/(<img.*?\ssrc=)(["']?)(.+?)\2(.*?>)/gi,(function(e,r,a,s,o){return r+'"'+t(s,A)+'" data-src="'+s+'"'+o})):""}Object.defineProperty(exports,"__esModule",{value:!0}),exports.supportAVIF=exports.supportWebP=void 0,exports.ossCompress=ossCompress,exports.cosCompress=cosCompress,exports.compressHTMLImgs=compressHTMLImgs,exports.supportWebP=function(){var e;return function(){if(null==e)try{e=function(){var e=document.createElement("canvas");if(e.getContext&&e.getContext("2d")){var A="image/webp";return 0===e.toDataURL(A).indexOf("data:"+A)}return!1}()||function(){var e=!1,A=navigator.userAgent,t=/\bOS\s([\d_.]+)\slike\sMac\sOS\sX\b/.exec(A);if(t)e=parseInt(t[1])>=14;else{var r=/\bVersion\/([\d.]+)\sSafari\b/.exec(A);r&&(e=parseInt(r[1])>=16)}return e}()}catch(A){e=!1}return e}}(),exports.supportAVIF=function(){var e;return function(){return e||(e=new Promise((function(e){var A=new Image;A.src="data:image/avif;base64,AAAAIGZ0eXBhdmlmAAAAAGF2aWZtaWYxbWlhZk1BMUIAAADybWV0YQAAAAAAAAAoaGRscgAAAAAAAAAAcGljdAAAAAAAAAAAAAAAAGxpYmF2aWYAAAAADnBpdG0AAAAAAAEAAAAeaWxvYwAAAABEAAABAAEAAAABAAABGgAAAB0AAAAoaWluZgAAAAAAAQAAABppbmZlAgAAAAABAABhdjAxQ29sb3IAAAAAamlwcnAAAABLaXBjbwAAABRpc3BlAAAAAAAAAAIAAAACAAAAEHBpeGkAAAAAAwgICAAAAAxhdjFDgQ0MAAAAABNjb2xybmNseAACAAIAAYAAAAAXaXBtYQAAAAAAAAABAAEEAQKDBAAAACVtZGF0EgAKCBgANogQEAwgMg8f8D///8WfhwB8+ErK42A=",A.onerror=function(){e(!1)},A.onload=function(){e(!0)},setTimeout((function(){e(!1)}),1e3)}))),e}}();
@@ -0,0 +1,170 @@
1
+ /**
2
+ * 本模块提供链接处理相关方法。
3
+ * @packageDocumentation
4
+ */
5
+ /**
6
+ * 保利威 WebView 桥接器接口
7
+ */
8
+ export interface WebViewBridge {
9
+ /**
10
+ * 向 WebView 发送数据
11
+ * @param event 事件
12
+ * @param data 数据对象
13
+ */
14
+ sendData: (event: string, data: Record<string, unknown>) => void;
15
+ }
16
+ /** 是否安卓 */
17
+ export declare const isAndroid: boolean;
18
+ /** 是否 iOS */
19
+ export declare const isIOS: boolean;
20
+ /** 是否微信(非企业微信) */
21
+ export declare const isWeixin: boolean;
22
+ /** 是否企业微信 */
23
+ export declare const isWorkWeixin: boolean;
24
+ /** 判断 PC 端微信小程序环境 */
25
+ export declare const isPcMiniProgram: boolean;
26
+ /** 是否移动端 */
27
+ export declare const isMobile: boolean;
28
+ type ToPointMallFunc = (params: string) => unknown;
29
+ declare global {
30
+ interface Window {
31
+ AndroidNative?: {
32
+ toPointMall?: ToPointMallFunc;
33
+ };
34
+ webkit?: {
35
+ messageHandlers?: {
36
+ gotoPointsMall?: {
37
+ postMessage?: ToPointMallFunc;
38
+ };
39
+ };
40
+ };
41
+ }
42
+ }
43
+ /**
44
+ * 链接类型
45
+ */
46
+ export declare enum LinkType {
47
+ /**
48
+ * 通用链接
49
+ */
50
+ Normal = 10,
51
+ /**
52
+ * 多平台链接
53
+ */
54
+ MultiPlatform = 11,
55
+ /**
56
+ * 原生方法跳转
57
+ */
58
+ Native = 12
59
+ }
60
+ /**
61
+ * 外链跳转方式
62
+ */
63
+ export declare enum LinkJumpWay {
64
+ /**
65
+ * iframe 弹框形式打开
66
+ */
67
+ PopUp = "POP_UP",
68
+ /**
69
+ * 新窗口打开
70
+ */
71
+ NewWindow = "NEW_WINDOW",
72
+ /**
73
+ * 当前窗口打开
74
+ */
75
+ CurrentWindow = "CURRENT_WINDOW"
76
+ }
77
+ /**
78
+ * 链接数据接口
79
+ */
80
+ export interface LinkData {
81
+ /**
82
+ * 链接类型
83
+ */
84
+ linkType: LinkType;
85
+ /**
86
+ * 跳转方式
87
+ */
88
+ jumpWay: LinkJumpWay;
89
+ /**
90
+ * 通用平台跳转链接
91
+ */
92
+ link: string;
93
+ /**
94
+ * PC 端跳转链接
95
+ */
96
+ pcLink: string;
97
+ /**
98
+ * 移动端跳转链接
99
+ */
100
+ mobileLink: string;
101
+ /**
102
+ * App 链接
103
+ */
104
+ mobileAppLink: string;
105
+ /**
106
+ * 安卓端 app 跳转链接
107
+ */
108
+ androidLink: string;
109
+ /**
110
+ * iOS app 跳转链接
111
+ */
112
+ iosLink: string;
113
+ /**
114
+ * 其他链接
115
+ */
116
+ otherLink: string;
117
+ /**
118
+ * 微信小程序原始 id
119
+ */
120
+ wxMiniprogramOriginalId: string;
121
+ /**
122
+ * 微信小程序应用 id
123
+ */
124
+ wxMiniprogramAppId: string;
125
+ /**
126
+ * 微信小程序内页面路径及参数
127
+ */
128
+ wxMiniprogramLink: string;
129
+ }
130
+ /**
131
+ * 跳转链接配置项
132
+ */
133
+ export interface NavigateToLinkOptions {
134
+ /** 链接数据 */
135
+ linkData: LinkData;
136
+ /** 通用链接打开处理器 */
137
+ openLink: (url: string, jumpWay: LinkJumpWay) => void;
138
+ /** 是否处于保利威 webview 中 */
139
+ isPlvWebview?: () => boolean;
140
+ /** 获取保利威 webview 桥接器 */
141
+ getPlvWebviewBridge?: () => Promise<WebViewBridge | undefined>;
142
+ /** 获取保利威 webview 小窗尺寸 */
143
+ getPlvWebviewSmallWindowSize?: () => {
144
+ width: number;
145
+ height: number;
146
+ };
147
+ isWxMiniProgramEnv?: () => Promise<boolean | undefined>;
148
+ /** 跳转微信小程序 */
149
+ toWxMiniProgram?: (link: string) => void;
150
+ }
151
+ /**
152
+ * 格式化链接地址
153
+ * @param url 需要格式化的链接地址
154
+ * @param getLinkParams 获取额外参数的函数,可选
155
+ * @returns 格式化后的链接地址
156
+ */
157
+ export declare function formatLink(url: string, getLinkParams?: (url: string) => Record<string, unknown>): string;
158
+ /**
159
+ * 根据链接类型和当前环境进行跳转处理
160
+ * @param options 跳转配置项
161
+ * @param options.linkData 链接数据,包含各种平台的链接和跳转方式
162
+ * @param options.openLink 通用链接打开处理器
163
+ * @param options.isPlvWebview 判断是否处于保利威 webview 中的函数
164
+ * @param options.getPlvWebviewSmallWindowSize 获取保利威 webview 小窗尺寸的函数
165
+ * @param options.getPlvWebviewBridge 获取保利威 webview 桥接器的函数
166
+ * @param options.isWxMiniProgramEnv 判断是否处于微信小程序环境的函数
167
+ * @param options.toWxMiniProgram 跳转微信小程序的函数
168
+ */
169
+ export declare function navigateToLink(options: NavigateToLinkOptions): void;
170
+ export {};
@@ -0,0 +1 @@
1
+ var __awaiter=this&&this.__awaiter||function(i,n,o,t){return new(o||(o=Promise))((function(e,r){function a(i){try{s(t.next(i))}catch(i){r(i)}}function l(i){try{s(t.throw(i))}catch(i){r(i)}}function s(i){var n;i.done?e(i.value):(n=i.value,n instanceof o?n:new o((function(i){i(n)}))).then(a,l)}s((t=t.apply(i,n||[])).next())}))};import{getCurrentUAInfo}from"@just4/ua-info";import{concat}from"@just4/querystring";const ua=navigator.userAgent.toLowerCase(),uaInfo=getCurrentUAInfo();export const isAndroid=uaInfo.os.isAndroid;export const isIOS=uaInfo.os.isIOS;export const isWeixin=uaInfo.client.isWx;export const isWorkWeixin=uaInfo.client.isWxWork;export const isPcMiniProgram=(isWeixin||isWorkWeixin)&&/miniprogramenv/.test(ua);export const isMobile=uaInfo.isPortable||isPcMiniProgram;export var LinkType;!function(i){i[i.Normal=10]="Normal",i[i.MultiPlatform=11]="MultiPlatform",i[i.Native=12]="Native"}(LinkType||(LinkType={}));export var LinkJumpWay;function getNativeToPointMallFn(){var i,n,o,t;return isAndroid?null===(i=window.AndroidNative)||void 0===i?void 0:i.toPointMall:isIOS?null===(t=null===(o=null===(n=window.webkit)||void 0===n?void 0:n.messageHandlers)||void 0===o?void 0:o.gotoPointsMall)||void 0===t?void 0:t.postMessage:void 0}function toNativeLink(i){const{androidLink:n,iosLink:o,otherLink:t}=i,e=getNativeToPointMallFn();if(e){let i=isAndroid?n:o;const t=encodeURIComponent(location.href);i=concat(i,{plt_back_uri:t});e(JSON.stringify({url:i}))}else window.open(t,"_blank","noopener=yes")}function toPlvWebviewBridge(i){return __awaiter(this,void 0,void 0,(function*(){const{getPlvWebviewSmallWindowSize:n,getPlvWebviewBridge:o}=i,t=(null==n?void 0:n())||{width:90,height:160},e=yield null==o?void 0:o();e&&e.sendData("clickProduct",{width:t.width,height:t.height,newPage:!0,link:i.link,data:i.data})}))}function toMultiPlatformLink(i){return __awaiter(this,void 0,void 0,(function*(){const{linkData:n,isWxMiniProgramEnv:o,toWxMiniProgram:t,openLink:e}=i,{wxMiniprogramLink:r,mobileLink:a,pcLink:l}=n;let s=!1;try{s=(yield null==o?void 0:o())||!1}catch(i){s=!1}s&&r&&t?t(formatLink(r)):isMobile?e(formatLink(a),LinkJumpWay.NewWindow):isMobile||e(formatLink(l),LinkJumpWay.NewWindow)}))}!function(i){i.PopUp="POP_UP",i.NewWindow="NEW_WINDOW",i.CurrentWindow="CURRENT_WINDOW"}(LinkJumpWay||(LinkJumpWay={}));export function formatLink(i,n){let o={};if(n){const t=n(i);o=Object.assign({},o,t)}return concat(i,o)}export function navigateToLink(i){const{linkData:n,openLink:o,isPlvWebview:t,getPlvWebviewSmallWindowSize:e,getPlvWebviewBridge:r,isWxMiniProgramEnv:a,toWxMiniProgram:l}=i,{linkType:s}=n,k=(null==t?void 0:t())||!1;if(s!==LinkType.Native){if(k){let i="";const{link:o,mobileLink:t,mobileAppLink:a,wxMiniprogramOriginalId:l,wxMiniprogramLink:k}=n;switch(s){case LinkType.Normal:i=o;break;case LinkType.MultiPlatform:i=a||t}let d=null;return s!==LinkType.Normal&&(d={mobileLink:formatLink(t),wxMiniprogramOriginalId:l,wxMiniprogramLink:formatLink(k),mobileAppLink:formatLink(a)}),void toPlvWebviewBridge({link:formatLink(i),data:d,getPlvWebviewSmallWindowSize:e,getPlvWebviewBridge:r})}if(s!==LinkType.Normal)s===LinkType.MultiPlatform&&toMultiPlatformLink({linkData:n,isWxMiniProgramEnv:a,toWxMiniProgram:l,openLink:o});else{const{link:i,jumpWay:t}=n;o(formatLink(i),t)}}else toNativeLink({androidLink:formatLink(n.androidLink),iosLink:formatLink(n.iosLink),otherLink:formatLink(n.otherLink)})}
package/es/image.js CHANGED
@@ -1 +1 @@
1
- export const supportWebP=(()=>{let A;return()=>{if(null==A)try{A=function(){const A=document.createElement("canvas");if(A.getContext&&A.getContext("2d")){const e="image/webp";return 0===A.toDataURL(e).indexOf("data:"+e)}return!1}()}catch(e){A=!1}return A}})();export const supportAVIF=(()=>{let A;return()=>(A||(A=new Promise((A=>{const e=new Image;e.src="data:image/avif;base64,AAAAIGZ0eXBhdmlmAAAAAGF2aWZtaWYxbWlhZk1BMUIAAADybWV0YQAAAAAAAAAoaGRscgAAAAAAAAAAcGljdAAAAAAAAAAAAAAAAGxpYmF2aWYAAAAADnBpdG0AAAAAAAEAAAAeaWxvYwAAAABEAAABAAEAAAABAAABGgAAAB0AAAAoaWluZgAAAAAAAQAAABppbmZlAgAAAAABAABhdjAxQ29sb3IAAAAAamlwcnAAAABLaXBjbwAAABRpc3BlAAAAAAAAAAIAAAACAAAAEHBpeGkAAAAAAwgICAAAAAxhdjFDgQ0MAAAAABNjb2xybmNseAACAAIAAYAAAAAXaXBtYQAAAAAAAAABAAEEAQKDBAAAACVtZGF0EgAKCBgANogQEAwgMg8f8D///8WfhwB8+ErK42A=",e.onerror=()=>{A(!1)},e.onload=()=>{A(!0)},setTimeout((()=>{A(!1)}),1e3)}))),A)})();function getExtname(A){const e=(A||"").split("."),t=e[e.length-1];return t?t.toLowerCase():""}function handleCompressOptions(A){var e,t;const o="number"==typeof A?{width:A}:A;return o.allowJPG=null===(e=o.allowJPG)||void 0===e||e,o.allowWebP=null!==(t=o.allowWebP)&&void 0!==t?t:"auto",o}function createURLObject(A){let e;if("undefined"!=typeof document){const t=document.createElement("a");t.href=A,e=t}else if("function"==typeof URL)try{e=new URL(/^\/\//.test(A)?"https:"+A:A)}catch(A){}return e}function genOSSCompressParams(A,e){let t="";return null==e.width&&null==e.height||(t+="/resize",e.width&&(t+=",w_"+e.width),e.height&&(t+=",h_"+e.height),t+=",limit_1"),e.allowAVIF&&"gif"!==A?t+="/format,avif":!0===e.allowWebP||"auto"===e.allowWebP&&supportWebP()?t+="/format,webp/quality,Q_80":e.allowJPG&&"gif"!==A&&(t+="/format,jpg/quality,Q_80"),t}function setOSSCompressParams(A,e){let t=!1;return A=A.replace(/([?&]x-oss-process)(?:=([^&]*))?/,((A,o,r)=>(t=!0,/^image/.test(r)?o+"="+r.replace(/\/(?:resize|format|quality)[^/]*/gi,"")+e:A))),t?A:A+(-1===A.indexOf("?")?"?":"&")+"x-oss-process=image"+e}export function ossCompress(A,e){const t=createURLObject(A);if(!t)return A;const o=getExtname(t.pathname.split("/").pop());if(!/\.videocc\.net$/i.test(t.hostname)||"svg"===o)return A;const r=genOSSCompressParams(o,handleCompressOptions(e));return r&&(t.search=setOSSCompressParams(t.search,r)),t.href}function genCOSCompressParams(A,e){let t="";return null!=e.width&&null!=e.height?t+=`/thumbnail/${e.width}x${e.height}`:null!=e.width?t+=`/thumbnail/${e.width}x`:null!=e.height&&(t+=`/thumbnail/x${e.height}`),e.allowAVIF&&"gif"!==A?t+="/format/avif":!0===e.allowWebP||"auto"===e.allowWebP&&supportWebP()?t+="/format/webp/quality/80":e.allowJPG&&"gif"!==A&&(t+="/format/jpg/quality/80"),t}function setCOSCompressParams(A,e){let t=!1;return A=A.replace(/([?&]imageMogr2)(\/[^&]*)/,((A,o,r)=>(t=!0,o+"="+r.replace(/\/(?:thumbnail|format|quality)\/[^/]+/gi,"")+e))),t?A:A+(-1===A.indexOf("?")?"?":"&")+"imageMogr2"+e}export function cosCompress(A,e){const t=createURLObject(A);if(!t)return A;const o=getExtname(t.pathname.split("/").pop());if(!/(?:\.videocc\.net|\.kingswayvideo\.com)$/i.test(t.hostname)||"svg"===o)return A;const r=genCOSCompressParams(o,handleCompressOptions(e));return r&&(t.search=setCOSCompressParams(t.search,r)),t.href}export function compressHTMLImgs(A,e,t=ossCompress){return A?A.replace(/(<img.*?\ssrc=)(["']?)(.+?)\2(.*?>)/gi,((A,o,r,a,n)=>o+'"'+t(a,e)+'" data-src="'+a+'"'+n)):""}
1
+ export const supportWebP=(()=>{let e;return()=>{if(null==e)try{e=function(){const e=document.createElement("canvas");if(e.getContext&&e.getContext("2d")){const A="image/webp";return 0===e.toDataURL(A).indexOf("data:"+A)}return!1}()||function(){let e=!1;const A=navigator.userAgent,t=/\bOS\s([\d_.]+)\slike\sMac\sOS\sX\b/.exec(A);if(t)e=parseInt(t[1])>=14;else{const t=/\bVersion\/([\d.]+)\sSafari\b/.exec(A);t&&(e=parseInt(t[1])>=16)}return e}()}catch(A){e=!1}return e}})();export const supportAVIF=(()=>{let e;return()=>(e||(e=new Promise((e=>{const A=new Image;A.src="data:image/avif;base64,AAAAIGZ0eXBhdmlmAAAAAGF2aWZtaWYxbWlhZk1BMUIAAADybWV0YQAAAAAAAAAoaGRscgAAAAAAAAAAcGljdAAAAAAAAAAAAAAAAGxpYmF2aWYAAAAADnBpdG0AAAAAAAEAAAAeaWxvYwAAAABEAAABAAEAAAABAAABGgAAAB0AAAAoaWluZgAAAAAAAQAAABppbmZlAgAAAAABAABhdjAxQ29sb3IAAAAAamlwcnAAAABLaXBjbwAAABRpc3BlAAAAAAAAAAIAAAACAAAAEHBpeGkAAAAAAwgICAAAAAxhdjFDgQ0MAAAAABNjb2xybmNseAACAAIAAYAAAAAXaXBtYQAAAAAAAAABAAEEAQKDBAAAACVtZGF0EgAKCBgANogQEAwgMg8f8D///8WfhwB8+ErK42A=",A.onerror=()=>{e(!1)},A.onload=()=>{e(!0)},setTimeout((()=>{e(!1)}),1e3)}))),e)})();function getExtname(e){const A=(e||"").split("."),t=A[A.length-1];return t?t.toLowerCase():""}function handleCompressOptions(e){var A,t;const n="number"==typeof e?{width:e}:e;return n.allowJPG=null===(A=n.allowJPG)||void 0===A||A,n.allowWebP=null!==(t=n.allowWebP)&&void 0!==t?t:"auto",n}function createURLObject(e){let A;if("undefined"!=typeof document){const t=document.createElement("a");t.href=e,A=t}else if("function"==typeof URL)try{A=new URL(/^\/\//.test(e)?"https:"+e:e)}catch(e){}return A}function genOSSCompressParams(e,A){let t="";return null==A.width&&null==A.height||(t+="/resize",A.width&&(t+=",w_"+A.width),A.height&&(t+=",h_"+A.height),t+=",limit_1"),A.allowAVIF&&"gif"!==e?t+="/format,avif":!0===A.allowWebP||"auto"===A.allowWebP&&supportWebP()?t+="/format,webp/quality,Q_80":A.allowJPG&&"gif"!==e&&(t+="/format,jpg/quality,Q_80"),t}function setOSSCompressParams(e,A){let t=!1;return e=e.replace(/([?&]x-oss-process)(?:=([^&]*))?/,((e,n,r)=>(t=!0,/^image/.test(r)?n+"="+r.replace(/\/(?:resize|format|quality)[^/]*/gi,"")+A:e))),t?e:e+(-1===e.indexOf("?")?"?":"&")+"x-oss-process=image"+A}export function ossCompress(e,A){const t=createURLObject(e);if(!t)return e;const n=getExtname(t.pathname.split("/").pop());if(!/\.videocc\.net$/i.test(t.hostname)||"svg"===n)return e;const r=genOSSCompressParams(n,handleCompressOptions(A));return r&&(t.search=setOSSCompressParams(t.search,r)),t.href}function genCOSCompressParams(e,A){let t="";return null!=A.width&&null!=A.height?t+=`/thumbnail/${A.width}x${A.height}`:null!=A.width?t+=`/thumbnail/${A.width}x`:null!=A.height&&(t+=`/thumbnail/x${A.height}`),A.allowAVIF&&"gif"!==e?t+="/format/avif":!0===A.allowWebP||"auto"===A.allowWebP&&supportWebP()?t+="/format/webp/quality/80":A.allowJPG&&"gif"!==e&&(t+="/format/jpg/quality/80"),t}function setCOSCompressParams(e,A){let t=!1;return e=e.replace(/([?&]imageMogr2)(\/[^&]*)/,((e,n,r)=>(t=!0,n+"="+r.replace(/\/(?:thumbnail|format|quality)\/[^/]+/gi,"")+A))),t?e:e+(-1===e.indexOf("?")?"?":"&")+"imageMogr2"+A}export function cosCompress(e,A){const t=createURLObject(e);if(!t)return e;const n=getExtname(t.pathname.split("/").pop());if(!/(?:\.videocc\.net|\.kingswayvideo\.com)$/i.test(t.hostname)||"svg"===n)return e;const r=genCOSCompressParams(n,handleCompressOptions(A));return r&&(t.search=setCOSCompressParams(t.search,r)),t.href}export function compressHTMLImgs(e,A,t=ossCompress){return e?e.replace(/(<img.*?\ssrc=)(["']?)(.+?)\2(.*?>)/gi,((e,n,r,s,o)=>n+'"'+t(s,A)+'" data-src="'+s+'"'+o)):""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polyv/utils",
3
- "version": "2.12.0",
3
+ "version": "2.13.0-beta.2",
4
4
  "description": "Utility functions of Polyv frontend development.",
5
5
  "keywords": [
6
6
  "polyv",
@@ -20,5 +20,9 @@
20
20
  "engines": {
21
21
  "node": ">=20.0.0"
22
22
  },
23
- "miniprogram": "./dist/es"
23
+ "miniprogram": "./dist/es",
24
+ "dependencies": {
25
+ "@just4/querystring": "^2.0.0",
26
+ "@just4/ua-info": "^3.2.0"
27
+ }
24
28
  }