Goods.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * 商品管理
  4. */
  5. namespace app\admin\controller;
  6. use think\facade\View;
  7. use think\facade\Lang;
  8. /**
  9. *
  10. *
  11. * ----------------------------------------------------------------------------
  12. *
  13. * 控制器
  14. */
  15. class Goods extends AdminControl
  16. {
  17. public function initialize()
  18. {
  19. parent::initialize();
  20. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/goods.lang.php');
  21. }
  22. /**
  23. * 商品管理
  24. */
  25. public function index()
  26. {
  27. $goods_model = model('goods');
  28. /**
  29. * 处理商品分类
  30. */
  31. $choose_gcid = ($t = intval(input('param.choose_gcid'))) > 0 ? $t : 0;
  32. $gccache_arr = model('goodsclass')->getGoodsclassCache($choose_gcid, 3);
  33. View::assign('gc_json', json_encode($gccache_arr['showclass']));
  34. View::assign('gc_choose_json', json_encode($gccache_arr['choose_gcid']));
  35. /**
  36. * 查询条件
  37. */
  38. $where = array();
  39. $search_goods_name = trim(input('param.search_goods_name'));
  40. if ($search_goods_name != '') {
  41. $where[] = array('goods_name', 'like', '%' . $search_goods_name . '%');
  42. }
  43. $search_commonid = intval(input('param.search_commonid'));
  44. if ($search_commonid > 0) {
  45. $where[] = array('goods_commonid', '=', $search_commonid);
  46. }
  47. $search_store_name = trim(input('param.search_store_name'));
  48. if ($search_store_name != '') {
  49. $where[] = array('store_name', 'like', '%' . $search_store_name . '%');
  50. }
  51. $b_id = intval(input('param.b_id'));
  52. if ($b_id > 0) {
  53. $where[] = array('brand_id', '=', $b_id);
  54. }
  55. if ($choose_gcid > 0) {
  56. $where[] = array('gc_id_' . ($gccache_arr['showclass'][$choose_gcid]['depth']), '=', $choose_gcid);
  57. }
  58. $type = input('param.type');
  59. switch ($type) {
  60. // 禁售
  61. case 'lockup':
  62. $goods_list = $goods_model->getGoodsCommonLockUpList($where);
  63. break;
  64. // 等待审核
  65. case 'waitverify':
  66. $goods_list = $goods_model->getGoodsCommonWaitVerifyList($where, '*', 10, 'goods_verify desc, goods_commonid desc');
  67. break;
  68. // 全部商品
  69. default:
  70. //默认所有商品才有此参数
  71. $goods_state = input('param.goods_state');
  72. if (in_array($goods_state, array('0', '1', '10'))) {
  73. $where[] = array('goods_state', '=', $goods_state);
  74. }
  75. $goods_verify = input('param.goods_verify');
  76. if (in_array($goods_verify, array('0', '1', '10'))) {
  77. $where[] = array('goods_verify', '=', $goods_verify);
  78. }
  79. $goods_list = $goods_model->getGoodsCommonList($where, '*', 10, 'mall_goods_commend desc,mall_goods_sort asc');
  80. break;
  81. }
  82. View::assign('goods_list', $goods_list);
  83. View::assign('show_page', $goods_model->page_info->render());
  84. $storage_array = $goods_model->calculateStorage($goods_list);
  85. View::assign('storage_array', $storage_array);
  86. // 品牌
  87. $brand_list = model('brand')->getBrandPassedList(array());
  88. View::assign('search', $where);
  89. View::assign('brand_list', $brand_list);
  90. View::assign('state', array('1' => lang('goods_state_1'), '0' => lang('goods_state_0'), '10' => lang('goods_state_10')));
  91. View::assign('verify', array('1' => lang('goods_verify_1'), '0' => lang('goods_verify_0'), '10' => lang('goods_verify_10')));
  92. View::assign('ownShopIds', array_fill_keys(model('store')->getOwnShopIds(), true));
  93. $type = input('param.type');
  94. if (!in_array($type, array('lockup', 'waitverify', 'allgoods'))) {
  95. $type = 'allgoods';
  96. }
  97. View::assign('type', $type);
  98. $this->setAdminCurItem($type);
  99. return View::fetch();
  100. }
  101. /**
  102. * 计算商品库存
  103. */
  104. public function goods_storage($goods_list)
  105. {
  106. $goods_model = model('goods');
  107. // 计算库存
  108. $storage_array = array();
  109. if (!empty($goods_list)) {
  110. foreach ($goods_list as $value) {
  111. $storage_array[$value['goods_commonid']]['goods_storage'] = $goods_model->getGoodsSum(array('goods_commonid' => $value['goods_commonid']), 'goods_storage');
  112. $storage_array[$value['goods_commonid']][] = $goods_model->getGoodsInfo(array('goods_commonid' => $value['goods_commonid']), 'goods_id');
  113. }
  114. return $storage_array;
  115. } else {
  116. return false;
  117. }
  118. }
  119. /**
  120. * 违规下架
  121. */
  122. public function goods_lockup()
  123. {
  124. if (request()->isPost()) {
  125. $commonids = input('param.commonids');
  126. $commonid_array = ds_delete_param($commonids);
  127. if ($commonid_array == FALSE) {
  128. $this->error(lang('ds_common_op_fail'));
  129. }
  130. $update = array();
  131. $update['goods_stateremark'] = trim(input('post.close_reason'));
  132. $where = array();
  133. $where[] = array('goods_commonid', 'in', $commonid_array);
  134. model('goods')->editProducesLockUp($update, $where);
  135. dsLayerOpenSuccess(lang('ds_common_op_succ'));
  136. } else {
  137. View::assign('commonids', input('param.commonid'));
  138. echo View::fetch('close_remark');
  139. }
  140. }
  141. /**
  142. * 删除商品
  143. */
  144. public function goods_del()
  145. {
  146. $common_id = input('param.common_id');
  147. $common_id_array = ds_delete_param($common_id);
  148. if ($common_id_array == FALSE) {
  149. ds_json_encode('10001', lang('ds_common_op_fail'));
  150. }
  151. $condition = array();
  152. $condition[] = array('goods_commonid', 'in', $common_id_array);
  153. model('goods')->delGoodsAll($condition);
  154. ds_json_encode('10000', lang('ds_common_op_succ'));
  155. }
  156. /**
  157. * 审核商品
  158. */
  159. public function goods_verify()
  160. {
  161. if (request()->isPost()) {
  162. $commonids = input('param.commonids');
  163. $commonid_array = ds_delete_param($commonids);
  164. if ($commonid_array == FALSE) {
  165. $this->error(lang('ds_common_op_fail'));
  166. }
  167. $update2 = array();
  168. $update2['goods_verify'] = intval(input('param.verify_state'));
  169. $update1 = array();
  170. $update1['goods_verifyremark'] = trim(input('param.verify_reason'));
  171. $update1 = array_merge($update1, $update2);
  172. $where = array();
  173. $where[] = array('goods_commonid', 'in', $commonid_array);
  174. $goods_model = model('goods');
  175. if (intval(input('param.verify_state')) == 0) {
  176. $goods_model->editProducesVerifyFail($where, $update1, $update2);
  177. } else {
  178. $goods_model->editProduces($where, $update1, $update2);
  179. }
  180. dsLayerOpenSuccess(lang('ds_common_op_succ'));
  181. } else {
  182. View::assign('commonids', input('param.commonid'));
  183. echo View::fetch('verify_remark');
  184. }
  185. }
  186. //ajax获取同一个commonid下面的商品信息
  187. public function get_goods_list_ajax()
  188. {
  189. $common_id = input('param.commonid');
  190. if (empty($common_id)) {
  191. $this->error(lang('param_error'));
  192. }
  193. $map['goods_commonid'] = $common_id;
  194. $goods_model = model('goods');
  195. $common_info = $goods_model->getGoodsCommonInfo($map, 'spec_name');
  196. $goods_list = $goods_model->getGoodsList($map);
  197. //halt($goods_list);
  198. $spec_name = array_values((array) unserialize($common_info['spec_name']));
  199. foreach ($goods_list as $key => $val) {
  200. $goods_spec = array_values((array) unserialize($val['goods_spec']));
  201. $spec_array = array();
  202. foreach ($goods_spec as $k => $v) {
  203. $spec_array[] = '<div class="goods_spec">' . $spec_name[$k] . ':' . '<em title="' . $v . '">' . $v . '</em>' . '</div>';
  204. }
  205. $goods_list[$key]['goods_image'] = goods_cthumb($val['goods_image']);
  206. $goods_list[$key]['goods_spec'] = implode('', $spec_array);
  207. $goods_list[$key]['url'] = (string)url('home/Goods/index', array('goods_id' => $val['goods_id']));
  208. }
  209. return json_encode($goods_list);
  210. }
  211. /**
  212. * ajax操作
  213. */
  214. public function ajax()
  215. {
  216. $goods_model = model('goods');
  217. switch (input('param.branch')) {
  218. case 'mall_goods_commend':
  219. case 'mall_goods_sort':
  220. if (empty($result)) {
  221. $goods_model->editGoodsCommonById(array(trim(input('param.branch')) => trim(input('param.value'))), array(intval(input('param.id'))));
  222. echo 'true';
  223. exit;
  224. } else {
  225. echo 'false';
  226. exit;
  227. }
  228. break;
  229. }
  230. }
  231. /**
  232. * 获取卖家栏目列表,针对控制器下的栏目
  233. */
  234. protected function getAdminItemList()
  235. {
  236. $menu_array = array(
  237. array(
  238. 'name' => 'allgoods',
  239. 'text' => lang('goods_index_all_goods'),
  240. 'url' => (string)url('Goods/index')
  241. ),
  242. array(
  243. 'name' => 'lockup',
  244. 'text' => lang('goods_index_lock_goods'),
  245. 'url' => (string)url('Goods/index', ['type' => 'lockup'])
  246. ),
  247. array(
  248. 'name' => 'waitverify',
  249. 'text' => lang('goods_index_waitverify_goods'),
  250. 'url' => (string)url('Goods/index', ['type' => 'waitverify'])
  251. ),
  252. );
  253. return $menu_array;
  254. }
  255. }