123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- <?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\Category;
- use app\common\model\Selection;
- use app\common\model\Version;
- use fast\Random;
- use think\Config;
- use think\Hook;
- class Common extends Api
- {
- protected $noNeedLogin = ['init','selection','category','selection','system_configuration'];
- protected $noNeedRight = '*';
-
- 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 upload()
- {
- Config::set('default_return_type', 'json');
-
- 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' => cdnurl($attachment->url, true)]);
- }
- }
-
- public function system_configuration(){
- $this->success('请求成功',config('site.'.input('site')));
- }
-
- public function category(){
- $this->success('请求成功',Category::where('type',input('type'))->select());
- }
-
-
- function getOrderId()
- {
- return date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
- }
-
- function getOpenid($code_ = ''){
- $code = $code_;
- $appid = 'wx5e111b790c719cae';
- $secret = 'a4e2a6d2a4fd06bd9e4ba279fd95e2d8';
- $get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
- $ch = curl_init();
- curl_setopt($ch,CURLOPT_URL,$get_token_url);
- curl_setopt($ch,CURLOPT_HEADER,0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
- $res = curl_exec($ch);
- curl_close($ch);
- $json_obj = json_decode($res,true);
- if($json_obj['openid'])return $json_obj['openid'];
- $this->error('0',$json_obj);
- }
-
- public function selection(){
- $this->success('',Selection::all(['type'=>input('type'),'status'=>1]));
- }
-
- public function getAllArea()
- {
- $field = ['id', 'pid', 'shortname', 'name', 'first'];
- $list = Area::with(['children' => function ($query) use ($field) {
- $query->field($field)->with(['children' => function ($query) use ($field) {
- $query->field($field);
- }]);
- }])
- ->where('pid', 0)->field($field)->select();
- $this->success('获取成功', $list);
- }
-
- public function get_area_city()
- {
- $field = ['id', 'pid', 'shortname', 'name', 'first'];
- $list = Area::with(['children' => function ($query) use ($field) {
- $query->field($field);
- }])
- ->where('pid', 0)->field($field)->select();
- $this->success('获取成功', $list);
- }
-
- public function get_area()
- {
- $pid = $this->request->get('pid', 0);
- $field = ['id', 'pid', 'shortname', 'name'];
- $list = Area::where('pid', $pid)->field($field)->select();
- $this->success('获取成功', $list);
- }
- }
|