DictdataController.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\admin\controller;
  3. use think\exception\ValidateException;
  4. use app\model\DictData;
  5. class DictdataController extends Base
  6. {
  7. function index()
  8. {
  9. $weid = weid();
  10. $keyword = input('post.keyword', '', 'serach_in');
  11. $query = DictData::where(['weid' => $weid]);
  12. if (!empty($keyword)) {
  13. $query->where('title', 'like', '%' . $keyword . '%');
  14. }
  15. $res = $query->order('sort asc,id asc')
  16. ->paginate(getpage())
  17. ->toArray();
  18. foreach ($res['data'] as &$vo) {
  19. $vo['pic'] = toimg($vo['pic']);
  20. }
  21. $data['data'] = $res;
  22. return $this->json($data);
  23. }
  24. function listUpdate()
  25. {
  26. $data = only('id,status,sort');
  27. if (!$data['id']) throw new ValidateException('参数错误');
  28. DictData::update($data);
  29. return $this->json(['msg' => '操作成功']);
  30. }
  31. public function update()
  32. {
  33. $id = $this->request->post('id');
  34. $data = input('post.');
  35. if (empty($id)) {
  36. $data['weid'] = weid();
  37. try {
  38. $res = DictData::create($data);
  39. if ($res->id && empty($data['sort'])) {
  40. DictData::update(['sort' => $res->id, 'id' => $res->id]);
  41. }
  42. } catch (\Exception $e) {
  43. throw new ValidateException($e->getMessage());
  44. }
  45. return $this->json(['msg' => '添加成功', 'data' => $res->id]);
  46. } else {
  47. try {
  48. DictData::update($data);
  49. } catch (\Exception $e) {
  50. throw new ValidateException($e->getMessage());
  51. }
  52. return $this->json(['msg' => '修改成功']);
  53. }
  54. }
  55. function getInfo()
  56. {
  57. $id = $this->request->post('id', '', 'serach_in');
  58. if (!$id) throw new ValidateException('参数错误');
  59. $data = DictData::find($id);
  60. if ($data) {
  61. $data = $data->toArray();
  62. $data['pic'] = toimg($data['pic']);
  63. }
  64. return $this->json(['data' => $data]);
  65. }
  66. function delete()
  67. {
  68. return $this->del(new DictData());
  69. }
  70. function getoptions()
  71. {
  72. $type = input('get.type', '', 'serach_in');
  73. if (!is_string($type)) {
  74. return [];
  75. }
  76. $type = explode(',', $type);
  77. $lists = DictData::whereIn('type_value', $type)->select()->toArray();
  78. if (empty($lists)) {
  79. return [];
  80. }
  81. $result = [];
  82. foreach ($type as $item) {
  83. foreach ($lists as $dict) {
  84. if ($dict['type_value'] == $item) {
  85. $result[$item][] = $dict;
  86. }
  87. }
  88. }
  89. return $this->json(['data' => $result]);
  90. }
  91. }