学习uniapp自己写的一些公共方法

<script>
	// 一,对对象首字母按A-z进行排序
	//toLowerCase小写,toUpperCase大写(若大写同时改test=ABCDEFGHIJKLMNOPQRSTUVWXYZ)
	//调用:m_sort(data,'py')
	function m_sort(arr,field) {
				var test = 'abcdefghijklmnopqrstuvwxyz';
				var test_arr = {};
				for (var i = 0; i < test.length; i++) {
					test_arr[test[i]] = [];
				}
				//排序
				arr.forEach(function (item,index) {
					//首字母
					test_arr[item[field][0].toLowerCase()].push(item);
				});
				//拼接数据,(若不拼接就是  A:[{...}]这种....,把下面三行删除即可)
				var result = [];
				for (var i = 0; i < test.length; i++) {
					result = result.concat(test_arr[test[i]]);
				}
				return result;
			}
			
	// 二:调试
	//调试用(放在pages.json的globalStyle下面)
		"condition": {	//调试用
			"current": 0,
			"list": [
				{
					"name": "测试",
					"path": "pages/phones/phones",
					"query": "id=1"
				}
			]
		}
	
	// 三,uniapp循环for里给canvas下标      
	:canvas-id="'canvasGauge'+index"
	
	// 四,处理时间
	//获取当前时间往后推三天
			getDay: function(day) {
				var today = new Date();
	
				var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
	
				today.setTime(targetday_milliseconds); //注意,这行是关键代码
	
				var tYear = today.getFullYear();
				var tMonth = today.getMonth();
				var tDate = today.getDate();
				tMonth = this.doHandleMonth(tMonth + 1);
				tDate = this.doHandleMonth(tDate);
				return tYear + '-' + tMonth + '-' + tDate;
			},
			//处理当前时间的月份问题
			doHandleMonth: function(month) {
				var m = month;
				if (month.toString().length == 1) {
					m = '0' + month;
				}
				return m;
			},
			//获取当前时间
			setDateTime: function() {
				var now = new Date();
				var year = now.getFullYear(); //年
				var month = now.getMonth() + 1; //月
				var day = now.getDate(); //日
				var moning;
				var clock = year + '-';
				if (month < 10) clock += '0';
				clock += month + '-';
				if (day < 10) clock += '0';
				clock += day + ' ';
				return clock;
			},
	
	
	
	 //js日期比较(yyyy-mm-dd)  a:当前时间    b :比较时间
			 duibi:function (a, b) {
				var arr = a[0].split("-");
				var starttime = new Date(arr[0], arr[1], arr[2]);
				var starttimes = starttime.getTime();
	
				var arrs = b.split("-");
				var lktime = new Date(arrs[0], arrs[1], arrs[2]);
				var lktimes = lktime.getTime();
	
				if (starttimes > lktimes) {
					return false;
				}
				else
					return true;
			},
			//获取当前时间往后推三天
			getDay: function(day) {
				var today = new Date();
	
				var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
	
				today.setTime(targetday_milliseconds); //注意,这行是关键代码
	
				var tYear = today.getFullYear();
				var tMonth = today.getMonth();
				var tDate = today.getDate();
				tMonth = this.doHandleMonth(tMonth + 1);
				tDate = this.doHandleMonth(tDate);
				return tYear + '-' + tMonth + '-' + tDate;
			},
			//处理当前时间的月份问题
			doHandleMonth: function(month) {
				var m = month;
				if (month.toString().length == 1) {
					m = '0' + month;
				}
				return m;
			},
			//获取时间
			setDateTime: function() {
				var now = new Date();
				var year = now.getFullYear(); //年
				var month = now.getMonth() + 1; //月
				var day = now.getDate(); //日
				var moning;
				var clock = year + '-';
				if (month < 10) clock += '0';
				clock += month + '-';
				if (day < 10) clock += '0';
				clock += day + ' ';
				return clock;
			},
			
			
			//两个时间相差天数 兼容firefox chrome
			    datedifference:function(sDate1, sDate2) {    //sDate1和sDate2是2006-12-18格式  
			        var dateSpan,
			            tempDate,
			            iDays;
			        sDate1 = Date.parse(sDate1);
			        sDate2 = Date.parse(sDate2);
			        dateSpan = sDate2 - sDate1;
			        dateSpan = Math.abs(dateSpan);
			        iDays = Math.floor(dateSpan / (24 * 3600 * 1000));
			        return iDays
			    },
			
			
			//格式化时间yyyy-mm-dd hh-MM-ss
			getDate :function(e){
					var dateee = new Date(e).toJSON();  		//去掉时间里的T
					var date = new Date(+new Date(dateee)+8*3600*1000).toISOString().replace(/T/g,' ').replace(/\.[\d]{3}Z/,'')
					return date;
			},
			
			
			
			//plsql参数如果有时间则加todate否则报错,原因就是由于pl/sql developer的bug,有时候不能将date列格式化
			to_date('2019-10-22','yyyy-mm-dd')
