Sellergoodsadd.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. <?php
  2. namespace app\home\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. use think\facade\Db;
  6. /**
  7. * ============================================================================
  8. *
  9. * ============================================================================
  10. *
  11. * ----------------------------------------------------------------------------
  12. *
  13. * ============================================================================
  14. * 控制器
  15. */
  16. class Sellergoodsadd extends BaseSeller {
  17. public function initialize() {
  18. parent::initialize();
  19. error_reporting(E_ERROR | E_WARNING);
  20. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/sellergoodsadd.lang.php');
  21. }
  22. /**
  23. * 三方店铺验证,商品数量,有效期
  24. */
  25. private function checkStore() {
  26. $goodsLimit = (int) $this->store_grade['storegrade_goods_limit'];
  27. if ($goodsLimit > 0) {
  28. // 是否到达商品数上限
  29. $goods_num = model('goods')->getGoodsCommonCount(array('store_id' => session('store_id')));
  30. if ($goods_num >= $goodsLimit) {
  31. $this->error(lang('store_goods_index_goods_limit') . $goodsLimit . lang('store_goods_index_goods_limit1'), (string) url('Sellergoodsonline/goods_list'));
  32. }
  33. }
  34. }
  35. public function index() {
  36. $this->checkStore();
  37. $this->add_step_one();
  38. }
  39. /**
  40. * 添加商品
  41. */
  42. public function add_step_one() {
  43. // 实例化商品分类模型
  44. $goodsclass_model = model('goodsclass');
  45. // 商品分类
  46. $goods_class = $goodsclass_model->getGoodsclass(session('store_id'));
  47. //halt($goods_class);
  48. // 常用商品分类
  49. $staple_model = model('goodsclassstaple');
  50. $param_array = array();
  51. $param_array['member_id'] = session('member_id');
  52. $staple_array = $staple_model->getGoodsclassstapleList($param_array);
  53. View::assign('staple_array', $staple_array);
  54. View::assign('goods_class', $goods_class);
  55. /* 设置卖家当前菜单 */
  56. $this->setSellerCurMenu('sellergoodsadd');
  57. $this->setSellerCurItem();
  58. echo View::fetch($this->template_dir . 'store_goods_add_step1');
  59. exit;
  60. }
  61. /**
  62. * 添加商品
  63. */
  64. public function add_step_two() {
  65. // 实例化商品分类模型
  66. $goodsclass_model = model('goodsclass');
  67. $gc_id = intval(input('get.class_id'));
  68. // 验证商品分类是否存在且商品分类是否为最后一级
  69. $data = model('goodsclass')->getGoodsclassForCacheModel();
  70. if (!isset($data[$gc_id]) || isset($data[$gc_id]['child']) || isset($data[$gc_id]['childchild'])) {
  71. $this->error(lang('store_goods_index_again_choose_category1'));
  72. }
  73. // 如果不是自营店铺或者自营店铺未绑定全部商品类目,读取绑定分类
  74. if (!check_platform_store_bindingall_goodsclass()) {
  75. //商品分类 支持批量显示分类
  76. $storebindclass_model = model('storebindclass');
  77. $goods_class = model('goodsclass')->getGoodsclassForCacheModel();
  78. $condition = array();
  79. $condition[] = array('store_id', '=', session('store_id'));
  80. $class_2 = isset($goods_class[$gc_id]['gc_parent_id'])?$goods_class[$gc_id]['gc_parent_id']:0;
  81. $class_1 = isset($goods_class[$class_2]['gc_parent_id'])?$goods_class[$class_2]['gc_parent_id']:0;
  82. $condition_class_1 = array(array('class_1', '=', ($class_1 > 0) ? $class_1 : (($class_2 > 0) ? $class_2 : $gc_id)));
  83. $condition_class_2 = array(array('class_2', '=', ($class_1 > 0) ? $class_2 : (($class_2 > 0) ? $gc_id : 0)));
  84. $condition_class_3 = array(array('class_3', '=', ($class_1 > 0 && $class_2 > 0) ? $gc_id : 0));
  85. $bind_info = $storebindclass_model->getStorebindclassInfo(array_merge($condition,$condition_class_1,$condition_class_2,$condition_class_3));
  86. if (empty($bind_info)) {
  87. $condition_class_3 = array(array('class_3', '=',0));
  88. $bind_info = $storebindclass_model->getStorebindclassInfo(array_merge($condition,$condition_class_1,$condition_class_2,$condition_class_3));
  89. if (empty($bind_info)) {
  90. $condition_class_2 = array(array('class_2', '=', 0));
  91. $condition_class_3 = array(array('class_3', '=', 0));
  92. $bind_info = $storebindclass_model->getStorebindclassInfo(array_merge($condition,$condition_class_1,$condition_class_2,$condition_class_3));
  93. if (empty($bind_info)) {
  94. $condition_class_1 = array(array('class_1', '=', 0));
  95. $condition_class_2 = array(array('class_2', '=', 0));
  96. $condition_class_3 = array(array('class_3', '=', 0));
  97. $bind_info = $storebindclass_model->getStorebindclassInfo(array_merge($condition,$condition_class_1,$condition_class_2,$condition_class_3));
  98. if (empty($bind_info)) {
  99. $this->error(lang('store_goods_index_again_choose_category2'));
  100. }
  101. }
  102. }
  103. }
  104. }
  105. // 更新常用分类信息
  106. $goods_class = $goodsclass_model->getGoodsclassLineForTag($gc_id);
  107. View::assign('goods_class', $goods_class);
  108. model('goodsclassstaple')->autoIncrementStaple($goods_class, session('member_id'));
  109. // 获取类型相关数据
  110. $typeinfo = model('type')->getAttribute($goods_class['type_id'], session('store_id'), $gc_id);
  111. list($spec_json, $spec_list, $attr_list, $brand_list) = $typeinfo;
  112. View::assign('sign_i', count($spec_list));
  113. View::assign('spec_list', $spec_list);
  114. View::assign('attr_list', $attr_list);
  115. View::assign('brand_list', $brand_list);
  116. // 实例化店铺商品分类模型
  117. $store_goods_class = model('storegoodsclass')->getClassTree(array(
  118. 'store_id' => session('store_id'),
  119. 'storegc_state' => '1'
  120. ));
  121. View::assign('store_goods_class', $store_goods_class);
  122. // 小时分钟显示
  123. $hour_array = array(
  124. '00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17',
  125. '18', '19', '20', '21', '22', '23'
  126. );
  127. View::assign('hour_array', $hour_array);
  128. $minute_array = array('05', '10', '15', '20', '25', '30', '35', '40', '45', '50', '55');
  129. View::assign('minute_array', $minute_array);
  130. // 关联版式
  131. $plate_list = model('storeplate')->getStoreplateList(array('store_id' => session('store_id')), 'storeplate_id,storeplate_name,storeplate_position');
  132. $plate_list = array_under_reset($plate_list, 'storeplate_position', 2);
  133. View::assign('plate_list', $plate_list);
  134. $store_service_model=model('store_service');
  135. $store_service_list=$store_service_model->getStoreServiceList(array(array('store_id','=',session('store_id'))),'*',0);
  136. View::assign('store_service_list', $store_service_list);
  137. /* 设置卖家当前菜单 */
  138. $this->setSellerCurMenu('sellergoodsadd');
  139. $this->setSellerCurItem();
  140. return View::fetch($this->template_dir . 'store_goods_add_step2');
  141. }
  142. /**
  143. * 保存商品(商品发布第二步使用)
  144. */
  145. public function save_goods() {
  146. if (request()->isPost()) {
  147. $goods_model = model('goods');
  148. Db::startTrans();
  149. try {
  150. $type_model = model('type');
  151. // 分类信息
  152. $goods_class = model('goodsclass')->getGoodsclassLineForTag(intval(input('post.cate_id')));
  153. $common_array = array();
  154. $common_array['goods_name'] = input('post.g_name');
  155. $common_array['goods_advword'] = input('post.g_jingle');
  156. $common_array['gc_id'] = intval(input('post.cate_id'));
  157. $common_array['gc_id_1'] = intval($goods_class['gc_id_1']);
  158. $common_array['gc_id_2'] = intval($goods_class['gc_id_2']);
  159. $common_array['gc_id_3'] = intval($goods_class['gc_id_3']);
  160. $common_array['gc_name'] = input('post.cate_name');
  161. $common_array['brand_id'] = input('post.b_id');
  162. $common_array['brand_name'] = input('post.b_name');
  163. $common_array['type_id'] = intval(input('post.type_id'));
  164. $common_array['goods_image'] = input('post.image_path');
  165. $common_array['goodsvideo_name'] = input('post.goodsvideo_name');
  166. $common_array['goods_price'] = floatval(input('post.g_price'));
  167. $common_array['goods_marketprice'] = floatval(input('post.g_marketprice'));
  168. $common_array['goods_costprice'] = floatval(input('post.g_costprice'));
  169. $common_array['goods_discount'] = floatval(input('post.g_discount'));
  170. $common_array['goods_serial'] = input('post.g_serial');
  171. $common_array['goods_storage_alarm'] = intval(input('post.g_alarm'));
  172. $common_array['store_service_ids'] = !empty(input('post.store_service_ids/a')) ? implode(',',input('post.store_service_ids/a')) : '';
  173. $attr_array = input('post.attr/a'); #获取数组
  174. if (!empty($attr_array)) {
  175. foreach ($attr_array as $attr_key => $attr_value) {
  176. foreach ($attr_value as $temp) {
  177. if ($temp == '') {
  178. unset($attr_array[$attr_key]); //过滤掉没选择的属性
  179. }
  180. }
  181. }
  182. if (!empty($attr_array)) {
  183. $attr = $attr_array;
  184. } else {
  185. $attr = null;
  186. }
  187. } else {
  188. $attr = null;
  189. }
  190. $common_array['goods_attr'] = serialize($attr);
  191. $goods_body = preg_replace_callback("/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i", function ($matches) {
  192. return str_replace($matches[2], strip_tags($matches[2]), $matches[0]);
  193. }, htmlspecialchars_decode(input('post.goods_body')));
  194. $common_array['goods_body'] = $goods_body;
  195. // 序列化保存手机端商品描述数据
  196. $mobile_body = input('post.m_body');
  197. if ($mobile_body != '') {
  198. $mobile_body = str_replace('&quot;', '"', $mobile_body);
  199. $mobile_body = json_decode($mobile_body, true);
  200. if (!empty($mobile_body)) {
  201. $mobile_body = serialize($mobile_body);
  202. } else {
  203. $mobile_body = '';
  204. }
  205. }
  206. $common_array['mobile_body'] = $mobile_body;
  207. $common_array['goods_commend'] = intval(input('post.g_commend'));
  208. $common_array['goods_state'] = ($this->store_info['store_state'] != 1) ? 0 : intval(input('post.g_state')); // 店铺关闭时,商品下架
  209. $common_array['goods_addtime'] = TIMESTAMP;
  210. $common_array['goods_shelftime'] = strtotime(input('post.starttime')) + intval(input('post.starttime_H')) * 3600 + intval(input('post.starttime_i')) * 60;
  211. $common_array['goods_verify'] = (config('ds_config.goods_verify') == 1) ? 10 : 1;
  212. $common_array['store_id'] = session('store_id');
  213. $common_array['store_name'] = session('store_name');
  214. $common_array['spec_name'] = is_array(input('post.spec/a')) ? serialize(input('post.sp_name/a')) : serialize(null);
  215. $common_array['spec_value'] = is_array(input('post.spec/a')) ? serialize(input('post.sp_val/a')) : serialize(null);
  216. $common_array['goods_vat'] = intval(input('post.g_vat'));
  217. $common_array['areaid_1'] = intval(input('post.province_id'));
  218. $common_array['areaid_2'] = intval(input('post.city_id'));
  219. $common_array['transport_id'] = (input('post.freight') == '0') ? '0' : intval(input('post.transport_id')); // 售卖区域
  220. $common_array['transport_title'] = input('post.transport_title');
  221. $common_array['goods_freight'] = floatval(input('post.g_freight'));
  222. $goods_validate = ds_validate('sellergoodsadd');
  223. if (!$goods_validate->scene('save_goods')->check($common_array)) {
  224. throw new \think\Exception($goods_validate->getError(), 10006);
  225. }
  226. //查询店铺商品分类
  227. $goods_stcids_arr = array();
  228. $sgcate_id_array = input('post.sgcate_id/a'); #获取数组
  229. if (!empty($sgcate_id_array)) {
  230. $sgcate_id_arr = array();
  231. foreach ($sgcate_id_array as $k => $v) {
  232. $sgcate_id_arr[] = intval($v);
  233. }
  234. $sgcate_id_arr = array_unique($sgcate_id_arr);
  235. $store_goods_class = model('storegoodsclass')->getStoregoodsclassList(array(
  236. array('store_id', '=', session('store_id')),
  237. array('storegc_id', 'in', $sgcate_id_arr),
  238. array('storegc_state', '=', '1')
  239. ));
  240. if (!empty($store_goods_class)) {
  241. foreach ($store_goods_class as $k => $v) {
  242. if ($v['storegc_id'] > 0) {
  243. $goods_stcids_arr[] = $v['storegc_id'];
  244. }
  245. if ($v['storegc_parent_id'] > 0) {
  246. $goods_stcids_arr[] = $v['storegc_parent_id'];
  247. }
  248. }
  249. $goods_stcids_arr = array_unique($goods_stcids_arr);
  250. sort($goods_stcids_arr);
  251. }
  252. }
  253. if (empty($goods_stcids_arr)) {
  254. $common_array['goods_stcids'] = '';
  255. } else {
  256. $common_array['goods_stcids'] = ',' . implode(',', $goods_stcids_arr) . ','; // 首尾需要加,
  257. }
  258. $common_array['plateid_top'] = intval(input('post.plate_top')) > 0 ? intval(input('post.plate_top')) : '';
  259. $common_array['plateid_bottom'] = intval(input('post.plate_bottom')) > 0 ? intval(input('post.plate_bottom')) : '';
  260. $common_array['is_virtual'] = intval(input('post.is_gv'));
  261. $common_array['virtual_type'] = $common_array['is_virtual']?intval(input('post.virtual_type')):0;
  262. $common_array['virtual_indate'] = input('post.g_vindate') != '' ? (strtotime(input('post.g_vindate')) + 24 * 60 * 60 - 1) : 0; // 当天的最后一秒结束
  263. $common_array['virtual_limit'] = intval(input('post.g_vlimit')) > 10 || intval(input('post.g_vlimit')) < 0 ? 10 : intval(input('post.g_vlimit'));
  264. $common_array['virtual_invalid_refund'] = intval(input('post.g_vinvalidrefund'));
  265. $common_array['is_goodsfcode'] = intval(input('post.is_fc'));
  266. $common_array['is_appoint'] = intval(input('post.is_appoint')); // 只有库存为零的商品可以预约
  267. $common_array['appoint_satedate'] = $common_array['is_appoint'] == 1 ? strtotime(input('post.g_saledate')) : ''; // 预约商品的销售时间
  268. $common_array['is_platform_store'] = in_array(session('store_id'), model('store')->getOwnShopIds()) ? 1 : 0;
  269. // 保存数据
  270. $common_id = $goods_model->addGoodsCommon($common_array);
  271. if ($common_id) {
  272. // 生成的商品id(SKU)
  273. $goodsid_array = array();
  274. // 商品规格
  275. $spec_array = input('post.spec/a'); #获取数组
  276. if (is_array($spec_array) && !empty($spec_array)) {
  277. foreach ($spec_array as $value) {
  278. $goods = array();
  279. $goods['goods_commonid'] = $common_id;
  280. $goods['goods_name'] = $common_array['goods_name'] . ' ' . implode(' ', $value['sp_value']);
  281. $goods['goods_advword'] = $common_array['goods_advword'];
  282. $goods['store_id'] = $common_array['store_id'];
  283. $goods['store_name'] = session('store_name');
  284. $goods['gc_id'] = $common_array['gc_id'];
  285. $goods['gc_id_1'] = $common_array['gc_id_1'];
  286. $goods['gc_id_2'] = $common_array['gc_id_2'];
  287. $goods['gc_id_3'] = $common_array['gc_id_3'];
  288. $goods['brand_id'] = $common_array['brand_id'];
  289. $goods['goods_price'] = $value['price'];
  290. $goods['goods_promotion_price'] = $value['price'];
  291. $goods['goods_marketprice'] = $value['marketprice'] == 0 ? $common_array['goods_marketprice'] : $value['marketprice'];
  292. $goods['goods_serial'] = $value['sku'];
  293. $goods['goods_storage_alarm'] = intval($value['alarm']);
  294. $goods['goods_spec'] = serialize($value['sp_value']);
  295. $goods['goods_storage'] = $value['stock'];
  296. $goods['goods_weight'] = $value['goods_weight'];
  297. $goods['goods_image'] = $common_array['goods_image'];
  298. $goods['goods_state'] = $common_array['goods_state'];
  299. $goods['goods_verify'] = $common_array['goods_verify'];
  300. $goods['goods_addtime'] = TIMESTAMP;
  301. $goods['goods_edittime'] = TIMESTAMP;
  302. $goods['areaid_1'] = $common_array['areaid_1'];
  303. $goods['areaid_2'] = $common_array['areaid_2'];
  304. $goods['color_id'] = isset($value['color']) ? intval($value['color']) : '';
  305. $goods['transport_id'] = $common_array['transport_id'];
  306. $goods['goods_freight'] = $common_array['goods_freight'];
  307. $goods['goods_vat'] = $common_array['goods_vat'];
  308. $goods['goods_commend'] = $common_array['goods_commend'];
  309. $goods['goods_stcids'] = $common_array['goods_stcids'];
  310. $goods['is_virtual'] = $common_array['is_virtual'];
  311. $goods['virtual_indate'] = $common_array['virtual_indate'];
  312. $goods['virtual_limit'] = $common_array['virtual_limit'];
  313. $goods['virtual_invalid_refund'] = $common_array['virtual_invalid_refund'];
  314. $goods['is_goodsfcode'] = $common_array['is_goodsfcode'];
  315. $goods['is_appoint'] = $common_array['is_appoint'];
  316. $goods['is_platform_store'] = $common_array['is_platform_store'];
  317. switch($common_array['virtual_type']){
  318. case 1:
  319. $goods['virtual_content'] = $value['vc_card'];
  320. $card_list=explode("\r\n",$goods['virtual_content']);
  321. $goods['goods_storage'] = count($card_list);
  322. break;
  323. case 2:
  324. $goods['virtual_content'] = $value['vc_pan'];
  325. $goods['goods_storage'] = 1;
  326. break;
  327. case 3:
  328. $goods['virtual_content'] = $value['vc_file'];
  329. $goods['goods_storage'] = 1;
  330. break;
  331. default:
  332. $goods['virtual_content'] = '';
  333. }
  334. $goods_id = $goods_model->addGoods($goods);
  335. $type_model->addGoodsType($goods_id, $common_id, array(
  336. 'cate_id' => input('post.cate_id'), 'type_id' => input('post.type_id'), 'attr' => input('post.attr/a')
  337. ));
  338. $goodsid_array[] = $goods_id;
  339. }
  340. } else {
  341. $goods = array();
  342. $goods['goods_commonid'] = $common_id;
  343. $goods['goods_name'] = $common_array['goods_name'];
  344. $goods['goods_advword'] = $common_array['goods_advword'];
  345. $goods['store_id'] = $common_array['store_id'];
  346. $goods['store_name'] = session('store_name');
  347. $goods['gc_id'] = $common_array['gc_id'];
  348. $goods['gc_id_1'] = $common_array['gc_id_1'];
  349. $goods['gc_id_2'] = $common_array['gc_id_2'];
  350. $goods['gc_id_3'] = $common_array['gc_id_3'];
  351. $goods['brand_id'] = $common_array['brand_id'];
  352. $goods['goods_price'] = $common_array['goods_price'];
  353. $goods['goods_promotion_price'] = $common_array['goods_price'];
  354. $goods['goods_marketprice'] = $common_array['goods_marketprice'];
  355. $goods['goods_serial'] = $common_array['goods_serial'];
  356. $goods['goods_storage_alarm'] = $common_array['goods_storage_alarm'];
  357. $goods['goods_spec'] = serialize(null);
  358. $goods['goods_storage'] = intval(input('post.g_storage'));
  359. $goods['goods_weight'] = floatval(input('post.goods_weight'));
  360. $goods['goods_image'] = $common_array['goods_image'];
  361. $goods['goods_state'] = $common_array['goods_state'];
  362. $goods['goods_verify'] = $common_array['goods_verify'];
  363. $goods['goods_addtime'] = TIMESTAMP;
  364. $goods['goods_edittime'] = TIMESTAMP;
  365. $goods['areaid_1'] = $common_array['areaid_1'];
  366. $goods['areaid_2'] = $common_array['areaid_2'];
  367. $goods['color_id'] = 0;
  368. $goods['transport_id'] = $common_array['transport_id'];
  369. $goods['goods_freight'] = $common_array['goods_freight'];
  370. $goods['goods_vat'] = $common_array['goods_vat'];
  371. $goods['goods_commend'] = $common_array['goods_commend'];
  372. $goods['goods_stcids'] = $common_array['goods_stcids'];
  373. $goods['is_virtual'] = $common_array['is_virtual'];
  374. $goods['virtual_indate'] = $common_array['virtual_indate'];
  375. $goods['virtual_limit'] = $common_array['virtual_limit'];
  376. $goods['virtual_invalid_refund'] = $common_array['virtual_invalid_refund'];
  377. $goods['is_goodsfcode'] = $common_array['is_goodsfcode'];
  378. $goods['is_appoint'] = $common_array['is_appoint'];
  379. $goods['is_platform_store'] = $common_array['is_platform_store'];
  380. switch($common_array['virtual_type']){
  381. case 1:
  382. $goods['virtual_content'] = input('post.vc_card');
  383. $card_list=explode("\r\n",$goods['virtual_content']);
  384. $goods['goods_storage'] = count($card_list);
  385. break;
  386. case 2:
  387. $goods['virtual_content'] = input('post.vc_pan');
  388. $goods['goods_storage'] = 1;
  389. break;
  390. case 3:
  391. $goods['virtual_content'] = input('post.vc_file');
  392. $goods['goods_storage'] = 1;
  393. break;
  394. default:
  395. $goods['virtual_content'] = '';
  396. }
  397. $goods_id = $goods_model->addGoods($goods);
  398. $type_model->addGoodsType($goods_id, $common_id, array(
  399. 'cate_id' => input('post.cate_id'), 'type_id' => input('post.type_id'), 'attr' => input('post.attr/a')
  400. ));
  401. $goodsid_array[] = $goods_id;
  402. }
  403. // 商品加入上架队列
  404. if (!empty(input('post.starttime'))) {
  405. $selltime = strtotime(input('post.starttime')) + intval(input('post.starttime_H')) * 3600 + intval(input('post.starttime_i')) * 60;
  406. if ($selltime > TIMESTAMP) {
  407. $this->addcron(array('cron_exetime' => $selltime, 'cron_value' => serialize(intval($common_id)), 'cron_type' => 'editProducesOnline'), true);
  408. }
  409. }
  410. // 记录日志
  411. $this->recordSellerlog('添加商品,平台货号:' . $common_id);
  412. // 生成F码
  413. if ($common_array['is_goodsfcode'] == 1) {
  414. model('goodsfcode')->createGoodsfcode(array(
  415. 'goods_commonid' => $common_id, 'goodsfcode_count' => intval(input('post.g_fccount')),
  416. 'goodsfcode_prefix' => input('post.g_fcprefix')));
  417. }
  418. } else {
  419. throw new \think\Exception(lang('store_goods_index_goods_add_fail'), 10006);
  420. }
  421. } catch (\Exception $e) {
  422. Db::rollback();
  423. $this->error($e->getMessage(), get_referer());
  424. }
  425. Db::commit();
  426. $this->redirect((string) url('Sellergoodsadd/add_step_three', ['commonid' => $common_id]));
  427. }
  428. }
  429. /**
  430. * 第三步添加颜色图片
  431. */
  432. public function add_step_three() {
  433. $common_id = input('param.commonid');
  434. if ($common_id <= 0) {
  435. $this->error(lang('param_error'), (string) url('Seller/index'));
  436. }
  437. $goods_model = model('goods');
  438. $img_array = $goods_model->getGoodsList(array('goods_commonid' => $common_id), 'color_id,goods_image', 'color_id');
  439. // 整理,更具id查询颜色名称
  440. $colorid_array = array();
  441. if (!empty($img_array)) {
  442. $image_array = array();
  443. foreach ($img_array as $val) {
  444. $image_array[$val['color_id']][0]['goodsimage_url'] = $val['goods_image'];
  445. $image_array[$val['color_id']][0]['goodsimage_isdefault'] = 1;
  446. $colorid_array[] = $val['color_id'];
  447. }
  448. View::assign('img', $image_array);
  449. }
  450. $common_list = $goods_model->getGoodsCommonInfoByID($common_id);
  451. $spec_value = unserialize($common_list['spec_value']);
  452. View::assign('spec_value', $spec_value);
  453. $spec_model = model('spec');
  454. $value_array = $spec_model->getSpecvalueList(array(
  455. array('spvalue_id', 'in', $colorid_array),
  456. array('store_id', '=', session('store_id'))
  457. ), 'spvalue_id,spvalue_name');
  458. if (empty($value_array)) {
  459. $value_array[] = array('spvalue_id' => '0', 'spvalue_name' => lang('no_color'));
  460. }
  461. View::assign('value_array', $value_array);
  462. View::assign('commonid', $common_id);
  463. /* 设置卖家当前菜单 */
  464. $this->setSellerCurMenu('sellergoodsadd');
  465. $this->setSellerCurItem();
  466. return View::fetch($this->template_dir . 'store_goods_add_step3');
  467. }
  468. /**
  469. * 保存商品颜色图片
  470. */
  471. public function save_image() {
  472. if (request()->isPost()) {
  473. $common_id = intval(input('param.commonid'));
  474. $img_array = input('post.img/a'); #获取数组
  475. if ($common_id <= 0 || empty($img_array)) {
  476. $this->error(lang('param_error'));
  477. }
  478. $goods_model = model('goods');
  479. // 保存
  480. $insert_array = array();
  481. $k = 0;
  482. foreach ($img_array as $key => $value) {
  483. foreach ($value as $v) {
  484. if ($v['name'] == '') {
  485. continue;
  486. }
  487. // 商品默认主图
  488. $update_array = array(); // 更新商品主图
  489. $update_where = array();
  490. $update_array['goods_image'] = $v['name'];
  491. $update_where['goods_commonid'] = $common_id;
  492. $update_where['color_id'] = $key;
  493. if ($k == 0 || $v['default'] == 1) {
  494. $k++;
  495. $update_array['goods_image'] = $v['name'];
  496. $update_where['goods_commonid'] = $common_id;
  497. $update_where['color_id'] = $key;
  498. // 更新商品主图
  499. $goods_model->editGoods($update_array, $update_where);
  500. }
  501. $tmp_insert = array();
  502. $tmp_insert['goods_commonid'] = $common_id;
  503. $tmp_insert['store_id'] = session('store_id');
  504. $tmp_insert['color_id'] = $key;
  505. $tmp_insert['goodsimage_url'] = $v['name'];
  506. $tmp_insert['goodsimage_sort'] = ($v['default'] == 1) ? 0 : intval($v['sort']);
  507. $tmp_insert['goodsimage_isdefault'] = $v['default'];
  508. $insert_array[] = $tmp_insert;
  509. }
  510. }
  511. if(!empty($insert_array)){
  512. $rs = $goods_model->addGoodsImagesAll($insert_array);
  513. if ($rs) {
  514. $this->redirect((string) url('Sellergoodsadd/add_step_four', ['commonid' => $common_id]));
  515. } else {
  516. $this->error(lang('ds_common_save_fail'));
  517. }
  518. }else{
  519. $this->redirect((string) url('Sellergoodsadd/add_step_four', ['commonid' => $common_id]));
  520. }
  521. }
  522. }
  523. /**
  524. * 商品发布第四步
  525. */
  526. public function add_step_four() {
  527. $commonid = input('param.commonid');
  528. // 单条商品信息
  529. $goods_info = model('goods')->getGoodsInfo(array('goods_commonid' => $commonid));
  530. // 自动发布动态
  531. $data_array = array();
  532. $data_array['goods_id'] = $goods_info['goods_id'];
  533. $data_array['store_id'] = $goods_info['store_id'];
  534. $data_array['goods_name'] = $goods_info['goods_name'];
  535. $data_array['goods_image'] = $goods_info['goods_image'];
  536. $data_array['goods_price'] = $goods_info['goods_price'];
  537. $data_array['goods_transfee_charge'] = $goods_info['goods_freight'] == 0 ? 1 : 0;
  538. $data_array['goods_freight'] = $goods_info['goods_freight'];
  539. //自动发布店铺动态
  540. $this->storeAutoShare($data_array, 'new');
  541. View::assign('allow_gift', model('goods')->checkGoodsIfAllowGift($goods_info));
  542. View::assign('allow_combo', model('goods')->checkGoodsIfAllowCombo($goods_info));
  543. View::assign('goods_id', $goods_info['goods_id']);
  544. /* 设置卖家当前菜单 */
  545. $this->setSellerCurMenu('sellergoodsadd');
  546. $this->setSellerCurItem();
  547. return View::fetch($this->template_dir . 'store_goods_add_step4');
  548. }
  549. /**
  550. * 上传图片
  551. */
  552. public function image_upload() {
  553. // 判断图片数量是否超限
  554. $album_model = model('album');
  555. $album_limit = $this->store_grade['storegrade_album_limit'];
  556. if ($album_limit > 0) {
  557. $album_count = $album_model->getCount(array('store_id' => session('store_id')));
  558. if ($album_count >= $album_limit) {
  559. $error = lang('store_goods_album_climit');
  560. exit(json_encode(array('error' => $error)));
  561. }
  562. }
  563. $aclass_id=input('param.aclass_id');
  564. if($aclass_id){
  565. $class_info = $album_model->getOne(array('store_id' => session('store_id'), 'aclass_id' => $aclass_id), 'albumclass');
  566. }
  567. if(!$aclass_id || !$class_info){
  568. $class_info = $album_model->getOne(array('store_id' => session('store_id'), 'aclass_isdefault' => 1), 'albumclass');
  569. }
  570. $store_id = session('store_id');
  571. /**
  572. * 上传图片
  573. */
  574. $index=intval(input('param.index'));
  575. $time=TIMESTAMP;
  576. //上传文件保存路径
  577. $upload_path = ATTACH_GOODS . '/' . $store_id . '/' . date('Ymd',$time);
  578. $save_name = session('store_id') . '_' . date('YmdHis',$time) . ($index?(10000+$index):(rand(20000, 99999)));
  579. $file_name = input('post.name');
  580. $result = upload_albumpic($upload_path, $file_name, $save_name);
  581. if ($result['code'] == '10000') {
  582. $img_path = $result['result'];
  583. list($width, $height, $type, $attr) = getimagesize($img_path);
  584. $img_path = substr(strrchr($img_path, "/"), 1);
  585. } else {
  586. //未上传图片或出错不做后面处理
  587. exit;
  588. }
  589. // 存入相册
  590. $insert_array = array();
  591. $insert_array['apic_name'] = $img_path;
  592. $insert_array['apic_tag'] = '';
  593. $insert_array['aclass_id'] = $class_info['aclass_id'];
  594. $insert_array['apic_cover'] = $img_path;
  595. $insert_array['apic_size'] = intval($_FILES[$file_name]['size']);
  596. $insert_array['apic_spec'] = $width . 'x' . $height;
  597. $insert_array['apic_uploadtime'] = $time;
  598. $insert_array['store_id'] = $store_id;
  599. $result = model('album')->addAlbumpic($insert_array);
  600. $data = array();
  601. $data ['thumb_name'] = goods_cthumb($img_path, 240, session('store_id'));
  602. $data ['name'] = $img_path;
  603. // 整理为json格式
  604. $output = json_encode($data);
  605. echo $output;
  606. exit();
  607. }
  608. /**
  609. * ajax获取商品分类的子级数据
  610. */
  611. public function ajax_goods_class() {
  612. $gc_id = intval(input('get.gc_id'));
  613. $deep = intval(input('get.deep'));
  614. if ($gc_id <= 0 || $deep <= 0 || $deep >= 4) {
  615. exit(json_encode(array()));
  616. }
  617. $goodsclass_model = model('goodsclass');
  618. $list = $goodsclass_model->getGoodsclass(session('store_id'), $gc_id, $deep);
  619. if (empty($list)) {
  620. exit(json_encode(array()));
  621. }
  622. echo json_encode($list);
  623. }
  624. /**
  625. * ajax删除常用分类
  626. */
  627. public function ajax_stapledel() {
  628. $staple_id = intval(input('get.staple_id'));
  629. if ($staple_id < 1) {
  630. echo json_encode(array('done' => false, 'msg' => lang('param_error')));
  631. die();
  632. }
  633. /**
  634. * 实例化模型
  635. */
  636. $staple_model = model('goodsclassstaple');
  637. $result = $staple_model->delGoodsclassstaple(array('staple_id' => $staple_id, 'member_id' => session('member_id')));
  638. if ($result) {
  639. echo json_encode(array(
  640. 'done' => true
  641. ));
  642. die();
  643. } else {
  644. echo json_encode(array(
  645. 'done' => false, 'msg' => ''
  646. ));
  647. die();
  648. }
  649. }
  650. /**
  651. * ajax选择常用商品分类
  652. */
  653. public function ajax_show_comm() {
  654. $staple_id = intval(input('get.stapleid'));
  655. /**
  656. * 查询相应的商品分类id
  657. */
  658. $staple_model = model('goodsclassstaple');
  659. $staple_info = $staple_model->getGoodsclassstapleInfo(array('staple_id' => intval($staple_id)), 'gc_id_1,gc_id_2,gc_id_3');
  660. if (empty($staple_info) || !is_array($staple_info)) {
  661. echo json_encode(array(
  662. 'done' => false, 'msg' => ''
  663. ));
  664. die();
  665. }
  666. $list_array = array();
  667. $list_array['gc_id'] = 0;
  668. $list_array['type_id'] = $staple_info['type_id'];
  669. $list_array['done'] = true;
  670. $list_array['one'] = '';
  671. $list_array['two'] = '';
  672. $list_array['three'] = '';
  673. $gc_id_1 = intval($staple_info['gc_id_1']);
  674. $gc_id_2 = intval($staple_info['gc_id_2']);
  675. $gc_id_3 = intval($staple_info['gc_id_3']);
  676. /**
  677. * 查询同级分类列表
  678. */
  679. $goodsclass_model = model('goodsclass');
  680. // 1级
  681. if ($gc_id_1 > 0) {
  682. $list_array['gc_id'] = $gc_id_1;
  683. $class_list = $goodsclass_model->getGoodsclass(session('store_id'));
  684. if (empty($class_list) || !is_array($class_list)) {
  685. echo json_encode(array('done' => false, 'msg' => ''));
  686. die();
  687. }
  688. foreach ($class_list as $val) {
  689. if ($val ['gc_id'] == $gc_id_1) {
  690. $list_array ['one'] .= '<li class="" onclick="selClass($(this));" data-param="{gcid:' . $val ['gc_id'] . ', deep:1, tid:' . $val ['type_id'] . '}" dstype="selClass"> <a class="classDivClick" href="javascript:void(0)"><span class="has_leaf"><i class="iconfont"></i>' . $val ['gc_name'] . '</span></a> </li>';
  691. } else {
  692. $list_array ['one'] .= '<li class="" onclick="selClass($(this));" data-param="{gcid:' . $val ['gc_id'] . ', deep:1, tid:' . $val ['type_id'] . '}" dstype="selClass"> <a class="" href="javascript:void(0)"><span class="has_leaf"><i class="iconfont"></i>' . $val ['gc_name'] . '</span></a> </li>';
  693. }
  694. }
  695. }
  696. // 2级
  697. if ($gc_id_2 > 0) {
  698. $list_array['gc_id'] = $gc_id_2;
  699. $class_list = $goodsclass_model->getGoodsclass(session('store_id'), $gc_id_1, 2);
  700. if (empty($class_list) || !is_array($class_list)) {
  701. echo json_encode(array(
  702. 'done' => false, 'msg' => ''
  703. ));
  704. die();
  705. }
  706. foreach ($class_list as $val) {
  707. if ($val ['gc_id'] == $gc_id_2) {
  708. $list_array ['two'] .= '<li class="" onclick="selClass($(this));" data-param="{gcid:' . $val ['gc_id'] . ', deep:2, tid:' . $val ['type_id'] . '}" dstype="selClass"> <a class="classDivClick" href="javascript:void(0)"><span class="has_leaf"><i class="iconfont"></i>' . $val ['gc_name'] . '</span></a> </li>';
  709. } else {
  710. $list_array ['two'] .= '<li class="" onclick="selClass($(this));" data-param="{gcid:' . $val ['gc_id'] . ', deep:2, tid:' . $val ['type_id'] . '}" dstype="selClass"> <a class="" href="javascript:void(0)"><span class="has_leaf"><i class="iconfont"></i>' . $val ['gc_name'] . '</span></a> </li>';
  711. }
  712. }
  713. }
  714. // 3级
  715. if ($gc_id_3 > 0) {
  716. $list_array['gc_id'] = $gc_id_3;
  717. $class_list = $goodsclass_model->getGoodsclass(session('store_id'), $gc_id_2, 3);
  718. if (empty($class_list) || !is_array($class_list)) {
  719. echo json_encode(array(
  720. 'done' => false, 'msg' => ''
  721. ));
  722. die();
  723. }
  724. foreach ($class_list as $val) {
  725. if ($val ['gc_id'] == $gc_id_3) {
  726. $list_array ['three'] .= '<li class="" onclick="selClass($(this));" data-param="{gcid:' . $val ['gc_id'] . ', deep:3, tid:' . $val ['type_id'] . '}" dstype="selClass"> <a class="classDivClick" href="javascript:void(0)"><span class="has_leaf"><i class="iconfont"></i>' . $val ['gc_name'] . '</span></a> </li>';
  727. } else {
  728. $list_array ['three'] .= '<li class="" onclick="selClass($(this));" data-param="{gcid:' . $val ['gc_id'] . ', deep:3, tid:' . $val ['type_id'] . '}" dstype="selClass"> <a class="" href="javascript:void(0)"><span class="has_leaf"><i class="iconfont"></i>' . $val ['gc_name'] . '</span></a> </li>';
  729. }
  730. }
  731. }
  732. echo json_encode($list_array);
  733. die();
  734. }
  735. /**
  736. * AJAX添加商品规格值
  737. */
  738. public function ajax_add_spec() {
  739. $name = trim(input('get.name'));
  740. $gc_id = intval(input('get.gc_id'));
  741. $sp_id = intval(input('get.sp_id'));
  742. if ($name == '' || $gc_id <= 0 || $sp_id <= 0) {
  743. echo json_encode(array('done' => false));
  744. die();
  745. }
  746. $insert = array(
  747. 'spvalue_name' => $name, 'sp_id' => $sp_id, 'gc_id' => $gc_id, 'store_id' => session('store_id'),
  748. 'spvalue_color' => null, 'spvalue_sort' => 0,
  749. );
  750. $value_id = model('spec')->addSpecvalue($insert);
  751. if ($value_id) {
  752. echo json_encode(array('done' => true, 'value_id' => $value_id));
  753. die();
  754. } else {
  755. echo json_encode(array('done' => false));
  756. die();
  757. }
  758. }
  759. /**
  760. * AJAX查询品牌
  761. */
  762. public function ajax_get_brand() {
  763. $type_id = intval(input('tid'));
  764. $initial = trim(input('letter'));
  765. $keyword = trim(input('keyword'));
  766. $type = trim(input('type'));
  767. if (!in_array($type, array(
  768. 'letter', 'keyword'
  769. )) || ($type == 'letter' && empty($initial)) || ($type == 'keyword' && empty($keyword))) {
  770. echo json_encode(array());
  771. die();
  772. }
  773. // 实例化模型
  774. $type_model = model('type');
  775. $where = array();
  776. $where[]=array('type_id','=',$type_id);
  777. // 验证类型是否关联品牌
  778. $count = $type_model->getTypebrandCount($where);
  779. if ($type == 'letter') {
  780. switch ($initial) {
  781. case 'all':
  782. break;
  783. case '0-9':
  784. $where[] = array('brand_initial','in', array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
  785. break;
  786. default:
  787. $where[]=array('brand_initial','=',$initial);
  788. break;
  789. }
  790. } else {
  791. $where[] = array('brand_name|brand_initial', 'like', '%' . $keyword . '%');
  792. }
  793. if ($count > 0) {
  794. $brand_array = $type_model->typeRelatedJoinList($where, 'brand', 'brand.brand_id,brand.brand_name,brand.brand_initial');
  795. }
  796. echo json_encode($brand_array);
  797. die();
  798. }
  799. }