Common.php 13 KB

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