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.

355 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
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. try {
  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 = ['subscribe' => 0];
  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. } catch (\Exception $e) {
  207. $data['subscribe_info'] = (object)[];
  208. }
  209. return $this->success($data);
  210. }
  211. /**
  212. * 商家入口详情
  213. */
  214. public function storeIndex(StoreIndexRequest $request)
  215. {
  216. $params = $request->validated();
  217. $userId = Store::query()->where('id',$params['store_id'])->value('user_id');
  218. //每日 每周 每月 开始结束时间
  219. $dayStartTime = strtotime(date('Y-m-d'.' 00:00:00'));
  220. $dayEndTime = strtotime(date('Y-m-d'.' 23:59:59'));
  221. $weekStartTime = mktime(0,0,0,date('m'),date('d')-date('N')+1,date('y'));
  222. //$weekEndTime = mktime(23,59,59,date('m'),date('d')-date('N')+7,date('Y'));
  223. $monthStartTime = mktime(0,0,0,date('m'),1,date('Y'));
  224. //$monthEndTime = mktime(23,59,59,date('m'),date('t'),date('Y'));
  225. $data['detail'] = $this->storeService->detail($params['store_id']);
  226. //当面付 and 线上订单个数统计
  227. $data['order_online']['count'] = $this->orderStatisticsService->countOrder($params['store_id'],OrderType::ONLINE,$dayStartTime,$dayEndTime);
  228. $data['order_offline']['count'] = $this->orderStatisticsService->countOrder($params['store_id'],OrderType::OFFLINE,$dayStartTime,$dayEndTime);
  229. //统计订单金额
  230. $type = [
  231. FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_COMP,
  232. FinancialRecord::MONEY_TYPE_STORE_OFL_ORDER_COMP,
  233. FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_DIRECT_REFUND,
  234. ];
  235. $sumAmounts = $this->financialRecordService->sumAmountGroup($userId, UserType::STORE, $type, $dayStartTime, $dayEndTime);
  236. $data['order_online']['total'] = bcsub(
  237. ($sumAmounts[FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_COMP]??'0'),
  238. ($sumAmounts[FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_DIRECT_REFUND]??'0'),
  239. 2
  240. );
  241. $data['order_offline']['total'] = bcadd(
  242. ($sumAmounts[FinancialRecord::MONEY_TYPE_STORE_OFL_ORDER_COMP]??'0'),
  243. '0',
  244. 2
  245. );
  246. // $revenueByOrder = $this->revenueListService->getRevenueByUser($userId,$type,$dayStartTime,$dayEndTime);
  247. // foreach ($revenueByOrder as $order){
  248. // if($order['money_type'] == FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_COMP){
  249. // //线上订单
  250. // $data['order_online']['total'] = bcadd($data['order_online']['total'],$order['money'],2);
  251. // }else if($order['money_type'] == FinancialRecord::MONEY_TYPE_STORE_OFL_ORDER_COMP){
  252. // //线下订单
  253. // $data['order_offline']['total'] = bcadd($data['order_offline']['total'],$order['money'],2);
  254. // }
  255. // }
  256. //统计新增用户 无法筛选时间 所以和订单分开查询
  257. $revenueByNewUsers = $this->revenueListService->getRevenueByUser($userId,[FinancialRecord::MONEY_TYPE_STORE_PLAT_NEW_USER]);
  258. $data['new_user'] = [
  259. 'day' => 0,
  260. 'week' => 0,
  261. 'month' => 0,
  262. 'all' => 0
  263. ];
  264. foreach ($revenueByNewUsers as $newUser)
  265. {
  266. if($newUser['created_at'] > $dayStartTime){
  267. $data['new_user']['day']++;
  268. }
  269. if($newUser['created_at'] > $weekStartTime){
  270. $data['new_user']['week']++;
  271. }
  272. if($newUser['created_at'] > $monthStartTime){
  273. $data['new_user']['month']++;
  274. }
  275. $data['new_user']['all']++;
  276. }
  277. $data['badge'] = $this->badgeService->allByStoreOrder($params['store_id']);
  278. return $this->success($data);
  279. }
  280. /**
  281. * 关于链街
  282. * about lanzu
  283. */
  284. public function aboutLanzu()
  285. {
  286. $data = [
  287. [
  288. 'id' => 1,
  289. 'title' => '关于链街',
  290. 'sub_title' => '链街生活678',
  291. 'path' => 'https://mp.weixin.qq.com/s/tGKIILpL2extoeKXgYNYIA',
  292. 'path_type' => 'webview'
  293. ],
  294. [
  295. 'id' => 1,
  296. 'title' => '隐私政策',
  297. 'sub_title' => '隐私政策123',
  298. 'path' => 'https://mp.weixin.qq.com/s/HQbJyUJVUYYMIJrHOZNbpA',
  299. 'path_type' => 'webview'
  300. ]
  301. ];
  302. return $this->success(['data' => $data]);
  303. }
  304. /**
  305. * 市场
  306. */
  307. public function marketInfo()
  308. {
  309. $marketId = $this->request->input('market_id', 0);
  310. return $this->success(['market' => Market::query()->find($marketId)]);
  311. }
  312. }