Common.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\Enum\ChannelEnum;
  5. use app\common\exception\UploadException;
  6. use app\common\library\Upload;
  7. use app\common\model\Area;
  8. use app\common\model\Version;
  9. use fast\Random;
  10. use think\captcha\Captcha;
  11. use think\Config;
  12. use think\Hook;
  13. use app\common\library\Theme;
  14. use app\common\model\Navigation;
  15. use app\common\model\Block;
  16. use app\common\model\Category;
  17. use app\common\model\SearchLog;
  18. use app\common\Enum\StatusEnum;
  19. use app\common\Enum\FeedbackEnum;
  20. use app\common\Enum\UserEnum;
  21. use app\common\Service\ShopConfigService;
  22. /**
  23. * 公共接口
  24. */
  25. class Common extends Base
  26. {
  27. protected $noNeedLogin = ['init', 'area', 'getBannerList','getDataByIndex'];
  28. protected $noNeedRight = '*';
  29. protected function convertPlatformName($platform)
  30. {
  31. // 将驼峰命名转换为下划线格式
  32. return strtolower(preg_replace('/([A-Z])/', '_$1', lcfirst($platform)));
  33. }
  34. /**
  35. * 初始化
  36. */
  37. public function init()
  38. {
  39. //配置信息
  40. $upload = Config::get('upload');
  41. $platform = $this->request->header('platform');
  42. // 转换
  43. //如果非服务端中转模式需要修改为中转
  44. if ($upload['storage'] != 'local' && isset($upload['uploadmode']) && $upload['uploadmode'] != 'server') {
  45. //临时修改上传模式为服务端中转
  46. set_addon_config($upload['storage'], ["uploadmode" => "server"], false);
  47. $upload = \app\common\model\Config::upload();
  48. // 上传信息配置后
  49. Hook::listen("upload_config_init", $upload);
  50. $upload = Config::set('upload', array_merge(Config::get('upload'), $upload));
  51. }
  52. $upload['cdnurl'] = $upload['cdnurl'] ? $upload['cdnurl'] : cdnurl('', true);
  53. //上传地址强制切换为使用本地上传,云存储插件会自动处理
  54. $upload['uploadurl'] = url('/api/common/upload', '', false, true);
  55. $arrConfig = ShopConfigService::getConfigs('shop.basic',false);
  56. $arrGoodsConfig = ShopConfigService::getConfigs('shop.goods',false);
  57. $orderTimeout = ShopConfigService::getConfigs('shop.order.order_timeout',false);
  58. $data = [
  59. 'upload' => $upload,
  60. ];
  61. $data['order_timeout'] = $orderTimeout;
  62. $data['sitename'] = $arrConfig['site_name']?? "";
  63. // 手机号
  64. $data['phone'] = $arrConfig['phone']??"";
  65. // 工作时间
  66. $data['workinghours'] = $arrConfig['working_hours']??"";
  67. // 客服邮箱
  68. $data['email'] =$arrConfig['email']??"";
  69. // 公司名称
  70. $data['company'] = $arrConfig['company']??"";
  71. // 站点logo
  72. $data['sitelogo'] = $arrConfig['site_logo']? cdnurl($arrConfig['site_logo'], true) : '';
  73. // 公安备案
  74. $data['gwabeian'] = $arrConfig['public_network_filing']??"";
  75. // icp备案
  76. $data['icpbeian'] = $arrConfig['icp']??"";
  77. // 默认商品图片
  78. $defaultGoodsImg = $arrGoodsConfig['default_goods_img']??"";
  79. $data['default_goods_img'] = !empty($defaultGoodsImg ) ? cdnurl( $defaultGoodsImg, true) : '';
  80. // 默认分类图片
  81. $defaultCategoryImg = $arrGoodsConfig['default_category_img']??"";
  82. $data['default_category_img'] = !empty($defaultCategoryImg)? cdnurl($defaultCategoryImg, true) :'';
  83. $platformKey = $this->convertPlatformName($platform);
  84. $arrPlatformConfigs = ShopConfigService::getConfigs('shop.platform.' . $platformKey, false);
  85. // app 处理
  86. if ($platform == ChannelEnum::CHANNEL_IOS_APP){
  87. // 微信appid
  88. $data['wx_universal_link'] =$arrPlatformConfigs['wx_universal_link'] ?? "";
  89. $data['app_wx_appid'] = $arrPlatformConfigs['app_id'] ?? "";
  90. $data['wechat_mini_program_original'] = $arrPlatformConfigs['wechat_mini_program_original'] ?? "";
  91. $data['wechat_mini_program_url'] = $arrPlatformConfigs['wechat_mini_program_url'] ?? "";
  92. }
  93. if ($platform == ChannelEnum::CHANNEL_ANDROID_APP){
  94. $data['wechat_mini_program_original'] = $arrPlatformConfigs['wechat_mini_program_original'] ?? "";
  95. $data['wechat_mini_program_url'] = $arrPlatformConfigs['wechat_mini_program_url'] ?? "";
  96. $data['app_wx_appid'] = $arrPlatformConfigs['app_id'] ?? "";
  97. }
  98. // 微信小程序处理
  99. if ($platform == ChannelEnum::CHANNEL_WECHAT_MINI_PROGRAM){
  100. // 微信appid
  101. $data['wx_appid'] = $arrPlatformConfigs['app_id'] ?? "";
  102. }
  103. // 反馈类型
  104. $data['feedback_type'] = FeedbackEnum::getFeedbackTypeMap();
  105. // 性别
  106. $data['gender'] = UserEnum::getGenderMap();
  107. $this->success('', $data);
  108. }
  109. /**
  110. * 读取省市区数据,联动列表
  111. */
  112. public function area()
  113. {
  114. $province = $this->request->param('province', '');
  115. $city = $this->request->param('city', '');
  116. $where = ['pid' => 0, 'level' => 1];
  117. $provincelist = null;
  118. if ($province !== '') {
  119. $where['pid'] = $province;
  120. $where['level'] = 2;
  121. }
  122. if ($city !== '') {
  123. $where['pid'] = $city;
  124. $where['level'] = 3;
  125. }
  126. $provincelist = Area::where($where)->field('id as value,name as label')->where('status', 'normal')->select();
  127. $this->success('', $provincelist);
  128. }
  129. public function getBannerList()
  130. {
  131. $type = $this->request->param('type');
  132. // 验证参数
  133. if (!$type) {
  134. $this->error('参数错误');
  135. }
  136. $list = Block::getBlockList(['type' => $type]);
  137. $this->success('', $list);
  138. }
  139. public function getDataByIndex()
  140. {
  141. // 固定
  142. $type = ['首页BANNER', '首页LOTTERY','首页ARTICE','首页MEASURE'];
  143. $field = 'id,type,name,image,url,page_name,status,begintime,endtime,title';
  144. $list = Block::getBlockList(['type' => $type, 'field' => $field]);
  145. // 根据type 区分新的数组
  146. $newList = [
  147. 'banner' => [],
  148. 'lottery' => [],
  149. 'article' => [],
  150. 'measure' => [],
  151. ];
  152. foreach ($list as $item) {
  153. if ($item['type'] == '首页BANNER') {
  154. $bannerList[] = $item;
  155. }
  156. if ($item['type'] == '首页LOTTERY') {
  157. $lotteryList[] = $item;
  158. }
  159. if ($item['type'] == '首页ARTICE') {
  160. $articleList[] = $item;
  161. }
  162. if ($item['type'] == '首页MEASURE') {
  163. $measureList[] = $item;
  164. }
  165. }
  166. $newList['banner'] = $bannerList;
  167. $newList['lottery'] = $lotteryList;
  168. $newList['article'] = $articleList;
  169. $newList['measure'] = $measureList;
  170. // todo 查询活动商品
  171. // 首页推荐的分类
  172. $indexCategoryList = Category::where('status', StatusEnum::ENABLED)
  173. ->field('id,name,image')
  174. ->where('is_recommend',StatusEnum::YES)
  175. ->order('weigh desc')
  176. ->limit(8)
  177. ->select();
  178. //热门分类
  179. $indexHotCategoryList = Category::where('status', StatusEnum::ENABLED)
  180. ->field('id,name,image')
  181. ->where('is_hot',StatusEnum::YES)
  182. ->order('weigh desc')
  183. ->limit(4)
  184. ->select();
  185. //查询 活动商品
  186. $newList['indexCategoryList'] = $indexCategoryList;
  187. $newList['indexHotCategoryList'] = $indexHotCategoryList;
  188. $this->success('', $newList);
  189. }
  190. /**
  191. * 上传文件
  192. * @ApiMethod (POST)
  193. * @ApiParams (name="file", type="file", required=true, description="文件流")
  194. */
  195. public function upload()
  196. {
  197. Config::set('default_return_type', 'json');
  198. //必须设定cdnurl为空,否则cdnurl函数计算错误
  199. Config::set('upload.cdnurl', '');
  200. $chunkid = $this->request->post("chunkid");
  201. if ($chunkid) {
  202. if (!Config::get('upload.chunking')) {
  203. $this->error(__('Chunk file disabled'));
  204. }
  205. $action = $this->request->post("action");
  206. $chunkindex = $this->request->post("chunkindex/d");
  207. $chunkcount = $this->request->post("chunkcount/d");
  208. $filename = $this->request->post("filename");
  209. $method = $this->request->method(true);
  210. if ($action == 'merge') {
  211. $attachment = null;
  212. //合并分片文件
  213. try {
  214. $upload = new Upload();
  215. $attachment = $upload->merge($chunkid, $chunkcount, $filename);
  216. } catch (UploadException $e) {
  217. $this->error($e->getMessage());
  218. }
  219. $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  220. } elseif ($method == 'clean') {
  221. //删除冗余的分片文件
  222. try {
  223. $upload = new Upload();
  224. $upload->clean($chunkid);
  225. } catch (UploadException $e) {
  226. $this->error($e->getMessage());
  227. }
  228. $this->success();
  229. } else {
  230. //上传分片文件
  231. //默认普通上传文件
  232. $file = $this->request->file('file');
  233. try {
  234. $upload = new Upload($file);
  235. $upload->chunk($chunkid, $chunkindex, $chunkcount);
  236. } catch (UploadException $e) {
  237. $this->error($e->getMessage());
  238. }
  239. $this->success();
  240. }
  241. } else {
  242. $attachment = null;
  243. //默认普通上传文件
  244. $file = $this->request->file('file');
  245. try {
  246. $upload = new Upload($file);
  247. $attachment = $upload->upload();
  248. } catch (UploadException $e) {
  249. $this->error($e->getMessage());
  250. } catch (\Exception $e) {
  251. $this->error($e->getMessage());
  252. }
  253. $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  254. }
  255. }
  256. /**
  257. * 验证码
  258. * @ApiParams (name="id", type="string", required=true, description="要生成验证码的标识")
  259. * @return \think\Response
  260. */
  261. public function captcha($id = "")
  262. {
  263. \think\Config::set([
  264. 'captcha' => array_merge(config('captcha'), [
  265. 'fontSize' => 44,
  266. 'imageH' => 150,
  267. 'imageW' => 350,
  268. ])
  269. ]);
  270. $captcha = new Captcha((array)Config::get('captcha'));
  271. return $captcha->entry($id);
  272. }
  273. }