e94f4f10
“wangming”
feat: 实现给金三角添加用户绑...
|
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
|
<script>
// 全局变量
let currentSelectField = null;
let currentOptions = [];
let selectedOption = null;
let userInfo = null;
// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', function() {
initializePage();
bindEvents();
});
// 初始化页面
async function initializePage() {
// 获取当前登录用户 通过接口获取
userInfo = JSON.parse(localStorage.getItem('userInfo'));
if (!userInfo || Object.keys(userInfo).length === 0) {
// 没有登录,跳转到登录页面
window.location.href = 'login.html';
return;
}
console.log(userInfo);
// 设置预约日期默认为今天
setDefaultDate();
}
// 设置默认日期为今天
function setDefaultDate() {
const today = new Date();
const year = today.getFullYear();
const month = (today.getMonth() + 1).toString().padStart(2, '0');
const day = today.getDate().toString().padStart(2, '0');
const dateString = `${year}-${month}-${day}`;
const weekDays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
const weekDay = weekDays[today.getDay()];
const label = `${month}月${day}日 ${weekDay} (今天)`;
// 设置输入框显示值和数据
const dateInput = document.getElementById('yyrq');
dateInput.value = label;
dateInput.dataset.selectedValue = dateString;
}
async function getUserInfo() {
try {
const apiUrl = `${APP_CONFIG.getApiBaseUrl()}/api/oauth/CurrentUser`;
const response = await fetch(apiUrl, {
method: 'GET',
headers: {
'Authorization': `${getAuthToken()}`,
'Content-Type': 'application/json'
},
});
if (response.ok) {
const result = await response.json();
if (result.code == 200 && result.data) {
return result.data.userInfo
}
}
return {};
} catch (error) {
console.error('获取门店列表出错:', error);
return {};
}
}
// 绑定事件
function bindEvents() {
// 绑定选择输入框点击事件
const selectInputs = document.querySelectorAll('.select-input');
selectInputs.forEach(input => {
input.addEventListener('click', function() {
openSelectModal(this);
});
});
// 绑定表单提交事件
document.getElementById('appointmentForm').addEventListener('submit', handleFormSubmit);
// 绑定搜索输入框事件
document.getElementById('searchInput').addEventListener('input', handleSearch);
}
// 打开选择弹窗
async function openSelectModal(inputField) {
currentSelectField = inputField;
const fieldId = inputField.id;
// 显示加载状态
showModal('加载中...', []);
try {
let options = [];
// 根据字段类型获取不同的选项数据
switch(fieldId) {
case 'djmd':
options = await getStoreOptions();
showModal('选择门店', options);
break;
case 'yytyxm':
options = await getProjectOptions();
showModal('选择体验项目', options);
break;
case 'gkxm':
options = await getCustomerOptions();
showModal('选择顾客姓名', options);
break;
case 'yyjks':
options = await getHealthWorkerOptions();
showModal('选择健康师', options);
break;
case 'yyrq':
options = getDateOptions();
showModal('选择预约日期', options);
break;
case 'yysj':
options = getTimeSlotOptions();
showModal('选择预约时间', options);
break;
}
} catch (error) {
console.error('获取选项数据失败:', error);
showModal('加载失败', []);
// 显示错误提示
setTimeout(() => {
showMessage('数据加载失败,请检查网络连接或联系管理员', 'error');
}, 500);
}
}
// 显示弹窗
function showModal(title, options) {
currentOptions = options;
document.getElementById('modalTitle').textContent = title;
document.getElementById('searchInput').value = '';
// 如果是加载状态,隐藏搜索框
const searchInput = document.getElementById('searchInput');
if (title === '加载中...' || title === '加载失败') {
searchInput.style.display = 'none';
} else {
searchInput.style.display = 'block';
}
renderOptions(options);
document.getElementById('selectModal').style.display = 'flex';
}
// 关闭弹窗
function closeModal() {
document.getElementById('selectModal').style.display = 'none';
currentSelectField = null;
currentOptions = [];
selectedOption = null;
}
// 渲染选项列表
function renderOptions(options) {
const optionList = document.getElementById('optionList');
optionList.innerHTML = '';
if (!options || options.length === 0) {
const emptyMessage = document.createElement('div');
emptyMessage.className = 'loading-message';
emptyMessage.innerHTML = '<span class="loading-spinner"></span>暂无数据';
optionList.appendChild(emptyMessage);
return;
}
options.forEach(option => {
const optionItem = document.createElement('div');
optionItem.className = 'option-item';
optionItem.textContent = option.label;
optionItem.dataset.value = option.value;
// 存储额外的属性到dataset中
Object.keys(option).forEach(key => {
if (key !== 'value' && key !== 'label') {
optionItem.dataset[key] = option[key];
}
});
optionItem.addEventListener('click', function() {
// 移除之前的选中状态
document.querySelectorAll('.option-item').forEach(item => {
item.classList.remove('selected');
});
// 设置当前选中状态
this.classList.add('selected');
selectedOption = {
value: this.dataset.value,
label: this.textContent,
khlx: this.dataset.khlx,
khmc: this.dataset.khmc
};
});
optionList.appendChild(optionItem);
});
}
// 处理搜索
function handleSearch() {
const searchTerm = document.getElementById('searchInput').value;
if (!searchTerm) {
// 如果搜索词为空,显示所有选项
renderOptions(currentOptions);
return;
}
const filteredOptions = currentOptions.filter(option =>
option.label && option.label.toLowerCase().includes(searchTerm.toLowerCase())
);
renderOptions(filteredOptions);
}
// 确认选择
function confirmSelection() {
if (selectedOption && currentSelectField) {
currentSelectField.value = selectedOption.label;
currentSelectField.dataset.selectedValue = selectedOption.value;
// 设置额外的属性到输入框的dataset中
Object.keys(selectedOption).forEach(key => {
if (key !== 'value' && key !== 'label') {
currentSelectField.dataset[key] = selectedOption[key];
}
});
closeModal();
} else {
showMessage('请先选择一个选项', 'warning');
}
}
// 获取门店选项(从API获取)
async function getStoreOptions() {
try {
const apiUrl = `${APP_CONFIG.getApiBaseUrl()}/api/Extend/LqMdxx?page=1&pageSize=1000`;
const response = await fetch(apiUrl, {
method: 'GET',
headers: {
'Authorization': `${getAuthToken()}`,
'Content-Type': 'application/json'
},
});
if (response.ok) {
const result = await response.json();
if (result.code == 200 && result.data) {
return result.data.list.map(item => ({
value: item.id,
label: item.dm
}));
}
}
// 如果API调用失败,返回空数组
console.warn('获取门店列表失败,使用默认数据');
return getDefaultStoreOptions();
} catch (error) {
console.error('获取门店列表出错:', error);
return getDefaultStoreOptions();
}
}
// 获取体验项目选项(从API获取)
async function getProjectOptions() {
try {
const apiUrl = `${APP_CONFIG.getApiBaseUrl()}/api/Extend/LqXmzl?page=1&pageSize=1000`;
const response = await fetch(apiUrl, {
method: 'GET',
headers: {
'Authorization': `${getAuthToken()}`,
'Content-Type': 'application/json'
}
});
if (response.ok) {
const result = await response.json();
if (result.code == 200 && result.data) {
return result.data.list.map(item => ({
value: item.id,
label: item.xmmc
}));
}
}
// 如果API调用失败,返回默认数据
console.warn('获取项目列表失败,使用默认数据');
return getDefaultProjectOptions();
} catch (error) {
console.error('获取项目列表出错:', error);
return getDefaultProjectOptions();
}
}
// 获取顾客姓名选项(从API获取)
async function getCustomerOptions() {
try {
const apiUrl = `${APP_CONFIG.getApiBaseUrl()}/api/Extend/LqKhxx?page=1&pageSize=1000`;
const response = await fetch(apiUrl, {
method: 'GET',
headers: {
'Authorization': `${getAuthToken()}`,
'Content-Type': 'application/json'
}
});
if (response.ok) {
const result = await response.json();
if (result.code == 200 && result.data) {
return result.data.list.map(item => ({
value: item.id,
label: item.khmc,
khlx: item.khlx,
khmc: item.khmc // 添加这一行,将客户名称也保存
}));
}
}
// 如果API调用失败,返回默认数据
console.warn('获取顾客列表失败,使用默认数据');
return getDefaultCustomerOptions();
} catch (error) {
console.error('获取顾客列表出错:', error);
return getDefaultCustomerOptions();
}
}
// 获取健康师选项(从API获取)
async function getHealthWorkerOptions() {
try {
const apiUrl = `${APP_CONFIG.getApiBaseUrl()}/api/Extend/user?page=1&pageSize=1000&mdid=${userInfo.mdid}&gw=健康师`;
const response = await fetch(apiUrl, {
method: 'GET',
headers: {
'Authorization': `${getAuthToken()}`,
'Content-Type': 'application/json'
}
});
if (response.ok) {
const result = await response.json();
if (result.code == 200 && result.data) {
return result.data.list.map(item => ({
value: item.id,
label: item.realName
}));
}
}
// 如果API调用失败,返回默认数据
console.warn('获取健康师列表失败,使用默认数据');
return getDefaultHealthWorkerOptions();
} catch (error) {
console.error('获取健康师列表出错:', error);
return getDefaultHealthWorkerOptions();
}
}
// 生成时间段选项(9:00-21:00,每半小时一个间隔)
function getTimeSlotOptions() {
const timeSlots = [];
const startHour = 9;
const endHour = 21;
for (let hour = startHour; hour < endHour; hour++) {
for (let minute = 0; minute < 60; minute += 30) {
const timeString = `${hour.toString().padStart(2, '0')}:${minute.toString().padStart(2, '0')}`;
const nextMinute = minute + 30;
const nextHour = nextMinute >= 60 ? hour + 1 : hour;
const nextMinuteStr = nextMinute >= 60 ? '00' : nextMinute.toString().padStart(2, '0');
const nextTimeString = `${nextHour.toString().padStart(2, '0')}:${nextMinuteStr}`;
const timeRange = `${timeString}-${nextTimeString}`;
timeSlots.push({
value: timeRange,
label: timeRange
});
}
}
return timeSlots;
}
// 生成日期选项(从今天开始,未来30天)
function getDateOptions() {
const dateOptions = [];
const today = new Date();
for (let i = 0; i < 30; i++) {
const date = new Date(today);
date.setDate(today.getDate() + i);
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');
const dateString = `${year}-${month}-${day}`;
const weekDays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
const weekDay = weekDays[date.getDay()];
let label = `${month}月${day}日 ${weekDay}`;
if (i === 0) {
label += ' (今天)';
} else if (i === 1) {
label += ' (明天)';
}
dateOptions.push({
value: dateString,
label: label
});
}
return dateOptions;
}
// 默认健康师选项(API失败时的备用数据)
function getDefaultHealthWorkerOptions() {
return [
{ value: 'jks001', label: '张健康师' },
{ value: 'jks002', label: '李健康师' },
{ value: 'jks003', label: '王健康师' },
{ value: 'jks004', label: '赵健康师' },
{ value: 'jks005', label: '钱健康师' }
];
}
// 默认门店选项(API失败时的备用数据)
function getDefaultStoreOptions() {
return [
{ value: 'store001', label: '绿纤旗舰店' },
{ value: 'store002', label: '绿纤体验店' },
{ value: 'store003', label: '绿纤服务中心' },
{ value: 'store004', label: '绿纤直营店' }
];
}
// 默认项目选项(API失败时的备用数据)
function getDefaultProjectOptions() {
return [
{ value: 'proj001', label: '基础护理' },
{ value: 'proj002', label: '深度清洁' },
{ value: 'proj003', label: '美白护理' },
{ value: 'proj004', label: '抗衰护理' },
{ value: 'proj005', label: '补水保湿' }
];
}
// 默认顾客选项(API失败时的备用数据)
function getDefaultCustomerOptions() {
return [
{ value: 'cust001', label: '张三', khlx: 'VIP' },
{ value: 'cust002', label: '李四', khlx: '普通' },
{ value: 'cust003', label: '王五', khlx: 'VIP' },
{ value: 'cust004', label: '赵六', khlx: '普通' },
{ value: 'cust005', label: '钱七', khlx: 'VIP' }
];
}
// 处理表单提交
function handleFormSubmit(event) {
event.preventDefault();
if(!document.getElementById('yytyxm').dataset.selectedValue){
showMessage('请选择体验项目', 'error');
return;
}
if(!document.getElementById('gkxm').dataset.selectedValue){
showMessage('请选择顾客姓名', 'error');
return;
}
if(!document.getElementById('yyjks').dataset.selectedValue){
showMessage('请选择健康师', 'error');
return;
}
if(!document.getElementById('yyrq').dataset.selectedValue){
showMessage('请选择预约日期', 'error');
return;
}
if(!document.getElementById('yysj').dataset.selectedValue){
showMessage('请选择预约时间', 'error');
return;
}
// 收集表单数据
const selectedDate = document.getElementById('yyrq').dataset.selectedValue;
const selectedTimeRange = document.getElementById('yysj').dataset.selectedValue;
// 解析时间段,获取开始时间和结束时间
const [startTime, endTime] = selectedTimeRange.split('-');
// 组合成完整的开始时间和结束时间
const startDateTime = `${selectedDate} ${startTime}`;
const endDateTime = `${selectedDate} ${endTime}`;
const formData = {
djmd: userInfo.mdid || "",
yyr: userInfo.userId || "",
yytyxm: document.getElementById('yytyxm').dataset.selectedValue,
gk: document.getElementById('gkxm').dataset.selectedValue,
gklx: document.getElementById('gkxm').dataset.khlx,
gkxm: document.getElementById('gkxm').dataset.khmc,
yyjks: document.getElementById('yyjks').dataset.selectedValue,
yysj: startDateTime, // 开始时间:2025-10-09 19:00
yyjs: endDateTime, // 结束时间:2025-10-09 19:30
f_Status:"已预约"
};
console.log(formData);
// 提交数据到API
submitAppointment(formData);
}
// 提交预约数据
function submitAppointment(data) {
const apiUrl = `${APP_CONFIG.getApiBaseUrl()}/api/Extend/LqYyjl`;
fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `${getAuthToken()}`
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then(result => {
if (result.code == 200) {
// 显示成功提示
showMessage('预约添加成功!', 'success');
clearForm();
} else {
// 显示失败提示
showMessage(`添加失败:${result.message || '未知错误'}`, 'error');
}
})
.catch(error => {
console.error('提交失败:', error);
// 显示网络错误提示
showMessage('网络错误,请稍后重试', 'error');
});
}
// 清空表单
function clearForm() {
document.getElementById('appointmentForm').reset();
// 清空选择字段的数据
const selectInputs = document.querySelectorAll('.select-input');
selectInputs.forEach(input => {
input.value = '';
delete input.dataset.selectedValue;
delete input.dataset.khlx;
delete input.dataset.khmc;
});
// 重新设置默认日期
setDefaultDate();
}
// 返回上一页
function goBack() {
if (window.history.length > 1) {
window.history.back();
} else {
window.location.href = 'index.html';
}
}
// 点击弹窗外部关闭弹窗
document.getElementById('selectModal').addEventListener('click', function(e) {
if (e.target === this) {
closeModal();
}
});
// 显示消息提示
function showMessage(message, type = 'info') {
const toast = document.getElementById('messageToast');
toast.textContent = message;
toast.className = `message-toast ${type}`;
// 显示提示
setTimeout(() => {
toast.classList.add('show');
}, 100);
// 自动隐藏
setTimeout(() => {
toast.classList.remove('show');
}, 3000);
}
// 取消预约
function cancelAppointment() {
// 清空表单
clearForm();
// // 显示取消提示
// showMessage('已取消预约', 'info');
window.location.href = 'index.html';
}
</script>
|