0.1.47 • Published 6 months ago

@kjts20/tool v0.1.47

Weekly downloads
-
License
Apache
Repository
-
Last release
6 months ago

时时工具包

安装方式

npm install @kjts20/tool

微信小程中使用指引

npm 构建

  • 工具 -> 构建 npm

实例化基础函数

  • 新建 wx.tool.ts,最为全局处理类
import { isNum, isObj, isStr } from '@kjts20/tool';
const logger = wx.getRealtimeLogManager();

// 记录日志
export const sendErr = function (...args) {
    console.warn('发送错误:', ...args);
    logger.error(...args);
};

export const error = function (title, err: any = null, callback?, waitTime?) {
    console.warn('error函数提醒=>', { title, err });
    logger.warn('error函数提醒=>', title, err);
    wx.showToast({
        title: title || '系统开小差了~',
        icon: 'none',
        duration: 2500,
        complete: () => {
            if (typeof callback === 'function') {
                let time = parseInt(waitTime);
                time = isNaN(time) ? 0 : time;
                setTimeout(() => {
                    callback();
                }, time);
            }
        }
    });
};
  • 新建 http-server.ts,作为请求类
import { HttpServer } from '@kjts20/tool';
import { error } from './wx.tool';
const host = '[host]';
const apiPrefix = '[apiPrefix]';
const getToken = () => '[token]';

export default new HttpServer({
    request: wx.request,
    uploadFile: wx.uploadFile,
    error,
    host: host,
    apiPrefix,
    getHeader: () => getToken(),
    responseIntercept(response: HttpResponse): HttpResponse {
        if (isObj(response)) {
            if (response.fail && response.code === 308) {
                // gotoLoginPage();
                throw new Error('重定向到登录页面');
            }
        }
        return response;
    }
});
  • 新建 storage.ts,作为仓库类
import { Storage } from '@kjts20/tool';
import { error } from './wx.tool';
export default new Storage(
    {
        getStorage: wx.getStorage,
        getStorageSync: wx.getStorageSync,
        setStorage: wx.setStorage,
        setStorageSync: wx.setStorageSync,
        clearStorage: wx.clearStorage,
        removeStorage: wx.removeStorage
    },
    error
);
  • 新建 http-filter.ts,作为过滤工具类
import { ResponseFilter } from '@kjts20/tool';
import { error } from './wx.tool';
export default new ResponseFilter({
    error
});

页面使用

import httpServer from '../http-server';
import httpFilter from '../http-filter';
import db from '../storage';

// 请求分页数据
const getPage = function (params) {
    return httpFilter.filter(httpServer.postJson('[url]', params), responseData => {
        // ...
        return responseData;
    });
};
// 保存缓存
const test = function () {
    db.setStorageSync('ttt', 1);
    console.log('保存数据=>' + db.getStorageSync('ttt'));
};

H5 使用指引

使用 axios 初始化 httpServer, utils/http-server.ts 中

import { HttpServer, isObj, toJson } from "@kjts20/tool";
import Axios from 'axios'
const host = 'http://192.168.0.10:7001';
const apiPrefix = '';

export default new HttpServer({
    request(options){
        const {url, data, header, timeout, method, success, error, complete} = options;
        Axios.request({
            url,
            data,
            headers: header,
            timeout,
            method
        }).then(res=>{
            if(isObj(res)){
                if(success){
                    success({
                        ...res,
                        statusCode: res.status
                    });
                }
            }else{
                if(error){
                    error(res);
                }
            }
           if(complete){
                complete(res);
           }
        }).catch(err=>{
            if(error){
                error(err);
            }
            if(complete){
                complete(err);
           }
        });
    },
    uploadFile(options){
        const {url, filePath, formData, header, timeout, method, success, error, complete} = options;
        Axios.request({
            url,
            data: formData,
            headers: header,
            timeout,
            method: "POSt",
        }).then(res=>{
            if(isObj(res)){
                if(success){
                    success({
                        ...res,
                        statusCode: res.status
                    });
                }
            }else{
                if(error){
                    error(res);
                }
            }
           if(complete){
                complete(res);
           }
        }).catch(err=>{
            if(error){
                error(err);
            }
            if(complete){
                complete(err);
           }
        });
    },
    host: host,
    apiPrefix
});

使用 next 初始化请求过滤类, response-filter.ts

import { ResponseFilter  } from "@kjts20/tool";
import { Message } from '@alifd/next';

export default new ResponseFilter({
    error:(msg, err)=>{
        Message.error(msg + '');
        console.error("错误提示=>", err);
    }
});

根据使用平台对几个类进行初始化

  • 浏览器中使用 sessionStorage 实例化仓库
import {CommonStorage ,ISetStorageOptions, IGetStorageOptions, IRemoveStorageOptions, IClearStorageOptions } from '@kjts20/tool';

// 保存json的key
const saveJsonKey = function (key: number | string) {
    return key + 'sskj-json';
};

// 保存localStorage
const setLocalStorage = function (key, data) {
    try {
        let saveKey = key + '';
        let saveData = data;
        if (typeof data !== 'string') {
            saveKey = saveJsonKey(key);
            saveData = JSON.stringify(data);
        }
        localStorage.setItem(saveKey, saveData);
        return null;
    } catch (err) {
        return err;
    }
};

// 获取localStorage
const getLocalStoragee = function (key) {
    const getVal = k => localStorage.getItem(k);
    let val = getVal(saveJsonKey(key));
    if (typeof val === 'string') {
        return JSON.parse(val);
    } else {
        return getVal(key);
    }
};

