Commit 0269e24c authored by ZZH's avatar ZZH

update news info 2024-03-6

parent 916d9f4b
...@@ -10,43 +10,46 @@ from pot_libs.sanic_api.column import Opt, Int, List, Str ...@@ -10,43 +10,46 @@ from pot_libs.sanic_api.column import Opt, Int, List, Str
@dataclass @dataclass
class NewsInfoReq(Model): class NewsInfoReq(Model):
news_id: str = Str("新闻id").eg("12") news_id: str = Str("文章id").eg("12")
@dataclass
class EntIndustryNewsPagesReq(Model):
news_type: int = Int("新闻类型(1:公司新闻, 2:行业新闻)").eg(1)
page_num: int = Int("页码").eg(1)
page_size: int = Opt(Int("条数").eg(10))
@dataclass @dataclass
class NewsInfoRsp(Model): class NewsInfoRsp(Model):
title: str = Str("新闻标题").eg("喜报丨热烈庆祝清科优能被评定为深圳市“专精特新”企业!") title: str = Str("文章标题").eg("喜报丨热烈庆祝清科优能被评定为深圳市“专精特新”企业!")
contents: str = Str("新闻内容").eg("喜报丨热烈庆祝清科优能被评定为深圳市“专精特新”企业!") contents: str = Str("文章内容").eg("喜报丨热烈庆祝清科优能被评定为深圳市“专精特新”企业!")
pub_time: str = Str("发布时间").eg("2023-12-29 18:06") pub_time: str = Str("发布时间").eg("2023-12-29 18:06")
@dataclass @dataclass
class PubNewsReq(Model): class PubNewsReq(Model):
title: str = Str("新闻标题").eg("喜报丨热烈庆祝清科优能被评定为深圳市“专精特新”企业!") title: str = Str("文章标题").eg("喜报丨热烈庆祝清科优能被评定为深圳市“专精特新”企业!")
author: str = Opt(Str("文章作者").eg("清科优能"))
pub_time: str = Str("发布时间").eg("2023-12-29 18:06") pub_time: str = Str("发布时间").eg("2023-12-29 18:06")
@dataclass
class ReviseNewsReq(Model):
news_id: str = Str("文章id").eg("12")
title: str = Str("文章标题").eg("喜报丨热烈庆祝清科优能被评定为深圳市“专精特新”企业!")
@dataclass @dataclass
class NewsPagesReq(Model): class NewsPagesReq(Model):
news_type: int = Opt(Int("文章类型(1:公司新闻, 2:行业新闻) 不传则返回所有类型文章").eg(1))
page_num: int = Int("页码").eg(1) page_num: int = Int("页码").eg(1)
page_size: int = Opt(Int("条数").eg(10)) page_size: int = Opt(Int("条数").eg(10))
@dataclass @dataclass
class NewsInfo(Model): class NewsInfo(Model):
id: int = Opt(Int("新闻ID").eg(1)) id: int = Opt(Int("文章ID").eg(1))
title: str = Str("新闻标题").eg("喜报丨热烈庆祝清科优能被评定为深圳市“专精特新”企业!") title: str = Str("文章标题").eg("喜报丨热烈庆祝清科优能被评定为深圳市“专精特新”企业!")
author: str = Str("文章作者").eg("清科优能")
pub_time: str = Str("发布时间").eg("2023-12-29 18:06") pub_time: str = Str("发布时间").eg("2023-12-29 18:06")
news_type: int = Int("文章类型(1:公司新闻, 2:行业新闻)").eg(1)
top: int = Opt(Int("是否置顶").eg(0)) top: int = Opt(Int("是否置顶").eg(0))
contents: str = Opt(Str("新闻内容").eg("喜报丨热烈庆祝清科优能被评定为深圳市“专精特新”企业!")) contents: str = Opt(Str("文章内容").eg("喜报丨热烈庆祝清科优能被评定为深圳市“专精特新”企业!"))
cover_url: str = Opt(Str("新闻封面URL").eg("xxxxxxxxxx")) cover_url: str = Opt(Str("文章封面URL").eg("xxxxxxxxxx"))
@dataclass @dataclass
......
...@@ -43,7 +43,7 @@ async def upload_news_qs(request): ...@@ -43,7 +43,7 @@ async def upload_news_qs(request):
else: else:
return False, url_article, url_cover return False, url_article, url_cover
log.info(f"上传新闻和封面成功 {url_article} {url_cover}") log.info(f"上传文章和封面成功 {url_article} {url_cover}")
return True, url_article, url_cover return True, url_article, url_cover
...@@ -51,19 +51,44 @@ async def save_news_info(title, pub_time, news_url, cover_url): ...@@ -51,19 +51,44 @@ async def save_news_info(title, pub_time, news_url, cover_url):
sql = "INSERT INTO official_web.news_info (" \ sql = "INSERT INTO official_web.news_info (" \
"title, pub_time, news_url, cover_url) " \ "title, pub_time, news_url, cover_url) " \
"VALUES (%s, %s, %s, %s);" "VALUES (%s, %s, %s, %s);"
async with MysqlUtil() as conn: async with MysqlUtil(db="official_web") as conn:
await conn.execute(sql, (title, str(pub_time), news_url, cover_url)) await conn.execute(sql, (title, str(pub_time), news_url, cover_url))
async def load_news_pages(page_num, page_size): async def update_news_info(news_id, title, news_url, cover_url):
async with MysqlUtil() as conn: async with MysqlUtil(db="official_web") as conn:
sql = "SELECT count(*) FROM official_web.news_info;" if cover_url:
total = conn.fetch_value(sql) sql = "UPDATE official_web.news_info " \
"SET title=%s, news_url=%s, cover_url=%s WHERE id=%s;"
return await conn.execute(sql, (news_id, title, news_url,
cover_url))
sql = f"SELECT id, title, pub_time, top " \ sql = "UPDATE official_web.news_info " \
f"FROM official_web.news_info " \ "SET title=%s, news_url=%s WHERE id=%s;"
f"ORDER BY pub_time DESC, id LIMIT %s OFFSET %s;" return await conn.execute(sql, (news_id, title, news_url,))
offsets = 0 if page_num == 0 else (page_num - 1) * page_size
articles = await conn.fetchall(sql, (page_size, offsets)) async def load_news_pages(news_type, page_num, page_size):
async with MysqlUtil(db="official_web") as conn:
if news_type:
sql = "SELECT count(*) FROM official_web.news_info " \
"WHERE news_type=%s;"
total = conn.fetch_value(sql, (news_type,))
sql = f"SELECT id, title, author, news_type, pub_time, top " \
f"FROM official_web.news_info " \
f"WHERE news_type=%s " \
f"ORDER BY pub_time DESC, id LIMIT %s OFFSET %s;"
offsets = 0 if page_num == 0 else (page_num - 1) * page_size
articles = await conn.fetchall(sql, (news_type, page_size,
offsets,))
else:
sql = "SELECT count(*) FROM official_web.news_info;"
total = conn.fetch_value(sql)
sql = f"SELECT id, title, author, news_type, pub_time, top " \
f"FROM official_web.news_info " \
f"ORDER BY pub_time DESC, id LIMIT %s OFFSET %s;"
offsets = 0 if page_num == 0 else (page_num - 1) * page_size
articles = await conn.fetchall(sql, (page_size, offsets,))
return total, articles return total, articles
...@@ -6,30 +6,43 @@ DATE:2024/3/5 16:19 ...@@ -6,30 +6,43 @@ DATE:2024/3/5 16:19
from pot_libs.sanic_api import summary from pot_libs.sanic_api import summary
from unify_api.modules.common.components.common_cps import SuccessRsp from unify_api.modules.common.components.common_cps import SuccessRsp
from unify_api.modules.qkadmin.components.news_info_cps import ( from unify_api.modules.qkadmin.components.news_info_cps import (
PubNewsReq, NewsPagesReq, NewsPagesRsp, NewsInfo PubNewsReq, NewsPagesReq, NewsPagesRsp, NewsInfo, ReviseNewsReq
) )
from unify_api.modules.qkadmin.service.news_mgr_srv import ( from unify_api.modules.qkadmin.service.news_mgr_srv import (
upload_news_qs, save_news_info, load_news_pages upload_news_qs, save_news_info, load_news_pages, update_news_info
) )
@summary("发布新闻") @summary("发布文章")
async def post_publish_news(request, body: PubNewsReq) -> SuccessRsp: async def post_publish_news(request, body: PubNewsReq) -> SuccessRsp:
title, pub_time = body.title, body.pub_time title, pub_time, author = body.title, body.pub_time, body.author
is_success, url_article, url_cover = await upload_news_qs(request) is_success, url_article, url_cover = await upload_news_qs(request)
if is_success: if is_success:
await save_news_info(title, pub_time, url_article, url_cover) await save_news_info(title, pub_time, url_article, url_cover)
return SuccessRsp(success=1, message="发布新闻成功") return SuccessRsp(success=1, message="发布文章成功")
return SuccessRsp(success=0, message="发布新闻失败") return SuccessRsp(success=0, message="发布文章失败")
@summary("后台新闻列表") @summary("修改文章")
async def post_revise_news(request, body: ReviseNewsReq) -> SuccessRsp:
news_id, title = body.news_id, body.title
is_success, url_article, url_cover = await upload_news_qs(request)
if is_success:
await update_news_info(news_id, title, url_article, url_cover)
return SuccessRsp(success=1, message="修改文章成功")
return SuccessRsp(success=0, message="修改文章失败")
@summary("后台文章列表")
async def get_news_pages(request, body: NewsPagesReq) -> NewsPagesRsp: async def get_news_pages(request, body: NewsPagesReq) -> NewsPagesRsp:
page_num = int(body.page_num) page_num = int(body.page_num)
page_size = int(body.page_size) page_size = int(body.page_size)
total, articles = load_news_pages(page_num, page_size) news_type = int(body.page_size) if body.page_size else 0
total, articles = load_news_pages(news_type, page_num, page_size)
rsp_articles = [ rsp_articles = [
NewsInfo(id=r["id"], title=r["title"], pub_time=r["pub_time"], NewsInfo(id=r["id"], title=r["title"], author=r["author"],
pub_time=r["pub_time"], news_type=r["news_type"],
top=r["top"]) for r in articles] top=r["top"]) for r in articles]
return NewsPagesRsp(total=total, articles=rsp_articles) return NewsPagesRsp(total=total, articles=rsp_articles)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment