uniapp常用api整合

站长软件开发5个月前技术分享81
uniapp常用api整合

每次使用uniapp开发都有一些常用的api需要调用,直接写官方的api显的代码很杂乱,重复的太多,于是把这些常用的api整理起来,作为常用的工具挂载到vue对象上,这样在任何页面都可以用this获取使用,非常方便,非常实用的uniapp使用技巧,大家看到了也可以根据个人使用习惯进行增删,这样每次新开项目时,都可以直接复制过去使用,增加开发效率!

let global={
	domain:"https://www.xxxx.com",
	post(url,data){
		/* url接口,data为参数 */
		var self=this;
		var requesturl=self.domain+url;
		let sysinfo=uni.getSystemInfoSync();
		let appVersionCode=sysinfo.appVersionCode;
		return new Promise((response,reject)=>{
			uni.request({
				url: requesturl,
				method:'POST',
				header:{
					'content-type':'application/x-www-form-urlencoded',
					'appVersionCode':appVersionCode,
				},
				data:data,
				success: (res) => {
					response(res.data);
				},
				fail: (res) => {
					reject(res);
					self.toast('网络错误!');
				}
			})
		})
	},
	toast(msg,time){
		var time=time||2000;
		uni.showToast({
			icon:'none',
			title: msg,
			duration: time
		});
	},
	go(url,flag){
		if(flag==1){
			uni.redirectTo({
				url:url
			})
		}else{
			uni.navigateTo({
				url:url
			})
		}
	},
	back(){
		var page=getCurrentPages();
		if(page.length==1){
			uni.switchTab({
				url:"/pages/index/index"
			})
		}else{
			uni.navigateBack({
				delta:1
			})
		}
	},
	selimg(){
		var self=this;
		self.choose(1).then(res=>{
		    self.upload({
			    url: '/plugin.php?id=xiaoxiangce&type=6',
			    file: res[0]
		    }).then((res1) => {
			let filePath=res1.filePath;
		    });
		}).catch(err=>{
					
		})
	},
	choose(num) {
		return new Promise((resolve, reject) => {
			uni.chooseImage({
				count: num,
				success(res) {
					// console.log(res);
					// 缓存文件路径
					resolve(res.tempFilePaths)
				},
				fail(err) {
					console.log(err)
					reject(err)
				}
			})
		})
	},
	upload(options){
		var self=this;
		var requesturl=self.domain+options.url;
		return new Promise(function (resolve, reject) {
		    uni.uploadFile({
		        url: requesturl,
			fileType: 'image',
		        filePath: options.file,
		        name:'file',
		        formData:{},
		        success: function (response) {
		            const res = JSON.parse(response.data);
		            resolve(res);
		        },
		        fail: function (err) {
		            //util.message('网络错误', 'error');
		            reject(err);
		        },
		        complete: function (res) {
		            
		        }
		    });
		});
	},
	parsetime(timestamp){//时间戳转日期格式
		var timelength=timestamp.toString().length;
		function add0(m){return m<10?'0'+m:m };
		var date=new Date(timestamp);
		if(timelength==10){
			date = new Date(timestamp * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
		}
		var Y = date.getFullYear() + '-';
		var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
		var D = date.getDate() + ' ';
		var h = add0(date.getHours()) + ':';
		var m = add0(date.getMinutes()) + ':';
		var s = date.getSeconds();
		return Y+add0(M)+add0(D);
	},
	getitem(name){
		return uni.getStorageSync(name);
	},
	setitem(name,val){
		uni.setStorageSync(name,val);
	},
	removeitem(name){
		uni.removeStorageSync(name);
	},
	copy(str){
		var self=this;
		uni.setClipboardData({
		    data: str+'',
		    success: function () {
				self.toast("已复制")
		    }
		});
	},
	validphone(phone){
		var self=this;
		if(phone==""){
			self.toast('请输入手机号码');
			return false; 
		}
		if(!(/^1[3456789]\d{9}$/.test(phone))){
			self.toast('手机号码格式有误');
			return false; 
		}else{
			return true;
		}
	},
}
module.exports=global;

//import global from './common/global.js';//引用
//Vue.prototype.$go = global


联系站长:

相关文章

【JavaScript】js获取当前年月日周

【JavaScript】js获取当前年月日周

【JavaScript】js获取当前年月日周function getcurtime(){     const addZero =...

js使用正则表达式获取html字符串中的img标签的src组成数组

js使用正则表达式获取html字符串中的img标签的src组成数组

js使用正则表达式获取html字符串中的img标签的src组成数组exec() 方法用于检索字符串中的正则表达式的匹配。如果字符串中有匹配的值返回该匹配值,否则返回 null。var htm...

Animate.css使用方法,及源码下载,包含中文文档教程-css3动画演示合集,可用于uniapp

Animate.css使用方法,及源码下载,包含中文文档教程-css3动画演示合集,可用于uniapp

Animate.css是一个即用型跨浏览器动画库,可在您的 Web 项目中使用。非常适合强调、主页、滑块和注意力引导提示。注意:Animate.css可以在uniapp中使用,并且兼容多端,源码放页面...

js实用函数之map()函数的使用,一分钟学会

js实用函数之map()函数的使用,一分钟学会

重点:map() 方法定义在JavaScript的Array中,它返回一个新的数组,数组中的元素为原始数组调用函数处理后的值。值得注意的是:1、map()函数不会对空数组进行检测;2、map...

原生js把时间戳转为日期格式年月日时分秒

原生js把时间戳转为日期格式年月日时分秒

原生js把时间戳转为日期格式年月日时分秒function parsetime(timestamp){     let timelength...

uniapp富文本编辑器editor的使用,复制可用

uniapp富文本编辑器editor的使用,复制可用

关于uniapp富文本编辑器editor的使用,我会把HTML,css,js三部分代码都完整列出来,其实也是一个总结,因为官方教程有些不是很清楚,这里总结一下,方便大家使用:1、html部分:<...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。