445 lines
21 KiB
JavaScript
445 lines
21 KiB
JavaScript
define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree','cascader'],
|
||
function ($, undefined, Backend, Table, Form) {
|
||
$.jstree.core.prototype.get_all_checked = function (full) {
|
||
var obj = this.get_selected(), i, j;
|
||
for (i = 0, j = obj.length; i < j; i++) {
|
||
obj = obj.concat(this.get_node(obj[i]).parents);
|
||
}
|
||
obj = $.grep(obj, function (v, i, a) {
|
||
return v != '#';
|
||
});
|
||
obj = obj.filter(function (itm, i, a) {
|
||
return i == a.indexOf(itm);
|
||
});
|
||
return full ? $.map(obj, $.proxy(function (i) {
|
||
return this.get_node(i);
|
||
}, this)) : obj;
|
||
};
|
||
var Controller = {
|
||
index: function () {
|
||
// 初始化表格参数配置
|
||
Table.api.init({
|
||
extend: {
|
||
index_url: 'workers/worker/index' + location.search,
|
||
add_url: 'workers/worker/add',
|
||
edit_url: 'workers/worker/edit',
|
||
del_url: 'workers/worker/del',
|
||
multi_url: 'workers/worker/multi',
|
||
import_url: 'workers/worker/import',
|
||
table: 'worker',
|
||
}
|
||
});
|
||
|
||
var table = $("#table");
|
||
|
||
// 初始化表格
|
||
table.bootstrapTable({
|
||
url: $.fn.bootstrapTable.defaults.extend.index_url,
|
||
pk: 'id',
|
||
sortName: 'id',
|
||
fixedColumns: true,
|
||
showExport:Config.permissions.export,
|
||
fixedRightNumber: 1,
|
||
columns: [
|
||
[
|
||
{checkbox: true},
|
||
{field: 'id', title: __('Id')},
|
||
{field: 'admin.username', title: '创建人'},
|
||
{field: 'name', title: __('Name'), operate: 'LIKE'},
|
||
{
|
||
field: 'type', title: '师傅归属', searchList: {"1": '自营', "2": '非自营', '3': '新师傅'},
|
||
formatter: Table.api.formatter.status,
|
||
},
|
||
{field: 'tel', title: __('Tel'), operate: 'LIKE'},
|
||
{
|
||
field: 'status',
|
||
title: __('Status'),
|
||
searchList: {"1": __('Status 1'), "0": __('Status 0')},
|
||
formatter: Table.api.formatter.status
|
||
},
|
||
{
|
||
field: 'worker_item',
|
||
title: '工种',
|
||
},
|
||
//{field: 'area_id', title: __('Area_id')},
|
||
//{field: 'lng', title: __('Lng'), operate:'BETWEEN'},
|
||
//{field: 'lat', title: __('Lat'), operate:'BETWEEN'},
|
||
{field: 'area.short_merge_name', title: __('Area.short_merge_name'), searchable: false},
|
||
{field: 'address', title: __('地址'), searchable: false},
|
||
{field: 'rate', title: '分成'},
|
||
{field: 'rate_remark', title: '分成备注'},
|
||
{field: 'remark', title: '备注'},
|
||
{field: 'deposit_amount', title: __('Deposit_amount'), operate: 'BETWEEN'},
|
||
{field: 'star', title: __('Star'), operate: 'BETWEEN'},
|
||
{
|
||
field: 'area_id', title: __('地区'), searchList: function (column) {
|
||
return Template('categorytpl', {});
|
||
}, formatter: function (value, row, index) {
|
||
return '无';
|
||
}, visible: false
|
||
},
|
||
{
|
||
field: 'item_id', title: '服务项目', searchList: function (column) {
|
||
return Template('items_picker', {});
|
||
}, formatter: function (value, row, index) {
|
||
return '无';
|
||
}, visible: false
|
||
},
|
||
{
|
||
field: '收款码',
|
||
title: __('Images'),
|
||
operate: false,
|
||
events: Table.api.events.image,
|
||
formatter: Table.api.formatter.images
|
||
},
|
||
{
|
||
field: 'create_time',
|
||
title: __('Create_time'),
|
||
operate: 'RANGE',
|
||
addclass: 'datetimerange',
|
||
autocomplete: false
|
||
},
|
||
{
|
||
field: 'update_time',
|
||
title: __('Update_time'),
|
||
operate: 'RANGE',
|
||
addclass: 'datetimerange',
|
||
autocomplete: false
|
||
},
|
||
{
|
||
field: 'operate', title: __('Operate'),
|
||
buttons: [
|
||
{
|
||
name: 'edit',
|
||
text: "修改",
|
||
icon: 'fa fa-pencil',
|
||
title: __('Edit'),
|
||
extend: 'data-toggle="tooltip" data-container="body"',
|
||
classname: 'btn btn-xs btn-info btn-editone',
|
||
},
|
||
{
|
||
name: 'del',
|
||
text: "删除",
|
||
icon: 'fa fa-trash',
|
||
title: __('Delete'),
|
||
extend: 'data-toggle="tooltip"',
|
||
classname: 'btn btn-xs btn-danger btn-delone',
|
||
},
|
||
],
|
||
table: table, events: Table.api.events.operate,
|
||
formatter: Table.api.formatter.operate
|
||
}
|
||
]
|
||
]
|
||
});
|
||
$("#select_city").on("cp:updated", function () {
|
||
var citypicker = $(this).data("citypicker");
|
||
var code = citypicker.getCode("district") || citypicker.getCode("city") || citypicker.getCode("province");
|
||
$("#select_area_id").val(code);
|
||
});
|
||
|
||
|
||
var _data = items;
|
||
$('#select_item').zdCascader({
|
||
data: _data,
|
||
onChange: function ($this, data, allPathData) {
|
||
// console.log(data,allPathData);
|
||
$('#select_item_id').val(data.value);
|
||
},
|
||
clear: true,
|
||
clickParent: true
|
||
});
|
||
|
||
// 批量修改状态事件
|
||
$(document).on("click", ".btn-change-status-off", function () {
|
||
var ids = Table.api.selectedids(table);
|
||
if (ids.length === 0) {
|
||
Toastr.error("请选择至少一条记录");
|
||
return;
|
||
}
|
||
layer.confirm('是否全部冻结', function () {
|
||
Fast.api.ajax({
|
||
url: 'workers/worker/status',
|
||
type: "POST",
|
||
contentType: 'application/json',
|
||
data: JSON.stringify({
|
||
ids,
|
||
status: 0
|
||
}),
|
||
success:function (){
|
||
layer.closeAll();
|
||
Toastr.info('操作成功');
|
||
table.bootstrapTable("refresh");
|
||
}
|
||
});
|
||
});
|
||
}); // 批量修改状态事件
|
||
$(document).on("click", ".btn-change-status-on", function () {
|
||
var ids = Table.api.selectedids(table);
|
||
if (ids.length === 0) {
|
||
Toastr.error("请选择至少一条记录");
|
||
return;
|
||
}
|
||
layer.confirm('是否全部激活', function () {
|
||
$.ajax({
|
||
url: 'workers/worker/status',
|
||
type: "POST",
|
||
contentType: 'application/json',
|
||
data: JSON.stringify({
|
||
ids,
|
||
status: 1
|
||
}),
|
||
success:function (){
|
||
layer.closeAll();
|
||
Toastr.info('操作成功');
|
||
table.bootstrapTable("refresh");
|
||
}
|
||
});
|
||
});
|
||
});
|
||
$(document).on("click", ".btn-change-type", function () {
|
||
var ids = Table.api.selectedids(table);
|
||
if (ids.length === 0) {
|
||
Toastr.error("请选择至少一条记录");
|
||
return;
|
||
}
|
||
layer.confirm('批量修改类型', function () {
|
||
$.ajax({
|
||
url: 'workers/worker/status',
|
||
type: "POST",
|
||
contentType: 'application/json',
|
||
data: JSON.stringify({
|
||
ids,
|
||
status: 1
|
||
}),
|
||
success:function (){
|
||
layer.closeAll();
|
||
Toastr.info('操作成功');
|
||
table.bootstrapTable("refresh");
|
||
}
|
||
});
|
||
});
|
||
});
|
||
|
||
// 为表格绑定事件
|
||
Table.api.bindevent(table);
|
||
},
|
||
add: function () {
|
||
|
||
const urlParams = new URLSearchParams(window.location.search);
|
||
const type = urlParams.get('type');
|
||
if (type) {
|
||
const select = document.getElementById('worker_type');
|
||
if (select) {
|
||
select.value = type;
|
||
}
|
||
}
|
||
Controller.api.bindevent();
|
||
Controller.api.area();
|
||
},
|
||
edit: function () {
|
||
|
||
Controller.api.bindevent();
|
||
Controller.api.area();
|
||
},
|
||
api: {
|
||
bindevent: function () {
|
||
Form.api.bindevent($("form[role=form]"), null, null, function () {
|
||
if ($("#treeview").length > 0) {
|
||
var r = $("#treeview").jstree("get_all_checked");
|
||
$("input[name='row[rules]']").val(r.join(','));
|
||
}
|
||
return true;
|
||
});
|
||
//渲染权限节点树
|
||
//销毁已有的节点树
|
||
$("#treeview").jstree("destroy");
|
||
Controller.api.rendertree(nodeData);
|
||
//全选和展开
|
||
$(document).on("click", "#checkall", function () {
|
||
$("#treeview").jstree($(this).prop("checked") ? "check_all" : "uncheck_all");
|
||
});
|
||
$(document).on("click", "#expandall", function () {
|
||
$("#treeview").jstree($(this).prop("checked") ? "open_all" : "close_all");
|
||
});
|
||
$("select[name='row[pid]']").trigger("change");
|
||
},
|
||
rendertree: function (content) {
|
||
$("#treeview")
|
||
.on('redraw.jstree', function (e) {
|
||
$(".layer-footer").attr("domrefresh", Math.random());
|
||
})
|
||
.jstree({
|
||
"themes": {"stripes": true},
|
||
"checkbox": {
|
||
"keep_selected_style": false,
|
||
},
|
||
"types": {
|
||
"root": {
|
||
"icon": "fa fa-folder-open",
|
||
},
|
||
"menu": {
|
||
"icon": "fa fa-folder-open",
|
||
},
|
||
"file": {
|
||
"icon": "fa fa-file-o",
|
||
}
|
||
},
|
||
"plugins": ["checkbox", "types"],
|
||
"core": {
|
||
'check_callback': true,
|
||
"data": content
|
||
}
|
||
});
|
||
},
|
||
|
||
area: function (){
|
||
const amapKey = 'c299da50c080dfccf9b1d00560ff9639';
|
||
|
||
let isComposing = false;
|
||
let addressSelected = false;
|
||
let selectedIndex = -1;
|
||
$('#c-address')
|
||
.on('compositionstart', () => {
|
||
isComposing = true;
|
||
})
|
||
.on('compositionend', () => {
|
||
isComposing = false;
|
||
// 不执行 handleInput,由 input 触发
|
||
})
|
||
.on('input', debounce(function (e) {
|
||
if (!isComposing) handleInput(e);
|
||
}, 300))
|
||
.on('blur', function () {
|
||
// 如果还没有选择地址,且列表中有内容,则默认选择第一个
|
||
|
||
// 如果还没有选择地址,且列表中有内容,则默认选择第一个
|
||
if (!addressSelected && $('#suggestionList li').length > 0) {
|
||
choseFirst();
|
||
}
|
||
|
||
$('#suggestionList').hide();
|
||
})
|
||
.on('focus', function () {
|
||
// 如果还没有选择地址,且列表中有内容,则默认选择第一个
|
||
$('#suggestionList').show();
|
||
})
|
||
.on('keydown', function (e) {
|
||
const $items = $('#suggestionList li');
|
||
const len = $items.length;
|
||
|
||
if (!len) return;
|
||
|
||
if (e.key === 'ArrowDown') {
|
||
e.preventDefault();
|
||
selectedIndex = (selectedIndex + 1) % len;
|
||
updateActiveItem($items);
|
||
} else if (e.key === 'ArrowUp') {
|
||
e.preventDefault();
|
||
selectedIndex = (selectedIndex - 1 + len) % len;
|
||
updateActiveItem($items);
|
||
} else if (e.key === 'Enter') {
|
||
if (selectedIndex >= 0 && selectedIndex < len) {
|
||
e.preventDefault();
|
||
$items.eq(selectedIndex).trigger('mousedown');
|
||
}
|
||
}
|
||
});
|
||
function updateActiveItem($items) {
|
||
$items.removeClass('active');
|
||
if (selectedIndex >= 0) {
|
||
const $active = $items.eq(selectedIndex);
|
||
$active.addClass('active');
|
||
|
||
// 自动滚动使其可见
|
||
const container = $('#suggestionList')[0];
|
||
const item = $active[0];
|
||
|
||
if (item && container) {
|
||
const itemTop = item.offsetTop;
|
||
const itemBottom = itemTop + item.offsetHeight;
|
||
const containerTop = container.scrollTop;
|
||
const containerBottom = containerTop + container.clientHeight;
|
||
|
||
if (itemTop < containerTop) {
|
||
container.scrollTop = itemTop;
|
||
} else if (itemBottom > containerBottom) {
|
||
container.scrollTop = itemBottom - container.clientHeight;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
function handleInput(e) {
|
||
selectedIndex = -1;
|
||
addressSelected = false;
|
||
const keyword = $(e.target).val();
|
||
if (!keyword.trim() || /^[\s\p{P}]+$/u.test(keyword)) return;
|
||
if (!keyword) return $('#c-address').empty();
|
||
|
||
$.getJSON('https://restapi.amap.com/v3/assistant/inputtips', {
|
||
key: amapKey,
|
||
keywords: keyword,
|
||
datatype: 'all',
|
||
city: '全国',
|
||
}, function (res) {
|
||
if (res.tips) {
|
||
let html = '';
|
||
res.tips.forEach(tip => {
|
||
if (tip.location) {
|
||
html += `<li data-name="${tip.district} ${tip.name}" data-area_id="${tip.adcode}" data-location="${tip.location}">
|
||
${tip.district} ${tip.name}
|
||
</li>`;
|
||
}
|
||
});
|
||
$('#suggestionList').html(html).show();
|
||
}
|
||
});
|
||
}
|
||
|
||
function debounce(fn, delay = 300) {
|
||
let timer = null;
|
||
return function (...args) {
|
||
clearTimeout(timer);
|
||
timer = setTimeout(() => {
|
||
fn.apply(this, args);
|
||
}, delay);
|
||
};
|
||
}
|
||
|
||
$('#suggestionList').on('mousedown', 'li', function (e) {
|
||
const name = $(this).data('name');
|
||
const location = $(this).data('location'); // "经度,纬度"
|
||
const area_id = $(this).data('area_id'); // "经度,纬度"
|
||
const [lng, lat] = location.split(',');
|
||
$('#c-address').val(name);
|
||
$('#lng').val(lng);
|
||
$('#lat').val(lat);
|
||
$('#area_id').val(area_id);
|
||
// 隐藏提示列表
|
||
$('#suggestionList').hide();
|
||
selectedIndex = -1;
|
||
addressSelected = true;
|
||
});
|
||
|
||
function choseFirst(){
|
||
const first = $('#suggestionList li').first();
|
||
const name = first.data('name');
|
||
const location = first.data('location'); // "经度,纬度"
|
||
const area_id = first.data('area_id'); // "经度,纬度"
|
||
const [lng, lat] = location.split(',');
|
||
console.log('chose');
|
||
$('#lng').val(lng);
|
||
$('#lat').val(lat);
|
||
$('#area_id').val(area_id);
|
||
// 隐藏提示列表
|
||
$('#suggestionList').hide();
|
||
selectedIndex = -1;
|
||
addressSelected = true;
|
||
}
|
||
}
|
||
}
|
||
};
|
||
return Controller;
|
||
});
|