Storegrade.php 8.3 KB

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