brt-utils v1.2.2
brt-utils
智石科技(bright beacon) javascript util库。
NPM 安装
# npm install brt-utils -g
CDN 引入
<script src="//map.brtbeacon.net/js-sdk/plugin/brt-utils.js"></script>
使用
import bUtil from 'brt-utils';
// 获取终端基本信息
bUtil.GetSystem(); // {platform, version, brand, env}
// 获取运行平台信息
bUtil.GetEnv() // wechat、 alipay、 devapp
// 更多
...
API
request
XMLHttpRequest
网络请求, 返回Promise
示例
bUtil.request(options:Object).then(res => {}).catch(err => {});
Options
属性 类型 默认值 必填 说明 url String 是 服务器接口地址 data Object {} 否 请求参数 method String GET 否 请求方式 headers Object {} 否 扩展请求头 responseType String text 否 响应的数据类型 contentType String 否 返回的数据格式 async Boolean true 否 同步/异步 全局配置 configs
// 全局配置 bUtil.request.configs = { // 服务器地址 baseURL: '' // 请求方式 method: 'GET' }
全局拦截器
// 准备 发送请求前 bUtil.request.interceptors.before(config => { console.log('before =>', config); return config; }); // 请求结束后 bUtil.request.interceptors.after(res => { console.log('after =>', res); return res; });
扩展 get/post/put/delete
// GET bUtil.request.get(url:String, data:Object, params:Object).then(res => {}).catch(err => {}); // POST bUtil.request.post(url:String, data:Object, params:Object).then(res => {}).catch(err => {}); // PUT bUtil.request.put(url:String, data:Object, params:Object).then(res => {}).catch(err => {}); // DELETE bUtil.request.delete(url:String, data:Object, params:Object).then(res => {}).catch(err => {});
EventListener
自定义
Event
事件监听示例
// 扩展 Event 事件 class Test extends bUtil.EventListener { constructor () { super(); // 绑定 this.on('ready', msg => { console.log(msg); //log ->: brt utils }); // 触发 this.emit('ready', 'brt utils'); } }
方法
名称 功能说明 on 绑定监听事件 once 绑定监听事件(触发一次后解除绑定) off 解除绑定事件 emit 触发事件 watch 双向绑定属性值 setOptions 属性混入合并
GetParams
获取链接url参数
示例
let url = '//ip:8080?name=张三&age=23'; // name let name = bUtil.GetParams(url, 'name'); // log ->: 张三 // age let age = bUtil.GetParams(url, 'age'); // log ->: 23
默认 location.href
// href location.href = '//ip:8080?name=张三&age=23'; // name let name = bUtil.GetParams('name'); // log ->: 张三 // age let age = bUtil.GetParams('age'); // log ->: 23
GetSystem
获取终端基础信息 返回
Object
示例
bUtil.GetSystem(); //{platform, version, brand, env}
GetEnv
获取运行平台 返回
String
示例
bUtil.GetEnv() // wechat(微信端)、 alipay(支付宝端)、 devapp(其他APP)
ColorMix
颜色混合函数
参数1:16 进制颜色码
参数2:16进制颜色码
参数3:混合比例 0 - 1
示例
// 蓝色与白色混合 20% bUtil.ColorMix('#0099FF', '#FFFFFF', 0.2);
ColorFade
颜色淡化函数
参数1:16 进制颜色码
参数2:透明度 0 - 1
示例
// 透明度 20% bUtil.ColorFade('#0099FF', 0.2);
RandomString
随机字符串
参数: 位数 (默认16位)
示例
// 随机32位的字符串 bUtil.RandomString(32);
DateFormat
日期格式化
示例
// 扩展原型链方式 Date.prototype.format = bUtil.DateFormat; // 调用 new Date().format('YYYY/MM/DD HH:mm:ss'); // 普通方式使用 bUtil.DateFormat(new Date(), 'YYYY/MM/DD HH:mm:ss');
Point
坐标对象
示例
// 格式化 bUtil.Point(1, 2); // to {x: 1, y: 2}; bUtil.Point([1, 2]); // to {x: 1, y: 2}; bUtil.Point({x: 1, y: 2}); // to {x: 1, y: 2};
LngLat
经纬度对象
示例
// 格式化 bUtil.LngLat(1, 2); // to {lng: 1, lat: 2}; bUtil.LngLat([1, 2]); // to {lng: 1, lat: 2}; bUtil.LngLat({lng: 1, lat: 2}); // to {lng: 1, lat: 2};
DebounceFunc
防抖函数 DebounceFunc(fn, delay, immediate)
参数1:函数
参数2:延迟时长(毫秒)
参数3:第一次执行时 是否立即执行
示例
// 随机32位的字符串 const fn = bUtil.DebounceFunc(() => { console.log("debounce"); }, 1000);
CreateElement
创建元素对象 CreateElement(node, content, props, events);
示例
// 随机32位的字符串 bUtil.CreateElement('div', "hello!", {className: "name"}, {click: (e) => {console.log(e)}});
Math
计算两点之间的常用算法。
- Distance
// 计算两点的距离 (墨卡托坐标) bUtil.Math.Distance({x: 1, y: 1}, {x: 10, y: 10}); // 12.72
- LngLatDistance
// 计算两点的距离 (经纬度坐标) bUtil.Math.LngLatDistance({lng: 1, lat: 1}, {lng: 10, lat: 10});
- Angle
// 计算两点的角度(顺时针) bUtil.Math.Angle({x: 1, y: 1}, {x: 10, y: 10}); // 45°
- IntersectionPoint
// 计算点到线的交叉点(吸附点/最近点) let pos = {x: 1, y: 5}; let line1 = {x: 1, y: 1}; let line2 = {x: 10, y: 10}; bUtil.Math.IntersectionPoint(pos, line1, line2); // {x: 3, y: 3}
- RatioPoint
// 计算两点之间 指定比例的点 bUtil.Math.RatioPoint({x: 1, y: 1}, {x: 10, y: 10}, 0.2); // {x: 2.8, y: 2.8}
- AdvancePointByAngle
// 计算点 朝指定方向和距离 偏移的点 bUtil.Math.AdvancePointByAngle({x: 1, y: 1}, 45, 5); // {x: 4.53, y: 4.53}
- LineIntersect
// 计算两个线段的交集点(交叉点) bUtil.Math.LineIntersect([{x: 1, y: 1}, {x: 5, y: 5}], [{x: 5, y: 1}, {x: 1, y: 5}]);
- PointInPolygon
// 计算点是否在多边形内 bUtil.Math.PointInPolygon({x: 2, y: 2}, [{x: 1, y: 1}, {x: 1, y: 5}, {x: 5, y: 5}, {x: 5, y: 1}]);
Coord
> 坐标转换 支持WGS84 (GPS/Google)、GCJ02(高德、腾讯)、BD09(百度) 之间的转换 ##### 示例 ```javascript // GCJ02 to WGS84 bUtil.Coord.GCJ02ToWGS84(longitude, latitude); // WGS84 to GCJ02 bUtil.Coord.WGS84ToGCJ02(longitude, latitude); // BD09 to WGS84 bUtil.Coord.BD09ToWGS84(longitude, latitude); // WGS84 to BD09 bUtil.Coord.WGS84ToBD09(longitude, latitude); // GCJ02 to BD09 bUtil.Coord.GCJ02ToBD09(longitude, latitude); // BD09 to GCJ02 bUtil.Coord.BD09ToGCJ02(longitude, latitude); // 经纬度 to 墨卡托 bUtil.Coord.lngLatToMercator(longitude, latitude); // 墨卡托 to 经纬度 bUtil.Coord.mercatorToLngLat(x, y); ``` ##### 方法 | 名称 | 功能说明 | | - | --- | | GCJ02ToWGS84 | GCJ02转换WGS84 | | WGS84ToGCJ02 | WGS84转换GCJ02 | | BD09ToWGS84 | BD09转换WGS84 | | WGS84ToBD09 | WGS84转换BD09 |
| GCJ02ToBD09 | GCJ02转换BD09 | | BD09ToGCJ02 | BD09转换GCJ02 | | lngLatToMercator | 经纬度转墨卡托 | | mercatorToLngLat | 墨卡托转经纬度 |
ClassMix
es6 语法,class继承扩展语法
示例
// base class Base { constructor () {} } // source class Source { constructor () {} } // 同时继承2个以上的类 class Test ClassMix(Base, Source) { constructor () {} }
3 months ago
3 months ago
4 months ago
5 months ago
6 months ago
6 months ago
9 months ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago