Pointprod.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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 Pointprod extends AdminControl
  13. {
  14. public function initialize()
  15. {
  16. parent::initialize(); // TODO: Change the autogenerated stub
  17. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/pointprod.lang.php');
  18. }
  19. /**
  20. * 积分礼品列表
  21. */
  22. public function index()
  23. {
  24. $pointprod_model = model('pointprod');
  25. //获得兑换商品的上下架状态
  26. $pgoodsshowstate_arr = $pointprod_model->getPgoodsShowState();
  27. //获得兑换商品的推荐状态
  28. $pgoodsrecommendstate_arr = $pointprod_model->getPgoodsRecommendState();
  29. //条件
  30. $where = array();
  31. $pgoods_name = trim(input('param.pg_name'));
  32. if ($pgoods_name) {
  33. $where[] = array('pgoods_name', 'like', "%{$pgoods_name}%");
  34. }
  35. switch (trim(input('param.pg_state'))) {
  36. case 'show':
  37. $where[] = array('pgoods_show', '=', $pgoodsshowstate_arr['show'][0]);
  38. break;
  39. case 'nshow':
  40. $where[] = array('pgoods_show', '=', $pgoodsshowstate_arr['unshow'][0]);
  41. break;
  42. case 'commend':
  43. $where[] = array('pgoods_commend', '=', $pgoodsrecommendstate_arr['commend'][0]);
  44. break;
  45. }
  46. $prod_list = $pointprod_model->getPointProdList($where, '*', 'pgoods_sort asc,pgoods_id desc', 0, 10);
  47. //信息输出
  48. View::assign('prod_list', $prod_list);
  49. View::assign('show_page', $pointprod_model->page_info->render());
  50. $this->setAdminCurItem('index');
  51. return View::fetch();
  52. }
  53. /**
  54. * 积分礼品添加
  55. */
  56. public function prod_add()
  57. {
  58. $hourarr = array(
  59. '00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17',
  60. '18', '19', '20', '21', '22', '23'
  61. );
  62. $upload_model = model('upload');
  63. if (request()->isPost()) {
  64. //验证表单
  65. $data = [
  66. 'goodsname' => input('post.goodsname'), 'goodsprice' => input('post.goodsprice'),
  67. 'goodspoints' => input('post.goodspoints'), 'goodsserial' => input('post.goodsserial'),
  68. 'goodsstorage' => input('post.goodsstorage'), 'sort' => input('post.sort'),
  69. ];
  70. if (input('post.sort') == 1) {
  71. $data['limitnum'] = input('postlimitnumsort');
  72. }
  73. if (input('post.islimittime')) {
  74. $data['starttime'] = input('post.starttime');
  75. $data['endtime'] = input('post.endtime');
  76. }
  77. $point_validate = ds_validate('point');
  78. if (!$point_validate->scene('prod_add')->check($data)) {
  79. $this->error($point_validate->getError());
  80. }
  81. $pointprod_model = model('pointprod');
  82. $goods_array = array();
  83. $goods_array['pgoods_name'] = trim(input('post.goodsname'));
  84. $goods_array['pgoods_tag'] = trim(input('post.goodstag'));
  85. $goods_array['pgoods_price'] = trim(input('post.goodsprice'));
  86. $goods_array['pgoods_points'] = trim(input('post.goodspoints'));
  87. $goods_array['pgoods_serial'] = trim(input('post.goodsserial'));
  88. $goods_array['pgoods_storage'] = intval(input('post.goodsstorage'));
  89. $goods_array['pgoods_islimit'] = intval(input('post.islimit'));
  90. if ($goods_array['pgoods_islimit'] == 1) {
  91. $goods_array['pgoods_limitnum'] = intval(input('post.limitnum'));
  92. } else {
  93. $goods_array['pgoods_limitnum'] = 0;
  94. }
  95. $goods_array['pgoods_islimittime'] = intval(input('post.islimittime'));
  96. if ($goods_array['pgoods_islimittime'] == 1) {
  97. //如果添加了开始时间
  98. if (trim(input('post.starttime'))) {
  99. $starttime = trim(input('post.starttime'));
  100. $sdatearr = explode('-', $starttime);
  101. $starttime = mktime(intval(input('post.starthour')), 0, 0, $sdatearr[1], $sdatearr[2], $sdatearr[0]);
  102. unset($sdatearr);
  103. }
  104. if (trim(input('post.endtime'))) {
  105. $endtime = trim(input('post.endtime'));
  106. $edatearr = explode('-', $endtime);
  107. $endtime = mktime(intval(input('post.endhour')), 0, 0, $edatearr[1], $edatearr[2], $edatearr[0]);
  108. }
  109. $goods_array['pgoods_starttime'] = $starttime;
  110. $goods_array['pgoods_endtime'] = $endtime;
  111. } else {
  112. $goods_array['pgoods_starttime'] = '';
  113. $goods_array['pgoods_endtime'] = '';
  114. }
  115. $goods_array['pgoods_show'] = trim(input('post.showstate'));
  116. $goods_array['pgoods_commend'] = trim(input('post.commendstate'));
  117. $goods_array['pgoods_addtime'] = TIMESTAMP;
  118. $goods_array['pgoods_close_reason'] = trim(input('post.forbidreason'));
  119. $goods_array['pgoods_keywords'] = trim(input('post.keywords'));
  120. $goods_array['pgoods_description'] = trim(input('post.description'));
  121. $goods_array['pgoods_body'] = trim(input('post.pgoods_body'));
  122. $goods_array['pgoods_sort'] = intval(input('post.sort'));
  123. $goods_array['pgoods_limitmgrade'] = intval(input('post.limitgrade'));
  124. //添加礼品代表图片
  125. $indeximg_succ = false;
  126. if (!empty($_FILES['goods_images']['name'])) {
  127. $upload_file = BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_POINTPROD;
  128. $res = ds_upload_pic(ATTACH_POINTPROD, 'goods_images');
  129. if ($res['code']) {
  130. $file_name = $res['data']['file_name'];
  131. $indeximg_succ = true;
  132. $goods_array['pgoods_image'] = $file_name;
  133. ds_create_thumb($upload_file, $file_name, '60,240', '60,240', '_small,_normal');
  134. } else {
  135. $this->error($res['msg']);
  136. }
  137. }
  138. $state = $pointprod_model->addPointgoods($goods_array);
  139. if ($state) {
  140. //礼品代表图片数据入库
  141. if ($indeximg_succ) {
  142. $insert_array = array();
  143. $insert_array['file_name'] = $file_name;
  144. $insert_array['upload_type'] = 5;
  145. $insert_array['file_size'] = (config('ds_config.upload_type') == 'alioss') ? 0 : filesize($upload_file . DIRECTORY_SEPARATOR . $file_name);
  146. $insert_array['item_id'] = $state;
  147. $insert_array['upload_time'] = TIMESTAMP;
  148. $upload_model->addUpload($insert_array);
  149. }
  150. //更新积分礼品描述图片
  151. $file_idstr = '';
  152. $condition = array();
  153. $condition[] = array('upload_type', '=', 6);
  154. $condition[] = array('item_id', '=', 0);
  155. $file_id_array = input('post.file_id/a');
  156. if (is_array($file_id_array) && count($file_id_array) > 0) {
  157. $condition[] = array('upload_id', 'in', $file_id_array);
  158. }
  159. $upload_model->editUpload(array('item_id' => $state), $condition);
  160. $this->log(lang('admin_pointprod_add_title') . '[' . input('post.goodsname') . ']');
  161. $this->success(lang('admin_pointprod_add_success'), 'pointprod/index');
  162. }
  163. }
  164. //模型实例化
  165. $condition = array();
  166. $condition[] = array('upload_type', '=', '6');
  167. $condition[] = array('item_id', '=', '0');
  168. $file_upload = $upload_model->getUploadList($condition);
  169. View::assign('file_upload', $file_upload);
  170. View::assign('PHPSESSID', session_id());
  171. $hourarr = array(
  172. '00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17',
  173. '18', '19', '20', '21', '22', '23'
  174. );
  175. View::assign('hourarr', $hourarr);
  176. //会员级别
  177. $member_grade = model('member')->getMemberGradeArr();
  178. View::assign('member_grade', $member_grade);
  179. $this->setAdminCurItem('prod_add');
  180. return View::fetch();
  181. }
  182. /**
  183. * 积分礼品编辑
  184. */
  185. public function prod_edit()
  186. {
  187. $hourarr = array(
  188. '00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17',
  189. '18', '19', '20', '21', '22', '23'
  190. );
  191. $upload_model = model('upload');
  192. $pg_id = intval(input('param.pg_id'));
  193. if (!$pg_id) {
  194. $this->error(lang('admin_pointprod_parameter_error'), 'pointprod/index');
  195. }
  196. $pointprod_model = model('pointprod');
  197. //查询礼品记录是否存在
  198. $prod_info = $pointprod_model->getPointProdInfo(array('pgoods_id' => $pg_id));
  199. if (!$prod_info) {
  200. $this->error(lang('admin_pointprod_record_error'), 'pointprod/index');
  201. }
  202. if (request()->isPost()) {
  203. //验证表单
  204. $data = [
  205. 'goodsname' => input('post.goodsname'), 'goodsprice' => input('post.goodsprice'),
  206. 'goodspoints' => input('post.goodspoints'), 'goodsserial' => input('post.goodsserial'),
  207. 'goodsstorage' => input('post.goodsstorage'), 'sort' => input('post.sort'),
  208. ];
  209. if (input('post.islimit') == 1) {
  210. $data['limitnum'] = input('post.limitnum');
  211. }
  212. if (input('post.islimittime')) {
  213. $data['starttime'] = input('post.starttime');
  214. $data['endtime'] = input('post.endtime');
  215. }
  216. $point_validate = ds_validate('point');
  217. if (!$point_validate->scene('prod_edit')->check($data)) {
  218. $this->error($point_validate->getError());
  219. }
  220. //实例化店铺商品模型
  221. $pointprod_model = model('pointprod');
  222. $goods_array = array();
  223. $goods_array['pgoods_name'] = trim(input('post.goodsname'));
  224. $goods_array['pgoods_tag'] = trim(input('post.goodstag'));
  225. $goods_array['pgoods_price'] = trim(input('post.goodsprice'));
  226. $goods_array['pgoods_points'] = trim(input('post.goodspoints'));
  227. $goods_array['pgoods_serial'] = trim(input('post.goodsserial'));
  228. $goods_array['pgoods_storage'] = intval(input('post.goodsstorage'));
  229. $goods_array['pgoods_islimit'] = intval(input('post.islimit'));
  230. if ($goods_array['pgoods_islimit'] == 1) {
  231. $goods_array['pgoods_limitnum'] = intval(input('post.limitnum'));
  232. } else {
  233. $goods_array['pgoods_limitnum'] = 0;
  234. }
  235. $goods_array['pgoods_islimittime'] = intval(input('post.islimittime'));
  236. if ($goods_array['pgoods_islimittime'] == 1) {
  237. //如果添加了开始时间
  238. if (trim(input('post.starttime'))) {
  239. $starttime = trim(input('post.starttime'));
  240. $sdatearr = explode('-', $starttime);
  241. $starttime = mktime(intval(input('post.starthour')), 0, 0, $sdatearr[1], $sdatearr[2], $sdatearr[0]);
  242. unset($sdatearr);
  243. }
  244. if (trim(input('post.endtime'))) {
  245. $endtime = trim(input('post.endtime'));
  246. $edatearr = explode('-', $endtime);
  247. $endtime = mktime(intval(input('post.endhour')), 0, 0, $edatearr[1], $edatearr[2], $edatearr[0]);
  248. }
  249. $goods_array['pgoods_starttime'] = $starttime;
  250. $goods_array['pgoods_endtime'] = $endtime;
  251. } else {
  252. $goods_array['pgoods_starttime'] = '';
  253. $goods_array['pgoods_endtime'] = '';
  254. }
  255. $goods_array['pgoods_show'] = trim(input('post.showstate'));
  256. $goods_array['pgoods_commend'] = trim(input('post.commendstate'));
  257. $goods_array['pgoods_close_reason'] = trim(input('post.forbidreason'));
  258. $goods_array['pgoods_keywords'] = trim(input('post.keywords'));
  259. $goods_array['pgoods_description'] = trim(input('post.description'));
  260. $goods_array['pgoods_body'] = trim(input('post.pgoods_body'));
  261. $goods_array['pgoods_sort'] = intval(input('post.sort'));
  262. $goods_array['pgoods_limitmgrade'] = intval(input('post.limitgrade'));
  263. //添加礼品代表图片
  264. $indeximg_succ = false;
  265. if (!empty($_FILES['goods_images']['name'])) {
  266. $upload_file = BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_POINTPROD;
  267. $res = ds_upload_pic(ATTACH_POINTPROD, 'goods_images');
  268. if ($res['code']) {
  269. $file_name = $res['data']['file_name'];
  270. $indeximg_succ = true;
  271. $goods_array['pgoods_image'] = $file_name;
  272. ds_create_thumb($upload_file, $file_name, '60,240', '60,240', '_small,_normal');
  273. } else {
  274. $this->error($res['msg']);
  275. }
  276. }
  277. $state = $pointprod_model->editPointProd($goods_array, array('pgoods_id' => $prod_info['pgoods_id']));
  278. if ($state) {
  279. //礼品代表图片数据入库
  280. if ($indeximg_succ) {
  281. //删除原有图片
  282. $upload_list = $upload_model->getUploadList(array('upload_type' => 5, 'item_id' => $prod_info['pgoods_id']));
  283. if (is_array($upload_list) && count($upload_list) > 0) {
  284. $upload_idarr = array();
  285. foreach ($upload_list as $v) {
  286. //批量删除图片
  287. ds_unlink($upload_file, $v['file_name']);
  288. $upload_idarr[] = $v['upload_id'];
  289. }
  290. //删除图片
  291. $condition = array();
  292. $condition[] = array('upload_id', 'in', $upload_idarr);
  293. $upload_model->delUpload($condition);
  294. }
  295. $insert_array = array();
  296. $insert_array['file_name'] = $file_name;
  297. $insert_array['upload_type'] = 5;
  298. $insert_array['file_size'] = (config('ds_config.upload_type') == 'alioss') ? 0 : filesize($upload_file . DIRECTORY_SEPARATOR . $file_name);
  299. $insert_array['item_id'] = $prod_info['pgoods_id'];
  300. $insert_array['upload_time'] = TIMESTAMP;
  301. $upload_model->addUpload($insert_array);
  302. }
  303. //更新积分礼品描述图片
  304. $file_idstr = '';
  305. $condition = array();
  306. $condition[] = array('upload_type', '=', 6);
  307. $condition[] = array('item_id', '=', 0);
  308. $file_id_array = input('post.file_id/a');
  309. if (is_array($file_id_array) && count($file_id_array) > 0) {
  310. $file_idstr = "'" . implode("','", $file_id_array) . "'";
  311. $condition[] = array('upload_id', 'in', $file_idstr);
  312. }
  313. $upload_model->editUpload(array('item_id' => $prod_info['pgoods_id']), $condition);
  314. $this->log(lang('ds_edit') . lang('admin_pointprodp') . '[' . input('post.goodsname') . ']');
  315. $this->success(lang('admin_pointprod_edit_success'), 'pointprod/index');
  316. }
  317. $this->error(lang('admin_pointprod_edit_fail'));
  318. } else {
  319. $condition = array();
  320. $condition[] = array('upload_type', '=', '6');
  321. $condition[] = array('item_id', '=', $pg_id);
  322. $file_upload = $upload_model->getUploadList($condition);
  323. //会员级别
  324. $member_grade = model('member')->getMemberGradeArr();
  325. View::assign('member_grade', $member_grade);
  326. View::assign('file_upload', $file_upload);
  327. View::assign('PHPSESSID', session_id());
  328. View::assign('hourarr', $hourarr);
  329. View::assign('prod_info', $prod_info);
  330. $this->setAdminCurItem('prod_edit');
  331. return View::fetch();
  332. }
  333. }
  334. /**
  335. * 删除积分礼品
  336. */
  337. public function prod_drop()
  338. {
  339. $pg_id = input('param.pg_id');
  340. $pg_id_array = ds_delete_param($pg_id);
  341. if ($pg_id_array === FALSE) {
  342. ds_json_encode('10001', lang('admin_pointprod_parameter_error'));
  343. }
  344. $pointprod_model = model('pointprod');
  345. $result = $pointprod_model->delPointProdById($pg_id_array);
  346. if ($result) {
  347. $this->log(lang('ds_del') . lang('admin_pointprodp') . '[ID:' . $pg_id . ']');
  348. ds_json_encode('10000', lang('admin_pointprod_del_success'));
  349. } else {
  350. ds_json_encode('10001', lang('admin_pointprod_del_fail'));
  351. }
  352. }
  353. /**
  354. * 积分礼品异步状态修改
  355. */
  356. public function ajax()
  357. {
  358. //礼品上架,礼品推荐,礼品禁售
  359. $id = intval(input('param.id'));
  360. if ($id <= 0) {
  361. echo 'false';
  362. exit;
  363. }
  364. $pointprod_model = model('pointprod');
  365. $update_array = array();
  366. $update_array[input('param.column')] = trim(input('param.value'));
  367. $pointprod_model->editPointProd($update_array, array('pgoods_id' => $id));
  368. echo 'true';
  369. exit;
  370. }
  371. /**
  372. * 积分礼品上传
  373. */
  374. public function pointprod_pic_upload()
  375. {
  376. /**
  377. * 上传图片
  378. */
  379. $file_name = '';
  380. $file_object = request()->file('fileupload');
  381. if ($file_object) {
  382. $res = ds_upload_pic(ATTACH_POINTPROD, 'fileupload');
  383. if ($res['code']) {
  384. $file_name = $res['data']['file_name'];
  385. } else {
  386. echo $res['msg'];
  387. exit;
  388. }
  389. } else {
  390. echo 'error';
  391. exit;
  392. }
  393. /**
  394. * 模型实例化
  395. */
  396. $upload_model = model('upload');
  397. /**
  398. * 图片数据入库
  399. */
  400. $insert_array = array();
  401. $insert_array['file_name'] = $file_name;
  402. $insert_array['upload_type'] = '6';
  403. $insert_array['file_size'] = $_FILES['fileupload']['size'];
  404. $insert_array['upload_time'] = TIMESTAMP;
  405. $insert_array['item_id'] = input('param.item_id', 0);
  406. $result = $upload_model->addUpload($insert_array);
  407. if ($result) {
  408. $data = array();
  409. $data['file_id'] = $result;
  410. $data['file_name'] = $file_name;
  411. $data['file_path'] = ds_get_pic(ATTACH_POINTPROD, $file_name);
  412. /**
  413. * 整理为json格式
  414. */
  415. $output = json_encode($data);
  416. echo $output;
  417. }
  418. }
  419. /**
  420. * ajax操作删除已上传图片
  421. */
  422. public function ajaxdelupload()
  423. {
  424. //删除文章图片
  425. if (intval(input('param.file_id')) > 0) {
  426. $upload_model = model('upload');
  427. /**
  428. * 删除图片
  429. */
  430. $file_array = $upload_model->getOneUpload(intval(input('param.file_id')));
  431. @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_POINTPROD . DIRECTORY_SEPARATOR . $file_array['file_name']);
  432. /**
  433. * 删除信息
  434. */
  435. $condition = array();
  436. $condition[] = array('upload_id', '=', intval(input('param.file_id')));
  437. $upload_model->delUpload($condition);
  438. echo 'true';
  439. exit;
  440. } else {
  441. echo 'false';
  442. exit;
  443. }
  444. }
  445. protected function getAdminItemList()
  446. {
  447. $menu_array = array(
  448. array(
  449. 'name' => 'index', 'text' => lang('admin_pointprod_list_title'), 'url' => (string) url('Pointprod/index')
  450. ), array(
  451. 'name' => 'prod_add', 'text' => lang('admin_pointprod_add_title'), 'url' => (string) url('Pointprod/prod_add')
  452. ),
  453. );
  454. if (request()->action() == 'prod_edit') {
  455. $menu_array[] = array(
  456. 'name' => 'prod_edit', 'text' => lang('admin_pointprod_edit_title'), 'url' => 'javascript:void()'
  457. );
  458. }
  459. return $menu_array;
  460. }
  461. }