code1000.py 9.5 KB

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