Oper.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php namespace Manage\Api\Page;
  2. use Dever;
  3. use Manage\Lib\Page;
  4. # 操作
  5. class Oper extends Page
  6. {
  7. public function __construct()
  8. {
  9. parent::__construct('oper');
  10. $this->id = Dever::input('id');
  11. if (!$this->id) {
  12. Dever::error('无效数据');
  13. }
  14. $this->checkFunc();
  15. }
  16. # 更改某个字段的值
  17. public function up_commit(){}
  18. public function up()
  19. {
  20. $input = Dever::input();
  21. $field = Dever::input('field');
  22. if (is_array($field)) {
  23. $field = $field['field'];
  24. }
  25. $field = explode(',', $field);
  26. foreach ($field as $k => $v) {
  27. if (isset($input[$v]) && $value = $input[$v]) {
  28. if (is_array($value)) {
  29. $value = implode(',', $value);
  30. }
  31. $data[$v] = $value;
  32. }
  33. }
  34. if (isset($this->config['up_start']) && $this->config['up_start']) {
  35. list($this->id, $data) = Dever::call($this->config['up_start'], [$this->id, $data]);
  36. }
  37. $where['id'] = ['in', $this->id];
  38. $state = $this->db->update($where, $data);
  39. if ($state) {
  40. if (isset($this->config['up_end']) && $this->config['up_end']) {
  41. Dever::call($this->config['up_end'], [$this->id, $data]);
  42. }
  43. return ['msg' => '操作成功', 'upAdmin' => false];
  44. } else {
  45. Dever::error('操作失败');
  46. }
  47. }
  48. # 删除 删除到回收站
  49. public function recycle_commit(){}
  50. public function recycle()
  51. {
  52. $where['id'] = ['in', $this->id];
  53. $data = $this->db->select($where);
  54. if ($data) {
  55. foreach ($data as $k => $v) {
  56. $insert['table'] = $this->db->config['load'];
  57. $insert['table_id'] = $v['id'];
  58. $insert['content'] = Dever::json_encode($v);
  59. $state = Dever::db('manage/recycler')->insert($insert);
  60. if (!$state) {
  61. Dever::error('删除失败,请重试');
  62. }
  63. $state = $this->db->delete($v['id']);
  64. if (!$state) {
  65. Dever::error('删除失败,请重试');
  66. }
  67. }
  68. }
  69. return '操作成功';
  70. }
  71. # 从回收站恢复
  72. public function recover_commit(){}
  73. public function recover()
  74. {
  75. $where['id'] = ['in', $this->id];
  76. $data = $this->db->select($where);
  77. if ($data) {
  78. foreach ($data as $k => $v) {
  79. $v['content'] = Dever::json_decode($v['content']);
  80. $state = Dever::db($v['table'])->insert($v['content']);
  81. if (!$state) {
  82. Dever::error('恢复失败,请重试');
  83. }
  84. $state = $this->db->delete($v['id']);
  85. if (!$state) {
  86. Dever::error('恢复失败,请重试');
  87. }
  88. }
  89. }
  90. return '操作成功';
  91. }
  92. # 直接删除
  93. public function delete_commit(){}
  94. public function delete()
  95. {
  96. $where['id'] = ['in', $this->id];
  97. $state = $this->db->delete($where);
  98. if (!$state) {
  99. Dever::error('删除失败,请重试');
  100. }
  101. return '操作成功';
  102. }
  103. }