async def login_access_token( *, db: Session = Depends(deps.get_db), user_info: sys_user_schema.UserEmailAuth, ) -> Any: """ 用户JWT登录 :param db: :param user_info: :return: """ # 验证用户 user = curd_user.authenticate(db, email=user_info.username, password=user_info.password) if not user: logger.info( f"用户邮箱认证错误: email{user_info.username} password:{user_info.password}" ) return response_code.resp_4003(message="username or password error") elif not curd_user.is_active(user): return response_code.resp_4003(message="User email not activated") access_token_expires = timedelta( minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES) # 登录token 存储了user.id 和 authority_id return response_code.resp_200( data={ "token": security.create_access_token(user.id, user.authority_id, expires_delta=access_token_expires), })
async def add_job_to_scheduler( *, seconds: int = Body(120, title="循环间隔时间/秒,默认120s", embed=True), job_id: str = Body(..., title="任务id", embed=True), ): """ 简易的任务调度演示 可自行参考文档 https://apscheduler.readthedocs.io/en/stable/ 三种模式 date: use when you want to run the job just once at a certain point of time interval: use when you want to run the job at fixed intervals of time cron: use when you want to run the job periodically at certain time(s) of day :param seconds: :param job_id: :return: """ res = schedule.get_job(job_id=job_id) if res: return response_code.resp_4001(message=f"{job_id} job already exists") schedule_job = schedule.add_job( demo_task, 'interval', args=(job_id, ), seconds=seconds, # 循环间隔时间 秒 id=job_id, # job ID next_run_time=datetime.now() # 立即执行 ) return response_code.resp_200(data={"id": schedule_job.id})
async def get_all_user_info( page: int = Query(1), # 分页等通用字段可以提取出来封装 page_size: int = Query(20), db: Session = Depends(deps.get_db), ): all_user = curd_user.get_multi(db=db, page=page, page_size=page_size) return response_code.resp_200(data=all_user)
async def remove_schedule(job_id: str = Body(..., title="job_id", embed=True)): res = schedule.get_job(job_id=job_id) if not res: return response_code.resp_4001(message=f"not found job {job_id}") schedule.remove_job(job_id) return response_code.resp_200()
async def del_authority( authority_info: AuthCreate ): e = get_casbin() res = e.remove_policy(authority_info.authority_id, authority_info.path, authority_info.method) if res: return response_code.resp_200() else: return response_code.resp_4001(message="删除失败,权限不存在")
async def remove_schedule(*, job_id: str = Body(..., title="job_id", embed=True), response: Response): res = schedule.get_job(job_id=job_id) if not res: response.status_code = status.HTTP_404_NOT_FOUND return response_code.resp_error(message=f"找不到工作 {job_id}") schedule.remove_job(job_id) response.status_code = status.HTTP_200_OK return response_code.resp_200()
async def get_map_url(*, lng: str, lat: str, response: Response) -> Any: """ 根据经纬度查看地图信息\n lng: 经度\n lat: 纬度 """ regeo = f"https://ditu.amap.com/regeo?lng={lng}&lat={lat}&src=uriapi&innersrc=uriapi" message = "查看地图信息成功" response.status_code = status.HTTP_200_OK return response_code.resp_200(data={"regeo": regeo}, message=message)
async def get_user_info( *, current_user: sys_auth.SysUser = Depends(deps.get_current_user)) -> Any: """ 获取用户信息 这个路由分组没有验证权限 :param current_user: :return: """ return response_code.resp_200(data={ "nickname": current_user.nickname, "avatar": current_user.avatar })
async def get_target_sync(job_id: str = Query(..., title="任务id")): job = schedule.get_job(job_id=job_id) if not job: return response_code.resp_4001(message=f"not found job {job_id}") return response_code.resp_200( data={ "job_id": job.id, "func_name": job.func_ref, "func_args": job.args, "cron_model": str(job.trigger), "next_run": str(job.next_run_time) })
async def get_scheduled_syncs(): """ 获取所有job :return: """ schedules = [] for job in schedule.get_jobs(): schedules.append({ "job_id": job.id, "func_name": job.func_ref, "func_args": job.args, "cron_model": str(job.trigger), "next_run": str(job.next_run_time) }) return response_code.resp_200(data=schedules)
async def upload_image(file: UploadFile = File(...), ): # 本地存储临时方案,一般生产都是使用第三方云存储OSS(如七牛云, 阿里云) # 建议计算并记录一次 文件md5值 避免重复存储相同资源 save_dir = f"{settings.BASE_PATH}/static/img" if not os.path.exists(save_dir): os.mkdir(save_dir) try: suffix = Path(file.filename).suffix with NamedTemporaryFile(delete=False, suffix=suffix, dir=save_dir) as tmp: shutil.copyfileobj(file.file, tmp) tmp_file_name = Path(tmp.name).name finally: file.file.close() return response_code.resp_200( data={"image": f"/static/img/{tmp_file_name}"})
async def items_test( *, bar: str = Query(..., title="测试字段", description="测试字段描述"), db: Session = Depends(deps.get_db), ) -> Any: """ 用户登录 :param bar: :param db: :return: """ # 测试redis使用 redis_client.set("test_items", bar, ex=60) redis_test = redis_client.get("test_items") # 用不惯orm查询的可以直接sql(建议把CURD操作单独放到service文件夹下,统一管理) test_sql = "SELECT nickname,avatar from sys_user WHERE id>=:id" admin_user_res = db.execute(text(test_sql), {"id": 1}).fetchall() return response_code.resp_200(data={ "items": "ok", "admin_user_info": admin_user_res, "redis_test": redis_test })
async def add_api( api_info: ApiCreate, db: Session = Depends(deps.get_db), ): obj_info = curd_api.create(db, api_info) return response_code.resp_200(data=obj_info)
async def get_all_apis(db: Session = Depends(deps.get_db), ): api_list = curd_api.get_multi(db) return response_code.resp_200(data=api_list)