Goods.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. * ============================================================================
  16. * 控制器
  17. */
  18. class Goods extends AdminControl {
  19. public function initialize() {
  20. parent::initialize();
  21. Lang::load(base_path() . 'admin/lang/'.config('lang.default_lang').'/goods.lang.php');
  22. }
  23. /**
  24. * 商品管理
  25. */
  26. public function index() {
  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. $goods_model = model('goods');
  106. // 计算库存
  107. $storage_array = array();
  108. if (!empty($goods_list)) {
  109. foreach ($goods_list as $value) {
  110. $storage_array[$value['goods_commonid']]['goods_storage'] = $goods_model->getGoodsSum(array('goods_commonid'=>$value['goods_commonid']),'goods_storage');
  111. $storage_array[$value['goods_commonid']][] = $goods_model->getGoodsInfo(array('goods_commonid'=>$value['goods_commonid']),'goods_id');
  112. }
  113. return $storage_array;
  114. } else {
  115. return false;
  116. }
  117. }
  118. /**
  119. * 违规下架
  120. */
  121. public function goods_lockup() {
  122. if (request()->isPost()) {
  123. $commonids = input('param.commonids');
  124. $commonid_array = ds_delete_param($commonids);
  125. if ($commonid_array == FALSE) {
  126. $this->error(lang('ds_common_op_fail'));
  127. }
  128. $update = array();
  129. $update['goods_stateremark'] = trim(input('post.close_reason'));
  130. $where = array();
  131. $where[]=array('goods_commonid','in', $commonid_array);
  132. model('goods')->editProducesLockUp($update, $where);
  133. dsLayerOpenSuccess(lang('ds_common_op_succ'));
  134. } else {
  135. View::assign('commonids', input('param.commonid'));
  136. echo View::fetch('close_remark');
  137. }
  138. }
  139. /**
  140. * 删除商品
  141. */
  142. public function goods_del() {
  143. $common_id = input('param.common_id');
  144. $common_id_array = ds_delete_param($common_id);
  145. if ($common_id_array == FALSE) {
  146. ds_json_encode('10001', lang('ds_common_op_fail'));
  147. }
  148. $condition = array();
  149. $condition[]=array('goods_commonid','in',$common_id_array);
  150. model('goods')->delGoodsAll($condition);
  151. ds_json_encode('10000', lang('ds_common_op_succ'));
  152. }
  153. /**
  154. * 审核商品
  155. */
  156. public function goods_verify() {
  157. if (request()->isPost()) {
  158. $commonids = input('param.commonids');
  159. $commonid_array = ds_delete_param($commonids);
  160. if ($commonid_array == FALSE) {
  161. $this->error(lang('ds_common_op_fail'));
  162. }
  163. $update2 = array();
  164. $update2['goods_verify'] = intval(input('param.verify_state'));
  165. $update1 = array();
  166. $update1['goods_verifyremark'] = trim(input('param.verify_reason'));
  167. $update1 = array_merge($update1, $update2);
  168. $where = array();
  169. $where[]=array('goods_commonid','in', $commonid_array);
  170. $goods_model = model('goods');
  171. if (intval(input('param.verify_state')) == 0) {
  172. $goods_model->editProducesVerifyFail($where, $update1, $update2);
  173. } else {
  174. $goods_model->editProduces($where, $update1, $update2);
  175. }
  176. dsLayerOpenSuccess(lang('ds_common_op_succ'));
  177. } else {
  178. View::assign('commonids', input('param.commonid'));
  179. echo View::fetch('verify_remark');
  180. }
  181. }
  182. //ajax获取同一个commonid下面的商品信息
  183. public function get_goods_list_ajax() {
  184. $common_id = input('param.commonid');
  185. if (empty($common_id)) {
  186. $this->error(lang('param_error'));
  187. }
  188. $map['goods_commonid'] = $common_id;
  189. $goods_model = model('goods');
  190. $common_info = $goods_model->getGoodsCommonInfo($map,'spec_name');
  191. $goods_list = $goods_model->getGoodsList($map);
  192. //halt($goods_list);
  193. $spec_name = array_values((array) unserialize($common_info['spec_name']));
  194. foreach ($goods_list as $key => $val) {
  195. $goods_spec = array_values((array) unserialize($val['goods_spec']));
  196. $spec_array = array();
  197. foreach ($goods_spec as $k => $v) {
  198. $spec_array[] = '<div class="goods_spec">' . $spec_name[$k] . ':' . '<em title="' . $v . '">' . $v . '</em>' . '</div>';
  199. }
  200. $goods_list[$key]['goods_image'] = goods_cthumb($val['goods_image']);
  201. $goods_list[$key]['goods_spec'] = implode('', $spec_array);
  202. $goods_list[$key]['url'] = (string)url('home/Goods/index', array('goods_id' => $val['goods_id']));
  203. }
  204. return json_encode($goods_list);
  205. }
  206. /**
  207. * ajax操作
  208. */
  209. public function ajax() {
  210. $goods_model = model('goods');
  211. switch (input('param.branch')) {
  212. case 'mall_goods_commend':
  213. case 'mall_goods_sort':
  214. if (empty($result)) {
  215. $goods_model->editGoodsCommonById(array(trim(input('param.branch')) => trim(input('param.value'))),array(intval(input('param.id'))));
  216. echo 'true';
  217. exit;
  218. } else {
  219. echo 'false';
  220. exit;
  221. }
  222. break;
  223. }
  224. }
  225. /**
  226. * 获取卖家栏目列表,针对控制器下的栏目
  227. */
  228. protected function getAdminItemList() {
  229. $menu_array = array(
  230. array(
  231. 'name' => 'allgoods',
  232. 'text' => lang('goods_index_all_goods'),
  233. 'url' => (string)url('Goods/index')
  234. ),
  235. array(
  236. 'name' => 'lockup',
  237. 'text' => lang('goods_index_lock_goods'),
  238. 'url' => (string)url('Goods/index', ['type' => 'lockup'])
  239. ),
  240. array(
  241. 'name' => 'waitverify',
  242. 'text' => lang('goods_index_waitverify_goods'),
  243. 'url' => (string)url('Goods/index', ['type' => 'waitverify'])
  244. ),
  245. );
  246. return $menu_array;
  247. }
  248. }