</script>


		
<!-- 自定义数字键盘 -->
<template>
	<!-- =========================自定义键盘(数字)=================== -->
	<view class="content">
		<view class="login">
			<view class="l_top">
				<input class="trade_pwd" disabled="true"  id="targetInput" @click="onkeyboardshow()"  v-model="trade_pwd" />
			</view>
		</view>
		<view :class="[keyboardshow?'keypan':'']">
			<view class="titles"   @tap="closeKeyboard()">
				<image v-show="keyboardshow" class="down_hide_imgge" src="../../static/down_hide.png"></image>
			</view>
			<view class="pan_num_key" v-if="keyboardshow" :class="[items.checked?'pan_num_checked':'pan_num_key']" v-for="(items,index) in boardlists"
			 :key="index" @touchend="writepwd_stop_delete()"   @touchstart="writepwd_delete(items.id)"  @tap="writepwd(items.id)">{{items.con}}</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				keyboardshow: true,
				trade_pwd: '',
				numarr: [],
				boardlists: [{
						id: 1,
						con: 1,
						checked: false
					}, {
						id: 2,
						con: 2,
						checked: false
					}, {
						id: 3,
						con: 3,
						checked: false
					}, {
						id: 4,
						con: 4,
						checked: false
					}, {
						id: 5,
						con: 5,
						checked: false
					}, {
						id: 6,
						con: 6,
						checked: false
					}, {
						id: 7,
						con: 7,
						checked: false
					}, {
						id: 8,
						con: 8,
						checked: false
					}, {
						id: 9,
						con: 9,
						checked: false
					}, {
						id: 10,
						con: "✓",
						checked: false
					}, {
						id: 11,
						con: 0,
						checked: false
					},
					{
						id: 12,
						con: "⋘",
						checked: false
					},

				],
				num_delete:''
			};
		},
		computed: {
		},
		onLoad() {
			//隐藏软键盘
			// uni.hideKeyboard()
		},
		methods: {
			//实现持续删除按键
			writepwd_delete:function(num){
				var _that=this;
				if(num>=12){
					//定时8微秒删除一个数字
					this.num_delete=setInterval(function() {
						_that.close();
					}, 80)
				}
			},
			//自定义键盘条件判断,删除,确定
			writepwd(num) {
				console.log(num);
				if (num == 12) {
					this.close();
					this.boardlists[11].checked = true;
					return;
				}
				if (num == 10) {
					this.setpwd();
					this.boardlists[9].checked = true;
					return;
				}
				if (num == 11) {
					num = 0;
					this.boardlists[10].checked = true;
				} else {
					this.boardlists[num - 1].checked = true;
				}
				this.numarr.push(num);
				var that = this;
				setTimeout(function() {
					for (var i = 0; i < that.boardlists.length; i++) {
						that.boardlists[i].checked = false;
					}
				}, 200)
				this.trade_pwd = this.numarr.join("");
			},
			//隐藏按钮框
			closeKeyboard: function() {
				this.keyboardshow = false;
			},
			//停止定时器
			writepwd_stop_delete:function(){
				clearInterval(this.num_delete);
			},
			//回退方法
			close() {
				this.numarr.pop();
				if (this.trade_pwd.length > 0) {
					this.trade_pwd = this.trade_pwd.substring(0, this.trade_pwd.length - 1);
				}else{
					//取消定时器的删除
					this.writepwd_stop_delete();
				}
			},
			onkeyboardshow:function(){
				this.keyboardshow=true;
			},
			// 确认执行的方法
			setpwd() {
				if (this.trade_pwd.length <=0) {
					uni.showToast({
						image:'',
						title:'亲你的输入为空~',
						duration:1500
					})
					return;
				}
				console.log(this.trade_pwd);
			},
		}
	};
