Discount.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace app\data\controller\base;
  3. use app\data\model\BaseUserDiscount;
  4. use app\data\model\BaseUserUpgrade;
  5. use app\data\service\UserUpgradeService;
  6. use think\admin\Controller;
  7. use think\admin\helper\QueryHelper;
  8. /**
  9. * 折扣方案管理
  10. * Class Discount
  11. * @package app\data\controller\base
  12. */
  13. class Discount extends Controller
  14. {
  15. /**
  16. * 折扣方案管理
  17. * @auth true
  18. * @menu true
  19. * @throws \think\db\exception\DataNotFoundException
  20. * @throws \think\db\exception\DbException
  21. * @throws \think\db\exception\ModelNotFoundException
  22. */
  23. public function index()
  24. {
  25. $this->type = input('get.type', 'index');
  26. BaseUserDiscount::mQuery()->layTable(function () {
  27. $this->title = '折扣方案管理';
  28. }, function (QueryHelper $query) {
  29. $query->where(['status' => intval($this->type === 'index'), 'deleted' => 0]);
  30. });
  31. }
  32. /**
  33. * 添加折扣方案
  34. * @auth true
  35. */
  36. public function add()
  37. {
  38. BaseUserDiscount::mForm('form');
  39. }
  40. /**
  41. * 编辑折扣方案
  42. * @auth true
  43. */
  44. public function edit()
  45. {
  46. BaseUserDiscount::mForm('form');
  47. }
  48. /**
  49. * 表单数据处理
  50. * @param array $vo
  51. */
  52. protected function _form_filter(array &$vo)
  53. {
  54. if ($this->request->isPost()) {
  55. $rule = [];
  56. foreach ($vo as $k => $v) if (stripos($k, '_level_') !== false) {
  57. [, $level] = explode('_level_', $k);
  58. $rule[] = ['level' => $level, 'discount' => $v];
  59. }
  60. $vo['items'] = json_encode($rule, JSON_UNESCAPED_UNICODE);
  61. } else {
  62. $this->levels = BaseUserUpgrade::items();
  63. if (empty($this->levels)) $this->error('未配置用户等级!');
  64. foreach ($vo['items'] ?? [] as $item) {
  65. $vo["_level_{$item['level']}"] = $item['discount'];
  66. }
  67. }
  68. }
  69. /**
  70. * 修改折扣方案状态
  71. * @auth true
  72. */
  73. public function state()
  74. {
  75. BaseUserDiscount::mSave();
  76. }
  77. /**
  78. * 删除折扣方案配置
  79. * @auth true
  80. */
  81. public function remove()
  82. {
  83. BaseUserDiscount::mDelete();
  84. }
  85. }