Brand.php 20 KB

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