Pointprod.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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 Pointprod extends AdminControl {
  18. public function initialize() {
  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. $pointprod_model = model('pointprod');
  27. //获得兑换商品的上下架状态
  28. $pgoodsshowstate_arr = $pointprod_model->getPgoodsShowState();
  29. //获得兑换商品的推荐状态
  30. $pgoodsrecommendstate_arr = $pointprod_model->getPgoodsRecommendState();
  31. //条件
  32. $where = array();
  33. $pgoods_name = trim(input('param.pg_name'));
  34. if ($pgoods_name) {
  35. $where[] = array('pgoods_name', 'like', "%{$pgoods_name}%");
  36. }
  37. switch (trim(input('param.pg_state'))) {
  38. case 'show':
  39. $where[] = array('pgoods_show', '=', $pgoodsshowstate_arr['show'][0]);
  40. break;
  41. case 'nshow':
  42. $where[] = array('pgoods_show', '=', $pgoodsshowstate_arr['unshow'][0]);
  43. break;
  44. case 'commend':
  45. $where[] = array('pgoods_commend', '=', $pgoodsrecommendstate_arr['commend'][0]);
  46. break;
  47. }
  48. $prod_list = $pointprod_model->getPointProdList($where, '*', 'pgoods_sort asc,pgoods_id desc', 0, 10);
  49. //信息输出
  50. View::assign('prod_list', $prod_list);
  51. View::assign('show_page', $pointprod_model->page_info->render());
  52. $this->setAdminCurItem('index');
  53. return View::fetch();
  54. }
  55. /**
  56. * 积分礼品添加
  57. */
  58. public function prod_add() {
  59. $hourarr = array(
  60. '00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17',
  61. '18', '19', '20', '21', '22', '23'
  62. );
  63. $upload_model = model('upload');
  64. if (request()->isPost()) {
  65. //验证表单
  66. $data = [
  67. 'goodsname' => input('post.goodsname'), 'goodsprice' => input('post.goodsprice'),
  68. 'goodspoints' => input('post.goodspoints'), 'goodsserial' => input('post.goodsserial'),
  69. 'goodsstorage' => input('post.goodsstorage'), 'sort' => input('post.sort'),
  70. ];
  71. if (input('post.sort') == 1) {
  72. $data['limitnum'] = input('postlimitnumsort');
  73. }
  74. if (input('post.islimittime')) {
  75. $data['starttime'] = input('post.starttime');
  76. $data['endtime'] = input('post.endtime');
  77. }
  78. $point_validate = ds_validate('point');
  79. if (!$point_validate->scene('prod_add')->check($data)) {
  80. $this->error($point_validate->getError());
  81. }
  82. $pointprod_model = model('pointprod');
  83. $goods_array = array();
  84. $goods_array['pgoods_name'] = trim(input('post.goodsname'));
  85. $goods_array['pgoods_tag'] = trim(input('post.goodstag'));
  86. $goods_array['pgoods_price'] = trim(input('post.goodsprice'));
  87. $goods_array['pgoods_points'] = trim(input('post.goodspoints'));
  88. $goods_array['pgoods_serial'] = trim(input('post.goodsserial'));
  89. $goods_array['pgoods_storage'] = intval(input('post.goodsstorage'));
  90. $goods_array['pgoods_islimit'] = intval(input('post.islimit'));
  91. if ($goods_array['pgoods_islimit'] == 1) {
  92. $goods_array['pgoods_limitnum'] = intval(input('post.limitnum'));
  93. } else {
  94. $goods_array['pgoods_limitnum'] = 0;
  95. }
  96. $goods_array['pgoods_islimittime'] = intval(input('post.islimittime'));
  97. if ($goods_array['pgoods_islimittime'] == 1) {
  98. //如果添加了开始时间
  99. if (trim(input('post.starttime'))) {
  100. $starttime = trim(input('post.starttime'));
  101. $sdatearr = explode('-', $starttime);
  102. $starttime = mktime(intval(input('post.starthour')), 0, 0, $sdatearr[1], $sdatearr[2], $sdatearr[0]);
  103. unset($sdatearr);
  104. }
  105. if (trim(input('post.endtime'))) {
  106. $endtime = trim(input('post.endtime'));
  107. $edatearr = explode('-', $endtime);
  108. $endtime = mktime(intval(input('post.endhour')), 0, 0, $edatearr[1], $edatearr[2], $edatearr[0]);
  109. }
  110. $goods_array['pgoods_starttime'] = $starttime;
  111. $goods_array['pgoods_endtime'] = $endtime;
  112. } else {
  113. $goods_array['pgoods_starttime'] = '';
  114. $goods_array['pgoods_endtime'] = '';
  115. }
  116. $goods_array['pgoods_show'] = trim(input('post.showstate'));
  117. $goods_array['pgoods_commend'] = trim(input('post.commendstate'));
  118. $goods_array['pgoods_addtime'] = TIMESTAMP;
  119. $goods_array['pgoods_close_reason'] = trim(input('post.forbidreason'));
  120. $goods_array['pgoods_keywords'] = trim(input('post.keywords'));
  121. $goods_array['pgoods_description'] = trim(input('post.description'));
  122. $goods_array['pgoods_body'] = trim(input('post.pgoods_body'));
  123. $goods_array['pgoods_sort'] = intval(input('post.sort'));
  124. $goods_array['pgoods_limitmgrade'] = intval(input('post.limitgrade'));
  125. //添加礼品代表图片
  126. $indeximg_succ = false;
  127. if (!empty($_FILES['goods_images']['name'])) {
  128. $upload_file = BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_POINTPROD;
  129. $res = ds_upload_pic(ATTACH_POINTPROD, 'goods_images');
  130. if ($res['code']) {
  131. $file_name = $res['data']['file_name'];
  132. $indeximg_succ = true;
  133. $goods_array['pgoods_image'] = $file_name;
  134. ds_create_thumb($upload_file, $file_name, '60,240', '60,240', '_small,_normal');
  135. } else {
  136. $this->error($res['msg']);
  137. }
  138. }
  139. $state = $pointprod_model->addPointgoods($goods_array);
  140. if ($state) {
  141. //礼品代表图片数据入库
  142. if ($indeximg_succ) {
  143. $insert_array = array();
  144. $insert_array['file_name'] = $file_name;
  145. $insert_array['upload_type'] = 5;
  146. $insert_array['file_size'] = (config('ds_config.upload_type')=='alioss')?0:filesize($upload_file . DIRECTORY_SEPARATOR . $file_name);
  147. $insert_array['item_id'] = $state;
  148. $insert_array['upload_time'] = TIMESTAMP;
  149. $upload_model->addUpload($insert_array);
  150. }
  151. //更新积分礼品描述图片
  152. $file_idstr = '';
  153. $condition = array();
  154. $condition[] = array('upload_type', '=', 6);
  155. $condition[] = array('item_id', '=', 0);
  156. $file_id_array = input('post.file_id/a');
  157. if (is_array($file_id_array) && count($file_id_array) > 0) {
  158. $condition[] = array('upload_id', 'in', $file_id_array);
  159. }
  160. $upload_model->editUpload(array('item_id' => $state), $condition);
  161. $this->log(lang('admin_pointprod_add_title') . '[' . input('post.goodsname') . ']');
  162. $this->success(lang('admin_pointprod_add_success'), 'pointprod/index');
  163. }
  164. }
  165. //模型实例化
  166. $condition = array();
  167. $condition[] = array('upload_type', '=', '6');
  168. $condition[] = array('item_id', '=', '0');
  169. $file_upload = $upload_model->getUploadList($condition);
  170. View::assign('file_upload', $file_upload);
  171. View::assign('PHPSESSID', session_id());
  172. $hourarr = array(
  173. '00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17',
  174. '18', '19', '20', '21', '22', '23'
  175. );
  176. View::assign('hourarr', $hourarr);
  177. //会员级别
  178. $member_grade = model('member')->getMemberGradeArr();
  179. View::assign('member_grade', $member_grade);
  180. $this->setAdminCurItem('prod_add');
  181. return View::fetch();
  182. }
  183. /**
  184. * 积分礼品编辑
  185. */
  186. public function prod_edit() {
  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. $pg_id = input('param.pg_id');
  339. $pg_id_array = ds_delete_param($pg_id);
  340. if ($pg_id_array === FALSE) {
  341. ds_json_encode('10001', lang('admin_pointprod_parameter_error'));
  342. }
  343. $pointprod_model = model('pointprod');
  344. $result = $pointprod_model->delPointProdById($pg_id_array);
  345. if ($result) {
  346. $this->log(lang('ds_del') . lang('admin_pointprodp') . '[ID:' . $pg_id . ']');
  347. ds_json_encode('10000', lang('admin_pointprod_del_success'));
  348. } else {
  349. ds_json_encode('10001', lang('admin_pointprod_del_fail'));
  350. }
  351. }
  352. /**
  353. * 积分礼品异步状态修改
  354. */
  355. public function ajax() {
  356. //礼品上架,礼品推荐,礼品禁售
  357. $id = intval(input('param.id'));
  358. if ($id <= 0) {
  359. echo 'false';
  360. exit;
  361. }
  362. $pointprod_model = model('pointprod');
  363. $update_array = array();
  364. $update_array[input('param.column')] = trim(input('param.value'));
  365. $pointprod_model->editPointProd($update_array, array('pgoods_id' => $id));
  366. echo 'true';
  367. exit;
  368. }
  369. /**
  370. * 积分礼品上传
  371. */
  372. public function pointprod_pic_upload() {
  373. /**
  374. * 上传图片
  375. */
  376. $file_name = '';
  377. $file_object = request()->file('fileupload');
  378. if ($file_object) {
  379. $res = ds_upload_pic(ATTACH_POINTPROD, 'fileupload');
  380. if ($res['code']) {
  381. $file_name = $res['data']['file_name'];
  382. } else {
  383. echo $res['msg'];
  384. exit;
  385. }
  386. } else {
  387. echo 'error';
  388. exit;
  389. }
  390. /**
  391. * 模型实例化
  392. */
  393. $upload_model = model('upload');
  394. /**
  395. * 图片数据入库
  396. */
  397. $insert_array = array();
  398. $insert_array['file_name'] = $file_name;
  399. $insert_array['upload_type'] = '6';
  400. $insert_array['file_size'] = $_FILES['fileupload']['size'];
  401. $insert_array['upload_time'] = TIMESTAMP;
  402. $insert_array['item_id'] = input('param.item_id', 0);
  403. $result = $upload_model->addUpload($insert_array);
  404. if ($result) {
  405. $data = array();
  406. $data['file_id'] = $result;
  407. $data['file_name'] = $file_name;
  408. $data['file_path'] = ds_get_pic( ATTACH_POINTPROD , $file_name);
  409. /**
  410. * 整理为json格式
  411. */
  412. $output = json_encode($data);
  413. echo $output;
  414. }
  415. }
  416. /**
  417. * ajax操作删除已上传图片
  418. */
  419. public function ajaxdelupload() {
  420. //删除文章图片
  421. if (intval(input('param.file_id')) > 0) {
  422. $upload_model = model('upload');
  423. /**
  424. * 删除图片
  425. */
  426. $file_array = $upload_model->getOneUpload(intval(input('param.file_id')));
  427. @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_POINTPROD . DIRECTORY_SEPARATOR . $file_array['file_name']);
  428. /**
  429. * 删除信息
  430. */
  431. $condition = array();
  432. $condition[] = array('upload_id', '=', intval(input('param.file_id')));
  433. $upload_model->delUpload($condition);
  434. echo 'true';
  435. exit;
  436. } else {
  437. echo 'false';
  438. exit;
  439. }
  440. }
  441. protected function getAdminItemList() {
  442. $menu_array = array(
  443. array(
  444. 'name' => 'index', 'text' => lang('admin_pointprod_list_title'), 'url' => (string) url('Pointprod/index')
  445. ), array(
  446. 'name' => 'prod_add', 'text' => lang('admin_pointprod_add_title'), 'url' => (string) url('Pointprod/prod_add')
  447. ),
  448. );
  449. if (request()->action() == 'prod_edit') {
  450. $menu_array[] = array(
  451. 'name' => 'prod_edit', 'text' => lang('admin_pointprod_edit_title'), 'url' => 'javascript:void()'
  452. );
  453. }
  454. return $menu_array;
  455. }
  456. }