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.

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