文件

313 行
10 KiB
PHP
可执行文件

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<?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\Db;
use think\Hook;
/**
* 公共接口
*/
class Common extends Api
{
protected $noNeedLogin = ['init', 'captcha', 'areas', 'expressCompanies', 'dicts'];
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="版本号")
*/
public function init()
{
if ($version = $this->request->request('version')) {
//配置信息
$upload = Config::get('upload');
$uploaddata = [];
$uploaddata['cdnurl'] = $upload['cdnurl'] ?: cdnurl('', true);
$uploaddata['uploadurl'] = url('/api/common/upload', '', false, true);
$content = [
'versiondata' => Version::check($version),
'uploaddata' => $uploaddata,
];
$this->success('', $content);
} else {
$this->error(__('Invalid parameters'));
}
}
/**
* 上传文件
* @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);
}
/**
* 行政区划真实数据库查询接口(源自 fa_area)
* - mode=tree: 返回完整省-市-区三级联树
* - pid: 按父级ID逐级查询下级行政区
*/
public function areas()
{
$mode = trim((string)$this->request->param('mode', ''));
if ($mode === 'tree') {
$rows = Db::name('area')
->field('id,pid,name,shortname,level')
->where('level', 'in', [1, 2, 3])
->order('id asc')
->select();
if (empty($rows)) {
$this->error('行政区划基础数据尚未初始化');
}
$provinces = [];
$citiesByPid = [];
$districtsByPid = [];
foreach ($rows as $row) {
$lvl = (int)$row['level'];
$pid = (int)$row['pid'];
$id = (int)$row['id'];
if ($lvl === 1) {
$provinces[] = [
'id' => $id,
'name' => (string)$row['name'],
'cities' => [],
];
} elseif ($lvl === 2) {
if (!isset($citiesByPid[$pid])) {
$citiesByPid[$pid] = [];
}
$citiesByPid[$pid][] = [
'id' => $id,
'name' => (string)$row['name'],
'districts' => [],
];
} elseif ($lvl === 3) {
if (!isset($districtsByPid[$pid])) {
$districtsByPid[$pid] = [];
}
$districtsByPid[$pid][] = (string)$row['name'];
}
}
foreach ($provinces as &$prov) {
$cities = isset($citiesByPid[$prov['id']]) ? $citiesByPid[$prov['id']] : [];
foreach ($cities as &$city) {
$city['districts'] = isset($districtsByPid[$city['id']]) ? $districtsByPid[$city['id']] : [];
}
unset($city);
$prov['cities'] = $cities;
}
unset($prov);
$this->success('', ['tree' => $provinces]);
}
$pid = (int)$this->request->param('pid', 0);
$list = Db::name('area')
->field('id,pid,name,shortname,mergename,level')
->where('pid', $pid)
->order('id asc')
->select();
$this->success('', ['list' => $list]);
}
/**
* 物流公司真实字典(源自 fa_express_company,按首字母分组)
*/
public function expressCompanies()
{
$rows = Db::name('express_company')
->field('id,code,name,initial')
->where('status', 'normal')
->order('initial asc, weigh desc, id asc')
->select();
$groupMap = [];
foreach ($rows as $r) {
$letter = strtoupper(trim((string)$r['initial'])) ?: 'Q';
if (!isset($groupMap[$letter])) {
$groupMap[$letter] = [];
}
$groupMap[$letter][] = (string)$r['name'];
}
$groups = [];
foreach ($groupMap as $letter => $items) {
$groups[] = [
'letter' => $letter,
'items' => $items,
];
}
// 将“Q 其它”组调整到末尾
usort($groups, function ($a, $b) {
$isQa = ($a['letter'] === 'Q');
$isQb = ($b['letter'] === 'Q');
if ($isQa !== $isQb) {
return $isQa ? 1 : -1;
}
return strcmp($a['letter'], $b['letter']);
});
$this->success('', [
'groups' => $groups,
'list' => $rows,
]);
}
/**
* 平台通用业务字典接口(真实表与系统配置驱动)
*/
public function dicts()
{
$parseCsv = static function ($name) {
$val = Db::name('config')->where('name', $name)->value('value');
if ($val === null || trim((string)$val) === '') {
$val = Config::get('site.' . $name);
}
if (!$val) {
return [];
}
return array_values(array_filter(array_map('trim', explode(',', (string)$val))));
};
$reviewTags = $parseCsv('review_tags');
$orderSources = $parseCsv('order_source_options');
$rawQuoteHours = $parseCsv('auto_quote_hours_options');
$autoQuoteHours = [];
foreach ($rawQuoteHours as $h) {
$hi = (int)$h;
if ($hi > 0) {
$autoQuoteHours[] = $hi;
}
}
// 师傅资质要求列表直接读取真实证书字典表 fa_cert_dict
$certRows = Db::name('cert_dict')
->field('id,cert_code,cert_name,sample_type')
->where('status', 'normal')
->where('cert_code', '<>', 'other')
->order('weigh desc, id asc')
->select();
$qualReqs = [];
foreach ($certRows as $cr) {
if (!empty($cr['cert_name'])) {
$qualReqs[] = (string)$cr['cert_name'];
}
}
$this->success('', [
'review_tags' => $reviewTags,
'order_sources' => $orderSources,
'auto_quote_hours' => $autoQuoteHours,
'qual_reqs' => $qualReqs,
'cert_dict' => $certRows,
]);
}
}