261 lines
9.0 KiB
PHP
261 lines
9.0 KiB
PHP
<?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\Config;
|
|
use think\Hook;
|
|
use think\Lang;
|
|
use think\Loader;
|
|
|
|
/**
|
|
* 公共接口
|
|
*/
|
|
class Common extends Api
|
|
{
|
|
protected $noNeedLogin = ['init','lang','agreenment'];
|
|
protected $noNeedRight = '*';
|
|
|
|
/**
|
|
* 加载初始化
|
|
*
|
|
* @param string $version 版本号
|
|
* @param string $lng 经度
|
|
* @param string $lat 纬度
|
|
*/
|
|
public function init()
|
|
{
|
|
global $_W;
|
|
//配置信息
|
|
$data = \app\common\model\config\System::getConfig('system');
|
|
|
|
$wxOfficialAccountConfig = \app\common\model\config\System::getConfig('wxOfficialAccount');
|
|
$data['wx_login'] = $wxOfficialAccountConfig['wx_login'];
|
|
$data['app_id'] = $wxOfficialAccountConfig['app_id'];
|
|
|
|
$wxMiniProgramConfig = \app\common\model\config\System::getConfig('wxMiniProgram');
|
|
$data['wx_mp_login'] = $wxMiniProgramConfig['wx_mp_login'];
|
|
$data['wx_mp_ban_pay'] = $wxMiniProgramConfig['ban_pay'];
|
|
$data['wx_mp_app_id'] = $wxMiniProgramConfig['app_id'];
|
|
|
|
if(!isset($wxMiniProgramConfig['ban_pc_use'])){
|
|
$wxMiniProgramConfig['ban_pc_use'] = 'close';
|
|
}
|
|
|
|
if(!isset($wxMiniProgramConfig['ban_screen_record'])){
|
|
$wxMiniProgramConfig['ban_screen_record'] = 'close';
|
|
}
|
|
|
|
|
|
$data['wx_mp_ban_screen_record'] = $wxMiniProgramConfig['ban_screen_record'];
|
|
$data['wx_mp_ban_pc_use'] = $wxMiniProgramConfig['ban_pc_use'];
|
|
|
|
$dyMiniProgramConfig = \app\common\model\config\System::getConfig('dyMiniProgram');
|
|
$data['dy_mp_login'] = $dyMiniProgramConfig['dy_login'];
|
|
$data['dy_mp_appid'] = $dyMiniProgramConfig['appid'];
|
|
$data['dy_mp_ban_pay'] = $dyMiniProgramConfig['ban_pay'];
|
|
|
|
global $_W;
|
|
$data['apiUrl'] = $_W['siteroot'].'/index.php?i='.$_W['uniacid'].'&route=api/';
|
|
|
|
$data['logo'] = cdnurl($data['logo']);
|
|
|
|
$data['app_isinstall_test'] = \app\admin\library\project\App::isInstall('test');
|
|
$data['app_isinstall_channels'] = \app\admin\library\project\App::isInstall('channels');
|
|
$data['app_isinstall_cert'] = \app\admin\library\project\App::isInstall('cert');
|
|
$data['app_isinstall_form'] = \app\admin\library\project\App::isInstall('form');
|
|
$data['app_isinstall_composite'] = \app\admin\library\project\App::isInstall('composite');
|
|
$data['app_isinstall_live_gift'] = \app\admin\library\project\App::isInstall('live_gift') && class_exists('app\admin\model\live\GiftOptions');
|
|
|
|
$data['marquee'] = \app\common\model\app\Config::getConfig('marquee');
|
|
|
|
// 投诉功能开关
|
|
$complaintConfig = \app\common\model\app\Config::getConfig('complaint');
|
|
$data['app_complaint'] = is_array($complaintConfig) && isset($complaintConfig['status']) ? (string)$complaintConfig['status'] : '1';
|
|
|
|
if(!isset($data['mobile_login'])){
|
|
$data['mobile_login'] = 'open';
|
|
}
|
|
|
|
if (\app\common\library\Platform::getPlatform() === 'wxMiniProgram') {
|
|
$virtualPayConfig = \app\common\model\config\System::getConfig('virtual_pay');
|
|
$data['coin_name'] = $virtualPayConfig['coin_name'] ?? '金币';
|
|
}
|
|
|
|
$this->success('', $data);
|
|
}
|
|
|
|
|
|
/**
|
|
* 用户协议
|
|
* @return void
|
|
*/
|
|
public function agreenment(){
|
|
$type = input('type');
|
|
|
|
$data = \app\common\model\config\System::getConfig('agreement');
|
|
|
|
if(!isset($data[$type])){
|
|
$this->error('未获取到相关内容');
|
|
}
|
|
$this->success('', $data[$type]);
|
|
}
|
|
|
|
/**
|
|
* 上传文件
|
|
* @ApiMethod (POST)
|
|
* @param File $file 文件流
|
|
*/
|
|
public function upload()
|
|
{
|
|
Config::set('default_return_type', 'json');
|
|
//必须设定cdnurl为空,否则cdnurl函数计算错误
|
|
if(\app\common\library\Platform::getSystemType() == 'single'){
|
|
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, 'full_url' => 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());
|
|
}
|
|
|
|
global $_W;
|
|
if(\app\common\library\Platform::getSystemType() == 'single'){
|
|
return $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'full_url' => cdnurl($attachment->url,true)]);
|
|
}else{
|
|
return $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'full_url' => cdnurl($attachment->url)]);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 加载语言包
|
|
*/
|
|
public function lang()
|
|
{
|
|
$header = ['Content-Type' => 'application/javascript'];
|
|
$header = [];
|
|
if (!config('app_debug')) {
|
|
$offset = 30 * 60 * 60 * 24; // 缓存一个月
|
|
$header['Cache-Control'] = 'public';
|
|
$header['Pragma'] = 'cache';
|
|
$header['Expires'] = gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
|
|
}
|
|
|
|
$controllername = input("controllername");
|
|
//默认只加载了控制器对应的语言名,你还根据控制器名来加载额外的语言包
|
|
$this->loadlang($controllername);
|
|
|
|
$allLangFile = $this->getAllLangFile(APP_PATH . $this->request->module() . '/lang/zh-cn/');
|
|
|
|
foreach ($allLangFile as $file){
|
|
$this->loadlang($file);
|
|
}
|
|
|
|
return $this->success("获取成功",Lang::get());
|
|
}
|
|
|
|
/**
|
|
* 加载语言文件
|
|
* @param string $name
|
|
*/
|
|
protected function loadlang($name)
|
|
{
|
|
$name = Loader::parseName($name);
|
|
$name = preg_match("/^([a-zA-Z0-9_\.\/]+)\$/i", $name) ? $name : 'index';
|
|
$lang = $this->request->langset();
|
|
$lang = preg_match("/^([a-zA-Z\-_]{2,10})\$/i", $lang) ? $lang : 'zh-cn';
|
|
Lang::load(APP_PATH . $this->request->module() . '/lang/' . $lang . '/' . str_replace('.', '/', $name) . '.php');
|
|
}
|
|
|
|
/**
|
|
* 获取全部语言文件名称
|
|
* @param $path
|
|
* @param $dir
|
|
* @return array
|
|
*/
|
|
public function getAllLangFile($path,$dir=[]){
|
|
$files=[];
|
|
$data = scandir($path);
|
|
foreach ($data as $value){
|
|
|
|
$sub_path = $path .'/'.$value;
|
|
if($value == '.'|| $value == '..'){
|
|
continue;
|
|
}
|
|
$mulu = $dir;
|
|
if(is_dir($sub_path)){
|
|
$mulu[] = $value;
|
|
$childFiles = $this->getAllLangFile($sub_path,$mulu);
|
|
if(!empty($childFiles)){
|
|
$files = array_merge($files,$childFiles);
|
|
}
|
|
}else{
|
|
$value = str_replace(".php","",$value);
|
|
$mulu = $dir;
|
|
if($mulu){
|
|
$mulu[] = $value;
|
|
$files[] = implode(".",$mulu);
|
|
}else{
|
|
$files[] = $value;
|
|
}
|
|
}
|
|
|
|
}
|
|
return $files;
|
|
}
|
|
}
|