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.

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