AdminLogic.php 5.1 KB

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