Common.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace addons\shop\controller\api;
  3. use addons\shop\library\Theme;
  4. use think\Config;
  5. use think\Hook;
  6. use addons\shop\model\Navigation;
  7. use addons\shop\model\Area;
  8. use addons\shop\model\Block;
  9. use addons\shop\model\SearchLog;
  10. /**
  11. * 公共
  12. */
  13. class Common extends Base
  14. {
  15. protected $noNeedLogin = ['init', 'area', 'getBannerList','getBannerListByIndex'];
  16. /**
  17. * 初始化
  18. */
  19. public function init()
  20. {
  21. //配置信息
  22. $upload = Config::get('upload');
  23. //如果非服务端中转模式需要修改为中转
  24. if ($upload['storage'] != 'local' && isset($upload['uploadmode']) && $upload['uploadmode'] != 'server') {
  25. //临时修改上传模式为服务端中转
  26. set_addon_config($upload['storage'], ["uploadmode" => "server"], false);
  27. $upload = \app\common\model\Config::upload();
  28. // 上传信息配置后
  29. Hook::listen("upload_config_init", $upload);
  30. $upload = Config::set('upload', array_merge(Config::get('upload'), $upload));
  31. }
  32. $upload['cdnurl'] = $upload['cdnurl'] ? $upload['cdnurl'] : cdnurl('', true);
  33. //上传地址强制切换为使用本地上传,云存储插件会自动处理
  34. $upload['uploadurl'] = url('/api/common/upload', '', false, true);
  35. //支付列表和默认支付方式
  36. $paytypearr = array_filter(explode(',', Config::get('shop.paytypelist')));
  37. $defaultPaytype = Config::get('shop.defaultpaytype');
  38. $defaultPaytype = in_array($defaultPaytype, $paytypearr) ? $defaultPaytype : reset($paytypearr);
  39. //登录类型列表
  40. $logintypearr = array_filter(explode(',', Config::get('shop.logintypelist')));
  41. $config = [
  42. 'upload' => $upload,
  43. //登录类型列表
  44. 'logintypearr' => $logintypearr,
  45. 'paytypelist' => implode(',', $logintypearr),
  46. 'defaultpaytype' => $defaultPaytype,
  47. '__token__' => $this->request->token()
  48. ];
  49. //焦点图
  50. // $bannerList = [];
  51. // $list = Block::getBlockListByName('uniappfocus', 5);
  52. // foreach ($list as $index => $item) {
  53. // $bannerList[] = ['image' => cdnurl($item['image'], true), 'url' => $item['url'], 'title' => $item['title']];
  54. // }
  55. // $config['swiper'] = $bannerList;
  56. $config['order_timeout'] = Config::get('shop.order_timeout');
  57. $config['sitename'] = Config::get('shop.sitename');
  58. $config['notice'] = Config::get('shop.notice');
  59. $config['phone'] = Config::get('shop.phone');
  60. $config['logisticstype'] = Config::get('shop.logisticstype');
  61. $config['category_mode'] = (int)Config::get('shop.category_mode');
  62. $config['money_score'] = Config::get('shop.money_score');
  63. $config['comment_score'] = Config::get('shop.comment_score');
  64. $config['default_goods_img'] = cdnurl(Config::get('shop.default_goods_img'), true);
  65. $config['default_category_img'] = cdnurl(Config::get('shop.default_category_img'), true);
  66. $config['navigate'] = Navigation::tableList();
  67. $config['brands'] = \addons\shop\model\Brand::field('id,name')->order('weigh desc')->select();
  68. //消息订阅模板id
  69. $config['tpl_ids'] = \addons\shop\model\TemplateMsg::getTplIds();
  70. //热门搜索关键词
  71. $config['hot_keyword'] = SearchLog::order('nums desc')->limit(10)->column('keywords');
  72. //合并主题样式,判断是否预览模式
  73. $isPreview = stripos($this->request->SERVER("HTTP_REFERER"), "mode=preview") !== false;
  74. $themeConfig = $isPreview && \think\Session::get("previewtheme-shop") ? \think\Session::get("previewtheme-shop") : Theme::get();
  75. $themeConfig = Theme::render($themeConfig);
  76. $data = array_merge($config, $themeConfig);
  77. $this->success('', $data);
  78. }
  79. /**
  80. * 读取省市区数据,联动列表
  81. */
  82. public function area()
  83. {
  84. $province = $this->request->param('province', '');
  85. $city = $this->request->param('city', '');
  86. $where = ['pid' => 0, 'level' => 1];
  87. $provincelist = null;
  88. if ($province !== '') {
  89. $where['pid'] = $province;
  90. $where['level'] = 2;
  91. }
  92. if ($city !== '') {
  93. $where['pid'] = $city;
  94. $where['level'] = 3;
  95. }
  96. $provincelist = Area::where($where)->field('id as value,name as label')->where('status', 'normal')->select();
  97. $this->success('', $provincelist);
  98. }
  99. public function getBannerList()
  100. {
  101. $type = $this->request->param('type');
  102. // 验证参数
  103. if (!$type) {
  104. $this->error('参数错误');
  105. }
  106. $list = Block::getBlockList(['type' => $type]);
  107. $this->success('', $list);
  108. }
  109. public function getBannerListByIndex()
  110. {
  111. // 固定
  112. $type = ['首页BANNER', '首页LOTTERY','首页ARTICE','首页MEASURE'];
  113. $field = 'id,type,name,image,url,status,begintime,endtime,title';
  114. $list = Block::getBlockList(['type' => $type, 'field' => $field]);
  115. // 根据type 区分新的数组
  116. $newList = [
  117. 'banner' => [],
  118. 'lottery' => [],
  119. 'article' => [],
  120. 'measure' => [],
  121. ];
  122. $bannerList = [];
  123. $lotteryList = [];
  124. $articleList = [];
  125. $measureList = [];
  126. foreach ($list as $item) {
  127. if ($item['type'] == '首页BANNER') {
  128. $bannerList[] = $item;
  129. }
  130. if ($item['type'] == '首页LOTTERY') {
  131. $lotteryList[] = $item;
  132. }
  133. if ($item['type'] == '首页ARTICE') {
  134. $articleList[] = $item;
  135. }
  136. if ($item['type'] == '首页MEASURE') {
  137. $measureList[] = $item;
  138. }
  139. }
  140. $newList['banner'] = $bannerList;
  141. $newList['lottery'] = $lotteryList;
  142. $newList['article'] = $articleList;
  143. $newList['measure'] = $measureList;
  144. $this->success('', $newList);
  145. }
  146. }