Common.php 11 KB

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