AdminLogic.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace app\admin\logic;
  3. use app\admin\controller\Common;
  4. use app\model\AdminModel;
  5. use app\model\CommonModel;
  6. use think\exception\ValidateException;
  7. use think\facade\Config;
  8. use think\response\Json;
  9. class AdminLogic extends BaseLogic
  10. {
  11. //获取运营账号列表
  12. public static function list(array $data = []): Json
  13. {
  14. $db = AdminModel::alias('a')
  15. ->leftJoin('role b', 'b.id=a.role_id')
  16. ->where('a.is_del', CommonModel::$del_normal);
  17. if ($data['username'] != '') $db->whereLike('a.username', '%' . $data['username'] . '%');
  18. if ($data['nickname'] != '') $db->whereLike('a.nickname', '%' . $data['nickname'] . '%');
  19. if ($data['mobile'] != '') $db->whereLike('a.mobile', '%' . $data['mobile'] . '%');
  20. if ($data['status'] != '') $db->where('a.status', $data['status']);
  21. $count = $db->count('a.id');
  22. $list = $db->field('a.id,a.username,a.nickname,a.mobile,a.status,a.addtime,b.name as role_name')
  23. ->order(['a.id' => 'desc'])
  24. ->page($data['page'], $data['size'])
  25. ->select()
  26. ->toArray();
  27. return json_show(CommonModel::$success, '获取运营账号列表成功', ['count' => $count, 'list' => $list]);
  28. }
  29. //添加运营账号
  30. public static function add(array $data = []): Json
  31. {
  32. $salt = randomkeys(6);
  33. $data = array_merge($data, [
  34. 'salt' => $salt,
  35. 'password' => getPassword(Config::get('common.default_password'), $salt),
  36. 'is_del' => CommonModel::$del_normal,
  37. 'status' => CommonModel::$status_normal,
  38. 'addtime' => date('Y-m-d H:i:s'),
  39. 'updatetime' => date('Y-m-d H:i:s'),
  40. ]);
  41. $rs = AdminModel::create($data)->save();
  42. return $rs ? json_show(CommonModel::$success, '添加运营账号成功') : json_show(CommonModel::$error_param, '添加运营账号失败');
  43. }
  44. //获取运营账号详情
  45. public static function read(int $id = 0): Json
  46. {
  47. $rs = AdminModel::where(['id' => $id, 'is_del' => CommonModel::$del_normal])
  48. ->withoutField('password,salt')
  49. ->findOrEmpty()
  50. ->toArray();
  51. return json_show(CommonModel::$success, '获取运营账号详情成功', $rs);
  52. }
  53. //编辑运营账号
  54. public static function edit(array $data = []): Json
  55. {
  56. $data = array_merge($data, [
  57. 'updatetime' => date('Y-m-d H:i:s'),
  58. ]);
  59. $rs = AdminModel::where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])->save($data);
  60. return $rs ? json_show(CommonModel::$success, '编辑运营账号成功') : json_show(CommonModel::$error_param, '编辑运营账号失败');
  61. }
  62. //删除运营账号
  63. public static function delete(int $id = 0): Json
  64. {
  65. $rs = AdminModel::where(['id' => $id, 'is_del' => CommonModel::$del_normal])
  66. ->save([
  67. 'is_del' => CommonModel::$del_deleted,
  68. 'updatetime' => date('Y-m-d H:i:s'),
  69. ]);
  70. return $rs ? json_show(CommonModel::$success, '删除运营账号成功') : json_show(CommonModel::$error_param, '删除运营账号失败,该账号不存在或重复删除');
  71. }
  72. //启禁用运营账号
  73. public static function status(array $data = []): Json
  74. {
  75. $data = array_merge($data, ['updatetime' => date('Y-m-d H:i:s')]);
  76. $rs = AdminModel::where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
  77. ->where('status', '<>', $data['status'])
  78. ->save($data);
  79. return $rs ? json_show(CommonModel::$success, '操作成功') : json_show(CommonModel::$error_param, '操作失败');
  80. }
  81. //更改密码
  82. public static function changePasswod(array $data = []): Json
  83. {
  84. $rs = AdminModel::where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
  85. ->field('id,password,salt,status')
  86. ->findOrEmpty();
  87. if ($rs->isEmpty()) throw new ValidateException('该运营账号不存在');
  88. if (getPassword($data['old_password'], $rs->salt) != $rs->password) throw new ValidateException('旧密码错误');
  89. $salt = randomkeys(6);
  90. $password = getPassword($data['new_password'], $salt);
  91. $da = [
  92. 'salt' => $salt,
  93. 'password' => $password,
  94. 'updatetime' => date('Y-m-d H:i:s'),
  95. ];
  96. $rs = AdminModel::where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
  97. ->save($da);
  98. return $rs ? json_show(CommonModel::$success, '更改密码成功') : json_show(CommonModel::$error_param, '更改密码失败');
  99. }
  100. //重置密码(慎用)
  101. public static function resetPasswod(int $id = 0): Json
  102. {
  103. $rs = AdminModel::where(['id' => $id, 'is_del' => CommonModel::$del_normal])
  104. ->field('id')
  105. ->findOrEmpty()
  106. ->isEmpty();
  107. if ($rs) throw new ValidateException('该运营账号不存在');
  108. $salt = randomkeys(6);
  109. $da = [
  110. 'salt' => $salt,
  111. 'password' => getPassword(Config::get('common.default_password'), $salt),
  112. 'updatetime' => date('Y-m-d H:i:s'),
  113. ];
  114. $rs = AdminModel::where(['id' => $id, 'is_del' => CommonModel::$del_normal])
  115. ->save($da);
  116. return $rs ? json_show(CommonModel::$success, '重置密码成功') : json_show(CommonModel::$error_param, '重置密码失败');
  117. }
  118. }