NewGoodsController.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: weifeng
  5. * Date: 2020-01-06
  6. * Time: 11:25
  7. *
  8. * .--, .--,
  9. * ( ( \.---./ ) )
  10. * '.__/o o\__.'
  11. * {= ^ =}
  12. * > - <
  13. * / \
  14. * // \\
  15. * //| . |\\
  16. * "'\ /'"_.-~^`'-.
  17. * \ _ /--' `
  18. * ___)( )(___
  19. * (((__) (__))) 梦之所想,心之所向.
  20. */
  21. namespace app\frontend\modules\goods\controllers;
  22. use app\common\components\BaseController;
  23. use app\common\facades\Setting;
  24. use app\common\models\GoodsSpecItem;
  25. use app\common\models\OrderGoods;
  26. use app\common\services\goods\LeaseToyGoods;
  27. use app\common\services\goods\SaleGoods;
  28. use app\common\services\goods\VideoDemandCourseGoods;
  29. use app\framework\Http\Request;
  30. use app\frontend\modules\goods\models\Comment;
  31. use app\frontend\modules\goods\models\Goods;
  32. use Yunshop\Love\Common\Models\GoodsLove;
  33. use Yunshop\MarketSub\models\MarketSubGoods;
  34. use Yunshop\ProductMarket\models\ProductMarketGoodsModel;
  35. use Yunshop\StoreCashier\common\models\StoreSetting;
  36. use Yunshop\StoreCashier\store\models\StoreGoods;
  37. use Yunshop\Supplier\common\models\SupplierGoods;
  38. use Yunshop\ServiceFee\models\ServiceFeeModel;
  39. use app\frontend\models\Member;
  40. use Yunshop\TeamDividend\models\TeamDividendAgencyModel;
  41. use Yunshop\Commission\models\Agents;
  42. use Yunshop\TeamDividend\Common\Services\TeamDividendGoodsDetailService;
  43. use Yunshop\Commission\Common\Services\GoodsDetailService;
  44. use app\common\models\goods\Privilege;
  45. use Yunshop\TeamSales\common\services\GoodsListService;
  46. class NewGoodsController extends BaseController
  47. {
  48. public function getGoodsPage()
  49. {
  50. $request = request();
  51. $this->dataIntegrated($this->getGoods($request, true), 'get_goods');
  52. $this->dataIntegrated($this->getGoodsType($request, true), 'goods_type');
  53. $storeId = $this->apiData['get_goods']->store_goods->store_id;
  54. if ($storeId) {
  55. if (class_exists('\Yunshop\StoreCashier\frontend\store\NewGoodsController')) {
  56. if ($this->apiData['goods_type']['store_id']) {
  57. $this->dataIntegrated(\Yunshop\StoreCashier\frontend\store\NewGoodsController::getStoreService($request, true, $this->apiData['goods_type']['store_id']), 'store_service');
  58. }
  59. $this->dataIntegrated(\Yunshop\StoreCashier\frontend\store\NewGoodsController::getInfobyStoreId($request, true, $storeId), 'get_store_Info');
  60. } else {
  61. return $this->errorJson('门店插件未开启');
  62. }
  63. }
  64. if ($this->apiData['get_goods']->is_hotel) {
  65. if (class_exists('\Yunshop\Hotel\frontend\hotel\NewGoodsController')) {
  66. $this->dataIntegrated(\Yunshop\Hotel\frontend\hotel\NewGoodsController::getGoodsDetailByGoodsId($request, true), 'get_hotel_info');
  67. } else {
  68. return $this->errorJson('酒店插件未开启');
  69. }
  70. }
  71. if (Setting::get('shop.member')['display_page'] == 1 && Setting::get('shop.member')['level_type'] == 2) {
  72. $this->apiData['display_page'] = 1;
  73. } else {
  74. $this->apiData['display_page'] = 0;
  75. }
  76. $this->dataIntegrated($this->pluginEnabled(), 'pluginEnabled');
  77. return $this->successJson('', $this->apiData);
  78. }
  79. public function getGoods(Request $request, $integrated = null)
  80. {
  81. $id = intval(\YunShop::request()->id);
  82. if (!$id) {
  83. if (is_null($integrated)) {
  84. return $this->errorJson('请传入正确参数.');
  85. } else {
  86. return show_json(0, '请传入正确参数.');
  87. }
  88. }
  89. //获取商品模型
  90. $goodsModel = $this->_getGoods($id);
  91. //设置商品相关插件信息
  92. $this->setGoodsPluginsRelations($goodsModel);
  93. $this->goodsProductMarket($goodsModel);
  94. //供应商logo转格式
  95. if (!empty($goodsModel->supplier)) {
  96. $goodsModel->supplier->logo = yz_tomedia($goodsModel->supplier->logo);
  97. }
  98. //默认供应商店铺名称
  99. if ($goodsModel->supplier->store_name == 'null') {
  100. $goodsModel->supplier->store_name = $goodsModel->supplier->user_name;
  101. }
  102. //判断该商品是否是视频插件商品
  103. $videoDemand = new VideoDemandCourseGoods();
  104. $goodsModel->is_course = $videoDemand->isCourse($id);
  105. //商城租赁
  106. //TODO 租赁插件是否开启 $lease_switch
  107. $lease_switch = LeaseToyGoods::whetherEnabled();
  108. $this->goods_lease_set($goodsModel, $lease_switch);
  109. //判断是否酒店商品
  110. $goodsModel->is_hotel = $goodsModel->plugin_id == 33 ? 1 : 0;
  111. $goodsModel->is_store = $goodsModel->plugin_id == 32 ? 1 : 0;
  112. if (app('plugins')->isEnabled('label')) {
  113. $goodsModel->label = '';
  114. $pic = Setting::get('plugin.label');
  115. if ($pic['is_label']) {
  116. $goodsModel->label = $pic;
  117. }
  118. }
  119. //商品服务费
  120. $this->goodsServiceFree($goodsModel);
  121. if (is_null($integrated)) {
  122. return $this->successJson('成功', $goodsModel);
  123. } else {
  124. return show_json(1, $goodsModel);
  125. }
  126. }
  127. public function _getGoods($id, $integrated = null)
  128. {
  129. $goods_model = \app\common\modules\shop\ShopConfig::current()->get('goods.models.commodity_classification');
  130. $goods_model = new $goods_model;
  131. $goodsModel = $goods_model->uniacid()
  132. ->with([
  133. 'hasManyParams' => function ($query) {
  134. return $query->select('goods_id', 'title', 'value')->orderby('displayorder', 'asc');
  135. },
  136. 'hasManySpecs' => function ($query) {
  137. return $query->select('id', 'goods_id', 'title', 'description');
  138. },
  139. 'hasManyOptions' => function ($query) {
  140. return $query->select('id', 'goods_id', 'title', 'thumb', 'product_price', 'market_price', 'stock', 'specs', 'weight');
  141. },
  142. 'hasOneBrand' => function ($query) {
  143. return $query->select('id', 'logo', 'name', 'desc');
  144. },
  145. 'hasOneShare',
  146. 'hasOneGoodsDispatch',
  147. 'hasOneSale',
  148. 'hasOneGoodsLimitBuy',
  149. 'hasOneGoodsVideo',
  150. ])
  151. ->find($id);
  152. //todo 不知道干嘛用
  153. // $goodsModel->vip_level_status;
  154. if (!$goodsModel) {
  155. if (is_null($integrated)) {
  156. return $this->errorJson('商品不存在.');
  157. } else {
  158. return show_json(0, '商品不存在.');
  159. }
  160. }
  161. //限时购 todo 后期优化 应该是前端优化
  162. $current_time = time();
  163. if (!is_null($goodsModel->hasOneGoodsLimitBuy)) {
  164. if ($goodsModel->hasOneGoodsLimitBuy->end_time < $current_time && $goodsModel->hasOneGoodsLimitBuy->status == 1) {
  165. $goodsModel->status = 0;
  166. $goodsModel->save();
  167. }
  168. }
  169. //商品上下架验证
  170. if (!$goodsModel->status) {
  171. if (is_null($integrated)) {
  172. return $this->errorJson('商品已下架.');
  173. } else {
  174. return show_json(0, '商品已下架.');
  175. }
  176. }
  177. //商品品牌处理
  178. if ($goodsModel->hasOneBrand) {
  179. $goodsModel->hasOneBrand->desc = html_entity_decode($goodsModel->hasOneBrand->desc);
  180. $goodsModel->hasOneBrand->logo = yz_tomedia($goodsModel->hasOneBrand->logo);
  181. }
  182. //商品规格图片处理
  183. if ($goodsModel->hasManyOptions && $goodsModel->hasManyOptions->toArray()) {
  184. foreach ($goodsModel->hasManyOptions as &$item) {
  185. $item->thumb = replace_yunshop(yz_tomedia($item->thumb));
  186. }
  187. }
  188. //商品内容百度编辑器转换格式
  189. $goodsModel->content = html_entity_decode($goodsModel->content);
  190. if ($goodsModel->has_option) {
  191. $goodsModel->min_price = $goodsModel->hasManyOptions->min("product_price");
  192. $goodsModel->max_price = $goodsModel->hasManyOptions->max("product_price");
  193. $goodsModel->stock = $goodsModel->hasManyOptions->sum('stock');
  194. }
  195. foreach ($goodsModel->hasManySpecs as &$spec) {
  196. $spec['specitem'] = GoodsSpecItem::select('id', 'title', 'specid', 'thumb')->where('show', 1)->where('show', 1)->where('specid', $spec['id'])->orderBy('display_order', 'asc')->get();
  197. foreach ($spec['specitem'] as &$specitem) {
  198. $specitem['thumb'] = yz_tomedia($specitem['thumb']);
  199. }
  200. }
  201. $goodsModel->setHidden(
  202. [
  203. 'deleted_at',
  204. 'created_at',
  205. 'updated_at',
  206. 'cost_price',
  207. 'real_sales',
  208. 'is_deleted',
  209. 'reduce_stock_method',
  210. ]);
  211. //商品图片处理
  212. if ($goodsModel->thumb) {
  213. $goodsModel->thumb = yz_tomedia($goodsModel->thumb);
  214. }
  215. if ($goodsModel->thumb_url) {
  216. $thumb_url = unserialize($goodsModel->thumb_url);
  217. foreach ($thumb_url as &$item) {
  218. $item = yz_tomedia($item);
  219. }
  220. $goodsModel->thumb_url = $thumb_url;
  221. }
  222. //商品视频处理
  223. if (!is_null($goodsModel->hasOneGoodsVideo) && $goodsModel->hasOneGoodsVideo->goods_video) {
  224. $goodsModel->goods_video = yz_tomedia($goodsModel->hasOneGoodsVideo->goods_video);
  225. $goodsModel->video_image = $goodsModel->hasOneGoodsVideo->video_image ? yz_tomedia($goodsModel->hasOneGoodsVideo->video_image) : yz_tomedia($goodsModel->thumb);
  226. } else {
  227. $goodsModel->goods_video = '';
  228. $goodsModel->video_image = '';
  229. }
  230. //商品爱心值赠送设置
  231. $goodsModel->love_shoppin_gift = $this->loveShoppingGift($goodsModel);
  232. // 商品详情挂件
  233. if (\app\common\modules\shop\ShopConfig::current()->get('goods_detail')) {
  234. foreach (\app\common\modules\shop\ShopConfig::current()->get('goods_detail') as $key_name => $row) {
  235. $row_res = $row['class']::{$row['function']}($id, true);
  236. if ($row_res) {
  237. $goodsModel->$key_name = $row_res;
  238. //供应商在售商品总数
  239. $class = new $row['class']();
  240. if (method_exists($class, 'getGoodsIdsBySid')) {
  241. $supplier_goods_id = SupplierGoods::getGoodsIdsBySid($goodsModel->supplier->id);
  242. $supplier_goods_count = Goods::select('id')
  243. ->whereIn('id', $supplier_goods_id)
  244. ->where('status', 1)
  245. ->count();
  246. $goodsModel->supplier_goods_count = $supplier_goods_count;
  247. }
  248. }
  249. }
  250. }
  251. if ($goodsModel->hasOneShare) {
  252. $goodsModel->hasOneShare->share_thumb = yz_tomedia($goodsModel->hasOneShare->share_thumb);
  253. }
  254. //该商品下的推广
  255. $goodsModel->show_push = $goodsModel->hasOneSale ? SaleGoods::getPushGoods($goodsModel->hasOneSale) : [];
  256. //商品评论
  257. $goodsModel->get_comment = $this->getComment($id);
  258. //商品好评率
  259. $goodsModel->favorable_rate = $this->favorableRate($id);
  260. return $goodsModel;
  261. }
  262. public function getGoodsType(Request $request, $integrated = null)
  263. {
  264. app('db')->cacheSelect = true;
  265. $goods_type = 'goods';//通用
  266. $id = request()->id;
  267. if (!$id) {
  268. if (is_null($integrated)) {
  269. return $this->errorJson('请传入正确参数.');
  270. } else {
  271. return show_json(0, '请传入正确参数.');
  272. }
  273. }
  274. $goodsModel = Goods::uniacid()->find($id);
  275. $data['title'] = $goodsModel->title;
  276. // 商品详情挂件
  277. if (\app\common\modules\shop\ShopConfig::current()->get('goods_detail')) {
  278. foreach (\app\common\modules\shop\ShopConfig::current()->get('goods_detail') as $key_name => $row) {
  279. $row_res = $row['class']::{$row['function']}($id, true);
  280. if ($row_res) {
  281. $goodsModel->$key_name = $row_res;
  282. }
  283. }
  284. }
  285. //判断该商品是否是视频插件商品
  286. $isCourse = (new VideoDemandCourseGoods())->isCourse($id);
  287. if ($isCourse) {
  288. $goods_type = 'course';
  289. }
  290. //判断是否酒店商品
  291. if ($goodsModel->plugin_id == 33) {
  292. $goods_type = 'hotelGoods';
  293. }
  294. if ($goodsModel->plugin_id == 66) {
  295. $goods_type = 'voiceGoods';
  296. }
  297. //门店商品
  298. if ($goodsModel->plugin_id == 32 && $goodsModel->store_goods) {
  299. $goods_type = 'store_goods';
  300. $store_id = $goodsModel->store_goods->store_id;
  301. $data['store_id'] = $store_id;
  302. if (!is_null(\app\common\modules\shop\ShopConfig::current()->get('customer_development_judge'))) {
  303. $class = array_get(\app\common\modules\shop\ShopConfig::current()->get('customer_development_judge'), 'class');
  304. $function = array_get(\app\common\modules\shop\ShopConfig::current()->get('customer_development_judge'), 'function');
  305. $judge_res = $class::$function($goodsModel->id);
  306. if($judge_res == 1)
  307. {
  308. $data['customer_development'] = 1;
  309. }
  310. }
  311. }
  312. //供应商商品
  313. if ($goodsModel->plugin_id == 92 && $goodsModel->supplier) {
  314. $goods_type = 'supplierGoods';
  315. }
  316. //分期购车插件
  317. if ($goodsModel->plugin_id == 47) {
  318. $goods_type = 'staging_buy_car_goods';
  319. }
  320. //预约插件
  321. if ($goodsModel->plugin_id == 101) {
  322. $goods_type = 'appointment_goods';
  323. }
  324. //芸签电子合同插件
  325. if ($goodsModel->plugin_id == 103) {
  326. $goods_type = 'yun_sign_goods';
  327. }
  328. $data['goods_type'] = $goods_type;
  329. if (is_null($integrated)) {
  330. return $this->successJson('成功', $data);
  331. } else {
  332. return show_json(1, $data);
  333. }
  334. }
  335. public function loveShoppingGift($goodsModel)
  336. {
  337. $exist_love = app('plugins')->isEnabled('love');
  338. if ($exist_love) {
  339. $love_goods = $this->getLoveSet($goodsModel, $goodsModel->id);
  340. if ($love_goods['award'] && \Setting::get('love.goods_detail_show_love') == 2) {
  341. return '购买赠送' . $love_goods['award_proportion'] . $love_goods['name'];
  342. }
  343. }
  344. return '';
  345. }
  346. public function getLoveSet($goods, $goods_id)
  347. {
  348. $data = [
  349. 'name' => \Setting::get('love.name') ?: '爱心值',
  350. 'deduction' => 0, //是否开启爱心值抵扣 0否,1是
  351. 'deduction_proportion' => 0, //爱心值最高抵扣
  352. 'award' => 0, //是否开启爱心值奖励 0否,1是
  353. 'award_proportion' => 0, //奖励爱心值
  354. ];
  355. $love_set = \Setting::get('love');
  356. $res = app('plugins')->isEnabled('store-cashier');
  357. if ($res) {//门店抵扣设置
  358. $store_goods = StoreGoods::where('goods_id', $goods_id)->first();
  359. $love = StoreSetting::getStoreSettingByStoreId($store_goods->store_id)->where('key', 'love')->first();
  360. }
  361. $item = GoodsLove::ofGoodsId($goods->id)->first();
  362. $deduction = 0;
  363. $deduction_proportion = \Setting::get('love.deduction_proportion');
  364. if ($item->deduction) {//商品独立设置
  365. if ($love_set['deduction']) {
  366. $deduction_proportion = $love_set['deduction_proportion'];
  367. $deduction = $love_set['deduction'];
  368. }
  369. if (!empty($love) && $love->value['deduction_proportion'] && $love->value['deduction_proportion'] != 0) {//门店设置
  370. $deduction_proportion = $love->value['deduction_proportion'];
  371. $deduction = $love->value['deduction'];
  372. }
  373. if ($item->deduction_proportion && $item->deduction_proportion != 0) {
  374. $deduction_proportion = $item->deduction_proportion;
  375. $deduction = $item->deduction;
  376. }
  377. $data['deduction'] = $deduction;
  378. $data['deduction_proportion'] = $deduction_proportion . '%';
  379. }
  380. if ($item->award) {
  381. $award = $item->award;
  382. //爱心值插件设置
  383. $award_proportion = \Setting::get('love.award_proportion');
  384. //门店设置
  385. if (!empty($love) && $love->value['award_proportion'] && $love->value['award_proportion'] != 0) {
  386. $award_proportion = $love->value['award_proportion'];
  387. $award = $love->value['award'];
  388. }
  389. //商品独立设置
  390. if ($item->award_proportion && $item->award_proportion != 0) {
  391. $award_proportion = $item->award_proportion;
  392. $award = $item->award;
  393. }
  394. $data['award'] = $award;
  395. $data['award_proportion'] = $award_proportion . '%';
  396. }
  397. return $data;
  398. }
  399. public function getComment($goodsId)
  400. {
  401. $pageSize = 5;
  402. $list = Comment::getCommentsByGoods($goodsId)->paginate($pageSize);
  403. if ($list) {
  404. foreach ($list as &$item) {
  405. $item->reply_count = $item->hasManyReply->count('id');
  406. $item->head_img_url = $item->head_img_url ? replace_yunshop(yz_tomedia($item->head_img_url)) : yz_tomedia(\Setting::get('shop.shop.logo'));
  407. }
  408. //对评论图片进行处理,反序列化并组装完整图片url
  409. $list = $list->toArray();
  410. foreach ($list['data'] as &$item) {
  411. // 反序列化图片
  412. self::unSerializeImage($item);
  413. }
  414. return $list;
  415. }
  416. return $list;
  417. }
  418. public function favorableRate($id)
  419. {
  420. //不跟订单关联的新好评率计算公式
  421. $total = \app\common\models\Comment::where(['goods_id' => $id, 'type' => 1])->count('id');//总评论数
  422. if ($total <= 0) return '100%';
  423. $level_comment = \app\common\models\Comment::where(['goods_id' => $id, 'type' => 1])->sum('level');//已评论的分数
  424. $mark = bcmul($total, 5, 2);//最高总评分 = 总条数 * 5
  425. //最终好评率 = (已评论分数/最高总评分)/100
  426. $have_comment = bcmul(bcdiv($level_comment, $mark, 2), 100, 2);
  427. return $have_comment . '%';
  428. }
  429. public static function unSerializeImage(&$arrComment)
  430. {
  431. $arrComment['images'] = unserialize($arrComment['images']);
  432. foreach ($arrComment['images'] as &$image) {
  433. $image = yz_tomedia($image);
  434. }
  435. // if ($arrComment['append']) {
  436. // foreach ($arrComment['append'] as &$comment) {
  437. // $comment['images'] = unserialize($comment['images']);
  438. // foreach ($comment['images'] as &$image) {
  439. // $image = yz_tomedia($image);
  440. // }
  441. // }
  442. // }
  443. if ($arrComment['append']) {
  444. $arrComment['append']['images'] = unserialize($arrComment['append']['images']);
  445. foreach ($arrComment['append']['images'] as &$image) {
  446. $image = yz_tomedia($image);
  447. }
  448. }
  449. if ($arrComment['has_many_reply']) {
  450. foreach ($arrComment['has_many_reply'] as &$comment) {
  451. $comment['images'] = unserialize($comment['images']);
  452. foreach ($comment['images'] as &$image) {
  453. $image = yz_tomedia($image);
  454. }
  455. }
  456. }
  457. }
  458. public function goodsServiceFree(&$goodsModel)
  459. {
  460. if (app('plugins')->isEnabled('service-fee')) {
  461. $serviceFee = Setting::get('plugins.service-fee');
  462. if ($serviceFee['service']['open'] == 1) {
  463. $serviceFees = ServiceFeeModel::where('goods_id', $goodsModel->id)->first();
  464. if ($serviceFees->is_open) {
  465. $fee = ['name' => $serviceFee['service']['name'], 'money' => $serviceFees->fee];
  466. $goodsModel->fee = $fee;
  467. }
  468. }
  469. }
  470. return;
  471. }
  472. public function goods_lease_set(&$goodsModel, $lease_switch)
  473. {
  474. if ($lease_switch) {
  475. //TODO 商品租赁设置 $id
  476. if (is_array($goodsModel)) {
  477. $goodsModel['lease_toy'] = LeaseToyGoods::getDate($goodsModel['id']);
  478. } else {
  479. $goodsModel->lease_toy = LeaseToyGoods::getDate($goodsModel->id);
  480. }
  481. } else {
  482. if (is_array($goodsModel)) {
  483. $goodsModel['lease_toy'] = [
  484. 'is_lease' => $lease_switch,
  485. 'is_rights' => 0,
  486. 'immed_goods_id' => 0,
  487. ];
  488. } else {
  489. $goodsModel->lease_toy = [
  490. 'is_lease' => $lease_switch,
  491. 'is_rights' => 0,
  492. 'immed_goods_id' => 0,
  493. ];
  494. }
  495. }
  496. }
  497. /**
  498. * @param $goodsModel
  499. * @param $member
  500. * @throws \app\common\exceptions\AppException
  501. */
  502. public function validatePrivilege($goodsModel, $member)
  503. {
  504. Privilege::validatePrivilegeLevel($goodsModel, $member);
  505. Privilege::validatePrivilegeGroup($goodsModel, $member);
  506. }
  507. public function setGoodsPluginsRelations($goods)
  508. {
  509. $goodsRelations = app('GoodsManager')->tagged('GoodsRelations');
  510. collect($goodsRelations)->each(function ($goodsRelation) use ($goods) {
  511. $goodsRelation->setGoods($goods);
  512. });
  513. }
  514. public function pluginEnabled()
  515. {
  516. $data['package_deliver_enabled'] = app('plugins')->isEnabled('package-deliver') ? 1 : 0;
  517. $data['help_center_enabled'] = app('plugins')->isEnabled('help-center') ? 1 : 0;
  518. return show_json(1, $data);
  519. }
  520. public function getGoodsSaleV2($goodsModel, $member)
  521. {
  522. $sale = [];
  523. //商城积分设置
  524. $set = \Setting::get('point.set');
  525. //获取商城设置: 判断 积分、余额 是否有自定义名称
  526. $shopSet = \Setting::get('shop.shop');
  527. if ($goodsModel->hasOneSale->ed_num || $goodsModel->hasOneSale->ed_money) {
  528. $data['name'] = '包邮';
  529. $data['key'] = 'ed_num';
  530. $data['type'] = 'array';
  531. if ($goodsModel->hasOneSale->ed_num) {
  532. $data['value'][] = '本商品满' . $goodsModel->hasOneSale->ed_num . '件包邮';
  533. }
  534. if ($goodsModel->hasOneSale->ed_money) {
  535. $data['value'][] = '本商品满¥' . $goodsModel->hasOneSale->ed_money . '包邮';
  536. }
  537. array_push($sale, $data);
  538. $data = [];
  539. }
  540. if ($goodsModel->hasOneSale->all_point_deduct && $goodsModel->hasOneSale->has_all_point_deduct) {//商品设置
  541. $data['name'] = $shopSet['credit1'] ? $shopSet['credit1'] . '全额抵扣' : '积分全额抵扣';
  542. $data['key'] = 'all_point_deduct';
  543. $data['type'] = 'string';
  544. $data['value'] = '可使用' . $goodsModel->hasOneSale->all_point_deduct . '个' . ($shopSet['credit1'] ? $shopSet['credit1'] . '全额抵扣购买' : '积分全额抵扣购买');
  545. array_push($sale, $data);
  546. $data = [];
  547. }
  548. if ((bccomp($goodsModel->hasOneSale->ed_full, 0.00, 2) == 1) && (bccomp($goodsModel->hasOneSale->ed_reduction, 0.00, 2) == 1)) {
  549. $data['name'] = '满减';
  550. $data['key'] = 'ed_full';
  551. $data['type'] = 'string';
  552. $data['value'] = '本商品满¥' . $goodsModel->hasOneSale->ed_full . '立减¥' . $goodsModel->hasOneSale->ed_reduction;
  553. array_push($sale, $data);
  554. $data = [];
  555. }
  556. if ($goodsModel->hasOneSale->award_balance) {
  557. $data['name'] = $shopSet['credit'] ?: '余额';
  558. $data['key'] = 'award_balance';
  559. $data['type'] = 'string';
  560. $data['value'] = '购买赠送' . $goodsModel->hasOneSale->award_balance . $data['name'];
  561. array_push($sale, $data);
  562. $data = [];
  563. }
  564. $res = app('plugins')->isEnabled('store-cashier');
  565. if ($res) {//门店抵扣设置
  566. $store_goods = StoreGoods::where('goods_id', $goodsModel->id)->first();
  567. $point = StoreSetting::getStoreSettingByStoreId($store_goods->store_id)->where('key', 'point')->first();
  568. }
  569. $data['name'] = $shopSet['credit1'] ?: '积分';
  570. $data['key'] = 'point';
  571. $data['type'] = 'array';
  572. if ($set['give_point']) {
  573. $points = $set['give_point'] ? $set['give_point'] : 0;
  574. }
  575. if (!empty($point['value']['set']['give_point']) && $point['value']['set']['give_point'] != 0) {//门店抵扣设置
  576. $points = $point['value']['set']['give_point'];
  577. }
  578. if ($goodsModel->hasOneSale->point !== '0') {
  579. if ($goodsModel->hasOneSale->point) {
  580. $points = $goodsModel->hasOneSale->point;
  581. }
  582. if (!empty($points)) {
  583. $data['value'][] = '购买赠送' . $points . $data['name'];
  584. }
  585. }
  586. if ($set['point_deduct'] && $set['money_max']) {//&& $goodsModel->hasOneSale->max_point_deduct !== '0'
  587. $max_point_deduct = $set['money_max'] ? $set['money_max'] : 0;
  588. }
  589. if (!empty($point['value']['set']['money_max']) && $point['value']['set']['money_max'] != 0) {//门店抵扣设置
  590. $max_point_deduct = $point['value']['set']['money_max'];
  591. }
  592. if ($goodsModel->hasOneSale->max_point_deduct && $goodsModel->hasOneSale->max_point_deduct != 0) {
  593. $max_point_deduct = $goodsModel->hasOneSale->max_point_deduct;
  594. }
  595. if (!empty($max_point_deduct) && $max_point_deduct != 0) {
  596. $data['value'][] = '最高抵扣' . $max_point_deduct . '元';
  597. }
  598. if ($set['point_deduct'] && $goodsModel->hasOneSale->min_point_deduct != 0) {
  599. $min_point_deduct = $set['money_min'] ? $set['money_min'] . '%' : 0;
  600. if ($goodsModel->hasOneSale->min_point_deduct) {
  601. $min_point_deduct = $goodsModel->hasOneSale->min_point_deduct;
  602. }
  603. if (!empty($min_point_deduct) && $min_point_deduct != 0) {
  604. $data['value'][] = '最少抵扣' . $min_point_deduct . '元';
  605. }
  606. }
  607. if (!empty($data['value'])) {
  608. array_push($sale, $data);
  609. }
  610. $data = [];
  611. if ($goodsModel->hasOneGoodsCoupon->is_give) {
  612. $data['name'] = '购买返券';
  613. $data['key'] = 'coupon';
  614. $data['type'] = 'string';
  615. $data['value'] = $goodsModel->hasOneGoodsCoupon->send_type ? '商品订单完成返优惠券' : '每月一号返优惠券';
  616. array_push($sale, $data);
  617. $data = [];
  618. }
  619. //爱心值
  620. $exist_love = app('plugins')->isEnabled('love');
  621. if ($exist_love) {
  622. $love_goods = $this->getLoveSet($goodsModel, $goodsModel->id);
  623. $data['name'] = $love_goods['name'];
  624. $data['key'] = 'love';
  625. $data['type'] = 'array';
  626. if ($love_goods['deduction']) {
  627. $data['value'][] = '最高抵扣' . $love_goods['deduction_proportion'] . $data['name'];
  628. }
  629. if ($love_goods['award'] && \Setting::get('love.goods_detail_show_love') != 2) {
  630. $data['value'][] = '购买赠送' . $love_goods['award_proportion'] . $data['name'];
  631. }
  632. if (!empty($data['value'])) {
  633. array_push($sale, $data);
  634. }
  635. $data = [];
  636. }
  637. //佣金
  638. $exist_commission = app('plugins')->isEnabled('commission');
  639. if ($exist_commission) {
  640. $is_agent = $this->isValidateCommission($member);
  641. if ($is_agent) {
  642. $commission_data = (new GoodsDetailService($goodsModel))->getGoodsDetailData();
  643. if ($commission_data['commission_show'] == 1) {
  644. $data['name'] = '预计佣金';
  645. $data['key'] = 'commission';
  646. $data['type'] = 'array';
  647. if (!empty($commission_data['first_commission']) && ($commission_data['commission_show_level'] > 0)) {
  648. $data['value'][] = '一级佣金' . $commission_data['first_commission'] . '元';
  649. }
  650. if (!empty($commission_data['second_commission']) && ($commission_data['commission_show_level'] > 1)) {
  651. $data['value'][] = '二级佣金' . $commission_data['second_commission'] . '元';
  652. }
  653. if (!empty($commission_data['third_commission']) && ($commission_data['commission_show_level'] > 2)) {
  654. $data['value'][] = '三级佣金' . $commission_data['third_commission'] . '元';
  655. }
  656. array_push($sale, $data);
  657. $data = [];
  658. }
  659. }
  660. }
  661. //经销商提成
  662. $exist_team_dividend = app('plugins')->isEnabled('team-dividend');
  663. if ($exist_team_dividend) {
  664. //验证是否是经销商及等级
  665. $is_agent = $this->isValidateTeamDividend($member);
  666. if ($is_agent) {
  667. //返回经销商等级奖励比例 商品等级奖励规则
  668. $team_dividend_data = (new TeamDividendGoodsDetailService($goodsModel))->getGoodsDetailData();
  669. if ($team_dividend_data['team_dividend_show'] == 1) {
  670. $data['name'] = '经销商提成';
  671. $data['key'] = 'team-dividend';
  672. $data['type'] = 'array';
  673. $data['value'][] = '经销商提成' . $team_dividend_data['team_dividend_royalty'];
  674. array_unshift($sale, $data);
  675. $data = [];
  676. }
  677. }
  678. }
  679. $exist_pending_order = app('plugins')->isEnabled('pending-order');
  680. if ($exist_pending_order) {
  681. $pending_order_goods = \Yunshop\PendingOrder\services\PendingOrderGoodsService::getGoodsWholesaleSend($goodsModel->id);
  682. $pending_order['name'] = '批发劵';
  683. $pending_order['key'] = 'pending-order';
  684. $pending_order['type'] = 'array';
  685. if ($pending_order_goods['send_condition']['code']) {
  686. $pending_order['value'][] = $pending_order_goods['send_condition']['msg'];
  687. array_push($sale, $pending_order);
  688. }
  689. }
  690. return [
  691. 'sale_count' => count($sale),
  692. // 'first_strip_key' => $sale ? $sale[rand(0, (count($sale) - 1))] : [],
  693. 'first_strip_key' => $sale[0] ? $sale[0] : [],
  694. 'sale' => $sale,
  695. ];
  696. }
  697. public function isValidateCommission($member)
  698. {
  699. return Agents::getAgentByMemberId($member->member_id)->first();
  700. }
  701. public function isValidateTeamDividend($member)
  702. {
  703. return TeamDividendAgencyModel::getAgencyByMemberId($member->member_id)->first();
  704. }
  705. private function goodsProductMarket(&$goodsModel)
  706. {
  707. if (app('plugins')->isEnabled('product-market')) {
  708. $productMarket = ProductMarketGoodsModel::where('goods_id', $goodsModel->id)->first();
  709. if ($productMarket['product']) {
  710. $goodsModel->prdocut_market = $productMarket['product'];
  711. }
  712. }
  713. if (app('plugins')->isEnabled('market-sub')) {
  714. $productMarket = MarketSubGoods::where('goods_id', $goodsModel->id)->first();
  715. if ($productMarket['product']) {
  716. $goodsModel->prdocut_market = $productMarket['product'];
  717. }
  718. }
  719. return;
  720. }
  721. }