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