async def applyCv(cvinfo: CvInfoRequest, ) -> Dict: cv = await crud_cv.select_one_by( and_(CvInfo.c.sno == cvinfo.sno, CvInfo.c.department == cvinfo.department)) # 存在则更新简历 if cv: info_dict = cvinfo.dict() # 除去联合主键字段,避免更新报错 info_dict.pop("sno") info_dict.pop("department") try: await crud_cv.update(condition=and_( CvInfo.c.sno == cvinfo.sno, CvInfo.c.department == cvinfo.department), **info_dict) except Exception as e: print(e) raise settings.CUSTOM_EXCEPTION(503, "简历更新失败") else: # 否则创建新简历 try: await crud_cv.create([cvinfo.dict()]) except Exception as e: print(e) raise settings.CUSTOM_EXCEPTION(503, "简历提交失败") return {"code": 0}
async def handleComment( commentData: CvComment, current_user: Dict = Security( get_current_user, scopes=["user", "administrator", "supervisor"]) ) -> Dict: cv = await crud_cv.select_one_by(CvInfo.c.sno == commentData.sno) if not cv: raise settings.CUSTOM_EXCEPTION(503, "简历信息不存在") commentList = eval(cv["comment"]) # 更新评论 for each in commentList: if each["nick"] == current_user["nick"]: each["content"] = commentData.content each["score"] = commentData.score try: await crud_cv.update(condition=CvInfo.c.sno == commentData.sno, comment=str(commentList)) except: raise settings.CUSTOM_EXCEPTION(503, "评价提交失败") return {"code": 0} # 新增评论 newComment = {"nick": current_user["nick"], "content": "", "score": ""} newComment["content"] = commentData.content newComment["score"] = commentData.score commentList.append(newComment) try: await crud_cv.update(condition=CvInfo.c.sno == commentData.sno, comment=str(commentList)) except: raise settings.CUSTOM_EXCEPTION(503, "评价提交失败") return {"code": 0}
async def updateAuthority( task: BackgroundTasks, openid: str = Body(..., embed = True), current_user: Dict = Security(get_current_user, scopes = ["administrator", "supervisor"]) ) -> Dict: user = await crud_user.select_one_by(User.c.openid == openid) if not user: raise settings.CUSTOM_EXCEPTION(503, "目标用户不存在") if "user" in user["role"]: targetRole = "['administrator']" operation = f"提高了{user['nick']}的权限" else: targetRole = "['user']" operation = f"移除了{user['nick']}的权限" await crud_user.update( condition = User.c.openid == openid, role = targetRole ) # 添加后台任务 task.add_task(addRecordTask, current_user["nick"], operation, current_user["department"]) return { "code": 0 }
async def __call__( self, security_scopes: SecurityScopes, token: str = Depends(oauth2_scheme) ) -> Dict: try: s = Serializer(secret_key=settings.SECRET_KEY, expires_in=settings.TOKEN_EXPIRE_Time) token_decode = s.loads(token) openid = token_decode["openid"] except Exception as e: # 出现异常则为token过期 print(e) raise settings.UNAUTHORIZED_EXCEPTION current_user = await crud_user.select_one_by(User.c.openid == openid) if not current_user: raise settings.CUSTOM_EXCEPTION(401, "用户不存在 请尝试重新扫码登录") scopes = eval(current_user["role"]) for scope in scopes: if scope in security_scopes.scopes: return current_user raise settings.UNAUTHORIZED_EXCEPTION
async def removeTemplate( task: BackgroundTasks, data: RemoveTemplate, current_user: Dict = Security( get_current_user, scopes=["user", "administrator", "supervisor"]) ) -> Dict: target = data.target dept = await crud_dept.select_one_by( Department.c.name == current_user["department"]) template = eval(dept["mail_template"]) removedList = [] for title in target: for each in template: if (each["title"] == title): template.remove(each) removedList.append(title) if not removedList: raise settings.CUSTOM_EXCEPTION(503, "模板不存在") await crud_dept.update( condition=Department.c.name == current_user["department"], mail_template=str(template)) # 添加后台任务 operation = f"删除了信息模板【{'|'.join(removedList)}】" task.add_task(addRecordTask, current_user["nick"], operation, current_user["department"]) return {"code": 0, "mail_template": template}
async def editTemplate( task: BackgroundTasks, editInfo: EditTemplate, current_user: Dict = Security( get_current_user, scopes=["user", "administrator", "supervisor"]) ) -> Dict: dept = await crud_dept.select_one_by( Department.c.name == current_user["department"]) template = eval(dept["mail_template"]) for each in template: if each["title"] == editInfo.title: each["content"] = editInfo.content each["title"] = editInfo.title await crud_dept.update( condition=Department.c.name == current_user["department"], mail_template=str(template)) # 添加后台任务 operation = f"更新了信息模板【{editInfo.title}】" task.add_task(addRecordTask, current_user["nick"], operation, current_user["department"]) return {"code": 0, "mail_template": template} raise settings.CUSTOM_EXCEPTION(503, "模板不存在")
async def getUserinfo(code: str) -> Dict[str, str]: access_key = settings.APP_ID time_stamp = str(round(time.time() * 1000)) signature = GenUrlSignature(settings.APP_SECRET, time_stamp) try: async with aiohttp.ClientSession() as session: async with session.post( url= f"https://oapi.dingtalk.com/sns/getuserinfo_bycode?accessKey={access_key}×tamp={time_stamp}&signature={signature}", json={"tmp_auth_code": code}) as resp: res = await resp.json() except: raise settings.CUSTOM_EXCEPTION(401, "授权码无效 请尝试重新登录") # 验证授权码是否有效 if res['errcode'] != 0: raise settings.CUSTOM_EXCEPTION(401, "授权码无效 请尝试重新登录") return res
async def updateSign( cvInfo: UpdateSign, task: BackgroundTasks, current_user: Dict = Security(get_current_user, scopes=["administrator", "supervisor"]) ) -> Dict: cv = await crud_cv.select_one_by(CvInfo.c.sno == cvInfo.sno) if not cv: raise settings.CUSTOM_EXCEPTION(503, "简历信息不存在") try: await crud_cv.update(condition=CvInfo.c.sno == cvInfo.sno, sign=cvInfo.sign) except: raise settings.CUSTOM_EXCEPTION(503, "状态更新失败") operation = f"标记{cvInfo.name}" if cvInfo.sign else f"取消标记{cvInfo.name}" task.add_task(addRecordTask, current_user["nick"], operation, current_user["department"]) return {"code": 0}
async def validate_auth(form: OAuth2PasswordRequestForm = Depends()) -> Dict: def get_openid(usr: str, pwd: str) -> str: return usr + pwd openid = get_openid(form.username, form.password) current_user = await crud_user.select_one_by(User.c.openid == openid) if not current_user: raise settings.CUSTOM_EXCEPTION(503, "账号或密码错误") return current_user
async def sendEmail(task: BackgroundTasks, current_user: Dict = Security( get_current_user, scopes=["administrator", "supervisor"]), content: str = Form(...), subject: str = Form(...), recipients: List[str] = Form(...), file: UploadFile = File(None)): if not recipients: raise settings.CUSTOM_EXCEPTION(503, "没有简历被选中") try: await send_email(recipients, subject, content, file) except Exception as e: print(e) raise settings.CUSTOM_EXCEPTION(503, "邮件发送失败") operation = "发送了邮件" task.add_task(addRecordTask, current_user["nick"], operation, current_user["department"]) return {"code": 0}
async def updateState( task: BackgroundTasks, data: UpdateState, current_user: Dict = Security(get_current_user, scopes=["administrator", "supervisor"]), ) -> Dict: show = data.show try: await crud_dept.update( condition=Department.c.name == current_user["department"], show=show) except: raise settings.CUSTOM_EXCEPTION(503, "状态更新失败") # 添加后台任务 operation = f"打开了部门展示" if show else "关闭了部门展示" task.add_task(addRecordTask, current_user["nick"], operation, current_user["department"]) return {"code": 0}
async def getDepartment(unionid: str) -> str: depts = [] try: async with aiohttp.ClientSession() as session: async with session.get( url= f"https://oapi.dingtalk.com/gettoken?appkey={settings.ACCESS_APPKEY}&appsecret={settings.ACCESS_APPSECRET}" ) as resp: res = await resp.json() access_token = res["access_token"] async with session.get( url= f"https://oapi.dingtalk.com/user/getUseridByUnionid?access_token={access_token}&unionid={unionid}" ) as resp: res = await resp.json() userid = res["userid"] async with session.get( url= f"https://oapi.dingtalk.com/user/get?access_token={access_token}&userid={userid}" ) as resp: res = await resp.json() dept_id = res["department"] for id in dept_id: async with session.get( url= f"https://oapi.dingtalk.com/department/get?access_token={access_token}&id={id}" ) as resp: res = await resp.json() depts.append(res["name"]) except: raise settings.SERVICE_EXCEPTION for dept in depts: for k, v in settings.DEPARTMENT_MAP.items(): if dept == k: return k if dept in v: return k raise settings.CUSTOM_EXCEPTION(503, "所属部门不合法")