pzx-web-api/app/api/v1/api.py

116 lines
3.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import socket
import os
import platform
import psutil
from apiflask import APIBlueprint
from flask import session
from app import rpc
from app.api.v1.exception.api import ImageUploadError, UserInfoError
from app.api.v1.schema.api import ImageIn
from app.util.auth import login_required
api = APIBlueprint('api', __name__)
@api.post('/images')
@api.doc(summary='上传图片', description='上传图片')
@api.input(ImageIn, location='files')
@login_required
def wechat_login(files_data):
f = files_data['image']
try:
result = rpc.storage.upload(file_name='avatar_' + str(session['user_id']+f'_{f.filename}'),
file_binary=f.read(),
bucket='bodyrecord',
folder='avatar',
app='bodyrecord')
except Exception as e:
raise ImageUploadError(extra_data={'error_docs': str(e)})
if result.get('status') and result['status'] == 'UPLOADED':
try:
rpc.admin.set_user_info(session['user_id'],
avatar_id=result['_id'])
except Exception as e:
raise UserInfoError(extra_data={'error_docs': str(e)})
return {'msg': 'uploading image success'}
else:
return {'msg': 'uploading image fail'}
@api.get('/hello')
def hello():
result_one = f'我是您的专属接口提供服务器:'
result_two = f'我的机器参数如下'
result_three = f'操作系统:{platform.system()}'
text = """
<style type="text/css">
* {
padding: 0;
margin: 0;
}
div {
padding: 4px 48px;
}
a {
color: black;
cursor: pointer;
text-decoration: none
}
a:hover {
text-decoration: None;
}
body {
background: #fff;
font-family:
"Century Gothic", "Microsoft yahei";
color: #333;
font-size: 18px;
}
h1 {
font-size: 100px;
font-weight: normal;
margin-bottom: 12px;
}
p {
line-height: 1.6em;
font-size: 42px
}
</style>
<div style="padding: 24px 48px;">
<p>
<a href="" target="_Blank">您好PZX 大人!</a>
<br />
<span style="font-size:30px">
<a href=""> """ + result_one + """</a>
<br />
<a href=""> """ + f'{os.environ["HOSTNAME"]} ' + f'进程 {socket.gethostname()}' + """</a>
</span>
<br />
<span style="font-size:25px">
<a href=""> """ + result_two + """</a>
</span>
<br />
<span style="font-size:20px">
<a href=""> """ + result_three + """</a>
<br />
<a href=""> """ + f'CPU 核数:{psutil.cpu_count()},目前 CPU 占用率: {psutil.cpu_percent()}' + """</a>
<br />
<a href=""> """ + f'总内存 {round(psutil.virtual_memory().total / 1024 / 1024, 2)} MB使用中内存{round(psutil.virtual_memory().used / 1024 / 1024, 2)} MB' + """</a>
<br />
<a href=""> """ + f'磁盘总空间 {round(psutil.disk_usage("/").total / 1024 / 1024 / 1024, 2)} G磁盘使用情况{round(psutil.disk_usage("/").used / 1024 / 1024 / 1024, 2)} G' + """</a>
</span>
</p>
</div>
"""
return text