xctc-utils 1.0.2 → 1.0.4
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/README.md +17 -0
- package/dist/crypto/index.d.ts +2 -0
- package/dist/crypto/index.js +45 -0
- package/dist/index.d.ts +1 -11
- package/dist/index.js +4 -4
- package/dist/utils.d.ts +2 -0
- package/dist/utils.js +20 -1
- package/dist/weixin/index.d.ts +18 -0
- package/dist/weixin/index.js +164 -0
- package/package.json +15 -3
    
        package/README.md
    ADDED
    
    
| @@ -0,0 +1,45 @@ | |
| 1 | 
            +
            "use strict";
         | 
| 2 | 
            +
            Object.defineProperty(exports, "__esModule", { value: true });
         | 
| 3 | 
            +
            exports.decrypt = exports.encrypt = void 0;
         | 
| 4 | 
            +
            var CryptoJS = require("crypto-js");
         | 
| 5 | 
            +
            var utils_1 = require("../utils");
         | 
| 6 | 
            +
            function formatVal(val) {
         | 
| 7 | 
            +
                var key = "";
         | 
| 8 | 
            +
                if (val) {
         | 
| 9 | 
            +
                    key = CryptoJS.enc.Utf8.parse(val);
         | 
| 10 | 
            +
                }
         | 
| 11 | 
            +
                else {
         | 
| 12 | 
            +
                    key = CryptoJS.enc.Utf8.parse('5uMz4R8r0926DkC8');
         | 
| 13 | 
            +
                }
         | 
| 14 | 
            +
                return key;
         | 
| 15 | 
            +
            }
         | 
| 16 | 
            +
            // 加密
         | 
