CreateGoodsService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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\GoodsParam;
  10. use app\backend\modules\goods\models\Goods;
  11. use app\backend\modules\goods\models\Brand;
  12. use app\backend\modules\goods\models\GoodsSpec;
  13. use app\backend\modules\goods\models\GoodsOption;
  14. use app\backend\modules\goods\models\GoodsVideo;
  15. use app\common\events\goods\GoodsCreateEvent;
  16. use Setting;
  17. class CreateGoodsService
  18. {
  19. public $params;
  20. public $brands;
  21. public $request;
  22. public $error = null;
  23. public $catetory_menus;
  24. /**
  25. * @var \app\common\models\Goods
  26. */
  27. public $goods_model;
  28. public $type;
  29. public function __construct($request, $type = 0)
  30. {
  31. $this->type = $type;
  32. $this->request = $request;
  33. }
  34. public function create()
  35. {
  36. $goods_data = $this->request->goods;
  37. $this->goods_model = $this->getGoodsModel();
  38. if ($goods_data) {
  39. //正则匹配富文本更改图片标签
  40. if ($goods_data['content']) {
  41. $goods_data['content'] = changeUmImgPath($goods_data['content']);
  42. }
  43. // 正则匹配富文本更改视频标签样式
  44. //$goods_data['content'] = preg_replace(htmlspecialchars('/<p[^>]*/'), htmlspecialchars('<p style="display: inline-block;"'), $goods_data['content']);
  45. $goods_data['content'] = preg_replace('/class="[^=]*/', 'class="edui-upload-video" controls', htmlspecialchars_decode($goods_data['content']));
  46. preg_match('/<video[^>]*/', $goods_data['content'], $matches);
  47. // $matches[0] .= ' x5-playsinline="true" webkit-playsinline="true" playsinline="true"';
  48. //
  49. // $goods_data['content'] = preg_replace('/<video[^>]*/', $matches[0], $goods_data['content']);
  50. $video_matche= '<video x5-playsinline="true" webkit-playsinline="true" playsinline="true" ';
  51. $goods_data['content'] = str_replace('<video', $video_matche, $goods_data['content']);
  52. $goods_data['content'] = htmlspecialchars($goods_data['content']);
  53. if ($this->type == 1) {
  54. $goods_data['status'] = 0;
  55. }
  56. // //商品视频地址
  57. // $goods_data['goods_video'] = yz_tomedia($goods_data['goods_video']);
  58. if (isset($goods_data['thumb_url'])) {
  59. $goods_data['thumb_url'] = serialize($goods_data['thumb_url']);
  60. }
  61. if (!$goods_data['virtual_sales']) {
  62. $goods_data['virtual_sales'] = 0;
  63. }
  64. if (!empty($this->request->widgets['sale']['max_point_deduct'])
  65. && !empty($goods_data['price'])
  66. && $this->request->widgets['sale']['max_point_deduct'] > $goods_data['price']) {
  67. return ['status' => -1, 'msg' => '积分最大抵扣金额大于商品现价'];
  68. }
  69. if (!empty($this->request->widgets['sale']['min_point_deduct'])
  70. && !empty($goods_data['price'])
  71. && $this->request->widgets['sale']['min_point_deduct'] > $goods_data['price']) {
  72. return ['status' => -1, 'msg' => '积分最少抵扣金额大于商品现价'];
  73. }
  74. if(empty($goods_data['price'])){
  75. $goods_data['price'] = 0;
  76. }
  77. if(empty($goods_data['cost_price'])){
  78. $goods_data['cost_price'] = 0;
  79. }
  80. $goods_data['has_option'] = $this->request->widgets['option']['has_option'] ?: 0;
  81. $save_data = array_except($goods_data,['category','withhold_stock','video_image','goods_video','category_to_option']);
  82. $this->goods_model->fill($save_data);
  83. $this->goods_model->widgets = $this->request->widgets;
  84. $this->setAfterHandle();
  85. $this->goods_model->uniacid = \YunShop::app()->uniacid;
  86. $this->goods_model->weight = $this->goods_model->weight ? $this->goods_model->weight : 0;
  87. $validator = $this->goods_model->validator($this->goods_model->getAttributes());
  88. if ($validator->fails()) {
  89. return ['status' => -1, 'msg' => $validator->messages()->first()];
  90. } else {
  91. if ($this->goods_model->save()) {
  92. (new \app\common\services\operation\GoodsLog($this->goods_model, 'create'));
  93. //商品视频保存
  94. GoodsVideo::store($this->goods_model->id, array_only($goods_data,['video_image','goods_video']));
  95. //商品分类保存
  96. GoodsService::store($this->goods_model->id, $goods_data['category'], \Setting::get('shop.category')['cat_level'],$goods_data['category_to_option']);
  97. //商品属性保存
  98. GoodsParam::store($this->goods_model->id,$this->request->widgets['param']);
  99. //规格项和规格组合保存
  100. SpecOptionService::store($this->goods_model->id,$this->request->widgets['option'],\YunShop::app()->uniacid);
  101. // GoodsSpec::saveSpec($this->request, $this->goods_model->id, \YunShop::app()->uniacid);
  102. // GoodsOption::saveOption($this->request, $this->goods_model->id, GoodsSpec::$spec_items, \YunShop::app()->uniacid);
  103. //商品保存之后
  104. $this->afterSaving();
  105. return ['status' => 1];
  106. } else {
  107. return ['status' => -1,'msg'=> '保存失败'];
  108. }
  109. }
  110. }
  111. return ['status' => -1,'msg'=>'商品保存失败'];
  112. }
  113. public function oldcreate()
  114. {
  115. $goods_data = $this->request->goods;
  116. $this->params = new GoodsParam();
  117. $this->goods_model = $this->getGoodsModel();
  118. $this->brands = Brand::getBrands()->getQuery()->select(['id','name'])->get();
  119. if ($goods_data) {
  120. //正则匹配富文本更改图片标签
  121. if ($goods_data['content']) {
  122. $goods_data['content'] = changeUmImgPath($goods_data['content']);
  123. }
  124. // 正则匹配富文本更改视频标签样式
  125. //$goods_data['content'] = preg_replace(htmlspecialchars('/<p[^>]*/'), htmlspecialchars('<p style="display: inline-block;"'), $goods_data['content']);
  126. $goods_data['content'] = preg_replace('/class="[^=]*/', 'class="edui-upload-video" controls', htmlspecialchars_decode($goods_data['content']));
  127. preg_match('/<video[^>]*/', $goods_data['content'], $matches);
  128. // $matches[0] .= ' x5-playsinline="true" webkit-playsinline="true" playsinline="true"';
  129. //
  130. // $goods_data['content'] = preg_replace('/<video[^>]*/', $matches[0], $goods_data['content']);
  131. $video_matche= '<video x5-playsinline="true" webkit-playsinline="true" playsinline="true" ';
  132. $goods_data['content'] = str_replace('<video', $video_matche, $goods_data['content']);
  133. $goods_data['content'] = htmlspecialchars($goods_data['content']);
  134. if ($this->type == 1) {
  135. $goods_data['status'] = 0;
  136. }
  137. // //商品视频地址
  138. // $goods_data['goods_video'] = yz_tomedia($goods_data['goods_video']);
  139. if (isset($goods_data['thumb_url'])) {
  140. $goods_data['thumb_url'] = serialize($goods_data['thumb_url']);
  141. }
  142. if (!$goods_data['virtual_sales']) {
  143. $goods_data['virtual_sales'] = 0;
  144. }
  145. if (!empty($this->request->widgets['sale']['max_point_deduct'])
  146. && !empty($goods_data['price'])
  147. && $this->request->widgets['sale']['max_point_deduct'] > $goods_data['price']) {
  148. return ['status' => -1, 'msg' => '积分最大抵扣金额大于商品现价'];
  149. }
  150. if (!empty($this->request->widgets['sale']['min_point_deduct'])
  151. && !empty($goods_data['price'])
  152. && $this->request->widgets['sale']['min_point_deduct'] > $goods_data['price']) {
  153. return ['status' => -1, 'msg' => '积分最少抵扣金额大于商品现价'];
  154. }
  155. if(empty($goods_data['price'])){
  156. $goods_data['price'] = 0;
  157. }
  158. if(empty($goods_data['cost_price'])){
  159. $goods_data['cost_price'] = 0;
  160. }
  161. if (mb_strlen($this->request['widgets']['advertising']['copywriting']) > 100) {
  162. return ['status' => -1, 'msg' => '广告宣传语文案输入超过100,请重新输入'];
  163. }
  164. $this->goods_model->fill($goods_data);
  165. $this->goods_model->widgets = $this->request->widgets;
  166. $this->goods_model->uniacid = \YunShop::app()->uniacid;
  167. $this->goods_model->weight = $this->goods_model->weight ? $this->goods_model->weight : 0;
  168. $validator = $this->goods_model->validator($this->goods_model->getAttributes());
  169. if ($validator->fails()) {
  170. $this->error = $validator->messages();
  171. } else {
  172. if ($this->goods_model->save()) {
  173. (new \app\common\services\operation\GoodsLog($this->goods_model, 'create'));
  174. GoodsService::saveGoodsMultiCategory($this->goods_model, $this->request->category, Setting::get('shop.category'));
  175. GoodsParam::saveParam($this->request, $this->goods_model->id, \YunShop::app()->uniacid);
  176. GoodsSpec::saveSpec($this->request, $this->goods_model->id, \YunShop::app()->uniacid);
  177. GoodsOption::saveOption($this->request, $this->goods_model->id, GoodsSpec::$spec_items, \YunShop::app()->uniacid);
  178. return ['status' => 1];
  179. } else {
  180. return ['status' => -1];
  181. }
  182. }
  183. }
  184. $this->catetory_menus = CategoryService::getCategoryMultiMenu(['catlevel' => Setting::get('shop.category')['cat_level']]);
  185. }
  186. //商品模型保存后
  187. public function afterSaving()
  188. {
  189. event(new GoodsCreateEvent($this->goods_model));
  190. }
  191. public function setAfterHandle()
  192. {
  193. }
  194. public function setGoodsModel($model)
  195. {
  196. $this->goods_model = $model;
  197. }
  198. protected function getGoodsModel()
  199. {
  200. if (isset($this->goods_model)) {
  201. return $this->goods_model;
  202. }
  203. return new Goods();
  204. }
  205. }