IndexController.php 8.3 KB

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