编辑商品字符长度限制

master
15386982806 2024-01-16 18:09:11 +08:00
parent 7ba94c7fe1
commit 94e389ab23
1 changed files with 19 additions and 36 deletions

View File

@ -1197,18 +1197,10 @@ export default {
this.$Message.error("已存在相同规格项!");
return;
}
// if (this.zz(0, val) > 20) {
// this.$Message.error("");
// return;
// }
if (this.zz(0, val) > 20) {
this.$Message.error("规格值最多十个字符长度!");
// val = val.toString().slice(0, 4);
this.$forceUpdate();//
// val = this.truncateString(val);
this.skuInfo[index].name = this.truncateString(val);
// this.$set(this.skuInfo[$index].spec_values[index], 'value', this.truncateString(val));
// this.$set(item, 'value', this.truncateString(val));
this.skuInfo[index].name = this.countCharacters(val, 10);
this.$forceUpdate();//
// return;
}
@ -1232,26 +1224,25 @@ export default {
}
return len;
},
truncateString(str) {
let len = str.length; //
if (len <= 10 && /^[\u4e00-\u9fa5]+$/.test(str)) { // 10
countCharacters (defaultStr, defaultNum) {
let str = '' + defaultStr || '',
num = + defaultNum || 0,
res = '',
length = 0;
if (!str || !num) {
return str;
} else {
let count = 0; // UTF-8
for (let i = 0; i < len; i++) {
let charCode = str.charCodeAt(i);
if ((charCode >= 0x0001 && charCode <= 0x007F) || (charCode >= 0xFF60 && charCode <= 0xFF9F)) { // ASCII
count += 1;
} else {
count += 2; // UTF-8ASCII
}
if (count > 10 * 2) { // 10*2
//
for (const i in str) {
res += str[i];
//
length += /[\u4e00-\u9fa5]/.test(str[i]) ? 2 : 1;
//
if (length >= num || +i == str.length - 1) {
break;
}
}
// console.log('', str, str.substr(0, Math.floor((count - 1) / 2)));
return str.substr(0, Math.floor((count + 1) / 2)); //
}
return res;
},
//
skuValueChange(val, index, item, $index) {
@ -1266,18 +1257,10 @@ export default {
if (this.zz(0, val.value) > 20) {
this.$Message.error("规格值最多十个字符长度!");
// val.value = val.value.toString().slice(0, 4);
this.$forceUpdate();//
// val.value = this.truncateString(val.value);
this.skuInfo[$index].spec_values[index].value = this.truncateString(val.value);
// this.$set(this.skuInfo[$index].spec_values[index], 'value', this.truncateString(val.value));
// this.$set(item, 'value', this.truncateString(val.value));
this.skuInfo[$index].spec_values[index].value = this.countCharacters(val.value, 10);
this.$forceUpdate();//
// return;
}
// console.log('', item);
let curVal = this.currentSkuVal;
this.skuTableData = this.skuTableData.map((e) => {
if (e[val.name] === curVal) {