Python实现苏大附一院挂号微服务:高效便捷的医疗预约系统

引言
随着科技的不断发展,医疗服务的数字化转型已成为大势所趋。苏州大学附属第一医院(苏大附一院)作为一所历史悠久的三甲医院,也在积极拥抱这一变革。为了提升患者的就医体验,减少排队等候时间,我们决定利用Python开发一套高效的挂号微服务系统。本文将详细介绍该系统的设计与实现过程。
系统需求分析
在开发之前,我们需要明确系统的基本需求:
用户注册与登录
:患者可以通过手机号或微信进行注册和登录。
科室与医生信息查询
:患者可以查看不同科室的医生信息,包括专家门诊和普通门诊。
预约挂号
:患者可以选择具体的时间段和医生进行挂号。
电子就诊码生成
:挂号成功后,系统生成电子就诊码,患者凭此码就诊。
医保脱卡支付
:支持医保电子凭证进行费用结算。
通知与提醒
:系统自动发送挂号成功和就诊时间提醒。
技术选型
为了实现上述功能,我们选择了以下技术栈:
后端
:Python + Flask框架
数据库
:MySQL
前端
:Vue.js + Element UI
微信接口
:微信小程序SDK
支付接口
:微信支付API
系统架构设计
系统采用前后端分离的架构,具体如下:
前端
:负责用户界面展示和交互。
后端
:处理业务逻辑,提供API接口。
数据库
:存储用户信息、医生信息、挂号记录等数据。
后端实现
1. 环境搭建
首先,我们需要安装Flask和相关库:
pip install Flask Flask-MySQLdb Flask-RESTful
2. 数据库设计
创建MySQL数据库,并设计以下表:
users
:存储用户信息
doctors
:存储医生信息
appointments
:存储挂号记录
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
phone VARCHAR(20) NOT NULL,
password VARCHAR(255) NOT NULL,
wechat_id VARCHAR(255)
);

CREATE TABLE doctors (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
department VARCHAR(50) NOT NULL,
title VARCHAR(50) NOT NULL
);

CREATE TABLE appointments (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
doctor_id INT NOT NULL,
appointment_time DATETIME NOT NULL,
status VARCHAR(20) NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (doctor_id) REFERENCES doctors(id)
);
3. 后端代码实现
创建一个Flask应用,并实现基本的API接口:
from flask import Flask, request, jsonify
from flask_restful import Resource, Api
import mysql.connector

app = Flask(__name__)
api = Api(app)

# 数据库配置
db_config = {
‘host’: ‘localhost’,
‘user’: ‘root’,
‘password’: ‘password’,
‘database’: ‘hospital’
}

# 用户注册
class UserRegister(Resource):
def post(self):
data = request.get_json()
phone = data[‘phone’]
password = data[‘password’]
wechat_id = data.get(‘wechat_id’, None)

conn = mysql.connector.connect(**db_config)
cursor = conn.cursor()
cursor.execute(“INSERT INTO users (phone, password, wechat_id) VALUES (%s, %s, %s)”, (phone, password, wechat_id))
conn.commit()
cursor.close()
conn.close()

return jsonify({‘message’: ‘User registered successfully’})

# 科室与医生信息查询
class DoctorInfo(Resource):
def get(self):
department = request.args.get(‘department’)

conn = mysql.connector.connect(**db_config)
cursor = conn.cursor(dictionary=True)
if department:
cursor.execute(“SELECT * FROM doctors WHERE department=%s”, (department,))
else:
cursor.execute(“SELECT * FROM doctors”)
doctors = cursor.fetchall()
cursor.close()
conn.close()

return jsonify(doctors)

# 预约挂号
class Appointment(Resource):
def post(self):
data = request.get_json()
user_id = data[‘user_id’]
doctor_id = data[‘doctor_id’]
appointment_time = data[‘appointment_time’]

conn = mysql.connector.connect(**db_config)
cursor = conn.cursor()
cursor.execute(“INSERT INTO appointments (user_id, doctor_id, appointment_time, status) VALUES (%s, %s, %s, ‘pending’)”, (user_id, doctor_id, appointment_time))
conn.commit()
cursor.close()
conn.close()

return jsonify({‘message’: ‘Appointment booked successfully’})

api.add_resource(UserRegister, ‘/register’)
api.add_resource(DoctorInfo, ‘/doctors’)
api.add_resource(Appointment, ‘/appointments’)

if __name__ == ‘__main__’:
app.run(debug=True)
前端实现
前端使用Vue.js和Element UI进行开发,这里简要展示挂号页面的实现:


微信接口与支付
1. 微信小程序登录
使用微信小程序SDK实现用户登录:
// 小程序端登录
wx.login({
success: function(res) {
if (res.code) {
// 发送请求到后端,换取openId和sessionKey
wx.request({
url: ‘https://your-backend.com/wx/login’,
method: ‘POST’,
data: { code: res.code },
success: function(response) {
// 存储用户信息
wx.setStorageSync(‘userId’, response.data.userId);
}
});
}
}
});
2. 微信支付
集成微信支付API,实现挂号费用的在线支付:
import requests

def wechat_pay(appointment_id, amount):
# 获取微信支付参数
params = {
‘body’: ‘挂号费用’,
‘out_trade_no’: appointment_id,
‘total_fee’: amount,
‘spbill_create_ip’: ‘127.0.0.1’,
‘notify_url’: ‘https://your-backend.com/wx/pay/notify’,
‘trade_type’: ‘JSAPI’,
‘openid’: ‘user_openid’
}
response = requests.post(‘https://api.mch.weixin.qq.com/pay/unifiedorder’, data=params)
return response.json()

@app.route(‘/wx/pay’, methods=[‘POST’])
def wx_pay():
data = request.get_json()
appointment_id = data[‘appointment_id’]
amount = data[‘amount’]
pay_info = wechat_pay(appointment_id, amount)
return jsonify(pay_info)
系统测试与部署
完成开发后,进行系统测试,确保各功能模块正常运行。测试通过后,将系统部署到云服务器,并进行必要的性能优化和安全配置。
总结
通过Python和Flask框架,我们成功开发了一套高效便捷的苏大附一院挂号微服务系统。该系统不仅提升了患者的就医体验,还优化了医院的资源配置。未来,我们还将继续完善系统功能,引入更多智能化服务,为患者提供更加优质的医疗服务。
希望本文能为有类似需求的项目提供参考和借鉴。如果你有任何问题或建议,欢迎在评论区留言交流!