Skip to content

Paddies/tools

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

常用js工具库

一.使用方法

1.原生js中使用
1.引入模块
2.调用模块抛出的方法
<script type="module">
	import tools from "./tools.js"
	tools.typeOf("123")
</script>
2.在vue中使用
1.main中引入模块
2.挂载在vue的原型链上
import p from './libs/tools.js';
Vue.prototype.p=p;

组件中使用
this.p.typeOf("123")

二.包含方法

序号 方法名 描述
1 Ajax 原生封装ajax请求,内部封装promise
2 typeOf 返回数据类型
3 setCookie 设置cookies值
4 getCookie 获取cookies值
5 delCookie 删除cookies值
6 isPhoneNumber 判断是否电话号码(包括手机号和固话)
7 isMobileUserAgent 判断是否移动端设备
8 isAndroidMobileDevice 判断是否安卓设备
9 isAppleMobileDevice 判断是否ios设备
10 uniqueId 随机数时间戳
11 timeFormat 时间个性化输出
12 transform 金额大写装换
13 getQueryString 提取URL参数
14 websocket 使用websocket

三.方法例子

Ajax

Ajax(method, url, data, flag)
method:方法("post"/"get"
url:请求地址
data:请求数据
this.p.Ajax("post","http://192.168.2.2/api/login",{
	"user_name":"admin",
	"user_password":"123",
}).then(function(data){
	//成功返回值
	console.log(data)
}).catch(function(err){
	//错误返回值
	console.log(err)
})

typeOf

this.p.typeOf("123")

setCookie

setCookie(name, value, Hours)
name:cookies名称
value:cookies值
Hours:有效时间(单位:小时)
this.p.setCookie("name","abc",3)

getCookie

getCookie(name)
name:cookies名称
this.p.getCookie("name")

delCookie

delCookie(name)
name:cookies名称
this.p.delCookie("name")

isPhoneNumber

isPhoneNumber(v,t)
v:号码
t:标识("phone":检测手机格式,"tel":检测固话格式,无则符合手机号或固话即可)
this.p.isPhoneNumber("13248979","phone")

isMobileUserAgent

this.p.isMobileUserAgent()

isAndroidMobileDevice

this.p.isAndroidMobileDevice()

isAppleMobileDevice

this.p.isAppleMobileDevice()

uniqueId

this.p.uniqueId()

timeFormat

timeFormat(time)
time:时间戳
this.p.timeFormat(1619321487923)

transform

transform(tranvalue)
tranvalue:数值字符串
this.p.transform("120.5")

getQueryString

getQueryString(url, param)
url:url地址
param:参数名
this.p.getQueryString("http://192.xxxxx","name")

websocket

let ws = new WebSocket("ws://xxxxxx");
this.p.setWs(ws);
this.p.ws.onopen = function() {
	 console.log("打开websocket")
}
this.p.ws.onerror = function(ev) {

}
this.p.ws.onclose = function(ev) {
	 // 关闭 websocket
	console.log("连接已关闭...");
}
this.p.ws.onmessage = function(res) {
	console.log("App.vue收到服务器内容", res.data);
}

//发送消息时
if(this.p.ws.readyState == 1){
	console.log('发送keepalve')
	this.p.sendMsg({
		"type":"keepalive"
	})
}

About

js tools

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors