console
// 将全局模态窗口挂载到 vue 实例中
Vue.prototype.$XModal = VXETable.modal
var listData = JSON.parse(listJson);
var localList, localIdlist;
const localAtrrArr = ['priceBase', 'bootFloorPrice', 'floorPrice', 'step', 'update', 'remark'];
const originAtrrArr = ['id', 'item_id', 'item_name', 'tag', 'money_min_sell_auto', 'money_min_sell_self']
var Main = {
data() {
return {
tableData: [],
formData: {},
taskStartTime: parseInt(localStorage.taskStartTime || 1),
taskEndTime: parseInt(localStorage.taskEndTime || 1),
interval: parseInt(localStorage.interval || 10),
runTask: true,
priceBaseList: [
{ label: '关闭', value: '0' },
{ label: '自动', value: '1' },
{ label: '人工', value: '2' }
],
loading: false
}
},
created() {
if (localStorage.tableData) {
localList = JSON.parse(localStorage.tableData);
localIdlist = localList.map(v => v.id);
}
if (this.taskEndTime && this.taskEndTime > new Date()) {
// this.runTask=true;
} else {
this.runTask = false;
}
this.load();
setInterval(() => {
if (this.taskEndTime <= new Date()) {
this.runTask = false
}
}, 60000);
},
watch: {
runTask(val) {
if (!val) return;
let p = prompt('请输入运行时长和改价间隔(单位分钟),例:600,10', localStorage.duration + ',' + this.interval);
if (!p || !p.trim()) return;
let arr = p.trim().split(/(?:,|,)/);
let duration = parseInt(arr[0]);
this.taskStartTime = +new Date();
this.taskEndTime = +new Date() + duration * 60000;
this.interval = parseInt(arr[1]);
localStorage.duration = duration;
localStorage.taskStartTime = this.taskStartTime;
localStorage.taskEndTime = this.taskEndTime;
localStorage.interval = this.interval;
}
},
methods: {
editClosed(...args) {
console.log('editClosed');
},
cellStyle({ row, rowIndex, column, columnIndex }) {
if (column.property === 'bootFloorPrice' || column.property === 'floorPrice') {
let val = row[column.property];
if (val && row.priceBase != '0' && val >= (row.priceBase == '2' ? row.money_min_sell_self : row.money_min_sell_auto)) {
return {
backgroundColor: 'RGB(255,0,0,0.1)'
}
}
}
},
batchSetPriceBase() {
let selectedRows = this.$refs.xTable.getCheckboxRecords();
if (selectedRows.length === 0) {
this.$XModal.message({ message: '未选择任何记录', status: 'error' });
return;
}
let p = prompt('请输入要修改定价依据的值,0关闭,1自动,2人工');
if (!p) return;
p = p.trim();
if (!p || ['0', '1', '2'].indexOf(p) == -1) {
this.$XModal.message({ message: '输入值不合法', status: 'error' });
return;
}
selectedRows.map(row => row.priceBase = p);
this.$XModal.message({ message: '已将选中记录定价依据设为' + p, status: 'success' });
localStorage.tableData = JSON.stringify(this.tableData);
},
load(loadRows) {
this.loading = true;
let tempList = [];
if (loadRows) {
for (let i = 0; i < listData.length; i++) {
originAtrrArr.map(key => {
loadRows[i][key] = listData[i][key];
});
loadRows[i].price = listData[i].price / 100;
loadRows[i].wear = listData[i].item_info.wear;
}
} else {
for (let i = 0; i < listData.length; i++) {
let obj = {};
originAtrrArr.map(key => obj[key] = listData[i][key]);
obj.item_id = '<a href="https://www.c5game.com/csgo/' + listData[i].item_id + '/S.html" target="_blank">查看</a>'
obj.price = listData[i].price / 100;
obj.wear = listData[i].item_info.wear;
obj['priceBase'] = '1';
obj['step'] = 0.01;
if (localList) {
let index = localIdlist.indexOf(listData[i].id)
if (index > -1) {
localAtrrArr.map(key => obj[key] = localList[index][key]);
// if (localList[index].price != obj.price) obj.update = new Date().getTime();
} else {
for (let j = localList.length - 1; j >= 0; j--) {
if (localList[j].item_id === listData[i].item_id) {
obj.bootFloorPrice = localList[j].bootFloorPrice;
break;
}
}
}
}
tempList.push(obj);
}
this.tableData = tempList;
}
this.loading = false;
this.$XModal.message({ message: '刷新成功', status: 'success' });
},
changePriceTask() {
let changeRows = [];
let changeArr = this.tableData.reduce((t, row, index) => {
if (!row.priceBase || row.priceBase == '0' || !row.step) return t;
if (!row.bootFloorPrice && !row.floorPrice) return t;
let sysMinPrice = row.priceBase == '2' ? row.money_min_sell_self : row.money_min_sell_auto;
if (row.price <= sysMinPrice) return t;
let newPrice = sysMinPrice - row.step;
let minFloorPrice = parseFloat(row.floorPrice || row.bootFloorPrice);
if (newPrice < minFloorPrice) {
if (minFloorPrice <= sysMinPrice) {
newPrice = minFloorPrice;
} else return t;
}
console.log(newPrice);
let obj = {};
obj.seq = index + 1;
obj['id[]'] = row.id;
obj['price[]'] = newPrice.toFixed(2);
obj['memo[]'] = row.remark;
changeRows.push(row);
t.push(obj);
return t;
}, []);
if (changeArr.length === 0) {
console.log('无可改价商品');
return;
}
changeRows.map(row => row.update = moment().format("MM-DD HH:mm:ss"));
// localStorage.tableData = JSON.stringify(this.tableData);
tip(changeArr.map(v => v.seq + ':' + v['price[]']).join(','));
},
editRowEvent(row) {
let currentActiveRecord = this.$refs.xTable.getActiveRecord();
if (currentActiveRecord) {
return;
//this.cancelRowEvent(currentActiveRecord.row);
}
this.$refs.xTable.setActiveRow(row);
},
saveRowEvent(row, rowIndex) {
this.$refs.xTable.clearActived().then(() => {
/*
if (!this.$refs.xTable.isUpdateByRow(row)) {
this.$XModal.message({ message: '数据未改动', status: 'info' });
return;
}*/
for (let i = this.tableData.length - 1; i >= 0; i--) {
if (this.tableData[i].item_id === row.item_id) {
this.tableData[i].bootFloorPrice = row.bootFloorPrice;
}
}
//console.log(rowIndex)
//var { _XID, ...nRow } = row;
//console.log(nRow);
this.$XModal.message({ message: '保存成功', status: 'success' });
this.changePriceTask();
localStorage.tableData = JSON.stringify(this.tableData);
localList = this.tableData;
localIdlist = localList.map(v => v.id);
})
},
cancelRowEvent(row) {
// 关闭单元格编辑
this.$refs.xTable.clearActived().then(() => {
// 还原行数据
this.$refs.xTable.revertData(row)
})
}
}
};
var Ctor = Vue.extend(Main);
new Ctor().$mount('#app');
<script src="https://cdn.jsdelivr.net/npm/vue">
</script>
<script src="https://cdn.jsdelivr.net/npm/xe-utils">
</script>
<script src="https://cdn.jsdelivr.net/npm/vxe-table">
</script>
<div id="app">
<template>
<p>
<vxe-button type="button" status="info" @click="$refs.xTable.exportData({ type: 'csv' })">导出csv</vxe-button>
<vxe-button type="button" status="primary" @click="batchSetPriceBase">修改选中依据</vxe-button>
<span>运行开始:{{moment(taskStartTime).format('MM-DD HH:mm')}},运行结束:{{moment(taskEndTime).format('MM-DD HH:mm')}},改价间隔:{{interval}}分钟</span>
<vxe-switch v-model="runTask" on-label="开" off-label="关"></vxe-switch>
<vxe-button type="button" status="primary" @click="changePriceTask()">手动改价</vxe-button>
<vxe-button style="float:right" type="button" status="primary" @click="load()">刷新</vxe-button>
</p>
<vxe-table ref="xTable" border resizable show-overflow :cell-style="cellStyle" :loading="loading" :data="tableData"
:edit-config="{trigger: 'manual', mode: 'row', autoClear: false}">
<vxe-table-column type="checkbox" width="40"></vxe-table-column>
<vxe-table-column type="seq" title="序号" width="50"></vxe-table-column>
<vxe-table-column field="item_name" title="名称" min-width="60" sortable></vxe-table-column>
<vxe-table-column field="item_id" title="查看" type="html" width="50" sortable></vxe-table-column>
<vxe-table-column field="tag" title="tag" width="80" sortable></vxe-table-column>
<vxe-table-column field="wear" title="wear" sortable></vxe-table-column>
<vxe-table-column field="money_min_sell_self" title="人工最低" sortable></vxe-table-column>
<vxe-table-column field="money_min_sell_auto" title="自动最低" sortable></vxe-table-column>
<vxe-table-column field="priceBase" title="定价依据" :edit-render="{name: 'select', options: priceBaseList}" sortable></vxe-table-column>
<vxe-table-column field="bootFloorPrice" title="根底价" :edit-render="{name: 'input'}" sortable></vxe-table-column>
<vxe-table-column field="floorPrice" title="底价" :edit-render="{name: 'input'}" sortable></vxe-table-column>
<vxe-table-column field="step" title="降幅" :edit-render="{name: 'input'}" sortable></vxe-table-column>
<vxe-table-column field="price" title="当前售价" :edit-render="{name: 'input'}" sortable></vxe-table-column>
<vxe-table-column field="update" title="最后改价时间" sortable></vxe-table-column>
<vxe-table-column field="remark" title="备注" min-width="60" :edit-render="{name: 'input'}" sortable></vxe-table-column>
<vxe-table-column title="操作" width="80">
<template v-slot="{ row,rowIndex}">
<template v-if="$refs.xTable.isActiveByRow(row)">
<vxe-button @click="saveRowEvent(row,rowIndex)">保存</vxe-button>
<!--<vxe-button @click="cancelRowEvent(row)">取消</vxe-button>-->
</template>
<template v-else>
<vxe-button @click="editRowEvent(row)">编辑</vxe-button>
</template>
</template>
</vxe-table-column>
</vxe-table>
</template>
</div>
@import url("https://cdn.jsdelivr.net/npm/vxe-table/lib/index.css");
body{
margin: 10px 1%;
}