StoreProductRule.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\admin\controller\store;
  3. use app\admin\controller\AuthController;
  4. use app\Request;
  5. use crmeb\traits\CurdControllerTrait;
  6. use think\facade\Route as Url;
  7. use app\models\store\StoreProductRule as ProductRuleModel;
  8. use crmeb\services\{
  9. JsonService as Json, UtilService
  10. };
  11. /**
  12. * 评论管理 控制器
  13. * Class StoreProductReply
  14. * @package app\admin\controller\store
  15. */
  16. class StoreProductRule extends AuthController
  17. {
  18. use CurdControllerTrait;
  19. /**
  20. * 显示资源列表
  21. *
  22. * @return \think\Response
  23. */
  24. public function index()
  25. {
  26. if (!$this->request->isAjax()) {
  27. return $this->fetch();
  28. }
  29. $where = UtilService::getMore([
  30. ['page', 1],
  31. ['limit', 15],
  32. ['rule_name', '']
  33. ]);
  34. $list = ProductRuleModel::sysPage($where);
  35. return Json::successlayui($list);
  36. }
  37. /**
  38. * 创建
  39. * @param int $id
  40. * @return string
  41. * @throws \Exception
  42. */
  43. public function create($id = 0)
  44. {
  45. $this->assign(compact('id'));
  46. return $this->fetch();
  47. }
  48. /**
  49. * 保存新建的资源
  50. *
  51. * @param \think\Request $request
  52. * @return \think\Response
  53. */
  54. public function save(Request $request, $id = 0)
  55. {
  56. $data = UtilService::postMore([
  57. ['rule_name', ''],
  58. ['rule_value', []]
  59. ]);
  60. if ($data['rule_name'] == '') return $this->fail('请输入规则名称');
  61. if (!$data['rule_value']) return $this->fail('缺少规则值');
  62. $data['rule_value'] = json_encode($data['rule_value']);
  63. if ($id) {
  64. $rule = ProductRuleModel::get($id);
  65. if (!$rule) return $this->fail('数据不存在');
  66. ProductRuleModel::edit($data, $id);
  67. return Json::success('编辑成功!');
  68. } else {
  69. ProductRuleModel::create($data);
  70. return Json::success('规则添加成功!');
  71. }
  72. }
  73. /**
  74. * 显示指定的资源
  75. *
  76. * @param int $id
  77. * @return \think\Response
  78. */
  79. public function read($id)
  80. {
  81. return Json::successful(ProductRuleModel::sysInfo($id));
  82. }
  83. /**
  84. * 删除指定资源
  85. *
  86. * @param int $id
  87. * @return \think\Response
  88. */
  89. public function delete()
  90. {
  91. $data = UtilService::postMore([
  92. ['ids', '']
  93. ]);
  94. if ($data['ids'] == '') return $this->fail('请至少选择一条数据');
  95. $ids = strval($data['ids']);
  96. $res = ProductRuleModel::whereIn('id', $ids)->delete();
  97. if (!$res)
  98. return Json::fail(ProductRuleModel::getErrorInfo('删除失败,请稍候再试!'));
  99. else
  100. return Json::success('删除成功!');
  101. }
  102. }