// 导出默认仓库
export const storage =  new CommonStorage({
    setStorage(options: ISetStorageOptions) {
        const { key, data, success, fail } = options;
        const err = setLocalStorage(key, data);
        if (err === null) {
            if (success) {
                success({ errMsg: 'setStorage:ok', err });
            }
            return true;
        } else {
            if (fail) {
                fail({ errMsg: '保存错误', err });
            }
            return false;
        }
    },
    setStorageSync(key: number | string, data) {
        const err = setLocalStorage(key, data);
        if (err == null) {
            return true;
        } else {
            console.warn('保存sessionStorage错误', err);
            return false;
        }
    },
    getStorage(options: IGetStorageOptions) {
        const { key, success, fail } = options;
        try {
            const data = getLocalStoragee(key);
            if (success) {
                success({ data });
            }
            return true;
        } catch (err) {
            if (fail) {
                fail({ errMsg: '获取值错误', err });
            }
            return false;
        }
    },
    getStorageSync(key: number | string) {
        try {
            return getLocalStoragee(key);
        } catch (err) {
            console.warn('获取sessionStorage错误', err);
            return undefined;
        }
    },
    removeStorage(options: IRemoveStorageOptions) {
        const { key, success, fail } = options;
        try {
            localStorage.removeItem(key + '');
            if (success) {
                success({ errMsg: 'removeStorage:ok' });
            }
            return true;
        } catch (err) {
            if (fail) {
                fail({ errMsg: '获取值错误', err });
            }
            return false;
        }
    },
    clearStorage(options: IClearStorageOptions) {
        const { success, fail } = options;
        try {
            localStorage.clear();
            if (success) {
                success({ errMsg: 'clearStorage:ok' });
            }
            return true;
        } catch (err) {
            if (fail) {
                fail({ errMsg: '获取值错误', err });
            }
            return false;
        }
    }
});

断言使用

// 结合promise使用(异步变同步)
const getDataRes = await Assert.try(async () => await Promise.all([....]), '获取信息错误');
0.1.41

7 months ago

0.1.42

6 months ago

0.1.43

6 months ago

0.1.44

6 months ago

0.1.45

6 months ago

0.1.46

6 months ago

0.1.47

6 months ago

0.1.40

7 months ago

0.1.38

7 months ago

0.1.39

7 months ago

0.1.30

8 months ago

0.1.31

8 months ago

0.1.32

7 months ago

0.1.33

7 months ago

0.1.34

7 months ago

0.1.35

7 months ago

0.1.36

7 months ago

0.1.37

7 months ago

0.1.27

9 months ago

0.1.28

8 months ago

0.1.29

8 months ago

0.1.20

9 months ago

0.1.21

9 months ago

0.1.22

9 months ago

0.1.23

9 months ago

0.1.24

9 months ago

0.1.25

9 months ago

0.1.26

9 months ago

0.1.16

9 months ago

0.1.17

9 months ago

0.1.18

9 months ago

0.1.19

9 months ago

0.1.10

9 months ago

0.1.11

9 months ago

0.1.12

9 months ago

0.1.13

9 months ago

0.1.14

9 months ago

0.1.15

9 months ago

0.1.0

11 months ago

0.1.2

11 months ago

0.1.1

11 months ago

0.1.8

10 months ago

0.1.7

10 months ago

0.1.9

10 months ago

0.1.4

10 months ago

0.1.3

10 months ago

0.1.6

10 months ago

0.1.5

10 months ago

0.0.97

11 months ago

0.0.98

11 months ago

0.0.84

1 year ago

0.0.85

1 year ago

0.0.86

1 year ago

0.0.87

1 year ago

0.0.88

1 year ago

0.0.89

1 year ago

0.0.83

1 year ago

0.0.95

1 year ago

0.0.96

1 year ago

0.0.90

1 year ago

0.0.91

1 year ago

0.0.92

1 year ago

0.0.93

1 year ago

0.0.94

1 year ago

0.0.80

1 year ago

0.0.81

1 year ago

0.0.82

1 year ago

0.0.73

1 year ago

0.0.74

1 year ago

0.0.75

1 year ago

0.0.76

1 year ago

0.0.77

1 year ago

0.0.78

1 year ago

0.0.79

1 year ago

0.0.70

1 year ago

0.0.71

1 year ago

0.0.72

1 year ago

0.0.67

1 year ago

0.0.68

1 year ago

0.0.69

1 year ago

0.0.66

1 year ago

0.0.65

1 year ago

0.0.64

1 year ago

0.0.63

1 year ago

0.0.62

1 year ago

0.0.61

1 year ago

0.0.60

1 year ago

0.0.59

1 year ago

0.0.58

1 year ago

0.0.57

1 year ago

0.0.56

1 year ago

0.0.54

1 year ago

0.0.53

1 year ago

0.0.52

1 year ago

0.0.51

1 year ago

0.0.50

1 year ago

0.0.49

1 year ago

0.0.48

1 year ago

0.0.47

1 year ago

0.0.46

1 year ago

0.0.45

1 year ago

0.0.44

1 year ago

0.0.43

1 year ago

0.0.42

1 year ago

0.0.41

1 year ago

0.0.40

1 year ago

0.0.39

1 year ago

0.0.38

2 years ago

0.0.37

2 years ago

0.0.36

2 years ago

0.0.35

2 years ago

0.0.34

2 years ago

0.0.33

2 years ago

0.0.32

2 years ago

0.0.31

2 years ago

0.0.30

2 years ago

0.0.29

2 years ago

0.0.27

2 years ago

0.0.26

2 years ago

0.0.24

2 years ago

0.0.22

2 years ago

0.0.21

2 years ago

0.0.19

2 years ago

0.0.18

2 years ago

0.0.17

2 years ago

0.0.16

2 years ago

0.0.15

2 years ago

0.0.13

2 years ago

0.0.12

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.1

2 years ago