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.

282 lines
8.9 KiB

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