Common.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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' => $paytypearr,
  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. // 微信授权登录地址
  141. $config['wx_universal_link'] = Config::get('shop.wx_universal_link');
  142. // 微信appid
  143. $config['wx_appid'] = Config::get('shop.wx_appid');
  144. // 微信小程序跳转
  145. //$config['wechat_min_program'] = ;
  146. // $config['navigate'] = Navigation::tableList();
  147. // $config['brands'] = \addons\shop\model\Brand::field('id,name')->order('weigh desc')->select();
  148. //消息订阅模板id
  149. $config['tpl_ids'] = \app\common\model\TemplateMsg::getTplIds();
  150. // 反馈类型
  151. $config['feedback_type'] = FeedbackEnum::getFeedbackTypeMap();
  152. // 性别
  153. $config['gender'] = UserEnum::getGenderMap();
  154. //合并主题样式,判断是否预览模式
  155. $isPreview = stripos($this->request->SERVER("HTTP_REFERER"), "mode=preview") !== false;
  156. $themeConfig = $isPreview && \think\Session::get("previewtheme-shop") ? \think\Session::get("previewtheme-shop") : Theme::get();
  157. $themeConfig = Theme::render($themeConfig);
  158. $data = array_merge($config, $themeConfig);
  159. $this->success('', $data);
  160. }
  161. /**
  162. * 读取省市区数据,联动列表
  163. */
  164. public function area()
  165. {
  166. $province = $this->request->param('province', '');
  167. $city = $this->request->param('city', '');
  168. $where = ['pid' => 0, 'level' => 1];
  169. $provincelist = null;
  170. if ($province !== '') {
  171. $where['pid'] = $province;
  172. $where['level'] = 2;
  173. }
  174. if ($city !== '') {
  175. $where['pid'] = $city;
  176. $where['level'] = 3;
  177. }
  178. $provincelist = Area::where($where)->field('id as value,name as label')->where('status', 'normal')->select();
  179. $this->success('', $provincelist);
  180. }
  181. public function getBannerList()
  182. {
  183. $type = $this->request->param('type');
  184. // 验证参数
  185. if (!$type) {
  186. $this->error('参数错误');
  187. }
  188. $list = Block::getBlockList(['type' => $type]);
  189. $this->success('', $list);
  190. }
  191. public function getDataByIndex()
  192. {
  193. // 固定
  194. $type = ['首页BANNER', '首页LOTTERY','首页ARTICE','首页MEASURE'];
  195. $field = 'id,type,name,image,url,page_name,status,begintime,endtime,title';
  196. $list = Block::getBlockList(['type' => $type, 'field' => $field]);
  197. // 根据type 区分新的数组
  198. $newList = [
  199. 'banner' => [],
  200. 'lottery' => [],
  201. 'article' => [],
  202. 'measure' => [],
  203. ];
  204. foreach ($list as $item) {
  205. if ($item['type'] == '首页BANNER') {
  206. $bannerList[] = $item;
  207. }
  208. if ($item['type'] == '首页LOTTERY') {
  209. $lotteryList[] = $item;
  210. }
  211. if ($item['type'] == '首页ARTICE') {
  212. $articleList[] = $item;
  213. }
  214. if ($item['type'] == '首页MEASURE') {
  215. $measureList[] = $item;
  216. }
  217. }
  218. $newList['banner'] = $bannerList;
  219. $newList['lottery'] = $lotteryList;
  220. $newList['article'] = $articleList;
  221. $newList['measure'] = $measureList;
  222. // todo 查询活动商品
  223. // 首页推荐的分类
  224. $indexCategoryList = Category::where('status', StatusEnum::NORMAL)
  225. ->field('id,name,image')
  226. ->where('is_recommend',StatusEnum::YES)
  227. ->order('weigh desc')
  228. ->limit(8)
  229. ->select();
  230. //热门分类
  231. $indexHotCategoryList = Category::where('status', StatusEnum::NORMAL)
  232. ->field('id,name,image')
  233. ->where('is_hot',StatusEnum::YES)
  234. ->order('weigh desc')
  235. ->limit(4)
  236. ->select();
  237. //查询 活动商品
  238. $newList['indexCategoryList'] = $indexCategoryList;
  239. $newList['indexHotCategoryList'] = $indexHotCategoryList;
  240. $this->success('', $newList);
  241. }
  242. /**
  243. * 上传文件
  244. * @ApiMethod (POST)
  245. * @ApiParams (name="file", type="file", required=true, description="文件流")
  246. */
  247. public function upload()
  248. {
  249. Config::set('default_return_type', 'json');
  250. //必须设定cdnurl为空,否则cdnurl函数计算错误
  251. Config::set('upload.cdnurl', '');
  252. $chunkid = $this->request->post("chunkid");
  253. if ($chunkid) {
  254. if (!Config::get('upload.chunking')) {
  255. $this->error(__('Chunk file disabled'));
  256. }
  257. $action = $this->request->post("action");
  258. $chunkindex = $this->request->post("chunkindex/d");
  259. $chunkcount = $this->request->post("chunkcount/d");
  260. $filename = $this->request->post("filename");
  261. $method = $this->request->method(true);
  262. if ($action == 'merge') {
  263. $attachment = null;
  264. //合并分片文件
  265. try {
  266. $upload = new Upload();
  267. $attachment = $upload->merge($chunkid, $chunkcount, $filename);
  268. } catch (UploadException $e) {
  269. $this->error($e->getMessage());
  270. }
  271. $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  272. } elseif ($method == 'clean') {
  273. //删除冗余的分片文件
  274. try {
  275. $upload = new Upload();
  276. $upload->clean($chunkid);
  277. } catch (UploadException $e) {
  278. $this->error($e->getMessage());
  279. }
  280. $this->success();
  281. } else {
  282. //上传分片文件
  283. //默认普通上传文件
  284. $file = $this->request->file('file');
  285. try {
  286. $upload = new Upload($file);
  287. $upload->chunk($chunkid, $chunkindex, $chunkcount);
  288. } catch (UploadException $e) {
  289. $this->error($e->getMessage());
  290. }
  291. $this->success();
  292. }
  293. } else {
  294. $attachment = null;
  295. //默认普通上传文件
  296. $file = $this->request->file('file');
  297. try {
  298. $upload = new Upload($file);
  299. $attachment = $upload->upload();
  300. } catch (UploadException $e) {
  301. $this->error($e->getMessage());
  302. } catch (\Exception $e) {
  303. $this->error($e->getMessage());
  304. }
  305. $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  306. }
  307. }
  308. /**
  309. * 验证码
  310. * @ApiParams (name="id", type="string", required=true, description="要生成验证码的标识")
  311. * @return \think\Response
  312. */
  313. public function captcha($id = "")
  314. {
  315. \think\Config::set([
  316. 'captcha' => array_merge(config('captcha'), [
  317. 'fontSize' => 44,
  318. 'imageH' => 150,
  319. 'imageW' => 350,
  320. ])
  321. ]);
  322. $captcha = new Captcha((array)Config::get('captcha'));
  323. return $captcha->entry($id);
  324. }
  325. }