Sellergoodsadd.php 40 KB

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