913 lines
41 KiB
JavaScript
913 lines
41 KiB
JavaScript
define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'cascader'], function ($, undefined, Backend, Table, Form) {
|
||
|
||
function clearInfo() {
|
||
$('[name^="row["]').val('');
|
||
$("#c-city").citypicker('reset');
|
||
$("#item_id").val('');
|
||
$('.zd-cascader-menu').find('li.in-active-path').removeClass('in-active-path');
|
||
$('.zd-cascader-panel').find('.is-selected-icon').remove();
|
||
$("#item_id_value").val('');
|
||
$(".selectpicker").val('').selectpicker('refresh');
|
||
}
|
||
|
||
function copyToClipboard(text) {
|
||
// 创建一个隐藏的 textarea 元素
|
||
const textarea = document.createElement('textarea');
|
||
textarea.value = text;
|
||
document.body.appendChild(textarea);
|
||
|
||
// 选中内容并复制
|
||
textarea.select();
|
||
document.execCommand('copy');
|
||
|
||
// 移除 textarea 元素
|
||
document.body.removeChild(textarea);
|
||
Toastr.info('复制成功');
|
||
}
|
||
|
||
|
||
// 拼装文本
|
||
function assembleOrderMessage(data) {
|
||
let message = '';
|
||
if (!data.coupon) {
|
||
message = `订单编号: ${data.order_no}
|
||
渠道来源:${data.source_total_name}
|
||
服务名称: ${data.item_title}
|
||
客户昵称: ${data.customer}
|
||
客户电话: ${data.tel}
|
||
客户地址: ${data.address}
|
||
${data.receive_type == 1 ? '已收定金' : '已收全款'}
|
||
下单金额: ¥${formatNumber(data.online_amount)}
|
||
订单备注: ${data.remark || '无'}
|
||
预约时间: ${data.plan_time || '无'}
|
||
`;
|
||
} else {
|
||
message = `订单编号: ${data.order_no}
|
||
渠道来源:${data.source_total_name}
|
||
服务名称: ${data.item_title}
|
||
客户昵称: ${data.customer}
|
||
客户电话: ${data.tel}
|
||
客户地址: ${data.address}
|
||
${data.receive_type == 1 ? '已收定金' : '已收全款'}
|
||
下单金额: ¥${formatNumber(data.online_amount)}
|
||
优惠活动: ${data.coupon?.code || '无'}
|
||
订单备注: ${data.remark ||'无'}
|
||
预约时间: ${data.plan_time || '无'}
|
||
`;
|
||
}
|
||
|
||
return message;
|
||
}
|
||
|
||
function formatNumber(value, decimals = 1) {
|
||
let num = parseFloat(value);
|
||
if (isNaN(num)) return '0.0'.slice(0, decimals + 2); // e.g. '0.0' or '0.00'
|
||
return num.toFixed(decimals);
|
||
}
|
||
|
||
|
||
var Controller = {
|
||
index: function () {
|
||
// 初始化表格参数配置
|
||
Table.api.init({
|
||
extend: {
|
||
index_url: 'order/index' + location.search,
|
||
add_url: 'order/add',
|
||
edit_url: 'order/edit',
|
||
//del_url: 'order/del',
|
||
multi_url: 'order/multi',
|
||
import_url: 'order/import',
|
||
push_url: 'order/status',
|
||
addabnormal: 'order/addAbnormal',
|
||
table: 'order',
|
||
}
|
||
});
|
||
|
||
var table = $("#table");
|
||
// 初始化表格
|
||
table.bootstrapTable({
|
||
url: $.fn.bootstrapTable.defaults.extend.index_url,
|
||
pk: 'id',
|
||
sortName: 'id',
|
||
fixedRightNumber: 1,
|
||
fixedNumber: 4,
|
||
fixedColumns: true,
|
||
renderDefault: true,
|
||
searchFormVisible: true,
|
||
search: false,
|
||
columns: [
|
||
[
|
||
{checkbox: true},
|
||
{field: 'order_no', title: __('Order_no'), operate: 'LIKE'},
|
||
{field: 'id', title: __('Id'),searchable: false},
|
||
{
|
||
field: 'status',
|
||
title: __('Status'),
|
||
fixed: true,
|
||
searchList: {
|
||
"0": __('Status 0'),
|
||
"10": __('Status 10'),
|
||
"20": __('Status 20'),
|
||
"30": __('Status 30'),
|
||
"40": __('Status 40'),
|
||
"41": __('Status 41'),
|
||
"50": __('Status 50'),
|
||
"60": __('Status 60'),
|
||
"70": __('Status 70'),
|
||
"-10": __('Status -10'),
|
||
/*"-20": __('Status -20'),
|
||
"-30": __('Status -30')*/
|
||
},
|
||
defaultValue: 10,
|
||
formatter: Table.api.formatter.status,
|
||
custom: {
|
||
"10": "my_dispatch"
|
||
}
|
||
},
|
||
{
|
||
field: 'create_time',
|
||
title: __('Create_time'),
|
||
operate: 'RANGE',
|
||
addclass: 'datetimerange',
|
||
autocomplete: false
|
||
},
|
||
{
|
||
field: 'is_timeout',
|
||
title: '是否超时',
|
||
formatter: function (value, row) {
|
||
value = row.create_time;
|
||
|
||
if (row.status !== 10 || !value) {
|
||
return '';
|
||
}
|
||
// 解析 create_time 时间戳或字符串为时间对象
|
||
var createTime = typeof value === 'string' ? new Date(value.replace(/-/g, '/')) : new Date(value * 1000); // 兼容时间戳和时间字符串
|
||
var now = new Date();
|
||
var diffMinutes = (now - createTime) / (1000 * 60); // 计算分钟差
|
||
|
||
return diffMinutes > 20 ? '<span style="color: red;">超时</span>' : '<span class="label label-success">未超时</span>';
|
||
;
|
||
},
|
||
searchList: {
|
||
"1": '超时',
|
||
"0": '未超时',
|
||
},
|
||
searchable: true,
|
||
custom: {
|
||
'1': 'danger',
|
||
'0': 'success'
|
||
},
|
||
operate: '=',
|
||
},
|
||
{
|
||
field: 'dispatch_type',
|
||
title: '派单方式',
|
||
searchList: {
|
||
"1": '手动派单',
|
||
"2": '自动派单',
|
||
},
|
||
formatter: Table.api.formatter.label
|
||
},
|
||
{field: 'item_title', title: __('Item_title'), operate: false},
|
||
|
||
|
||
{field: 'user.nickname', title: '录单员',operate: 'LIKE'},
|
||
{field: 'customer', title: __('Customer'), operate: 'LIKE'},
|
||
{field: 'tel', title: __('Tel'), operate: 'LIKE'},
|
||
{field: 'area.merge_name', title: __('Area_id'), searchable: false},
|
||
{
|
||
field: 'address',
|
||
title: __('Address'),
|
||
operate: false,
|
||
table: table,
|
||
class: 'autocontent',
|
||
formatter: Table.api.formatter.content
|
||
},
|
||
|
||
{
|
||
field: 'receive_type',
|
||
title: '收款方式',
|
||
searchList: {
|
||
"1": '已收定金',
|
||
"2": '已收全款',
|
||
},
|
||
formatter: Table.api.formatter.label
|
||
},
|
||
|
||
{
|
||
field: 'total',
|
||
title: '总收款',
|
||
operate: false,
|
||
formatter: Table.api.formatter.Text
|
||
},
|
||
|
||
// {field: 'worker_id', title: __('Worker_id')},
|
||
{field: 'source_shop', title: __('Source')},
|
||
// {field: 'source_uid', title: __('Source_uid'), operate: 'LIKE'},
|
||
{
|
||
field: 'detail',
|
||
title: __('Detail'),
|
||
operate: false,
|
||
table: table,
|
||
class: 'autocontent',
|
||
formatter: Table.api.formatter.content
|
||
},
|
||
{
|
||
field: 'remark',
|
||
title: __('Remark'),
|
||
operate: false,
|
||
table: table,
|
||
class: 'autocontent',
|
||
formatter: Table.api.formatter.content
|
||
},
|
||
{
|
||
field: 'images',
|
||
title: __('Images'),
|
||
operate: false,
|
||
events: Table.api.events.image,
|
||
formatter: Table.api.formatter.images
|
||
},
|
||
{
|
||
field: 'update_time',
|
||
title: __('Update_time'),
|
||
operate: false,
|
||
addclass: 'datetimerange',
|
||
autocomplete: false
|
||
},
|
||
|
||
{
|
||
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: 'operate',
|
||
title: __('Operate'),
|
||
table: table,
|
||
events: Table.api.events.operate,
|
||
formatter: Table.api.formatter.operate,
|
||
align: "left",
|
||
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',
|
||
dropdown: "更多",
|
||
visible: function (row) {
|
||
if (Config.permissions.edit && row.status != 60 && row.status != 70) {
|
||
return true;
|
||
}
|
||
return false;
|
||
},
|
||
|
||
},
|
||
{
|
||
name: 'push',
|
||
icon: 'fa fa-copy',
|
||
title: '复制订单',
|
||
text: "复制订单",
|
||
url: 'order/copy',
|
||
extend: 'data-toggle="tooltip" data-container="body"',
|
||
classname: 'btn btn-dialog',
|
||
callback: function ($data) {
|
||
// console.log($data);
|
||
},
|
||
visible:function (row) {
|
||
return Config.permissions.copy;
|
||
},
|
||
dropdown: "更多"
|
||
},
|
||
|
||
{
|
||
name: "warning",
|
||
text: "发起异常",
|
||
title: "发起异常",
|
||
extend: 'data-toggle="tooltip" data-container="body"',
|
||
classname: 'btn btn-xs btn-dialog',
|
||
icon: 'fa fa-add',
|
||
url: 'order/warning',
|
||
visible: function (row) {
|
||
if (Config.permissions.warning && row.status == 10) {
|
||
return true;
|
||
}
|
||
return false;
|
||
},
|
||
dropdown: "更多",
|
||
refresh: true,
|
||
},
|
||
{
|
||
name: "mapdispatch",
|
||
text: "地图派单",
|
||
title: "派单",
|
||
extend: 'data-toggle="tooltip" data-container="body"',
|
||
classname: 'btn btn-xs btn-info btn-dialog',
|
||
icon: 'fa fa-add',
|
||
url: function (row) {
|
||
return 'orders/dispatch/map?order_id=' + row.id;
|
||
},
|
||
visible: function (row) {
|
||
if (Config.permissions.map && row.status == 10) {
|
||
return true;
|
||
}
|
||
return false;
|
||
},
|
||
callback: function () {
|
||
table.bootstrapTable('refresh');
|
||
}
|
||
},
|
||
{
|
||
name: "dispatch",
|
||
text: "派单",
|
||
title: "派单",
|
||
extend: 'data-toggle="tooltip" data-container="body"',
|
||
classname: 'btn btn-xs btn-info btn-dialog',
|
||
icon: 'fa fa-add',
|
||
url: function (row) {
|
||
return 'orders/dispatch/add?order_id=' + row.id;
|
||
},
|
||
visible: function (row) {
|
||
if (Config.permissions.dispatch && row.status == 10) {
|
||
return true;
|
||
}
|
||
return false;
|
||
},
|
||
refresh: true,
|
||
},
|
||
{
|
||
name: "reminder",
|
||
text: "催单",
|
||
title: "催单",
|
||
extend: 'data-toggle="tooltip" data-container="body"',
|
||
classname: 'btn btn-ajax',
|
||
icon: 'fa fa-bolt',
|
||
url: 'order/reminder',
|
||
dropdown: "更多",
|
||
success: function (data, ret) {
|
||
if (ret.code === 1) {
|
||
Layer.alert('催单成功');
|
||
} else {
|
||
Layer.alert(ret.msg);
|
||
}
|
||
return false;
|
||
//如果需要阻止成功提示,则必须使用return false;
|
||
//return false;
|
||
},
|
||
error: function (data, ret) {
|
||
Layer.alert(ret.msg);
|
||
return false;
|
||
},
|
||
visible: function (row) {
|
||
if (Config.permissions.reminder && row.status == 10) {
|
||
return true;
|
||
}
|
||
return false;
|
||
},
|
||
},
|
||
{
|
||
name: "copy_text",
|
||
text: "复制信息",
|
||
title: "复制信息",
|
||
extend: 'data-toggle="tooltip" data-container="body"',
|
||
classname: 'btn btn-xs btn-info btn-click',
|
||
icon: 'fa fa-text',
|
||
click: function (data, item) {
|
||
const text = assembleOrderMessage(item);
|
||
copyToClipboard(text);
|
||
}
|
||
}, {
|
||
name: 'delete',
|
||
text: '取消订单',
|
||
title: '取消订单',
|
||
classname: 'btn btn-dialog',
|
||
icon: 'fa fa-trash',
|
||
url: 'order/delete',
|
||
dropdown: "更多",
|
||
visible: function (row) {
|
||
if (Config.permissions.delete && row.status >= 0 && row.status < 60) {
|
||
return true;
|
||
}
|
||
return false;
|
||
},
|
||
},
|
||
{
|
||
name: "error",
|
||
text: "订单报错",
|
||
title: "订单报错",
|
||
extend: 'data-toggle="tooltip" data-container="body"',
|
||
classname: 'btn btn-dialog',
|
||
icon: 'fa fa-bolt',
|
||
url: 'order/addAbnormal',
|
||
refresh: true,
|
||
dropdown: "更多",
|
||
visible: function (row) {
|
||
if (Config.permissions.addAbnormal && row.status != 60 && row.status != 70) {
|
||
return true;
|
||
}
|
||
return false;
|
||
},
|
||
},
|
||
{
|
||
name: "log",
|
||
text: "订单记录",
|
||
title: "订单记录",
|
||
extend: 'data-toggle="tooltip" data-container="body"',
|
||
classname: 'btn btn-xs btn-dialog',
|
||
// icon: 'fa fa-bolt',
|
||
url: 'order/log',
|
||
dropdown: "更多",
|
||
},
|
||
{
|
||
name: 'invoice',
|
||
text: '申请开票',
|
||
title: '申请开票',
|
||
classname: 'btn btn-dialog',
|
||
icon: 'fa fa-send',
|
||
url: 'order/invoice/',
|
||
dropdown: "更多",
|
||
|
||
visible: function (row) {
|
||
if (Config.permissions.invoice && row.status == 60 || row.status == 70) {
|
||
return true;
|
||
}
|
||
return false;
|
||
},
|
||
},
|
||
{
|
||
name: 'aftersale',
|
||
text: '申请售后',
|
||
title: '申请售后',
|
||
classname: 'btn btn-dialog',
|
||
icon: 'fa fa-phone-square',
|
||
url: 'aftersales/aftersale/add',
|
||
dropdown: "更多",
|
||
visible: function (row) {
|
||
if (Config.permissions.aftersale && row.status !== -10 && row.status !== 70 && row.aftersale_id <= 0) {
|
||
return true;
|
||
}
|
||
return false;
|
||
},
|
||
},
|
||
|
||
],
|
||
}
|
||
]
|
||
],
|
||
});
|
||
|
||
// 为表格绑定事件
|
||
Table.api.bindevent(table);
|
||
table.off('dbl-click-row.bs.table');
|
||
let cloneQueryParams = null;
|
||
//绑定tab事件
|
||
$('a[data-toggle="tab"]').on('show.bs.tab', function (event) {
|
||
let type = $(this).attr("data-value");
|
||
|
||
let options = table.bootstrapTable('getOptions');
|
||
$('.nav-tabs li').attr('class', '');
|
||
$(this).parent().attr("class", 'active');
|
||
options.pageNumber = 1;
|
||
if (cloneQueryParams == null) {
|
||
cloneQueryParams = options.queryParams;
|
||
}
|
||
if (type == 1){
|
||
$("form.form-commonsearch [name='status']").val(10).trigger("change");
|
||
$("form.form-commonsearch [name='is_timeout']").val('').trigger("change");
|
||
}else if(type == 2){
|
||
$("form.form-commonsearch [name='status']").val(10).trigger("change");
|
||
$("form.form-commonsearch [name='is_timeout']").val(1).trigger("change");
|
||
}else {
|
||
$("form.form-commonsearch [name='status']").val('').trigger("change");
|
||
$("form.form-commonsearch [name='is_timeout']").val('').trigger("change");
|
||
}
|
||
table.bootstrapTable('refresh', {});
|
||
return false;
|
||
});
|
||
let idleTimer = null;
|
||
|
||
function resetIdleTimer() {
|
||
clearTimeout(idleTimer);
|
||
idleTimer = setTimeout(function () {
|
||
// 30秒无操作后执行刷新
|
||
table.bootstrapTable('refresh', {});
|
||
}, 30 * 1000); // 30 秒
|
||
}
|
||
|
||
['mousemove', 'keydown', 'click', 'scroll'].forEach(function (event) {
|
||
document.addEventListener(event, resetIdleTimer, false);
|
||
});
|
||
$("#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);
|
||
});
|
||
resetIdleTimer();
|
||
|
||
|
||
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
|
||
});
|
||
|
||
},
|
||
add: function () {
|
||
$("#mybuttom").on("click", function () {
|
||
const res = $("form[role=form]").isValid();
|
||
if ($('#lng').val() && $('#lng').val() !== '') {
|
||
if (res) {
|
||
Form.api.submit($("form[role=form]"), function (data) {
|
||
copyToClipboard(assembleOrderMessage(data));
|
||
Toastr.success('录入成功');
|
||
return false;
|
||
});
|
||
}
|
||
} else {
|
||
Toastr.success('请选择有效地址');
|
||
}
|
||
|
||
return false;
|
||
});
|
||
|
||
$("#mysubmit").on("click", function () {
|
||
const res = $("form[role=form]").isValid();
|
||
|
||
if ($('#lng').val() && $('#lng').val() !== '') {
|
||
if (res) {
|
||
// Form.api.submit($("form[role=form]"), function (data, ret) {
|
||
// clearInfo();
|
||
// copyToClipboard(assembleOrderMessage(data));
|
||
// Toastr.success('录入成功');
|
||
// return false;
|
||
// });
|
||
Form.api.submit($("form[role=form]"), function (data, ret) {
|
||
// 成功提交后重新加载当前页面
|
||
copyToClipboard(assembleOrderMessage(data));
|
||
Toastr.success('录入成功');
|
||
location.reload();
|
||
return false;
|
||
});
|
||
}
|
||
} else {
|
||
Toastr.success('请选择有效地址');
|
||
}
|
||
return false;
|
||
});
|
||
|
||
$("#smart").on("click", function () {
|
||
Fast.api.ajax({
|
||
url: "order/smart", // 你的 API 地址
|
||
type: "post",
|
||
contentType: 'application/json',
|
||
dataType: "json",
|
||
data: JSON.stringify({
|
||
str: $('#smart_text').val()
|
||
})
|
||
}, function (data) {
|
||
if (data.mobile !== '') {
|
||
$('#c-tel').val(data.mobile);
|
||
}
|
||
// if (data.name !== '') {
|
||
// $('#c-customer').val(data.name);
|
||
// }
|
||
if (data.item.id !== 0) {
|
||
$('#item_id_value').val(data.item.id);
|
||
$('#item_id').val(data.item.item);
|
||
}
|
||
if (data.idn) {
|
||
$('#c-source-id').val(data.idn);
|
||
}
|
||
if (data.lat) {
|
||
$("#lat").val(data.lat);
|
||
}
|
||
if (data.lng) {
|
||
$("#lng").val(data.lng);
|
||
}
|
||
if (data.area_id) {
|
||
$("#area_id").val(data.area_id);
|
||
}
|
||
if (data.plan_time) {
|
||
$("#time").val(data.plan_time);
|
||
}
|
||
if (data.addr && data.addr !== '') {
|
||
$("#c-address").val(data.addr);
|
||
$("#area_name").val(data.addr);
|
||
}
|
||
if (data.ext){
|
||
Toastr.error('该客户电话为分机号,请输入客户真实号码!');
|
||
|
||
}else {
|
||
Toastr.info('识别成功');
|
||
}
|
||
return false;
|
||
});
|
||
});
|
||
Controller.api.bindevent();
|
||
Controller.api.map();
|
||
},
|
||
edit: function () {
|
||
Controller.api.bindevent();
|
||
Controller.api.map();
|
||
},
|
||
copy: function () {
|
||
Controller.api.bindevent();
|
||
Controller.api.map();
|
||
},
|
||
saleafter: function () {
|
||
Controller.api.bindevent();
|
||
},
|
||
addabnormal: function () {
|
||
Form.api.bindevent($("#add-form"));
|
||
},
|
||
warning: function () {
|
||
Form.api.bindevent($("#add-form"));
|
||
},
|
||
delete: function () {
|
||
Form.api.bindevent($("form[role=form]"));
|
||
},
|
||
invoice: function () {
|
||
function toggleInvoiceFields() {
|
||
const type = $('#c-source').val();
|
||
if (type === '1') {
|
||
// 公司发票
|
||
$('#c-tax_number').closest('.form-group').show();
|
||
$('#c-company_address').closest('.form-group').show();
|
||
$('#c-company_phone').closest('.form-group').show();
|
||
$('#c-bank_name').closest('.form-group').show();
|
||
$('#c-bank_account').closest('.form-group').show();
|
||
} else {
|
||
// 个人发票
|
||
$('#c-tax_number').closest('.form-group').hide();
|
||
$('#c-company_address').closest('.form-group').hide();
|
||
$('#c-company_phone').closest('.form-group').hide();
|
||
$('#c-bank_name').closest('.form-group').hide();
|
||
$('#c-bank_account').closest('.form-group').hide();
|
||
}
|
||
}
|
||
|
||
// 初始化时执行一次
|
||
toggleInvoiceFields();
|
||
|
||
// 监听 select 改变
|
||
$('#c-source').on('change', function () {
|
||
toggleInvoiceFields();
|
||
});
|
||
Form.api.bindevent($("form[role=form]"));
|
||
},
|
||
api: {
|
||
bindevent: function () {
|
||
Form.api.bindevent($("form[role=form]"));
|
||
},
|
||
map: function () {
|
||
$("#c-city").on("cp:updated", function () {
|
||
var citypicker = $(this).data("citypicker");
|
||
var code = citypicker.getCode("district") || citypicker.getCode("city") || citypicker.getCode("province");
|
||
|
||
$("#area_id").val(code);
|
||
$("#area_name").val(citypicker.getVal());
|
||
});
|
||
$(document).on('click', "#area_map", function (e) {
|
||
var that = this;
|
||
var callback = $(that).data('callback');
|
||
var input_id = $(that).data("input-id") ? $(that).data("input-id") : "";
|
||
var lat_id = $(that).data("lat-id") ? $(that).data("lat-id") : "";
|
||
var lng_id = $(that).data("lng-id") ? $(that).data("lng-id") : "";
|
||
var zoom_id = $(that).data("zoom-id") ? $(that).data("zoom-id") : "";
|
||
var lat = lat_id ? $("#" + lat_id).val() : '';
|
||
var lng = lng_id ? $("#" + lng_id).val() : '';
|
||
var city_code = $("#area_id").val();
|
||
var zoom = zoom_id ? $("#" + zoom_id).val() : '';
|
||
var url = "/addons/address/index/select?";
|
||
url += (lat && lng) ? 'lat=' + lat + '&lng=' + lng +
|
||
(input_id ? "&address=" + $("#" + input_id).val() : "")
|
||
+ (zoom ? "&zoom=" + zoom : "") : ''
|
||
;
|
||
if (city_code) {
|
||
url += city_code ? "&city_code=" + city_code : "";
|
||
}
|
||
// console.log(url);
|
||
Fast.api.open(url, '位置选择', {
|
||
callback: function (res, data) {
|
||
input_id && $("#" + input_id).val(res.address).trigger("change");
|
||
lat_id && $("#" + lat_id).val(res.lat).trigger("change");
|
||
lng_id && $("#" + lng_id).val(res.lng).trigger("change");
|
||
zoom_id && $("#" + zoom_id).val(res.zoom).trigger("change");
|
||
$('#area_id').val(res.city_code);
|
||
try {
|
||
//执行回调函数
|
||
if (typeof callback === 'function') {
|
||
callback.call(that, res);
|
||
}
|
||
} catch (e) {
|
||
|
||
}
|
||
}
|
||
});
|
||
|
||
});
|
||
var _data = items;
|
||
$('#item_id').zdCascader({
|
||
data: _data,
|
||
onChange: function ($this, data, allPathData) {
|
||
// console.log(data,allPathData);
|
||
$('#item_id_value').val(data.value);
|
||
}
|
||
});
|
||
$('#item_id').val($('#item_id').data('value'));
|
||
|
||
const mainSelect = document.getElementById('receive_type');
|
||
const otherSelect = document.getElementById('coupon');
|
||
|
||
function toggleOtherSelect() {
|
||
if (mainSelect.value === '2') {
|
||
otherSelect.style.display = 'none';
|
||
} else {
|
||
otherSelect.style.display = '';
|
||
}
|
||
}
|
||
|
||
mainSelect.addEventListener('change', toggleOtherSelect);
|
||
toggleOtherSelect();
|
||
|
||
$('input[name="row[set_time]"]').on('change', toggleTime);
|
||
|
||
function toggleTime() {
|
||
var val = $('input[name="row[set_time]"]:checked').val();
|
||
if (val == 1) {
|
||
$('#set-time').show();
|
||
} else {
|
||
$('#set-time').hide();
|
||
}
|
||
}
|
||
|
||
toggleTime();
|
||
|
||
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;
|
||
|
||
});
|