code1000.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. from werkzeug.security import generate_password_hash
  2. from hub import methods, Global
  3. async def code1001(**sources):
  4. """
  5. 新增用户
  6. """
  7. # --- check ---
  8. if not sources.get('username'):
  9. return dict(code=1, details=f"something is wrong.")
  10. # --- check ---
  11. if sources.get('password'):
  12. sources['password'] = generate_password_hash(sources.get('password'))
  13. else:
  14. sources['password'] = generate_password_hash('baosteel@2024')
  15. # --- save ---
  16. """
  17. UserInfo: 用户信息表
  18. UserInfo.username: 登录账号
  19. UserInfo.password: 登录密码 (默认值:baosteel@2024)
  20. UserInfo.role_type: 角色类型 (1: 超级管理员 2: 普通管理员 3: 普通用户) (默认值:3)
  21. UserInfo.create_at: 创建时间
  22. UserInfo.update_at: 修改时间
  23. UserInfo.name: 姓名
  24. UserInfo.cid: 身份证号
  25. UserInfo.phone: 手机号
  26. UserInfo.sex: 性别 (0:未设置 1:男 2:女)
  27. UserInfo.department: 部门
  28. UserInfo.wid: 工号
  29. UserInfo.allow_at: 注册审批时间
  30. UserInfo.state: 禁用状态 (0:激活 1:禁用)
  31. """
  32. data = {
  33. 'username': sources.get('username'),
  34. 'password': sources.get('password'),
  35. 'role_type': str(sources.get('role_type', 3)),
  36. 'name': sources.get('name'),
  37. 'cid': sources.get('cid'),
  38. 'phone': sources.get('phone'),
  39. 'sex': str(sources.get('sex')),
  40. 'department': sources.get('department'),
  41. 'wid': sources.get('wid'),
  42. 'state': '0',
  43. 'allow_at': methods.now_ts(),
  44. }
  45. uuid = Global.mdb.add('UserInfo', data)
  46. return dict(code=0, data=uuid)
  47. async def code1002(**sources):
  48. """
  49. 查询用户列表
  50. """
  51. # --- check ---
  52. if not sources.get('page'):
  53. return dict(code=1, details=f"something is wrong.")
  54. elif not sources.get('size'):
  55. return dict(code=2, details=f"something is wrong.")
  56. # --- debug ---
  57. # methods.debug_log('code1000.code1002.82:', f"#role_type: {sources.get('role_type')}")
  58. # --- fill d1 ---
  59. d1 = list()
  60. page = sources.get('page', 1)
  61. size = sources.get('size', 10)
  62. for item in Global.mdb.get_all('UserInfo', sort_field=[('allow_at', -1)]):
  63. # --- check ---
  64. if sources.get('name') and sources.get('name') not in str(item.get('name')):
  65. continue
  66. # --- check ---
  67. if sources.get('phone') and str(sources.get('phone')) != str(item.get('phone')):
  68. continue
  69. # --- check ---
  70. if not item.get('role_type'):
  71. item['role_type'] = '3'
  72. # --- check ---
  73. if sources.get('role_type') and str(item.get('role_type')) not in sources.get('role_type'):
  74. continue
  75. # --- check ---
  76. item['role_type'] = str(item.get('role_type'))
  77. item['sex'] = str(item.get('sex'))
  78. item['state'] = str(item.get('state'))
  79. item['uuid'] = str(item.get('_id'))
  80. item.pop('_id')
  81. item.pop('password')
  82. d1.append(item)
  83. return dict(code=0, data=d1[(page - 1) * size: page * size], total=len(d1), page=page, size=size)
  84. async def code1003(**sources):
  85. """
  86. 删除指定用户
  87. """
  88. # --- check ---
  89. uuid = sources.get('uuid')
  90. if not uuid:
  91. return dict(code=1, details=f"something is wrong.")
  92. # --- check ---
  93. item = Global.mdb.get_one_by_id('UserInfo', uuid)
  94. if not item:
  95. return dict(code=2, details=f"something is wrong.")
  96. # --- remove ---
  97. Global.mdb.remove_one_by_id('UserInfo', uuid)
  98. return dict(code=0, data=uuid)
  99. async def code1004(**sources):
  100. """
  101. 禁用指定用户
  102. """
  103. # --- check ---
  104. uuid = sources.get('uuid')
  105. if not uuid:
  106. return dict(code=1, details=f"something is wrong.")
  107. # --- check ---
  108. item = Global.mdb.get_one_by_id('UserInfo', uuid)
  109. if not item:
  110. return dict(code=2, details=f"something is wrong.")
  111. # --- update ---
  112. """
  113. UserInfo: 用户信息表
  114. UserInfo.username: 登录账号
  115. UserInfo.password: 登录密码 (默认值:baosteel@2024)
  116. UserInfo.role_type: 角色类型 (1: 超级管理员 2: 普通管理员 3: 普通用户) (默认值:3)
  117. UserInfo.create_at: 创建时间
  118. UserInfo.update_at: 修改时间
  119. UserInfo.name: 姓名
  120. UserInfo.cid: 身份证号
  121. UserInfo.phone: 手机号
  122. UserInfo.sex: 性别 (0:未设置 1:男 2:女)
  123. UserInfo.department: 部门
  124. UserInfo.wid: 工号
  125. UserInfo.allow_at: 注册审批时间
  126. UserInfo.state: 禁用状态 (0:激活 1:禁用)
  127. """
  128. update_dict = dict()
  129. update_dict['state'] = 1
  130. item = Global.mdb.update_one_by_id('UserInfo', uuid, update_dict, need_back=True)
  131. return dict(code=0, data=item)
  132. async def code1005(**sources):
  133. """
  134. 启用指定用户
  135. """
  136. # --- check ---
  137. uuid = sources.get('uuid')
  138. if not uuid:
  139. return dict(code=1, details=f"something is wrong.")
  140. # --- check ---
  141. item = Global.mdb.get_one_by_id('UserInfo', uuid)
  142. if not item:
  143. return dict(code=2, details=f"something is wrong.")
  144. # --- update ---
  145. """
  146. UserInfo: 用户信息表
  147. UserInfo.username: 登录账号
  148. UserInfo.password: 登录密码 (默认值:baosteel@2024)
  149. UserInfo.role_type: 角色类型 (1: 超级管理员 2: 普通管理员 3: 普通用户) (默认值:3)
  150. UserInfo.create_at: 创建时间
  151. UserInfo.update_at: 修改时间
  152. UserInfo.name: 姓名
  153. UserInfo.cid: 身份证号
  154. UserInfo.phone: 手机号
  155. UserInfo.sex: 性别 (0:未设置 1:男 2:女)
  156. UserInfo.department: 部门
  157. UserInfo.wid: 工号
  158. UserInfo.allow_at: 注册审批时间
  159. UserInfo.state: 禁用状态 (0:激活 1:禁用)
  160. """
  161. update_dict = dict()
  162. update_dict['state'] = 0
  163. item = Global.mdb.update_one_by_id('UserInfo', uuid, update_dict, need_back=True)
  164. return dict(code=0, data=item)
  165. async def code1006(**sources):
  166. """
  167. 获取指定用户详情
  168. """
  169. uuid = sources.get('uuid')
  170. if not uuid:
  171. return dict(code=1, details=f"something is wrong.")
  172. item = Global.mdb.get_one_by_id('UserInfo', uuid)
  173. if not item:
  174. return dict(code=2, details=f"something is wrong.")
  175. # --- fill ---
  176. """
  177. UserInfo: 用户信息表
  178. UserInfo.username: 登录账号
  179. UserInfo.password: 登录密码 (默认值:baosteel@2024)
  180. UserInfo.role_type: 角色类型 (1: 超级管理员 2: 普通管理员 3: 普通用户) (默认值:3)
  181. UserInfo.create_at: 创建时间
  182. UserInfo.update_at: 修改时间
  183. UserInfo.name: 姓名
  184. UserInfo.cid: 身份证号
  185. UserInfo.phone: 手机号
  186. UserInfo.sex: 性别 (0:未设置 1:男 2:女)
  187. UserInfo.department: 部门
  188. UserInfo.wid: 工号
  189. UserInfo.allow_at: 注册审批时间
  190. UserInfo.state: 禁用状态 (0:激活 1:禁用)
  191. """
  192. item.pop('_id')
  193. item['uuid'] = uuid
  194. # --- check ---
  195. if not item.get('role_type'):
  196. item['role_type'] = '3'
  197. # --- check ---
  198. item['role_type'] = str(item.get('role_type'))
  199. item['sex'] = str(item.get('sex'))
  200. item['state'] = str(item.get('state'))
  201. return dict(code=0, data=item)
  202. async def code1007(**sources):
  203. """
  204. 修改指定用户信息
  205. """
  206. # --- check ---
  207. uuid = sources.get('uuid')
  208. if not uuid:
  209. return dict(code=1, details=f"something is wrong.")
  210. # --- check again ---
  211. item = Global.mdb.get_one_by_id('UserInfo', uuid)
  212. if not item:
  213. return dict(code=2, details=f"something is wrong.")
  214. # --- update UserCamera by camera_uuid ---
  215. """
  216. UserInfo: 用户信息表
  217. UserInfo.username: 登录账号
  218. UserInfo.password: 登录密码 (默认值:baosteel@2024)
  219. UserInfo.role_type: 角色类型 (1: 超级管理员 2: 普通管理员 3: 普通用户) (默认值:3)
  220. UserInfo.create_at: 创建时间
  221. UserInfo.update_at: 修改时间
  222. UserInfo.name: 姓名
  223. UserInfo.cid: 身份证号
  224. UserInfo.phone: 手机号
  225. UserInfo.sex: 性别 (0:未设置 1:男 2:女)
  226. UserInfo.department: 部门
  227. UserInfo.wid: 工号
  228. UserInfo.allow_at: 注册审批时间
  229. UserInfo.state: 禁用状态 (0:激活 1:禁用)
  230. """
  231. update_dict = dict()
  232. update_dict['update_at'] = methods.now_ts()
  233. if sources.get('username'):
  234. update_dict['username'] = sources.get('username')
  235. if sources.get('password'):
  236. update_dict['password'] = generate_password_hash(sources.get('password'))
  237. if sources.get('role_type'):
  238. update_dict['role_type'] = str(sources.get('role_type'))
  239. if sources.get('name'):
  240. update_dict['name'] = sources.get('name')
  241. if sources.get('cid'):
  242. update_dict['cid'] = sources.get('cid')
  243. if sources.get('phone'):
  244. update_dict['phone'] = sources.get('phone')
  245. if sources.get('sex'):
  246. update_dict['sex'] = str(sources.get('sex'))
  247. if sources.get('department'):
  248. update_dict['department'] = sources.get('department')
  249. if sources.get('wid'):
  250. update_dict['wid'] = sources.get('wid')
  251. # --- check ---
  252. if type(item.get('role_type')) == int:
  253. update_dict['role_type'] = str(item.get('role_type'))
  254. if type(item.get('sex')) == int:
  255. update_dict['sex'] = str(item.get('sex'))
  256. if type(item.get('state')) == int:
  257. update_dict['state'] = str(item.get('state'))
  258. item = Global.mdb.update_one_by_id('UserInfo', uuid, update_dict, need_back=True)
  259. return dict(code=0, data=item)