GoodsController.php 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  1. <?php
  2. namespace app\frontend\modules\goods\controllers;
  3. use app\backend\modules\goods\models\Brand;
  4. use app\common\components\ApiController;
  5. use app\common\exceptions\AppException;
  6. use app\common\facades\Setting;
  7. use app\common\models\Category;
  8. use app\common\models\goods\GoodsFiltering;
  9. use app\common\models\SearchFiltering;
  10. use app\framework\Http\Request;
  11. use app\frontend\modules\goods\models\Goods;
  12. use app\common\services\goods\SaleGoods;
  13. use app\common\services\goods\VideoDemandCourseGoods;
  14. use app\frontend\modules\goods\services\TradeGoodsPointsServer;
  15. use Illuminate\Support\Facades\DB;
  16. use Yunshop\Commission\Common\Services\GoodsDetailService;
  17. use Yunshop\Designer\models\ViewSet;
  18. use Yunshop\GoodsLink\services\GetGoodsDocService;
  19. use Yunshop\Room\models\Room;
  20. use Yunshop\StoreCashier\store\models\StoreGoods;
  21. use Yunshop\TeamDividend\Common\Services\TeamDividendGoodsDetailService;
  22. use Yunshop\Commission\models\Agents;
  23. use Yunshop\Love\Common\Models\GoodsLove;
  24. use app\frontend\modules\coupon\models\Coupon;
  25. use app\frontend\modules\coupon\controllers\MemberCouponController;
  26. use app\common\services\goods\LeaseToyGoods;
  27. use Yunshop\TeamDividend\models\TeamDividendAgencyModel;
  28. use app\common\models\MemberLevel;
  29. use Yunshop\StoreCashier\common\models\StoreSetting;
  30. use app\frontend\modules\goods\models\Comment;
  31. use Yunshop\TeamSales\common\services\GoodsListService;
  32. use Yunshop\Decorate\models\DecorateTempletModel;
  33. class GoodsController extends ApiController
  34. {
  35. protected $publicAction = ['getRecommendGoods'];
  36. protected $ignoreAction = ['getRecommendGoods'];
  37. public function __construct()
  38. {
  39. parent::__construct();
  40. $is_new_goods = 0;
  41. if (app('plugins')->isEnabled('decorate') && \Setting::get('plugin.decorate.is_open') == "1") {
  42. //商品模版
  43. if (!empty(\YunShop::request()->pc) && app('plugins')->isEnabled('pc-terminal')) {
  44. $pc_status = \Yunshop\PcTerminal\service\SetService::getPcStatus(); //PC端开启状态
  45. }
  46. if (!empty($pc_status)) {
  47. $view_set = DecorateTempletModel::getList(['is_default' => 1, 'type' => 6], '*', false);
  48. if ($view_set && $view_set->code == 'PCGoods02') {
  49. $is_new_goods = 1;
  50. }
  51. } else {
  52. $view_set = DecorateTempletModel::getList(['is_default' => 1, 'type' => 4], '*', false);
  53. if ($view_set && $view_set->code == 'goods02') {
  54. $is_new_goods = 1;
  55. }
  56. }
  57. }
  58. if ($is_new_goods == 1) {
  59. $this->publicAction[] = request()->route()->getActionMethod();
  60. $this->ignoreAction[] = request()->route()->getActionMethod();
  61. }
  62. }
  63. public function getGoodsPage()
  64. {
  65. $goods_id = request()->id;
  66. //查出商品模型
  67. $ims = DB::getTablePrefix();
  68. $goods_model = app('GoodsDetail')->make('Goods')->find($goods_id);
  69. if (is_null($goods_model)) {
  70. return $this->errorJson('商品不存在');
  71. }
  72. //设置商品详情主类
  73. app('GoodsDetail')->setDetailInstance($goods_model);
  74. //获取商品详情主类
  75. $detail_service = app('GoodsDetail')->make('GoodsDetailInstance');
  76. //初始化数据
  77. $detail_service->init($goods_model);
  78. //获取商品数据
  79. $detail_service->getData();
  80. return $this->successJson('成功', $detail_service->detail_data);
  81. }
  82. /**
  83. * todo 此方法需要优化,把插件的内容写到插件里面,通过配置文件读取
  84. * todo 插件关闭前端商城就不应该显示关闭插件的商品
  85. * @param $request
  86. * @param null $integrated
  87. * @return array|\Illuminate\Http\JsonResponse
  88. */
  89. public function getGoodsType(Request $request, $integrated = null)
  90. {
  91. app('db')->cacheSelect = true;
  92. $goods_type = 'goods';//通用
  93. $id = request()->id;
  94. if (!$id) {
  95. if (is_null($integrated)) {
  96. return $this->errorJson('请传入正确参数.');
  97. } else {
  98. return show_json(0, '请传入正确参数.');
  99. }
  100. }
  101. $goodsModel = Goods::uniacid()->find($id);
  102. $data['title'] = $goodsModel->title;
  103. // 商品详情挂件
  104. if (\app\common\modules\shop\ShopConfig::current()->get('goods_detail')) {
  105. foreach (\app\common\modules\shop\ShopConfig::current()->get('goods_detail') as $key_name => $row) {
  106. $row_res = $row['class']::{$row['function']}($id, true);
  107. if ($row_res) {
  108. $goodsModel->$key_name = $row_res;
  109. }
  110. }
  111. }
  112. //判断该商品是否是视频插件商品
  113. $isCourse = (new VideoDemandCourseGoods())->isCourse($id);
  114. if ($isCourse) {
  115. $goods_type = 'course';
  116. }
  117. //判断是否酒店商品
  118. if ($goodsModel->plugin_id == 33) {
  119. $goods_type = 'hotelGoods';
  120. }
  121. if ($goodsModel->plugin_id == 66) {
  122. $goods_type = 'voiceGoods';
  123. }
  124. //门店商品
  125. if ($goodsModel->plugin_id == 32 && $goodsModel->store_goods) {
  126. $goods_type = 'store_goods';
  127. $store_id = $goodsModel->store_goods->store_id;
  128. $data['store_id'] = $store_id;
  129. if (!is_null(\app\common\modules\shop\ShopConfig::current()->get('customer_development_judge'))) {
  130. $class = array_get(\app\common\modules\shop\ShopConfig::current()->get('customer_development_judge'), 'class');
  131. $function = array_get(\app\common\modules\shop\ShopConfig::current()->get('customer_development_judge'), 'function');
  132. $judge_res = $class::$function($goodsModel->id);
  133. if ($judge_res == 1) {
  134. $data['customer_development'] = 1;
  135. }
  136. }
  137. }
  138. //供应商商品
  139. if ($goodsModel->plugin_id == 92 && $goodsModel->supplier) {
  140. $goods_type = 'supplierGoods';
  141. }
  142. //分期购车插件
  143. if ($goodsModel->plugin_id == 47) {
  144. $goods_type = 'staging_buy_car_goods';
  145. }
  146. //预约插件
  147. if ($goodsModel->plugin_id == 101) {
  148. $goods_type = 'appointment_goods';
  149. }
  150. //芸签电子合同插件
  151. if ($goodsModel->plugin_id == 103) {
  152. $goods_type = 'yun_sign_goods';
  153. }
  154. $data['goods_type'] = $goods_type;
  155. if (is_null($integrated)) {
  156. return $this->successJson('成功', $data);
  157. } else {
  158. return show_json(1, $data);
  159. }
  160. }
  161. /**
  162. * @param $goodsModel
  163. * @param $member
  164. * @throws \app\common\exceptions\AppException
  165. */
  166. private function setGoodsPluginsRelations($goods)
  167. {
  168. $goodsRelations = app('GoodsManager')->tagged('GoodsRelations');
  169. collect($goodsRelations)->each(function ($goodsRelation) use ($goods) {
  170. $goodsRelation->setGoods($goods);
  171. });
  172. }
  173. protected function getGoodsModel()
  174. {
  175. $goods_model = \app\common\modules\shop\ShopConfig::current()->get('goods.models.commodity_classification');
  176. return new $goods_model;
  177. }
  178. public function searchGoods()
  179. {
  180. app('db')->cacheSelect = false;
  181. $requestSearch = \YunShop::request()->search;
  182. $plugin_id = \YunShop::request()->plugin_id;//搜索特定插件商品
  183. $order_field = \YunShop::request()->order_field;
  184. $goods_model = $this->getGoodsModel();
  185. // $goods_model = \app\common\modules\shop\ShopConfig::current()->get('goods.models.commodity_classification');
  186. // $goods_model = new $goods_model;
  187. if (!in_array($order_field, ['price', 'show_sales', 'comment_num', 'min_price', 'max_price'])) {
  188. $order_field = 'display_order';
  189. } else {
  190. if ($order_field == 'show_sales') {
  191. //排序改为虚拟销量
  192. $order_field = 'total_sales';
  193. }
  194. }
  195. $order_by = (\YunShop::request()->order_by == 'asc') ? 'asc' : 'desc';
  196. if ($requestSearch) {
  197. $requestSearch = array_filter($requestSearch, function ($item) {
  198. return !empty($item) && $item !== 0 && $item !== "undefined";
  199. });
  200. $categorySearch = array_filter(\YunShop::request()->category, function ($item) {
  201. return !empty($item);
  202. });
  203. if ($categorySearch) {
  204. $requestSearch['category'] = $categorySearch;
  205. }
  206. }
  207. //增加默认搜索不隐藏的商品
  208. $requestSearch['is_hide'] = 1;
  209. $ims = DB::getTablePrefix();
  210. $goods_select = $ims . 'yz_goods.has_option,' . $ims . "yz_goods.id,thumb,plugin_id,real_sales+virtual_sales total_sales,market_price,price,min_price,max_price,cost_price,title," . $ims . "yz_goods.stock, " . $ims . "yz_goods.id as goods_id";
  211. $option_select = 'goods_id,product_price,market_price,stock,cost_price';
  212. $build = $goods_model->SearchList($requestSearch)
  213. ->selectRaw($goods_select)
  214. ->with(['hasManyOptions' => function ($query) use ($option_select) {
  215. $query->selectRaw($option_select);
  216. }])
  217. ->where("yz_goods.status", 1);
  218. if ($plugin_id) {
  219. $build->where('plugin_id', $plugin_id);
  220. } else {
  221. $build->whereInPluginIds();
  222. }
  223. $list = $build->orderBy($order_field, $order_by)
  224. ->orderBy('yz_goods.id', 'desc')
  225. ->paginate(20);
  226. if ($list->isEmpty()) {
  227. return $this->errorJson('没有找到商品.');
  228. }
  229. //TODO 租赁插件是否开启 $lease_switch
  230. $lease_switch = LeaseToyGoods::whetherEnabled();
  231. //由于之前 foreach 太多了 合在一起
  232. $list->map(function ($item) use ($lease_switch) {
  233. $item->thumb = yz_tomedia($item->thumb);
  234. //租赁商品
  235. $this->goods_lease_set($item, $lease_switch);
  236. // 商品标签
  237. $this->setGoodsLabel($item);
  238. $item->id = $item->goods_id;
  239. if (Setting::get('goods.profit_show_status')) {
  240. if ($item->has_option) {
  241. if ($item->hasManyOptions && $item->hasManyOptions->isNotEmpty()) {
  242. $item->hasManyOptions = $item->hasManyOptions->each(function ($option) {
  243. $option->goods_profit = bcsub($option->product_price, $option->cost_price, 2);
  244. if (bccomp($option->goods_profit, 0, 2) == -1) {
  245. $option->goods_profit = 0;
  246. }
  247. });
  248. $item->min_goods_profit = $item->hasManyOptions->min('goods_profit') ?: 0;
  249. } else {
  250. $item->min_goods_profit = 0;
  251. }
  252. } else {
  253. $item->goods_profit = bcsub($item->price, $item->cost_price, 2);
  254. if (bccomp($item->goods_profit, 0, 2) == -1) {
  255. $item->goods_profit = 0;
  256. }
  257. $item->min_goods_profit = $item->goods_profit;
  258. }
  259. }
  260. // if ($item->has_option && $item->hasManyOptions->isNotEmpty()) {
  261. // $item->min_price = $item->hasManyOptions->min("product_price");
  262. // $item->stock = $item->hasManyOptions->sum('stock');
  263. // }
  264. //会员vip价格
  265. $item->vip_price = $item->vip_price;
  266. $item->vip_next_price = $item->vip_next_price;
  267. $item->vip_level_status = $item->vip_level_status;
  268. $item->price_level = $item->price_level;
  269. $item->is_open_micro = $item->is_open_micro;
  270. $item->goods_points = $this->setGoodPoints($item);
  271. return $item;
  272. });
  273. $list = $list->toArray();
  274. foreach ($list['data'] as &$v){
  275. if ($v['has_option'] && $v['has_many_options']){
  276. $v['stock'] = array_sum(array_column($v['has_many_options'],'stock'));
  277. }
  278. }
  279. unset($v);
  280. $category_data = [
  281. 'names' => '01',
  282. 'type' => 'goodsList',
  283. ];
  284. if (app('plugins')->isEnabled('decorate') && \Setting::get('plugin.decorate.is_open') == 1) {
  285. // $view_set = \Yunshop\Decorate\models\DecorateTempletModel::uniacid()
  286. // ->where('code','goodsList02')
  287. // ->where('type',5)
  288. // ->where('is_default',1)
  289. // ->first();
  290. // $category_data = $view_set ? ['names' => '02', 'type' => 'goodsList']: $category_data;
  291. $view_set = \Yunshop\Decorate\models\DecorateTempletModel::uniacid()
  292. ->whereIn('code', ['goodsList01', 'goodsList02', 'goodsList03'])
  293. ->where('type', 5)
  294. ->where('is_default', 1)
  295. ->first();
  296. $tmp = [
  297. 'goodsList01' => '01',
  298. 'goodsList02' => '02',
  299. 'goodsList03' => '03',
  300. ];
  301. $category_data = $view_set ? ['names' => $tmp[$view_set->code], 'type' => 'goodsList'] : $category_data;
  302. if ($view_set) {
  303. $list['data'] = $this->getGoodsCouponSale($list['data']);
  304. //团队销售佣金一级分销
  305. if (app('plugins')->isEnabled('team-sales')) {
  306. $list['data'] = GoodsListService::getFirstDividend($list['data']);
  307. }
  308. }
  309. if ($category_data['names'] == '03') {
  310. if (app('plugins')->isEnabled('love')) {//爱心值天天兑价
  311. $list['data'] = \Yunshop\Love\Frontend\Models\GoodsLove::setGoodsLove($list['data']);
  312. }
  313. }
  314. } elseif (app('plugins')->isEnabled('designer')) {
  315. //商品分类模板
  316. // $view_set = ViewSet::uniacid()->where('type', 'goodsList')->first();
  317. $view_set = ViewSet::uniacid()->where('type', 'goodsList')->select('names', 'type')->first();
  318. $category_data = $view_set ?: $category_data;
  319. if (!empty($view_set) && $view_set->names == '02') {
  320. $list['data'] = $this->getGoodsCouponSale($list['data']);
  321. //团队销售佣金一级分销
  322. if (app('plugins')->isEnabled('team-sales')) {
  323. $list['data'] = GoodsListService::getFirstDividend($list['data']);
  324. }
  325. }
  326. }
  327. //增加商品链接
  328. if (app('plugins')->isEnabled('goods-link')) {
  329. $list['data'] = GetGoodsDocService::getDoc($list['data']);
  330. }
  331. //通证价计算
  332. if (app('plugins')->isEnabled('pass-price') && \Setting::get('pass-price.set.plugin_enable')) {
  333. $list['data'] = \Yunshop\PassPrice\services\PassPriceService::getPass($list['data']);
  334. }
  335. //积分商城
  336. if (app('plugins')->isEnabled('point-mall')) {
  337. $list['data'] = \Yunshop\PointMall\api\models\PointMallGoodsModel::setPointGoods($list['data']);
  338. }
  339. $list['goods_template'] = $category_data;
  340. $goods_style_set = Setting::get('plugin.good_style');
  341. if ($goods_style_set) {
  342. $goods_style_set['current_thumb'] = yz_tomedia($goods_style_set['current_thumb']);
  343. $goods_style_set['member_thumb'] = yz_tomedia($goods_style_set['member_thumb']);
  344. }
  345. $list['goods_style_set'] = $goods_style_set;
  346. return $this->successJson('成功', $list);
  347. }
  348. private function getGoodsCouponSale($goods)
  349. {
  350. //$memberLevel = MemberShopInfo::where('member_id', \YunShop::app()->getMemberId())->with(['level'])->first(); 去除优惠券等级限制
  351. $coupon = \app\common\models\Coupon::uniacid()->where('use_type', 2)->where('get_type', 1)->get();
  352. foreach ($goods as $k => &$v) {
  353. $v['coupon']['coupon_method'] = 0; //商品没有折扣
  354. foreach ($coupon as $key => $value) {
  355. if ($value->time_limit == 1) {
  356. if (time() < $value->time_statt || time() > $value->time_end) continue;
  357. }
  358. if (in_array($v['goods_id'], $value['goods_ids']) || $v['goods_id'] == $value['goods_ids']) {
  359. if ($value['coupon_method'] == 1) {
  360. $v['coupon']['deduct_price'] = bcsub($v['price'], $value['deduct'], 2); //立减折扣
  361. $v['coupon']['coupon_method'] = $value['coupon_method'];
  362. $v['coupon']['deduct'] = $value['deduct'];
  363. $v['coupon']['discount'] = $value['discount'];
  364. } else if ($value['coupon_method'] == 2) {
  365. $v['coupon']['deduct_price'] = bcmul($v['price'], $value['discount'] / 10, 2); //打折优惠
  366. $v['coupon']['coupon_method'] = $value['coupon_method'];
  367. $v['coupon']['discount'] = $value['discount'];
  368. $v['coupon']['deduct'] = $value['deduct'];
  369. }
  370. if ($v['coupon']['deduct_price'] < 0) {
  371. $v['coupon']['deduct_price'] = 0;
  372. }
  373. if ($v['coupon']['enough'] > 0 && $v['price'] < $value['enough']) {
  374. $v['coupon']['deduct_price'] = 0;
  375. }
  376. }
  377. }
  378. }
  379. return $goods;
  380. }
  381. public function getGoodsCategoryList()
  382. {
  383. $category_id = intval(\YunShop::request()->category_id);
  384. if (empty($category_id)) {
  385. return $this->errorJson('请输入正确的商品分类.');
  386. }
  387. $order_field = \YunShop::request()->order_field;
  388. if (!in_array($order_field, ['price', 'show_sales', 'comment_num'])) {
  389. $order_field = 'display_order';
  390. }
  391. $order_by = (\YunShop::request()->order_by == 'asc') ? 'asc' : 'desc';
  392. $categorys = Category::uniacid()->select("name", "thumb", "id")->where(['id' => $category_id])->first();
  393. if ($categorys) {
  394. $categorys->thumb = yz_tomedia($categorys->thumb);
  395. }
  396. $goodsList = Goods::uniacid()->select('yz_goods.id', 'yz_goods.id as goods_id', 'title', 'thumb', 'price', 'market_price')
  397. ->join('yz_goods_category', 'yz_goods_category.goods_id', '=', 'yz_goods.id')
  398. ->where("category_id", $category_id)
  399. ->where('status', '1')
  400. ->orderBy($order_field, $order_by)
  401. ->paginate(20)->toArray();
  402. if (empty($goodsList)) {
  403. return $this->errorJson('此分类下没有商品.');
  404. }
  405. $goodsList['data'] = set_medias($goodsList['data'], 'thumb');
  406. $categorys->goods = $goodsList;
  407. return $this->successJson('成功', $categorys);
  408. }
  409. public function getGoodsBrandList()
  410. {
  411. $goods_model = \app\common\modules\shop\ShopConfig::current()->get('goods.models.commodity_classification');
  412. $goods_model = new $goods_model;
  413. $brand_id = intval(\YunShop::request()->brand_id);
  414. $order_field = \YunShop::request()->order_field;
  415. $search['keyword'] = \YunShop::request()->keywords;
  416. if (!in_array($order_field, ['price', 'show_sales', 'comment_num'])) {
  417. $order_field = 'display_order';
  418. }
  419. $order_by = (\YunShop::request()->order_by == 'asc') ? 'asc' : 'desc';
  420. if (empty($brand_id)) {
  421. return $this->errorJson('请输入正确的品牌id.');
  422. }
  423. $brand = Brand::uniacid()->select("name", "logo", "id")->where(['id' => $brand_id])->first();
  424. if (!$brand) {
  425. return $this->errorJson('没有此品牌.');
  426. }
  427. $brand->logo = yz_tomedia($brand->logo);
  428. $goodsList = $goods_model->uniacid()->select('id', 'id as goods_id', 'title', 'thumb', 'price', 'market_price')
  429. ->where('status', '1')
  430. ->where('brand_id', $brand_id)
  431. ->search($search)
  432. ->whereInPluginIds()
  433. ->orderBy($order_field, $order_by)
  434. ->paginate(20)->toArray();
  435. $goodsList->vip_level_status;
  436. if (empty($goodsList)) {
  437. return $this->errorJson('此品牌下没有商品.');
  438. }
  439. $goodsList['data'] = set_medias($goodsList['data'], 'thumb');
  440. $category_data = [
  441. 'names' => '01',
  442. 'type' => 'goodsList',
  443. ];
  444. if (app('plugins')->isEnabled('decorate') && \Setting::get('plugin.decorate.is_open') == 1) {
  445. // $view_set = \Yunshop\Decorate\models\DecorateTempletModel::uniacid()
  446. // ->where('code', 'goodsList02')
  447. // ->where('type', 5)
  448. // ->where('is_default', 1)
  449. // ->first();
  450. // $category_data = $view_set ? ['names' => '02', 'type' => 'goodsList'] : $category_data;
  451. //
  452. $view_set = \Yunshop\Decorate\models\DecorateTempletModel::uniacid()
  453. ->whereIn('code', ['goodsList01', 'goodsList02', 'goodsList03'])
  454. ->where('type', 5)
  455. ->where('is_default', 1)
  456. ->first();
  457. $tmp = [
  458. 'goodsList01' => '01',
  459. 'goodsList02' => '02',
  460. 'goodsList03' => '03',
  461. ];
  462. $category_data = $view_set ? ['names' => $tmp[$view_set->code], 'type' => 'goodsList'] : $category_data;
  463. if ($view_set) {
  464. $goodsList['data'] = $this->getGoodsCouponSale($goodsList['data']);
  465. //团队销售佣金一级分销
  466. if (app('plugins')->isEnabled('team-sales')) {
  467. $goodsList['data'] = GoodsListService::getFirstDividend($goodsList['data']);
  468. }
  469. }
  470. if ($category_data['names'] == '03') {
  471. if (app('plugins')->isEnabled('love')) {//爱心值天天兑价
  472. $goodsList['data'] = \Yunshop\Love\Frontend\Models\GoodsLove::setGoodsLove($goodsList['data']);
  473. }
  474. }
  475. } elseif (app('plugins')->isEnabled('designer')) {
  476. //商品分类模板
  477. // $view_set = ViewSet::uniacid()->where('type', 'goodsList')->first();
  478. $view_set = ViewSet::uniacid()->where('type', 'goodsList')->select('names', 'type')->first();
  479. $category_data = $view_set ?: $category_data;
  480. if (!empty($view_set) && $view_set->names == '02') {
  481. $goodsList['data'] = $this->getGoodsCouponSale($goodsList['data']);
  482. //团队销售佣金一级分销
  483. if (app('plugins')->isEnabled('team-sales')) {
  484. $goodsList['data'] = GoodsListService::getFirstDividend($goodsList['data']);
  485. }
  486. }
  487. }
  488. //增加商品链接
  489. if (app('plugins')->isEnabled('goods-link')) {
  490. $goodsList['data'] = GetGoodsDocService::getDoc($goodsList['data']);
  491. }
  492. //积分商城
  493. if (app('plugins')->isEnabled('point-mall')) {
  494. $goodsList['data'] = \Yunshop\PointMall\api\models\PointMallGoodsModel::setPointGoods($goodsList['data']);
  495. }
  496. $goodsList['goods_template'] = $category_data;
  497. $goods_style_set = Setting::get('plugin.good_style');
  498. if ($goods_style_set) {
  499. $goods_style_set['current_thumb'] = yz_tomedia($goods_style_set['current_thumb']);
  500. $goods_style_set['member_thumb'] = yz_tomedia($goods_style_set['member_thumb']);
  501. }
  502. $brand->goods_style_set = $goods_style_set;
  503. $brand->goods = $goodsList;
  504. return $this->successJson('成功', $brand);
  505. }
  506. public function getRecommendGoods()
  507. {
  508. $list = Goods::uniacid()
  509. ->select('id', 'id as goods_id', 'title', 'thumb', 'price', 'market_price')
  510. ->where('is_recommand', '1')
  511. ->whereStatus('1')
  512. ->orderBy('id', 'desc')
  513. ->get();
  514. if (!$list->isEmpty()) {
  515. $list = set_medias($list->toArray(), 'thumb');
  516. }
  517. return $this->successJson('获取推荐商品成功', $list);
  518. }
  519. /**
  520. * 会员折扣后的价格
  521. * @param Goods $goodsModel
  522. * @param [type] $discountModel [description]
  523. * @return array [type] [description]
  524. */
  525. public function getDiscount($goodsModel, $memberModel)
  526. {
  527. if ($goodsModel->vip_price === null) {
  528. return [];
  529. }
  530. $discount_switch = Setting::get('shop.member.discount');
  531. $level_type = Setting::get('shop.member.level_type');
  532. $display_page = Setting::get('shop.member.display_page');
  533. if ($memberModel->level) {
  534. $data = [
  535. 'level_name' => $memberModel->level->level_name,
  536. 'discount_value' => $goodsModel->vip_price,
  537. 'discount' => $discount_switch,
  538. 'next_level_price' => $goodsModel->next_level_price,
  539. 'next_level_name' => $goodsModel->next_level_name,
  540. 'level_type' => $level_type,
  541. 'display_page' => $display_page
  542. ];
  543. } else {
  544. $level = Setting::get('shop.member.level_name');
  545. $level_name = $level ?: '普通会员';
  546. $data = [
  547. 'level_name' => $level_name,
  548. 'discount_value' => $goodsModel->vip_price,
  549. 'discount' => $discount_switch,
  550. 'next_level_price' => $goodsModel->next_level_price,
  551. 'next_level_name' => $goodsModel->next_level_name ?: MemberLevel::value('level_name'),
  552. 'level_type' => $level_type,
  553. 'display_page' => $display_page
  554. ];
  555. }
  556. return $data;
  557. }
  558. public function getGoodsSaleV2($goodsModel, $member)
  559. {
  560. $sale = [];
  561. //商城积分设置
  562. $set = \Setting::get('point.set');
  563. //获取商城设置: 判断 积分、余额 是否有自定义名称
  564. $shopSet = \Setting::get('shop.shop');
  565. if ($goodsModel->hasOneSale->ed_num || $goodsModel->hasOneSale->ed_money) {
  566. $data['name'] = '包邮';
  567. $data['key'] = 'ed_num';
  568. $data['type'] = 'array';
  569. if ($goodsModel->hasOneSale->ed_num) {
  570. $data['value'][] = '本商品满' . $goodsModel->hasOneSale->ed_num . '件包邮';
  571. }
  572. if ($goodsModel->hasOneSale->ed_money) {
  573. $data['value'][] = '本商品满¥' . $goodsModel->hasOneSale->ed_money . '包邮';
  574. }
  575. array_push($sale, $data);
  576. $data = [];
  577. }
  578. if ($goodsModel->hasOneSale->all_point_deduct && $goodsModel->hasOneSale->has_all_point_deduct) {//商品设置
  579. $data['name'] = $shopSet['credit1'] ? $shopSet['credit1'] . '全额抵扣' : '积分全额抵扣';
  580. $data['key'] = 'all_point_deduct';
  581. $data['type'] = 'string';
  582. $data['value'] = '可使用' . $goodsModel->hasOneSale->all_point_deduct . '个' . ($shopSet['credit1'] ? $shopSet['credit1'] . '全额抵扣购买' : '积分全额抵扣购买');
  583. array_push($sale, $data);
  584. $data = [];
  585. }
  586. if ((bccomp($goodsModel->hasOneSale->ed_full, 0.00, 2) == 1) && (bccomp($goodsModel->hasOneSale->ed_reduction, 0.00, 2) == 1)) {
  587. $data['name'] = '满减';
  588. $data['key'] = 'ed_full';
  589. $data['type'] = 'string';
  590. $data['value'] = '本商品满¥' . $goodsModel->hasOneSale->ed_full . '立减¥' . $goodsModel->hasOneSale->ed_reduction;
  591. array_push($sale, $data);
  592. $data = [];
  593. }
  594. if ($goodsModel->hasOneSale->award_balance) {
  595. $data['name'] = $shopSet['credit'] ?: '余额';
  596. $data['key'] = 'award_balance';
  597. $data['type'] = 'string';
  598. $data['value'] = '购买赠送' . $goodsModel->hasOneSale->award_balance . $data['name'];
  599. array_push($sale, $data);
  600. $data = [];
  601. }
  602. $point = [];
  603. if (app('plugins')->isEnabled('store-cashier')) {//门店抵扣设置
  604. $store_goods = StoreGoods::where('goods_id', $goodsModel->id)->first();
  605. $point = StoreSetting::getStoreSettingByStoreId($store_goods->store_id)->where('key', 'point')->first();
  606. }
  607. $data['name'] = $shopSet['credit1'] ?: '积分';
  608. $data['key'] = 'point';
  609. $data['type'] = 'array';
  610. if ((strlen($goodsModel->hasOneSale->point) === 0) || $goodsModel->hasOneSale->point != 0) {
  611. if ($goodsModel->hasOneSale->point) {
  612. $points = $goodsModel->hasOneSale->point;
  613. } elseif (!empty($point['value']['set']['give_point']) && $point['value']['set']['give_point'] != 0) {//门店抵扣设置
  614. $points = $point['value']['set']['give_point'] . '%';
  615. } else {
  616. $points = $set['give_point'] ? $set['give_point'] : 0;
  617. }
  618. if (!empty($points)) {
  619. $data['value'][] = '购买赠送' . $points . $data['name'];
  620. }
  621. }
  622. //设置不等于0,支持积分抵扣
  623. //积分抵扣优先级 商品独立设置 ---> 门店设置 ---> 积分统一设置
  624. if ($set['point_deduct'] && (strlen($goodsModel->hasOneSale->max_point_deduct) === 0 || $goodsModel->hasOneSale->max_point_deduct != 0)) {
  625. if ($goodsModel->hasOneSale->max_point_deduct) {
  626. $max_point_deduct = $goodsModel->hasOneSale->max_point_deduct . '元';
  627. } elseif (strlen($point['value']['set']['money_max']) !== 0) {
  628. if (!($point['value']['set']['money_max'] === 0 || $point['value']['set']['money_max'] === '0')) {
  629. $max_point_deduct = $point['value']['set']['money_max'] . '%';
  630. }
  631. } else {
  632. $max_point_deduct = $set['money_max'] ? $set['money_max'] . '%' : 0;
  633. }
  634. if (!empty(mb_substr($max_point_deduct, 0, -1))) {
  635. $data['value'][] = '最高抵扣' . $max_point_deduct;
  636. }
  637. }
  638. if ($set['point_deduct'] && (strlen($goodsModel->hasOneSale->min_point_deduct) === 0 || $goodsModel->hasOneSale->min_point_deduct != 0)) {
  639. if ($goodsModel->hasOneSale->min_point_deduct) {
  640. $min_point_deduct = $goodsModel->hasOneSale->min_point_deduct . '元';
  641. } else {
  642. $min_point_deduct = $set['money_min'] ? $set['money_min'] . '%' : 0;
  643. }
  644. if (!empty(mb_substr($min_point_deduct, 0, -1))) {
  645. $data['value'][] = '最少抵扣' . $min_point_deduct;
  646. }
  647. }
  648. if (!empty($data['value'])) {
  649. array_push($sale, $data);
  650. }
  651. $data = [];
  652. if ($goodsModel->hasOneGoodsCoupon->is_give) {
  653. $data['name'] = '购买返券';
  654. $data['key'] = 'coupon';
  655. $data['type'] = 'string';
  656. $data['value'] = $goodsModel->hasOneGoodsCoupon->send_type ? '商品订单完成返优惠券' : '每月一号返优惠券';
  657. array_push($sale, $data);
  658. $data = [];
  659. }
  660. //爱心值
  661. $exist_love = app('plugins')->isEnabled('love');
  662. if ($exist_love) {
  663. $love_goods = $this->getLoveSet($goodsModel, $goodsModel->id);
  664. $data['name'] = $love_goods['name'];
  665. $data['key'] = 'love';
  666. $data['type'] = 'array';
  667. if ($love_goods['deduction']) {
  668. $data['value'][] = '最高抵扣' . $love_goods['deduction_proportion'] . $data['name'];
  669. }
  670. if ($love_goods['award'] && \Setting::get('love.goods_detail_show_love') != 2) {
  671. $data['value'][] = '购买赠送' . $love_goods['award_proportion'] . $data['name'];
  672. }
  673. if (!empty($data['value'])) {
  674. array_push($sale, $data);
  675. }
  676. $data = [];
  677. }
  678. //佣金
  679. $exist_commission = app('plugins')->isEnabled('commission');
  680. if ($exist_commission) {
  681. $is_agent = $this->isValidateCommission($member);
  682. if ($is_agent) {
  683. $commission_data = (new GoodsDetailService($goodsModel))->getGoodsDetailData();
  684. if ($commission_data['commission_show'] == 1) {
  685. // $data['name'] = '佣金';
  686. $data['key'] = 'commission';
  687. $data['type'] = 'array';
  688. $lang = isset($lang) ? $lang : \Setting::get('shop.lang');
  689. $commission_alisa = array_get($lang, 'zh_cn.commission.commission_alias', '佣金') ?: '佣金';
  690. $data['name'] = $commission_alisa;
  691. $data['desc'] = '最终获得' . $commission_alisa . '根据等级、独立' . $commission_alisa . '设置、下单实际支付金额等因数决定,请以最终到账' . $commission_alisa . '为准!';
  692. if (!empty($commission_data['first_commission']) && ($commission_data['commission_show_level'] > 0)) {
  693. // $data['value'][] = '一级佣金' . $commission_data['first_commission'] . '元';
  694. $data['value'][] = '一级' . $commission_alisa . $commission_data['first_commission'] . '元';
  695. }
  696. if (!empty($commission_data['second_commission']) && ($commission_data['commission_show_level'] > 1)) {
  697. // $data['value'][] = '二级佣金' . $commission_data['second_commission'] . '元';
  698. $data['value'][] = '二级' . $commission_alisa . $commission_data['second_commission'] . '元';
  699. }
  700. if (!empty($commission_data['third_commission']) && ($commission_data['commission_show_level'] > 2)) {
  701. // $data['value'][] = '三级佣金' . $commission_data['third_commission'] . '元';
  702. $data['value'][] = '三级' . $commission_alisa . $commission_data['third_commission'] . '元';
  703. }
  704. array_push($sale, $data);
  705. $data = [];
  706. }
  707. }
  708. }
  709. //经销商提成
  710. $exist_team_dividend = app('plugins')->isEnabled('team-dividend');
  711. if ($exist_team_dividend) {
  712. //验证是否是经销商及等级
  713. $is_agent = $this->isValidateTeamDividend($member);
  714. if ($is_agent) {
  715. // $lang = \Setting::get('shop.lang');
  716. $lang = isset($lang) ? $lang : \Setting::get('shop.lang');
  717. $team_dividend_name = array_get($lang, 'zh_cn.team_dividend.title', '经销商') ?: '经销商';
  718. $dividend_alisa = array_get($lang, 'zh_cn.team_dividend.dividend_alias', $team_dividend_name . '提成') ?: $team_dividend_name . '提成';
  719. //返回经销商等级奖励比例 商品等级奖励规则
  720. $team_dividend_data = (new TeamDividendGoodsDetailService($goodsModel))->getGoodsDetailData();
  721. if ($team_dividend_data['team_dividend_show'] == 1) {
  722. // $data['name'] = $team_dividend_name.'提成';
  723. $data['name'] = $dividend_alisa;
  724. $data['key'] = 'team-dividend';
  725. $data['type'] = 'array';
  726. // $data['value'][] = $team_dividend_name.'提成' . $team_dividend_data['team_dividend_royalty'];
  727. $data['value'][] = $dividend_alisa . $team_dividend_data['team_dividend_royalty'];
  728. array_unshift($sale, $data);
  729. $data = [];
  730. }
  731. }
  732. }
  733. $exist_pending_order = app('plugins')->isEnabled('pending-order');
  734. if ($exist_pending_order) {
  735. $pending_order_goods = \Yunshop\PendingOrder\services\PendingOrderGoodsService::getGoodsWholesaleSend($goodsModel->id);
  736. $pending_order['name'] = '批发劵';
  737. $pending_order['key'] = 'pending-order';
  738. $pending_order['type'] = 'array';
  739. if ($pending_order_goods['send_condition']['code']) {
  740. $pending_order['value'][] = $pending_order_goods['send_condition']['msg'];
  741. array_push($sale, $pending_order);
  742. }
  743. }
  744. return [
  745. 'sale_count' => count($sale),
  746. // 'first_strip_key' => $sale ? $sale[rand(0, (count($sale) - 1))] : [],
  747. 'first_strip_key' => $sale[0] ? $sale[0] : [],
  748. 'sale' => $sale,
  749. ];
  750. }
  751. public function isValidateCommission($member)
  752. {
  753. return Agents::getAgentByMemberId($member->member_id)->first();
  754. }
  755. public function isValidateTeamDividend($member)
  756. {
  757. return TeamDividendAgencyModel::getAgencyByMemberId($member->member_id)->first();
  758. }
  759. /**
  760. * 商品的营销
  761. * @param [type] $goodsModel [description]
  762. * @return [type] [description]
  763. */
  764. public function getGoodsSale($goodsModel)
  765. {
  766. $set = \Setting::get('point.set');
  767. $shopSet = \Setting::get('shop.shop');
  768. if (!empty($shopSet['credit1'])) {
  769. $point_name = $shopSet['credit1'];
  770. } else {
  771. $point_name = '积分';
  772. }
  773. $data = [
  774. 'first_strip_key' => 0,
  775. 'point_name' => $point_name, //积分名称
  776. 'love_name' => '爱心值',
  777. 'ed_num' => 0, //满件包邮
  778. 'ed_money' => 0, //满额包邮
  779. 'ed_full' => 0, //单品满额
  780. 'ed_reduction' => 0, //单品立减
  781. 'award_balance' => 0, //赠送余额
  782. 'point' => 0, //赠送积分
  783. 'max_point_deduct' => 0, //积分最大抵扣
  784. 'min_point_deduct' => 0, //积分最小抵扣
  785. 'coupon' => 0, //商品优惠券赠送
  786. 'deduction_proportion' => 0, //爱心值最高抵扣
  787. 'award_proportion' => 0, //奖励爱心值
  788. 'sale_count' => 0, //活动总数
  789. ];
  790. if (ceil($goodsModel->hasOneSale->ed_full) && ceil($goodsModel->hasOneSale->ed_reduction)) {
  791. $data['ed_full'] = $goodsModel->hasOneSale->ed_full;
  792. $data['ed_reduction'] = $goodsModel->hasOneSale->ed_reduction;
  793. $data['first_strip_key'] = 'ed_full';
  794. $data['sale_count'] += 1;
  795. }
  796. if ($goodsModel->hasOneSale->award_balance) {
  797. $data['award_balance'] = $goodsModel->hasOneSale->award_balance;
  798. $data['first_strip_key'] = 'award_balance';
  799. $data['sale_count'] += 1;
  800. }
  801. if ($goodsModel->hasOneSale->point !== '0') {
  802. $data['point'] = $set['give_point'] ? $set['give_point'] : 0;
  803. if ($goodsModel->hasOneSale->point) {
  804. $data['point'] = $goodsModel->hasOneSale->point;
  805. }
  806. if (!empty($data['point'])) {
  807. $data['first_strip_key'] = 'point';
  808. $data['sale_count'] += 1;
  809. }
  810. }
  811. if ($set['point_deduct'] && $goodsModel->hasOneSale->max_point_deduct !== '0') {
  812. $data['max_point_deduct'] = $set['money_max'] ? $set['money_max'] . '%' : 0;
  813. if ($goodsModel->hasOneSale->max_point_deduct) {
  814. $data['max_point_deduct'] = $goodsModel->hasOneSale->max_point_deduct;
  815. }
  816. if (!empty($data['max_point_deduct'])) {
  817. $data['first_strip_key'] = 'max_point_deduct';
  818. $data['sale_count'] += 1;
  819. }
  820. }
  821. if ($set['point_deduct'] && $goodsModel->hasOneSale->min_point_deduct !== '0') {
  822. $data['min_point_deduct'] = $set['money_min'] ? $set['money_min'] . '%' : 0;
  823. if ($goodsModel->hasOneSale->min_point_deduct) {
  824. $data['min_point_deduct'] = $goodsModel->hasOneSale->min_point_deduct;
  825. }
  826. if (!empty($data['min_point_deduct'])) {
  827. $data['first_strip_key'] = 'min_point_deduct';
  828. $data['sale_count'] += 1;
  829. }
  830. }
  831. if ($goodsModel->hasOneGoodsCoupon->is_give) {
  832. $data['coupon'] = $goodsModel->hasOneGoodsCoupon->send_type ? '商品订单完成返优惠券' : '每月一号返优惠券';
  833. $data['first_strip_key'] = 'coupon';
  834. $data['sale_count'] += 1;
  835. }
  836. if ($goodsModel->hasOneSale->ed_num) {
  837. $data['ed_num'] = $goodsModel->hasOneSale->ed_num;
  838. $data['first_strip_key'] = 'ed_num';
  839. $data['sale_count'] += 1;
  840. }
  841. if ($goodsModel->hasOneSale->ed_money) {
  842. $data['ed_money'] = $goodsModel->hasOneSale->ed_money;
  843. $data['first_strip_key'] = 'ed_money';
  844. $data['sale_count'] += 1;
  845. }
  846. $exist_love = app('plugins')->isEnabled('love');
  847. if ($exist_love) {
  848. $love_goods = $this->getLoveSet($goodsModel, $goodsModel->id);
  849. $data['love_name'] = $love_goods['name'];
  850. if ($love_goods['deduction']) {
  851. $data['deduction_proportion'] = $love_goods['deduction_proportion'];
  852. $data['first_strip_key'] = 'deduction_proportion';
  853. $data['sale_count'] += 1;
  854. }
  855. if ($love_goods['award']) {
  856. $data['award_proportion'] = $love_goods['award_proportion'];
  857. $data['first_strip_key'] = 'award_proportion';
  858. $data['sale_count'] += 1;
  859. }
  860. }
  861. $exist_commission = app('plugins')->isEnabled('commission');
  862. if ($exist_commission) {
  863. $commission_data = (new GoodsDetailService($goodsModel))->getGoodsDetailData();
  864. if ($commission_data['commission_show'] == 1) {
  865. $data['sale_count'] += 1;
  866. $data['first_strip_key'] = 'commission_show';
  867. }
  868. $data = array_merge($data, $commission_data);
  869. }
  870. return $data;
  871. }
  872. /**
  873. * 获取商品爱心值设置
  874. */
  875. public function getLoveSet($goods, $goods_id)
  876. {
  877. $data = [
  878. 'name' => \Setting::get('love.name') ?: '爱心值',
  879. 'deduction' => 0, //是否开启爱心值抵扣 0否,1是
  880. 'deduction_proportion' => 0, //爱心值最高抵扣
  881. 'award' => 0, //是否开启爱心值奖励 0否,1是
  882. 'award_proportion' => 0, //奖励爱心值
  883. ];
  884. $love_set = \Setting::get('love');
  885. $res = app('plugins')->isEnabled('store-cashier');
  886. if ($res) {//门店抵扣设置
  887. $store_goods = StoreGoods::where('goods_id', $goods_id)->first();
  888. $love = StoreSetting::getStoreSettingByStoreId($store_goods->store_id)->where('key', 'love')->first();
  889. $set = \Setting::get('plugin.store_widgets', 'deduction_proportion');
  890. // dd($set['love']['deduction_proportion'],$love->value['deduction_proportion']);
  891. }
  892. $item = GoodsLove::ofGoodsId($goods->id)->first();
  893. // dd($item->deduction);
  894. $deduction = 0;
  895. // $deduction_proportion = (bccomp($item->deduction_proportion, 0.00, 2) == 1) ? $item->deduction_proportion : \Setting::get('love.deduction_proportion');
  896. $deduction_proportion = \Setting::get('love.deduction_proportion');
  897. if ($item->deduction) {//商品独立设置
  898. if ($love_set['deduction']) {
  899. $deduction_proportion = $love_set['deduction_proportion'];
  900. $deduction = $love_set['deduction'];
  901. }
  902. // if (!empty($set['love']['deduction'])){//平台设置
  903. // $deduction_proportion = $set['love']['deduction_proportion'];
  904. // $deduction = $set['love']['deduction'];
  905. // }
  906. // $price = $goods->price * ($deduction_proportion / 100);love[deduction_proportion_low]
  907. if (!empty($love) && $love->value['deduction_proportion'] && $love->value['deduction_proportion'] != 0) {//门店设置
  908. $deduction_proportion = $love->value['deduction_proportion'];
  909. $deduction = $love->value['deduction'];
  910. }
  911. if ($item->deduction_proportion && $item->deduction_proportion != 0) {
  912. $deduction_proportion = $item->deduction_proportion;
  913. $deduction = $item->deduction;
  914. }
  915. $data['deduction'] = $deduction;//$item->deduction;
  916. $data['deduction_proportion'] = $deduction_proportion . '%';
  917. }
  918. // $award_proportion = (bccomp($item->award_proportion, 0.00, 2) == 1) ? $item->award_proportion : \Setting::get('love.award_proportion');
  919. if ($item->award) {
  920. $award = $item->award;
  921. //爱心值插件设置
  922. $award_proportion = \Setting::get('love.award_proportion');
  923. //平台设置
  924. // if (!empty($set)){
  925. // $award_proportion = $set['love']['award_proportion'];
  926. // $award = $set['love']['award'];
  927. // }
  928. // $award_price = $goods->price * ($award_proportion / 100);
  929. //门店设置
  930. if (!empty($love) && $love->value['award_proportion'] && $love->value['award_proportion'] != 0) {
  931. $award_proportion = $love->value['award_proportion'];
  932. $award = $love->value['award'];
  933. }
  934. //商品独立设置
  935. if ($item->award_proportion && $item->award_proportion != 0) {
  936. $award_proportion = $item->award_proportion;//bccomp($item->award_proportion, 0.00, 2);
  937. $award = $item->award;
  938. }
  939. $data['award'] = $love_set['award'] ? $award : 0;//$item->award;
  940. $data['award_proportion'] = $award_proportion . '%';
  941. }
  942. // dd(\Setting::get('love.award_proportion'),$set['love']['award_proportion'],$love->value['award_proportion'],$item->award_proportion,bccomp(66, 0.00, 2));
  943. return $data;
  944. }
  945. /**
  946. * 是否开启领优惠卷
  947. * @param $member
  948. * @return \Illuminate\Http\JsonResponse|int
  949. */
  950. public function couponsMemberLj($member)
  951. {
  952. if (empty($member)) {
  953. throw new AppException('没有找到该用户');
  954. }
  955. $memberLevel = $member->level_id;
  956. $now = strtotime('now');
  957. $coupons = Coupon::getCouponsForMember($member->member_id, $memberLevel, null, $now)
  958. ->orderBy('display_order', 'desc')
  959. ->orderBy('updated_at', 'desc')
  960. ->get();
  961. if ($coupons->isEmpty()) {
  962. return 0;
  963. }
  964. foreach ($coupons as $v) {
  965. if (($v->total == MemberCouponController::NO_LIMIT) || ($v->has_many_member_coupon_count < $v->total)) {
  966. return 1;
  967. }
  968. }
  969. return 0;
  970. }
  971. private function goods_lease_set(&$goodsModel, $lease_switch)
  972. {
  973. if ($lease_switch) {
  974. //TODO 商品租赁设置 $id
  975. if (is_array($goodsModel)) {
  976. $goodsModel['lease_toy'] = LeaseToyGoods::getDate($goodsModel['id']);
  977. } else {
  978. $goodsModel->lease_toy = LeaseToyGoods::getDate($goodsModel->id);
  979. }
  980. } else {
  981. if (is_array($goodsModel)) {
  982. $goodsModel['lease_toy'] = [
  983. 'is_lease' => $lease_switch,
  984. 'is_rights' => 0,
  985. 'immed_goods_id' => 0,
  986. ];
  987. } else {
  988. $goodsModel->lease_toy = [
  989. 'is_lease' => $lease_switch,
  990. 'is_rights' => 0,
  991. 'immed_goods_id' => 0,
  992. ];
  993. }
  994. }
  995. }
  996. public function loveShoppingGift($goodsModel)
  997. {
  998. //爱心值
  999. $exist_love = app('plugins')->isEnabled('love');
  1000. if ($exist_love) {
  1001. $love_goods = $this->getLoveSet($goodsModel, $goodsModel->id);
  1002. if ($love_goods['award'] && \Setting::get('love.goods_detail_show_love') == 2) {
  1003. return '购买赠送' . $love_goods['award_proportion'] . $love_goods['name'];
  1004. }
  1005. }
  1006. return '';
  1007. }
  1008. public function showPush()
  1009. {
  1010. $id = intval(\YunShop::request()->id);
  1011. $goods = Goods::with('hasOneSale')->find($id);
  1012. $show_push = SaleGoods::getPushGoods($goods->hasOneSale);//SaleGoods::getPushGoods($goodsModel->hasOneSale)
  1013. $data['show_push'] = $show_push ?: [];
  1014. $data['content'] = html_entity_decode($goods->content) ?: '';
  1015. return $this->successJson('返回成功', $data);
  1016. }
  1017. public function getComment($goodsId)
  1018. {
  1019. app('db')->cacheSelect = true;
  1020. // $goodsId = \YunShop::request()->goods_id;
  1021. $pageSize = 5;
  1022. $list = Comment::getCommentsByGoods($goodsId)->paginate($pageSize);//
  1023. if ($list) {
  1024. foreach ($list as &$item) {
  1025. $item->nick_name = self::substrCut($item->nick_name);
  1026. $item->reply_count = $item->hasManyReply->count('id');
  1027. $item->head_img_url = $item->head_img_url ? replace_yunshop(yz_tomedia($item->head_img_url)) : yz_tomedia(\Setting::get('shop.shop.logo'));
  1028. }
  1029. //对评论图片进行处理,反序列化并组装完整图片url
  1030. $list = $list->toArray();
  1031. foreach ($list['data'] as &$item) {
  1032. self::unSerializeImage($item);
  1033. }
  1034. return $list;
  1035. }
  1036. return $list;
  1037. }
  1038. /*
  1039. * 获取商品好评率
  1040. */
  1041. public function favorableRate($id)
  1042. {
  1043. //不跟订单关联的新好评率计算公式
  1044. $total = \app\common\models\Comment::where(['goods_id' => $id, 'type' => 1])->count('id');//总评论数
  1045. if ($total <= 0) return '100%';
  1046. $level_comment = \app\common\models\Comment::where(['goods_id' => $id, 'type' => 1])->sum('level');//已评论的分数
  1047. $mark = bcmul($total, 5, 2);//最高总评分 = 总条数 * 5
  1048. //最终好评率 = (已评论分数/最高总评分)/100
  1049. $have_comment = bcmul(bcdiv($level_comment, $mark, 2), 100, 2);
  1050. return $have_comment . '%';
  1051. // $total = OrderGoods::with(['hasOneOrder',function($q){
  1052. // $q->where('status',3);
  1053. // }])->where('goods_id',$id)->count('id');//总条数
  1054. //
  1055. // if ($total <= 0){
  1056. // return '100%';
  1057. // }
  1058. // $level_comment = \app\common\models\Comment::where(['goods_id' => $id,'type'=> 1])->sum('level');//已评论的分数
  1059. // $comment = \app\common\models\Comment::where(['goods_id' => $id,'type'=>1])->count('id');//总评论数
  1060. // $mark = bcmul($total,5,2);//总评分 = 总条数 * 5
  1061. // $no_comment = bcmul(bcsub($total,$comment,2) ,5,2);//未评分 = 总条数 - 已评论条数
  1062. // $have_comment = bcmul(bcdiv(bcadd($level_comment,$no_comment,2),$mark,2),100,2);//最终好评率
  1063. // //最终好评率 = ((已评论分数 + 未评分) / 总评分)/100
  1064. // return $have_comment.'%';
  1065. }
  1066. // 反序列化图片
  1067. public static function unSerializeImage(&$arrComment)
  1068. {
  1069. $arrComment['images'] = unserialize($arrComment['images']);
  1070. foreach ($arrComment['images'] as &$image) {
  1071. $image = yz_tomedia($image);
  1072. }
  1073. // if ($arrComment['append']) {
  1074. // foreach ($arrComment['append'] as &$comment) {
  1075. // $comment['images'] = unserialize($comment['images']);
  1076. // foreach ($comment['images'] as &$image) {
  1077. // $image = yz_tomedia($image);
  1078. // }
  1079. // }
  1080. // }
  1081. if ($arrComment['append']) {
  1082. $arrComment['append']['images'] = unserialize($arrComment['append']['images']);
  1083. foreach ($arrComment['append']['images'] as &$image) {
  1084. $image = yz_tomedia($image);
  1085. }
  1086. }
  1087. if ($arrComment['has_many_reply']) {
  1088. foreach ($arrComment['has_many_reply'] as &$comment) {
  1089. $comment['images'] = unserialize($comment['images']);
  1090. foreach ($comment['images'] as &$image) {
  1091. $image = yz_tomedia($image);
  1092. }
  1093. }
  1094. }
  1095. }
  1096. //直播间
  1097. public function getRoom()
  1098. {
  1099. app('db')->cacheSelect = true;
  1100. $goods_id = intval(request()->goods_id);
  1101. $rooms = Room::select('yz_room.*', 'yz_room_record_file.id as back_id')
  1102. ->where(function ($querys) {
  1103. $querys->whereIn('status', [2, 3])
  1104. ->orwhere(function ($query) {
  1105. $query->where('status', 4)
  1106. ->where('yz_room_record_file.id', '>', 0)
  1107. ->where('yz_room_record_file.is_show', 1);
  1108. });
  1109. })
  1110. ->leftJoin('yz_room_record_file', function ($join) {
  1111. $join->on('yz_room_record_file.room_id', '=', 'yz_room.id');
  1112. })
  1113. ->with('hasOneMember')
  1114. ->wherehas('hasManyGoods', function ($query) use ($goods_id) {
  1115. $query->where('goods_id', $goods_id);
  1116. })
  1117. ->whereNull('yz_room_record_file.deleted_at')
  1118. ->orderByRaw("FIELD(status, " . implode(", ", [3, 2, 4]) . ")")
  1119. ->orderBy('yz_room.recommend', 'asc')
  1120. ->orderBy('yz_room_record_file.recommend', 'asc')
  1121. ->orderBy('yz_room.id', 'desc')
  1122. ->paginate(10);
  1123. $room = [];
  1124. foreach ($rooms as $key => $val) {
  1125. $room[$key]['avatar'] = $val->hasOneMember['avatar_image'];
  1126. $room[$key]['nickname'] = $val->hasOneMember['nickname'];
  1127. $room[$key]['id'] = $val->id;
  1128. $room[$key]['status'] = $val->status;
  1129. $room[$key]['title'] = $val->title;
  1130. $room[$key]['cover'] = yz_tomedia($val->cover);
  1131. $room[$key]['banner'] = yz_tomedia($val->banner);
  1132. $room[$key]['live_time'] = $val->live_time;
  1133. $room[$key]['view_num'] = $val->view_count + $val->virtual;
  1134. if (empty($val->goods)) {
  1135. $room[$key]['goods_num'] = 0;
  1136. } else {
  1137. $room[$key]['goods_num'] = count(explode(',', $val->goods));
  1138. }
  1139. if ($val->status == 2) {
  1140. $room[$key]['play_type'] = 3;
  1141. } elseif ($val->status == 3) {
  1142. $room[$key]['play_type'] = 1;
  1143. } else {
  1144. $room[$key]['play_type'] = 2;
  1145. $room[$key]['back_id'] = $val->back_id;
  1146. }
  1147. }
  1148. $json = $rooms->toArray();
  1149. $json['data'] = $room;
  1150. return $this->successJson('成功', $json);
  1151. }
  1152. /**
  1153. * 只保留字符串首尾字符,隐藏中间用*代替(两个字符时只显示第一个)
  1154. * @param string $user_name 姓名
  1155. * @return string 格式化后的姓名
  1156. */
  1157. function substrCut($user_name)
  1158. {
  1159. $strlen = mb_strlen($user_name, 'utf-8');
  1160. $firstStr = mb_substr($user_name, 0, 1, 'utf-8');
  1161. $lastStr = mb_substr($user_name, -1, 1, 'utf-8');
  1162. if ($strlen < 2) {
  1163. return $user_name;
  1164. } else {
  1165. return $strlen == 2 ? $firstStr . str_repeat('*', mb_strlen($user_name, 'utf-8') - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
  1166. }
  1167. }
  1168. /**
  1169. * @description 获取商品积分
  1170. * @param $goodsModel
  1171. * @return mixed|string
  1172. */
  1173. private function setGoodPoints($goodsModel)
  1174. {
  1175. $points = '';
  1176. $tradeGoodsPointsServer = new TradeGoodsPointsServer();
  1177. if ($tradeGoodsPointsServer->close(TradeGoodsPointsServer::SEARCH_PAGE)) {
  1178. return $points;
  1179. }
  1180. $tradeGoodsPointsServer->getPointSet($goodsModel);
  1181. $points = $tradeGoodsPointsServer->finalSetPoint($points);
  1182. return $tradeGoodsPointsServer->getPoint($points, $goodsModel->price, $goodsModel->cost_price);
  1183. }
  1184. public function getLimitBuyGoods()
  1185. {
  1186. $page_size = request()->page_size ? : 20;
  1187. $goods_model = \app\common\modules\shop\ShopConfig::current()->get('goods.models.commodity_classification');
  1188. $goods_model = new $goods_model;
  1189. $goods = $goods_model->uniacid()->select('yz_goods.id','yz_goods.title','yz_goods.thumb','yz_goods.market_price',
  1190. 'yz_goods.show_sales','yz_goods.virtual_sales','yz_goods.price','yz_goods.stock','yz_goods.has_option', 'yz_goods.plugin_id',
  1191. 'yz_goods_limitbuy.start_time','yz_goods_limitbuy.end_time')
  1192. ->join('yz_goods_limitbuy',function ($join) {
  1193. $join->on('yz_goods.id','=','yz_goods_limitbuy.goods_id')->where(function ($where) {
  1194. return $where->where('yz_goods_limitbuy.status',1)
  1195. ->where('yz_goods_limitbuy.start_time' ,'<=',time())
  1196. ->where('yz_goods_limitbuy.end_time' ,'>',time());
  1197. });
  1198. })
  1199. ->with(['hasManyOptions' => function($query){
  1200. $query->select('id', 'goods_id', 'title', 'thumb', 'product_price', 'market_price', 'stock', 'specs', 'weight');
  1201. }])
  1202. ->where('yz_goods.status',1)
  1203. ->orderBy('yz_goods.is_recommand','desc')
  1204. ->orderBy('yz_goods.display_order','desc')
  1205. ->orderBy('yz_goods.id', 'desc');
  1206. $goods = $goods->paginate($page_size);
  1207. foreach ($goods as $good) {
  1208. //前端需要goods_id
  1209. $good->goods_id = $good->id;
  1210. $good->buyNum = 0;
  1211. $good->thumb = yz_tomedia($good->thumb);
  1212. if ($good->has_option) {
  1213. $good->min_price = $good->hasManyOptions->min("product_price");
  1214. $good->max_price = $good->hasManyOptions->max("product_price");
  1215. $good->stock = $good->hasManyOptions->sum('stock');
  1216. }
  1217. }
  1218. return $this->successJson('成功', $goods->toArray());
  1219. }
  1220. // 获取商品标签
  1221. private function setGoodsLabel(&$goods)
  1222. {
  1223. $goods['label_list'] = array();
  1224. $filter_ids = GoodsFiltering::where('goods_id',$goods['id'])->get()->pluck('filtering_id')->toArray();
  1225. if (empty($filter_ids)){
  1226. return;
  1227. }
  1228. $goods['label_list'] = SearchFiltering::getAllEnableFiltering()->whereIn('id',$filter_ids)->where('is_front_show',1)->values()->toArray();
  1229. }
  1230. }