Brand.php 20 KB

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