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.

341 lines
11 KiB

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