12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073 |
- /*---------------------------------------------参数配置---------------------------------------*/
- const CONSTANTS = {
- KEY_CHANNEL_NAMES: 'channel_names', //上游的名字,通道分组用
- KEY_MERCHANT_NAMES: 'merchant_names', //下游的名字,机构分组用
- KEY_CHANNEL_FAST: 'channel_fast', //上下游余额统计:统计的快充通道
- KEY_CHANNEL_NORMAL: 'channel_normal', //上下游余额统计:统计的普充通道
- KEY_MERCHANT: 'merchant', //上下游余额统计:统计的机构
- KEY_DAILY_CHANNEL: 'daily_channel', //日对账单:统计的通道
- KEY_DAILY_MERCHANT: 'daily_merchant', //日对账单:统计的通道
- };
- /*---------------------------------------------公共方法---------------------------------------*/
- function formatDecimals(number, digit= 2) {//格式化金额
- if (typeof number === 'string') {
- number = Number(number);
- }
- if (number === 0) {
- return '0';
- }
- return parseFloat(number).toFixed(digit);
- }
- function isValidKey(obj, key) {
- if (obj.hasOwnProperty(key)) {
- if (obj[key] !== null && obj[key] !== undefined && obj[key] !== '') {
- return true;
- }
- }
- return false;
- }
- function deepCloneData(obj) {//数据深拷贝
- if (obj === null || typeof obj !== 'object') {
- return obj;
- }
- let clone = Array.isArray(obj) ? [] : {};
- for (let key in obj) {
- if (obj.hasOwnProperty(key)) {
- clone[key] = deepCloneData(obj[key]);
- }
- }
- return clone;
- }
- function sortSysData(sysData, sortData, key) {//sortData数据前置,sysData中未包含在sortData中的数据放在后面
- let sortedData = deepCloneData(sortData);
- const unselectedData = sysData.filter(function (sys) {
- const selected = sortData.find(function(cust) {
- return sys[key] === cust[key] && sys.tag === sys.tag;
- });
- return !selected;
- });
- return sortedData.concat(unselectedData);
- }
- /**
- * @param sortCustSet 排好序的元素
- * @param data 实时数据,未排序
- * @param groupKey 分组字段
- * @param nameKey 过滤字段,机构和通道均包含name字段,增加nameKey,比如store_name,mch_name,多一层过滤条件防止存在重名情况
- * @returns {*[]}
- */
- function sortCustData(sortCustSet, data, groupKey, nameKey) {
- // 去掉不在当前数据中的排序值
- const sortCust = sortCustSet.filter(function (item) {
- return data.find(function (litem) {
- return litem.name === item.name && litem[nameKey] === item[nameKey] && litem.tag === item.tag;
- });
- });
- const custMap = sortCust.reduce((acc, item, index) => {
- acc[item.name] = index;
- return acc;
- }, {});
- data.sort((a, b) => custMap[a.name] - custMap[b.name]);
- const groupMap = {};
- data.forEach(item => {
- const group = item[groupKey];
- if (!groupMap[group]) {
- groupMap[group] = [];
- }
- groupMap[group].push(item);
- });
- const sortedData = [];
- data.forEach(item => {
- const group = item[groupKey];
- if (groupMap[group]) {
- sortedData.push(...groupMap[group]);
- delete groupMap[group];
- }
- });
- return sortedData;
- }
- /*
- * 合并二维数据,表格数据合并/通道机构配置项合并
- */
- function mergeData(data1, data2, key) {
- let dataMormalMerge = [];
- for (let item of data1) {
- const p2Item = data2.find(function(p2) {
- return item[key] === p2[key] && item.tag === p2.tag;
- });
- if (!p2Item) {
- dataMormalMerge.push(item);
- } else {
- dataMormalMerge.push(p2Item);
- }
- }
- for (let item of data2) {
- const mItem = dataMormalMerge.find(function(m) {
- return item[key] === m[key] && item.tag === m.tag;
- });
- if (!mItem) {
- dataMormalMerge.push(item);
- }
- }
- return dataMormalMerge;
- }
- /*
- * 合并一维数组,上下游名称配置项合并
- */
- function mergeNames(data1, data2) {
- const notin = data2.filter(item => !data1.includes(item));
- return data1.concat(notin);
- }
- function handleInputd2(event) {//限制金额输入框
- let element = event.target;
- let value = element.textContent;
- let selection = window.getSelection();
- let range = selection.getRangeAt(0);
- let startOffset = range.startOffset;
- value = value.replace(/[^\d.-]/g, '');
- const parts = value.split('.');
- if (parts.length > 1) {
- parts[1] = parts[1].slice(0, 2);
- value = parts.join('.');
- }
- element.textContent = value;
- let newNode = element.childNodes[0] || element;
- range.setStart(newNode, Math.min(startOffset, value.length));
- range.setEnd(newNode, Math.min(startOffset, value.length));
- selection.removeAllRanges();
- selection.addRange(range);
- }
- function handleInputd4(event) {
- let element = event.target;
- let value = element.textContent;
- let selection = window.getSelection();
- let range = selection.getRangeAt(0);
- let startOffset = range.startOffset;
- value = value.replace(/[^\d.-]/g, '');
- const parts = value.split('.');
- if (parts.length > 1) {
- parts[1] = parts[1].slice(0, 4);
- value = parts.join('.');
- }
- element.textContent = value;
- let newNode = element.childNodes[0] || element;
- range.setStart(newNode, Math.min(startOffset, value.length));
- range.setEnd(newNode, Math.min(startOffset, value.length));
- selection.removeAllRanges();
- selection.addRange(range);
- }
- function isObjectEmpty(obj) {
- return Object.keys(obj).length === 0;
- }
- function showErr(error) {
- layer.alert(error, {
- icon: 2,
- title: '错误'
- });
- }
- function showSuccess(msg) {
- layer.msg(msg, {
- icon: 1,
- time: 2000
- });
- }
- function isNumeric(value) {
- return /^-?\d+(\.\d+)?$/.test(value);
- }
- /*----------------------------------------------统一请求定义---------------------------------------*/
- class ApiService {
- constructor(baseUrl) {
- this.baseUrl = baseUrl;
- }
- request(endpoint, method, data) {
- const that = this;
- return new Promise(function(resolve, reject) {
- $.ajax({
- url: that.baseUrl + endpoint,
- type: method,
- data: data,
- dataType: 'json',
- success: function(response) {
- if (response.state) {
- resolve(response);
- } else {
- reject(new Error('请求失败'));
- }
- },
- error: function(jqXHR, textStatus, errorThrown) {
- reject(new Error('请求失败: ' + textStatus + ', ' + errorThrown));
- }
- });
- });
- }
- getStatsSettings() {
- const endpoint = '?act=refill_amount_stats&op=stats_settings';
- return this.request(endpoint, 'GET', {});
- };
- setStatsSettings(key, value) {
- const endpoint = '?act=refill_amount_stats&op=stats_settings_save';
- let data = {
- key: key,
- value: value
- };
- return this.request(endpoint, 'POST', data);
- };
- getSysChannels() {
- const endpoint = '?act=refill_amount_stats&op=sys_channels';
- return this.request(endpoint, 'GET', {});
- };
- getSysMchs() {
- const endpoint = '?act=refill_amount_stats&op=sys_mchs';
- return this.request(endpoint, 'GET', {});
- };
- getChannelData(params) {
- const queryString = $.param(params);
- const endpoint = '?act=refill_amount_stats&op=channel_data&' + queryString;
- return this.request(endpoint, 'GET', {});
- };
- getMchData(params) {
- const queryString = $.param(params);
- const endpoint = '?act=refill_amount_stats&op=mch_data&' + queryString;
- return this.request(endpoint, 'GET', {});
- };
- getDailyProviderData(params) {
- const queryString = $.param(params);
- const endpoint = '?act=refill_amount_stats&op=daily_provider_data&' + queryString;
- return this.request(endpoint, 'GET', {});
- };
- getDailyMerchantData(params) {
- const queryString = $.param(params);
- const endpoint = '?act=refill_amount_stats&op=daily_merchant_data&' + queryString;
- return this.request(endpoint, 'GET', {});
- };
- }
- const statsApi = new ApiService('');
- /*------------------------------------------------------组件实现:调度类-------------------------------------------*/
- class ComponentBase {
- constructor(options) {
- this.el = document.querySelector(options.el);
- this.components = options.components || {};
- this.componentData = options.componentData || {};
- this.events = {};
- this.componentInstances = {};
- this.render();
- }
- render() {
- this.el.innerHTML = this.compile(this.el.innerHTML);
- }
- compile(template) {
- const componentNames = Object.keys(this.components);
- componentNames.forEach(name => {
- const regex = new RegExp(`<${name}([^>]*)>(.*?)<\/${name}>`, 'g');
- template = template.replace(regex, (match, attrs) => {
- const props = {};
- attrs.replace(/(\w+)="([^"]*)"/g, (match, key, value) => {
- if (CONSTANTS[value] !== undefined) {
- value = CONSTANTS[value];
- }
- props[key] = value;
- });
- const component = new this.components[name]({
- props: props,
- data: this.componentData[name] || {},
- parent: this
- });
- this.componentInstances[name] = component;
- return component.render();
- });
- });
- return template;
- }
- on(event, callback) {
- if (!this.events[event]) {
- this.events[event] = [];
- }
- this.events[event].push(callback);
- }
- emit(event, ...args) {
- if (this.events[event]) {
- this.events[event].forEach(callback => callback(...args));
- }
- }
- getComponent(name) {
- return this.componentInstances[name];
- }
- }
- /*------------------------------------------------------组件实现:组件基类-------------------------------------------*/
- class Component {
- constructor(options) {
- this.data = options.data || {};
- this.props = options.props || {};
- this.template = options.template || '';
- this.id = 'component-' + Math.random().toString(36).substr(2, 9);
- this.props['comp-id'] = this.id;
- }
- render() {
- let template = this.template;
- for (const key in this.props) {
- template = template.replace(new RegExp(`{{${key}}}`, 'g'), this.props[key]);
- }
- setTimeout(() => {
- this.bindEvents();
- this.mounted();
- }, 0);
- return template;
- }
- bindEvents() {
- const rootElement = document.querySelector(`#${this.id}`);
- if (!rootElement) {
- return;
- }
- this.unbindEvents();
- const supportedEvents = ['click', 'input', 'blur', 'change'];
- supportedEvents.forEach(eventType => {
- const elements = rootElement.querySelectorAll(`[v-on\\:${eventType}]`);
- elements.forEach(element => {
- const method = element.getAttribute(`v-on:${eventType}`);
- if (method && typeof this[method] === 'function') {
- const listener = this[method].bind(this);
- element.addEventListener(eventType, listener);
- if (!this.eventListeners[eventType]) {
- this.eventListeners[eventType] = [];
- }
- this.eventListeners[eventType].push({ element, listener });
- }
- });
- });
- }
- unbindEvents() {
- for (const eventType in this.eventListeners) {
- this.eventListeners[eventType].forEach(({ element, listener }) => {
- element.removeEventListener(eventType, listener);
- });
- }
- this.eventListeners = {};
- }
- mounted() {
- }
- }
- /*------------------------------------------------------layui表格基类-----------------------------------------------*/
- class LayuiTableBase {
- constructor(id) {
- this.tableId = id;
- }
- /*
- * 表格中可编辑列的处理
- */
- editEvent(editColumns = [], callback) {
- const tableItem = $('#' + this.tableId);
- const trs = tableItem.next('.layui-table-view').find('.layui-table-body tbody tr');
- trs.each(function (i, tr) {
- for (let col of editColumns) {
- const td = $(tr).find('td[data-field="' + col + '"]').not('.layui-hide');
- if (td.length > 0) {//合并隐藏的不处理
- const valDiv = td.find('div');
- valDiv.attr('contenteditable', true).css('background-color', 'lightblue');
- valDiv.off('input', handleInputd4)
- .off('keypress')
- .off('blur')
- .on('input', handleInputd4)
- .on('keypress', function (e) {
- if (e.keyCode === 13) {
- e.preventDefault();
- $(this).blur();
- }
- })
- .on('blur', function () {
- const val = valDiv.text().trim();
- if (!isNumeric(val)) {
- valDiv.html('0');
- }
- if (callback !== null && typeof callback === 'function') {
- const index = $(this).parent().parent().data('index');
- callback(index);
- }
- });
- }
- }
- });
- }
- switchEvent(switchColumns = [], callback) {
- const tableItem = $('#' + this.tableId);
- const trs = tableItem.next('.layui-table-view').find('.layui-table-body tbody tr');
- trs.each(function (i, tr) {
- for (let clo of switchColumns) {
- const td = $(tr).find('td[data-field="' + clo + '"]').not('.layui-hide');
- if (td.length > 0) {
- const buttons = td.find('.layui-btn');
- buttons.off('click').on('click', function (e) {
- e.preventDefault();
- e.stopPropagation();
- const currTr = $(this).closest('tr');
- const index = currTr.data('index');
- buttons.removeClass('selected').addClass('unselected');
- $(this).removeClass('unselected').addClass('selected');
- if (callback !== null && typeof callback === 'function') {
- callback(index);
- }
- });
- }
- }
- });
- }
- /*
- * 表格中金额字段样式,小于0的数值设置为红色
- */
- setValueClass(valColumns) {
- const trs = $('#' + this.tableId).next('.layui-table-view').find('.layui-table-body tbody tr');
- trs.each(function(i, tr) {
- for(let col of valColumns) {
- const td = $(tr).find('td[data-field="' + col + '"]').not('.layui-hide');
- if (td.length > 0) {//合并隐藏的不处理
- const valDiv = td.find('div');
- const val = valDiv.text().trim();
- if (parseFloat(val) < 0) {
- valDiv.addClass('negative-value');
- } else {
- valDiv.removeClass('negative-value');
- }
- }
- }
- });
- }
- }
- /*------------------------------------------------------组件:上下游名称设置-------------------------------------------*/
- class StreamNameComponent extends Component {
- constructor(options) {
- super(Object.assign(options, {
- template: `
- <div id="{{comp-id}}">
- <div class="bottom10">
- <span>{{title}}:</span>
- <span id="names-selected-{{comp-id}}" class="names_ca"></span>
- <button v-on:click="open" type="button" class="layui-btn layui-bg-green">修改</button>
- </div>
- <div id="group-names-model-{{comp-id}}" style="display: none;">
- <div class="input-group">
- <div class="input-item">
- <input type="text" id="input-{{comp-id}}" placeholder="多个名称使用,分隔">
- </div>
- <div class="input-item">
- <button v-on:click="save" class="layui-btn layui-btn-sm">保存</button>
- </div>
- </div>
- <div class="names-manager-container">
- </div>
- </div>
- </div>
- `
- }));
- this.cacheKey = this.props['type'];
- this.cacheData = [];
- }
- async mounted() {
- await this.loadData();
- this.updateUI();
- }
- async open() {
- const content = this.createModelContent();
- $('#group-names-model-' + this.id + ' .names-manager-container').html(content);
- this.bindEvents();
- layer.open({
- type: 1,
- title: '设置上游名称',
- content: $('#group-names-model-' + this.props['comp-id']),
- area: ['500px', '400px'],
- success: function() {
- }
- });
- }
- async loadData() {
- const statsSettings = await statsApi.getStatsSettings();
- if (statsSettings.state === true) {
- this.cacheData = statsSettings[this.cacheKey] || [];
- }
- }
- createModelContent() {
- const channelNames = this.cacheData;
- let html = '';
- for (let i = 0; i < channelNames.length; i++) {
- html += '<label class="ch-tags">' + channelNames[i] +
- '<span class="delete-btn" v-on:click="delete" data-index="' + i + '">×</span>' +
- '</label>';
- }
- return html;
- }
- save() {
- const that = this;
- const inputVal = $('#input-' + this.id).val().replace(/,/g, ',');
- const newNames = inputVal.split(',').map(item => item.trim()).filter(item => item);
- if (newNames.length > 0) {
- const cache = this.cacheData;
- const updatedNames = cache.concat(newNames);
- const uniqueNames = updatedNames.filter((item, index) => updatedNames.indexOf(item) === index);
- const loadIndex = layer.load(2, { shade: [0.2, '#000'] });
- that.updateCache(uniqueNames, [loadIndex]);
- }
- }
- delete(event) {
- const that = this;
- const index = event.currentTarget.getAttribute('data-index');
- const cache = this.cacheData;
- const tmpNames = deepCloneData(cache);
- const tag = tmpNames[index];
- layer.confirm('确定要删除 "' + tag + '" 吗?', {
- title: '删除确认',
- icon: 3,
- btn: ['删除', '取消'],
- btn1: function (btn_i) {
- tmpNames.splice(index, 1);
- const loadIndex = layer.load(2, { shade: [0.2, '#000'] });
- that.updateCache(tmpNames, [loadIndex, btn_i]);
- }
- });
- }
- updateCache(tmpNames, closer = []) {
- const that = this;
- const cacheKey = this.cacheKey;
- statsApi.setStatsSettings(cacheKey, tmpNames)
- .then(function (data) {
- closer.forEach(function (lr) {
- layer.close(lr);
- });
- if (data.state === true) {
- that.cacheData = data[cacheKey];
- that.updateUI();
- const content = that.createModelContent();
- $('#group-names-model-' + that.id + ' .names-manager-container').html(content);
- $('#input-' + that.id).val('');
- that.bindEvents();
- } else {
- showErr('保存失败,请重试');
- }
- })
- .catch(function (error) {
- showErr('保存失败,请重试');
- });
- }
- updateUI() {
- const selector = $('#names-selected-' + this.id);
- const cache = this.cacheData;
- let html = '';
- for(let item of cache) {
- html += '<label class="ch-tags">' +item+ '</label>';
- }
- selector.html(html);
- }
- }
- /*-------------------------------------------------------组件:设置统计的通道/机构-----------------------------------------*/
- class SelectedChannelComponent extends Component {
- constructor(options) {
- super(Object.assign(options, {
- template: `
- <div id="{{comp-id}}">
- <button class="layui-btn layui-bg-green" v-on:click="open">新增</button>
- <div id="select-modal-{{comp-id}}" style="display: none;">
- <form id="select-form-{{comp-id}}"></form>
- <div id="selected-items-{{comp-id}}" class="selected-items"></div>
- </div>
- </div>
- `
- }));
- this.parent = options.parent;
- this.cacheKey = this.props['type'];
- this.cacheData = []; //计入统计的通道
- this.lastCacheData = []; //打开弹层时的数据,用于对比是否做了修改
- this.sysData = []; //系统全量通道,初始时为空,数据在第一次打开弹层时加载
- }
- mounted() {
- const that = this;
- $('#selected-items-' + this.id).sortable({
- containment: "parent",
- cursor: "move",
- update: function(event, ui) {
- that.updateCheckboxOrder();
- }
- }).disableSelection();
- }
- async open() {
- await this.loadData();
- this.renderForm(true);
- const that = this;
- layer.open({
- type: 1,
- title: this.props['title'],
- area: ['960px', '600px'],
- content: $('#select-modal-' + this.id),
- btn: ['确定', '取消'],
- yes: function() {
- if (that.isEdit() === true) {
- statsApi.setStatsSettings(that.cacheKey, that.cacheData)
- .then(function (data) {
- layer.closeAll();
- })
- .catch(function (error) {
- layer.closeAll();
- showErr('保存失败,请重试');
- });
- } else {
- layer.closeAll();
- }
- },
- end: function() {
- if (that.isEdit() === true) {
- if (that.parent && typeof that.parent.emit === 'function') {
- that.parent.emit(that.props['emit'], that.cacheKey, that.cacheData);
- }
- }
- }
- });
- }
- async loadData() {
- const loadIndex = layer.load(2, { shade: [0.2, '#000'] });
- const sysChannels = await statsApi.getSysChannels();
- if (sysChannels.state === true) {
- this.sysData = sysChannels.list;
- }
- const statsSettings = await statsApi.getStatsSettings();
- this.cacheData = statsSettings[this.cacheKey] || [];
- this.sysData = sortSysData(this.sysData, this.cacheData, 'store_name');
- this.lastCacheData = deepCloneData(this.cacheData);
- layer.close(loadIndex);
- }
- isEdit() {
- if (this.lastCacheData.length !== this.cacheData.length) {
- return true;
- }
- for (let i = 0; i < this.lastCacheData.length; i++) {
- const u1 = this.cacheData[i].tag + '-' + this.cacheData[i].store_id;
- const u2 = this.lastCacheData[i].tag + '-' + this.lastCacheData[i].store_id;
- if (u1 !== u2) {
- return true;
- }
- }
- return false;
- }
- renderForm(updateSelectedItems) {
- let formHtml = '';
- for (let item of this.sysData) {
- let checked = '';
- const dataItem = this.cacheData.find(function (chan) {
- return chan.store_name === item.store_name && chan.tag === item.tag;
- });
- if (dataItem) {
- checked = 'checked="true"';
- }
- formHtml += '<label class="checkout-items">';
- formHtml += '<input type="checkbox" name="stats_channels-' + this.id + '" v-on:change="change" value="' + item.store_name + '" data-name="' + item.name + '" data-quality_text="' + item.quality_text + '" data-tag="' + item.tag + '" ' + checked + ' />' + item.store_name;
- formHtml += '</label>';
- }
- $('#select-form-' + this.id).html(formHtml);
- this.bindEvents();
- if (updateSelectedItems === true) {
- this.updateSelectedItems();
- }
- }
- change(event) {
- const tag = event.currentTarget.getAttribute('data-tag');
- const value = event.currentTarget.value;
- const checked = event.currentTarget.checked;
- this.updateCache(value, tag, checked);
- this.updateSelectedItems();
- }
- updateCache(storeName, tag, checked) {
- if (checked === true) {
- const item = this.sysData.find(function (chan) {
- return chan.store_name === storeName && chan.tag === tag;
- });
- if (item) {
- this.cacheData.push(item);
- }
- } else {
- let cusChannelsNew = [];
- for (let item of this.cacheData) {
- if (item.store_name !== storeName || (item.store_name === storeName && item.tag !== tag)) {
- cusChannelsNew.push(item);
- }
- }
- this.cacheData = cusChannelsNew;
- }
- }
- updateSelectedItems() {
- const selectedItems = $('#' + this.id).find('#selected-items-' + this.id);
- selectedItems.empty();
- for(let item of this.cacheData) {//选中项放入排序区
- const itemHtml = '<div class="sortable-item" data-tag="' + item.tag + '" id="' + item.store_name + '">' + item.store_name + '</div>';
- selectedItems.append(itemHtml);
- }
- this.updateCheckboxOrder();
- }
- updateCheckboxOrder() {
- const selectedItems = $('#' + this.id).find('#selected-items-' + this.id + ' .sortable-item');
- let sortedData = [];
- const that = this;
- selectedItems.each(function() {
- const value = $(this).text().trim();
- const tag = $(this).data('tag');
- const dataItem = that.sysData.find(function (item) {
- return item.store_name === value && item.tag === tag;
- });
- if (dataItem) {
- sortedData.push(dataItem);
- }
- });
- this.cacheData = deepCloneData(sortedData);
- //全局通道数据,已选中项,排在前面
- const unselectedData = that.sysData.filter(function (item) {
- return !sortedData.includes(item);
- });
- sortedData = sortedData.concat(unselectedData);
- this.sysData = sortedData;
- this.renderForm(false);
- }
- }
- /*-------------------------------------------------------统计的机构设置---------------------------------------------*/
- class SelectedMerchantComponent extends Component {
- constructor(options) {
- super(Object.assign(options, {
- template: `
- <div id="{{comp-id}}">
- <button class="layui-btn layui-bg-green" v-on:click="open">新增</button>
- <div id="select-modal-{{comp-id}}" style="display: none;">
- <form id="select-form-{{comp-id}}"></form>
- <div id="selected-items-{{comp-id}}" class="selected-items"></div>
- </div>
- </div>
- `
- }));
- this.parent = options.parent;
- this.cacheKey = this.props['type'];
- this.cacheData = []; //计入统计的机构
- this.lastCacheData = []; //打开弹层时的数据,用于对比是否做了修改
- this.sysData = []; //系统全量通道,初始时为空,数据在第一次打开弹层时加载
- }
- mounted() {
- const that = this;
- $('#selected-items-' + this.id).sortable({
- containment: "parent",
- cursor: "move",
- update: function(event, ui) {
- that.updateCheckboxOrder();
- }
- }).disableSelection();
- }
- async open() {
- await this.loadData();
- this.renderForm(true);
- const that = this;
- layer.open({
- type: 1,
- title: this.props['title'],
- area: ['960px', '600px'],
- content: $('#select-modal-' + this.id),
- btn: ['确定', '取消'],
- yes: function() {
- if (that.isEdit() === true) {
- statsApi.setStatsSettings(that.cacheKey, that.cacheData)
- .then(function (data) {
- layer.closeAll();
- })
- .catch(function (error) {
- layer.closeAll();
- showErr('保存失败,请重试');
- });
- } else {
- layer.closeAll();
- }
- },
- end: function() {
- if (that.isEdit() === true) {
- if (that.parent && typeof that.parent.emit === 'function') {
- that.parent.emit(that.props['emit'], that.cacheKey, that.cacheData);
- }
- }
- }
- });
- }
- async loadData() {
- const loadIndex = layer.load(2, { shade: [0.2, '#000'] });
- if (this.sysData.length === 0) { //系统全量机构数据,仅加载1次
- const sysMchs = await statsApi.getSysMchs();
- if (sysMchs.state === true) {
- this.sysData = sysMchs.list;
- }
- }
- const statsSettings = await statsApi.getStatsSettings();
- this.cacheData = statsSettings[this.cacheKey] || [];
- this.sysData = sortSysData(this.sysData, this.cacheData, 'mch_name');
- this.lastCacheData = deepCloneData(this.cacheData);
- layer.close(loadIndex);
- }
- isEdit() {
- if (this.lastCacheData.length !== this.cacheData.length) {
- return true;
- }
- for (let i = 0; i < this.lastCacheData.length; i++) {
- const u1 = this.cacheData[i].tag + '-' + this.cacheData[i].mch_id;
- const u2 = this.lastCacheData[i].tag + '-' + this.lastCacheData[i].mch_id;
- if (u1 !== u2) {
- return true;
- }
- }
- return false;
- }
- renderForm(updateSelectedItems) {
- let formHtml = '';
- for (let item of this.sysData) {
- let checked = '';
- const dataItem = this.cacheData.find(function (chan) {
- return chan.mch_name === item.mch_name && chan.tag === item.tag;
- });
- if (dataItem) {
- checked = 'checked="true"';
- }
- formHtml += '<label class="checkout-items">';
- formHtml += '<input type="checkbox" name="stats_mchs-' + this.id + '" v-on:change="change" value="' + item.mch_name + '" data-name="' + item.name + '" data-quality_text="' + item.quality_text + '" data-tag="' + item.tag + '" ' + checked + ' />' + item.mch_name;
- formHtml += '</label>';
- }
- $('#select-form-' + this.id).html(formHtml);
- this.bindEvents();
- if (updateSelectedItems === true) {
- this.updateSelectedItems();
- }
- }
- change(event) {
- const tag = event.currentTarget.getAttribute('data-tag');
- const value = event.currentTarget.value;
- const checked = event.currentTarget.checked;
- this.updateCache(value, tag, checked);
- this.updateSelectedItems();
- }
- updateCache(mchName, tag, checked) {
- if (checked === true) {
- const item = this.sysData.find(function (chan) {
- return chan.mch_name === mchName && chan.tag === tag;
- });
- if (item) {
- this.cacheData.push(item);
- }
- } else {
- let cusMchsNew = [];
- for (let item of this.cacheData) {
- if (item.mch_name !== mchName || (item.mch_name === mchName && item.tag !== tag)) {
- cusMchsNew.push(item);
- }
- }
- this.cacheData = cusMchsNew;
- }
- }
- updateSelectedItems() {
- const selectedItems = $('#' + this.id).find('#selected-items-' + this.id);
- selectedItems.empty();
- for(let item of this.cacheData) {//选中项放入排序区
- const itemHtml = '<div class="sortable-item" data-tag="' + item.tag + '" id="' + item.mch_name + '">' + item.mch_name + '</div>';
- selectedItems.append(itemHtml);
- }
- this.updateCheckboxOrder();
- }
- updateCheckboxOrder() {
- const selectedItems = $('#' + this.id).find('#selected-items-' + this.id + ' .sortable-item');
- let sortedData = [];
- const that = this;
- selectedItems.each(function() {
- const value = $(this).text().trim();
- const tag = $(this).data('tag');
- const dataItem = that.sysData.find(function (item) {
- return item.mch_name === value && item.tag === tag;
- });
- if (dataItem) {
- sortedData.push(dataItem);
- }
- });
- this.cacheData = deepCloneData(sortedData);
- //全局通道数据,已选中项,排在前面
- const unselectedData = that.sysData.filter(function (item) {
- return !sortedData.includes(item);
- });
- sortedData = sortedData.concat(unselectedData);
- this.sysData = sortedData;
- this.renderForm(false);
- }
- }
|