Common.php 11 KB

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