From e0f16d6eb1192816a3928a054d67b747363980a5 Mon Sep 17 00:00:00 2001 From: spudding <1115683088@qq.com> Date: Fri, 18 Sep 2026 21:25:43 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E7=89=88=E6=9C=AC=E5=8D=87=E7=BA=A7?= =?UTF-8?q?=E8=87=B31.1.8=EF=BC=8C=E6=96=B0=E5=A2=9E=E9=92=89=E9=92=89?= =?UTF-8?q?=E8=B4=A6=E5=8F=B7=E9=80=9A=E7=9F=A5=E7=AD=89=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 版本号更新:shell.version与install.iss同步升级到1.1.8 2. 新增钉钉账号创建通知:新用户收账号密码,管理员收权限提醒 3. 优化钉钉扫码登录逻辑:自动补全账号信息并同步昵称 4. 修复首次改密校验逻辑,统一初始密码验证规则 5. 新增AGENTS文档写入操作沙箱规避规范 6. 优化雇主险调度脚本,新增四级部门自动填充逻辑 7. 重构前端更新弹窗,将下载进度从顶部条改为独立弹窗 8. 新增EHR考勤异常检查脚本,优化打卡判定逻辑 --- AGENTS.md | 1 + _tmp_verify_match.py | 25 + aa.json | 556 ++++++------------ desktop/controlplane/apis/admin.py | 3 + desktop/controlplane/apis/auth.py | 51 +- desktop/controlplane/apis/dingtalk.py | 3 + desktop/controlplane/core/config.py | 9 + desktop/controlplane/core/notify.py | 173 ++++++ desktop/webhost/frontend/src/App.vue | 64 +- desktop/webhost/tools/install.iss | 2 +- desktop/webhost/tools/shell.version | 2 +- .../employer_insurance_schedule.py | 45 +- .../attendance_abnormal_monthly.py | 336 +++++++++-- .../notice_mail_merge.py | 462 ++++++++++----- 14 files changed, 1134 insertions(+), 598 deletions(-) create mode 100644 _tmp_verify_match.py create mode 100644 desktop/controlplane/core/notify.py diff --git a/AGENTS.md b/AGENTS.md index 6c3f0af..b1a377a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -6,6 +6,7 @@ AI 智能体开发约束 & 项目执行规范 - 默认强制使用本地 .venv 虚拟环境,禁止使用任何沙箱环境、在线临时环境、容器模拟环境。 - 所有代码运行、依赖安装、调试、语法检查、模块测试,均基于项目根目录 .venv 环境执行。 - 如需安装依赖,仅操作本地虚拟环境,不污染全局 Python 环境。 +- 所有需要【写入文件内容】的操作(导出落盘、覆盖目标文件、复制/移动文件、下载保存等)一律不使用沙箱:执行时触发沙箱外运行(requires_approval 申请用户批准),避免被沙箱拦截(如 D:\资料 等受保护目录只读可读、写入被拒)。 2. 影刀开发规范 - 影刀项目默认采用单文件自包含架构。 - 输出影刀脚本、逻辑代码、流程封装时,优先单文件独立实现,功能自包含、可直接导入运行,不拆分冗余子文件。 diff --git a/_tmp_verify_match.py b/_tmp_verify_match.py new file mode 100644 index 0000000..40a232a --- /dev/null +++ b/_tmp_verify_match.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +"""临时验证脚本:用实际 Excel 数据验证新 _match_rule 筛选(用后即删)""" +import sys +sys.path.insert(0, r"d:\work\code\37duAutomation\workflows\XQ0015 参保成功及异常告知书制作与下发") +import py_compile +py_compile.compile(r"d:\work\code\37duAutomation\workflows\XQ0015 参保成功及异常告知书制作与下发\notice_mail_merge.py", doraise=True) +print("SYNTAX OK") + +import notice_mail_merge as nm + +excel = r"D:\资料\人资\告知书制作与下发\2026年9月社保变更及增减名单及人数统计.xlsx" +records = nm._read_insurance_records(excel) +print("参保名单记录数:", len(records)) +if records: + keys = [k for k in records[0].keys()] + print("首条字段数:", len(keys), "含参保告知:", "参保告知" in keys, "含费用:", "费用" in keys) + +for rn, rule in nm.TEMPLATES.items(): + if rn == "社保转移": + continue + matched = [r for r in records if nm._match_rule(r, rule["status"], rule["type"])] + print(f"[{rn}] status={rule['status']} type={rule['type']} -> 命中 {len(matched)} 人") + for r in matched[:10]: + print(" ", r.get("姓名"), "| 参保类型:", r.get("参保类型"), "| 办理状态:", r.get("办理状态"), + "| 参保告知:", repr(r.get("参保告知")), "| 费用:", r.get("费用"), "| 人员类型:", r.get("人员类型"), "| 二级部门:", r.get("二级部门")) diff --git a/aa.json b/aa.json index cbaa4c1..5b98825 100644 --- a/aa.json +++ b/aa.json @@ -1,383 +1,177 @@ { - "code": "10000", - "msg": null, - "subCode": "200", - "subMsg": "", - "data": { - "pageSize": 10, - "pageIndex": 0, - "total": 8, - "data": [ - { - "bws041": null, - "bws061": null, - "bws021": "2098294969579888640", - "bws211": null, - "bws102": "2098295046716928000", - "aab003": "91500107MA61B4BT7B", - "aac001": null, - "aac002": "511381199310155558", - "aac003": "席枭阳", - "aae200": null, - "aae201": null, - "aae202": null, - "aad030": null, - "aad013": null, - "abi030": null, - "abj030": null, - "abk030": null, - "abl030": null, - "abm030": null, - "abi031": null, - "abj040": null, - "aab001": "30009572", - "aab004": "重庆金色湖畔酒店管理有限公司", - "aaa121": "Z1001", - "aaa120": "企业职工人员增加", - "xh": null, - "aae014": "1858909245211283457", - "aae015": "2026-09-11 14:19:37.0", - "aae110s": "审核通过", - "aae110": "1", - "aae111": "若申报险种有医疗保险、生育保险,申报信息已同步推送医保部门,最终以医保部门审核结果为准。", - "aae100": "1", - "aae002": "202609", - "asl001": "10049406053875", - "aae030": null, - "aae031": null, - "aae099": null, - "aae140": null, - "aab084": null, - "aab085": null, - "aab086": null, - "itmes": null, - "baz001": null, - "aae005": "18323329271" - }, - { - "bws041": null, - "bws061": null, - "bws021": "2098294969579888641", - "bws211": null, - "bws102": "2098295046716928000", - "aab003": "91500107MA61B4BT7B", - "aac001": null, - "aac002": "511602198907066992", - "aac003": "肖飞", - "aae200": null, - "aae201": null, - "aae202": null, - "aad030": null, - "aad013": null, - "abi030": null, - "abj030": null, - "abk030": null, - "abl030": null, - "abm030": null, - "abi031": null, - "abj040": null, - "aab001": "30009572", - "aab004": "重庆金色湖畔酒店管理有限公司", - "aaa121": "Z1001", - "aaa120": "企业职工人员增加", - "xh": null, - "aae014": "1858909245211283457", - "aae015": "2026-09-11 14:19:37.0", - "aae110s": "审核通过", - "aae110": "1", - "aae111": "若申报险种有医疗保险、生育保险,申报信息已同步推送医保部门,最终以医保部门审核结果为准。", - "aae100": "1", - "aae002": "202609", - "asl001": "10049406053875", - "aae030": null, - "aae031": null, - "aae099": null, - "aae140": null, - "aab084": null, - "aab085": null, - "aab086": null, - "itmes": null, - "baz001": null, - "aae005": "17508262764" - }, - { - "bws041": null, - "bws061": null, - "bws021": "2098294969579888642", - "bws211": null, - "bws102": "2098295046716928000", - "aab003": "91500107MA61B4BT7B", - "aac001": null, - "aac002": "51290219790311446X", - "aac003": "吴长春", - "aae200": null, - "aae201": null, - "aae202": null, - "aad030": null, - "aad013": null, - "abi030": null, - "abj030": null, - "abk030": null, - "abl030": null, - "abm030": null, - "abi031": null, - "abj040": null, - "aab001": "30009572", - "aab004": "重庆金色湖畔酒店管理有限公司", - "aaa121": "Z1001", - "aaa120": "企业职工人员增加", - "xh": null, - "aae014": "1858909245211283457", - "aae015": "2026-09-11 14:19:37.0", - "aae110s": "不通过", - "aae110": "2", - "aae111": "status 400 reading PersonRegisterApiClient#executeRegisterPerson(PersonInfoDTO)", - "aae100": "1", - "aae002": "202609", - "asl001": "10049406053875", - "aae030": null, - "aae031": null, - "aae099": null, - "aae140": null, - "aab084": null, - "aab085": null, - "aab086": null, - "itmes": null, - "baz001": null, - "aae005": "18090507933" - }, - { - "bws041": null, - "bws061": null, - "bws021": "2098294969579888643", - "bws211": null, - "bws102": "2098295046716928000", - "aab003": "91500107MA61B4BT7B", - "aac001": null, - "aac002": "500113200506014322", - "aac003": "殷李圆", - "aae200": null, - "aae201": null, - "aae202": null, - "aad030": null, - "aad013": null, - "abi030": null, - "abj030": null, - "abk030": null, - "abl030": null, - "abm030": null, - "abi031": null, - "abj040": null, - "aab001": "30009572", - "aab004": "重庆金色湖畔酒店管理有限公司", - "aaa121": "Z1001", - "aaa120": "企业职工人员增加", - "xh": null, - "aae014": "1858909245211283457", - "aae015": "2026-09-11 14:19:37.0", - "aae110s": "不通过", - "aae110": "2", - "aae111": "status 400 reading PersonRegisterApiClient#executeRegisterPerson(PersonInfoDTO)", - "aae100": "1", - "aae002": "202609", - "asl001": "10049406053875", - "aae030": null, - "aae031": null, - "aae099": null, - "aae140": null, - "aab084": null, - "aab085": null, - "aab086": null, - "itmes": null, - "baz001": null, - "aae005": "15696188987" - }, - { - "bws041": null, - "bws061": null, - "bws021": "2098294969579888644", - "bws211": null, - "bws102": "2098295046716928000", - "aab003": "91500107MA61B4BT7B", - "aac001": null, - "aac002": "511304200206221327", - "aac003": "付冬秀", - "aae200": null, - "aae201": null, - "aae202": null, - "aad030": null, - "aad013": null, - "abi030": null, - "abj030": null, - "abk030": null, - "abl030": null, - "abm030": null, - "abi031": null, - "abj040": null, - "aab001": "30009572", - "aab004": "重庆金色湖畔酒店管理有限公司", - "aaa121": "Z1001", - "aaa120": "企业职工人员增加", - "xh": null, - "aae014": "1858909245211283457", - "aae015": "2026-09-11 14:19:37.0", - "aae110s": "审核通过", - "aae110": "1", - "aae111": "若申报险种有医疗保险、生育保险,申报信息已同步推送医保部门,最终以医保部门审核结果为准。", - "aae100": "1", - "aae002": "202609", - "asl001": "10049406053875", - "aae030": null, - "aae031": null, - "aae099": null, - "aae140": null, - "aab084": null, - "aab085": null, - "aab086": null, - "itmes": null, - "baz001": null, - "aae005": "19122971035" - }, - { - "bws041": null, - "bws061": null, - "bws021": "2098294969579888645", - "bws211": null, - "bws102": "2098295046716928000", - "aab003": "91500107MA61B4BT7B", - "aac001": null, - "aac002": "511622199812259425", - "aac003": "易伟列", - "aae200": null, - "aae201": null, - "aae202": null, - "aad030": null, - "aad013": null, - "abi030": null, - "abj030": null, - "abk030": null, - "abl030": null, - "abm030": null, - "abi031": null, - "abj040": null, - "aab001": "30009572", - "aab004": "重庆金色湖畔酒店管理有限公司", - "aaa121": "Z1001", - "aaa120": "企业职工人员增加", - "xh": null, - "aae014": "1858909245211283457", - "aae015": "2026-09-11 14:19:37.0", - "aae110s": "不通过", - "aae110": "2", - "aae111": "status 400 reading PersonRegisterApiClient#executeRegisterPerson(PersonInfoDTO)", - "aae100": "1", - "aae002": "202609", - "asl001": "10049406053875", - "aae030": null, - "aae031": null, - "aae099": null, - "aae140": null, - "aab084": null, - "aab085": null, - "aab086": null, - "itmes": null, - "baz001": null, - "aae005": "15608313153" - }, - { - "bws041": null, - "bws061": null, - "bws021": "2098294969579888646", - "bws211": null, - "bws102": "2098295046716928000", - "aab003": "91500107MA61B4BT7B", - "aac001": null, - "aac002": "500221198608205133", - "aac003": "蒋洁", - "aae200": null, - "aae201": null, - "aae202": null, - "aad030": null, - "aad013": null, - "abi030": null, - "abj030": null, - "abk030": null, - "abl030": null, - "abm030": null, - "abi031": null, - "abj040": null, - "aab001": "30009572", - "aab004": "重庆金色湖畔酒店管理有限公司", - "aaa121": "Z1001", - "aaa120": "企业职工人员增加", - "xh": null, - "aae014": "1858909245211283457", - "aae015": "2026-09-11 14:19:37.0", - "aae110s": "不通过", - "aae110": "2", - "aae111": "status 400 reading PersonRegisterApiClient#executeRegisterPerson(PersonInfoDTO)", - "aae100": "1", - "aae002": "202609", - "asl001": "10049406053875", - "aae030": null, - "aae031": null, - "aae099": null, - "aae140": null, - "aab084": null, - "aab085": null, - "aab086": null, - "itmes": null, - "baz001": null, - "aae005": "18908365870" - }, - { - "bws041": null, - "bws061": null, - "bws021": "2098294969579888647", - "bws211": null, - "bws102": "2098295046716928000", - "aab003": "91500107MA61B4BT7B", - "aac001": null, - "aac002": "50010519990524033X", - "aac003": "李国鑫", - "aae200": null, - "aae201": null, - "aae202": null, - "aad030": null, - "aad013": null, - "abi030": null, - "abj030": null, - "abk030": null, - "abl030": null, - "abm030": null, - "abi031": null, - "abj040": null, - "aab001": "30009572", - "aab004": "重庆金色湖畔酒店管理有限公司", - "aaa121": "Z1001", - "aaa120": "企业职工人员增加", - "xh": null, - "aae014": "1858909245211283457", - "aae015": "2026-09-11 14:19:37.0", - "aae110s": "审核通过", - "aae110": "1", - "aae111": "若申报险种有医疗保险、生育保险,申报信息已同步推送医保部门,最终以医保部门审核结果为准。", - "aae100": "1", - "aae002": "202609", - "asl001": "10049406053875", - "aae030": null, - "aae031": null, - "aae099": null, - "aae140": null, - "aab084": null, - "aab085": null, - "aab086": null, - "itmes": null, - "baz001": null, - "aae005": "17633503787" - } - ], - "lastPageIndex": 0, - "pageCount": 1 - } + "attributeMap" : null, + "count" : 1, + "datas" : [ { + "ORG_FULL_PATH" : "集团-金色印象事业部-第二大区-金色印象长沙经营中心-第三片区-八一路行政会馆-服务组", + "person_status" : "2", + "IDCARD" : "511823200909152619", + "base_code" : "37036650", + "POST_NO" : "287", + "BELONG_UNIT_STRU_ID" : "0bcdedeb-e592-11ee-b449-000c296f9469", + "BELONG_UNIT_ORG_NAME" : "金色印象事业部", + "POST_ALIAS" : "服务员", + "POST_ORG_NAME" : "服务员", + "staff_id" : "9bbaec51-f4aa-4271-bb75-44583d510331", + "staff_name" : "李俊熙", + "DEPT_STRU_ID" : "57fab6e7-c7fd-4ffa-ac1a-0a085a05f7de", + "DEPT_ORG_NAME" : "服务组", + "POST_ALIAS_NAME" : "服务员", + "leave_type" : "17", + "DEPT_ORG_ONE" : "第二大区", + "DEPT_ORG_TWO" : "金色印象长沙经营中心", + "DEPT_ORG_THREE" : "第三片区", + "DEPT_ORG_FOUR" : "八一路行政会馆", + "DEPT_ORG_FIVE" : "服务组", + "dept_one" : null, + "dept_two" : null, + "vacation_type_name" : "特殊事假", + "time_unit" : "天", + "start_time" : "2026-08-30 10:00:00", + "end_time" : "2026-08-30 19:00:00", + "leave_num" : "1", + "leave_reason" : "处理个人事务", + "status" : "005", + "leave_hours" : "", + "leave_days" : "1.00", + "businessKey" : "bpmc6afb65ed9a14078a467c0045994ad3e", + "process_id" : "dc63c919-04db-4fa1-9f6d-0eaa76037f90", + "form_id" : "399b1612-d410-4a59-86cb-b5260b933232", + "source" : "1", + "leave_id" : "bpmc6afb65ed9a14078a467c0045994ad3e", + "operat_time" : "2026-09-01 21:11:56", + "apply_time" : "2026-09-01 16:24:55", + "leave_hours_ori" : "0", + "leave_days_ori" : "1", + "begin_date" : "2026-08-30", + "begin_other_name" : "1", + "end_date" : "2026-08-30", + "end_other_name" : "2", + "if_other_name_open" : "1", + "begin_date_other_name" : "2026-08-30 上半段", + "end_date_other_name" : "2026-08-30 下半段", + "operat_user_id" : "fdb2e691-1f5c-46f1-b2b4-d69987a4ac96", + "operat_user_name" : "谌超", + "processing_reason" : null, + "remark" : null, + "kqext_create_user_id" : null, + "kqext_create_time" : null, + "kqext_operator_id" : null, + "kqext_operate_time" : null, + "hrext_hsb_ext_speciality" : null, + "hrext_ext_technician_code" : "", + "hrext_ext_cal_person2" : "", + "hrext_ext_call_phone2" : "", + "hrext_ext_star_level" : "正式", + "hrext_ext_rehiring_reason_type" : "", + "hrext_ext_staff_type" : "门店服务岗", + "hrext_ext_insurance_type" : "雇主险", + "hrext_ext_recruit_way" : "其它", + "hrext_ext_call_relationship2" : "", + "hrext_ext_is_CRBT_open" : "", + "hrext_ext_dept_principal" : "", + "hrext_ext_yb_pay_year" : "", + "hrext_ext_Have_worked" : "", + "hrext_ext_family_join" : "否", + "hrext_ext_criminal_record" : "否", + "hrext_ext_infectious_disease" : "否", + "hrext_ext_References" : "", + "hrext_ext_References_phone" : "", + "hrext_ext_References_outside" : "", + "hrext_ext_outside_References_phone" : "", + "hrext_ext_is_dormitory" : "是", + "hrext_ext_process_approval_status" : "已审批", + "hrext_ext_trans_type" : "其他" + } ], + "pageSize" : 15, + "start" : 0, + "pageNo" : 1, + "pageCount" : 1, + "allDatas" : { + "#result-set-1" : [ { + "ORG_FULL_PATH" : "集团-金色印象事业部-第二大区-金色印象长沙经营中心-第三片区-八一路行政会馆-服务组", + "person_status" : "2", + "IDCARD" : "511823200909152619", + "base_code" : "37036650", + "POST_NO" : "287", + "BELONG_UNIT_STRU_ID" : "0bcdedeb-e592-11ee-b449-000c296f9469", + "BELONG_UNIT_ORG_NAME" : "金色印象事业部", + "POST_ALIAS" : "服务员", + "POST_ORG_NAME" : "服务员", + "staff_id" : "9bbaec51-f4aa-4271-bb75-44583d510331", + "staff_name" : "李俊熙", + "DEPT_STRU_ID" : "57fab6e7-c7fd-4ffa-ac1a-0a085a05f7de", + "DEPT_ORG_NAME" : "服务组", + "POST_ALIAS_NAME" : "服务员", + "leave_type" : "17", + "DEPT_ORG_ONE" : "第二大区", + "DEPT_ORG_TWO" : "金色印象长沙经营中心", + "DEPT_ORG_THREE" : "第三片区", + "DEPT_ORG_FOUR" : "八一路行政会馆", + "DEPT_ORG_FIVE" : "服务组", + "dept_one" : null, + "dept_two" : null, + "vacation_type_name" : "特殊事假", + "time_unit" : "天", + "start_time" : "2026-08-30 10:00:00", + "end_time" : "2026-08-30 19:00:00", + "leave_num" : "1", + "leave_reason" : "处理个人事务", + "status" : "005", + "leave_hours" : "", + "leave_days" : "1.00", + "businessKey" : "bpmc6afb65ed9a14078a467c0045994ad3e", + "process_id" : "dc63c919-04db-4fa1-9f6d-0eaa76037f90", + "form_id" : "399b1612-d410-4a59-86cb-b5260b933232", + "source" : "1", + "leave_id" : "bpmc6afb65ed9a14078a467c0045994ad3e", + "operat_time" : "2026-09-01 21:11:56", + "apply_time" : "2026-09-01 16:24:55", + "leave_hours_ori" : "0", + "leave_days_ori" : "1", + "begin_date" : "2026-08-30", + "begin_other_name" : "1", + "end_date" : "2026-08-30", + "end_other_name" : "2", + "if_other_name_open" : "1", + "begin_date_other_name" : "2026-08-30 上半段", + "end_date_other_name" : "2026-08-30 下半段", + "operat_user_id" : "fdb2e691-1f5c-46f1-b2b4-d69987a4ac96", + "operat_user_name" : "谌超", + "processing_reason" : null, + "remark" : null, + "kqext_create_user_id" : null, + "kqext_create_time" : null, + "kqext_operator_id" : null, + "kqext_operate_time" : null, + "hrext_hsb_ext_speciality" : null, + "hrext_ext_technician_code" : "", + "hrext_ext_cal_person2" : "", + "hrext_ext_call_phone2" : "", + "hrext_ext_star_level" : "正式", + "hrext_ext_rehiring_reason_type" : "", + "hrext_ext_staff_type" : "门店服务岗", + "hrext_ext_insurance_type" : "雇主险", + "hrext_ext_recruit_way" : "其它", + "hrext_ext_call_relationship2" : "", + "hrext_ext_is_CRBT_open" : "", + "hrext_ext_dept_principal" : "", + "hrext_ext_yb_pay_year" : "", + "hrext_ext_Have_worked" : "", + "hrext_ext_family_join" : "否", + "hrext_ext_criminal_record" : "否", + "hrext_ext_infectious_disease" : "否", + "hrext_ext_References" : "", + "hrext_ext_References_phone" : "", + "hrext_ext_References_outside" : "", + "hrext_ext_outside_References_phone" : "", + "hrext_ext_is_dormitory" : "是", + "hrext_ext_process_approval_status" : "已审批", + "hrext_ext_trans_type" : "其他" + } ] + }, + "url" : null, + "state" : "1", + "meg" : "", + "des" : "", + "viewMapper" : null } \ No newline at end of file diff --git a/desktop/controlplane/apis/admin.py b/desktop/controlplane/apis/admin.py index d1bb147..3120db0 100644 --- a/desktop/controlplane/apis/admin.py +++ b/desktop/controlplane/apis/admin.py @@ -17,6 +17,7 @@ from controlplane import config, schemas from controlplane.core.auth import _require_admin, _resolve_login from controlplane.core.crypto import _hash_password from controlplane.core.db import _audit, _db, _now +from controlplane.core.notify import notify_account_created from controlplane.core.pkgutil import ( _SAFE_NAME_RE, _extract_manifest, _load_manifest, _prune_versions, _validate_pkg_zip, _version_key, @@ -67,6 +68,8 @@ def admin_account_create(a: schemas.AccountCreateIn, (account, _hex, _salt, int(a.is_admin), (a.dept or "").strip(), (a.nickname or "").strip(), _now())) conn.commit() + # 钉钉工作通知(异步,失败不阻断建号):新用户收账号密码(按昵称解析),管理员收权限开通提醒 + notify_account_created(account, (a.nickname or "").strip(), a.password, source="管理台") return {"ok": True, "account": account, "dept": (a.dept or "").strip(), "nickname": (a.nickname or "").strip(), "is_admin": a.is_admin} diff --git a/desktop/controlplane/apis/auth.py b/desktop/controlplane/apis/auth.py index 88d4c90..6bfd1f3 100644 --- a/desktop/controlplane/apis/auth.py +++ b/desktop/controlplane/apis/auth.py @@ -10,12 +10,13 @@ from fastapi import APIRouter, Header, HTTPException from controlplane import config, schemas from controlplane.core.auth import ( - _AUTH_FAILS, _auth_account_lookup, _auth_fail, _auth_gate, _check_token, + _AUTH_FAILS, _auth_fail, _auth_gate, _check_token, _cloud_account, _issue_cloud_token, _resolve_login, _require_admin, _sign_admin_token, ) from controlplane.core.crypto import _hash_password, _verify_password from controlplane.core.db import _db from controlplane.core.dingtalk import _dingtalk_userinfo +from controlplane.core.notify import notify_account_created router = APIRouter() @@ -117,9 +118,8 @@ def admin_password(p: schemas.PwdIn, (login,)).fetchone() if not row: raise HTTPException(401, "账号不存在") - # 首次登录(must_change_pwd=1,钉钉自动建号的默认密码)允许免旧密码直接改密 - if not row["must_change_pwd"] and ( - not row["salt"] or not _verify_password(p.old_password, row["salt"], row["password_hash"])): + # 旧密码校验(所有账号均带盐;首次改密的旧密码 = 钉钉通知下发的初始密码,同样必须输入) + if not row["salt"] or not _verify_password(p.old_password, row["salt"], row["password_hash"]): raise HTTPException(401, "原密码错误") _hex, _salt = _hash_password(p.new_password) with _db() as conn: @@ -130,14 +130,15 @@ def admin_password(p: schemas.PwdIn, # ==================== 员工端登录 /auth/exchange(v3 最简方案,壳本机直连接 code) ==================== -# 壳 login 云端的唯一接口:壳把浏览器带回的钉钉 code POST 到此,用 AppSecret 换身份 → 只验证不建号 -# → 签发云端会话 token(供壳调 /api/plugins 鉴权,见 apis/plugins §2.2)。token 存储统一在 -# core.auth(_issue_cloud_token 签发落库 cloud_tokens 表、_cloud_account 解析),此处不再重复声明。 +# 壳 login 云端的唯一接口:壳把浏览器带回的钉钉 code POST 到此,用 AppSecret 换身份 → 找建账号 +# (不存在则自动建号:空壳+默认密码+强制改密,并推送钉钉通知)→ 签发云端会话 token(供壳调 +# /api/plugins 鉴权,见 apis/plugins §2.2)。token 存储统一在 core.auth(_issue_cloud_token 签发 +# 落库 cloud_tokens 表、_cloud_account 解析),此处不再重复声明。 @router.post("/auth/exchange") def auth_exchange(d: schemas.AuthExchangeIn) -> dict: - """壳 → 云端:壳 POST 钉钉授权码 code,换取员工工号 + 云端会话 token(只验证不建号)。""" + """壳 → 云端:壳 POST 钉钉授权码 code,换取员工工号 + 云端会话 token(找建账号)。""" code = (d.code or "").strip() if not code: raise HTTPException(400, "缺少 code") @@ -147,9 +148,33 @@ def auth_exchange(d: schemas.AuthExchangeIn) -> dict: account = (info.get("account") or "").strip() if not account: raise HTTPException(401, "扫码未解析到有效账号,请重新扫码") - acct = _auth_account_lookup(account) - nickname = acct["nickname"] or (info.get("nickname") or "") + nickname = (info.get("nickname") or "").strip() + unionid = (info.get("unionid") or "").strip() + # 找建账号(与 /api/admin/dingtalk/auth 同语义):已存在则同步昵称;不存在则自动建号并通知 + with _db() as conn: + row = conn.execute( + "SELECT nickname, status, must_change_pwd FROM accounts WHERE account = %s", + (account,)).fetchone() + if row: + if not row["status"]: + raise HTTPException(403, "账号已停用,请联系管理员") + if nickname and nickname != (row["nickname"] or ""): + conn.execute("UPDATE accounts SET nickname = %s WHERE account = %s", + (nickname, account)) + conn.commit() + created, must_change = False, bool(row["must_change_pwd"]) + else: + # 自动建号(空壳):初始密码取默认值,强制首次改密 + pwd = config.DINGTALK_DEFAULT_PASSWORD + _hex, _salt = _hash_password(pwd) + conn.execute( + "INSERT INTO accounts(account, password_hash, salt, is_admin, dept, nickname, " + "dingtalk_uid, must_change_pwd) VALUES (%s, %s, %s, 0, '', %s, %s, 1)", + (account, _hex, _salt, nickname, unionid)) + conn.commit() + created, must_change = True, True + notify_account_created(account, nickname, pwd, unionid, source="钉钉扫码") token = _issue_cloud_token(account, nickname) - return {"token": token, - "account": account, "nickname": nickname, - "exists": bool(acct["exists"]), "is_admin": False} \ No newline at end of file + return {"token": token, "account": account, "nickname": nickname, + "exists": True, "created": created, "must_change_pwd": must_change, + "is_admin": False} \ No newline at end of file diff --git a/desktop/controlplane/apis/dingtalk.py b/desktop/controlplane/apis/dingtalk.py index c1ce562..fc4a569 100644 --- a/desktop/controlplane/apis/dingtalk.py +++ b/desktop/controlplane/apis/dingtalk.py @@ -18,6 +18,7 @@ from controlplane.core.auth import _check_token, _sign_admin_token from controlplane.core.crypto import _hash_password from controlplane.core.db import _db from controlplane.core.dingtalk import _dingtalk_sns_userinfo, _dingtalk_userinfo +from controlplane.core.notify import notify_account_created router = APIRouter() @@ -63,6 +64,8 @@ def admin_dingtalk_auth(d: schemas.DingTalkIn, "dingtalk_uid, must_change_pwd) VALUES (%s, %s, %s, 0, '', %s, %s, 1)", (account, _hex, _salt, nickname, ding_uid)) conn.commit() + # 钉钉工作通知(异步,失败不阻断建号):新用户收默认账号密码,管理员收权限开通提醒 + notify_account_created(account, nickname, pwd, ding_uid, source="钉钉扫码") return {"ok": True, "account": account, "nickname": nickname, "is_admin": False, "created": True, "must_change_pwd": True, "token": _sign_admin_token(account)} diff --git a/desktop/controlplane/core/config.py b/desktop/controlplane/core/config.py index 0427e3f..3277cd8 100644 --- a/desktop/controlplane/core/config.py +++ b/desktop/controlplane/core/config.py @@ -71,6 +71,15 @@ DINGTALK_APP_SECRET = os.getenv("DINGTALK_APP_SECRET", "") # 钉钉扫码回调地址。网页扫码是"浏览器重定向"(非钉钉服务器回访),故可直接用内网 IP/域名, # 只要与用户实际访问地址、钉钉后台登记的回调域名一致即可。留空禁用扫码。 DINGTALK_CALLBACK = os.getenv("DINGTALK_CALLBACK", "").rstrip("/") +# 钉钉群机器人通知(新账号创建等事件推送)。webhook 留空则静默跳过;加签密钥按机器人安全设置可选。 +DINGTALK_WEBHOOK_URL = os.getenv("DINGTALK_WEBHOOK_URL", "") +DINGTALK_WEBHOOK_SECRET = os.getenv("DINGTALK_WEBHOOK_SECRET", "") +# 钉钉自建应用工作通知(corpconversation 异步推送)。AgentId 优先 DINGTALK_AGENT_ID, +# 兜底复用 CREDS_DINGTALK_AGENT_ID(同一套钉钉应用凭证);access_token 复用 DINGTALK_APP_KEY/SECRET。 +DINGTALK_AGENT_ID = (os.getenv("DINGTALK_AGENT_ID", "") + or os.getenv("CREDS_DINGTALK_AGENT_ID", "")).strip() +# 权限开通提醒的管理员姓名(钉钉扫码自动建号时通知其开通权限),经「搜索用户」接口直查 userid。 +DINGTALK_ADMIN_NAME = os.getenv("DINGTALK_ADMIN_NAME", "夏一平") # 客户端敏感凭证默认值参考(webhost 不再明文落盘,中心机 .env 为唯一可信明文源)。 # 明文源只由管理员经 /api/admin/creds/seed 录入;客户端 register 不再自动填充本默认值 diff --git a/desktop/controlplane/core/notify.py b/desktop/controlplane/core/notify.py new file mode 100644 index 0000000..005a0c1 --- /dev/null +++ b/desktop/controlplane/core/notify.py @@ -0,0 +1,173 @@ +# -*- coding: utf-8 -*- +"""钉钉通知:自建应用工作通知(markdown 美化样式,个人推送)+ 群机器人 webhook(渠道保留,备用)。 + +后台线程发送,失败仅记日志不阻断业务。 + +配置项(controlplane/.env): + DINGTALK_AGENT_ID 自建应用的 AgentId;留空则不发工作通知(APP_KEY/SECRET 复用扫码登录那套) + DINGTALK_ADMIN_NAME 权限开通提醒的管理员姓名(默认 夏一平),经「搜索用户」接口直查 userid + DINGTALK_WEBHOOK_URL 群机器人 webhook 地址(备用渠道,当前业务不自动发送) + DINGTALK_WEBHOOK_SECRET 群机器人加签密钥(安全设置选"加签"时必填;留空则不加签) +""" +from __future__ import annotations + +import base64 +import hashlib +import hmac +import json +import logging +import threading +import time +from urllib.parse import quote_plus +from urllib.request import Request, urlopen + +from controlplane.core import config +from controlplane.core.dingtalk import _dingtalk_topapi_token + +log = logging.getLogger(__name__) + +# 消息署名与标题着色(样式对齐 workflows/share/dingtalk_robot.py 的 send) +_SIGN_NAME = "37du 自动化平台" +_STATUS_COLORS = { + "info": "#0077ff", # 蓝色 + "success": "#00b300", # 绿色 + "warning": "#ff9900", # 橙色 + "error": "#ff0000", # 红色 +} + + +def _footer() -> str: + return f"###### {_SIGN_NAME} | {time.strftime('%Y-%m-%d %H:%M:%S')}" + + +def _post_json(url: str, payload: dict, headers: dict | None = None) -> dict: + req = Request(url, data=json.dumps(payload).encode("utf-8"), method="POST") + req.add_header("Content-Type", "application/json") + for k, v in (headers or {}).items(): + req.add_header(k, v) + with urlopen(req, timeout=10) as resp: + return json.loads(resp.read().decode("utf-8")) + + +# ==================== userid 解析(单次直查,不做通讯录遍历/缓存) ==================== +def _userid_by_unionid(at: str, unionid: str) -> str: + """unionid → userid(精确,无重名歧义)。""" + try: + body = _post_json( + "https://oapi.dingtalk.com/topapi/user/getbyunionid?access_token=%s" % at, + {"unionid": unionid}) + return ((body.get("result") or {}).get("userid")) or "" + except Exception: # noqa: BLE001 + return "" + + +def _userid_by_name(at: str, name: str) -> str: + """姓名 → userid:调「搜索用户」接口单次直查,命中多人取第一个。""" + try: + body = _post_json( + "https://api.dingtalk.com/v1.0/contact/users/search", + {"queryWord": name.strip(), "offset": 0, "size": 5, "maxResults": 5}, + headers={"x-acs-dingtalk-access-token": at}) + lst = body.get("list") or [] + return str(lst[0]) if lst else "" + except Exception as e: # noqa: BLE001 查询失败不阻断业务 + log.warning("钉钉搜索用户失败(%s):%s", name, e) + return "" + + +# ==================== 渠道一:自建应用工作通知(markdown 美化样式) ==================== +def _send_work_to(at: str, agent_id: str, userid: str, title: str, content: str, + status: str = "info") -> bool: + """向单个 userid 发工作通知,样式对齐 workflows/share/dingtalk_robot.py 的 send: + 彩色加粗标题 + 正文 Markdown 直接渲染(引用块字段)+「###### 署名 | 时间」页脚。""" + color = _STATUS_COLORS.get(status, _STATUS_COLORS["info"]) + text = f"**{title}**\n\n" + text += content.strip() + "\n" + text += f"\n\n---\n\n{_footer()}" + try: + body = _post_json( + "https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token=%s" % at, + {"agent_id": agent_id, "userid_list": userid, + "msg": {"msgtype": "markdown", "markdown": {"title": title, "text": text}}}) + if body.get("errcode") != 0: + log.warning("钉钉工作通知被拒: %s", body) + return False + return True + except Exception as e: # noqa: BLE001 通知失败不阻断业务 + log.warning("钉钉工作通知异常: %s", e) + return False + + +# ==================== 统一入口:新账号创建通知(管理台/钉钉扫码共用) ==================== +def _work_created(account: str, nickname: str, password: str, unionid: str, source: str) -> None: + agent_id = (config.DINGTALK_AGENT_ID or "").strip() + if not agent_id: + return + try: + at = _dingtalk_topapi_token() + if not at: + log.warning("钉钉建号通知失败:未配置 DINGTALK_APP_KEY/SECRET 或取 token 失败") + return + # 1) 新用户本人:unionid 精确解析 userid,兜底按昵称搜索直查 + uid = _userid_by_unionid(at, unionid) if unionid else "" + if not uid and nickname: + uid = _userid_by_name(at, nickname) + if uid: + _send_work_to( + at, agent_id, uid, "账号开通通知", + f"**🔹 {nickname or account}‑账号开通**\n\n" + f"> 账号:{account}\n\n" + f"> 初始密码:{password}\n\n" + "> 备注:首次登录须修改密码,请及时登录并妥善保管", + status="success") + else: + log.warning("钉钉建号通知:无法解析新用户 userid(account=%s)", account) + # 2) 管理员:权限开通提醒(按姓名搜索直查,默认 夏一平) + admin_name = (config.DINGTALK_ADMIN_NAME or "夏一平").strip() + admin_uid = _userid_by_name(at, admin_name) + if admin_uid: + _send_work_to( + at, agent_id, admin_uid, "新账号待开通权限", + f"> 账号:{account}\n\n" + f"> 姓名:{nickname or '—'}\n\n" + f"> 来源:{source}\n\n" + "> 备注:请到管理台为该账号开通插件权限", + status="warning") + else: + log.warning("钉钉建号通知:管理员 %s 未搜索到 userid", admin_name) + except Exception as e: # noqa: BLE001 通知失败不阻断业务 + log.warning("钉钉建号通知异常: %s", e) + + +def notify_account_created(account: str, nickname: str = "", password: str = "", + unionid: str = "", source: str = "管理台") -> None: + """新账号创建通知(新用户收账号+初始密码,管理员收权限开通提醒),后台线程发送。""" + threading.Thread(target=_work_created, args=(account, nickname, password, unionid, source), + daemon=True).start() + + +# ==================== 渠道二:群机器人 webhook(+ 加签) ==================== +def _signed_webhook_url() -> str: + url = (config.DINGTALK_WEBHOOK_URL or "").strip() + if not url: + return "" + secret = (config.DINGTALK_WEBHOOK_SECRET or "").strip() + if secret: + ts = str(int(time.time() * 1000)) + sign = base64.b64encode(hmac.new( + secret.encode("utf-8"), f"{ts}\n{secret}".encode("utf-8"), + hashlib.sha256).digest()).decode("utf-8") + url += ("&" if "?" in url else "?") + f"timestamp={ts}&sign={quote_plus(sign)}" + return url + + +def _send_robot(title: str, text: str) -> None: + url = _signed_webhook_url() + if not url: + return + try: + body = _post_json(url, {"msgtype": "markdown", "markdown": {"title": title, "text": text}}) + if body.get("errcode") != 0: + log.warning("钉钉群通知被拒: %s", body) + except Exception as e: # noqa: BLE001 通知失败不阻断业务 + log.warning("钉钉群通知异常: %s", e) diff --git a/desktop/webhost/frontend/src/App.vue b/desktop/webhost/frontend/src/App.vue index 4b090e3..c7d7726 100644 --- a/desktop/webhost/frontend/src/App.vue +++ b/desktop/webhost/frontend/src/App.vue @@ -6,16 +6,9 @@
- +
- -