UNPKG

yg-tools

Version:

some convenient APIs and Methods for Youngon

299 lines (298 loc) 9.99 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LDKey = void 0; var url_1 = __importDefault(require("../api/url")); var LDK_1 = require("./LDK"); Object.defineProperty(exports, "LDKey", { enumerable: true, get: function () { return LDK_1.LDKey; } }); var YGUtils = /** @class */ (function () { function YGUtils(config, customLDK) { var _this = this; /** * 本地数据存储 * */ this.LocalData = { /** * 获取本地存储的数据 * */ getItem: function (key) { return _this.config.getContext().getStorageSync(key); }, /** * 将 data 存储在本地缓存中指定的 key 中,会覆盖掉原来该 key 对应的内容 * */ setItem: function (key, data) { return _this.config.getContext().setStorageSync(key, data); }, /** * 从本地缓存中同步移除指定 key * */ removeItem: function (key) { return _this.config.getContext().removeStorageSync(key); }, /** * 同步清理本地数据缓存 * */ clear: function () { return _this.config.getContext().clearStorageSync(); } }; /** * 本地数据是否过期 */ this.isDataTimeOut = function (timeout) { var localTimeStamp = _this.LocalData.getItem(LDK_1.LDKey.TIMESTAMP); if (localTimeStamp) { var nowTime = new Date().getTime(); var saveTimeStamp = Number(localTimeStamp); return nowTime - saveTimeStamp > (timeout || LDK_1.TTL); } return true; }; /** * iYoungon 小程序签到详情页面地址 */ this.signDetailPagePath = '/pages/signdetail/index'; /** * 判断不为空 */ this.notEmpty = function (arrs) { var rs = true; for (var i = 0; i < arrs.length; i++) { if (!arrs[i].value) { _this.config.getContext().showToast({ title: arrs[i].key + "\u4E0D\u80FD\u4E3A\u7A7A", icon: 'none', duration: 2000 }); rs = false; break; } } return rs; }; /** * 跳转到小程序首页 */ this.gotoIndex = function () { _this.config.getContext().switchTab({ url: _this.config.getMPIndexPath() }); }; /** * 用户类型标志转换为名称 */ this.utype2Name = function (utype) { var utypeName = '普通用户'; switch (utype) { case 0: break; case 1: utypeName = '实习站员'; break; case 2: utypeName = '正式站员'; break; case 3: utypeName = '往届站员'; break; case 4: utypeName = '管理员'; break; case 5: utypeName = '超级管理员'; break; } return utypeName; }; /** * 职位标志转换为名称 */ this.position2Name = function (position) { var positionName = '站员'; switch (position) { case 0: break; case 1: positionName = '实习部长'; break; case 2: positionName = '实习副站'; break; case 3: positionName = '实习站长'; break; case 4: positionName = '部长'; break; case 5: positionName = '副站'; break; case 6: positionName = '站长'; break; case 7: positionName = '指导老师'; break; } return positionName; }; /** * 性别标志转换为名称 */ this.sex2Name = function (sex) { return sex === 1 ? '男' : sex === 2 ? '女' : '保密'; }; /** * 性别 Map */ this.sexMap = [ { text: '保密', value: 0 }, { text: '男', value: 1 }, { text: '女', value: 2 } ]; /** * 部门 Array */ this.departmentArray = [ '开发部', '企划部', '信息部', '运营部' ]; /** * 用户信息标志转换为名称 */ this.handelUserInfo = function (user) { var userHandeled = __assign(__assign({}, user), { photo: user.photo && user.photo.replace(/\\/, '/'), department: user.department || '天商人', fullhead: "" + _this.config.getBaseUrl() + url_1.default.asset_url + user.head, sexName: _this.sex2Name(Number(user.sex)), utypeName: _this.utype2Name(Number(user.utype)), positionName: _this.position2Name(Number(user.position)) }); return userHandeled; }; /** * 部门颜色 */ this.transDepartMentColor = function (department) { var color = '#2196F3'; switch (department) { case '开发部': color = '#2196F3'; break; case '企划部': color = '#9C7DCF'; break; case '信息部': color = '#FFA500'; break; case '运营部': color = '#4CAF50'; break; case '实习站员': color = '#008B8B'; break; case '天商人': color = '#2db7f5'; break; default: color = '#2196F3'; } return color; }; /** * 对象转 Query 字符串 并过滤空值 */ this.toQuery = function (data) { return Object.entries(data) .filter(function (e) { return String(e[1]).trim() !== ''; }) .map(function (e) { return e[0] + "=" + e[1]; }) .join('&'); }; /** * 检测月、日、时、分、秒是否补零 */ this.checkTime = function (i) { if (String(i).length < 2) { i = "0" + i; } return i; }; /** * 获取当前时间 */ this.getNowTime = function () { var today = new Date(); var weekday = new Array(7); weekday[0] = '星期日'; weekday[1] = '星期一'; weekday[2] = '星期二'; weekday[3] = '星期三'; weekday[4] = '星期四'; weekday[5] = '星期五'; weekday[6] = '星期六'; var year = today.getFullYear(); var month = today.getMonth() + 1; var day = today.getDate(); var h = today.getHours(); var m = today.getMinutes(); var s = today.getSeconds(); month = _this.checkTime(month); day = _this.checkTime(day); h = _this.checkTime(h); m = _this.checkTime(m); s = _this.checkTime(s); var cn = year + '年' + month + '月' + day + '日 ' + h + ':' + m + ':' + s + ' ' + weekday[today.getDay()]; var en = year + "-" + month + "-" + day + " " + h + ":" + m + ":" + s; var ymd = year + "-" + month + "-" + day; var hms = h + ":" + m + ":" + s; return { cn: cn, en: en, ymd: ymd, hms: hms }; }; this.config = config; this.LDK = customLDK || LDK_1.LDKey; } YGUtils.createYGUtils = function (config, customLDK) { return new YGUtils(config, customLDK); }; /** * 函数防抖 */ YGUtils.prototype.debounce = function (func, wait, immediate) { var timeout; return function () { // @ts-ignore var context = this; var args = arguments; if (timeout) clearTimeout(timeout); if (immediate) { var callNow = !timeout; timeout = setTimeout(function () { timeout = null; }, wait); if (callNow) func.apply(context, args); } else { timeout = setTimeout(function () { func.apply(context, args); }, wait); } }; }; return YGUtils; }()); exports.default = YGUtils;