Fleaclass.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. * ============================================================================
  7. * DSMall多用户商城
  8. * ============================================================================
  9. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  10. * 网站地址: http://www.csdeshang.com
  11. * ----------------------------------------------------------------------------
  12. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  13. * 不允许对程序代码以任何形式任何目的的再发布。
  14. * ============================================================================
  15. * 控制器
  16. */
  17. class Fleaclass extends AdminControl
  18. {
  19. public function initialize()
  20. {
  21. parent::initialize(); // TODO: Change the autogenerated stub
  22. Lang::load(base_path() . 'admin/lang/'.config('lang.default_lang').'/flea.lang.php');
  23. }
  24. public function goods_class(){
  25. $this->flea_class();
  26. }
  27. /**
  28. * 分类管理
  29. */
  30. public function flea_class(){
  31. $fleaclass_model = model('fleaclass');
  32. /**
  33. * 删除,编辑
  34. */
  35. if (request()->isPost()){
  36. /**
  37. * 删除
  38. */
  39. $check_fleaclass_id_array = input('post.check_fleaclass_id/a');#获取数组
  40. if (input('post.submit_type') == 'del'){
  41. if (!empty($check_fleaclass_id_array)){
  42. if (is_array($check_fleaclass_id_array)){
  43. $del_array = $fleaclass_model->getChildClass($check_fleaclass_id_array);
  44. if (is_array($del_array)){
  45. foreach ($del_array as $k => $v){
  46. $fleaclass_model->delFleaclass($v['fleaclass_id']);
  47. }
  48. }
  49. }
  50. $this->success(lang('goods_class_index_del_succ'));
  51. }else {
  52. $this->error(lang('goods_class_index_choose_del'));
  53. }
  54. }
  55. /**
  56. * 编辑
  57. */
  58. if (input('post.submit_type') == 'brach_edit'){
  59. if (!empty($check_fleaclass_id_array)){
  60. View::assign('id',implode(',',$check_fleaclass_id_array));
  61. $this->setAdminCurItem('brach_edit');
  62. return View::fetch('brach_edit');
  63. }else {
  64. $this->error(lang('goods_class_index_choose_edit'));
  65. }
  66. }
  67. /**
  68. * 首页显示
  69. */
  70. if(input('post.submit_type') == 'index_show' or input('post.submit_type') == 'index_hide'){
  71. if (!empty($check_fleaclass_id_array)){
  72. if (is_array($check_fleaclass_id_array)){
  73. $param = array();
  74. $param['fleaclass_index_show'] = input('post.submit_type') == 'index_show'?'1':'0';
  75. foreach ($check_fleaclass_id_array as $k=>$v){
  76. $fleaclass_model->editFleaclass(array('fleaclass_id'=>$v),$param);
  77. }
  78. }
  79. $this->success(lang('goods_class_index_in_homepage').(input('post.submit_type') == 'index_show'?lang('goods_class_index_display'):lang('goods_class_index_hide')).lang('goods_class_index_succ'));
  80. }else {
  81. $this->error(lang('goods_class_index_choose_in_homepage').(input('post.submit_type') == 'index_show'?lang('goods_class_index_display'):lang('goods_class_index_hide')).lang('goods_class_index_content'));
  82. }
  83. }
  84. }
  85. /**
  86. * 父ID
  87. */
  88. $parent_id = input('fleaclass_parent_id')?input('fleaclass_parent_id'):0;
  89. /**
  90. * 列表
  91. */
  92. $tmp_list = $fleaclass_model->getTreeClassList(4);
  93. $class_list=array();
  94. if (is_array($tmp_list)){
  95. foreach ($tmp_list as $k => $v){
  96. if ($v['fleaclass_parent_id'] == $parent_id){
  97. /**
  98. * 判断是否有子类
  99. */
  100. if (isset($tmp_list[$k+1]) && ($tmp_list[$k+1]['deep'] > $v['deep'])){
  101. $v['have_child'] = 1;
  102. }else{
  103. $v['have_child'] = 0;
  104. }
  105. $class_list[] = $v;
  106. }
  107. }
  108. }
  109. if (input('ajax') == '1'){
  110. $output = json_encode($class_list);
  111. print_r($output);
  112. exit;
  113. }else {
  114. View::assign('class_list',$class_list);
  115. $this->setAdminCurItem('index');
  116. return View::fetch('index');
  117. }
  118. }
  119. /**
  120. * 保存批量修改分类
  121. */
  122. public function brach_edit_save(){
  123. if (input('post.fleaclass_show') == '-1'){
  124. $this->success(lang('goods_class_batch_edit_succ'),'fleaclass/flea_class');
  125. }
  126. if (request()->isPost()){
  127. $fleaclass_model = model('fleaclass');
  128. $array = explode(',',input('post.id'));
  129. if (is_array($array)){
  130. foreach ($array as $k => $v){
  131. $update_array = array();
  132. $update_array['fleaclass_show'] = input('post.fleaclass_show');
  133. $fleaclass_model->editFleaclass(array('fleaclass_id'=>$v),$update_array);
  134. }
  135. $this->success(lang('goods_class_batch_edit_succ'));
  136. }else {
  137. $this->success(lang('goods_class_batch_edit_wrong_content'));
  138. }
  139. }else {
  140. $this->success(lang('goods_class_batch_edit_wrong_content'));
  141. }
  142. }
  143. /**
  144. * 商品分类添加
  145. */
  146. public function goods_class_add(){
  147. $fleaclass_model = model('fleaclass');
  148. if (request()->isPost()){
  149. /**
  150. * 验证
  151. */
  152. $data=[
  153. 'fleaclass_name' =>input('post.fleaclass_name'),
  154. 'fleaclass_sort' =>input('post.fleaclass_sort')
  155. ];
  156. $fleaclass_validate = ds_validate('fleaclass');
  157. if (!$fleaclass_validate->scene('goods_class_add')->check($data)){
  158. $this->error($fleaclass_validate->getError());
  159. }else {
  160. $insert_array = array();
  161. $insert_array['fleaclass_name'] = input('post.fleaclass_name');
  162. $insert_array['fleaclass_parent_id'] = input('post.fleaclass_parent_id');
  163. $insert_array['fleaclass_sort'] = input('post.fleaclass_sort');
  164. $insert_array['fleaclass_show'] = input('post.fleaclass_show');
  165. $insert_array['fleaclass_index_show'] = input('post.fleaclass_index_show');
  166. $result = $fleaclass_model->addFleaclass($insert_array);
  167. if ($result){
  168. dsLayerOpenSuccess(lang('goods_class_add_succ'));
  169. }else {
  170. $this->error(lang('goods_class_add_fail'));
  171. }
  172. }
  173. }
  174. /**
  175. * 父类列表,只取到第三级
  176. */
  177. $parent_list = $fleaclass_model->getTreeClassList(3);
  178. if (is_array($parent_list)){
  179. foreach ($parent_list as $k => $v){
  180. $parent_list[$k]['fleaclass_name'] = str_repeat("&nbsp;",$v['deep']*2).$v['fleaclass_name'];
  181. }
  182. }
  183. View::assign('fleaclass_parent_id',input('fleaclass_parent_id'));
  184. View::assign('parent_list',$parent_list);
  185. $this->setAdminCurItem('add');
  186. return View::fetch('add');
  187. }
  188. /**
  189. * 编辑
  190. */
  191. public function goods_class_edit() {
  192. $fleaclass_model = model('fleaclass');
  193. if (request()->isPost()) {
  194. /**
  195. * 验证
  196. */
  197. $data = [
  198. 'fleaclass_name' => input('post.fleaclass_name'),
  199. 'fleaclass_sort' => input('post.fleaclass_sort')
  200. ];
  201. $fleaclass_validate = ds_validate('fleaclass');
  202. if (!$fleaclass_validate->scene('goods_class_edit')->check($data)){
  203. $this->error($fleaclass_validate->getError());
  204. } else {
  205. $update_array = array();
  206. $update_array['fleaclass_name'] = input('post.fleaclass_name');
  207. $fleaclass_parent_id = intval(input('post.fleaclass_parent_id'));
  208. if ($fleaclass_parent_id) {
  209. $update_array['fleaclass_parent_id'] = $fleaclass_parent_id;
  210. }
  211. $update_array['fleaclass_sort'] = input('post.fleaclass_sort');
  212. $update_array['fleaclass_show'] = input('post.fleaclass_show');
  213. $update_array['fleaclass_index_show'] = input('post.fleaclass_index_show');
  214. $result = $fleaclass_model->editFleaclass(array('fleaclass_id'=>input('post.fleaclass_id')),$update_array);
  215. if ($result) {
  216. dsLayerOpenSuccess(lang('goods_class_batch_edit_ok'));
  217. } else {
  218. $this->error(lang('goods_class_batch_edit_fail'));
  219. }
  220. }
  221. } else {
  222. $class_array = $fleaclass_model->getOneFleaclass(input('param.fleaclass_id'));
  223. if (empty($class_array)) {
  224. $this->error(lang('goods_class_batch_edit_paramerror'));
  225. }
  226. /**
  227. * 父类列表,只取到第三级
  228. */
  229. $parent_list = $fleaclass_model->getTreeClassList(3);
  230. if (is_array($parent_list)) {
  231. $unset_sign = false;
  232. foreach ($parent_list as $k => $v) {
  233. if ($v['fleaclass_id'] == $class_array['fleaclass_id']) {
  234. $deep = $v['deep'];
  235. $unset_sign = true;
  236. }
  237. if ($unset_sign == true) {
  238. if ($v['deep'] == $deep && $v['fleaclass_id'] != $class_array['fleaclass_id']) {
  239. $unset_sign = false;
  240. }
  241. if ($v['deep'] > $deep || $v['fleaclass_id'] == $class_array['fleaclass_id']) {
  242. unset($parent_list[$k]);
  243. }
  244. } else {
  245. $parent_list[$k]['fleaclass_name'] = str_repeat("&nbsp;", $v['deep'] * 2) . $v['fleaclass_name'];
  246. }
  247. }
  248. }
  249. View::assign('parent_list', $parent_list);
  250. View::assign('class_array', $class_array);
  251. $this->setAdminCurItem('add');
  252. return View::fetch('edit');
  253. }
  254. }
  255. /**
  256. * 分类导入
  257. */
  258. public function goods_class_import(){
  259. /**
  260. * 实例化模型
  261. */
  262. $fleaclass_model = model('fleaclass');
  263. /**
  264. * 导入
  265. */
  266. if (request()->isPost()){
  267. /**
  268. * 得到导入文件后缀名
  269. */
  270. $csv_name=explode('.',$_FILES['csv']['name']);
  271. $file_type = end($csv_name);
  272. if (!empty($_FILES['csv']) && !empty($_FILES['csv']['name']) && $file_type == 'csv'){
  273. $fp = @fopen($_FILES['csv']['tmp_name'],'rb');
  274. /**
  275. * 父ID
  276. */
  277. $parent_id_1 = 0;
  278. while (!feof($fp)) {
  279. $data = fgets($fp, 4096);
  280. switch (strtoupper(input('post.charset'))){
  281. case 'UTF-8':
  282. if (strtoupper(CHARSET) !== 'UTF-8'){
  283. $data = iconv('UTF-8',strtoupper(CHARSET),$data);
  284. }
  285. break;
  286. case 'GBK':
  287. if (strtoupper(CHARSET) !== 'GBK'){
  288. $data = iconv('GBK',strtoupper(CHARSET),$data);
  289. }
  290. break;
  291. }
  292. if (!empty($data)){
  293. $data = str_replace('"','',$data);
  294. /**
  295. * 逗号去除
  296. */
  297. $tmp_array = array();
  298. $tmp_array = explode(',',$data);
  299. if($tmp_array[0] == 'sort_order')continue;
  300. /**
  301. * 第一位是序号,后面的是内容,最后一位名称
  302. */
  303. $tmp_deep = 'parent_id_'.(count($tmp_array)-1);
  304. $insert_array = array();
  305. $insert_array['fleaclass_sort'] = $tmp_array[0];
  306. $insert_array['fleaclass_parent_id'] = $$tmp_deep;
  307. $insert_array['fleaclass_name'] = $tmp_array[count($tmp_array)-1];
  308. $fleaclass_id = $fleaclass_model->addFleaclass($insert_array);
  309. /**
  310. * 赋值这个深度父ID
  311. */
  312. $tmp = 'parent_id_'.count($tmp_array);
  313. $$tmp = $fleaclass_id;
  314. }
  315. }
  316. /**
  317. * 重新生成缓存
  318. */
  319. $this->success(lang('goods_class_import_succ'),'fleaclass/flea_class');
  320. }else {
  321. $this->error(lang('goods_class_import_csv_null'));
  322. }
  323. }
  324. $this->setAdminCurItem('import');
  325. return View::fetch('import');
  326. }
  327. /**
  328. * 分类导出
  329. */
  330. public function goods_class_export() {
  331. /**
  332. * 导出
  333. */
  334. if (request()->isPost()) {
  335. /**
  336. * 实例化模型
  337. */
  338. $fleaclass_model = model('fleaclass');
  339. /**
  340. * 分类信息
  341. */
  342. $class_list = $fleaclass_model->getTreeClassList();
  343. @header("Content-type: application/unknown");
  344. @header("Content-Disposition: attachment; filename=flea_class.csv");
  345. if (is_array($class_list)) {
  346. foreach ($class_list as $k => $v) {
  347. $tmp = array();
  348. /**
  349. * 序号
  350. */
  351. $tmp['fleaclass_sort'] = $v['fleaclass_sort'];
  352. /**
  353. * 深度
  354. */
  355. for ($i = 1; $i <= ($v['deep'] - 1); $i++) {
  356. $tmp[] = '';
  357. }
  358. /**
  359. * 分类名称
  360. */
  361. $tmp['fleaclass_name'] = $v['fleaclass_name'];
  362. /**
  363. * 转码 utf-gbk
  364. */
  365. if (strtoupper(CHARSET) == 'UTF-8') {
  366. switch (input('post.if_convert')) {
  367. case '1':
  368. $tmp_line = iconv('UTF-8', 'GB2312//IGNORE', join(',', $tmp));
  369. break;
  370. case '0':
  371. $tmp_line = join(',', $tmp);
  372. break;
  373. }
  374. } else {
  375. $tmp_line = join(',', $tmp);
  376. }
  377. $tmp_line = str_replace("\r\n", '', $tmp_line);
  378. echo $tmp_line . "\r\n";
  379. }
  380. }
  381. exit;
  382. } else {
  383. $this->setAdminCurItem('export');
  384. return View::fetch('export');
  385. }
  386. }
  387. /**
  388. * 删除分类
  389. */
  390. public function goods_class_del(){
  391. $fleaclass_model = model('fleaclass');
  392. $fleaclass_id = input('get.fleaclass_id');
  393. if ($fleaclass_id > 0){
  394. /* 删除分类 */
  395. $fleaclass_model->delFleaclass($fleaclass_id);
  396. ds_json_encode(10000, lang('goods_class_index_del_succ'));
  397. }else {
  398. ds_json_encode(10001, lang('goods_class_index_choose_del'));
  399. }
  400. }
  401. /**
  402. * ajax操作
  403. */
  404. public function ajax(){
  405. switch (input('branch')){
  406. /**
  407. * 验证是否有重复的名称
  408. */
  409. case 'goods_class_name':
  410. $fleaclass_model = model('fleaclass');
  411. $class_array = $fleaclass_model->getOneFleaclass(input('id'));
  412. $condition = array();
  413. $condition[] = array('fleaclass_name','=',input('param.value'));
  414. $condition[] = array('fleaclass_parent_id','=',$class_array['fleaclass_parent_id']);
  415. $condition[] = array('fleaclass_id','not in',input('param.id'));
  416. $class_list = $fleaclass_model->getFleaclassList($condition);
  417. if (empty($class_list)){
  418. $update_array = array();
  419. $update_array['fleaclass_name'] = input('value');
  420. $fleaclass_model->editFleaclass(array('fleaclass_id'=> intval(input('param.id'))),$update_array);
  421. echo 'true';exit;
  422. }else {
  423. echo 'false';exit;
  424. }
  425. break;
  426. /**
  427. * 分类 排序 显示 设置
  428. */
  429. case 'goods_class_sort':
  430. case 'goods_class_show':
  431. case 'goods_class_index_show':
  432. $fleaclass_model = model('fleaclass');
  433. $update_array = array();
  434. $update_array[input('column')] = input('value');
  435. $fleaclass_model->editFleaclass(array('fleaclass_id'=>intval(input('param.id'))),$update_array);
  436. echo 'true';exit;
  437. break;
  438. /**
  439. * 添加、修改操作中 检测类别名称是否有重复
  440. */
  441. case 'check_class_name':
  442. $fleaclass_model = model('fleaclass');
  443. $condition = array();
  444. $condition[] = array('fleaclass_name','=',input('param.fleaclass_name'));
  445. $condition[] = array('fleaclass_parent_id','=',input('param.fleaclass_parent_id'));
  446. $condition[] = array('fleaclass_id','not in',input('param.fleaclass_id'));
  447. $class_list = $fleaclass_model->getFleaclassList($condition);
  448. if (empty($class_list)){
  449. echo 'true';exit;
  450. }else {
  451. echo 'false';exit;
  452. }
  453. break;
  454. }
  455. }
  456. protected function getAdminItemList()
  457. {
  458. $menu_array = array(
  459. array(
  460. 'name' => 'index', 'text' => lang('ds_manage'), 'url' => (string)url('Fleaclass/flea_class')
  461. ),
  462. array(
  463. 'name' => 'add', 'text' => lang('ds_add'), 'url' => "javascript:dsLayerOpen('".(string)url('Fleaclass/goods_class_add')."','".lang('ds_add')."')"
  464. ),
  465. array(
  466. 'name' => 'export', 'text' => lang('ds_export'), 'url' => (string)url('Fleaclass/goods_class_export')
  467. ),
  468. array(
  469. 'name' => 'import', 'text' => lang('ds_import'), 'url' => (string)url('Fleaclass/goods_class_import')
  470. ),
  471. );
  472. return $menu_array;
  473. }
  474. }