123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- from werkzeug.security import generate_password_hash
- from hub import methods, Global
- async def code1001(**sources):
- """
- 新增用户
- """
- # --- check ---
- if not sources.get('username'):
- return dict(code=1, details=f"something is wrong.")
- # --- check ---
- if sources.get('password'):
- sources['password'] = generate_password_hash(sources.get('password'))
- else:
- sources['password'] = generate_password_hash('baosteel@2024')
- # --- save ---
- """
- UserInfo: 用户信息表
- UserInfo.username: 登录账号
- UserInfo.password: 登录密码 (默认值:baosteel@2024)
- UserInfo.role_type: 角色类型 (1: 超级管理员 2: 普通管理员 3: 普通用户) (默认值:3)
- UserInfo.create_at: 创建时间
- UserInfo.update_at: 修改时间
- UserInfo.name: 姓名
- UserInfo.cid: 身份证号
- UserInfo.phone: 手机号
- UserInfo.sex: 性别 (0:未设置 1:男 2:女)
- UserInfo.department: 部门
- UserInfo.wid: 工号
- UserInfo.allow_at: 注册审批时间
- UserInfo.state: 禁用状态 (0:激活 1:禁用)
- """
- data = {
- 'username': sources.get('username'),
- 'password': sources.get('password'),
- 'role_type': str(sources.get('role_type', 3)),
- 'name': sources.get('name'),
- 'cid': sources.get('cid'),
- 'phone': sources.get('phone'),
- 'sex': str(sources.get('sex')),
- 'department': sources.get('department'),
- 'wid': sources.get('wid'),
- 'state': '0',
- 'allow_at': methods.now_ts(),
- }
- uuid = Global.mdb.add('UserInfo', data)
- return dict(code=0, data=uuid)
- async def code1002(**sources):
- """
- 查询用户列表
- """
- # --- check ---
- if not sources.get('page'):
- return dict(code=1, details=f"something is wrong.")
- elif not sources.get('size'):
- return dict(code=2, details=f"something is wrong.")
- # --- debug ---
- # methods.debug_log('code1000.code1002.82:', f"#role_type: {sources.get('role_type')}")
- # --- fill d1 ---
- d1 = list()
- page = sources.get('page', 1)
- size = sources.get('size', 10)
- for item in Global.mdb.get_all('UserInfo', sort_field=[('allow_at', -1)]):
- # --- check ---
- if sources.get('name') and sources.get('name') not in str(item.get('name')):
- continue
- # --- check ---
- if sources.get('phone') and str(sources.get('phone')) != str(item.get('phone')):
- continue
- # --- check ---
- if not item.get('role_type'):
- item['role_type'] = '3'
- # --- check ---
- if sources.get('role_type') and str(item.get('role_type')) not in sources.get('role_type'):
- continue
- # --- check ---
- item['role_type'] = str(item.get('role_type'))
- item['sex'] = str(item.get('sex'))
- item['state'] = str(item.get('state'))
- item['uuid'] = str(item.get('_id'))
- item.pop('_id')
- item.pop('password')
- d1.append(item)
- return dict(code=0, data=d1[(page - 1) * size: page * size], total=len(d1), page=page, size=size)
- async def code1003(**sources):
- """
- 删除指定用户
- """
- # --- check ---
- uuid = sources.get('uuid')
- if not uuid:
- return dict(code=1, details=f"something is wrong.")
- # --- check ---
- item = Global.mdb.get_one_by_id('UserInfo', uuid)
- if not item:
- return dict(code=2, details=f"something is wrong.")
- # --- remove ---
- Global.mdb.remove_one_by_id('UserInfo', uuid)
- return dict(code=0, data=uuid)
- async def code1004(**sources):
- """
- 禁用指定用户
- """
- # --- check ---
- uuid = sources.get('uuid')
- if not uuid:
- return dict(code=1, details=f"something is wrong.")
- # --- check ---
- item = Global.mdb.get_one_by_id('UserInfo', uuid)
- if not item:
- return dict(code=2, details=f"something is wrong.")
- # --- update ---
- """
- UserInfo: 用户信息表
- UserInfo.username: 登录账号
- UserInfo.password: 登录密码 (默认值:baosteel@2024)
- UserInfo.role_type: 角色类型 (1: 超级管理员 2: 普通管理员 3: 普通用户) (默认值:3)
- UserInfo.create_at: 创建时间
- UserInfo.update_at: 修改时间
- UserInfo.name: 姓名
- UserInfo.cid: 身份证号
- UserInfo.phone: 手机号
- UserInfo.sex: 性别 (0:未设置 1:男 2:女)
- UserInfo.department: 部门
- UserInfo.wid: 工号
- UserInfo.allow_at: 注册审批时间
- UserInfo.state: 禁用状态 (0:激活 1:禁用)
- """
- update_dict = dict()
- update_dict['state'] = 1
- item = Global.mdb.update_one_by_id('UserInfo', uuid, update_dict, need_back=True)
- return dict(code=0, data=item)
- async def code1005(**sources):
- """
- 启用指定用户
- """
- # --- check ---
- uuid = sources.get('uuid')
- if not uuid:
- return dict(code=1, details=f"something is wrong.")
- # --- check ---
- item = Global.mdb.get_one_by_id('UserInfo', uuid)
- if not item:
- return dict(code=2, details=f"something is wrong.")
- # --- update ---
- """
- UserInfo: 用户信息表
- UserInfo.username: 登录账号
- UserInfo.password: 登录密码 (默认值:baosteel@2024)
- UserInfo.role_type: 角色类型 (1: 超级管理员 2: 普通管理员 3: 普通用户) (默认值:3)
- UserInfo.create_at: 创建时间
- UserInfo.update_at: 修改时间
- UserInfo.name: 姓名
- UserInfo.cid: 身份证号
- UserInfo.phone: 手机号
- UserInfo.sex: 性别 (0:未设置 1:男 2:女)
- UserInfo.department: 部门
- UserInfo.wid: 工号
- UserInfo.allow_at: 注册审批时间
- UserInfo.state: 禁用状态 (0:激活 1:禁用)
- """
- update_dict = dict()
- update_dict['state'] = 0
- item = Global.mdb.update_one_by_id('UserInfo', uuid, update_dict, need_back=True)
- return dict(code=0, data=item)
- async def code1006(**sources):
- """
- 获取指定用户详情
- """
- uuid = sources.get('uuid')
- if not uuid:
- return dict(code=1, details=f"something is wrong.")
- item = Global.mdb.get_one_by_id('UserInfo', uuid)
- if not item:
- return dict(code=2, details=f"something is wrong.")
- # --- fill ---
- """
- UserInfo: 用户信息表
- UserInfo.username: 登录账号
- UserInfo.password: 登录密码 (默认值:baosteel@2024)
- UserInfo.role_type: 角色类型 (1: 超级管理员 2: 普通管理员 3: 普通用户) (默认值:3)
- UserInfo.create_at: 创建时间
- UserInfo.update_at: 修改时间
- UserInfo.name: 姓名
- UserInfo.cid: 身份证号
- UserInfo.phone: 手机号
- UserInfo.sex: 性别 (0:未设置 1:男 2:女)
- UserInfo.department: 部门
- UserInfo.wid: 工号
- UserInfo.allow_at: 注册审批时间
- UserInfo.state: 禁用状态 (0:激活 1:禁用)
- """
- item.pop('_id')
- item['uuid'] = uuid
- # --- check ---
- if not item.get('role_type'):
- item['role_type'] = '3'
- # --- check ---
- item['role_type'] = str(item.get('role_type'))
- item['sex'] = str(item.get('sex'))
- item['state'] = str(item.get('state'))
- return dict(code=0, data=item)
- async def code1007(**sources):
- """
- 修改指定用户信息
- """
- # --- check ---
- uuid = sources.get('uuid')
- if not uuid:
- return dict(code=1, details=f"something is wrong.")
- # --- check again ---
- item = Global.mdb.get_one_by_id('UserInfo', uuid)
- if not item:
- return dict(code=2, details=f"something is wrong.")
- # --- update UserCamera by camera_uuid ---
- """
- UserInfo: 用户信息表
- UserInfo.username: 登录账号
- UserInfo.password: 登录密码 (默认值:baosteel@2024)
- UserInfo.role_type: 角色类型 (1: 超级管理员 2: 普通管理员 3: 普通用户) (默认值:3)
- UserInfo.create_at: 创建时间
- UserInfo.update_at: 修改时间
- UserInfo.name: 姓名
- UserInfo.cid: 身份证号
- UserInfo.phone: 手机号
- UserInfo.sex: 性别 (0:未设置 1:男 2:女)
- UserInfo.department: 部门
- UserInfo.wid: 工号
- UserInfo.allow_at: 注册审批时间
- UserInfo.state: 禁用状态 (0:激活 1:禁用)
- """
- update_dict = dict()
- update_dict['update_at'] = methods.now_ts()
- if sources.get('username'):
- update_dict['username'] = sources.get('username')
- if sources.get('password'):
- update_dict['password'] = generate_password_hash(sources.get('password'))
- if sources.get('role_type'):
- update_dict['role_type'] = str(sources.get('role_type'))
- if sources.get('name'):
- update_dict['name'] = sources.get('name')
- if sources.get('cid'):
- update_dict['cid'] = sources.get('cid')
- if sources.get('phone'):
- update_dict['phone'] = sources.get('phone')
- if sources.get('sex'):
- update_dict['sex'] = str(sources.get('sex'))
- if sources.get('department'):
- update_dict['department'] = sources.get('department')
- if sources.get('wid'):
- update_dict['wid'] = sources.get('wid')
- # --- check ---
- if type(item.get('role_type')) == int:
- update_dict['role_type'] = str(item.get('role_type'))
- if type(item.get('sex')) == int:
- update_dict['sex'] = str(item.get('sex'))
- if type(item.get('state')) == int:
- update_dict['state'] = str(item.get('state'))
- item = Global.mdb.update_one_by_id('UserInfo', uuid, update_dict, need_back=True)
- return dict(code=0, data=item)
|