GoodsquantityunitlController.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace app\admin\controller;
  3. use think\exception\ValidateException;
  4. use app\model\GoodsQuantityUnit;
  5. class GoodsquantityunitlController extends Base
  6. {
  7. function getPtype()
  8. {
  9. $path = input('post.path', '', 'serach_in');
  10. if ($path == '/goodsquantityunitl/service') {
  11. return 2;
  12. } else {
  13. return 1;
  14. }
  15. }
  16. function index()
  17. {
  18. $weid = weid();
  19. $keyword = input('post.keyword', '', 'serach_in');
  20. $ptype = $this->getPtype();
  21. GoodsQuantityUnit::datainitial($ptype);
  22. $query = GoodsQuantityUnit::where(['weid' => $weid, 'ptype' => $ptype]);
  23. if (!empty($keyword)) {
  24. $query->where('title', 'like', '%' . $keyword . '%');
  25. }
  26. $datalist = $query->order('sort asc,id asc')->select()->toArray();
  27. $data['data'] = $datalist;
  28. return $this->json($data);
  29. }
  30. function listUpdate()
  31. {
  32. $data = only('id,status,sort');
  33. if (!$data['id']) throw new ValidateException('参数错误');
  34. GoodsQuantityUnit::update($data);
  35. return $this->json(['msg' => '操作成功']);
  36. }
  37. public function update()
  38. {
  39. $id = $this->request->post('id');
  40. $data = input('post.');
  41. unset($data['create_time']);
  42. $weid = weid();
  43. $ptype = $this->getPtype();
  44. if (empty($id)) {
  45. $data['weid'] = $weid;
  46. $data['ptype'] = $ptype;
  47. try {
  48. $res = GoodsQuantityUnit::create($data);
  49. } catch (\Exception $e) {
  50. throw new ValidateException($e->getMessage());
  51. }
  52. return $this->json(['msg' => '添加成功', 'data' => $res->id]);
  53. } else {
  54. try {
  55. GoodsQuantityUnit::update($data);
  56. } catch (\Exception $e) {
  57. throw new ValidateException($e->getMessage());
  58. }
  59. return $this->json(['msg' => '修改成功']);
  60. }
  61. }
  62. function getInfo()
  63. {
  64. $id = $this->request->post('id', '', 'serach_in');
  65. if (!$id) throw new ValidateException('参数错误');
  66. $data = GoodsQuantityUnit::find($id);
  67. if ($data) {
  68. $data = $data->toArray();
  69. }
  70. return $this->json(['data' => $data]);
  71. }
  72. function delete()
  73. {
  74. return $this->del(new GoodsQuantityUnit());
  75. }
  76. }