Common.php 11 KB

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