IndexController.php 7.3 KB

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