zhaojs
2023-06-01 802997e673d862ba81c6908a6ab05d9282de596e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?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]);
    }
 
  
}