编辑商品字符长度限制

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