封装原生提示框
// 封装提示框
showToast: function(str, icon) {
uni.showToast({
icon: icon,
title: str,
duration: 2000,
mask: true
});
},
//使用
this.showToast('上传成功', 'success')/第一个参数对应str,第二个参数对应icon
2.监听返回事件onBackPress
//监听返回事件
onBackPress(e) {
console.log(e);//返回e{ "from": "backbutton"}
if (e.from == 'backbutton') {
uni.showModal({
title: '提示',
content: '盘点库数据尚未进行上传,是否要退出此页面?',
cancelText: "去上传", // 取消按钮的文字
confirmText: "退出", // 确认按钮文字
showCancel: true, // 是否显示取消按钮,默认为 true
success: (res) => {
if (res.confirm) {
uni.navigateBack({ //uni.navigateTo跳转的返回,默认1为返回上一级
delta: 1
});
} else {
console.log('去上传数据')
//这里是否要直接上传看业务需求
}
}
})
return true; //阻止默认返回行为
}
},
3.监听APP退出事件
//在代码段末尾添加内容,这个是安卓版本的供参考
// #ifdef APP-PLUS
let main = plus.android.runtimeMainActivity();
// 重写plus.runtime.quit
plus.runtime.quit = function() {
uni.showModal({
//计算删除的数量
title: '提示',
content: '是否要退出应用', //文章内容
cancelText: '取消', //取消按钮的文字
confirmText: '确定', //确定文本的文字
success: function(res) {
if (res.confirm) {
console.log('用户点击了确定')
uni.removeStorage({
key: 'userInfo', //指定移除登录后缓存的user用户信息
success: function(res) {
console.log('清空了用户缓存信息', );
uni.showToast({
title: '退出成功',
icon: 'success',
duration: 500
})
}
})
setTimeout(() => {
main.finish();
}, 600)
} else if (res.cancel) {
console.log('用户点击了取消')
}
}
})
};
// #endif
4.循环校验数据是否存在
var flag = 0 //定义一个状态
for (let i = 0; i < newcodeList.length; i++) {//遍历数组
if (_this.ORcode == newcodeList[i]) {//找到对应的值
flag = 1 //状态改变
}
}
console.log(flag, '状态')
if (flag == 1) {//通过此状态去写自己的逻辑业务
console.log('已有此值')
radio.play_ding_fail() //调取失败声音
uni.showToast({
title: '此条码已重复,请重新扫描',
icon: 'none',
duration: 1000
})
} else if (flag == 0) {
console.log('没有此值')
_this.codeList.push(_this.ORcode)
let uploadUsername = _this.userInfo //拿到存储的操作员名称
let data = {
"matNo": _this.ORcode,
"remark2": _this.Areavalue,
"recCreator": uploadUsername
}
_this.everyRowlist.push(data)
console.log(_this.everyRowlist, '最外层上传数组数据')
radio.play_ding_success() //调取成功声音
console.log(_this.codeList, '去重后的结果赋值', typeof(_this.codeList))
}
5.截取字符串首位及字符串去空格
let codenum = data.code.replace(/\s*/g, "") //正则表达式去除字符串内所有的空格
let codeFirst = codenum.substr(0, 1) //截取第一个字符是'B'
6.uniapp本地同步或异步存储
//同步存储数据
uni.setStorageSync('token',res.data.token)//单独存储token
//同步拿到数据
'Authorization': uni.getStorageSync('token'), //自定义请求头信息携带token
//异步存储数据
uni.setStorage({
key: 'userInfo', //存储用户信息数据
data: res,
success: function() {
console.log('存储成功success');
},
fail: function() {
console.log('存储失败error')
}
});
//异步拿到数据
var _this = this
uni.getStorage({
key: 'localSql',
success: function(res) {
console.log(res, '555555555'),
_this.localData = [...res.data]; //es6的扩展运算符
console.log(this.localData, '本地存储的数据');
},
})
console.log(_this.localData, '本地存储的数据2');
7.数组对象转换成数组
//定义数据
this.BillNumber_Matno = [{
matNo: "B722C0327003"
},
{
matNo: "B722C0327005"
}
]
console.log(this.BillNumber_Matno, '提单号中的材料号')
if(this.BillNumber_Matno!=""){
var str =[];
this.BillNumber_Matno.forEach(function(e) {
console.log(e.matNo)
str.push(e.matNo)
});
console.log(str,'遍历出来的对象')//["B722C0327001","B722C0327002"]
}
8.js精确字符串
function getXSW(s,t){//封装方法,传递两个参数 截取字符串
s =s.toString();
t =t.toString();
let a = 1;
let arr = new Array;
let arr2 = new Array;
let s1 = 1;
let t1 = 1;
arr = s.split('.');//看是几位小数
if(arr.length>1){
s1 = arr[1].length*10;
}
arr2 = t.split('.');
if(arr2.length>1){
t1 = arr2[1].length*10;
}
if(s1>t1 || s1==t1){
a = s1;
}else{
a = t1;
}
return a;
};
//使用方法
Bill_matno.forEach(function(item, index) {
if (codenum == item.BillNo) { //找到该数据
// Billtype = 1
let a = getXSW(_this.totalWeight,item.MAT_Weight)//传递两个参数
_this.totalWeight =(Number(_this.totalWeight)*a + Number(item.MAT_Weight)*a)/a
console.log(_this.totalWeight,'总重量')
// _this.totalWeight = keepTwoDecimal(_this.totalWeight);
console.log(typeof _this.totalWeight); //number
}
})
9.过滤对象数组数据,拿到想要的数据
const data = [{
id: 1,
name: '张三',
age: 15,
},
{
id: 2,
name: 'John',
age: 18,
},
{
id: 3,
name: '李四',
age: 18,
},
{
id: 4,
name: '张三',
age: 19,
},
{
id: 5,
name: 'Jack',
age: 18,
},
{
id: 6,
name: '三',
age: 10,
},
{
id: 7,
name: 'Jack',
age: 18,
},
{
id: 8,
name: 'John',
age: 18,
},
];
let res = data.filter((item) => {
console.log(item)
if (item.name == '张三') {
return item
}
});
console.log(res, '拿到的name==张三的数据')
10.删除指定字符串
let imgurl = this.customFormData.imgArr.replace('data:image/jpeg;base64,','') //截取第一个字符是'B'
console.log(imgurl)
11.遍历对象数组生成新的数组数据
let chooseRow = this.treeValue;//this。treeValue是个数组对象格式,字段不是我们想要的
let rowList = chooseRow.map((item) => ({
rawMaterialId: item,
materialId: this.rowMaterialId,
}));
console.log(rowList)
12.对象数组的每一天对象添加新的字段(非常常用)
let data = [
{
parentid: "0",
processName: "张三",
},
{
parentid: "1394579386803421185",
processName: "钢铁",
},
{
parentid: "1",
processName: "铁荣",
},
{
parentid: "13",
processName: "张三",
},
{
parentid: "22",
processName: "张三",
},
];
let _arr = [];
data.map((item) => {
_arr.push(Object.assign({}, item, { processName1: "" }));
});
console.log(_arr, "新加的字段");
//打印
[
{parentid
:
"0"
processName
:
"张三"
processName1
:
""},
{parentid
:
"1394579386803421185"
processName
:
"钢铁"
processName1
:
""},
]
13.sqlite本地存储文章正在建设筹划中...,请耐心等待。