PcLogic.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. 'service_agreement_content' => ConfigService::get('shop', 'service_agreement_content', ''),
  68. 'is_mobile_register_code' => ConfigService::get('config', 'is_mobile_register_code', config('project.login.is_mobile_register_code')),
  69. //oss域名
  70. 'oss_domain' => FileService::getFileUrl(),
  71. 'oa_uri' => request()->domain() . '/mobile' . ShopPageEnum::HOME_PAGE['mobile'],
  72. 'mobile_uri' => request()->domain() . '/mobile',
  73. 'mp_base64' => $mpBase64,
  74. 'login_ad' => '',
  75. 'seckill_ad' => '',
  76. 'notice_ad' => '',
  77. // 小程序商城关闭状态
  78. 'mnp_status' => ConfigService::get('shop', 'status', 1),
  79. // H5商城关闭状态
  80. 'h5_status' => ConfigService::get('h5', 'status', 1),
  81. // PC商城关闭状态
  82. 'pc_status' => ConfigService::get('pc', 'status', 1),
  83. // 腾讯地图秘钥
  84. 'tencent_map_key' => ConfigService::get('map', 'tencent_map_key',''),
  85. ];
  86. $data['shop']['logo'] = FileService::getFileUrl($data['shop']['logo']);
  87. $data['shop']['login_ad'] = FileService::getFileUrl($data['shop']['login_ad']);
  88. $data['shop']['seckill_ad'] = FileService::getFileUrl($data['shop']['seckill_ad']);
  89. $data['shop']['notice_ad'] = FileService::getFileUrl($data['shop']['notice_ad']);
  90. // pc
  91. $shopName = ConfigService::get('shop', 'name');
  92. $data['pc'] = [
  93. 'title' => ConfigService::get('pc', 'title', $shopName),
  94. 'ico' => ConfigService::get('pc', 'ico'),
  95. 'description' => ConfigService::get('pc', 'description', ''),
  96. 'keywords' => ConfigService::get('pc', 'keywords', ''),
  97. 'tools_code' => ConfigService::get('pc', 'tools_code', ''),
  98. ];
  99. $data['pc']['ico'] = empty($data['pc']['ico']) ? '' : FileService::getFileUrl($data['pc']['ico']);
  100. $data['my'] = [
  101. 'cart' => 0,
  102. 'order' => 0,
  103. 'collect' => 0,
  104. 'user_id' => 0,
  105. 'nickname' => '',
  106. 'avatar' => '',
  107. 'coupon' => '',
  108. ];
  109. if ($params['user_id']) {
  110. $data['my']['user_id'] = $params['user_id'];
  111. $data['my']['cart'] = Cart::where('user_id', $params['user_id'])->count('item_id');
  112. $data['my']['order'] = Order::where('user_id', $params['user_id'])->count('id');
  113. $data['my']['collect'] = GoodsCollect::where('user_id', $params['user_id'])->count('goods_id');
  114. $data['my']['nickname'] = $params['user_info']['nickname'];
  115. $data['my']['avatar'] = empty($params['user_info']['avatar']) ? '' : FileService::getFileUrl($params['user_info']['avatar']);
  116. $data['my']['coupon'] = CouponList::where([
  117. ['user_id', '=', $params['user_id']],
  118. ['status', '=', CouponEnum::USE_STATUS_NOT],
  119. ['invalid_time', '>=', time()]
  120. ])->count();
  121. }
  122. // 公告(最近3条)
  123. $notices = ShopNotice::field('id, name')->order([
  124. 'sort' => 'desc',
  125. 'id' => 'desc',
  126. ])->limit(3)->select()->toArray();
  127. $data['notices'] = $notices;
  128. //装修数据
  129. $page = PcDecorateThemePage::where(['type' => PcDecorateThemePageEnum::TYPE_HOME])
  130. ->field('content,common')->find()->toArray();
  131. $moduleList = array_column($page['content'], 'name');
  132. $headerIndex = array_search('header', $moduleList);
  133. $footerIndex = array_search('footer', $moduleList);
  134. $fixedIndex = array_search('fixed', $moduleList);
  135. $data['decoration']['header'] = $page['content'][$headerIndex] ?? [];
  136. $data['decoration']['footer'] = $page['content'][$footerIndex] ?? [];
  137. $data['decoration']['fixed'] = $page['content'][$fixedIndex] ?? [];
  138. // 站点统计
  139. $data['site_statistic'] = [
  140. 'clarity_app_id' => ConfigService::get('site_statistic', 'clarity_app_id', ''),
  141. ];
  142. return $data;
  143. }
  144. /**
  145. * @notes 获取商品详情
  146. * @author Tab
  147. * @date 2021/11/30 11:15
  148. */
  149. public static function goodsDetail($params)
  150. {
  151. $params['id'] = $params['goods_id'];
  152. $goodsDetail = (new GoodsLogic())->detail($params);
  153. // 秒杀商品处理
  154. if (isset($params['seckill_id']) && $params['seckill_id']) {
  155. self::seckillHandler($goodsDetail, $params);
  156. }
  157. // 添加店铺推荐商品
  158. $goodsDetail['recommend'] = self::recommendGoods();
  159. $goodsDetail['buy_num'] = 0;//已购买数量
  160. $goodsDetail['cart_goods_num'] = 0;
  161. if($params['user_id']) {
  162. //用户下单数量
  163. $goodsDetail['buy_num'] = OrderGoods::alias('og')
  164. ->join('order o', 'o.id = og.order_id')
  165. ->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]])
  166. ->sum('og.goods_num');
  167. //购物车商品数量
  168. $goodsDetail['cart_goods_num'] = Cart::where(['goods_id'=>$params['goods_id'],'user_id'=>$params['user_id']])->sum('goods_num');
  169. }
  170. return $goodsDetail;
  171. }
  172. /**
  173. * @notes 秒杀处理
  174. * @param $goodsDetail
  175. * @param $params
  176. * @author Tab
  177. * @date 2021/12/1 16:25
  178. */
  179. public static function seckillHandler(&$goodsDetail, $params)
  180. {
  181. // 秒杀活动信息
  182. $activity = SeckillActivity::findOrEmpty($params['seckill_id']);
  183. if ($activity->isEmpty()) {
  184. return false;
  185. }
  186. $goodsDetail['seckill'] = [
  187. 'id' => $activity->id,
  188. 'name' => $activity->name,
  189. 'end_time' => $activity->end_time,
  190. ];
  191. // 替换秒杀价
  192. $items = SeckillGoodsItem::where([
  193. 'seckill_id' => $params['seckill_id'],
  194. 'goods_id' => $params['goods_id'],
  195. ])->column('seckill_id,goods_id,item_id,sell_price,seckill_price', 'item_id');
  196. foreach ($goodsDetail['spec_value_list'] as $key => $value) {
  197. $goodsDetail['spec_value_list'][$key]['sell_price'] = isset($items[$value['id']]) ? $items[$value['id']]['seckill_price'] : $goodsDetail['spec_value_list'][$key]['sell_price'];
  198. }
  199. }
  200. /**
  201. * @notes 店铺推荐商品
  202. * @author Tab
  203. * @date 2021/11/30 11:43
  204. */
  205. public static function recommendGoods()
  206. {
  207. $field = [
  208. 'id',
  209. 'name',
  210. 'image',
  211. 'min_price as sell_price',
  212. 'min_lineation_price as lineation_price',
  213. ];
  214. $where = [
  215. 'status' => GoodsEnum::STATUS_SELL
  216. ];
  217. $order = [
  218. 'sales_num' => 'desc',
  219. 'click_num' => 'desc',
  220. 'id' => 'desc',
  221. ];
  222. $goodsLists = Goods::field($field)
  223. ->where($where)
  224. ->order($order)
  225. ->limit(5)
  226. ->select()
  227. ->toArray();
  228. $showPrice = ConfigService::get('goods_set', 'show_price', 1);
  229. if(0 == $showPrice){
  230. foreach ($goodsLists as $key => $goods){
  231. $goodsLists[$key]['lineation_price'] = 0;
  232. }
  233. }
  234. return $goodsLists;
  235. }
  236. /**
  237. * @notes 获取商品评论
  238. * @param $params
  239. * @author Tab
  240. * @date 2021/11/30 11:57
  241. */
  242. public static function goodsCommentCategory($params)
  243. {
  244. $data = (new GoodsCommentLogic())->commentCategory($params);
  245. return $data;
  246. }
  247. /**
  248. * @notes 设置用户信息
  249. * @param $params
  250. * @author Tab
  251. * @date 2021/12/2 16:49
  252. */
  253. public static function setUserInfo($params)
  254. {
  255. try {
  256. $user = User::findOrEmpty($params['user_id']);
  257. if ($user->isEmpty()) {
  258. throw new \Exception('用户不存在');
  259. }
  260. $user->nickname = $params['nickname'];
  261. $user->sex = $params['sex'];
  262. $user->save();
  263. return true;
  264. } catch (\Exception $e) {
  265. self::$error = $e->getMessage();
  266. return false;
  267. }
  268. }
  269. /**
  270. * @notes 售后申请页信息
  271. * @param $params
  272. * @author Tab
  273. * @date 2021/12/3 15:12
  274. */
  275. public static function afterSaleApplyPage($params)
  276. {
  277. $field = [
  278. 'id',
  279. 'goods_snap',
  280. 'goods_price',
  281. 'goods_num',
  282. 'total_pay_price',
  283. ];
  284. $detail = OrderGoods::field($field)
  285. ->where('id', $params['order_goods_id'])
  286. ->findOrEmpty();
  287. if ($detail->isEmpty()) {
  288. return [];
  289. }
  290. $detail = $detail->toArray();
  291. $detail['reason'] = AfterSaleEnum::getReason('', true);
  292. return $detail;
  293. }
  294. /**
  295. * @notes 获取页面
  296. * @param int $type
  297. * @return PcDecorateThemePage|array|\think\Model|null
  298. * @author cjhao
  299. * @date 2021/12/6 18:53
  300. */
  301. public static function getPage(int $type)
  302. {
  303. $page = PcDecorateThemePage::where(['type' => $type])->field('content,common')->find()->toArray();
  304. if (PcDecorateThemePageEnum::TYPE_HOME == $type) {
  305. $page['content'] = ThemeService::getPCModuleData($page['content']);
  306. }
  307. return $page;
  308. }
  309. }