Common.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\exception\UploadException;
  5. use app\common\library\Upload;
  6. use app\common\model\Area;
  7. use app\common\model\Version;
  8. use fast\Random;
  9. use think\captcha\Captcha;
  10. use think\Config;
  11. use think\Hook;
  12. use app\common\library\Theme;
  13. use app\common\model\Navigation;
  14. use app\common\model\Block;
  15. use app\common\model\Category;
  16. use app\common\model\SearchLog;
  17. use app\common\Enum\StatusEnum;
  18. /**
  19. * 公共接口
  20. */
  21. class Common extends Api
  22. {
  23. protected $noNeedLogin = ['init', 'area', 'getBannerList','getDataByIndex'];
  24. protected $noNeedRight = '*';
  25. public function _initialize()
  26. {
  27. if (isset($_SERVER['HTTP_ORIGIN'])) {
  28. header('Access-Control-Expose-Headers: __token__');//跨域让客户端获取到
  29. }
  30. //跨域检测
  31. check_cors_request();
  32. if (!isset($_COOKIE['PHPSESSID'])) {
  33. Config::set('session.id', $this->request->server("HTTP_SID"));
  34. }
  35. parent::_initialize();
  36. }
  37. /**
  38. * 加载初始化
  39. *
  40. * @ApiParams (name="version", type="string", required=true, description="版本号")
  41. * @ApiParams (name="lng", type="string", required=true, description="经度")
  42. * @ApiParams (name="lat", type="string", required=true, description="纬度")
  43. */
  44. // public function init()
  45. // {
  46. // if ($version = $this->request->request('version')) {
  47. // $lng = $this->request->request('lng');
  48. // $lat = $this->request->request('lat');
  49. // //配置信息
  50. // $upload = Config::get('upload');
  51. // //如果非服务端中转模式需要修改为中转
  52. // if ($upload['storage'] != 'local' && isset($upload['uploadmode']) && $upload['uploadmode'] != 'server') {
  53. // //临时修改上传模式为服务端中转
  54. // set_addon_config($upload['storage'], ["uploadmode" => "server"], false);
  55. // $upload = \app\common\model\Config::upload();
  56. // // 上传信息配置后
  57. // Hook::listen("upload_config_init", $upload);
  58. // $upload = Config::set('upload', array_merge(Config::get('upload'), $upload));
  59. // }
  60. // $upload['cdnurl'] = $upload['cdnurl'] ? $upload['cdnurl'] : cdnurl('', true);
  61. // $upload['uploadurl'] = preg_match("/^((?:[a-z]+:)?\/\/)(.*)/i", $upload['uploadurl']) ? $upload['uploadurl'] : url($upload['storage'] == 'local' ? '/api/common/upload' : $upload['uploadurl'], '', false, true);
  62. // $content = [
  63. // 'citydata' => Area::getCityFromLngLat($lng, $lat),
  64. // 'versiondata' => Version::check($version),
  65. // 'uploaddata' => $upload,
  66. // 'coverdata' => Config::get("cover"),
  67. // ];
  68. // $this->success('', $content);
  69. // } else {
  70. // $this->error(__('Invalid parameters'));
  71. // }
  72. // }
  73. /**
  74. * 初始化
  75. */
  76. public function init()
  77. {
  78. //配置信息
  79. $upload = Config::get('upload');
  80. //如果非服务端中转模式需要修改为中转
  81. if ($upload['storage'] != 'local' && isset($upload['uploadmode']) && $upload['uploadmode'] != 'server') {
  82. //临时修改上传模式为服务端中转
  83. set_addon_config($upload['storage'], ["uploadmode" => "server"], false);
  84. $upload = \app\common\model\Config::upload();
  85. // 上传信息配置后
  86. Hook::listen("upload_config_init", $upload);
  87. $upload = Config::set('upload', array_merge(Config::get('upload'), $upload));
  88. }
  89. $upload['cdnurl'] = $upload['cdnurl'] ? $upload['cdnurl'] : cdnurl('', true);
  90. //上传地址强制切换为使用本地上传,云存储插件会自动处理
  91. $upload['uploadurl'] = url('/api/common/upload', '', false, true);
  92. //支付列表和默认支付方式
  93. $paytypearr = array_filter(explode(',', Config::get('shop.paytypelist')));
  94. $defaultPaytype = Config::get('shop.defaultpaytype');
  95. $defaultPaytype = in_array($defaultPaytype, $paytypearr) ? $defaultPaytype : reset($paytypearr);
  96. //登录类型列表
  97. $logintypearr = array_filter(explode(',', Config::get('shop.logintypelist')));
  98. $config = [
  99. 'upload' => $upload,
  100. //登录类型列表
  101. 'logintypearr' => $logintypearr,
  102. 'paytypelist' => implode(',', $logintypearr),
  103. 'defaultpaytype' => $defaultPaytype,
  104. '__token__' => $this->request->token()
  105. ];
  106. //焦点图
  107. // $bannerList = [];
  108. // $list = Block::getBlockListByName('uniappfocus', 5);
  109. // foreach ($list as $index => $item) {
  110. // $bannerList[] = ['image' => cdnurl($item['image'], true), 'url' => $item['url'], 'title' => $item['title']];
  111. // }
  112. // $config['swiper'] = $bannerList;
  113. $config['order_timeout'] = Config::get('shop.order_timeout');
  114. $config['sitename'] = Config::get('shop.sitename');
  115. $config['notice'] = Config::get('shop.notice');
  116. $config['phone'] = Config::get('shop.phone');
  117. $config['logisticstype'] = Config::get('shop.logisticstype');
  118. $config['category_mode'] = (int)Config::get('shop.category_mode');
  119. $config['money_score'] = Config::get('shop.money_score');
  120. $config['comment_score'] = Config::get('shop.comment_score');
  121. $config['default_goods_img'] = cdnurl(Config::get('shop.default_goods_img'), true);
  122. $config['default_category_img'] = cdnurl(Config::get('shop.default_category_img'), true);
  123. $config['navigate'] = Navigation::tableList();
  124. $config['brands'] = \addons\shop\model\Brand::field('id,name')->order('weigh desc')->select();
  125. //消息订阅模板id
  126. $config['tpl_ids'] = \addons\shop\model\TemplateMsg::getTplIds();
  127. //热门搜索关键词
  128. $config['hot_keyword'] = SearchLog::order('nums desc')->limit(10)->column('keywords');
  129. //合并主题样式,判断是否预览模式
  130. $isPreview = stripos($this->request->SERVER("HTTP_REFERER"), "mode=preview") !== false;
  131. $themeConfig = $isPreview && \think\Session::get("previewtheme-shop") ? \think\Session::get("previewtheme-shop") : Theme::get();
  132. $themeConfig = Theme::render($themeConfig);
  133. $data = array_merge($config, $themeConfig);
  134. $this->success('', $data);
  135. }
  136. /**
  137. * 读取省市区数据,联动列表
  138. */
  139. public function area()
  140. {
  141. $province = $this->request->param('province', '');
  142. $city = $this->request->param('city', '');
  143. $where = ['pid' => 0, 'level' => 1];
  144. $provincelist = null;
  145. if ($province !== '') {
  146. $where['pid'] = $province;
  147. $where['level'] = 2;
  148. }
  149. if ($city !== '') {
  150. $where['pid'] = $city;
  151. $where['level'] = 3;
  152. }
  153. $provincelist = Area::where($where)->field('id as value,name as label')->where('status', 'normal')->select();
  154. $this->success('', $provincelist);
  155. }
  156. public function getBannerList()
  157. {
  158. $type = $this->request->param('type');
  159. // 验证参数
  160. if (!$type) {
  161. $this->error('参数错误');
  162. }
  163. $list = Block::getBlockList(['type' => $type]);
  164. $this->success('', $list);
  165. }
  166. public function getDataByIndex()
  167. {
  168. // 固定
  169. $type = ['首页BANNER', '首页LOTTERY','首页ARTICE','首页MEASURE'];
  170. $field = 'id,type,name,image,url,status,begintime,endtime,title';
  171. $list = Block::getBlockList(['type' => $type, 'field' => $field]);
  172. // 根据type 区分新的数组
  173. $newList = [
  174. 'banner' => [],
  175. 'lottery' => [],
  176. 'article' => [],
  177. 'measure' => [],
  178. ];
  179. foreach ($list as $item) {
  180. if ($item['type'] == '首页BANNER') {
  181. $bannerList[] = $item;
  182. }
  183. if ($item['type'] == '首页LOTTERY') {
  184. $lotteryList[] = $item;
  185. }
  186. if ($item['type'] == '首页ARTICE') {
  187. $articleList[] = $item;
  188. }
  189. if ($item['type'] == '首页MEASURE') {
  190. $measureList[] = $item;
  191. }
  192. }
  193. $newList['banner'] = $bannerList;
  194. $newList['lottery'] = $lotteryList;
  195. $newList['article'] = $articleList;
  196. $newList['measure'] = $measureList;
  197. // todo 查询活动商品
  198. // 首页推荐的分类
  199. $indexCategoryList = Category::where('status', StatusEnum::NORMAL)
  200. ->field('id,name,image')
  201. ->where("FIND_IN_SET('index',`flag`)")
  202. ->order('weigh desc')
  203. ->limit(8)
  204. ->select();
  205. //热门分类
  206. $indexHotCategoryList = Category::where('status', StatusEnum::NORMAL)
  207. ->field('id,name,image')
  208. ->where("FIND_IN_SET('hot',`flag`)")
  209. ->order('weigh desc')
  210. ->limit(4)
  211. ->select();
  212. $newList['indexCategoryList'] = $indexCategoryList;
  213. $newList['indexHotCategoryList'] = $indexHotCategoryList;
  214. $this->success('', $newList);
  215. }
  216. /**
  217. * 上传文件
  218. * @ApiMethod (POST)
  219. * @ApiParams (name="file", type="file", required=true, description="文件流")
  220. */
  221. public function upload()
  222. {
  223. Config::set('default_return_type', 'json');
  224. //必须设定cdnurl为空,否则cdnurl函数计算错误
  225. Config::set('upload.cdnurl', '');
  226. $chunkid = $this->request->post("chunkid");
  227. if ($chunkid) {
  228. if (!Config::get('upload.chunking')) {
  229. $this->error(__('Chunk file disabled'));
  230. }
  231. $action = $this->request->post("action");
  232. $chunkindex = $this->request->post("chunkindex/d");
  233. $chunkcount = $this->request->post("chunkcount/d");
  234. $filename = $this->request->post("filename");
  235. $method = $this->request->method(true);
  236. if ($action == 'merge') {
  237. $attachment = null;
  238. //合并分片文件
  239. try {
  240. $upload = new Upload();
  241. $attachment = $upload->merge($chunkid, $chunkcount, $filename);
  242. } catch (UploadException $e) {
  243. $this->error($e->getMessage());
  244. }
  245. $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  246. } elseif ($method == 'clean') {
  247. //删除冗余的分片文件
  248. try {
  249. $upload = new Upload();
  250. $upload->clean($chunkid);
  251. } catch (UploadException $e) {
  252. $this->error($e->getMessage());
  253. }
  254. $this->success();
  255. } else {
  256. //上传分片文件
  257. //默认普通上传文件
  258. $file = $this->request->file('file');
  259. try {
  260. $upload = new Upload($file);
  261. $upload->chunk($chunkid, $chunkindex, $chunkcount);
  262. } catch (UploadException $e) {
  263. $this->error($e->getMessage());
  264. }
  265. $this->success();
  266. }
  267. } else {
  268. $attachment = null;
  269. //默认普通上传文件
  270. $file = $this->request->file('file');
  271. try {
  272. $upload = new Upload($file);
  273. $attachment = $upload->upload();
  274. } catch (UploadException $e) {
  275. $this->error($e->getMessage());
  276. } catch (\Exception $e) {
  277. $this->error($e->getMessage());
  278. }
  279. $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  280. }
  281. }
  282. /**
  283. * 验证码
  284. * @ApiParams (name="id", type="string", required=true, description="要生成验证码的标识")
  285. * @return \think\Response
  286. */
  287. public function captcha($id = "")
  288. {
  289. \think\Config::set([
  290. 'captcha' => array_merge(config('captcha'), [
  291. 'fontSize' => 44,
  292. 'imageH' => 150,
  293. 'imageW' => 350,
  294. ])
  295. ]);
  296. $captcha = new Captcha((array)Config::get('captcha'));
  297. return $captcha->entry($id);
  298. }
  299. }