Storegrade.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. *
  7. *
  8. * ----------------------------------------------------------------------------
  9. *
  10. * 控制器
  11. */
  12. class Storegrade extends AdminControl
  13. {
  14. public function initialize()
  15. {
  16. parent::initialize();
  17. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/storegrade.lang.php');
  18. }
  19. public function index()
  20. {
  21. $like_storegrade_name = trim(input('param.like_storegrade_name'));
  22. $condition[] = array('storegrade_name', 'like', "%" . $like_storegrade_name . "%");
  23. $storegrade_list = model('storegrade')->getStoregradeList($condition);
  24. // 获取分页显示
  25. View::assign('storegrade_list', $storegrade_list);
  26. View::assign('like_storegrade_name', $like_storegrade_name);
  27. $this->setAdminCurItem('index');
  28. return View::fetch();
  29. }
  30. public function add()
  31. {
  32. if (!request()->isPost()) {
  33. return View::fetch('form');
  34. } else {
  35. $data = array(
  36. 'storegrade_name' => input('post.storegrade_name'),
  37. 'storegrade_goods_limit' => input('post.storegrade_goods_limit'),
  38. 'storegrade_album_limit' => input('post.storegrade_album_limit'),
  39. 'storegrade_space_limit' => input('post.storegrade_space_limit'),
  40. 'storegrade_price' => intval(input('post.storegrade_price')),
  41. 'storegrade_description' => input('post.storegrade_description'),
  42. 'storegrade_sort' => input('post.storegrade_sort'),
  43. );
  44. $storegrade_validate = ds_validate('storegrade');
  45. if (!$storegrade_validate->scene('add')->check($data)) {
  46. $this->error($storegrade_validate->getError());
  47. }
  48. //验证等级名称
  49. if (!$this->checkGradeName(array('storegrade_name' => trim(input('post.storegrade_name'))))) {
  50. $this->error(lang('now_store_grade_name_is_there'));
  51. }
  52. //验证级别是否存在
  53. if (!$this->checkGradeSort(array('storegrade_sort' => trim(input('post.storegrade_sort'))))) {
  54. $this->error(lang('add_gradesortexist'));
  55. }
  56. $result = model('storegrade')->addStoregrade($data);
  57. if ($result) {
  58. dsLayerOpenSuccess(lang('ds_common_op_succ'), (string)url('Storegrade/index'));
  59. } else {
  60. $this->error(lang('ds_common_op_fail'));
  61. }
  62. }
  63. }
  64. public function edit()
  65. {
  66. //注:pathinfo地址参数不能通过get方法获取,查看“获取PARAM变量”
  67. $storegrade_id = input('param.storegrade_id');
  68. if (empty($storegrade_id)) {
  69. $this->error(lang('param_error'));
  70. }
  71. if (!request()->isPost()) {
  72. $storegrade = model('storegrade')->getOneStoregrade($storegrade_id);
  73. View::assign('storegrade', $storegrade);
  74. return View::fetch('form');
  75. } else {
  76. $data = array(
  77. 'storegrade_name' => input('post.storegrade_name'),
  78. 'storegrade_goods_limit' => input('post.storegrade_goods_limit'),
  79. 'storegrade_album_limit' => input('post.storegrade_album_limit'),
  80. 'storegrade_space_limit' => input('post.storegrade_space_limit'),
  81. 'storegrade_price' => intval(input('post.storegrade_price')),
  82. 'storegrade_description' => input('post.storegrade_description'),
  83. 'storegrade_sort' => input('post.storegrade_sort'),
  84. );
  85. $storegrade_validate = ds_validate('storegrade');
  86. if (!$storegrade_validate->scene('edit')->check($data)) {
  87. $this->error($storegrade_validate->getError());
  88. }
  89. //验证等级名称
  90. if (!$this->checkGradeName(array('storegrade_name' => trim(input('post.storegrade_name')), 'storegrade_id' => intval(input('param.storegrade_id'))))) {
  91. $this->error(lang('now_store_grade_name_is_there'));
  92. }
  93. //验证级别是否存在
  94. if (!$this->checkGradeSort(array('storegrade_sort' => trim(input('post.storegrade_sort')), 'storegrade_id' => intval(input('param.storegrade_id'))))) {
  95. $this->error(lang('add_gradesortexist'));
  96. }
  97. $result = model('storegrade')->editStoregrade($storegrade_id, $data);
  98. if ($result >= 0) {
  99. dsLayerOpenSuccess(lang('ds_common_op_succ'), (string)url('Storegrade/index'));
  100. } else {
  101. $this->error(lang('ds_common_op_fail'));
  102. }
  103. }
  104. }
  105. public function drop()
  106. {
  107. //注:pathinfo地址参数不能通过get方法获取,查看“获取PARAM变量”
  108. $storegrade_id = intval(input('param.storegrade_id'));
  109. if ($storegrade_id <= 0) {
  110. ds_json_encode(10001, lang('param_error'));
  111. }
  112. if ($storegrade_id == '1') {
  113. ds_json_encode(10001, lang('default_store_grade_no_del'));
  114. }
  115. //判断该等级下是否存在店铺,存在的话不能删除
  116. if (!$this->isable_delStoregrade($storegrade_id)) {
  117. $this->error(lang('del_gradehavestore'), (string)url('Storegrade/index'));
  118. }
  119. $result = model('storegrade')->delStoregrade($storegrade_id);
  120. if ($result) {
  121. ds_json_encode(10000, lang('ds_common_del_succ'));
  122. } else {
  123. ds_json_encode(10001, lang('ds_common_del_fail'));
  124. }
  125. }
  126. /**
  127. * 查询店铺等级名称是否存在
  128. */
  129. private function checkGradeName($param)
  130. {
  131. $storegrade_model = model('storegrade');
  132. $condition[] = array('storegrade_name', '=', $param['storegrade_name']);
  133. if (isset($param['storegrade_id'])) {
  134. $storegrade_id = intval($param['storegrade_id']);
  135. $condition[] = array('storegrade_id', '<>', $storegrade_id);
  136. }
  137. $list = $storegrade_model->getStoregradeList($condition);
  138. if (empty($list)) {
  139. return true;
  140. } else {
  141. return false;
  142. }
  143. }
  144. /**
  145. * 查询店铺等级是否存在
  146. */
  147. private function checkGradeSort($param)
  148. {
  149. $storegrade_model = model('storegrade');
  150. $condition = array();
  151. $condition[] = array('storegrade_sort', '=', $param['storegrade_sort']);
  152. if (isset($param['storegrade_id'])) {
  153. $storegrade_id = intval($param['storegrade_id']);
  154. $condition[] = array('storegrade_id', '<>', $storegrade_id);
  155. }
  156. $list = array();
  157. $list = $storegrade_model->getStoregradeList($condition);
  158. if (is_array($list) && count($list) > 0) {
  159. return false;
  160. } else {
  161. return true;
  162. }
  163. }
  164. /**
  165. * 判断店铺等级是否能删除
  166. */
  167. public function isable_delStoregrade($storegrade_id)
  168. {
  169. //判断该等级下是否存在店铺,存在的话不能删除
  170. $store_model = model('store');
  171. $store_list = $store_model->getStoreList(array('grade_id' => $storegrade_id));
  172. if (count($store_list) > 0) {
  173. return false;
  174. }
  175. return true;
  176. }
  177. /**
  178. * 获取卖家栏目列表,针对控制器下的栏目
  179. */
  180. protected function getAdminItemList()
  181. {
  182. $menu_array = array(
  183. array(
  184. 'name' => 'index',
  185. 'text' => lang('ds_storegrade'),
  186. 'url' => (string)url('Storegrade/index')
  187. ),
  188. );
  189. if (request()->action() == 'add' || request()->action() == 'index') {
  190. $menu_array[] = array(
  191. 'name' => 'add',
  192. 'text' => lang('ds_new'),
  193. 'url' => "javascript:dsLayerOpen('" . (string)url('Storegrade/add') . "','" . lang('ds_new') . "')"
  194. );
  195. }
  196. return $menu_array;
  197. }
  198. }