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.

308 lines
9.8 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. $version = $this->request->input('version', -1);
  118. $banners = $this->bannerService->all(Banner::TYPE_APPLET_INDEX, $marketId);
  119. $categories = $this->categoryService->allForAppletIndex();
  120. $activity = $this->activityService->allForAppletIndex(env('APPLET_INDEX_ACTIVITY_TYPE'), $marketId);
  121. $tabs = $this->tabsService->allForAppletIndex($version);
  122. return $this->success([
  123. 'banners' => $banners,
  124. 'categories' => $categories,
  125. 'activity' => ['type' => ActivityType::FLASH_SALE, 'goods' => $activity],
  126. 'tabs' => $tabs,
  127. ]);
  128. }
  129. /**
  130. * 用户首页,我的页面
  131. * 1、用户信息,id、昵称、名字、头像
  132. * 2、收藏、红包、积分
  133. * 3、badge角标,待付款、待收货、已完成、售后, SSDB维护
  134. * 4、为你推荐的商品列表
  135. * @param UserIndexRequest $request
  136. * @return ResponseInterface
  137. */
  138. public function userIndex(UserIndexRequest $request)
  139. {
  140. $params = $request->validated();
  141. $data['user'] = $this->userInfoService->detail($params['user_id']);
  142. $store_info = $this->userInfoService->getStoreByUID($params['user_id']);
  143. $sp_info = $this->userInfoService->getServicePersonnelByUID($params['user_id']);
  144. $data['user']['store_info'] = $store_info;
  145. $data['user']['sp_info'] = $sp_info;
  146. $roles = [];
  147. //角色判断
  148. if($store_info){
  149. $roles[] = [
  150. 'key'=>'store',
  151. 'title'=>'商家',
  152. 'color'=>'#0091FF',
  153. 'data'=>$store_info,
  154. ];
  155. }
  156. if($sp_info){
  157. $roles[] = [
  158. 'key'=>'sp',
  159. 'title'=>'服务',
  160. 'color'=>'#0091FF',
  161. 'data'=>$sp_info,
  162. ];
  163. }
  164. $data['user']['collection_count'] = $this->collectStoreService->countByUser($params['user_id']);
  165. $couponStatistics = $this->couponRecService->statistics($params['user_id']);
  166. $data['user']['coupon_count'] = $couponStatistics['unused'];
  167. $data['user']['role'] = $roles;
  168. $data['badge'] = $this->badgeService->allByUserOrder($params['user_id']);
  169. $data['block'] = $this->userCenterBlockService->all($roles);
  170. return $this->success($data);
  171. }
  172. /**
  173. * 商家入口详情
  174. */
  175. public function storeIndex(StoreIndexRequest $request)
  176. {
  177. $params = $request->validated();
  178. $userId = Store::query()->where('id',$params['store_id'])->value('user_id');
  179. //每日 每周 每月 开始结束时间
  180. $dayStartTime = strtotime(date('Y-m-d'.' 00:00:00'));
  181. $dayEndTime = strtotime(date('Y-m-d'.' 23:59:59'));
  182. $weekStartTime = mktime(0,0,0,date('m'),date('d')-date('N')+1,date('y'));
  183. //$weekEndTime = mktime(23,59,59,date('m'),date('d')-date('N')+7,date('Y'));
  184. $monthStartTime = mktime(0,0,0,date('m'),1,date('Y'));
  185. //$monthEndTime = mktime(23,59,59,date('m'),date('t'),date('Y'));
  186. $data['detail'] = $this->storeService->detail($params['store_id']);
  187. //当面付 and 线上订单个数统计
  188. $data['order_online']['count'] = $this->orderStatisticsService->countOrder($params['store_id'],OrderType::ONLINE,$dayStartTime,$dayEndTime);
  189. $data['order_offline']['count'] = $this->orderStatisticsService->countOrder($params['store_id'],OrderType::OFFLINE,$dayStartTime,$dayEndTime);
  190. //统计订单金额
  191. $type = [
  192. FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_COMP,
  193. FinancialRecord::MONEY_TYPE_STORE_OFL_ORDER_COMP,
  194. FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_DIRECT_REFUND,
  195. ];
  196. $sumAmounts = $this->financialRecordService->sumAmountGroup($userId, UserType::STORE, $type, $dayStartTime, $dayEndTime);
  197. $data['order_online']['total'] = bcsub(
  198. ($sumAmounts[FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_COMP]??'0'),
  199. ($sumAmounts[FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_DIRECT_REFUND]??'0'),
  200. 2
  201. );
  202. $data['order_offline']['total'] = bcadd(
  203. ($sumAmounts[FinancialRecord::MONEY_TYPE_STORE_OFL_ORDER_COMP]??'0'),
  204. '0',
  205. 2
  206. );
  207. // $revenueByOrder = $this->revenueListService->getRevenueByUser($userId,$type,$dayStartTime,$dayEndTime);
  208. // foreach ($revenueByOrder as $order){
  209. // if($order['money_type'] == FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_COMP){
  210. // //线上订单
  211. // $data['order_online']['total'] = bcadd($data['order_online']['total'],$order['money'],2);
  212. // }else if($order['money_type'] == FinancialRecord::MONEY_TYPE_STORE_OFL_ORDER_COMP){
  213. // //线下订单
  214. // $data['order_offline']['total'] = bcadd($data['order_offline']['total'],$order['money'],2);
  215. // }
  216. // }
  217. //统计新增用户 无法筛选时间 所以和订单分开查询
  218. $revenueByNewUsers = $this->revenueListService->getRevenueByUser($userId,[FinancialRecord::MONEY_TYPE_STORE_PLAT_NEW_USER]);
  219. $data['new_user'] = [
  220. 'day' => 0,
  221. 'week' => 0,
  222. 'month' => 0,
  223. 'all' => 0
  224. ];
  225. foreach ($revenueByNewUsers as $newUser)
  226. {
  227. if($newUser['created_at'] > $dayStartTime){
  228. $data['new_user']['day']++;
  229. }
  230. if($newUser['created_at'] > $weekStartTime){
  231. $data['new_user']['week']++;
  232. }
  233. if($newUser['created_at'] > $monthStartTime){
  234. $data['new_user']['month']++;
  235. }
  236. $data['new_user']['all']++;
  237. }
  238. $data['badge'] = $this->badgeService->allByStoreOrder($params['store_id']);
  239. return $this->success($data);
  240. }
  241. /**
  242. * 关于懒族
  243. * about lanzu
  244. */
  245. public function aboutLanzu()
  246. {
  247. $data = [
  248. [
  249. 'id' => 1,
  250. 'title' => '关于懒族',
  251. 'sub_title' => '懒族生活678',
  252. 'path' => 'https://mp.weixin.qq.com/s/tGKIILpL2extoeKXgYNYIA',
  253. 'path_type' => 'webview'
  254. ],
  255. [
  256. 'id' => 1,
  257. 'title' => '隐私政策',
  258. 'sub_title' => '隐私政策123',
  259. 'path' => 'https://mp.weixin.qq.com/s/HQbJyUJVUYYMIJrHOZNbpA',
  260. 'path_type' => 'webview'
  261. ]
  262. ];
  263. return $this->success(['data' => $data]);
  264. }
  265. /**
  266. * 市场
  267. */
  268. public function marketInfo()
  269. {
  270. $marketId = $this->request->input('market_id', 0);
  271. return $this->success(['market' => Market::query()->find($marketId)]);
  272. }
  273. }