|
@@ -1,3 +1,420 @@
|
|
|
+// 显示分发模态框的函数
|
|
|
+function showDistributeModal(item) {
|
|
|
+ const modal = document.getElementById('DistributeModal');
|
|
|
+
|
|
|
+ // 显示模态框
|
|
|
+ modal.style.display = 'flex';
|
|
|
+
|
|
|
+ let usersInfo;
|
|
|
+ // 获取用户信息并填充第一个用户输入框
|
|
|
+ fetch('http://127.0.0.1:8080/api/admin/distributeLicenseByUserInfo', {
|
|
|
+ method: 'GET',
|
|
|
+ headers: {
|
|
|
+ 'Authorization': `Bearer ${authToken}`, // 确保autoToken是您获取的用户授权令牌
|
|
|
+ 'Content-Type': 'application/json'
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .then(response => {
|
|
|
+ if (!response.ok) {
|
|
|
+ throw new Error('Network response was not ok');
|
|
|
+ }
|
|
|
+ return response.json();
|
|
|
+ })
|
|
|
+ .then(data => {
|
|
|
+ usersInfo = data.data; // 假设 data 是返回的用户信息数组或对象
|
|
|
+ console.log('用户信息:', usersInfo); // 打印用户信息
|
|
|
+
|
|
|
+ // 调用处理上半部分(邮箱输入框)的函数
|
|
|
+ handleEmailSection(item,usersInfo);
|
|
|
+
|
|
|
+ // 调用处理下半部分(用户输入框)的函数
|
|
|
+ handleUserSection(item, usersInfo);
|
|
|
+
|
|
|
+ })
|
|
|
+ .catch(error => {
|
|
|
+ console.error('获取用户信息失败:', error);
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // 绑定关闭事件
|
|
|
+ const closeModalBtn = document.querySelector('.DistributeModal-close');
|
|
|
+ closeModalBtn.addEventListener('click', closeDistributeModal);
|
|
|
+
|
|
|
+ // 点击模态框外部关闭
|
|
|
+ window.addEventListener('click', (event) => {
|
|
|
+ if (event.target === modal) {
|
|
|
+ closeDistributeModal();
|
|
|
+ }
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+function handleEmailSection(item,usersInfo) {
|
|
|
+ console.log('handleEmailSection 用户信息:', usersInfo); // 打印用户信息
|
|
|
+ const emailInputs = document.getElementById('DistributeModal-email-inputs');
|
|
|
+ emailInputs.innerHTML = ''; // 清空之前添加的邮箱输入框
|
|
|
+
|
|
|
+ // 设置复选框标签内容
|
|
|
+ const salesEmailLabel = document.getElementById('salesEmailCheckbox').parentElement;
|
|
|
+ salesEmailLabel.innerHTML = `
|
|
|
+ <input type="checkbox" id="salesEmailCheckbox" class="DistributeModal-checkbox">
|
|
|
+ <span class="DistributeModal-checkbox-custom"></span>
|
|
|
+ 销售邮箱 (${item.SalesPerson}: ${item.SalesEmail})
|
|
|
+ `;
|
|
|
+
|
|
|
+ const supportEmailLabel = document.getElementById('supportEmailCheckbox').parentElement;
|
|
|
+ supportEmailLabel.innerHTML = `
|
|
|
+ <input type="checkbox" id="supportEmailCheckbox" class="DistributeModal-checkbox">
|
|
|
+ <span class="DistributeModal-checkbox-custom"></span>
|
|
|
+ 运维邮箱 (${item.SupportPerson}: ${item.SupportEmail})
|
|
|
+ `;
|
|
|
+
|
|
|
+ // 防止重复绑定事件,先移除任何现有的点击事件监听器
|
|
|
+ const addEmailBtn = document.getElementById('DistributeModal-add-email-btn');
|
|
|
+ addEmailBtn.replaceWith(addEmailBtn.cloneNode(true)); // 通过克隆移除所有之前绑定的事件
|
|
|
+ const newAddEmailBtn = document.getElementById('DistributeModal-add-email-btn');
|
|
|
+
|
|
|
+ // 绑定一次点击事件,动态添加邮箱输入框
|
|
|
+ newAddEmailBtn.addEventListener('click', () => {
|
|
|
+ const emailWrapper = document.createElement('div');
|
|
|
+ emailWrapper.className = 'DistributeModal-email-input-wrapper';
|
|
|
+
|
|
|
+ const newEmailInput = document.createElement('input');
|
|
|
+ newEmailInput.type = 'email';
|
|
|
+ newEmailInput.placeholder = '输入分发邮箱';
|
|
|
+ newEmailInput.className = 'DistributeModal-email-input';
|
|
|
+
|
|
|
+ // 创建删除按钮
|
|
|
+ const removeEmailBtn = document.createElement('button');
|
|
|
+ removeEmailBtn.className = 'DistributeModal-remove-email-btn';
|
|
|
+ removeEmailBtn.innerHTML = '×'; // × 符号
|
|
|
+ removeEmailBtn.addEventListener('click', () => {
|
|
|
+ emailWrapper.remove(); // 删除邮箱输入框
|
|
|
+ });
|
|
|
+
|
|
|
+ // 将输入框和删除按钮加入到 emailWrapper 中
|
|
|
+ emailWrapper.appendChild(newEmailInput);
|
|
|
+ emailWrapper.appendChild(removeEmailBtn);
|
|
|
+
|
|
|
+ // 将 emailWrapper 加入到 emailInputs 容器中
|
|
|
+ emailInputs.appendChild(emailWrapper);
|
|
|
+ });
|
|
|
+
|
|
|
+ // 内部函数:检查是否存在重复项
|
|
|
+ function findDuplicates(arr) {
|
|
|
+ const seen = new Set();
|
|
|
+ const duplicates = [];
|
|
|
+
|
|
|
+ arr.forEach((email) => {
|
|
|
+ if (seen.has(email)) {
|
|
|
+ duplicates.push(email);
|
|
|
+ } else {
|
|
|
+ seen.add(email);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ return duplicates;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 点击上半部分的确认按钮
|
|
|
+ const emailConfirmBtn = document.getElementById('DistributeModal-email-confirm-btn');
|
|
|
+ emailConfirmBtn.addEventListener('click', () => {
|
|
|
+ // 收集邮箱信息
|
|
|
+ let emails = Array.from(document.querySelectorAll('.DistributeModal-email-input'))
|
|
|
+ .map(input => input.value.trim());
|
|
|
+
|
|
|
+ // 检查是否勾选销售邮箱或运维邮箱
|
|
|
+ const isSalesEmailChecked = document.getElementById('salesEmailCheckbox').checked;
|
|
|
+ const isSupportEmailChecked = document.getElementById('supportEmailCheckbox').checked;
|
|
|
+
|
|
|
+ if (isSalesEmailChecked) {
|
|
|
+ emails.push(item.SalesEmail); // 如果勾选了销售邮箱,添加到邮箱列表
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isSupportEmailChecked) {
|
|
|
+ emails.push(item.SupportEmail); // 如果勾选了运维邮箱,添加到邮箱列表
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查是否存在重复的邮箱
|
|
|
+ const duplicateEmails = findDuplicates(emails);
|
|
|
+
|
|
|
+ if (duplicateEmails.length > 0) {
|
|
|
+ // 如果有重复的邮箱,弹出提示
|
|
|
+ alert(`以下邮箱重复:\n${duplicateEmails.join('\n')}`);
|
|
|
+ } else {
|
|
|
+ // 没有重复的邮箱,弹出确认发送提示
|
|
|
+ if (confirm("确认发送这些邮件吗?")) {
|
|
|
+ // 执行邮箱分发逻辑
|
|
|
+ // distributeLicense(item, emails, []);
|
|
|
+ distributeEmails(item, emails, usersInfo);
|
|
|
+ closeDistributeModal(); // 关闭模态框
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+function handleUserSection(item, usersInfo) {
|
|
|
+ console.log('handleUserSection 用户信息:', usersInfo); // 打印用户信息
|
|
|
+ const userInputs = document.getElementById('DistributeModal-user-inputs');
|
|
|
+ userInputs.innerHTML = ''; // 清空之前添加的用户选择框
|
|
|
+
|
|
|
+ // 过滤掉 admin 账户
|
|
|
+ const filteredUsersInfo = usersInfo.filter(user => user.Username !== 'admin');
|
|
|
+
|
|
|
+ // 用于存储已经选择的用户
|
|
|
+ let selectedUsersSet = new Set();
|
|
|
+
|
|
|
+ // 绑定新增下拉框按钮
|
|
|
+ const addSelectBtn = document.getElementById('distributeUser-add-select-btn');
|
|
|
+ addSelectBtn.replaceWith(addSelectBtn.cloneNode(true)); // 防止重复绑定
|
|
|
+ const newAddSelectBtn = document.getElementById('distributeUser-add-select-btn');
|
|
|
+
|
|
|
+ // 函数:更新所有下拉框中的可选项
|
|
|
+ const updateSelectOptions = () => {
|
|
|
+ Array.from(document.querySelectorAll('.distributeUser-select')).forEach(select => {
|
|
|
+ const currentValue = select.value; // 当前选中的值
|
|
|
+ select.innerHTML = ''; // 清空选项
|
|
|
+
|
|
|
+ // 添加默认的空选项(必须手动选择)
|
|
|
+ const defaultOption = document.createElement('option');
|
|
|
+ defaultOption.value = ''; // 设置为空值
|
|
|
+ defaultOption.textContent = '请选择用户';
|
|
|
+ select.appendChild(defaultOption);
|
|
|
+
|
|
|
+ filteredUsersInfo.forEach(user => {
|
|
|
+ if (!selectedUsersSet.has(user.UniqueID) || user.UniqueID === currentValue) {
|
|
|
+ const option = document.createElement('option');
|
|
|
+ option.value = user.UniqueID;
|
|
|
+ option.setAttribute('data-username', user.Username);
|
|
|
+ option.setAttribute('data-account', user.Account);
|
|
|
+ option.textContent = `${user.Username} (${user.Account})`;
|
|
|
+
|
|
|
+ // 如果是当前选中的值,保持选中状态
|
|
|
+ if (user.UniqueID === currentValue) {
|
|
|
+ option.selected = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ select.appendChild(option);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+ };
|
|
|
+
|
|
|
+ newAddSelectBtn.addEventListener('click', () => {
|
|
|
+ const selectWrapper = document.createElement('div');
|
|
|
+ selectWrapper.className = 'distributeUser-select-wrapper'; // Flexbox 容器
|
|
|
+
|
|
|
+ const select = document.createElement('select');
|
|
|
+ select.className = 'distributeUser-select'; // 设置下拉框样式
|
|
|
+
|
|
|
+ // 添加一个默认的空选项
|
|
|
+ const defaultOption = document.createElement('option');
|
|
|
+ defaultOption.value = '';
|
|
|
+ defaultOption.textContent = '请选择用户'; // 这个选项提示用户选择
|
|
|
+ defaultOption.selected = true; // 默认选中这个选项
|
|
|
+ select.appendChild(defaultOption);
|
|
|
+
|
|
|
+ // 初始更新下拉框
|
|
|
+ updateSelectOptions();
|
|
|
+
|
|
|
+ // 监听下拉框的变化
|
|
|
+ select.addEventListener('change', () => {
|
|
|
+ const previousValue = select.dataset.previousValue;
|
|
|
+
|
|
|
+ // 如果之前有选中,移除旧值
|
|
|
+ if (previousValue) {
|
|
|
+ selectedUsersSet.delete(previousValue);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果有新的选择,添加到 set 中
|
|
|
+ if (select.value) {
|
|
|
+ selectedUsersSet.add(select.value);
|
|
|
+ select.dataset.previousValue = select.value; // 保存当前值为下次移除准备
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新其他下拉框的选项
|
|
|
+ updateSelectOptions();
|
|
|
+ });
|
|
|
+
|
|
|
+ // 创建删除按钮,使用与关闭邮件输入框相同的样式
|
|
|
+ const removeSelectBtn = document.createElement('button');
|
|
|
+ removeSelectBtn.className = 'DistributeModal-remove-email-btn'; // 使用关闭邮件输入框的按钮样式
|
|
|
+ removeSelectBtn.innerHTML = '×';
|
|
|
+ removeSelectBtn.addEventListener('click', () => {
|
|
|
+ const previousValue = select.dataset.previousValue;
|
|
|
+ if (previousValue) {
|
|
|
+ selectedUsersSet.delete(previousValue); // 删除时从选中集中移除
|
|
|
+ }
|
|
|
+ selectWrapper.remove(); // 删除下拉框
|
|
|
+ updateSelectOptions(); // 更新其他下拉框的选项
|
|
|
+ });
|
|
|
+
|
|
|
+ // 将下拉框和删除按钮加入到 selectWrapper 中
|
|
|
+ selectWrapper.appendChild(select);
|
|
|
+ selectWrapper.appendChild(removeSelectBtn);
|
|
|
+
|
|
|
+ // 将 selectWrapper 加入到 userInputs 容器中
|
|
|
+ userInputs.appendChild(selectWrapper);
|
|
|
+
|
|
|
+ // 初始更新其他下拉框
|
|
|
+ updateSelectOptions();
|
|
|
+ });
|
|
|
+
|
|
|
+ // 点击下半部分的确认按钮
|
|
|
+ const userConfirmBtn = document.getElementById('distributeUser-confirm-btn');
|
|
|
+ userConfirmBtn.addEventListener('click', () => {
|
|
|
+ // 收集用户信息,收集每个下拉框选中的值及其额外数据
|
|
|
+ const selectedUsers = Array.from(document.querySelectorAll('.distributeUser-select'))
|
|
|
+ .map(select => {
|
|
|
+ if (select.value) {
|
|
|
+ const selectedUserInfo = {
|
|
|
+ uniqueID: select.value,
|
|
|
+ username: select.options[select.selectedIndex].getAttribute('data-username'),
|
|
|
+ account: select.options[select.selectedIndex].getAttribute('data-account')
|
|
|
+ };
|
|
|
+ return selectedUserInfo; // 返回用户的所有信息
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .filter(user => user !== undefined); // 过滤掉未选择的空值
|
|
|
+
|
|
|
+ // 分别提取 uniqueID, username 和 account 数组
|
|
|
+ const uniqueIDArray = selectedUsers.map(user => user.uniqueID);
|
|
|
+ const usernameArray = selectedUsers.map(user => user.username);
|
|
|
+ const accountArray = selectedUsers.map(user => user.account);
|
|
|
+
|
|
|
+ console.log("Unique IDs: ", uniqueIDArray);
|
|
|
+ console.log("Usernames: ", usernameArray);
|
|
|
+ console.log("Accounts: ", accountArray);
|
|
|
+
|
|
|
+ // 执行用户分发逻辑,传递用户详细信息到 distributeLicense
|
|
|
+ distributeUser(item, uniqueIDArray, usernameArray, accountArray);
|
|
|
+ closeDistributeModal(); // 关闭模态框
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// 关闭分发模态框的函数
|
|
|
+function closeDistributeModal() {
|
|
|
+ const modal = document.getElementById('DistributeModal');
|
|
|
+ modal.style.display = 'none'; // 隐藏模态框
|
|
|
+
|
|
|
+ // 清空邮箱和用户输入区域
|
|
|
+ const emailInputs = document.getElementById('DistributeModal-email-inputs');
|
|
|
+ emailInputs.innerHTML = ''; // 清空动态生成的邮箱输入框
|
|
|
+
|
|
|
+ const userInputs = document.getElementById('DistributeModal-user-inputs');
|
|
|
+ userInputs.innerHTML = ''; // 清空动态生成的用户选择框
|
|
|
+}
|
|
|
+
|
|
|
+function distributeEmails(item, emails, usersInfo) {
|
|
|
+ console.log('分发 distributeEmails', { item, emails, usersInfo });
|
|
|
+
|
|
|
+ // 将 emails 数组转为以逗号分隔的字符串
|
|
|
+ const emailsString = emails.join(',');
|
|
|
+
|
|
|
+ // 从 usersInfo 中获取 currentUserInfo
|
|
|
+ //const currentUserInfo = usersInfo.find(user => user.role === 'currentUser'); // 假设 currentUser 标识当前用户
|
|
|
+
|
|
|
+ if (!currentUserInfo) {
|
|
|
+ console.error('无法获取当前用户信息');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建要发送的数据
|
|
|
+ const postData = {
|
|
|
+ emails: emailsString,
|
|
|
+ LicenseUniqueID: item.UniqueID, // item 中的 UniqueID
|
|
|
+ Oa_request_id: item.oa_request_id, // item 中的 oa_request_id
|
|
|
+ OperatorUniqueID: currentUserInfo.UniqueID // currentUserInfo 中的 UniqueID
|
|
|
+ };
|
|
|
+
|
|
|
+ console.log('即将发送的数据:', postData);
|
|
|
+ showLoadingModal("正在分发邮箱中...");
|
|
|
+ // 发送 POST 请求到 DistributeLicenseToEmail 接口
|
|
|
+ fetch('http://127.0.0.1:8080/api/admin/DistributeLicenseToEmail', {
|
|
|
+ method: 'POST',
|
|
|
+ headers: {
|
|
|
+ 'Authorization': `Bearer ${authToken}`, // 使用 authToken
|
|
|
+ 'Content-Type': 'application/json'
|
|
|
+ },
|
|
|
+ body: JSON.stringify(postData) // 将数据转为 JSON 格式
|
|
|
+ })
|
|
|
+ .then(response => {
|
|
|
+ if (!response.ok) {
|
|
|
+ hideLoadingModal();
|
|
|
+ throw new Error('Network response was not ok');
|
|
|
+ }
|
|
|
+ return response.json();
|
|
|
+ })
|
|
|
+ .then(data => {
|
|
|
+ // 处理成功响应
|
|
|
+ hideLoadingModal();
|
|
|
+ console.log('邮件分发成功:', data);
|
|
|
+ alert('邮件分发成功');
|
|
|
+ })
|
|
|
+ .catch(error => {
|
|
|
+ // 处理错误
|
|
|
+ hideLoadingModal();
|
|
|
+ console.error('分发邮件失败:', error);
|
|
|
+ alert('分发邮件失败,请稍后重试');
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+// 执行分发的逻辑
|
|
|
+function distributeUser(item, uniqueIDArray, usernameArray, accountArray) {
|
|
|
+ // 构建要发送的数据对象
|
|
|
+ const postData = {
|
|
|
+ Oa_request_id: item.oa_request_id,
|
|
|
+ LicenseUniqueID: item.UniqueID,
|
|
|
+ OperatorUniqueID: currentUserInfo.UniqueID, // currentUserInfo 中的 UniqueID
|
|
|
+ UserUniqueIDs: uniqueIDArray,
|
|
|
+ UserNames: usernameArray,
|
|
|
+ UserAccounts: accountArray
|
|
|
+ };
|
|
|
+
|
|
|
+ console.log('Sending user data:', postData);
|
|
|
+ showLoadingModal("正在分发用户中...");
|
|
|
+ // 发送 POST 请求到 DistributeLicenseToUser 接口
|
|
|
+ fetch('http://127.0.0.1:8080/api/admin/DistributeLicenseToUser', {
|
|
|
+ method: 'POST',
|
|
|
+ headers: {
|
|
|
+ 'Content-Type': 'application/json',
|
|
|
+ 'Authorization': `Bearer ${authToken}` // 使用适当的 authToken
|
|
|
+ },
|
|
|
+ body: JSON.stringify(postData) // 将数据转换为 JSON 字符串发送
|
|
|
+ })
|
|
|
+ .then(response => {
|
|
|
+ hideLoadingModal(); // 隐藏 loading 模态框
|
|
|
+ if (!response.ok) {
|
|
|
+ // 返回错误信息作为 Promise,以便 catch 块捕获
|
|
|
+ return response.json().then(errorData => {
|
|
|
+ // 检查服务器是否返回了特定的错误信息并抛出错误
|
|
|
+ const errorMessage = errorData.error || '网络响应不正确';
|
|
|
+ throw new Error(errorMessage);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ return response.json();
|
|
|
+ })
|
|
|
+ .then(data => {
|
|
|
+ console.log('分发成功:', data);
|
|
|
+ alert('分发成功!');
|
|
|
+ })
|
|
|
+ .catch(error => {
|
|
|
+ hideLoadingModal(); // 在失败时隐藏 loading 模态框
|
|
|
+ console.error('分发失败:', error.message); // 打印具体的错误信息
|
|
|
+ alert('分发失败: ' + error.message); // 将错误信息展示给用户
|
|
|
+ });
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
//--------------分发历史模态框------------------------
|