diff --git a/api/main.py b/api/main.py index 27deb9e..983f853 100644 --- a/api/main.py +++ b/api/main.py @@ -11,53 +11,47 @@ app = Flask(__name__) client = MongoClient("mongodb://localhost:27019/") db = client["back"] collection = db["users"] -# sockitIo -socketio = SocketIO() -# 解决跨域问题 -socketio.init_app(app, cors_allowed_origins="*") +# sockitIo解决跨域问题 +socketMsg = SocketIO(app, cors_allowed_origins="*") +socketVedio = SocketIO(app, cors_allowed_origins="*") # 用来存放客户端的 sid,即 session id # 可以不单独定义字典存放 sid与namespace,flask-socketio 默认将 sid 存放在 room 中 socket_pool = {} # Websocket 通过namespace 和 sid 标识具体客户端 -# 第一个 Websocket 类 +# 第一个 Websocket 类用于聊天 class MyCustomNamespace(Namespace): name_space = "/msg" # 连接成功调用的方法 def on_connect(self): global socket_pool - - print("connect..") - print("-----------------") - print(self.server.manager) - print(self.socketio) - print("-----------------") - print(request.namespace) socket_pool[request.sid] = self.name_space - print(socket_pool) + print(1, socket_pool) + print(2, "Client connected") + print(3, "-----------------") # 断开连接调用的方法 def on_disconnect(self): global socket_pool - print("disconnect...") + print(4, "disconnect...") del socket_pool[request.sid] - print(socket_pool) + print(5, socket_pool) # 往 接收客户端标消息识为 'message' 的方法 def on_message(self, data): - print(data) + print(6, data) # 把消息发送到客户端的 'response' 标识的方法中, 一般是 on_response() emit("response", "123", to=request.sid) - print("--------------") - print(request.sid) - print("--------------") - print(socketio.server.manager.rooms) + print(7, "--------------") + print(8, request.sid) + print(9, "--------------") + print(10, socketio.server.manager.rooms) # 往 接收客户端标消息识为 'hello' 的方法 def on_hello(self, data): - print("hello world") + print(11, "hello world") # 发送消息 def send(self, data): @@ -68,26 +62,26 @@ class MyCustomNamespace(Namespace): # 为了详细展示不同类,这里就不做继承 -# 第二个 Websocket 类 +# 第二个 Websocket 类用于视频 class MyCustomNamespace1(Namespace): name_space = "/vedio" def on_connect(self): global socket_pool - print("connect..") - print(request.namespace) + print(12, "connect..") + print(13, request.namespace) socket_pool[request.sid] = self.name_space - print(socket_pool) + print(14, socket_pool) def on_disconnect(self): global socket_pool - print("disconnect...") + print(15, "disconnect...") del socket_pool[request.sid] - print(socket_pool) + print(16, socket_pool) def on_message(self, data): - print(data) + print(17, data) emit("response", "123") self.send("asd") @@ -96,12 +90,16 @@ class MyCustomNamespace1(Namespace): emit("response", data, namespace=self.name_space) +socketMsg.on_namespace(MyCustomNamespace("/msg")) +socketVedio.on_namespace(MyCustomNamespace("/vedio")) + + @app.route("/sendMsg") def sendMsg(): for sid, namespace_value in socket_pool.items(): - print(sid) + print(18, sid) emit("response", "123456", namespace=namespace_value, to=sid) - print("ok") + print(19, "ok") return "ok" @@ -188,36 +186,37 @@ def insert_data(): # 获取到POST过来的数据,转为json形式 userJson = json.loads(resData) admin = isAdmin(userJson["id"]) - print(777, userJson, admin) if admin: - if userJson["subType"] is "add": - userItem = { - "xuexiaomingcheng": userJson["xuexiaomingcheng"], - "xuexiaodaihao": userJson["xuexiaodaihao"], - "zhuanyemingcheng": userJson["zhuanyemingcheng"], - "zhuanyedaihao": userJson["zhuanyedaihao"], - "nianji": userJson["nianji"], - "banji": userJson["banji"], - "xueshengxingming": userJson["xueshengxingming"], - "xuehao": userJson["xuehao"], - "mima": md5_encrypt(userJson["mima"]), - "chengji": userJson["chengji"], - "zuobiqingkuang": userJson["zuobiqingkuang"], - "kaoshileixing": userJson["kaoshileixing"], - "kaoshikemu": userJson["kaoshikemu"], - "kaoshishijianduan": userJson["kaoshishijianduan"], - "kaoshilianjie": "", # 需要自动生成一个链接 - "isAdmin": 0, - "isExit": 1, - "chuangjianshijian": time.strftime( - "%Y-%m-%d %H:%M:%S", time.localtime(time.time()) - ), - "gengxinshijian": time.strftime( - "%Y-%m-%d %H:%M:%S", time.localtime(time.time()) - ), - } - collection.insert_one(userItem) - return "新增成功" + _id = ObjectId() + print(77337, str(_id)) + userItem = { + "_id": _id, + "xuexiaomingcheng": userJson["xuexiaomingcheng"], + "xuexiaodaihao": userJson["xuexiaodaihao"], + "zhuanyemingcheng": userJson["zhuanyemingcheng"], + "zhuanyedaihao": userJson["zhuanyedaihao"], + "nianji": userJson["nianji"], + "banji": userJson["banji"], + "xueshengxingming": userJson["xueshengxingming"], + "xuehao": userJson["xuehao"], + "mima": md5_encrypt(userJson["mima"]), + "chengji": userJson["chengji"], + "zuobiqingkuang": userJson["zuobiqingkuang"], + "kaoshileixing": userJson["kaoshileixing"], + "kaoshikemu": userJson["kaoshikemu"], + "kaoshishijianduan": userJson["kaoshishijianduan"], + "kaoshilianjie": "/student/" + str(_id), # 需要自动生成一个链接 + "isAdmin": 0, + "isExit": 1, + "chuangjianshijian": time.strftime( + "%Y-%m-%d %H:%M:%S", time.localtime(time.time()) + ), + "gengxinshijian": time.strftime( + "%Y-%m-%d %H:%M:%S", time.localtime(time.time()) + ), + } + collection.insert_one(userItem) + return "新增成功" # 查询用户 @@ -244,7 +243,15 @@ def query_data(): "hasError": False, } else: - return {"code": 200, "msg": "您不是管理员用户", "list": [], "hasError": True} + mongoId = ObjectId(userJson["id"]) + userObj = collection.find_one({"_id": mongoId, "isExit": 1}) + userObj["_id"] = str(userObj["_id"]) # 将ObjectId转换为字符串 + return { + "code": 200, + "msg": "查询成功", + "list": [userObj], + "hasError": False, + } # 更新用户 @@ -324,4 +331,6 @@ def updatePassword(): if __name__ == "__main__": initData() - app.run(debug=True) + # app.run(debug=True) + socketMsg.run(app, debug=True) + # socketVedio.run(app, debug=True) diff --git a/front/package.json b/front/package.json index 9370595..0bd5bf3 100644 --- a/front/package.json +++ b/front/package.json @@ -17,6 +17,7 @@ "nprogress": "^0.2.0", "papaparse": "^5.4.1", "qs": "^6.11.2", + "socket.io-client": "^4.7.5", "sqids": "^0.3.0", "unplugin-auto-import": "^0.17.4", "unplugin-vue-components": "^0.26.0", diff --git a/front/src/api/student.js b/front/src/api/student.js new file mode 100644 index 0000000..a93007a --- /dev/null +++ b/front/src/api/student.js @@ -0,0 +1,26 @@ +import _axios from "@/plugins/axios"; + +//修改用户 +export function updateUser(data) { + return _axios({ + url: `/v1/updateUser`, + method: "POST", + data: { id: localStorage.getItem("userId"), ...data }, + }); +} +//查找用户 +export function getUser(data) { + return _axios({ + url: `/v1/getUser`, + method: "POST", + data: { id: localStorage.getItem("userId"), ...data }, + }); +} +//修改密码 +export function updatePassword(data) { + return _axios({ + url: `/v1/updatePassword`, + method: "POST", + data: { id: localStorage.getItem("userId"), ...data }, + }); +} diff --git a/front/src/api/user.js b/front/src/api/teacher.js similarity index 100% rename from front/src/api/user.js rename to front/src/api/teacher.js diff --git a/front/src/component/table.vue b/front/src/component/table.vue index 2867438..4b1b183 100644 --- a/front/src/component/table.vue +++ b/front/src/component/table.vue @@ -15,6 +15,16 @@ + + + diff --git a/front/src/views/user.vue b/front/src/views/teacher.vue similarity index 95% rename from front/src/views/user.vue rename to front/src/views/teacher.vue index c8cd2fa..495c521 100644 --- a/front/src/views/user.vue +++ b/front/src/views/teacher.vue @@ -47,9 +47,9 @@ import { updateUser, //修改user信息 getUser, //获取user信息 updatePassword,//修改密码 -} from "@/api/user"; +} from "@/api/teacher"; export default { - name: "user", + name: "teacher", components: { vueTable, changeItem }, data() { return { @@ -60,14 +60,14 @@ export default { prop: "xuexiaomingcheng", label: "学校名称", type: "text", - tableShow: true, + tableShow: false, formShow: true }, { prop: "xuexiaodaihao", label: "学校代号", type: "text", - tableShow: true, + tableShow: false, formShow: true }, { @@ -81,14 +81,14 @@ export default { prop: "zhuanyedaihao", label: "专业代号", type: "text", - tableShow: true, + tableShow: false, formShow: true }, { prop: "nianji", label: "年级", type: "text", - tableShow: true, + tableShow: false, formShow: true }, { @@ -116,7 +116,7 @@ export default { prop: "mima", label: "密码", type: "text", - tableShow: true, + tableShow: false, formShow: true }, { @@ -130,7 +130,7 @@ export default { prop: "zuobiqingkuang", label: "作弊情况", type: "text", - tableShow: true, + tableShow: false, formShow: true }, { @@ -147,6 +147,13 @@ export default { tableShow: true, formShow: true }, + { + prop: "kaoshilianjie", + label: "考试链接", + type: "link", + tableShow: true, + formShow: true + }, { prop: "kaoshishijianduan", label: "考试时间段", @@ -282,7 +289,7 @@ export default { cancelButtonText: "取消", type: "error", }).then(async () => { - let res = await delUser({ subType: "del", ids: delData }); + let res = await delUser({ ids: delData }); await this.init(); ElMessage({ message: res.msg, diff --git a/test.py b/test.py new file mode 100644 index 0000000..fd3cb56 --- /dev/null +++ b/test.py @@ -0,0 +1,22 @@ +from flask import Flask, render_template +from flask_socketio import SocketIO, emit, Namespace +from threading import Thread, Event + +app = Flask(__name__) +app.config['SECRET_KEY'] = 'secret!' +socketio = SocketIO(app, cors_allowed_origins="*") + +class MyCustomNamespace(Namespace): + def on_connect(self): + print('Client connected') + + def on_disconnect(self): + print('Client disconnected') + + def on_message(self, message): + emit('response', {'data': 'Got it!'}, broadcast=True) + +socketio.on_namespace(MyCustomNamespace('/test')) + +if __name__ == '__main__': + socketio.run(app, debug=True) \ No newline at end of file