EditGoodsService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/5/2
  6. * Time: 上午11:51
  7. */
  8. namespace app\backend\modules\goods\services;
  9. use app\backend\modules\goods\models\Goods;
  10. use app\backend\modules\goods\models\GoodsSpecItem;
  11. use app\backend\modules\goods\models\GoodsParam;
  12. use app\backend\modules\goods\models\GoodsSpec;
  13. use app\backend\modules\goods\models\GoodsOption;
  14. use app\backend\modules\goods\models\Brand;
  15. use app\backend\modules\goods\models\GoodsVideo;
  16. use app\common\events\goods\GoodsChangeEvent;
  17. use app\common\models\GoodsCategory;
  18. use Setting;
  19. class EditGoodsService
  20. {
  21. public $goods_id;
  22. public $goods_model;
  23. public $request;
  24. public $catetory_menus;
  25. public $brands;
  26. public $optionsHtml;
  27. public $type;
  28. public function __construct($goods_id, $request, $type = 0)
  29. {
  30. $this->type = $type;
  31. $this->goods_id = $goods_id;
  32. $this->request = $request;
  33. $this->goods_model = Goods::with([
  34. 'hasOneGoodsVideo',
  35. 'hasManyGoodsCategory',
  36. 'hasManyParams' => function ($query) {
  37. $query->orderBy('displayorder', 'asc');
  38. },
  39. 'hasManySpecs' => function ($query) {
  40. $query->orderBy('display_order', 'asc');
  41. }
  42. ])->find($goods_id);
  43. }
  44. public function edit()
  45. {
  46. //获取规格名及规格项
  47. $goods_data = $this->request->goods;
  48. if ($goods_data) {
  49. //正则匹配富文本更改图片标签
  50. if ($goods_data['content']) {
  51. $goods_data['content'] = changeUmImgPath($goods_data['content']);
  52. }
  53. preg_match('/<video[^>]*/', $goods_data['content'], $matches);
  54. $video_matche = '<video x5-playsinline="true" webkit-playsinline="true" playsinline="true" ';
  55. $goods_data['content'] = str_replace('<video', $video_matche, $goods_data['content']);
  56. $goods_data['content'] = htmlspecialchars($goods_data['content']);
  57. if ($this->type == 1) {
  58. $goods_data['status'] = 0;
  59. }
  60. if (!$goods_data['virtual_sales']) {
  61. $goods_data['virtual_sales'] = 0;
  62. }
  63. $goods_data['has_option'] = $this->request->widgets['option']['has_option'] ?: 0;
  64. $goods_data['weight'] = $goods_data['weight'] ? $goods_data['weight'] : 0;
  65. if (isset($goods_data['thumb_url'])) {
  66. $goods_data['thumb_url'] = serialize($goods_data['thumb_url']);
  67. } else {
  68. $goods_data['thumb_url'] = '';
  69. }
  70. if (empty($this->request->widgets['sale']['point_deduct_type']) && !empty($this->request->widgets['sale']['max_point_deduct']) && !empty($goods_data['price'])
  71. && $this->request->widgets['sale']['max_point_deduct'] > $goods_data['price']) {
  72. return ['status' => -1, 'msg' => '积分抵扣金额大于商品现价'];
  73. }
  74. $goods_data['price'] = $goods_data['price'] ?: 0;
  75. $goods_data['market_price'] = $goods_data['market_price'] ?: 0;
  76. $goods_data['cost_price'] = $goods_data['cost_price'] ?: 0;
  77. $goods_data['brand_id'] = $goods_data['brand_id'] ?: 0;
  78. $save_data = array_except(
  79. $goods_data,
  80. ['category', 'withhold_stock', 'video_image', 'goods_video', 'category_to_option']
  81. );
  82. $this->goods_model->fill($save_data);
  83. $this->goods_model->widgets = $this->request->widgets;
  84. //其他字段赋值
  85. $this->goods_model->uniacid = \YunShop::app()->uniacid;
  86. //数据保存
  87. $validator = $this->goods_model->validator($this->goods_model->getAttributes());
  88. if ($validator->fails()) {
  89. return ['status' => -1, 'msg' => $validator->messages()];
  90. //$this->error($validator->messages());
  91. } else {
  92. $this->goods_model->hasManyOptions;
  93. $goods_model = clone $this->goods_model;
  94. if ($this->goods_model->save()) {
  95. //商品视频保存
  96. GoodsVideo::store($this->goods_model->id, array_only($goods_data, ['video_image', 'goods_video']));
  97. //商品分类保存
  98. GoodsService::store(
  99. $this->goods_model->id,
  100. $goods_data['category'],
  101. \Setting::get('shop.category')['cat_level'],
  102. $goods_data['category_to_option']
  103. );
  104. //商品属性保存
  105. GoodsParam::store($this->goods_model->id, $this->request->widgets['param']);
  106. //规格项和规格组合保存
  107. SpecOptionService::store(
  108. $this->goods_model->id,
  109. $this->request->widgets['option'],
  110. \YunShop::app()->uniacid
  111. );
  112. event(new GoodsChangeEvent($goods_model));//todo 有时需要监听商品更改过后规格的变化,挂件更改在规格保存之前,无法使用
  113. //显示信息并跳转
  114. return ['status' => 1];
  115. } else {
  116. return ['status' => -1, 'msg' => '商品保存失败'];
  117. }
  118. }
  119. }
  120. return ['status' => -1, 'msg' => '参数为空'];
  121. }
  122. public function oldedit()
  123. {
  124. //商品属性默认值
  125. $arrt_default = [
  126. 'is_recommand' => 0,
  127. 'is_new' => 0,
  128. 'is_hot' => 0,
  129. 'is_discount' => 0
  130. ];
  131. //获取规格名及规格项
  132. $goods_data = $this->request->goods;
  133. $goods_data = array_merge($arrt_default, $goods_data);
  134. foreach ($this->goods_model->hasManySpecs as &$spec) {
  135. $spec['items'] = GoodsSpecItem::where('specid', $spec['id'])->orderBy('display_order', 'asc')->get(
  136. )->toArray();
  137. }
  138. //获取具体规格内容html
  139. $this->optionsHtml = GoodsOptionService::getOptions($this->goods_id, $this->goods_model->hasManySpecs);
  140. //商品其它图片反序列化
  141. $this->goods_model->thumb_url = !empty($this->goods_model->thumb_url) ? unserialize(
  142. $this->goods_model->thumb_url
  143. ) : [];
  144. $this->goods_model->withhold_stock = $this->goods_model->withhold_stock;
  145. if ($goods_data) {
  146. //正则匹配富文本更改图片标签
  147. if ($goods_data['content']) {
  148. $goods_data['content'] = changeUmImgPath($goods_data['content']);
  149. }
  150. // 正则匹配富文本更改视频标签样式
  151. //$goods_data['content'] = preg_replace(htmlspecialchars('/<p[^>]*/'), htmlspecialchars('<p style="display: inline-block;"'), $goods_data['content']);
  152. $goods_data['content'] = preg_replace(
  153. '/class="[^=]*/',
  154. 'class="edui-upload-video" controls',
  155. htmlspecialchars_decode($goods_data['content'])
  156. );
  157. preg_match('/<video[^>]*/', $goods_data['content'], $matches);
  158. // $matches[0] .= ' x5-playsinline="true" webkit-playsinline="true" playsinline="true"';
  159. //
  160. // $goods_data['content'] = preg_replace('/<video[^>]*/', $matches[0], $goods_data['content']);
  161. $video_matche = '<video x5-playsinline="true" webkit-playsinline="true" playsinline="true" ';
  162. $goods_data['content'] = str_replace('<video', $video_matche, $goods_data['content']);
  163. $goods_data['content'] = htmlspecialchars($goods_data['content']);
  164. if ($this->type == 1) {
  165. $goods_data['status'] = 0;
  166. }
  167. if (!$goods_data['virtual_sales']) {
  168. $goods_data['virtual_sales'] = 0;
  169. }
  170. $goods_data['has_option'] = $goods_data['has_option'] ? $goods_data['has_option'] : 0;
  171. $goods_data['weight'] = $goods_data['weight'] ? $goods_data['weight'] : 0;
  172. if (isset($goods_data['thumb_url'])) {
  173. $goods_data['thumb_url'] = serialize($goods_data['thumb_url']);
  174. } else {
  175. $goods_data['thumb_url'] = '';
  176. }
  177. $category_model = GoodsCategory::where("goods_id", $this->goods_model->id)->first();
  178. if (!empty($category_model)) {
  179. $category_model->delete();
  180. }
  181. GoodsService::saveGoodsMultiCategory(
  182. $this->goods_model,
  183. \YunShop::request()->category,
  184. Setting::get('shop.category')
  185. );
  186. if (!empty($this->request->widgets['sale']['max_point_deduct'])
  187. && !empty($goods_data['price'])
  188. && $this->request->widgets['sale']['max_point_deduct'] > $goods_data['price']) {
  189. return ['status' => -1, 'msg' => '积分抵扣金额大于商品现价'];
  190. }
  191. if (mb_strlen($this->request['widgets']['advertising']['copywriting']) > 100) {
  192. return ['status' => -1, 'msg' => '广告宣传语文案输入超过100,请重新输入'];
  193. }
  194. $goods_data['price'] = $goods_data['price'] ?: 0;
  195. $goods_data['market_price'] = $goods_data['market_price'] ?: 0;
  196. $goods_data['cost_price'] = $goods_data['cost_price'] ?: 0;
  197. $this->goods_model->setRawAttributes($goods_data);
  198. $this->goods_model->widgets = $this->request->widgets;
  199. //其他字段赋值
  200. $this->goods_model->uniacid = \YunShop::app()->uniacid;
  201. $this->goods_model->id = $this->goods_id;
  202. //数据保存
  203. $validator = $this->goods_model->validator($this->goods_model->getAttributes());
  204. if ($validator->fails()) {
  205. return ['status' => -1, 'msg' => $validator->messages()];
  206. //$this->error($validator->messages());
  207. } else {
  208. if ($this->goods_model->save()) {
  209. GoodsParam::saveParam($this->request, $this->goods_model->id, \YunShop::app()->uniacid);
  210. GoodsSpec::saveSpec($this->request, $this->goods_model->id, \YunShop::app()->uniacid);
  211. GoodsOption::saveOption(
  212. $this->request,
  213. $this->goods_model->id,
  214. GoodsSpec::$spec_items,
  215. \YunShop::app()->uniacid
  216. );
  217. event(new GoodsChangeEvent($this->goods_model));//todo 有时需要监听商品更改过后规格的变化,挂件更改在规格保存之前,无法使用
  218. //显示信息并跳转
  219. return ['status' => 1];
  220. } else {
  221. return ['status' => -1];
  222. }
  223. }
  224. }
  225. $this->brands = Brand::getBrand($this->goods_model->brand_id);
  226. if (isset($this->goods_model->hasManyGoodsCategory[0])) {
  227. foreach ($goods_categorys = $this->goods_model->hasManyGoodsCategory->toArray() as $goods_category) {
  228. $this->catetory_menus[] = CategoryService::getCategoryMultiMenu(
  229. [
  230. 'catlevel' => Setting::get('shop.category')['cat_level'],
  231. 'ids' => explode(",", $goods_category['category_ids'])
  232. ]
  233. );
  234. }
  235. } else {
  236. $this->catetory_menus[] = CategoryService::getCategoryMultiMenu(
  237. ['catlevel' => Setting::get('shop.category')['cat_level'], 'ids' => []]
  238. );
  239. }
  240. }
  241. }