Sellergoodsadd.php 43 KB

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