xctc-utils 1.1.4 → 1.1.6
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 +24 -11
- package/dist/weixin/index.js +14 -12
- package/package.json +2 -2
    
        package/README.md
    CHANGED
    
    | @@ -5,21 +5,27 @@ | |
| 5 5 |  | 
| 6 6 | 
             
            #### LocalStorage使用,存取值时数据已经过处理
         | 
| 7 7 | 
             
            ```
         | 
| 8 | 
            +
             | 
| 8 9 | 
             
            存储: utils.useLocalStorage(key,value)
         | 
| 9 10 | 
             
            取值: utils.getLocalStorage(key)
         | 
| 11 | 
            +
             | 
| 10 12 | 
             
            ```
         | 
| 11 13 |  | 
| 12 14 | 
             
            #### sessionStorage使用,存取值时数据已经过处理
         | 
| 13 15 | 
             
            ```
         | 
| 16 | 
            +
             | 
| 14 17 | 
             
            存储: utils.useSessionStorage(key,value)
         | 
| 15 18 | 
             
            取值: utils.getSessionStorage(key)
         | 
| 19 | 
            +
             | 
| 16 20 | 
             
            ```
         | 
| 17 21 |  | 
| 18 22 | 
             
            #### 获取当前设备环境
         | 
| 19 23 | 
             
            设备环境:
         | 
| 20 24 | 
             
            ```
         | 
| 25 | 
            +
             | 
| 21 26 | 
             
            当前使用设备类型: utils.deviceEnvironment() // android ios
         | 
| 22 27 | 
             
            是否在微信浏览器环境中:  utils.weixinBrowser() // true false
         | 
| 28 | 
            +
             | 
| 23 29 | 
             
            ```
         | 
| 24 30 |  | 
| 25 31 |  | 
| @@ -28,25 +34,28 @@ | |
| 28 34 | 
             
            word: 需要加密的数据,数据可是字符串,对象等
         | 
| 29 35 | 
             
            key: 加密 密钥
         | 
| 30 36 | 
             
            iv:  密钥偏移量
         | 
| 37 | 
            +
            对同一个数据进行加密、解密时,encrypt和decrypt的iv和key保持一致
         | 
| 31 38 | 
             
            对数据进行加密
         | 
| 32 39 | 
             
            utils.encrypt(word:any,key:any,iv:any)
         | 
| 33 40 | 
             
            // 对数据进行解密
         | 
| 34 41 | 
             
            utils.decrypt(word:any,key:any,iv:any)
         | 
| 42 | 
            +
             | 
| 35 43 | 
             
            ```
         | 
| 36 44 | 
             
            #### 微信H5使用方法集合
         | 
| 37 45 | 
             
            ##### 微信授权登录
         | 
| 38 46 | 
             
            ```
         | 
| 39 47 | 
             
            interface configOption {
         | 
| 40 48 | 
             
                appId?:string, // 当前微信服务号 appId
         | 
| 41 | 
            -
                scope?:string,// 网页授权类型
         | 
| 49 | 
            +
                scope?:string,// 网页授权类型 默认 snsapi_userinfo
         | 
| 42 50 | 
             
                http?:any, // 执行微信登录时,请求后端的接口方法,默认请求数据格式 {"app_id":"","js_code":"" },app_id为当前传入的配置参数,js_code为自动截取。
         | 
| 43 | 
            -
                codeKey?:string, //  | 
| 44 | 
            -
                stateKey?:string, //  | 
| 45 | 
            -
                cryptoiv?:string, // 将地址栏携带参数加密iv
         | 
| 46 | 
            -
                cryptokey?:string, // 将地址栏携带参数加密key
         | 
| 51 | 
            +
                codeKey?:string, // 回调地址栏中对 code 进行存储的键, codeKey:code
         | 
| 52 | 
            +
                stateKey?:string, // 回调地址栏中对 state 进行存储的键 stateKey:state
         | 
| 53 | 
            +
                cryptoiv?:string, // 将地址栏携带参数加密iv, 必须与 weixinShareInit 方法中的 iv 参数一致
         | 
| 54 | 
            +
                cryptokey?:string, // 将地址栏携带参数加密key, 必须与 weixinShareInit 方法中的 key 参数一致
         | 
| 47 55 | 
             
            }
         | 
