Brand.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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 Brand extends AdminControl
  13. {
  14. const EXPORT_SIZE = 1000;
  15. public function initialize()
  16. {
  17. parent::initialize();
  18. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/brand.lang.php');
  19. }
  20. /**
  21. * 品牌列表
  22. */
  23. public function index()
  24. {
  25. $brand_model = model('brand');
  26. /**
  27. * 检索条件
  28. */
  29. if (!empty(input('param.search_brand_name'))) {
  30. $condition[] = array('brand_name', 'like', "%" . input('param.search_brand_name') . "%");
  31. }
  32. if (!empty(input('param.search_brand_class'))) {
  33. $condition[] = array('brand_class', 'like', "%" . input('param.search_brand_class') . "%");
  34. }
  35. $condition[] = array('brand_apply', '=', '1');
  36. $brand_list = $brand_model->getBrandList($condition, "*", 10);
  37. View::assign('showpage', $brand_model->page_info->render());
  38. View::assign('brand_list', $brand_list);
  39. View::assign('search_brand_name', trim(input('param.search_brand_name')));
  40. View::assign('search_brand_class', trim(input('param.search_brand_class')));
  41. $this->setAdminCurItem('index');
  42. return View::fetch();
  43. }
  44. /**
  45. * 增加品牌
  46. */
  47. public function brand_add()
  48. {
  49. $brand_model = model('brand');
  50. if (request()->isPost()) {
  51. $data = [
  52. 'brand_name' => input('post.brand_name'), 'brand_initial' => input('post.brand_initial'),
  53. 'brand_sort' => input('post.brand_sort')
  54. ];
  55. $brand_validate = ds_validate('brand');
  56. if (!$brand_validate->scene('brand_add')->check($data)) {
  57. $this->error($brand_validate->getError());
  58. } else {
  59. $insert_array = array();
  60. if (!empty($_FILES['_pic']['name'])) {
  61. $res = ds_upload_pic(ATTACH_BRAND, '_pic');
  62. if ($res['code']) {
  63. $brand_pic = $res['data']['file_name'];
  64. } else {
  65. $this->error($res['msg']);
  66. }
  67. }
  68. $insert_array['brand_name'] = trim(input('post.brand_name'));
  69. $insert_array['brand_initial'] = strtoupper(input('post.brand_initial'));
  70. $insert_array['gc_id'] = input('post.class_id');
  71. $insert_array['brand_class'] = trim(input('post.brand_class'));
  72. if (!empty($brand_pic)) {
  73. $insert_array['brand_pic'] = $brand_pic;
  74. }
  75. $insert_array['brand_recommend'] = trim(input('post.brand_recommend'));
  76. $insert_array['brand_sort'] = intval(input('post.brand_sort'));
  77. $insert_array['brand_showtype'] = intval(input('post.brand_showtype')) == 1 ? 1 : 0;
  78. $result = $brand_model->addBrand($insert_array);
  79. if ($result) {
  80. $this->log(lang('ds_add') . lang('brand_index_brand') . '[' . input('post.brand_name') . ']', 1);
  81. dsLayerOpenSuccess(lang('ds_common_save_succ'));
  82. } else {
  83. $this->error(lang('ds_common_save_fail'));
  84. }
  85. }
  86. } else {
  87. $brand_array = [
  88. 'brand_id' => '',
  89. 'brand_name' => '',
  90. 'brand_initial' => '',
  91. 'gc_id' => '',
  92. 'brand_class' => '',
  93. 'brand_pic' => '',
  94. 'brand_showtype' => '0',
  95. 'brand_recommend' => '1',
  96. 'brand_sort' => '0',
  97. ];
  98. View::assign('brand_array', $brand_array);
  99. // 一级商品分类
  100. $gc_list = model('goodsclass')->getGoodsclassListByParentId(0);
  101. View::assign('gc_list', $gc_list);
  102. return View::fetch('form');
  103. }
  104. }
  105. /**
  106. * 品牌编辑
  107. */
  108. public function brand_edit()
  109. {
  110. $brand_model = model('brand');
  111. if (request()->isPost()) {
  112. $data = [
  113. 'brand_name' => input('post.brand_name'), 'brand_initial' => input('post.brand_initial'),
  114. 'brand_sort' => input('post.brand_sort')
  115. ];
  116. $brand_validate = ds_validate('brand');
  117. if (!$brand_validate->scene('brand_edit')->check($data)) {
  118. $this->error($brand_validate->getError());
  119. } else {
  120. if (!empty($_FILES['_pic']['name'])) {
  121. $res = ds_upload_pic(ATTACH_BRAND, '_pic');
  122. if ($res['code']) {
  123. $brand_pic = $res['data']['file_name'];
  124. } else {
  125. $this->error($res['msg']);
  126. }
  127. }
  128. $brand_info = $brand_model->getBrandInfo(array('brand_id' => intval(input('post.brand_id'))));
  129. $condition = array();
  130. $condition[] = array('brand_id', '=', intval(input('post.brand_id')));
  131. $update_array = array();
  132. $update_array['brand_name'] = trim(input('post.brand_name'));
  133. $update_array['brand_initial'] = strtoupper(input('post.brand_initial'));
  134. $update_array['gc_id'] = input('post.class_id');
  135. $update_array['brand_class'] = trim(input('post.brand_class'));
  136. if (!empty($brand_pic)) {
  137. $update_array['brand_pic'] = $brand_pic;
  138. }
  139. $update_array['brand_recommend'] = intval(input('post.brand_recommend'));
  140. $update_array['brand_sort'] = intval(input('post.brand_sort'));
  141. $update_array['brand_showtype'] = intval(input('post.brand_showtype')) == 1 ? 1 : 0;
  142. $result = $brand_model->editBrand($condition, $update_array);
  143. if ($result >= 0) {
  144. if (!empty(input('post.brand_pic')) && !empty($brand_info['brand_pic'])) {
  145. @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_BRAND . DIRECTORY_SEPARATOR . $brand_info['brand_pic']);
  146. }
  147. $this->log(lang('ds_edit') . lang('brand_index_brand') . '[' . input('post.brand_name') . ']', 1);
  148. dsLayerOpenSuccess(lang('ds_common_save_succ'));
  149. } else {
  150. $this->log(lang('ds_edit') . lang('brand_index_brand') . '[' . input('post.brand_name') . ']', 0);
  151. $this->error(lang('ds_common_save_fail'));
  152. }
  153. }
  154. } else {
  155. $brand_info = $brand_model->getBrandInfo(array('brand_id' => intval(input('param.brand_id'))));
  156. if (empty($brand_info)) {
  157. $this->error(lang('param_error'));
  158. }
  159. View::assign('brand_array', $brand_info);
  160. // 一级商品分类
  161. $gc_list = model('goodsclass')->getGoodsclassListByParentId(0);
  162. View::assign('gc_list', $gc_list);
  163. return View::fetch('form');
  164. }
  165. }
  166. /**
  167. * 删除品牌
  168. */
  169. public function brand_del()
  170. {
  171. $brand_id = input('param.brand_id');
  172. $brand_id_array = ds_delete_param($brand_id);
  173. if ($brand_id_array == FALSE) {
  174. $this->log(lang('ds_del') . lang('brand_index_brand') . '[ID:' . $brand_id . ']', 0);
  175. ds_json_encode(10001, lang('param_error'));
  176. }
  177. $brand_mod = model('brand');
  178. $condition = array();
  179. $condition[] = array('brand_id', 'in', $brand_id_array);
  180. $brand_mod->delBrand($condition);
  181. $this->log(lang('ds_del') . lang('brand_index_brand') . '[ID:' . $brand_id . ']', 1);
  182. ds_json_encode(10000, lang('ds_common_del_succ'));
  183. }
  184. /**
  185. * 品牌申请
  186. */
  187. public function brand_apply()
  188. {
  189. $brand_model = model('brand');
  190. /**
  191. * 对申请品牌进行操作 通过,拒绝
  192. */
  193. if (request()->isPost()) {
  194. $del_id_array = input('post.del_id/a'); #获取数组
  195. if (!empty($del_id_array)) {
  196. switch (input('post.type')) {
  197. case 'pass':
  198. //更新品牌 申请状态
  199. $brandid_array = array();
  200. foreach ($del_id_array as $v) {
  201. $brandid_array[] = intval($v);
  202. }
  203. $update_array = array();
  204. $update_array['brand_apply'] = 1;
  205. $condition = array();
  206. $condition[] = array('brand_id', 'in', $brandid_array);
  207. $brand_model->editBrand($condition, $update_array);
  208. $this->log(lang('brand_apply_pass') . '[ID:' . implode(',', $brandid_array) . ']', null);
  209. $this->success(lang('brand_apply_passed'));
  210. break;
  211. case 'refuse':
  212. //删除该品牌
  213. $brandid_array = array();
  214. foreach ($del_id_array as $v) {
  215. $brandid_array[] = intval($v);
  216. }
  217. $condition = array();
  218. $condition[] = array('brand_id', 'in', $brandid_array);
  219. $brand_model->delBrand($condition);
  220. $this->log(lang('ds_del') . lang('brand_index_brand') . '[ID:' . implode(',', $del_id_array) . ']', 1);
  221. $this->success(lang('ds_common_del_succ'));
  222. break;
  223. default:
  224. $this->success(lang('brand_apply_invalid_argument'));
  225. }
  226. } else {
  227. $this->log(lang('ds_del') . lang('brand_index_brand'), 0);
  228. $this->error(lang('ds_common_del_fail'));
  229. }
  230. } else {
  231. /**
  232. * 检索条件
  233. */
  234. $condition = array();
  235. if (!empty(input('param.search_brand_name'))) {
  236. $condition[] = array('brand_name', 'like', '%' . trim(input('param.search_brand_name')) . '%');
  237. }
  238. if (!empty(input('param.search_brand_class'))) {
  239. $condition[] = array('brand_class', 'like', '%' . trim(input('param.search_brand_class')) . '%');
  240. }
  241. $brand_list = $brand_model->getBrandNoPassedList($condition, '*', 10);
  242. View::assign('brand_list', $brand_list);
  243. View::assign('show_page', $brand_model->page_info->render());
  244. View::assign('search_brand_name', trim(input('param.search_brand_name')));
  245. View::assign('search_brand_class', trim(input('param.search_brand_class')));
  246. View::assign('filtered', $condition ? 1 : 0); //是否有查询条件
  247. $this->setAdminCurItem('brand_apply');
  248. return View::fetch('brand_apply');
  249. }
  250. }
  251. /**
  252. * 审核 申请品牌操作
  253. */
  254. public function brand_apply_set()
  255. {
  256. $brand_model = model('brand');
  257. if (intval(input('param.brand_id')) > 0) {
  258. switch (input('param.state')) {
  259. case 'pass':
  260. /**
  261. * 更新品牌 申请状态
  262. */
  263. $update_array = array();
  264. $update_array['brand_apply'] = 1;
  265. $result = $brand_model->editBrand(array('brand_id' => intval(input('param.brand_id'))), $update_array);
  266. if ($result) {
  267. $this->log(lang('brand_apply_pass') . '[ID:' . intval(input('param.brand_id')) . ']', null);
  268. $this->success(lang('brand_apply_pass'));
  269. } else {
  270. $this->log(lang('brand_apply_fail') . '[ID:' . intval(input('param.brand_id')) . ')', 0);
  271. $this->error(lang('brand_apply_fail'));
  272. }
  273. break;
  274. case 'refuse':
  275. // 删除
  276. $brand_model->delBrand(array('brand_id' => intval(input('param.brand_id'))));
  277. $this->log(lang('ds_del') . lang('brand_index_brand') . '[ID:' . intval(input('param.brand_id')) . ']', 1);
  278. $this->success(lang('ds_common_del_succ'));
  279. break;
  280. default:
  281. $this->error(lang('brand_apply_paramerror'));
  282. }
  283. } else {
  284. $this->log(lang('ds_del') . lang('brand_index_brand') . '[ID:' . intval(input('param.brand_id')) . ']', 0);
  285. $this->error(lang('brand_apply_brandparamerror'));
  286. }
  287. }
  288. /**
  289. * ajax操作
  290. */
  291. public function ajax()
  292. {
  293. $brand_model = model('brand');
  294. switch (input('param.branch')) {
  295. /**
  296. * 品牌名称
  297. */
  298. case 'brand_name':
  299. /**
  300. * 判断是否有重复
  301. */
  302. $condition[] = array('brand_name', '=', trim(input('param.value')));
  303. $condition[] = array('brand_id', '<>', intval(input('param.id')));
  304. $result = $brand_model->getBrandList($condition);
  305. if (empty($result)) {
  306. $brand_model->editBrand(array('brand_id' => intval(input('param.id'))), array('brand_name' => trim(input('param.value'))));
  307. $this->log(lang('ds_edit') . lang('brand_index_name') . '[' . input('param.value') . ']', 1);
  308. echo 'true';
  309. exit;
  310. } else {
  311. echo 'false';
  312. exit;
  313. }
  314. break;
  315. /**
  316. * 品牌类别,品牌排序,推荐
  317. */
  318. case 'brand_class':
  319. case 'brand_sort':
  320. case 'brand_recommend':
  321. $brand_model->editBrand(array('brand_id' => intval(input('param.id'))), array(input('param.column') => trim(input('param.value'))));
  322. $detail_log = str_replace(array(
  323. 'brand_class', 'brand_sort', 'brand_recommend'
  324. ), array(
  325. lang('brand_index_class'), lang('ds_sort'), lang('ds_recommend')
  326. ), input('param.branch'));
  327. $this->log(lang('ds_edit') . lang('brand_index_brand') . $detail_log . '[ID:' . intval(input('param.id')) . ')', 1);
  328. echo 'true';
  329. exit;
  330. break;
  331. /**
  332. * 验证品牌名称是否有重复
  333. */
  334. case 'check_brand_name':
  335. $condition[] = array('brand_name', '=', trim(input('param.brand_name')));
  336. $condition[] = array('brand_id', '<>', intval(input('param.id')));
  337. $result = $brand_model->getBrandList($condition);
  338. if (empty($result)) {
  339. echo 'true';
  340. exit;
  341. } else {
  342. echo 'false';
  343. exit;
  344. }
  345. break;
  346. }
  347. }
  348. /**
  349. * 品牌导出第一步
  350. */
  351. public function export_step1()
  352. {
  353. $brand_model = model('brand');
  354. $condition = array();
  355. if ((input('param.search_brand_name'))) {
  356. $condition[] = array('brand_name', 'like', "%{input('param.search_brand_name')}%");
  357. }
  358. if ((input('param.search_brand_class'))) {
  359. $condition[] = array('brand_class', 'like', "%{input('param.search_brand_class')}%");
  360. }
  361. $condition[] = array('brand_apply', '=', '1');
  362. if (!is_numeric(input('param.page'))) {
  363. $count = $brand_model->getBrandCount($condition);
  364. $export_list = array();
  365. if ($count > self::EXPORT_SIZE) { //显示下载链接
  366. $page = ceil($count / self::EXPORT_SIZE);
  367. for ($i = 1; $i <= $page; $i++) {
  368. $limit1 = ($i - 1) * self::EXPORT_SIZE + 1;
  369. $limit2 = $i * self::EXPORT_SIZE > $count ? $count : $i * self::EXPORT_SIZE;
  370. $export_list[$i] = $limit1 . ' ~ ' . $limit2;
  371. }
  372. View::assign('export_list', $export_list);
  373. return View::fetch('export_excel');
  374. } else { //如果数量小,直接下载
  375. $data = $brand_model->getBrandList($condition, '*', self::EXPORT_SIZE, 'brand_id desc');
  376. $this->createExcel($data);
  377. }
  378. } else { //下载
  379. $limit1 = (input('param.page') - 1) * self::EXPORT_SIZE;
  380. $limit2 = self::EXPORT_SIZE;
  381. $data = $brand_model->getBrandList($condition, '*', $limit2, 'brand_id desc');
  382. $this->createExcel($data);
  383. }
  384. }
  385. /**
  386. * 生成excel
  387. *
  388. * @param array $data
  389. */
  390. private function createExcel($data = array())
  391. {
  392. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/export.lang.php');
  393. $excel_obj = new \excel\Excel();
  394. $excel_data = array();
  395. //设置样式
  396. $excel_obj->setStyle(array(
  397. 'id' => 's_title', 'Font' => array('FontName' => lang('ds_song_typeface'), 'Size' => '12', 'Bold' => '1')
  398. ));
  399. //header
  400. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_brandid'));
  401. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_brand'));
  402. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_brand_cate'));
  403. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_brand_img'));
  404. foreach ((array) $data as $k => $v) {
  405. $tmp = array();
  406. $tmp[] = array('data' => $v['brand_id']);
  407. $tmp[] = array('data' => $v['brand_name']);
  408. $tmp[] = array('data' => $v['brand_class']);
  409. $tmp[] = array('data' => $v['brand_pic']);
  410. $excel_data[] = $tmp;
  411. }
  412. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  413. $excel_obj->addArray($excel_data);
  414. $excel_obj->addWorksheet($excel_obj->charset(lang('exp_brand'), CHARSET));
  415. $excel_obj->generateXML($excel_obj->charset(lang('exp_brand'), CHARSET) . input('param.page') . '-' . date('Y-m-d-H', TIMESTAMP));
  416. }
  417. /**
  418. * 获取卖家栏目列表,针对控制器下的栏目
  419. */
  420. protected function getAdminItemList()
  421. {
  422. $menu_array = array(
  423. array(
  424. 'name' => 'index',
  425. 'text' => lang('ds_manage'),
  426. 'url' => (string) url('Brand/index'),
  427. ),
  428. array(
  429. 'name' => 'brand_add',
  430. 'text' => lang('ds_add'),
  431. 'url' => "javascript:dsLayerOpen('" . (string) url('Brand/brand_add') . "','" . lang('ds_add') . "')"
  432. ),
  433. array(
  434. 'name' => 'brand_apply',
  435. 'text' => lang('brand_index_to_audit'),
  436. 'url' => (string) url('Brand/brand_apply')
  437. )
  438. );
  439. return $menu_array;
  440. }
  441. }