Pointprod.php 21 KB

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