PcLogic.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\shopapi\logic;
  20. use app\common\enum\AfterSaleEnum;
  21. use app\common\enum\CouponEnum;
  22. use app\common\enum\GoodsEnum;
  23. use app\common\enum\OrderEnum;
  24. use app\common\enum\PcDecorateThemePageEnum;
  25. use app\common\enum\ShopPageEnum;
  26. use app\common\logic\BaseLogic;
  27. use app\common\model\Cart;
  28. use app\common\model\CouponList;
  29. use app\common\model\Goods;
  30. use app\common\model\GoodsCollect;
  31. use app\common\model\GoodsComment;
  32. use app\common\model\Order;
  33. use app\common\model\OrderGoods;
  34. use app\common\model\PcDecorateThemePage;
  35. use app\common\model\SeckillActivity;
  36. use app\common\model\SeckillGoodsItem;
  37. use app\common\model\ShopNotice;
  38. use app\common\model\User;
  39. use app\common\service\ConfigService;
  40. use app\common\service\FileService;
  41. use app\common\service\ThemeService;
  42. use app\common\service\WeChatService;
  43. use app\shopapi\lists\GoodsCommentLists;
  44. /**
  45. * Pc商城
  46. */
  47. class PcLogic extends BaseLogic
  48. {
  49. /**
  50. * @notes 公共数据
  51. * @author Tab
  52. * @date 2021/11/29 16:18
  53. */
  54. public static function commonData($params)
  55. {
  56. $res = WeChatService::makeMpQrCode(['page' => substr(ShopPageEnum::HOME_PAGE['mobile'], 1)], 'base64');
  57. $mpBase64 = '';
  58. if (true == $res) {
  59. $mpBase64 = WeChatService::getReturnData();
  60. }
  61. $data['shop'] = [
  62. 'name' => ConfigService::get('shop', 'name'),
  63. 'logo' => ConfigService::get('shop', 'pc_logo'),
  64. 'copyright' => ConfigService::get('shop', 'copyright', ''),
  65. 'record_number' => ConfigService::get('shop', 'record_number', ''),
  66. 'record_system_link' => ConfigService::get('shop', 'record_system_link', ''),
  67. 'copyright2' => ConfigService::get('shop', 'copyright2', ''),
  68. 'copyright_auth' => ConfigService::get('shop', 'copyright_auth', []) ? : [
  69. 'type' => "1",
  70. 'list' => [
  71. [ 'image' => '', 'name' =>'', 'link' => '' ],
  72. ]
  73. ],
  74. 'service_agreement_content' => ConfigService::get('shop', 'service_agreement_content', ''),
  75. 'is_mobile_register_code' => ConfigService::get('config', 'is_mobile_register_code', config('project.login.is_mobile_register_code')),
  76. //oss域名
  77. 'oss_domain' => FileService::getFileUrl(),
  78. 'oa_uri' => request()->domain() . '/mobile' . ShopPageEnum::HOME_PAGE['mobile'],
  79. 'mobile_uri' => request()->domain() . '/mobile',
  80. 'mp_base64' => $mpBase64,
  81. 'login_ad' => '',
  82. 'seckill_ad' => '',
  83. 'notice_ad' => '',
  84. // 小程序商城关闭状态
  85. 'mnp_status' => ConfigService::get('shop', 'status', 1),
  86. // H5商城关闭状态
  87. 'h5_status' => ConfigService::get('h5', 'status', 1),
  88. // PC商城关闭状态
  89. 'pc_status' => ConfigService::get('pc', 'status', 1),
  90. // 腾讯地图秘钥
  91. 'tencent_map_key' => ConfigService::get('map', 'tencent_map_key',''),
  92. ];
  93. $data['shop']['logo'] = FileService::getFileUrl($data['shop']['logo']);
  94. $data['shop']['login_ad'] = FileService::getFileUrl($data['shop']['login_ad']);
  95. $data['shop']['seckill_ad'] = FileService::getFileUrl($data['shop']['seckill_ad']);
  96. $data['shop']['notice_ad'] = FileService::getFileUrl($data['shop']['notice_ad']);
  97. // pc
  98. $shopName = ConfigService::get('shop', 'name');
  99. $data['pc'] = [
  100. 'title' => ConfigService::get('pc', 'title', $shopName),
  101. 'ico' => ConfigService::get('pc', 'ico'),
  102. 'description' => ConfigService::get('pc', 'description', ''),
  103. 'keywords' => ConfigService::get('pc', 'keywords', ''),
  104. 'tools_code' => ConfigService::get('pc', 'tools_code', ''),
  105. ];
  106. $data['pc']['ico'] = empty($data['pc']['ico']) ? '' : FileService::getFileUrl($data['pc']['ico']);
  107. $data['my'] = [
  108. 'cart' => 0,
  109. 'order' => 0,
  110. 'collect' => 0,
  111. 'user_id' => 0,
  112. 'nickname' => '',
  113. 'avatar' => '',
  114. 'coupon' => '',
  115. ];
  116. if ($params['user_id']) {
  117. $data['my']['user_id'] = $params['user_id'];
  118. $data['my']['cart'] = Cart::where('user_id', $params['user_id'])->count('item_id');
  119. $data['my']['order'] = Order::where('user_id', $params['user_id'])->count('id');
  120. $data['my']['collect'] = GoodsCollect::where('user_id', $params['user_id'])->count('goods_id');
  121. $data['my']['nickname'] = $params['user_info']['nickname'];
  122. $data['my']['avatar'] = empty($params['user_info']['avatar']) ? '' : FileService::getFileUrl($params['user_info']['avatar']);
  123. $data['my']['coupon'] = CouponList::where([
  124. ['user_id', '=', $params['user_id']],
  125. ['status', '=', CouponEnum::USE_STATUS_NOT],
  126. ['invalid_time', '>=', time()]
  127. ])->count();
  128. }
  129. // 公告(最近3条)
  130. $notices = ShopNotice::field('id, name')->order([
  131. 'sort' => 'desc',
  132. 'id' => 'desc',
  133. ])->limit(3)->select()->toArray();
  134. $data['notices'] = $notices;
  135. //装修数据
  136. $page = PcDecorateThemePage::where(['type' => PcDecorateThemePageEnum::TYPE_HOME])
  137. ->field('content,common')->find()->toArray();
  138. $moduleList = array_column($page['content'], 'name');
  139. $headerIndex = array_search('header', $moduleList);
  140. $footerIndex = array_search('footer', $moduleList);
  141. $fixedIndex = array_search('fixed', $moduleList);
  142. $data['decoration']['header'] = $page['content'][$headerIndex] ?? [];
  143. $data['decoration']['footer'] = $page['content'][$footerIndex] ?? [];
  144. $data['decoration']['fixed'] = $page['content'][$fixedIndex] ?? [];
  145. // 站点统计
  146. $data['site_statistic'] = [
  147. 'clarity_app_id' => ConfigService::get('site_statistic', 'clarity_app_id', ''),
  148. ];
  149. return $data;
  150. }
  151. /**
  152. * @notes 获取商品详情
  153. * @author Tab
  154. * @date 2021/11/30 11:15
  155. */
  156. public static function goodsDetail($params)
  157. {
  158. $params['id'] = $params['goods_id'];
  159. $goodsDetail = (new GoodsLogic())->detail($params);
  160. // 秒杀商品处理
  161. if (isset($params['seckill_id']) && $params['seckill_id']) {
  162. self::seckillHandler($goodsDetail, $params);
  163. }
  164. // 添加店铺推荐商品
  165. $goodsDetail['recommend'] = self::recommendGoods();
  166. $goodsDetail['buy_num'] = 0;//已购买数量
  167. $goodsDetail['cart_goods_num'] = 0;
  168. if($params['user_id']) {
  169. //用户下单数量
  170. $goodsDetail['buy_num'] = OrderGoods::alias('og')
  171. ->join('order o', 'o.id = og.order_id')
  172. ->where(['og.goods_id'=>$params['goods_id'],'o.order_status'=>[OrderEnum::STATUS_WAIT_PAY,OrderEnum::STATUS_WAIT_DELIVERY,OrderEnum::STATUS_WAIT_RECEIVE,OrderEnum::STATUS_FINISH],'o.user_id'=>$params['user_id'],'o.order_type'=>[OrderEnum::NORMAL_ORDER,OrderEnum::VIRTUAL_ORDER]])
  173. ->sum('og.goods_num');
  174. //购物车商品数量
  175. $goodsDetail['cart_goods_num'] = Cart::where(['goods_id'=>$params['goods_id'],'user_id'=>$params['user_id']])->sum('goods_num');
  176. }
  177. return $goodsDetail;
  178. }
  179. /**
  180. * @notes 秒杀处理
  181. * @param $goodsDetail
  182. * @param $params
  183. * @author Tab
  184. * @date 2021/12/1 16:25
  185. */
  186. public static function seckillHandler(&$goodsDetail, $params)
  187. {
  188. // 秒杀活动信息
  189. $activity = SeckillActivity::findOrEmpty($params['seckill_id']);
  190. if ($activity->isEmpty()) {
  191. return false;
  192. }
  193. $goodsDetail['seckill'] = [
  194. 'id' => $activity->id,
  195. 'name' => $activity->name,
  196. 'end_time' => $activity->end_time,
  197. ];
  198. // 替换秒杀价
  199. $items = SeckillGoodsItem::where([
  200. 'seckill_id' => $params['seckill_id'],
  201. 'goods_id' => $params['goods_id'],
  202. ])->column('seckill_id,goods_id,item_id,sell_price,seckill_price', 'item_id');
  203. foreach ($goodsDetail['spec_value_list'] as $key => $value) {
  204. $goodsDetail['spec_value_list'][$key]['sell_price'] = isset($items[$value['id']]) ? $items[$value['id']]['seckill_price'] : $goodsDetail['spec_value_list'][$key]['sell_price'];
  205. }
  206. }
  207. /**
  208. * @notes 店铺推荐商品
  209. * @author Tab
  210. * @date 2021/11/30 11:43
  211. */
  212. public static function recommendGoods()
  213. {
  214. $field = [
  215. 'id',
  216. 'name',
  217. 'image',
  218. 'min_price as sell_price',
  219. 'min_lineation_price as lineation_price',
  220. ];
  221. $where = [
  222. 'status' => GoodsEnum::STATUS_SELL
  223. ];
  224. $order = [
  225. 'sales_num' => 'desc',
  226. 'click_num' => 'desc',
  227. 'id' => 'desc',
  228. ];
  229. $goodsLists = Goods::field($field)
  230. ->where($where)
  231. ->order($order)
  232. ->limit(5)
  233. ->select()
  234. ->toArray();
  235. $showPrice = ConfigService::get('goods_set', 'show_price', 1);
  236. if(0 == $showPrice){
  237. foreach ($goodsLists as $key => $goods){
  238. $goodsLists[$key]['lineation_price'] = 0;
  239. }
  240. }
  241. return $goodsLists;
  242. }
  243. /**
  244. * @notes 获取商品评论
  245. * @param $params
  246. * @author Tab
  247. * @date 2021/11/30 11:57
  248. */
  249. public static function goodsCommentCategory($params)
  250. {
  251. $data = (new GoodsCommentLogic())->commentCategory($params);
  252. return $data;
  253. }
  254. /**
  255. * @notes 设置用户信息
  256. * @param $params
  257. * @author Tab
  258. * @date 2021/12/2 16:49
  259. */
  260. public static function setUserInfo($params)
  261. {
  262. try {
  263. $user = User::findOrEmpty($params['user_id']);
  264. if ($user->isEmpty()) {
  265. throw new \Exception('用户不存在');
  266. }
  267. $user->nickname = $params['nickname'];
  268. $user->sex = $params['sex'];
  269. $user->save();
  270. return true;
  271. } catch (\Exception $e) {
  272. self::$error = $e->getMessage();
  273. return false;
  274. }
  275. }
  276. /**
  277. * @notes 售后申请页信息
  278. * @param $params
  279. * @author Tab
  280. * @date 2021/12/3 15:12
  281. */
  282. public static function afterSaleApplyPage($params)
  283. {
  284. $field = [
  285. 'id',
  286. 'goods_snap',
  287. 'goods_price',
  288. 'goods_num',
  289. 'total_pay_price',
  290. ];
  291. $detail = OrderGoods::field($field)
  292. ->where('id', $params['order_goods_id'])
  293. ->findOrEmpty();
  294. if ($detail->isEmpty()) {
  295. return [];
  296. }
  297. $detail = $detail->toArray();
  298. $detail['reason'] = AfterSaleEnum::getReason('', true);
  299. return $detail;
  300. }
  301. /**
  302. * @notes 获取页面
  303. * @param int $type
  304. * @return PcDecorateThemePage|array|\think\Model|null
  305. * @author cjhao
  306. * @date 2021/12/6 18:53
  307. */
  308. public static function getPage(int $type)
  309. {
  310. $page = PcDecorateThemePage::where(['type' => $type])->field('content,common')->find()->toArray();
  311. if (PcDecorateThemePageEnum::TYPE_HOME == $type) {
  312. $page['content'] = ThemeService::getPCModuleData($page['content']);
  313. }
  314. return $page;
  315. }
  316. }