Sellergoodsadd.php 44 KB

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