Common.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. $data['wechat_mini_program_type'] = $arrPlatformConfigs['wechat_mini_program_type'] ?? "";
  93. }
  94. if ($platform == ChannelEnum::CHANNEL_ANDROID_APP){
  95. $data['wechat_mini_program_original'] = $arrPlatformConfigs['wechat_mini_program_original'] ?? "";
  96. $data['wechat_mini_program_url'] = $arrPlatformConfigs['wechat_mini_program_url'] ?? "";
  97. $data['app_wx_appid'] = $arrPlatformConfigs['app_id'] ?? "";
  98. $data['wechat_mini_program_type'] = $arrPlatformConfigs['wechat_mini_program_type'] ?? "";
  99. }
  100. // 微信小程序处理
  101. if ($platform == ChannelEnum::CHANNEL_WECHAT_MINI_PROGRAM){
  102. // 微信appid
  103. $data['wx_appid'] = $arrPlatformConfigs['app_id'] ?? "";
  104. $data['app_url'] = !empty($arrPlatformConfigs['app_url']) ? cdnurl($arrPlatformConfigs['app_url'], true) : '';
  105. }
  106. // 反馈类型
  107. $data['feedback_type'] = FeedbackEnum::getFeedbackTypeMap();
  108. // 性别
  109. $data['gender'] = UserEnum::getGenderMap();
  110. $this->success('', $data);
  111. }
  112. /**
  113. * 读取省市区数据,联动列表
  114. */
  115. public function area()
  116. {
  117. $province = $this->request->param('province', '');
  118. $city = $this->request->param('city', '');
  119. $where = ['pid' => 0, 'level' => 1];
  120. $provincelist = null;
  121. if ($province !== '') {
  122. $where['pid'] = $province;
  123. $where['level'] = 2;
  124. }
  125. if ($city !== '') {
  126. $where['pid'] = $city;
  127. $where['level'] = 3;
  128. }
  129. $provincelist = Area::where($where)->field('id as value,name as label')->where('status', 'normal')->select();
  130. $this->success('', $provincelist);
  131. }
  132. public function getBannerList()
  133. {
  134. $type = $this->request->param('type');
  135. // 验证参数
  136. if (!$type) {
  137. $this->error('参数错误');
  138. }
  139. $list = Block::getBlockList(['type' => $type]);
  140. $this->success('', $list);
  141. }
  142. public function getDataByIndex()
  143. {
  144. // 固定
  145. $type = ['首页BANNER', '首页LOTTERY','首页ARTICE','首页MEASURE'];
  146. $field = 'id,type,name,image,url,page_name,status,begintime,endtime,title,page_type';
  147. $list = Block::getBlockList(['type' => $type, 'field' => $field]);
  148. // 根据type 区分新的数组
  149. $newList = [
  150. 'banner' => [],
  151. 'lottery' => [],
  152. 'article' => [],
  153. 'measure' => [],
  154. ];
  155. foreach ($list as $item) {
  156. if ($item['type'] == '首页BANNER') {
  157. $bannerList[] = $item;
  158. }
  159. if ($item['type'] == '首页LOTTERY') {
  160. $lotteryList[] = $item;
  161. }
  162. if ($item['type'] == '首页ARTICE') {
  163. $articleList[] = $item;
  164. }
  165. if ($item['type'] == '首页MEASURE') {
  166. $measureList[] = $item;
  167. }
  168. }
  169. $newList['banner'] = $bannerList;
  170. $newList['lottery'] = $lotteryList;
  171. $newList['article'] = $articleList;
  172. $newList['measure'] = $measureList;
  173. // todo 查询活动商品
  174. // 首页推荐的分类
  175. $indexCategoryList = Category::where('status', StatusEnum::ENABLED)
  176. ->field('id,name,image')
  177. ->where('is_recommend',StatusEnum::YES)
  178. ->order('weigh desc')
  179. ->limit(8)
  180. ->select();
  181. //热门分类
  182. $indexHotCategoryList = Category::where('status', StatusEnum::ENABLED)
  183. ->field('id,name,image')
  184. ->where('is_hot',StatusEnum::YES)
  185. ->order('weigh desc')
  186. ->limit(4)
  187. ->select();
  188. //查询 活动商品
  189. $newList['indexCategoryList'] = $indexCategoryList;
  190. $newList['indexHotCategoryList'] = $indexHotCategoryList;
  191. $this->success('', $newList);
  192. }
  193. /**
  194. * 上传文件
  195. * @ApiMethod (POST)
  196. * @ApiParams (name="file", type="file", required=true, description="文件流")
  197. */
  198. public function upload()
  199. {
  200. Config::set('default_return_type', 'json');
  201. //必须设定cdnurl为空,否则cdnurl函数计算错误
  202. Config::set('upload.cdnurl', '');
  203. $chunkid = $this->request->post("chunkid");
  204. if ($chunkid) {
  205. if (!Config::get('upload.chunking')) {
  206. $this->error(__('Chunk file disabled'));
  207. }
  208. $action = $this->request->post("action");
  209. $chunkindex = $this->request->post("chunkindex/d");
  210. $chunkcount = $this->request->post("chunkcount/d");
  211. $filename = $this->request->post("filename");
  212. $method = $this->request->method(true);
  213. if ($action == 'merge') {
  214. $attachment = null;
  215. //合并分片文件
  216. try {
  217. $upload = new Upload();
  218. $attachment = $upload->merge($chunkid, $chunkcount, $filename);
  219. } catch (UploadException $e) {
  220. $this->error($e->getMessage());
  221. }
  222. $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  223. } elseif ($method == 'clean') {
  224. //删除冗余的分片文件
  225. try {
  226. $upload = new Upload();
  227. $upload->clean($chunkid);
  228. } catch (UploadException $e) {
  229. $this->error($e->getMessage());
  230. }
  231. $this->success();
  232. } else {
  233. //上传分片文件
  234. //默认普通上传文件
  235. $file = $this->request->file('file');
  236. try {
  237. $upload = new Upload($file);
  238. $upload->chunk($chunkid, $chunkindex, $chunkcount);
  239. } catch (UploadException $e) {
  240. $this->error($e->getMessage());
  241. }
  242. $this->success();
  243. }
  244. } else {
  245. $attachment = null;
  246. //默认普通上传文件
  247. $file = $this->request->file('file');
  248. try {
  249. $upload = new Upload($file);
  250. $attachment = $upload->upload();
  251. } catch (UploadException $e) {
  252. $this->error($e->getMessage());
  253. } catch (\Exception $e) {
  254. $this->error($e->getMessage());
  255. }
  256. $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  257. }
  258. }
  259. /**
  260. * 验证码
  261. * @ApiParams (name="id", type="string", required=true, description="要生成验证码的标识")
  262. * @return \think\Response
  263. */
  264. public function captcha($id = "")
  265. {
  266. \think\Config::set([
  267. 'captcha' => array_merge(config('captcha'), [
  268. 'fontSize' => 44,
  269. 'imageH' => 150,
  270. 'imageW' => 350,
  271. ])
  272. ]);
  273. $captcha = new Captcha((array)Config::get('captcha'));
  274. return $captcha->entry($id);
  275. }
  276. }