IndexController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace App\Http\Controllers\Api\v1;
  3. use App\Http\Controllers\Api\webApp;
  4. use App\Services\Api\OrderService;
  5. use App\Services\Api\SocialCircleService;
  6. use App\Services\Api\StoreService;
  7. use App\Services\Common\AdService;
  8. use App\Services\Common\NoticeService;
  9. use App\Services\ConfigService;
  10. use App\Services\RedisService;
  11. /**
  12. * 首页
  13. * Class IndexController
  14. * @package App\Http\Controllers\Api
  15. */
  16. class IndexController extends webApp
  17. {
  18. /**
  19. * 配置信息
  20. * @return array
  21. */
  22. public function config()
  23. {
  24. try {
  25. $appSources = request()->post('app_sources', 'ios');
  26. $cacheKey = "caches:config:app_{$appSources}";
  27. $config = RedisService::get($cacheKey);
  28. if (empty($config)) {
  29. $socialOpen = ConfigService::make()->getConfigByCode('social_open', 0);
  30. $config = [
  31. 'app_name' => ConfigService::make()->getConfigByCode('app_name'),
  32. 'app_logo' => get_image_url(ConfigService::make()->getConfigByCode('app_logo')),
  33. 'app_version' => ConfigService::make()->getConfigByCode('app_version'),
  34. 'app_android_url' => ConfigService::make()->getConfigByCode('app_android_url'),
  35. 'alipay_open' => ConfigService::make()->getConfigByCode('alipay_open', 0),
  36. 'agent_apply_desc' => format_content(ConfigService::make()->getConfigByCode('agent_apply_desc', '')),
  37. 'withdraw_desc' => format_content(ConfigService::make()->getConfigByCode('withdraw_desc', '')),
  38. 'recharge_desc' => format_content(ConfigService::make()->getConfigByCode('recharge_desc', '')),
  39. 'wxpay_open' => ConfigService::make()->getConfigByCode('wxpay_open', 0),
  40. 'pay_recharge_open' => ConfigService::make()->getConfigByCode('pay_recharge_open', 0),
  41. 'social_open' => $socialOpen,
  42. 'withdraw_open' => ConfigService::make()->getConfigByCode('withdraw_open', 0),
  43. 'withdraw_min' => ConfigService::make()->getConfigByCode('withdraw_min', 0),
  44. 'withdraw_fee' => ConfigService::make()->getConfigByCode('withdraw_fee', 0),
  45. 'notices' => NoticeService::make()->getRecommandList(),
  46. 'menuList' => $this->getMenus($socialOpen)
  47. ];
  48. RedisService::set($cacheKey, $config, 3600);
  49. }
  50. return showJson(1010, true, $config);
  51. } catch (\Exception $exception) {
  52. $error = ['data' => $exception->getTrace(), 'err' => $exception->getMessage()];
  53. return showJson(1046, false, $error);
  54. }
  55. }
  56. /**
  57. * 菜单
  58. * @param int $socialOpen
  59. * @return array[]
  60. */
  61. public function getMenus($socialOpen=-1)
  62. {
  63. $socialOpen = $socialOpen>=0?$socialOpen:ConfigService::make()->getConfigByCode('social_open', 0);
  64. return [
  65. [
  66. "name" => '首页',
  67. "code" => 'home',
  68. "icon" => 'home',
  69. "status"=> 1,
  70. "page" => '/pages/index/index'
  71. ], [
  72. "name" => '客服',
  73. "code" => 'custom',
  74. "icon" => 'custom',
  75. "status"=> 1,
  76. ], [
  77. "name" => '',
  78. "code" => 'social',
  79. "icon" => 'social',
  80. "status"=> $socialOpen?1:0,
  81. "page" => '/pages/social/index'
  82. ], [
  83. "name" => '购物车',
  84. "code" => 'cart',
  85. "icon" => 'cart',
  86. "badge" => OrderService::make()->getCountByStatus($this->userId,1),
  87. "status"=> 1,
  88. "page" => '/pages/cart/index'
  89. ], [
  90. "name" => '我的',
  91. "code" => 'mine',
  92. "icon" => 'mine',
  93. "status"=> 1,
  94. "page" => '/pages/mine/index'
  95. ]
  96. ];
  97. }
  98. /**
  99. * 首页数据
  100. * @return array
  101. */
  102. public function data()
  103. {
  104. try {
  105. $params = request()->all();
  106. $type = isset($params['type']) && $params['type'] ? $params['type'] : 1;
  107. $position = isset($params['position']) && $params['position'] ? $params['position'] : 1;
  108. $data = [
  109. // 轮播
  110. 'banners' => AdService::make()->getListByPosition($position),
  111. ];
  112. if ($type == 2) {
  113. $data['socials'] = SocialCircleService::make()->getIndexList();
  114. } else {
  115. $data['notices'] = NoticeService::make()->getRecommandList();
  116. $data['menuList'] = $this->getMenus();
  117. }
  118. return showJson(1010, true, $data);
  119. } catch (\Exception $exception) {
  120. $error = ['data' => $exception->getTrace(), 'err' => $exception->getMessage()];
  121. return showJson(1046, false, $error);
  122. }
  123. }
  124. /**
  125. * 主页店铺商品
  126. * @return array
  127. */
  128. public function storeGoods()
  129. {
  130. $params = request()->post();
  131. try {
  132. $params['user_id'] = $this->userId;
  133. $datas = StoreService::make()->getGoodsList($params);
  134. return showJson(StoreService::make()->getError(), true, $datas);
  135. } catch (\Exception $exception) {
  136. $error = ['data' => $exception->getTrace(), 'err' => $exception->getMessage()];
  137. return showJson(1046, false, $error);
  138. }
  139. }
  140. /**
  141. * 验证更新
  142. * @return array
  143. */
  144. public function versionCheck()
  145. {
  146. $version = request()->post('version', '');
  147. $appSources = request()->post('app_sources', 'ios');
  148. $currentVersion = ConfigService::make()->getConfigByCode('app_version');
  149. if (getVersion($version) < getVersion($currentVersion)) {
  150. $data = [
  151. 'has_update' => true,
  152. 'app_version' => $currentVersion,
  153. 'app_name' => ConfigService::make()->getConfigByCode('app_name'),
  154. 'app_url' => ConfigService::make()->getConfigByCode("app_{$appSources}_url"),
  155. 'is_force' => ConfigService::make()->getConfigByCode("app_force_update"),
  156. 'auto_update' => false, // 是否APP内自动更新
  157. ];
  158. return showJson(1010, true, $data);
  159. } else {
  160. return showJson(1010, false, ['has_update' => false, 'version' => $version]);
  161. }
  162. }
  163. }