123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\common\exception\UploadException;
- use app\common\library\Upload;
- use app\common\model\Area;
- use app\common\model\Version;
- use fast\Random;
- use think\captcha\Captcha;
- use think\Config;
- use think\Hook;
- use app\common\library\Theme;
- use app\common\model\Navigation;
- use app\common\model\Block;
- use app\common\model\Category;
- use app\common\model\SearchLog;
- use app\common\Enum\StatusEnum;
- use app\common\Enum\FeedbackEnum;
- use app\common\Enum\UserEnum;
- /**
- * 公共接口
- */
- class Common extends Api
- {
- protected $noNeedLogin = ['init', 'area', 'getBannerList','getDataByIndex'];
- protected $noNeedRight = '*';
- public function _initialize()
- {
- if (isset($_SERVER['HTTP_ORIGIN'])) {
- header('Access-Control-Expose-Headers: __token__');//跨域让客户端获取到
- }
- //跨域检测
- check_cors_request();
- if (!isset($_COOKIE['PHPSESSID'])) {
- Config::set('session.id', $this->request->server("HTTP_SID"));
- }
- parent::_initialize();
- }
- /**
- * 加载初始化
- *
- * @ApiParams (name="version", type="string", required=true, description="版本号")
- * @ApiParams (name="lng", type="string", required=true, description="经度")
- * @ApiParams (name="lat", type="string", required=true, description="纬度")
- */
- // public function init()
- // {
- // if ($version = $this->request->request('version')) {
- // $lng = $this->request->request('lng');
- // $lat = $this->request->request('lat');
- // //配置信息
- // $upload = Config::get('upload');
- // //如果非服务端中转模式需要修改为中转
- // if ($upload['storage'] != 'local' && isset($upload['uploadmode']) && $upload['uploadmode'] != 'server') {
- // //临时修改上传模式为服务端中转
- // set_addon_config($upload['storage'], ["uploadmode" => "server"], false);
- // $upload = \app\common\model\Config::upload();
- // // 上传信息配置后
- // Hook::listen("upload_config_init", $upload);
- // $upload = Config::set('upload', array_merge(Config::get('upload'), $upload));
- // }
- // $upload['cdnurl'] = $upload['cdnurl'] ? $upload['cdnurl'] : cdnurl('', true);
- // $upload['uploadurl'] = preg_match("/^((?:[a-z]+:)?\/\/)(.*)/i", $upload['uploadurl']) ? $upload['uploadurl'] : url($upload['storage'] == 'local' ? '/api/common/upload' : $upload['uploadurl'], '', false, true);
- // $content = [
- // 'citydata' => Area::getCityFromLngLat($lng, $lat),
- // 'versiondata' => Version::check($version),
- // 'uploaddata' => $upload,
- // 'coverdata' => Config::get("cover"),
- // ];
- // $this->success('', $content);
- // } else {
- // $this->error(__('Invalid parameters'));
- // }
- // }
- /**
- * 初始化
- */
- public function init()
- {
- //配置信息
- $upload = Config::get('upload');
- //如果非服务端中转模式需要修改为中转
- if ($upload['storage'] != 'local' && isset($upload['uploadmode']) && $upload['uploadmode'] != 'server') {
- //临时修改上传模式为服务端中转
- set_addon_config($upload['storage'], ["uploadmode" => "server"], false);
- $upload = \app\common\model\Config::upload();
- // 上传信息配置后
- Hook::listen("upload_config_init", $upload);
- $upload = Config::set('upload', array_merge(Config::get('upload'), $upload));
- }
- $upload['cdnurl'] = $upload['cdnurl'] ? $upload['cdnurl'] : cdnurl('', true);
- //上传地址强制切换为使用本地上传,云存储插件会自动处理
- $upload['uploadurl'] = url('/api/common/upload', '', false, true);
- //支付列表和默认支付方式
- $paytypearr = array_filter(explode(',', Config::get('shop.paytypelist')));
- $defaultPaytype = Config::get('shop.defaultpaytype');
- $defaultPaytype = in_array($defaultPaytype, $paytypearr) ? $defaultPaytype : reset($paytypearr);
- //登录类型列表
- $logintypearr = array_filter(explode(',', Config::get('shop.logintypelist')));
- $config = [
- 'upload' => $upload,
- //登录类型列表
- 'logintypearr' => $logintypearr,
- 'paytypelist' => implode(',', $logintypearr),
- 'defaultpaytype' => $defaultPaytype,
- '__token__' => $this->request->token()
- ];
- $config['order_timeout'] = Config::get('shop.order_timeout');
- $config['sitename'] = Config::get('shop.sitename');
- // 公告
- $config['notice'] = Config::get('shop.notice');
- // 手机号
- $config['phone'] = Config::get('shop.phone');
- // 工作时间
- $config['workinghours'] = Config::get('shop.workinghours');
- // 客服邮箱
- $config['email'] = Config::get('shop.email');
- // 公司名称
- $config['company'] = Config::get('shop.company');
- // 站点logo
- $config['sitelogo'] = cdnurl(Config::get('shop.sitelogo'), true);
- // 公安备案
- $config['gwabeian'] = Config::get('shop.gwabeian');
- // icp备案
- $config['icpbeian'] = Config::get('shop.icpbeian');
- // 物流类型
- $config['logisticstype'] = Config::get('shop.logisticstype');
- // 分类模式
- $config['category_mode'] = (int)Config::get('shop.category_mode');
- // 积分
- $config['money_score'] = Config::get('shop.money_score');
- // 评论积分
- $config['comment_score'] = Config::get('shop.comment_score');
- // 默认商品图片
- $config['default_goods_img'] = cdnurl(Config::get('shop.default_goods_img'), true);
- // 默认分类图片
- $config['default_category_img'] = cdnurl(Config::get('shop.default_category_img'), true);
- // 热门搜索关键词
- $config['hot_keyword'] = SearchLog::order('nums desc')->limit(10)->column('keywords');
- // $config['navigate'] = Navigation::tableList();
- // $config['brands'] = \addons\shop\model\Brand::field('id,name')->order('weigh desc')->select();
- //消息订阅模板id
- $config['tpl_ids'] = \addons\shop\model\TemplateMsg::getTplIds();
- // 反馈类型
- $config['feedback_type'] = FeedbackEnum::getFeedbackTypeMap();
- // 性别
- $config['gender'] = UserEnum::getGenderMap();
- //合并主题样式,判断是否预览模式
- $isPreview = stripos($this->request->SERVER("HTTP_REFERER"), "mode=preview") !== false;
- $themeConfig = $isPreview && \think\Session::get("previewtheme-shop") ? \think\Session::get("previewtheme-shop") : Theme::get();
- $themeConfig = Theme::render($themeConfig);
- $data = array_merge($config, $themeConfig);
- $this->success('', $data);
- }
- /**
- * 读取省市区数据,联动列表
- */
- public function area()
- {
- $province = $this->request->param('province', '');
- $city = $this->request->param('city', '');
- $where = ['pid' => 0, 'level' => 1];
- $provincelist = null;
- if ($province !== '') {
- $where['pid'] = $province;
- $where['level'] = 2;
- }
- if ($city !== '') {
- $where['pid'] = $city;
- $where['level'] = 3;
- }
- $provincelist = Area::where($where)->field('id as value,name as label')->where('status', 'normal')->select();
- $this->success('', $provincelist);
- }
- public function getBannerList()
- {
- $type = $this->request->param('type');
- // 验证参数
- if (!$type) {
- $this->error('参数错误');
- }
- $list = Block::getBlockList(['type' => $type]);
- $this->success('', $list);
- }
- public function getDataByIndex()
- {
- // 固定
- $type = ['首页BANNER', '首页LOTTERY','首页ARTICE','首页MEASURE'];
- $field = 'id,type,name,image,url,status,begintime,endtime,title';
- $list = Block::getBlockList(['type' => $type, 'field' => $field]);
- // 根据type 区分新的数组
- $newList = [
- 'banner' => [],
- 'lottery' => [],
- 'article' => [],
- 'measure' => [],
- ];
- foreach ($list as $item) {
- if ($item['type'] == '首页BANNER') {
- $bannerList[] = $item;
- }
- if ($item['type'] == '首页LOTTERY') {
- $lotteryList[] = $item;
- }
- if ($item['type'] == '首页ARTICE') {
- $articleList[] = $item;
- }
- if ($item['type'] == '首页MEASURE') {
- $measureList[] = $item;
- }
- }
- $newList['banner'] = $bannerList;
- $newList['lottery'] = $lotteryList;
- $newList['article'] = $articleList;
- $newList['measure'] = $measureList;
- // todo 查询活动商品
- // 首页推荐的分类
- $indexCategoryList = Category::where('status', StatusEnum::NORMAL)
- ->field('id,name,image')
- ->where("FIND_IN_SET('index',`flag`)")
- ->order('weigh desc')
- ->limit(8)
- ->select();
- //热门分类
- $indexHotCategoryList = Category::where('status', StatusEnum::NORMAL)
- ->field('id,name,image')
- ->where("FIND_IN_SET('hot',`flag`)")
- ->order('weigh desc')
- ->limit(4)
- ->select();
- //查询 活动商品
- $newList['indexCategoryList'] = $indexCategoryList;
- $newList['indexHotCategoryList'] = $indexHotCategoryList;
- $this->success('', $newList);
- }
- /**
- * 上传文件
- * @ApiMethod (POST)
- * @ApiParams (name="file", type="file", required=true, description="文件流")
- */
- public function upload()
- {
- Config::set('default_return_type', 'json');
- //必须设定cdnurl为空,否则cdnurl函数计算错误
- Config::set('upload.cdnurl', '');
- $chunkid = $this->request->post("chunkid");
- if ($chunkid) {
- if (!Config::get('upload.chunking')) {
- $this->error(__('Chunk file disabled'));
- }
- $action = $this->request->post("action");
- $chunkindex = $this->request->post("chunkindex/d");
- $chunkcount = $this->request->post("chunkcount/d");
- $filename = $this->request->post("filename");
- $method = $this->request->method(true);
- if ($action == 'merge') {
- $attachment = null;
- //合并分片文件
- try {
- $upload = new Upload();
- $attachment = $upload->merge($chunkid, $chunkcount, $filename);
- } catch (UploadException $e) {
- $this->error($e->getMessage());
- }
- $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
- } elseif ($method == 'clean') {
- //删除冗余的分片文件
- try {
- $upload = new Upload();
- $upload->clean($chunkid);
- } catch (UploadException $e) {
- $this->error($e->getMessage());
- }
- $this->success();
- } else {
- //上传分片文件
- //默认普通上传文件
- $file = $this->request->file('file');
- try {
- $upload = new Upload($file);
- $upload->chunk($chunkid, $chunkindex, $chunkcount);
- } catch (UploadException $e) {
- $this->error($e->getMessage());
- }
- $this->success();
- }
- } else {
- $attachment = null;
- //默认普通上传文件
- $file = $this->request->file('file');
- try {
- $upload = new Upload($file);
- $attachment = $upload->upload();
- } catch (UploadException $e) {
- $this->error($e->getMessage());
- } catch (\Exception $e) {
- $this->error($e->getMessage());
- }
- $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
- }
- }
- /**
- * 验证码
- * @ApiParams (name="id", type="string", required=true, description="要生成验证码的标识")
- * @return \think\Response
- */
- public function captcha($id = "")
- {
- \think\Config::set([
- 'captcha' => array_merge(config('captcha'), [
- 'fontSize' => 44,
- 'imageH' => 150,
- 'imageW' => 350,
- ])
- ]);
- $captcha = new Captcha((array)Config::get('captcha'));
- return $captcha->entry($id);
- }
- }
|