Sellergoodsadd.php 43 KB

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