一般我都习惯把公共js封装到util这个文件夹下
常见的的方法,手机号验证,邮箱验证等方法我都会写到util下的util.js里
目录:
util.js代码
//校验邮箱格式
function checkEmail(email) {
return RegExp(/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/).test(
email);
}
//校验手机格式
function checkMobile(mobile) {
return RegExp(/^1[34578]\d{9}$/).test(mobile);
}
module.exports = {
checkEmail: checkEmail,
checkMobile: checkMobile
}
----------------------------------------------------
封装好的方法一定到暴露出来,要不使用的时候是找不到的
首选需要获取手机号,邮箱
邮箱:
验证
手机号:
验证
data中的数据一定要return出来 ,下面是获取不到的
data() {
return {
eml: '',
phone:''
};
},
// 输入邮箱
methods: {
eml_input(e) {
console.log(e);
this.eml = e.detail.value;
},
// 验证邮箱
yz_eml() {
if (this.eml == '') {
uni.showToast({ title: '请输入邮箱', icon: 'none' });
return;
}
if (!until.checkEmail(this.eml)) {
uni.showToast({ title: '邮箱格式错误', icon: 'none' });
return;
}
},
}
使用的时候一点要先将js文件引入
import until from '@/util/util.js';
该方法仅简单验证
下面为请求封装
目录:
// 请求域名
var rqUrl = ''
api.js
// 请求头
var rqUrl = '域名'
// 请求处理
function requst(url, data, post_get) {
return new Promise((resolve, reject) => {
uni.request({
url: this.rqUrl + url,
method: post_get || "GET",
data: data || {},
header: {
'content-type': 'application/json'
},
success: (res) => {
console.log(res)
resolve(res)
},
fail: (res) => {
uni.showToast({
title: "接口请求失败"
})
reject(res)
}
})
})
}
module.exports = {
rqUrl: rqUrl,
requst: requst,
}
---------------------------------------------------
使用方法
data 接口参数
post请求方式
// 请求使用
yz_api() {
let data = {};
reqAPI.requst('接口', data, 'post').then(res => {
console.log('返回数据', res);
});
}
邮箱:
enm验证
手机号:
phone验证
接口验证