| 48 56 | 
             
            进入系统时,默认调用该方法,自动执行微信跳转授权,回调地址中code处理、state处理,
         | 
| 49 57 | 
             
            utils.weixinUrlCode(config)
         | 
| 58 | 
            +
             | 
| 50 59 | 
             
            ```
         | 
| 51 60 | 
             
            #### 微信config接口权限注入
         | 
| 52 61 | 
             
            ```
         | 
| @@ -63,13 +72,13 @@ utils.weixinShareConfig(config:ShareConfig) | |
| 63 72 | 
             
            #### 微信分享接口加载
         | 
| 64 73 | 
             
            ```
         | 
| 65 74 | 
             
            interface Share{
         | 
| 66 | 
            -
                title?:string, // 分享标题
         | 
| 67 | 
            -
                desc?:string, // 分享描述
         | 
| 68 | 
            -
                link: '', // 分享链接,该链接域名或路径必须与当前页面对应的公众号 JS  | 
| 75 | 
            +
                title?:string, // 分享标题 默认值 微信分享
         | 
| 76 | 
            +
                desc?:string, // 分享描述 默认值 微信分享描述
         | 
| 77 | 
            +
                link: '', // 分享链接,该链接域名或路径必须与当前页面对应的公众号 JS 安全域名一致,默认取当前访问地址
         | 
| 69 78 | 
             
                imgUrl: '', // 分享图标
         | 
| 70 | 
            -
                data?:any,//微信分享时需要携带的数据,默认传键值格式
         | 
| 71 | 
            -
                iv?:string, // 分享链接中对 state 数据加密的iv
         | 
| 72 | 
            -
                key?:string, // 分享链接中对 state 数据加密的 key
         | 
| 79 | 
            +
                data?:any,//微信分享时需要携带的数据,默认传键值格式 {id:1,name:"test",path:"微信分享跳转地址,如: /user/login,好友点击后自动跳转到/user/login页面"},系统自动将该数据保存到缓存中,同时将数据拼接到地址栏,跳转 path 页面后可通过地址栏获取拼接数据
         | 
| 80 | 
            +
                iv?:string, // 分享链接中对 state 数据加密的iv 必须与 weixinUrlCode 方法中的 cryptoiv 参数一致
         | 
| 81 | 
            +
                key?:string, // 分享链接中对 state 数据加密的 key 必须与 weixinUrlCode 方法中的 cryptokey 参数一致
         | 
| 73 82 | 
             
            }
         | 
| 74 83 | 
             
            utils.weixinShareInit(config:Share)
         | 
| 75 84 | 
             
            ```
         | 
| @@ -77,7 +86,9 @@ utils.weixinShareInit(config:Share) | |
| 77 86 | 
             
            ### 时间相关方法
         | 
| 78 87 | 
             
            #### 获取当前时间的时间戳
         | 
| 79 88 | 
             
            ```
         | 
| 89 | 
            +
             | 
| 80 90 | 
             
            utils.getTime() // 返回秒
         | 
| 91 | 
            +
             | 
| 81 92 | 
             
            ```
         | 
| 82 93 | 
             
            #### 时间戳转任意格式时间字符串
         | 
| 83 94 | 
             
            ```
         | 
| @@ -88,6 +99,7 @@ utils.getTime() // 返回秒 | |
| 88 99 | 
             
             * @returns 
         | 
| 89 100 | 
             
             */
         | 
| 90 101 | 
             
            utils.formatTimeStamp(num,format) // 返回 format 格式时间戳
         | 
| 102 | 
            +
             | 
| 91 103 | 
             
            ```
         | 
| 92 104 | 
             
            #### 时间字符串转时间戳
         | 
| 93 105 | 
             
            ```
         | 
| @@ -97,4 +109,5 @@ utils.formatTimeStamp(num,format) // 返回 format 格式时间戳 | |
| 97 109 | 
             
             * @returns 
         | 
| 98 110 | 
             
             */
         | 
| 99 111 | 
             
            utils.formatStrTime(str) // 返回时间戳 秒
         | 
| 112 | 
            +
             | 
| 100 113 | 
             
            ```
         | 
    
        package/dist/weixin/index.js
    CHANGED
    
    | @@ -5,6 +5,7 @@ exports.weixinPay = exports.weixinShareInit = exports.weixinShareConfig = export | |
| 5 5 | 
             
            var weixin = require("weixin-js-sdk");
         | 
| 6 6 | 
             
            var utils_1 = require("../utils");
         | 
| 7 7 | 
             
            var crypto_1 = require("../crypto");
         | 
| 8 | 
            +
            var index_1 = require("../storage/index");
         | 
| 8 9 | 
             
            var w = window;
         | 
| 9 10 | 
             
            var loc = w.location;
         | 
| 10 11 | 
             
            var defaultKey = "5uMz4R8rop26DkC8";
         | 
| @@ -18,7 +19,7 @@ function weixinUrlCode(config) { | |
| 18 19 | 
             
                var codeKey = weixinConfig['codeKey'];
         | 
| 19 20 | 
             
                var localCode = "";
         | 
| 20 21 | 
             
                if (codeKey)
         | 
| 21 | 
            -
                    localCode =  | 
| 22 | 
            +
                    localCode = (0, index_1.getSessionStorage)(codeKey);
         | 
| 22 23 | 
             
                if (!localCode && search && search.indexOf("code=") != -1) {
         | 
| 23 24 | 
             
                    // localCode 不存在, 微信链接中存在 code 用户初次登录系统,
         | 
| 24 25 | 
             
                    var urlArr = formatSearch(search, "code=");
         | 
| @@ -28,7 +29,7 @@ function weixinUrlCode(config) { | |
| 28 29 | 
             
                        if (item.indexOf("code=") != -1) {
         | 
| 29 30 | 
             
                            code = item.substr(5);
         | 
| 30 31 | 
             
                            if (codeKey)
         | 
| 31 | 
            -
                                 | 
| 32 | 
            +
                                (0, index_1.useSessionStorage)(codeKey, code);
         | 
| 32 33 | 
             
                        }
         | 
| 33 34 | 
             
                    }
         | 
| 34 35 | 
             
                    weixinLogin(code);
         | 
| @@ -46,11 +47,11 @@ function weixinUrlCode(config) { | |
| 46 47 | 
             
                            if (item.indexOf("state=") != -1) {
         | 
| 47 48 | 
             
                                hrefState = item.substr(6);
         | 
| 48 49 | 
             
                                var stateKey = weixinConfig['stateKey'] || "weixinState";
         | 
| 49 | 
            -
                                 | 
| 50 | 
            +
                                (0, index_1.useSessionStorage)(stateKey, hrefState);
         | 
| 50 51 | 
             
                                var stateObj = (0, crypto_1.decrypt)(hrefState, cryptoConfig(cryptokey).key, cryptoConfig(cryptoiv).iv);
         | 
| 51 52 | 
             
                                if (stateObj instanceof Object)
         | 
| 52 53 | 
             
                                    stateObj = JSON.stringify(stateObj);
         | 
| 53 | 
            -
                                 | 
| 54 | 
            +
                                (0, index_1.useSessionStorage)("".concat(stateKey, "Obj"), stateObj);
         | 
| 54 55 | 
             
                            }
         | 
| 55 56 | 
             
                        }
         | 
| 56 57 | 
             
                    }
         | 
| @@ -71,13 +72,14 @@ function cryptoConfig(key, iv) { | |
| 71 72 | 
             
            }
         | 
| 72 73 | 
             
            // 执行微信登录
         | 
| 73 74 | 
             
            function weixinLogin(code) {
         | 
| 74 | 
            -
                 | 
| 75 | 
            +
                var cryptoiv = weixinConfig.cryptoiv, cryptokey = weixinConfig.cryptokey, appId = weixinConfig.appId, http = weixinConfig.http, stateKey = weixinConfig.stateKey;
         | 
| 76 | 
            +
                if (!appId)
         | 
| 75 77 | 
             
                    return;
         | 
| 76 78 | 
             
                var obj = {
         | 
| 77 | 
            -
                    "app_id":  | 
| 79 | 
            +
                    "app_id": appId,
         | 
| 78 80 | 
             
                    "js_code": code
         | 
| 79 81 | 
             
                };
         | 
| 80 | 
            -
                if (! | 
| 82 | 
            +
                if (!http)
         | 
| 81 83 | 
             
                    return;
         | 
| 82 84 | 
             
                weixinConfig.http(obj).then(function (res) {
         | 
| 83 85 | 
             
                    if (res.code == 40163) {
         | 
| @@ -90,13 +92,13 @@ function weixinLogin(code) { | |
| 90 92 | 
             
                        if (item instanceof Object) {
         | 
| 91 93 | 
             
                            item = JSON.stringify(item);
         | 
| 92 94 | 
             
                        }
         | 
| 93 | 
            -
                         | 
| 95 | 
            +
                        (0, index_1.useSessionStorage)(key, item);
         | 
| 94 96 | 
             
                    }
         | 
| 95 97 | 
             
                    var url = loc.origin;
         | 
| 96 | 
            -
                    var state =  | 
| 98 | 
            +
                    var state = (0, index_1.getSessionStorage)(stateKey);
         | 
| 97 99 | 
             
                    var str = "";
         | 
| 98 100 | 
             
                    if (state) {
         | 
| 99 | 
            -
                        var stateObj = (0,  | 
| 101 | 
            +
                        var stateObj = (0, index_1.getSessionStorage)("".concat(stateKey, "Obj"));
         | 
| 100 102 | 
             
                        for (var key in stateObj) {
         | 
| 101 103 | 
             
                            if (key == "path") {
         | 
| 102 104 | 
             
                                if (stateObj[key][0] == "/") {
         | 
| @@ -213,7 +215,7 @@ function weixinShareInit(options) { | |
| 213 215 | 
             
                var desc = "微信分享描述";
         | 
| 214 216 | 
             
                if (options === null || options === void 0 ? void 0 : options.desc)
         | 
| 215 217 | 
             
                    desc = options.desc;
         | 
| 216 | 
            -
                var link = loc. | 
| 218 | 
            +
                var link = loc.origin;
         | 
| 217 219 | 
             
                if (options === null || options === void 0 ? void 0 : options.link)
         | 
| 218 220 | 
             
                    link = options.link;
         | 
| 219 221 | 
             
                var imgUrl = "";
         | 
| @@ -229,7 +231,7 @@ function weixinShareInit(options) { | |
| 229 231 | 
             
                }
         | 
| 230 232 | 
             
                var wx = weixinInit();
         | 
| 231 233 | 
             
                // console.log("shareUrl===",link)
         | 
| 232 | 
            -
                 | 
| 234 | 
            +
                (0, index_1.useSessionStorage)("shareUrl", link);
         | 
| 233 235 | 
             
                wx.ready(function () {
         | 
| 234 236 | 
             
                    //需在用户可能点击分享按钮前就先调用
         | 
| 235 237 | 
             
                    wx.updateAppMessageShareData({
         | 
    
        package/package.json
    CHANGED
    
    | @@ -1,6 +1,6 @@ | |
| 1 1 | 
             
            {
         | 
| 2 2 | 
             
              "name": "xctc-utils",
         | 
| 3 | 
            -
              "version": "1.1. | 
| 3 | 
            +
              "version": "1.1.6",
         | 
| 4 4 | 
             
              "description": "localStorage存储\r ```\r sessionStorage存储\r ```\r crypto-js加密、解密\r ```\r 微信授权登录、微信分享\r ```\r 设备环境获取\r ```\r 是否是微信浏览器\r ```\r 时间戳转时间,字符串转时间戳",
         | 
| 5 5 | 
             
              "main": "./dist/index.js",
         | 
| 6 6 | 
             
              "types": "./dist/index.d.ts",
         | 
| @@ -24,5 +24,5 @@ | |
| 24 24 | 
             
                "type": "git",
         | 
| 25 25 | 
             
                "url": "git@gitee.com:npm-management/xctc-utils.git"
         | 
| 26 26 | 
             
              },
         | 
| 27 | 
            -
              "keywords": [ "localStorage","sessionStorage","crypto-js","微信授权登录","微信H5"," | 
| 27 | 
            +
              "keywords": [ "localStorage","sessionStorage","crypto-js","微信授权登录","weixin-js-sdk","微信H5","时间戳时间字符串互转" ]
         | 
| 28 28 | 
             
            }
         |