<?php
|
|
namespace app\api\controller;
|
|
use app\common\controller\Api;
|
use app\common\exception\UploadException;
|
use app\common\library\Upload;
|
use think\Config;
|
use app\api\logic\Banner as LogicBanner;
|
use app\api\logic\Message as LogicMessage;
|
use app\api\model\Version as ModelVersion;
|
use app\common\model\China as ModelChina;
|
use app\api\model\HelpType as ModelHelpType;
|
use app\api\model\HelpTypeContent as ModelHelpTypeContent;
|
use fast\Http;
|
use think\Cache;
|
/**
|
* 公共接口
|
*/
|
class Common extends Api
|
{
|
protected $noNeedLogin = ['init','get_banner','get_message','appversion','get_city','coordinate','get_city_id','get_help_type','get_help_content','get_help_detail','get_top_mess_id'];
|
protected $noNeedRight = '*';
|
|
/**
|
* 加载初始化
|
*
|
* @param string $version 版本号
|
* @param string $lng 经度
|
* @param string $lat 纬度
|
*/
|
public function init()
|
{
|
$inviteImgs = config('site.inviteImgs');
|
$inviteImgsArr = [];
|
if(!empty($inviteImgs)){
|
foreach($inviteImgs as $item){
|
$inviteImgsArr[] = [
|
"img" => cdnurl($item)
|
];
|
}
|
}
|
$content = [
|
'basic' => [
|
"iosExamine"=> (boolean)config('site.iosExamine'),
|
"IosExamineV" => config('site.IosExamineV'),
|
"appName"=> config('site.name'),
|
"logo"=> cdnurl(config('site.logo')),
|
'isAppletJump' => config('site.isAppletJump') == 0 ? false : true,
|
'appAndroidDownUrl' => config('site.appAndroidDownUrl'),
|
'appIosDownUrl' => config('site.appIosDownUrl'),
|
'inviteImgs' => $inviteImgsArr,
|
'wxNumber' => config('site.wxNumber'),
|
'wxImg' => cdnurl(config('site.wxImg')),
|
'userAgreement' => config('site.userAgreement'),
|
'privacyAgreement' => config('site.privacyAgreement'),
|
'isOpenOfficialCode' => config('site.isOpenOfficialCode'),
|
'officialInvitationCode' => config('site.invite_code'),
|
'tbEmpower' => config('site.tbEmpower'),
|
'appForce' => config('site.agent_mode') == 1 ? 1 : 0,
|
'invite_url' => config('site.invite_url')
|
]
|
];
|
$this->success('', $content);
|
}
|
|
/**
|
* 获取最新一条消息ID
|
*
|
* @return void
|
*/
|
public function get_top_mess_id()
|
{
|
$id = LogicMessage::getMessageTop();
|
$this->success('获取成功',['id'=>empty($id) ? "" : $id]);
|
}
|
|
/**
|
* 上传文件
|
* @ApiMethod (POST)
|
* @param File $file 文件流
|
*/
|
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());
|
}
|
$this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => request()->domain().$attachment->url]);
|
// $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
|
}
|
|
}
|
|
|
/**
|
* 获取轮播图列表
|
*/
|
public function get_banner()
|
{
|
$type = request()->param('type');
|
if($type == "") $this->error('位置类型错误');
|
$LogicBanner = new LogicBanner();
|
$list = $LogicBanner->getList($type);
|
$this->success('获取成功',['info'=>$list]);
|
}
|
|
|
/**
|
* 获取系统消息
|
*/
|
public function get_message()
|
{
|
$offset = request()->param('offset',0);
|
$limit = request()->param('limit',20);
|
$LogicMessage = new LogicMessage();
|
$list = $LogicMessage->getList($offset,$limit);
|
$this->success('获取成功',['list'=>$list]);
|
}
|
|
|
/**
|
* 获取当前最新一条版本信息
|
*
|
* @return void
|
*/
|
public function appversion()
|
{
|
$equipmentType = request()->param('equipmentType');
|
|
$ModelVersion = new ModelVersion();
|
|
$info = $ModelVersion->where(['equipment_type'=>$equipmentType])->order('id desc')->find();
|
|
$this->success('获取成功',['info'=>empty($info) ? [] : $info]);
|
|
}
|
|
|
/**
|
* 获取城市信息
|
*/
|
public function get_city()
|
{
|
$key = "city_list";
|
$list_json = Cache::get($key);
|
if(empty($list_json)){
|
$ModelChina = new ModelChina();
|
$list = $ModelChina->field('mid as `value`,district as `label`')->where(['pid'=>0])->select();
|
if(empty($list)) return [];
|
foreach($list as &$item){
|
$child = $ModelChina->field('mid as `value`,district as `label`')->where(['pid'=>$item['value']])->select();
|
$item['children'] = empty($child) ? [] : $child;
|
}
|
$list_json = json_encode($list);
|
Cache::set($key,$list_json,86400*3);
|
}
|
$list = json_decode($list_json,true);
|
$this->success('获取成功',['list'=>empty($list) ? [] : $list]);
|
|
}
|
|
|
/**
|
* 根据城市名返回对应经纬度
|
*/
|
public function coordinate()
|
{
|
$city = request()->param('city');
|
|
if(empty($city)) $this->error('查询城市不能为空');
|
|
$key = config('site.gaode_key');
|
$url = "https://restapi.amap.com/v3/geocode/geo?address={$city}&output=JSON&key={$key}";
|
$Http = new Http();
|
|
$result = $Http->get($url);
|
|
if(!$result) $this->error('获取失败');
|
|
$result = json_decode($result, true);
|
|
if($result['status'] != 1) $this->error('获取失败');
|
|
$info = isset($result['geocodes'][0]['location']) ? $result['geocodes'][0]['location'] : [];
|
|
if(empty($info)) $this->error('位置信息获取失败');
|
$location = explode(',',$info);
|
|
$data['latitude'] = $location[1];
|
|
$data['longitude'] = $location[0];
|
|
$this->success('获取成功',['info'=>$data]);
|
}
|
|
|
/**
|
* 根据名称获取城市ID
|
*/
|
public function get_city_id()
|
{
|
$name = $this->request->post("name",'','trim');
|
if(strstr($name,'市')) $name = str_replace('市','',$name);
|
$ModelChina = new ModelChina();
|
$info = $ModelChina->field('mid as `value`,district as `label`')->where(['district'=>$name])->find();
|
$this->success('获取成功',['info'=>empty($info) ? [] : $info]);
|
}
|
|
/**
|
* 获取新手教程分类
|
*
|
* @return void
|
*/
|
public function get_help_type()
|
{
|
$ModelHelpType = new ModelHelpType();
|
$list = $ModelHelpType->field('id,title')->where(['status'=>1])->select();
|
$this->success('获取成功',['list'=>empty($list) ? [] : $list]);
|
}
|
|
|
/**
|
* 获取内容
|
*
|
* @return void
|
*/
|
public function get_help_content()
|
{
|
$type = request()->param('type','');
|
if(empty($type)) $this->error('参数错误');
|
$page = request()->param('page',1);
|
$pageSize = request()->param('pageSize',10);
|
$ModelHelpTypeContent = new ModelHelpTypeContent();
|
$list = $ModelHelpTypeContent->field('id,title,type_id,image,view_num,create_time')->where(['type_id'=>$type])->order('sort desc')->page($page,$pageSize)->select();
|
$this->success('获取成功',['list'=>empty($list) ? [] : $list]);
|
}
|
|
/**
|
* 获取帮助详情
|
*
|
* @return void
|
*/
|
public function get_help_detail()
|
{
|
$id = request()->param('id','');
|
if(empty($id)) $this->error('参数错误');
|
$ModelHelpTypeContent = new ModelHelpTypeContent();
|
$info = $ModelHelpTypeContent->field('id,title,type_id,image,content,view_num,create_time')->where(['id'=>$id])->find();
|
if(!empty($info)){
|
$ModelHelpTypeContent->where(['id'=>$id])->setInc('view_num',1);
|
}
|
$this->success('获取成功',['info' => $info]);
|
}
|
|
|
}
|