Common.php 6.6 KB

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