0.1.69 • Published 8 months ago

@kjts20/tool v0.1.69

Weekly downloads
-
License
Apache
Repository
-
Last release
8 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.69

8 months ago

0.1.66

9 months ago

0.1.67

9 months ago

0.1.68

9 months ago

0.1.54

9 months ago

0.1.55

9 months ago

0.1.56

9 months ago

0.1.57

9 months ago

0.1.58

9 months ago

0.1.59

9 months ago

0.1.63

9 months ago

0.1.64

9 months ago

0.1.65

9 months ago

0.1.60

9 months ago

0.1.61

9 months ago

0.1.62

9 months ago

0.1.52

12 months ago

0.1.53

12 months ago

0.1.50

12 months ago

0.1.51

12 months ago

0.1.49

1 year ago

0.1.48

1 year ago

0.1.41

2 years ago

0.1.42

2 years ago

0.1.43

2 years ago

0.1.44

2 years ago

0.1.45

2 years ago

0.1.46

2 years ago

0.1.47

2 years ago

0.1.40

2 years ago

0.1.38

2 years ago

0.1.39

2 years ago

0.1.30

2 years ago

0.1.31

2 years ago

0.1.32

2 years ago

0.1.33

2 years ago

0.1.34

2 years ago

0.1.35

2 years ago

0.1.36

2 years ago

0.1.37

2 years ago

0.1.27

2 years ago

0.1.28

2 years ago

0.1.29

2 years ago

0.1.20

2 years ago

0.1.21

2 years ago

0.1.22

2 years ago

0.1.23

2 years ago

0.1.24

2 years ago

0.1.25

2 years ago

0.1.26

2 years ago

0.1.16

2 years ago

0.1.17

2 years ago

0.1.18

2 years ago

0.1.19

2 years ago

0.1.10

2 years ago

0.1.11

2 years ago

0.1.12

2 years ago

0.1.13

2 years ago

0.1.14

2 years ago

0.1.15

2 years ago

0.1.0

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.9

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.0.97

2 years ago

0.0.98

2 years ago

0.0.84

2 years ago

0.0.85

2 years ago

0.0.86

2 years ago

0.0.87

2 years ago

0.0.88

2 years ago

0.0.89

2 years ago

0.0.83

2 years ago

0.0.95

2 years ago

0.0.96

2 years ago

0.0.90

2 years ago

0.0.91

2 years ago

0.0.92

2 years ago

0.0.93

2 years ago

0.0.94

2 years ago

0.0.80

2 years ago

0.0.81

2 years ago

0.0.82

2 years ago

0.0.73

2 years ago

0.0.74

2 years ago

0.0.75

2 years ago

0.0.76

2 years ago

0.0.77

2 years ago

0.0.78

2 years ago

0.0.79

2 years ago

0.0.70

2 years ago

0.0.71

2 years ago

0.0.72

2 years ago

0.0.67

2 years ago

0.0.68

2 years ago

0.0.69

2 years ago

0.0.66

2 years ago

0.0.65

3 years ago

0.0.64

3 years ago

0.0.63

3 years ago

0.0.62

3 years ago

0.0.61

3 years ago

0.0.60

3 years ago

0.0.59

3 years ago

0.0.58

3 years ago

0.0.57

3 years ago

0.0.56

3 years ago

0.0.54

3 years ago

0.0.53

3 years ago

0.0.52

3 years ago

0.0.51

3 years ago

0.0.50

3 years ago

0.0.49

3 years ago

0.0.48

3 years ago

0.0.47

3 years ago

0.0.46

3 years ago

0.0.45

3 years ago

0.0.44

3 years ago

0.0.43

3 years ago

0.0.42

3 years ago

0.0.41

3 years ago

0.0.40

3 years ago

0.0.39

3 years ago

0.0.38

3 years ago

0.0.37

3 years ago

0.0.36

3 years ago

0.0.35

3 years ago

0.0.34

3 years ago

0.0.33

3 years ago

0.0.32

3 years ago

0.0.31

3 years ago

0.0.30

3 years ago

0.0.29

3 years ago

0.0.27

3 years ago

0.0.26

3 years ago

0.0.24

3 years ago

0.0.22

3 years ago

0.0.21

3 years ago

0.0.19

3 years ago

0.0.18

3 years ago

0.0.17

3 years ago

0.0.16

3 years ago

0.0.15

3 years ago

0.0.13

3 years ago

0.0.12

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.1

3 years ago