|
@@ -3,9 +3,9 @@
|
|
|
基于 create_calendar_info.sql 中的DDL定义创建
|
|
|
"""
|
|
|
|
|
|
-from datetime import date
|
|
|
+from datetime import date, datetime
|
|
|
from typing import Optional
|
|
|
-from sqlalchemy import Column, Integer, Date, Text, String
|
|
|
+from sqlalchemy import Column, Integer, Date, Text, String, Boolean, DateTime
|
|
|
from sqlalchemy.orm import Session
|
|
|
from sqlalchemy import create_engine, text
|
|
|
import json
|
|
@@ -143,6 +143,126 @@ class CalendarInfo(db.Model):
|
|
|
)
|
|
|
|
|
|
|
|
|
+class WechatUser(db.Model):
|
|
|
+ """
|
|
|
+ 微信用户信息表数据模型
|
|
|
+
|
|
|
+ 对应数据库表: public.wechat_users
|
|
|
+ 表注释: 微信用户信息表
|
|
|
+ """
|
|
|
+ __tablename__ = 'wechat_users'
|
|
|
+ __table_args__ = {'schema': 'public'}
|
|
|
+
|
|
|
+ # 主键ID (serial primary key)
|
|
|
+ id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='主键ID')
|
|
|
+
|
|
|
+ # 微信授权码/openid (varchar(255) not null unique)
|
|
|
+ wechat_code = db.Column(db.String(255), nullable=False, unique=True, comment='微信授权码/openid,唯一标识')
|
|
|
+
|
|
|
+ # 用户手机号码 (varchar(20))
|
|
|
+ phone_number = db.Column(db.String(20), nullable=True, comment='用户手机号码')
|
|
|
+
|
|
|
+ # 用户身份证号码 (varchar(18))
|
|
|
+ id_card_number = db.Column(db.String(18), nullable=True, comment='用户身份证号码')
|
|
|
+
|
|
|
+ # 当前登录状态 (boolean default false not null)
|
|
|
+ login_status = db.Column(db.Boolean, nullable=False, default=False, comment='当前登录状态,true表示已登录,false表示未登录')
|
|
|
+
|
|
|
+ # 最后登录时间 (timestamp with time zone)
|
|
|
+ login_time = db.Column(db.DateTime(timezone=True), nullable=True, comment='最后登录时间')
|
|
|
+
|
|
|
+ # 用户账户状态 (varchar(20) default 'active' not null)
|
|
|
+ user_status = db.Column(db.String(20), nullable=False, default='active', comment='用户账户状态:active-活跃,inactive-非活跃,suspended-暂停,deleted-已删除')
|
|
|
+
|
|
|
+ # 账户创建时间 (timestamp with time zone default current_timestamp not null)
|
|
|
+ created_at = db.Column(db.DateTime(timezone=True), nullable=False, default=datetime.utcnow, comment='账户创建时间')
|
|
|
+
|
|
|
+ # 信息更新时间 (timestamp with time zone default current_timestamp not null)
|
|
|
+ updated_at = db.Column(db.DateTime(timezone=True), nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow, comment='信息更新时间')
|
|
|
+
|
|
|
+ def __init__(self, **kwargs):
|
|
|
+ super().__init__(**kwargs)
|
|
|
+
|
|
|
+ def __repr__(self):
|
|
|
+ return f"<WechatUser(id={self.id}, wechat_code='{self.wechat_code}', user_status='{self.user_status}')>"
|
|
|
+
|
|
|
+ def to_dict(self) -> dict:
|
|
|
+ """
|
|
|
+ 将模型对象转换为字典
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ dict: 包含所有字段的字典
|
|
|
+ """
|
|
|
+ return {
|
|
|
+ 'id': self.id,
|
|
|
+ 'wechat_code': self.wechat_code,
|
|
|
+ 'phone_number': self.phone_number,
|
|
|
+ 'id_card_number': self.id_card_number,
|
|
|
+ 'login_status': self.login_status,
|
|
|
+ 'login_time': self.login_time.isoformat() if self.login_time is not None else None,
|
|
|
+ 'user_status': self.user_status,
|
|
|
+ 'created_at': self.created_at.isoformat() if self.created_at is not None else None,
|
|
|
+ 'updated_at': self.updated_at.isoformat() if self.updated_at is not None else None
|
|
|
+ }
|
|
|
+
|
|
|
+ def to_json(self) -> str:
|
|
|
+ """
|
|
|
+ 将模型对象转换为JSON字符串
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ str: JSON格式的字符串
|
|
|
+ """
|
|
|
+ return json.dumps(self.to_dict(), ensure_ascii=False, indent=2)
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def from_dict(cls, data: dict) -> 'WechatUser':
|
|
|
+ """
|
|
|
+ 从字典创建模型对象
|
|
|
+
|
|
|
+ Args:
|
|
|
+ data (dict): 包含字段数据的字典
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ WechatUser: 新创建的模型对象
|
|
|
+ """
|
|
|
+ # 处理日期时间字段
|
|
|
+ login_time = data.get('login_time')
|
|
|
+ if isinstance(login_time, str):
|
|
|
+ try:
|
|
|
+ login_time = datetime.fromisoformat(login_time.replace('Z', '+00:00'))
|
|
|
+ except ValueError:
|
|
|
+ login_time = None
|
|
|
+
|
|
|
+ created_at = data.get('created_at')
|
|
|
+ if isinstance(created_at, str):
|
|
|
+ try:
|
|
|
+ created_at = datetime.fromisoformat(created_at.replace('Z', '+00:00'))
|
|
|
+ except ValueError:
|
|
|
+ created_at = datetime.utcnow()
|
|
|
+ elif created_at is None:
|
|
|
+ created_at = datetime.utcnow()
|
|
|
+
|
|
|
+ updated_at = data.get('updated_at')
|
|
|
+ if isinstance(updated_at, str):
|
|
|
+ try:
|
|
|
+ updated_at = datetime.fromisoformat(updated_at.replace('Z', '+00:00'))
|
|
|
+ except ValueError:
|
|
|
+ updated_at = datetime.utcnow()
|
|
|
+ elif updated_at is None:
|
|
|
+ updated_at = datetime.utcnow()
|
|
|
+
|
|
|
+ return cls(
|
|
|
+ wechat_code=data.get('wechat_code'), # type: ignore
|
|
|
+ phone_number=data.get('phone_number'), # type: ignore
|
|
|
+ id_card_number=data.get('id_card_number'), # type: ignore
|
|
|
+ login_status=data.get('login_status', False), # type: ignore
|
|
|
+ login_time=login_time, # type: ignore
|
|
|
+ user_status=data.get('user_status', 'active'), # type: ignore
|
|
|
+ created_at=created_at, # type: ignore
|
|
|
+ updated_at=updated_at # type: ignore
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
class CalendarService:
|
|
|
"""
|
|
|
黄历信息服务类
|
|
@@ -500,6 +620,352 @@ class CalendarService:
|
|
|
return None
|
|
|
|
|
|
|
|
|
+class WechatUserService:
|
|
|
+ """
|
|
|
+ 微信用户信息服务类
|
|
|
+ 提供微信用户的注册、登录、状态管理等操作
|
|
|
+ """
|
|
|
+
|
|
|
+ def __init__(self, engine=None):
|
|
|
+ """
|
|
|
+ 初始化服务
|
|
|
+
|
|
|
+ Args:
|
|
|
+ engine: SQLAlchemy引擎对象,如果为None则使用Flask-SQLAlchemy的db.session
|
|
|
+ """
|
|
|
+ self.engine = engine
|
|
|
+
|
|
|
+ def create_user(self, user_data: dict) -> WechatUser:
|
|
|
+ """
|
|
|
+ 创建新的微信用户记录
|
|
|
+
|
|
|
+ Args:
|
|
|
+ user_data (dict): 用户信息数据
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ WechatUser: 创建的用户对象
|
|
|
+ """
|
|
|
+ user = WechatUser.from_dict(user_data)
|
|
|
+
|
|
|
+ if self.engine:
|
|
|
+ with Session(self.engine) as session:
|
|
|
+ session.add(user)
|
|
|
+ session.commit()
|
|
|
+ session.refresh(user)
|
|
|
+ else:
|
|
|
+ # 使用Flask-SQLAlchemy的db.session
|
|
|
+ db.session.add(user)
|
|
|
+ db.session.commit()
|
|
|
+ db.session.refresh(user)
|
|
|
+
|
|
|
+ return user
|
|
|
+
|
|
|
+ def get_user_by_wechat_code(self, wechat_code: str) -> Optional[WechatUser]:
|
|
|
+ """
|
|
|
+ 根据微信授权码查询用户
|
|
|
+
|
|
|
+ Args:
|
|
|
+ wechat_code (str): 微信授权码/openid
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ Optional[WechatUser]: 用户对象,如果不存在则返回None
|
|
|
+ """
|
|
|
+ if self.engine:
|
|
|
+ with Session(self.engine) as session:
|
|
|
+ return session.query(WechatUser).filter(
|
|
|
+ WechatUser.wechat_code == wechat_code
|
|
|
+ ).first()
|
|
|
+ else:
|
|
|
+ # 使用Flask-SQLAlchemy的db.session
|
|
|
+ return WechatUser.query.filter(
|
|
|
+ WechatUser.wechat_code == wechat_code
|
|
|
+ ).first()
|
|
|
+
|
|
|
+ def get_user_by_id(self, user_id: int) -> Optional[WechatUser]:
|
|
|
+ """
|
|
|
+ 根据ID查询用户
|
|
|
+
|
|
|
+ Args:
|
|
|
+ user_id (int): 用户ID
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ Optional[WechatUser]: 用户对象,如果不存在则返回None
|
|
|
+ """
|
|
|
+ if self.engine:
|
|
|
+ with Session(self.engine) as session:
|
|
|
+ return session.query(WechatUser).filter(
|
|
|
+ WechatUser.id == user_id
|
|
|
+ ).first()
|
|
|
+ else:
|
|
|
+ # 使用Flask-SQLAlchemy的db.session
|
|
|
+ return WechatUser.query.filter(
|
|
|
+ WechatUser.id == user_id
|
|
|
+ ).first()
|
|
|
+
|
|
|
+ def get_user_by_phone(self, phone_number: str) -> Optional[WechatUser]:
|
|
|
+ """
|
|
|
+ 根据手机号查询用户
|
|
|
+
|
|
|
+ Args:
|
|
|
+ phone_number (str): 手机号码
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ Optional[WechatUser]: 用户对象,如果不存在则返回None
|
|
|
+ """
|
|
|
+ if self.engine:
|
|
|
+ with Session(self.engine) as session:
|
|
|
+ return session.query(WechatUser).filter(
|
|
|
+ WechatUser.phone_number == phone_number
|
|
|
+ ).first()
|
|
|
+ else:
|
|
|
+ # 使用Flask-SQLAlchemy的db.session
|
|
|
+ return WechatUser.query.filter(
|
|
|
+ WechatUser.phone_number == phone_number
|
|
|
+ ).first()
|
|
|
+
|
|
|
+ def register_user(self, wechat_code: str, phone_number: Optional[str] = None, id_card_number: Optional[str] = None) -> WechatUser:
|
|
|
+ """
|
|
|
+ 注册新用户
|
|
|
+
|
|
|
+ Args:
|
|
|
+ wechat_code (str): 微信授权码/openid
|
|
|
+ phone_number (str, optional): 手机号码
|
|
|
+ id_card_number (str, optional): 身份证号码
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ WechatUser: 注册的用户对象
|
|
|
+
|
|
|
+ Raises:
|
|
|
+ ValueError: 如果用户已存在
|
|
|
+ """
|
|
|
+ # 检查用户是否已存在
|
|
|
+ existing_user = self.get_user_by_wechat_code(wechat_code)
|
|
|
+ if existing_user:
|
|
|
+ raise ValueError(f"用户已存在,微信授权码: {wechat_code}")
|
|
|
+
|
|
|
+ # 创建用户数据
|
|
|
+ user_data = {
|
|
|
+ 'wechat_code': wechat_code,
|
|
|
+ 'phone_number': phone_number,
|
|
|
+ 'id_card_number': id_card_number,
|
|
|
+ 'login_status': False,
|
|
|
+ 'user_status': 'active'
|
|
|
+ }
|
|
|
+
|
|
|
+ return self.create_user(user_data)
|
|
|
+
|
|
|
+ def login_user(self, wechat_code: str) -> Optional[WechatUser]:
|
|
|
+ """
|
|
|
+ 用户登录
|
|
|
+
|
|
|
+ Args:
|
|
|
+ wechat_code (str): 微信授权码/openid
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ Optional[WechatUser]: 登录成功返回用户对象,否则返回None
|
|
|
+ """
|
|
|
+ user = self.get_user_by_wechat_code(wechat_code)
|
|
|
+ if not user:
|
|
|
+ return None
|
|
|
+
|
|
|
+ # 检查用户状态
|
|
|
+ if user.user_status != 'active':
|
|
|
+ return None
|
|
|
+
|
|
|
+ # 更新登录状态和登录时间
|
|
|
+ update_data = {
|
|
|
+ 'login_status': True,
|
|
|
+ 'login_time': datetime.utcnow()
|
|
|
+ }
|
|
|
+
|
|
|
+ return self.update_user(user.id, update_data)
|
|
|
+
|
|
|
+ def logout_user(self, wechat_code: str) -> bool:
|
|
|
+ """
|
|
|
+ 用户登出
|
|
|
+
|
|
|
+ Args:
|
|
|
+ wechat_code (str): 微信授权码/openid
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ bool: 登出成功返回True,否则返回False
|
|
|
+ """
|
|
|
+ user = self.get_user_by_wechat_code(wechat_code)
|
|
|
+ if not user:
|
|
|
+ return False
|
|
|
+
|
|
|
+ # 更新登录状态
|
|
|
+ update_data = {
|
|
|
+ 'login_status': False
|
|
|
+ }
|
|
|
+
|
|
|
+ updated_user = self.update_user(user.id, update_data)
|
|
|
+ return updated_user is not None
|
|
|
+
|
|
|
+ def update_user(self, user_id: int, update_data: dict) -> Optional[WechatUser]:
|
|
|
+ """
|
|
|
+ 更新用户信息
|
|
|
+
|
|
|
+ Args:
|
|
|
+ user_id (int): 用户ID
|
|
|
+ update_data (dict): 要更新的数据
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ Optional[WechatUser]: 更新后的用户对象,如果不存在则返回None
|
|
|
+ """
|
|
|
+ if self.engine:
|
|
|
+ with Session(self.engine) as session:
|
|
|
+ user = session.query(WechatUser).filter(
|
|
|
+ WechatUser.id == user_id
|
|
|
+ ).first()
|
|
|
+
|
|
|
+ if not user:
|
|
|
+ return None
|
|
|
+
|
|
|
+ # 更新字段
|
|
|
+ for key, value in update_data.items():
|
|
|
+ if hasattr(user, key):
|
|
|
+ if key in ['login_time', 'created_at', 'updated_at'] and isinstance(value, str):
|
|
|
+ try:
|
|
|
+ value = datetime.fromisoformat(value.replace('Z', '+00:00'))
|
|
|
+ except ValueError:
|
|
|
+ continue
|
|
|
+ setattr(user, key, value)
|
|
|
+
|
|
|
+ session.commit()
|
|
|
+ session.refresh(user)
|
|
|
+
|
|
|
+ else:
|
|
|
+ # 使用Flask-SQLAlchemy的db.session
|
|
|
+ user = WechatUser.query.filter(
|
|
|
+ WechatUser.id == user_id
|
|
|
+ ).first()
|
|
|
+
|
|
|
+ if not user:
|
|
|
+ return None
|
|
|
+
|
|
|
+ # 更新字段
|
|
|
+ for key, value in update_data.items():
|
|
|
+ if hasattr(user, key):
|
|
|
+ if key in ['login_time', 'created_at', 'updated_at'] and isinstance(value, str):
|
|
|
+ try:
|
|
|
+ value = datetime.fromisoformat(value.replace('Z', '+00:00'))
|
|
|
+ except ValueError:
|
|
|
+ continue
|
|
|
+ setattr(user, key, value)
|
|
|
+
|
|
|
+ db.session.commit()
|
|
|
+ db.session.refresh(user)
|
|
|
+
|
|
|
+ return user
|
|
|
+
|
|
|
+ def deactivate_user(self, user_id: int) -> bool:
|
|
|
+ """
|
|
|
+ 停用用户账户
|
|
|
+
|
|
|
+ Args:
|
|
|
+ user_id (int): 用户ID
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ bool: 停用成功返回True,否则返回False
|
|
|
+ """
|
|
|
+ update_data = {
|
|
|
+ 'user_status': 'inactive',
|
|
|
+ 'login_status': False
|
|
|
+ }
|
|
|
+
|
|
|
+ updated_user = self.update_user(user_id, update_data)
|
|
|
+ return updated_user is not None
|
|
|
+
|
|
|
+ def activate_user(self, user_id: int) -> bool:
|
|
|
+ """
|
|
|
+ 激活用户账户
|
|
|
+
|
|
|
+ Args:
|
|
|
+ user_id (int): 用户ID
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ bool: 激活成功返回True,否则返回False
|
|
|
+ """
|
|
|
+ update_data = {
|
|
|
+ 'user_status': 'active'
|
|
|
+ }
|
|
|
+
|
|
|
+ updated_user = self.update_user(user_id, update_data)
|
|
|
+ return updated_user is not None
|
|
|
+
|
|
|
+ def delete_user(self, user_id: int) -> bool:
|
|
|
+ """
|
|
|
+ 删除用户(软删除,将状态设为deleted)
|
|
|
+
|
|
|
+ Args:
|
|
|
+ user_id (int): 用户ID
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ bool: 删除成功返回True,否则返回False
|
|
|
+ """
|
|
|
+ update_data = {
|
|
|
+ 'user_status': 'deleted',
|
|
|
+ 'login_status': False
|
|
|
+ }
|
|
|
+
|
|
|
+ updated_user = self.update_user(user_id, update_data)
|
|
|
+ return updated_user is not None
|
|
|
+
|
|
|
+ def get_active_users(self, limit: int = 100, offset: int = 0) -> list[WechatUser]:
|
|
|
+ """
|
|
|
+ 获取活跃用户列表
|
|
|
+
|
|
|
+ Args:
|
|
|
+ limit (int): 限制返回数量,默认100
|
|
|
+ offset (int): 偏移量,默认0
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ list[WechatUser]: 活跃用户对象列表
|
|
|
+ """
|
|
|
+ if self.engine:
|
|
|
+ with Session(self.engine) as session:
|
|
|
+ return session.query(WechatUser).filter(
|
|
|
+ WechatUser.user_status == 'active'
|
|
|
+ ).order_by(
|
|
|
+ WechatUser.created_at.desc()
|
|
|
+ ).offset(offset).limit(limit).all()
|
|
|
+ else:
|
|
|
+ # 使用Flask-SQLAlchemy的db.session
|
|
|
+ return WechatUser.query.filter(
|
|
|
+ WechatUser.user_status == 'active'
|
|
|
+ ).order_by(
|
|
|
+ WechatUser.created_at.desc()
|
|
|
+ ).offset(offset).limit(limit).all()
|
|
|
+
|
|
|
+ def get_logged_in_users(self, limit: int = 100) -> list[WechatUser]:
|
|
|
+ """
|
|
|
+ 获取当前已登录的用户列表
|
|
|
+
|
|
|
+ Args:
|
|
|
+ limit (int): 限制返回数量,默认100
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ list[WechatUser]: 已登录用户对象列表
|
|
|
+ """
|
|
|
+ if self.engine:
|
|
|
+ with Session(self.engine) as session:
|
|
|
+ return session.query(WechatUser).filter(
|
|
|
+ WechatUser.login_status == True,
|
|
|
+ WechatUser.user_status == 'active'
|
|
|
+ ).order_by(
|
|
|
+ WechatUser.login_time.desc()
|
|
|
+ ).limit(limit).all()
|
|
|
+ else:
|
|
|
+ # 使用Flask-SQLAlchemy的db.session
|
|
|
+ return WechatUser.query.filter(
|
|
|
+ WechatUser.login_status == True,
|
|
|
+ WechatUser.user_status == 'active'
|
|
|
+ ).order_by(
|
|
|
+ WechatUser.login_time.desc()
|
|
|
+ ).limit(limit).all()
|
|
|
+
|
|
|
+
|
|
|
# 便捷函数
|
|
|
def create_calendar_info(calendar_data: dict, engine=None) -> CalendarInfo:
|
|
|
"""
|
|
@@ -644,11 +1110,342 @@ def get_calendar_by_id(calendar_id: int, engine=None) -> Optional[CalendarInfo]:
|
|
|
return service.get_calendar_by_id(calendar_id)
|
|
|
|
|
|
|
|
|
+# 微信用户相关便捷函数
|
|
|
+def register_wechat_user(wechat_code: str, phone_number: Optional[str] = None, id_card_number: Optional[str] = None, engine=None) -> dict:
|
|
|
+ """
|
|
|
+ 注册微信用户的便捷函数
|
|
|
+
|
|
|
+ Args:
|
|
|
+ wechat_code (str): 微信授权码/openid
|
|
|
+ phone_number (str, optional): 手机号码
|
|
|
+ id_card_number (str, optional): 身份证号码
|
|
|
+ engine: SQLAlchemy引擎对象
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ dict: 包含注册结果的JSON格式数据
|
|
|
+ """
|
|
|
+ try:
|
|
|
+ # 验证必填参数
|
|
|
+ if not wechat_code or not isinstance(wechat_code, str):
|
|
|
+ return {
|
|
|
+ "reason": "failed",
|
|
|
+ "return_code": 400,
|
|
|
+ "result": None,
|
|
|
+ "error": "微信授权码不能为空"
|
|
|
+ }
|
|
|
+
|
|
|
+ # 创建服务实例
|
|
|
+ service = WechatUserService(engine)
|
|
|
+
|
|
|
+ # 尝试注册用户
|
|
|
+ user = service.register_user(wechat_code, phone_number, id_card_number)
|
|
|
+
|
|
|
+ # 注册成功
|
|
|
+ return {
|
|
|
+ "reason": "successed",
|
|
|
+ "return_code": 201,
|
|
|
+ "result": {
|
|
|
+ "id": str(user.id),
|
|
|
+ "wechat_code": user.wechat_code,
|
|
|
+ "phone_number": user.phone_number,
|
|
|
+ "id_card_number": user.id_card_number,
|
|
|
+ "login_status": user.login_status,
|
|
|
+ "user_status": user.user_status,
|
|
|
+ "created_at": user.created_at.isoformat() if user.created_at is not None else None
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ except ValueError as e:
|
|
|
+ # 用户已存在等业务逻辑错误
|
|
|
+ return {
|
|
|
+ "reason": "failed",
|
|
|
+ "return_code": 409,
|
|
|
+ "result": None,
|
|
|
+ "error": str(e)
|
|
|
+ }
|
|
|
+ except Exception as e:
|
|
|
+ # 其他系统错误
|
|
|
+ return {
|
|
|
+ "reason": "failed",
|
|
|
+ "return_code": 500,
|
|
|
+ "result": None,
|
|
|
+ "error": f"注册过程中发生错误: {str(e)}"
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+def login_wechat_user(wechat_code: str, engine=None) -> dict:
|
|
|
+ """
|
|
|
+ 微信用户登录的便捷函数
|
|
|
+
|
|
|
+ Args:
|
|
|
+ wechat_code (str): 微信授权码/openid
|
|
|
+ engine: SQLAlchemy引擎对象
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ dict: 包含登录结果的JSON格式数据
|
|
|
+ """
|
|
|
+ try:
|
|
|
+ # 验证必填参数
|
|
|
+ if not wechat_code or not isinstance(wechat_code, str):
|
|
|
+ return {
|
|
|
+ "reason": "failed",
|
|
|
+ "return_code": 400,
|
|
|
+ "result": None,
|
|
|
+ "error": "微信授权码不能为空"
|
|
|
+ }
|
|
|
+
|
|
|
+ # 创建服务实例
|
|
|
+ service = WechatUserService(engine)
|
|
|
+
|
|
|
+ # 尝试登录
|
|
|
+ user = service.login_user(wechat_code)
|
|
|
+
|
|
|
+ if user:
|
|
|
+ # 登录成功
|
|
|
+ return {
|
|
|
+ "reason": "successed",
|
|
|
+ "return_code": 200,
|
|
|
+ "result": {
|
|
|
+ "id": str(user.id),
|
|
|
+ "wechat_code": user.wechat_code,
|
|
|
+ "phone_number": user.phone_number,
|
|
|
+ "id_card_number": user.id_card_number,
|
|
|
+ "login_status": user.login_status,
|
|
|
+ "login_time": user.login_time.isoformat() if user.login_time is not None else None,
|
|
|
+ "user_status": user.user_status
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else:
|
|
|
+ # 登录失败
|
|
|
+ return {
|
|
|
+ "reason": "failed",
|
|
|
+ "return_code": 401,
|
|
|
+ "result": None,
|
|
|
+ "error": "用户不存在或账户状态异常"
|
|
|
+ }
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ # 系统错误
|
|
|
+ return {
|
|
|
+ "reason": "failed",
|
|
|
+ "return_code": 500,
|
|
|
+ "result": None,
|
|
|
+ "error": f"登录过程中发生错误: {str(e)}"
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+def logout_wechat_user(wechat_code: str, engine=None) -> dict:
|
|
|
+ """
|
|
|
+ 微信用户登出的便捷函数
|
|
|
+
|
|
|
+ Args:
|
|
|
+ wechat_code (str): 微信授权码/openid
|
|
|
+ engine: SQLAlchemy引擎对象
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ dict: 包含登出结果的JSON格式数据
|
|
|
+ """
|
|
|
+ try:
|
|
|
+ # 验证必填参数
|
|
|
+ if not wechat_code or not isinstance(wechat_code, str):
|
|
|
+ return {
|
|
|
+ "reason": "failed",
|
|
|
+ "return_code": 400,
|
|
|
+ "result": None,
|
|
|
+ "error": "微信授权码不能为空"
|
|
|
+ }
|
|
|
+
|
|
|
+ # 创建服务实例
|
|
|
+ service = WechatUserService(engine)
|
|
|
+
|
|
|
+ # 尝试登出
|
|
|
+ success = service.logout_user(wechat_code)
|
|
|
+
|
|
|
+ if success:
|
|
|
+ # 登出成功
|
|
|
+ return {
|
|
|
+ "reason": "successed",
|
|
|
+ "return_code": 200,
|
|
|
+ "result": {
|
|
|
+ "message": "用户已成功登出"
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else:
|
|
|
+ # 登出失败
|
|
|
+ return {
|
|
|
+ "reason": "failed",
|
|
|
+ "return_code": 404,
|
|
|
+ "result": None,
|
|
|
+ "error": "用户不存在"
|
|
|
+ }
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ # 系统错误
|
|
|
+ return {
|
|
|
+ "reason": "failed",
|
|
|
+ "return_code": 500,
|
|
|
+ "result": None,
|
|
|
+ "error": f"登出过程中发生错误: {str(e)}"
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+def get_wechat_user_info(wechat_code: str, engine=None) -> dict:
|
|
|
+ """
|
|
|
+ 获取微信用户信息的便捷函数
|
|
|
+
|
|
|
+ Args:
|
|
|
+ wechat_code (str): 微信授权码/openid
|
|
|
+ engine: SQLAlchemy引擎对象
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ dict: 包含用户信息的JSON格式数据
|
|
|
+ """
|
|
|
+ try:
|
|
|
+ # 验证必填参数
|
|
|
+ if not wechat_code or not isinstance(wechat_code, str):
|
|
|
+ return {
|
|
|
+ "reason": "failed",
|
|
|
+ "return_code": 400,
|
|
|
+ "result": None,
|
|
|
+ "error": "微信授权码不能为空"
|
|
|
+ }
|
|
|
+
|
|
|
+ # 创建服务实例
|
|
|
+ service = WechatUserService(engine)
|
|
|
+
|
|
|
+ # 查询用户信息
|
|
|
+ user = service.get_user_by_wechat_code(wechat_code)
|
|
|
+
|
|
|
+ if user:
|
|
|
+ # 查询成功
|
|
|
+ return {
|
|
|
+ "reason": "successed",
|
|
|
+ "return_code": 200,
|
|
|
+ "result": {
|
|
|
+ "id": str(user.id),
|
|
|
+ "wechat_code": user.wechat_code,
|
|
|
+ "phone_number": user.phone_number,
|
|
|
+ "id_card_number": user.id_card_number,
|
|
|
+ "login_status": user.login_status,
|
|
|
+ "login_time": user.login_time.isoformat() if user.login_time is not None else None,
|
|
|
+ "user_status": user.user_status,
|
|
|
+ "created_at": user.created_at.isoformat() if user.created_at is not None else None,
|
|
|
+ "updated_at": user.updated_at.isoformat() if user.updated_at is not None else None
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else:
|
|
|
+ # 用户不存在
|
|
|
+ return {
|
|
|
+ "reason": "failed",
|
|
|
+ "return_code": 404,
|
|
|
+ "result": None,
|
|
|
+ "error": f"未找到微信授权码为 {wechat_code} 的用户"
|
|
|
+ }
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ # 系统错误
|
|
|
+ return {
|
|
|
+ "reason": "failed",
|
|
|
+ "return_code": 500,
|
|
|
+ "result": None,
|
|
|
+ "error": f"查询过程中发生错误: {str(e)}"
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+def update_wechat_user_info(wechat_code: str, update_data: dict, engine=None) -> dict:
|
|
|
+ """
|
|
|
+ 更新微信用户信息的便捷函数
|
|
|
+
|
|
|
+ Args:
|
|
|
+ wechat_code (str): 微信授权码/openid
|
|
|
+ update_data (dict): 要更新的数据
|
|
|
+ engine: SQLAlchemy引擎对象
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ dict: 包含更新结果的JSON格式数据
|
|
|
+ """
|
|
|
+ try:
|
|
|
+ # 验证必填参数
|
|
|
+ if not wechat_code or not isinstance(wechat_code, str):
|
|
|
+ return {
|
|
|
+ "reason": "failed",
|
|
|
+ "return_code": 400,
|
|
|
+ "result": None,
|
|
|
+ "error": "微信授权码不能为空"
|
|
|
+ }
|
|
|
+
|
|
|
+ if not update_data or not isinstance(update_data, dict):
|
|
|
+ return {
|
|
|
+ "reason": "failed",
|
|
|
+ "return_code": 400,
|
|
|
+ "result": None,
|
|
|
+ "error": "更新数据不能为空"
|
|
|
+ }
|
|
|
+
|
|
|
+ # 创建服务实例
|
|
|
+ service = WechatUserService(engine)
|
|
|
+
|
|
|
+ # 先查找用户
|
|
|
+ user = service.get_user_by_wechat_code(wechat_code)
|
|
|
+ if not user:
|
|
|
+ return {
|
|
|
+ "reason": "failed",
|
|
|
+ "return_code": 404,
|
|
|
+ "result": None,
|
|
|
+ "error": f"未找到微信授权码为 {wechat_code} 的用户"
|
|
|
+ }
|
|
|
+
|
|
|
+ # 更新用户信息
|
|
|
+ updated_user = service.update_user(user.id, update_data)
|
|
|
+
|
|
|
+ if updated_user:
|
|
|
+ # 更新成功
|
|
|
+ return {
|
|
|
+ "reason": "successed",
|
|
|
+ "return_code": 200,
|
|
|
+ "result": {
|
|
|
+ "id": str(updated_user.id),
|
|
|
+ "wechat_code": updated_user.wechat_code,
|
|
|
+ "phone_number": updated_user.phone_number,
|
|
|
+ "id_card_number": updated_user.id_card_number,
|
|
|
+ "login_status": updated_user.login_status,
|
|
|
+ "login_time": updated_user.login_time.isoformat() if updated_user.login_time is not None else None,
|
|
|
+ "user_status": updated_user.user_status,
|
|
|
+ "created_at": updated_user.created_at.isoformat() if updated_user.created_at is not None else None,
|
|
|
+ "updated_at": updated_user.updated_at.isoformat() if updated_user.updated_at is not None else None
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else:
|
|
|
+ # 更新失败
|
|
|
+ return {
|
|
|
+ "reason": "failed",
|
|
|
+ "return_code": 500,
|
|
|
+ "result": None,
|
|
|
+ "error": "更新用户信息失败"
|
|
|
+ }
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ # 系统错误
|
|
|
+ return {
|
|
|
+ "reason": "failed",
|
|
|
+ "return_code": 500,
|
|
|
+ "result": None,
|
|
|
+ "error": f"更新过程中发生错误: {str(e)}"
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
# 导出主要类和函数
|
|
|
__all__ = [
|
|
|
'CalendarInfo',
|
|
|
+ 'WechatUser',
|
|
|
'CalendarService',
|
|
|
+ 'WechatUserService',
|
|
|
'create_calendar_info',
|
|
|
'get_calendar_by_date',
|
|
|
- 'get_calendar_by_id'
|
|
|
+ 'get_calendar_by_id',
|
|
|
+ 'register_wechat_user',
|
|
|
+ 'login_wechat_user',
|
|
|
+ 'logout_wechat_user',
|
|
|
+ 'get_wechat_user_info',
|
|
|
+ 'update_wechat_user_info'
|
|
|
]
|