You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

307 lines
9.7 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Controller\v3;
  3. use App\Constants\v3\ActivityType;
  4. use App\Constants\v3\Banner;
  5. use App\Constants\v3\OrderState;
  6. use App\Constants\v3\OrderType;
  7. use App\Constants\v3\SsdbKeys;
  8. use App\Constants\v3\Tabs;
  9. use App\Constants\v3\UserType;
  10. use App\Controller\BaseController;
  11. use App\Model\v3\FinancialRecord;
  12. use App\Model\v3\Market;
  13. use App\Model\v3\Store;
  14. use App\Request\v3\StoreIndexRequest;
  15. use App\Request\v3\UserIndexRequest;
  16. use App\Service\v3\Interfaces\ActivityServiceInterface;
  17. use App\Service\v3\Interfaces\BadgeServiceInterface;
  18. use App\Service\v3\Interfaces\BannerServiceInterface;
  19. use App\Service\v3\Interfaces\CategoryServiceInterface;
  20. use App\Service\v3\Interfaces\CollectStoreServiceInterface;
  21. use App\Service\v3\Interfaces\CouponRecServiceInterface;
  22. use App\Service\v3\Interfaces\FinancialRecordServiceInterface;
  23. use App\Service\v3\Interfaces\OrderOnlineServiceInterface;
  24. use App\Service\v3\Interfaces\OrderStatisticsServiceInterface;
  25. use App\Service\v3\Interfaces\RevenueListServiceInterface;
  26. use App\Service\v3\Interfaces\StoreServiceInterface;
  27. use App\Service\v3\Interfaces\TabsServiceInterface;
  28. use App\Service\v3\Interfaces\UserCenterBlockServiceInterface;
  29. use App\Service\v3\Interfaces\UserInfoServiceInterface;
  30. use App\TaskWorker\SSDBTask;
  31. use Hyperf\Di\Annotation\Inject;
  32. use Hyperf\Redis\Redis;
  33. use Hyperf\Utils\ApplicationContext;
  34. use Psr\Http\Message\ResponseInterface;
  35. /**
  36. * 首页相关
  37. * Class HomeController
  38. * @package App\Controller\v3
  39. */
  40. class HomeController extends BaseController
  41. {
  42. /**
  43. * @Inject
  44. * @var UserInfoServiceInterface
  45. */
  46. protected $userInfoService;
  47. /**
  48. * @Inject
  49. * @var CollectStoreServiceInterface
  50. */
  51. protected $collectStoreService;
  52. /**
  53. * @Inject
  54. * @var CouponRecServiceInterface
  55. */
  56. protected $couponRecService;
  57. /**
  58. * @Inject
  59. * @var UserCenterBlockServiceInterface
  60. */
  61. protected $userCenterBlockService;
  62. /**
  63. * @Inject
  64. * @var StoreServiceInterface
  65. */
  66. protected $storeService;
  67. /**
  68. * @Inject
  69. * @var BannerServiceInterface
  70. */
  71. protected $bannerService;
  72. /**
  73. * @Inject
  74. * @var CategoryServiceInterface
  75. */
  76. protected $categoryService;
  77. /**
  78. * @Inject
  79. * @var ActivityServiceInterface
  80. */
  81. protected $activityService;
  82. /**
  83. * @Inject
  84. * @var TabsServiceInterface
  85. */
  86. protected $tabsService;
  87. /**
  88. * @Inject
  89. * @var OrderStatisticsServiceInterface
  90. */
  91. protected $orderStatisticsService;
  92. /**
  93. * @Inject
  94. * @var RevenueListServiceInterface
  95. */
  96. protected $revenueListService;
  97. /**
  98. * @Inject
  99. * @var BadgeServiceInterface
  100. */
  101. protected $badgeService;
  102. /**
  103. * @Inject
  104. * @var FinancialRecordServiceInterface
  105. */
  106. protected $financialRecordService;
  107. /**
  108. * 小程序首页,根据market_id
  109. * 1.banner数据
  110. * 2.一级分类
  111. * 3.活动数据(秒杀、团购、新品、无)
  112. * 4.tabs数据
  113. */
  114. public function appletIndex()
  115. {
  116. $marketId = $this->request->input('market_id', -1);
  117. $banners = $this->bannerService->all(Banner::TYPE_APPLET_INDEX, $marketId);
  118. $categories = $this->categoryService->allForAppletIndex();
  119. $activity = $this->activityService->allForAppletIndex(env('APPLET_INDEX_ACTIVITY_TYPE'), $marketId);
  120. $tabs = $this->tabsService->allForAppletIndex();
  121. return $this->success([
  122. 'banners' => $banners,
  123. 'categories' => $categories,
  124. 'activity' => ['type' => ActivityType::FLASH_SALE, 'goods' => $activity],
  125. 'tabs' => $tabs,
  126. ]);
  127. }
  128. /**
  129. * 用户首页,我的页面
  130. * 1、用户信息,id、昵称、名字、头像
  131. * 2、收藏、红包、积分
  132. * 3、badge角标,待付款、待收货、已完成、售后, SSDB维护
  133. * 4、为你推荐的商品列表
  134. * @param UserIndexRequest $request
  135. * @return ResponseInterface
  136. */
  137. public function userIndex(UserIndexRequest $request)
  138. {
  139. $params = $request->validated();
  140. $data['user'] = $this->userInfoService->detail($params['user_id']);
  141. $store_info = $this->userInfoService->getStoreByUID($params['user_id']);
  142. $sp_info = $this->userInfoService->getServicePersonnelByUID($params['user_id']);
  143. $data['user']['store_info'] = $store_info;
  144. $data['user']['sp_info'] = $sp_info;
  145. $roles = [];
  146. //角色判断
  147. if($store_info){
  148. $roles[] = [
  149. 'key'=>'store',
  150. 'title'=>'商家',
  151. 'color'=>'#0091FF',
  152. 'data'=>$store_info,
  153. ];
  154. }
  155. if($sp_info){
  156. $roles[] = [
  157. 'key'=>'sp',
  158. 'title'=>'服务',
  159. 'color'=>'#0091FF',
  160. 'data'=>$sp_info,
  161. ];
  162. }
  163. $data['user']['collection_count'] = $this->collectStoreService->countByUser($params['user_id']);
  164. $couponStatistics = $this->couponRecService->statistics($params['user_id']);
  165. $data['user']['coupon_count'] = $couponStatistics['unused'];
  166. $data['user']['role'] = $roles;
  167. $data['badge'] = $this->badgeService->allByUserOrder($params['user_id']);
  168. $data['block'] = $this->userCenterBlockService->all($roles);
  169. return $this->success($data);
  170. }
  171. /**
  172. * 商家入口详情
  173. */
  174. public function storeIndex(StoreIndexRequest $request)
  175. {
  176. $params = $request->validated();
  177. $userId = Store::query()->where('id',$params['store_id'])->value('user_id');
  178. //每日 每周 每月 开始结束时间
  179. $dayStartTime = strtotime(date('Y-m-d'.' 00:00:00'));
  180. $dayEndTime = strtotime(date('Y-m-d'.' 23:59:59'));
  181. $weekStartTime = mktime(0,0,0,date('m'),date('d')-date('N')+1,date('y'));
  182. //$weekEndTime = mktime(23,59,59,date('m'),date('d')-date('N')+7,date('Y'));
  183. $monthStartTime = mktime(0,0,0,date('m'),1,date('Y'));
  184. //$monthEndTime = mktime(23,59,59,date('m'),date('t'),date('Y'));
  185. $data['detail'] = $this->storeService->detail($params['store_id']);
  186. //当面付 and 线上订单个数统计
  187. $data['order_online']['count'] = $this->orderStatisticsService->countOrder($params['store_id'],OrderType::ONLINE,$dayStartTime,$dayEndTime);
  188. $data['order_offline']['count'] = $this->orderStatisticsService->countOrder($params['store_id'],OrderType::OFFLINE,$dayStartTime,$dayEndTime);
  189. //统计订单金额
  190. $type = [
  191. FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_COMP,
  192. FinancialRecord::MONEY_TYPE_STORE_OFL_ORDER_COMP,
  193. FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_DIRECT_REFUND,
  194. ];
  195. $sumAmounts = $this->financialRecordService->sumAmountGroup($userId, UserType::STORE, $type, $dayStartTime, $dayEndTime);
  196. $data['order_online']['total'] = bcsub(
  197. ($sumAmounts[FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_COMP]??'0'),
  198. ($sumAmounts[FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_DIRECT_REFUND]??'0'),
  199. 2
  200. );
  201. $data['order_offline']['total'] = bcadd(
  202. ($sumAmounts[FinancialRecord::MONEY_TYPE_STORE_OFL_ORDER_COMP]??'0'),
  203. '0',
  204. 2
  205. );
  206. // $revenueByOrder = $this->revenueListService->getRevenueByUser($userId,$type,$dayStartTime,$dayEndTime);
  207. // foreach ($revenueByOrder as $order){
  208. // if($order['money_type'] == FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_COMP){
  209. // //线上订单
  210. // $data['order_online']['total'] = bcadd($data['order_online']['total'],$order['money'],2);
  211. // }else if($order['money_type'] == FinancialRecord::MONEY_TYPE_STORE_OFL_ORDER_COMP){
  212. // //线下订单
  213. // $data['order_offline']['total'] = bcadd($data['order_offline']['total'],$order['money'],2);
  214. // }
  215. // }
  216. //统计新增用户 无法筛选时间 所以和订单分开查询
  217. $revenueByNewUsers = $this->revenueListService->getRevenueByUser($userId,[FinancialRecord::MONEY_TYPE_STORE_PLAT_NEW_USER]);
  218. $data['new_user'] = [
  219. 'day' => 0,
  220. 'week' => 0,
  221. 'month' => 0,
  222. 'all' => 0
  223. ];
  224. foreach ($revenueByNewUsers as $newUser)
  225. {
  226. if($newUser['created_at'] > $dayStartTime){
  227. $data['new_user']['day']++;
  228. }
  229. if($newUser['created_at'] > $weekStartTime){
  230. $data['new_user']['week']++;
  231. }
  232. if($newUser['created_at'] > $monthStartTime){
  233. $data['new_user']['month']++;
  234. }
  235. $data['new_user']['all']++;
  236. }
  237. $data['badge'] = $this->badgeService->allByStoreOrder($params['store_id']);
  238. return $this->success($data);
  239. }
  240. /**
  241. * 关于懒族
  242. * about lanzu
  243. */
  244. public function aboutLanzu()
  245. {
  246. $data = [
  247. [
  248. 'id' => 1,
  249. 'title' => '关于懒族',
  250. 'sub_title' => '懒族生活678',
  251. 'path' => 'https://mp.weixin.qq.com/s/tGKIILpL2extoeKXgYNYIA',
  252. 'path_type' => 'webview'
  253. ],
  254. [
  255. 'id' => 1,
  256. 'title' => '隐私政策',
  257. 'sub_title' => '隐私政策123',
  258. 'path' => 'https://mp.weixin.qq.com/s/HQbJyUJVUYYMIJrHOZNbpA',
  259. 'path_type' => 'webview'
  260. ]
  261. ];
  262. return $this->success(['data' => $data]);
  263. }
  264. /**
  265. * 市场
  266. */
  267. public function marketInfo()
  268. {
  269. $marketId = $this->request->input('market_id', 0);
  270. return $this->success(['market' => Market::query()->find($marketId)]);
  271. }
  272. }