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.

375 lines
12 KiB

6 years ago
6 years ago
6 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\Payment;
  8. use App\Constants\v3\SsdbKeys;
  9. use App\Constants\v3\Tabs;
  10. use App\Constants\v3\UserType;
  11. use App\Controller\BaseController;
  12. use App\Model\v3\FinancialRecord;
  13. use App\Model\v3\Market;
  14. use App\Model\v3\Store;
  15. use App\Model\v3\User;
  16. use App\Request\v3\StoreIndexRequest;
  17. use App\Request\v3\UserIndexRequest;
  18. use App\Service\v3\Interfaces\ActivityServiceInterface;
  19. use App\Service\v3\Interfaces\BadgeServiceInterface;
  20. use App\Service\v3\Interfaces\BannerServiceInterface;
  21. use App\Service\v3\Interfaces\CategoryServiceInterface;
  22. use App\Service\v3\Interfaces\CollectStoreServiceInterface;
  23. use App\Service\v3\Interfaces\CouponRecServiceInterface;
  24. use App\Service\v3\Interfaces\FinancialRecordServiceInterface;
  25. use App\Service\v3\Interfaces\MiniprogramServiceInterface;
  26. use App\Service\v3\Interfaces\OrderOnlineServiceInterface;
  27. use App\Service\v3\Interfaces\OrderStatisticsServiceInterface;
  28. use App\Service\v3\Interfaces\RevenueListServiceInterface;
  29. use App\Service\v3\Interfaces\StoreServiceInterface;
  30. use App\Service\v3\Interfaces\TabsServiceInterface;
  31. use App\Service\v3\Interfaces\UserCenterBlockServiceInterface;
  32. use App\Service\v3\Interfaces\UserInfoServiceInterface;
  33. use App\TaskWorker\SSDBTask;
  34. use Hyperf\Di\Annotation\Inject;
  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 MiniprogramServiceInterface
  113. */
  114. protected $miniprogramService;
  115. /**
  116. * 小程序首页,根据market_id
  117. * 1.banner数据
  118. * 2.一级分类
  119. * 3.活动数据(秒杀、团购、新品、无)
  120. * 4.tabs数据
  121. */
  122. public function appletIndex()
  123. {
  124. // 小程序订阅消息发给商户
  125. // 付款金额:{{amount1.DATA}}\n商户名称:{{thing2.DATA}}\n支付方式:{{thing3.DATA}}\n交易单号:{{character_string4.DATA}}\n交易时间:{{time5.DATA}}\n
  126. $tmplId = 'PCRNmKGb7t98xsz_GPux3rhXjsu68TPI3nXR7749dC0';
  127. $page = 'pages/faceOrderList/faceOrderList?store_id=111';
  128. $res1 = $this->miniprogramService->sendSubscribeMessage(
  129. 'ojbLs4sgoo1l6K8TQ_8HhUF6m_1s',
  130. $tmplId,
  131. [
  132. 'amount1' => rand(1,20),
  133. 'thing2' => '测试发送'.date('H:i:s'),
  134. 'thing3' => '微信支付',
  135. 'character_string4' => date('YmdHis'),
  136. 'time5' => $item['created_at_text']??date('Y-m-d H:i:s'),
  137. ],
  138. $page
  139. );
  140. // 小程序订阅消息发给商户
  141. // 订单编号:{{character_string7.DATA}}\n商品名称:{{thing10.DATA}}\n退款金额:{{amount3.DATA}}\n处理时间:{{date4.DATA}}\n退款说明:{{thing5.DATA}}\n
  142. $tmplId = 'OaWeg0q40NVW2WeO5BY1TFdWi4GsgIY1rFBwo2wDa0g';
  143. $page = 'pages/shopOrders/shopOrders?status=refund&store_id=111';
  144. $res2 = $this->miniprogramService->sendSubscribeMessage(
  145. 'ojbLs4sgoo1l6K8TQ_8HhUF6m_1s',
  146. $tmplId,
  147. [
  148. 'character_string7' => date('YmdHis'),
  149. 'thing10' => '测试商品',
  150. 'amount3' => '2.01',
  151. 'date4' => date('Y-m-d H:i:s'),
  152. 'thing5' => '退款',
  153. ],
  154. $page
  155. );
  156. // 小程序订阅消息发给商户
  157. // 支付金额:{{amount1.DATA}}\n订单编号:{{character_string2.DATA}}\n支付时间:{{date3.DATA}}\n商品详情:{{thing4.DATA}}\n备注:{{thing7.DATA}}\n
  158. $tmplId = 'UfCON4Dj_7aH_Q5k_HvDgzUb3gu67TokPITsJuvCMxc';
  159. $page = 'pages/shopOrders/shopOrders?status=paid&store_id=111';
  160. $res3 = $this->miniprogramService->sendSubscribeMessage(
  161. 'ojbLs4sgoo1l6K8TQ_8HhUF6m_1s',
  162. $tmplId,
  163. [
  164. 'amount1' => rand(1,20),
  165. 'character_string2' => date('YmdHis'),
  166. 'date3' => date('Y-m-d H:i:s'),
  167. 'thing4' => '测试商品',
  168. 'thing7' => '无备注',
  169. ],
  170. $page
  171. );
  172. return $this->success([
  173. 'res1' => $res1,
  174. 'res2' => $res2,
  175. 'res3' => $res3,
  176. ]);
  177. $marketId = $this->request->input('market_id', -1);
  178. $version = $this->request->input('version', -1);
  179. $banners = $this->bannerService->all(Banner::TYPE_APPLET_INDEX, $marketId);
  180. $categories = $this->categoryService->allForAppletIndex();
  181. $activity = $this->activityService->allForAppletIndex(env('APPLET_INDEX_ACTIVITY_TYPE'), $marketId);
  182. $tabs = $this->tabsService->allForAppletIndex($version);
  183. return $this->success([
  184. 'banners' => $banners,
  185. 'categories' => $categories,
  186. 'activity' => ['type' => ActivityType::FLASH_SALE, 'goods' => $activity],
  187. 'tabs' => $tabs,
  188. ]);
  189. }
  190. /**
  191. * 用户首页,我的页面
  192. * 1、用户信息,id、昵称、名字、头像
  193. * 2、收藏、红包、积分
  194. * 3、badge角标,待付款、待收货、已完成、售后, SSDB维护
  195. * 4、为你推荐的商品列表
  196. * @param UserIndexRequest $request
  197. * @return ResponseInterface
  198. */
  199. public function userIndex(UserIndexRequest $request)
  200. {
  201. $params = $request->validated();
  202. $data['user'] = $this->userInfoService->detail($params['user_id']);
  203. $store_info = $this->userInfoService->getStoreByUID($params['user_id']);
  204. $employees = $this->userInfoService->getEmployeesByUID($params['user_id']);
  205. $data['user']['store_info'] = $store_info;
  206. $data['user']['sp_info'] = $employees;
  207. $roles = [];
  208. //角色判断
  209. if($store_info){
  210. $roles[] = [
  211. 'key'=>'store',
  212. 'title'=>'商家',
  213. 'color'=>'#0091FF',
  214. 'data'=>$store_info,
  215. ];
  216. }
  217. if($employees){
  218. $roles[] = [
  219. 'key'=>'sp',
  220. 'title'=>'服务',
  221. 'color'=>'#0091FF',
  222. 'data'=>$employees,
  223. ];
  224. }
  225. $data['user']['collection_count'] = $this->collectStoreService->countByUser($params['user_id']);
  226. $couponStatistics = $this->couponRecService->statistics($params['user_id']);
  227. $data['user']['coupon_count'] = $couponStatistics['unused'];
  228. $data['user']['role'] = $roles;
  229. $data['badge'] = $this->badgeService->allByUserOrder($params['user_id']);
  230. $data['block'] = $this->userCenterBlockService->all($roles);
  231. return $this->success($data);
  232. }
  233. /**
  234. * 商家入口详情
  235. */
  236. public function storeIndex(StoreIndexRequest $request)
  237. {
  238. $params = $request->validated();
  239. $userId = Store::query()->where('id',$params['store_id'])->value('user_id');
  240. //每日 每周 每月 开始结束时间
  241. $dayStartTime = strtotime(date('Y-m-d'.' 00:00:00'));
  242. $dayEndTime = strtotime(date('Y-m-d'.' 23:59:59'));
  243. $weekStartTime = mktime(0,0,0,date('m'),date('d')-date('N')+1,date('y'));
  244. //$weekEndTime = mktime(23,59,59,date('m'),date('d')-date('N')+7,date('Y'));
  245. $monthStartTime = mktime(0,0,0,date('m'),1,date('Y'));
  246. //$monthEndTime = mktime(23,59,59,date('m'),date('t'),date('Y'));
  247. $data['detail'] = $this->storeService->detail($params['store_id']);
  248. //当面付 and 线上订单个数统计
  249. $data['order_online']['count'] = $this->orderStatisticsService->countOrder($params['store_id'],OrderType::ONLINE,$dayStartTime,$dayEndTime);
  250. $data['order_offline']['count'] = $this->orderStatisticsService->countOrder($params['store_id'],OrderType::OFFLINE,$dayStartTime,$dayEndTime);
  251. //统计订单金额
  252. $type = [
  253. FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_COMP,
  254. FinancialRecord::MONEY_TYPE_STORE_OFL_ORDER_COMP,
  255. FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_DIRECT_REFUND,
  256. ];
  257. $sumAmounts = $this->financialRecordService->sumAmountGroup($userId, UserType::STORE, $type, $dayStartTime, $dayEndTime);
  258. $data['order_online']['total'] = bcsub(
  259. ($sumAmounts[FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_COMP]??'0'),
  260. ($sumAmounts[FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_DIRECT_REFUND]??'0'),
  261. 2
  262. );
  263. $data['order_offline']['total'] = bcadd(
  264. ($sumAmounts[FinancialRecord::MONEY_TYPE_STORE_OFL_ORDER_COMP]??'0'),
  265. '0',
  266. 2
  267. );
  268. // $revenueByOrder = $this->revenueListService->getRevenueByUser($userId,$type,$dayStartTime,$dayEndTime);
  269. // foreach ($revenueByOrder as $order){
  270. // if($order['money_type'] == FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_COMP){
  271. // //线上订单
  272. // $data['order_online']['total'] = bcadd($data['order_online']['total'],$order['money'],2);
  273. // }else if($order['money_type'] == FinancialRecord::MONEY_TYPE_STORE_OFL_ORDER_COMP){
  274. // //线下订单
  275. // $data['order_offline']['total'] = bcadd($data['order_offline']['total'],$order['money'],2);
  276. // }
  277. // }
  278. //统计新增用户 无法筛选时间 所以和订单分开查询
  279. $revenueByNewUsers = $this->revenueListService->getRevenueByUser($userId,[FinancialRecord::MONEY_TYPE_STORE_PLAT_NEW_USER]);
  280. $data['new_user'] = [
  281. 'day' => 0,
  282. 'week' => 0,
  283. 'month' => 0,
  284. 'all' => 0
  285. ];
  286. foreach ($revenueByNewUsers as $newUser)
  287. {
  288. if($newUser['created_at'] > $dayStartTime){
  289. $data['new_user']['day']++;
  290. }
  291. if($newUser['created_at'] > $weekStartTime){
  292. $data['new_user']['week']++;
  293. }
  294. if($newUser['created_at'] > $monthStartTime){
  295. $data['new_user']['month']++;
  296. }
  297. $data['new_user']['all']++;
  298. }
  299. $data['badge'] = $this->badgeService->allByStoreOrder($params['store_id']);
  300. return $this->success($data);
  301. }
  302. /**
  303. * 关于懒族
  304. * about lanzu
  305. */
  306. public function aboutLanzu()
  307. {
  308. $data = [
  309. [
  310. 'id' => 1,
  311. 'title' => '关于懒族',
  312. 'sub_title' => '懒族生活678',
  313. 'path' => 'https://mp.weixin.qq.com/s/tGKIILpL2extoeKXgYNYIA',
  314. 'path_type' => 'webview'
  315. ],
  316. [
  317. 'id' => 1,
  318. 'title' => '隐私政策',
  319. 'sub_title' => '隐私政策123',
  320. 'path' => 'https://mp.weixin.qq.com/s/HQbJyUJVUYYMIJrHOZNbpA',
  321. 'path_type' => 'webview'
  322. ]
  323. ];
  324. return $this->success(['data' => $data]);
  325. }
  326. /**
  327. * 市场
  328. */
  329. public function marketInfo()
  330. {
  331. $marketId = $this->request->input('market_id', 0);
  332. return $this->success(['market' => Market::query()->find($marketId)]);
  333. }
  334. }