</script>
<style lang="scss" scoped>
	.trade_pwd{
		border: 1upx solid#000;
	}
	.content {
		width: 100%;
		font-size: 28upx;
		background: #fff;
		color: #555;
		height: 100vh;
		font-weight: 400;

		.keypan {
			width: 100%;
			height: 550upx;
			position: fixed;
			left: 0;
			bottom: 0;
			background: #E6E6E6;

			.titles {
				width: 90%;
				height: 58upx;
				background: #E6E6E6;
				display: flex;
				justify-content: space-between;
				line-height: 80upx;
				padding: 0 5%;
				font-size: 32upx;
				color: #292824;

				span {
					width: 100upx;
					height: 70upx;
					display: inline-block;
					padding-left: 30upx;
				}

			}

			.pan_num_key {
				width: 33.3%;
				height: 124upx;
				float: Left;
				text-align: center;
				background: #fff;
				font-size: 60upx;
				line-height: 120upx;
				box-shadow: 0 2upx 5upx rgba(0, 0, 0, 0.5);

			}

			.pan_num_checked {
				width: 33.3%;
				height: 124upx;
				float: Left;
				// margin-left: 2.1%;
				// margin-top: 2.1%;
				text-align: center;
				font-size: 60upx;
				// border-radius: 10upx;
				// font-size: 35upx;
				line-height: 120upx;
				box-shadow: 0 2upx 5upx rgba(0, 0, 0, 0.5);
				animation: checked_bg 0.3s ease;
			}
		}

		.navigation_bar {
			display: flex;
			flex-direction: row;
			align-items: center;
			padding-top: 60upx;

			.back-icon {
				width: 18upx;
				height: 34upx;
			}
		}

		.login {
			width: 100%;
			height: 300upx;

			.l_top {
				width: 700upx;
				height: 200upx;
				margin: 0 auto;
				position: relative;

				.l_text {
					width: 445upx;
					height: 69upx;
					font-size: 28upx;
					text-align: center;
					margin: auto;
					top: 100upx;
					position: relative;

				}

				.lt {
					margin-top: -20upx;
					font-size: 25upx;
					color: #555
				}
			}
		}

		.title {
			color: #616161;
			text-align: center;
			font-size: 30upx;
			margin-bottom: 34upx;
		}

		.mima {

			flex-direction: row;
			width: 680upx;
			height: 78upx;
			margin: 0 auto;
			position: relative;

			.item {
				width: 92upx;
				height: 100%;
				box-sizing: border-box;
				display: flex;
				float: left;
				align-items: center;
				justify-content: center;
				border-bottom: 1upx solid #E5E5E5;
				margin-left: 20upx;

				.line {
					width: 2upx;
					height: 40upx;
					background: #979797;
					animation: shan 1s ease infinite;
				}

				.dot {
					width: 20upx;
					height: 20upx;
					border-radius: 20upx;
					background: black;
				}
			}

			.trade_pwd {
				position: absolute;
				height: 78upx !important;
				width: 480upx;
				opacity: 0;
			}
		}
	}

	@keyframes shan {
		from {
			opacity: 1;
		}

		to {
			opacity: 0;
		}
	}

	@keyframes checked_bg {
		0% {
			background: #fff
		}

		50% {
			background: #D3D1E2
		}

		100% {
			background: #fff
		}
	}
	.down_hide_imgge{
		position: absolute;
		width: 90upx;
		height: 60upx;
		margin-left: 295upx;
	}
</style>


<--删除字符最后一个逗号-->
Warehouse_data = (Warehouse_data.substring(Warehouse_data.length - 1) == ',') ? Warehouse_data.substring(0, Warehouse_data.length - 1) : str;



bootstarp的选择加减框

//选择的物料减按钮
            //{
            //    field: 'supplies_Number', title: "申请数量",
            //    formatter: function (value, row, index) {
            //        var html = "";
            //        html += "";
            //        return html;
            //    },
            //    align: 'center', // 左右居中
            //    valign: 'middle',// 上下居中
            //    width:100
            //},

function btnmin() {
    var i = document.getElementById("quantity").value;
    if (i > 0) {
        i--;
        document.getElementById("quantity").value = i;
    } else {
        i = 0;
        document.getElementById("quantity").value = i;
    }
}
//选择的物料加按钮
function btnadd() { 
        var i = document.getElementById("quantity").value;
        console.log(document.getElementById("quantity").value)
        if (i < document.getElementById("quantity").max) {
            i++;
            document.getElementById("quantity").value = i;
        } else {
            toastr.warning('申请数量不能超过库存数量!');
            return;
        }
}





//数组去重
function Duplicate(List) {
    //根据价格(Id)排序
    function sortprice(a, b) {
        return a.id - b.id
    }
    List.sort(sortprice);
    var res = [List[0]];
    for (var i = 1; i < List.length; i++) {
        if (List[i].id !== res[res.length - 1].id) {
            res.push(List[i]);
        }
    }
    return res;
}

你可能感兴趣的:(uniapp)