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.

337 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
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. // $config = config('wechat.official');
  184. // $app = Factory::officialAccount(['app_id' => $config['app_id'], 'secret' => $config['app_secret']]);
  185. // $app['guzzle_handler'] = CoroutineHandler::class;
  186. // $fansInfo = $app->user->get($data['user']['openid']);
  187. //
  188. // $redis = ApplicationContext::getContainer()->get(Redis::class);
  189. // if ($fansInfo['subscribe'] == 1) {
  190. // $subscribeInfo = $redis->hGet('official:subscribe_info', 'subscribed');
  191. // } else {
  192. // $subscribeInfo = $redis->hGet('official:subscribe_info', 'not_subscribed');
  193. // }
  194. //
  195. // $subscribeInfo = !empty($subscribeInfo) ? json_decode($subscribeInfo, true) : [];
  196. // $data['subscribe_info'] = array_merge($fansInfo, $subscribeInfo);
  197. return $this->success($data);
  198. }
  199. /**
  200. * 商家入口详情
  201. */
  202. public function storeIndex(StoreIndexRequest $request)
  203. {
  204. $params = $request->validated();
  205. $userId = Store::query()->where('id',$params['store_id'])->value('user_id');
  206. //每日 每周 每月 开始结束时间
  207. $dayStartTime = strtotime(date('Y-m-d'.' 00:00:00'));
  208. $dayEndTime = strtotime(date('Y-m-d'.' 23:59:59'));
  209. $weekStartTime = mktime(0,0,0,date('m'),date('d')-date('N')+1,date('y'));
  210. //$weekEndTime = mktime(23,59,59,date('m'),date('d')-date('N')+7,date('Y'));
  211. $monthStartTime = mktime(0,0,0,date('m'),1,date('Y'));
  212. //$monthEndTime = mktime(23,59,59,date('m'),date('t'),date('Y'));
  213. $data['detail'] = $this->storeService->detail($params['store_id']);
  214. //当面付 and 线上订单个数统计
  215. $data['order_online']['count'] = $this->orderStatisticsService->countOrder($params['store_id'],OrderType::ONLINE,$dayStartTime,$dayEndTime);
  216. $data['order_offline']['count'] = $this->orderStatisticsService->countOrder($params['store_id'],OrderType::OFFLINE,$dayStartTime,$dayEndTime);
  217. //统计订单金额
  218. $type = [
  219. FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_COMP,
  220. FinancialRecord::MONEY_TYPE_STORE_OFL_ORDER_COMP,
  221. FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_DIRECT_REFUND,
  222. ];
  223. $sumAmounts = $this->financialRecordService->sumAmountGroup($userId, UserType::STORE, $type, $dayStartTime, $dayEndTime);
  224. $data['order_online']['total'] = bcsub(
  225. ($sumAmounts[FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_COMP]??'0'),
  226. ($sumAmounts[FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_DIRECT_REFUND]??'0'),
  227. 2
  228. );
  229. $data['order_offline']['total'] = bcadd(
  230. ($sumAmounts[FinancialRecord::MONEY_TYPE_STORE_OFL_ORDER_COMP]??'0'),
  231. '0',
  232. 2
  233. );
  234. // $revenueByOrder = $this->revenueListService->getRevenueByUser($userId,$type,$dayStartTime,$dayEndTime);
  235. // foreach ($revenueByOrder as $order){
  236. // if($order['money_type'] == FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_COMP){
  237. // //线上订单
  238. // $data['order_online']['total'] = bcadd($data['order_online']['total'],$order['money'],2);
  239. // }else if($order['money_type'] == FinancialRecord::MONEY_TYPE_STORE_OFL_ORDER_COMP){
  240. // //线下订单
  241. // $data['order_offline']['total'] = bcadd($data['order_offline']['total'],$order['money'],2);
  242. // }
  243. // }
  244. //统计新增用户 无法筛选时间 所以和订单分开查询
  245. $revenueByNewUsers = $this->revenueListService->getRevenueByUser($userId,[FinancialRecord::MONEY_TYPE_STORE_PLAT_NEW_USER]);
  246. $data['new_user'] = [
  247. 'day' => 0,
  248. 'week' => 0,
  249. 'month' => 0,
  250. 'all' => 0
  251. ];
  252. foreach ($revenueByNewUsers as $newUser)
  253. {
  254. if($newUser['created_at'] > $dayStartTime){
  255. $data['new_user']['day']++;
  256. }
  257. if($newUser['created_at'] > $weekStartTime){
  258. $data['new_user']['week']++;
  259. }
  260. if($newUser['created_at'] > $monthStartTime){
  261. $data['new_user']['month']++;
  262. }
  263. $data['new_user']['all']++;
  264. }
  265. $data['badge'] = $this->badgeService->allByStoreOrder($params['store_id']);
  266. return $this->success($data);
  267. }
  268. /**
  269. * 关于懒族
  270. * about lanzu
  271. */
  272. public function aboutLanzu()
  273. {
  274. $data = [
  275. [
  276. 'id' => 1,
  277. 'title' => '关于懒族',
  278. 'sub_title' => '懒族生活678',
  279. 'path' => 'https://mp.weixin.qq.com/s/tGKIILpL2extoeKXgYNYIA',
  280. 'path_type' => 'webview'
  281. ],
  282. [
  283. 'id' => 1,
  284. 'title' => '隐私政策',
  285. 'sub_title' => '隐私政策123',
  286. 'path' => 'https://mp.weixin.qq.com/s/HQbJyUJVUYYMIJrHOZNbpA',
  287. 'path_type' => 'webview'
  288. ]
  289. ];
  290. return $this->success(['data' => $data]);
  291. }
  292. /**
  293. * 市场
  294. */
  295. public function marketInfo()
  296. {
  297. $marketId = $this->request->input('market_id', 0);
  298. return $this->success(['market' => Market::query()->find($marketId)]);
  299. }
  300. }