| 17 | 
            +
            function encrypt(word, key, iv) {
         | 
| 18 | 
            +
                // key  //十六位十六进制数作为密钥
         | 
| 19 | 
            +
                // iv 十六位十六进制数作为密钥偏移量
         | 
| 20 | 
            +
                word = JSON.stringify(word);
         | 
| 21 | 
            +
                var src = CryptoJS.enc.Utf8.parse(word);
         | 
| 22 | 
            +
                key = formatVal(key);
         | 
| 23 | 
            +
                iv = formatVal(iv);
         | 
| 24 | 
            +
                var encrypted = CryptoJS.AES.encrypt(src, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
         | 
| 25 | 
            +
                return encrypted.ciphertext.toString().toUpperCase();
         | 
| 26 | 
            +
            }
         | 
| 27 | 
            +
            exports.encrypt = encrypt;
         | 
| 28 | 
            +
            ;
         | 
| 29 | 
            +
            // 解密
         | 
| 30 | 
            +
            function decrypt(word, key, iv) {
         | 
| 31 | 
            +
                key = formatVal(key);
         | 
| 32 | 
            +
                iv = formatVal(iv);
         | 
| 33 | 
            +
                var encryptedHexStr = CryptoJS.enc.Hex.parse(word);
         | 
| 34 | 
            +
                var src = CryptoJS.enc.Base64.stringify(encryptedHexStr);
         | 
| 35 | 
            +
                var decrypt = CryptoJS.AES.decrypt(src, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
         | 
| 36 | 
            +
                var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
         | 
| 37 | 
            +
                var data = decryptedStr.toString();
         | 
| 38 | 
            +
                if ((0, utils_1.isJson)(data)) {
         | 
| 39 | 
            +
                    return JSON.parse(data);
         | 
| 40 | 
            +
                }
         | 
| 41 | 
            +
                else {
         | 
| 42 | 
            +
                    return data;
         | 
| 43 | 
            +
                }
         | 
| 44 | 
            +
            }
         | 
| 45 | 
            +
            exports.decrypt = decrypt;
         | 
    
        package/dist/index.d.ts
    CHANGED
    
    | @@ -1,12 +1,2 @@ | |
| 1 | 
            -
            declare const obj:  | 
| 2 | 
            -
                isJson(str: string): any;
         | 
| 3 | 
            -
                $id(id: string): false | HTMLElement;
         | 
| 4 | 
            -
                checkIframeContentHeight(option: import("./types").UeditorHeightOption): string;
         | 
| 5 | 
            -
                getIframeContentHeight(id: string): any;
         | 
| 6 | 
            -
                useLocalStorage(key: string, value: any): false | undefined;
         | 
| 7 | 
            -
                getLocalStorage(key: string): any;
         | 
| 8 | 
            -
                useSessionStorage(key: string, value: any): false | undefined;
         | 
| 9 | 
            -
                getSessionStorage(key: string): any;
         | 
| 10 | 
            -
            };
         | 
| 11 | 
            -
            export { obj };
         | 
| 1 | 
            +
            declare const obj: any;
         | 
| 12 2 | 
             
            export default obj;
         | 
    
        package/dist/index.js
    CHANGED
    
    | @@ -11,9 +11,9 @@ var __assign = (this && this.__assign) || function () { | |
| 11 11 | 
             
                return __assign.apply(this, arguments);
         | 
| 12 12 | 
             
            };
         | 
| 13 13 | 
             
            Object.defineProperty(exports, "__esModule", { value: true });
         | 
| 14 | 
            -
             | 
| 15 | 
            -
            var storage = require("./storage/index");
         | 
| 14 | 
            +
            var storage = require("./storage");
         | 
| 16 15 | 
             
            var utils = require("./utils");
         | 
| 17 | 
            -
            var  | 
| 18 | 
            -
             | 
| 16 | 
            +
            var crypto = require("./crypto");
         | 
| 17 | 
            +
            var weixin = require("./weixin");
         | 
| 18 | 
            +
            var obj = __assign(__assign(__assign(__assign({}, storage), utils), weixin), crypto);
         | 
| 19 19 | 
             
            exports.default = obj;
         | 
    
        package/dist/utils.d.ts
    CHANGED
    
    | @@ -3,3 +3,5 @@ export declare function isJson(str: string): any; | |
| 3 3 | 
             
            export declare function $id(id: string): false | HTMLElement;
         | 
| 4 4 | 
             
            export declare function checkIframeContentHeight(option: UeditorHeightOption): string;
         | 
| 5 5 | 
             
            export declare function getIframeContentHeight(id: string): any;
         | 
| 6 | 
            +
            export declare function deviceEnvironment(): "android" | "ios" | undefined;
         | 
| 7 | 
            +
            export declare function weixinBrowser(): boolean;
         | 
    
        package/dist/utils.js
    CHANGED
    
    | @@ -1,6 +1,6 @@ | |
| 1 1 | 
             
            "use strict";
         | 
| 2 2 | 
             
            Object.defineProperty(exports, "__esModule", { value: true });
         | 
| 3 | 
            -
            exports.getIframeContentHeight = exports.checkIframeContentHeight = exports.$id = exports.isJson = void 0;
         | 
| 3 | 
            +
            exports.weixinBrowser = exports.deviceEnvironment = exports.getIframeContentHeight = exports.checkIframeContentHeight = exports.$id = exports.isJson = void 0;
         | 
| 4 4 | 
             
            function isJson(str) {
         | 
| 5 5 | 
             
                if (!str)
         | 
| 6 6 | 
             
                    return false;
         | 
| @@ -67,3 +67,22 @@ function getIframeContentHeight(id) { | |
| 67 67 | 
             
                return 0;
         | 
| 68 68 | 
             
            }
         | 
| 69 69 | 
             
            exports.getIframeContentHeight = getIframeContentHeight;
         | 
| 70 | 
            +
            function deviceEnvironment() {
         | 
| 71 | 
            +
                var u = navigator.userAgent;
         | 
| 72 | 
            +
                if (u.indexOf('Android') > -1 || u.indexOf('Adr') > -1)
         | 
| 73 | 
            +
                    return "android";
         | 
| 74 | 
            +
                if (!!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/))
         | 
| 75 | 
            +
                    return "ios";
         | 
| 76 | 
            +
            }
         | 
| 77 | 
            +
            exports.deviceEnvironment = deviceEnvironment;
         | 
| 78 | 
            +
            function weixinBrowser() {
         | 
| 79 | 
            +
                var ua = navigator.userAgent.toLowerCase();
         | 
| 80 | 
            +
                var isWeixin = ua.indexOf("micromessenger") != -1;
         | 
| 81 | 
            +
                if (isWeixin) {
         | 
| 82 | 
            +
                    return true;
         | 
| 83 | 
            +
                }
         | 
| 84 | 
            +
                else {
         | 
| 85 | 
            +
                    return false;
         | 
| 86 | 
            +
                }
         | 
| 87 | 
            +
            }
         | 
| 88 | 
            +
            exports.weixinBrowser = weixinBrowser;
         | 
| @@ -0,0 +1,18 @@ | |
| 1 | 
            +
            interface configOption {
         | 
| 2 | 
            +
                appId?: string;
         | 
| 3 | 
            +
                http?: any;
         | 
| 4 | 
            +
                codeKey?: string;
         | 
| 5 | 
            +
                stateKey?: string;
         | 
| 6 | 
            +
                cryptoiv?: string;
         | 
| 7 | 
            +
                cryptokey?: string;
         | 
| 8 | 
            +
            }
         | 
| 9 | 
            +
            export declare function weixinUrlCode(config: configOption): void;
         | 
| 10 | 
            +
            /**
         | 
| 11 | 
            +
             *
         | 
| 12 | 
            +
             * 获取地址栏中的 search 参数,将参数转换为数组,最终返回地址栏参数数组
         | 
| 13 | 
            +
             * keys 需要截取字符串开始字符
         | 
| 14 | 
            +
             */
         | 
| 15 | 
            +
            export declare function formatSearch(search: string | undefined, keys: string): false | any[];
         | 
| 16 | 
            +
            export declare function weixinInit(): any;
         | 
| 17 | 
            +
            export declare function weixinCode(): void;
         | 
| 18 | 
            +
            export {};
         | 
| @@ -0,0 +1,164 @@ | |
| 1 | 
            +
            "use strict";
         | 
| 2 | 
            +
            Object.defineProperty(exports, "__esModule", { value: true });
         | 
| 3 | 
            +
            exports.weixinCode = exports.weixinInit = exports.formatSearch = exports.weixinUrlCode = void 0;
         | 
| 4 | 
            +
            // import * as weixin from 'weixin-js-sdk' // 引入微信SDK
         | 
| 5 | 
            +
            var weixin = require("weixin-js-sdk");
         | 
| 6 | 
            +
            var utils_1 = require("../utils");
         | 
| 7 | 
            +
            var crypto_1 = require("../crypto");
         | 
| 8 | 
            +
            var w = window;
         | 
| 9 | 
            +
            var loc = w.location;
         | 
| 10 | 
            +
            var weixinConfig = {};
         | 
| 11 | 
            +
            function weixinUrlCode(config) {
         | 
| 12 | 
            +
                weixinConfig = config;
         | 
| 13 | 
            +
                // let word:any = {path:"/user/user-agreement",id:222}
         | 
| 14 | 
            +
                // let state:string = encrypt(word) 
         | 
| 15 | 
            +
                var search = loc.search; // ?code=0916SzFa1KH9TA0Ke7Ha1AQx6446SzFr&state=123
         | 
| 16 | 
            +
                // 获取地址栏返回 code 参数
         | 
| 17 | 
            +
                var codeKey = weixinConfig['codeKey'];
         | 
| 18 | 
            +
                //  key:string="weixinCode" 
         | 
| 19 | 
            +
                var localCode = "weixinCode";
         | 
| 20 | 
            +
                if (codeKey)
         | 
| 21 | 
            +
                    localCode = w.sessionStorage.getItem(codeKey);
         | 
| 22 | 
            +
                if (!localCode && search && search.indexOf("code=") != -1) {
         | 
| 23 | 
            +
                    // localCode 不存在, 微信链接中存在 code 用户初次登录系统,
         | 
| 24 | 
            +
                    var urlArr = formatSearch(search, "code=");
         | 
| 25 | 
            +
                    var code = "";
         | 
| 26 | 
            +
                    for (var k = 0; k < urlArr.length; k++) {
         | 
| 27 | 
            +
                        var item = urlArr[k];
         | 
| 28 | 
            +
                        if (item.indexOf("code=") != -1) {
         | 
| 29 | 
            +
                            code = item.substr(5);
         | 
| 30 | 
            +
                            if (codeKey)
         | 
| 31 | 
            +
                                w.sessionStorage.setItem(codeKey, code);
         | 
| 32 | 
            +
                        }
         | 
| 33 | 
            +
                    }
         | 
| 34 | 
            +
                    weixinLogin(code);
         | 
| 35 | 
            +
                }
         | 
| 36 | 
            +
                else if (localCode) {
         | 
| 37 | 
            +
                    // localCode 存在,用户已执行微信跳转链接,执行刷新操作
         | 
| 38 | 
            +
                }
         | 
| 39 | 
            +
                else {
         | 
| 40 | 
            +
                    // 通过分享链接进入系统时,先提取分享链接中的 state
         | 
| 41 | 
            +
                    var hrefState = "";
         | 
| 42 | 
            +
                    if (search && search.indexOf("state=") != -1) {
         | 
| 43 | 
            +
                        var urlArr = formatSearch(search, "state=");
         | 
| 44 | 
            +
                        for (var k = 0; k < urlArr.length; k++) {
         | 
| 45 | 
            +
                            var item = urlArr[k];
         | 
| 46 | 
            +
                            if (item.indexOf("state=") != -1) {
         | 
| 47 | 
            +
                                hrefState = item.substr(6);
         | 
| 48 | 
            +
                                var stateKey = weixinConfig['stateKey'] || "weixinState";
         | 
| 49 | 
            +
                                w.sessionStorage.setItem(stateKey, hrefState);
         | 
| 50 | 
            +
                                w.sessionStorage.setItem("".concat(stateKey, "Obj"), (0, crypto_1.decrypt)(hrefState, cryptoConfig().key, cryptoConfig().iv));
         | 
| 51 | 
            +
                            }
         | 
| 52 | 
            +
                        }
         | 
| 53 | 
            +
                    }
         | 
| 54 | 
            +
                    // 将进入系统所携带的state参数传到微信登录链接,微信跳转登录后原样返回
         | 
| 55 | 
            +
                    weixinCode(); // 微信授权页面获取CODE
         | 
| 56 | 
            +
                }
         | 
| 57 | 
            +
            }
         | 
| 58 | 
            +
            exports.weixinUrlCode = weixinUrlCode;
         | 
| 59 | 
            +
            function cryptoConfig() {
         | 
| 60 | 
            +
                var key = "5uMz4R8rop26DkC8", iv = "5uMz4Rsd0926DkC8";
         | 
| 61 | 
            +
                if (weixinConfig['cryptoiv'])
         | 
| 62 | 
            +
                    iv = weixinConfig.cryptoiv;
         | 
| 63 | 
            +
                if (weixinConfig['cryptokey'])
         | 
| 64 | 
            +
                    key = weixinConfig.cryptokey;
         | 
| 65 | 
            +
                return {
         | 
| 66 | 
            +
                    key: key,
         | 
| 67 | 
            +
                    iv: iv,
         | 
| 68 | 
            +
                };
         | 
| 69 | 
            +
            }
         | 
| 70 | 
            +
            // 执行微信登录
         | 
| 71 | 
            +
            function weixinLogin(code) {
         | 
| 72 | 
            +
                if (!weixinConfig['appId'])
         | 
| 73 | 
            +
                    return;
         | 
| 74 | 
            +
                var obj = {
         | 
| 75 | 
            +
                    "app_id": weixinConfig.appId,
         | 
| 76 | 
            +
                    "js_code": code
         | 
| 77 | 
            +
                };
         | 
| 78 | 
            +
                if (!weixinConfig.http)
         | 
| 79 | 
            +
                    return;
         | 
| 80 | 
            +
                weixinConfig.http(obj).then(function (res) {
         | 
| 81 | 
            +
                    if (res.code == 40163) {
         | 
| 82 | 
            +
                        // 未登录则先执行登录
         | 
| 83 | 
            +
                        return weixinCode();
         | 
| 84 | 
            +
                    }
         | 
| 85 | 
            +
                    var data = res.data;
         | 
| 86 | 
            +
                    for (var key in data) {
         | 
| 87 | 
            +
                        var item = data[key];
         | 
| 88 | 
            +
                        if (item instanceof Object) {
         | 
| 89 | 
            +
                            item = JSON.stringify(item);
         | 
| 90 | 
            +
                        }
         | 
| 91 | 
            +
                        w.sessionStorage.setItem(key, item);
         | 
| 92 | 
            +
                    }
         | 
| 93 | 
            +
                    var url = loc.origin;
         | 
| 94 | 
            +
                    var state = w.sessionStorage.getItem(weixinConfig.key);
         | 
| 95 | 
            +
                    var str = "";
         | 
| 96 | 
            +
                    if (state) {
         | 
| 97 | 
            +
                        var stateObj = (0, crypto_1.decrypt)(state, cryptoConfig().key, cryptoConfig().iv);
         | 
| 98 | 
            +
                        for (var key in stateObj) {
         | 
| 99 | 
            +
                            if (key == "path") {
         | 
| 100 | 
            +
                                if (stateObj[key][0] == "/") {
         | 
| 101 | 
            +
                                    url = "".concat(url, "/#").concat(stateObj.path);
         | 
| 102 | 
            +
                                }
         | 
| 103 | 
            +
                                else {
         | 
| 104 | 
            +
                                    url = "".concat(url, "/#/").concat(stateObj.path);
         | 
| 105 | 
            +
                                }
         | 
| 106 | 
            +
                            }
         | 
| 107 | 
            +
                            else {
         | 
| 108 | 
            +
                                str += "".concat(key, "=").concat(stateObj[key], "&");
         | 
| 109 | 
            +
                            }
         | 
| 110 | 
            +
                        }
         | 
| 111 | 
            +
                        if (stateObj['path']) {
         | 
| 112 | 
            +
                            if (str) {
         | 
| 113 | 
            +
                                url = "".concat(url, "?").concat(str);
         | 
| 114 | 
            +
                            }
         | 
| 115 | 
            +
                        }
         | 
| 116 | 
            +
                        loc.replace(url);
         | 
| 117 | 
            +
                    }
         | 
| 118 | 
            +
                    else {
         | 
| 119 | 
            +
                        loc.replace(url);
         | 
| 120 | 
            +
                    }
         | 
| 121 | 
            +
                });
         | 
| 122 | 
            +
            }
         | 
| 123 | 
            +
            /**
         | 
| 124 | 
            +
             *
         | 
| 125 | 
            +
             * 获取地址栏中的 search 参数,将参数转换为数组,最终返回地址栏参数数组
         | 
| 126 | 
            +
             * keys 需要截取字符串开始字符
         | 
| 127 | 
            +
             */
         | 
| 128 | 
            +
            function formatSearch(search, keys) {
         | 
| 129 | 
            +
                if (search === void 0) { search = ""; }
         | 
| 130 | 
            +
                if (!search)
         | 
| 131 | 
            +
                    return false;
         | 
| 132 | 
            +
                var index = search.indexOf(keys);
         | 
| 133 | 
            +
                search = search.substr(index);
         | 
| 134 | 
            +
                var urlArr = search.split("&");
         | 
| 135 | 
            +
                return urlArr;
         | 
| 136 | 
            +
            }
         | 
| 137 | 
            +
            exports.formatSearch = formatSearch;
         | 
| 138 | 
            +
            function weixinInit() {
         | 
| 139 | 
            +
                var wx = null;
         | 
| 140 | 
            +
                if (w.wx && w.wx.ready) {
         | 
| 141 | 
            +
                    wx = w.wx;
         | 
| 142 | 
            +
                }
         | 
| 143 | 
            +
                else {
         | 
| 144 | 
            +
                    wx = weixin;
         | 
| 145 | 
            +
                }
         | 
| 146 | 
            +
                return wx;
         | 
| 147 | 
            +
            }
         | 
| 148 | 
            +
            exports.weixinInit = weixinInit;
         | 
| 149 | 
            +
            function weixinCode() {
         | 
| 150 | 
            +
                if ((0, utils_1.weixinBrowser)()) {
         | 
| 151 | 
            +
                    if (!weixinConfig['appId'])
         | 
| 152 | 
            +
                        return;
         | 
| 153 | 
            +
                    var base = "https://open.weixin.qq.com/connect/oauth2/authorize?";
         | 
| 154 | 
            +
                    var uri = encodeURIComponent(loc.href.split("?")[0]);
         | 
| 155 | 
            +
                    var config = "appid=".concat(weixinConfig.appId, "&redirect_uri=").concat(uri, "&response_type=code&scope=snsapi_userinfo&state=state#wechat_redirect");
         | 
| 156 | 
            +
                    var wxJumpURL = base + config;
         | 
| 157 | 
            +
                    loc.replace(wxJumpURL);
         | 
| 158 | 
            +
                }
         | 
| 159 | 
            +
                else {
         | 
| 160 | 
            +
                    // 非微信内核浏览器执行其他业务
         | 
| 161 | 
            +
                    console.log("非微信内核浏览器执行其他业务");
         | 
| 162 | 
            +
                }
         | 
| 163 | 
            +
            }
         | 
| 164 | 
            +
            exports.weixinCode = weixinCode;
         | 
    
        package/package.json
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            {
         | 
| 2 2 | 
             
              "name": "xctc-utils",
         | 
| 3 | 
            -
              "version": "1.0. | 
| 4 | 
            -
              "description": "",
         | 
| 3 | 
            +
              "version": "1.0.4",
         | 
| 4 | 
            +
              "description": "LocalStorage存储\r ```\r useLocalStorage(key,value)\r ```\r 获取LocalStorage值\r ```\r getLocalStorage(key)\r ```\r 设备环境:\r ```\r deviceEnvironment() // android ios\r ```\r 是否微信APP环境\r ```\r weixinBrowser() // true false\r ```",
         | 
| 5 5 | 
             
              "main": "./dist/index.js",
         | 
| 6 6 | 
             
              "types": "./dist/index.d.ts",
         | 
| 7 7 | 
             
              "scripts": {
         | 
| @@ -12,6 +12,18 @@ | |
| 12 12 | 
             
              "author": "",
         | 
| 13 13 | 
             
              "license": "ISC",
         | 
| 14 14 | 
             
              "devDependencies": {
         | 
| 15 | 
            +
                "@types/node": "^18.11.19",
         | 
| 15 16 | 
             
                "typescript": "^4.9.5"
         | 
| 16 | 
            -
              }
         | 
| 17 | 
            +
              },
         | 
| 18 | 
            +
              "dependencies": {
         | 
| 19 | 
            +
                "@types/crypto-js": "^4.1.1",
         | 
| 20 | 
            +
                "crypto-js": "^4.1.1",
         | 
| 21 | 
            +
                "weixin-js-sdk": "^1.6.0",
         | 
| 22 | 
            +
                "weixin-js-sdk-ts": "^1.6.1"
         | 
| 23 | 
            +
              },
         | 
| 24 | 
            +
              "repository": {
         | 
| 25 | 
            +
                "type": "git",
         | 
| 26 | 
            +
                "url": "git@gitee.com:npm-management/xctc-utils.git"
         | 
| 27 | 
            +
              },
         | 
| 28 | 
            +
              "keywords": []
         | 
| 17 29 | 
             
            }
         |