123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- namespace app\admin\controller;
- use Throwable;
- use ba\Terminal;
- use think\Response;
- use think\facade\Db;
- use think\facade\Cache;
- use think\facade\Event;
- use app\admin\model\AdminLog;
- use app\common\library\Upload;
- use app\common\controller\Backend;
- class Ajax extends Backend
- {
- protected array $noNeedPermission = ['*'];
- /**
- * 无需登录的方法
- * terminal 内部自带验权
- */
- protected array $noNeedLogin = ['terminal'];
- public function initialize(): void
- {
- parent::initialize();
- }
- public function upload(): void
- {
- AdminLog::setTitle(__('upload'));
- $file = $this->request->file('file');
- try {
- $upload = new Upload($file);
- $attachment = $upload->upload(null, $this->auth->id);
- unset($attachment['create_time'], $attachment['quote']);
- } catch (Throwable $e) {
- $this->error($e->getMessage());
- }
- $this->success(__('File uploaded successfully'), [
- 'file' => $attachment ?? []
- ]);
- }
- //保存电子签名
- public function saveSign(): void
- {
- if ($this->request->isPost()) {
- $data = $this->request->post();
- if (!$data) {
- $this->error(__('Parameter %s can not be empty', ['']));
- }
- $id = $this->auth->id;
- $result = false;
- if (isset($data['sign']) && $data['sign']) {
- $result = Db::name('sign')->where('uid',$id)->find();
- if($result){
- $result['sign']= $data['sign'];
- if (Db::name('sign')->update($result)) {
- $this->success(__('Avatar modified successfully!'));
- }
- }else{
- $result = Db::name('sign')->insert(['uid'=> $id , 'sign'=> $data['sign']]);
- if ($result) {
- $this->success(__('Avatar modified successfully!'));
- }
- }
- }
- if ($result == false){
- $this->error('更新失败!');
- }
- }
- }
- //保存电子签名
- public function getSign(): void
- {
- $id = $this->auth->id;
- $result = Db::query('SELECT * FROM yx_borrow WHERE user_id = ? ORDER BY id DESC LIMIT 1', [$id]);
- $this->success('签名获取成功!', [
- 'sign' => $result ?? []
- ]);
- }
- /**
- * 获取省市区数据
- * @throws Throwable
- */
- public function area(): void
- {
- $this->success('', get_area());
- }
- public function buildSuffixSvg(): Response
- {
- $suffix = $this->request->param('suffix', 'file');
- $background = $this->request->param('background');
- $content = build_suffix_svg((string)$suffix, (string)$background);
- return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/svg+xml');
- }
- /**
- * 获取表主键字段
- * @param ?string $table
- * @throws Throwable
- */
- public function getTablePk(?string $table = null): void
- {
- if (!$table) {
- $this->error(__('Parameter error'));
- }
- $tablePk = Db::query("SHOW TABLE STATUS LIKE '$table'", [], true);
- if (!$tablePk) {
- $table = config('database.connections.mysql.prefix') . $table;
- $tablePk = Db::query("SHOW TABLE STATUS LIKE '$table'", [], true);
- if (!$tablePk) {
- $this->error(__('Data table does not exist'));
- }
- }
- $tablePk = Db::table($table)->getPk();
- $this->success('', ['pk' => $tablePk]);
- }
- public function getTableFieldList(): void
- {
- $table = $this->request->param('table');
- $clean = $this->request->param('clean', true);
- if (!$table) {
- $this->error(__('Parameter error'));
- }
- $tablePk = Db::name($table)->getPk();
- $this->success('', [
- 'pk' => $tablePk,
- 'fieldList' => get_table_fields($table, $clean),
- ]);
- }
- public function changeTerminalConfig(): void
- {
- AdminLog::setTitle(__('changeTerminalConfig'));
- if (Terminal::changeTerminalConfig()) {
- $this->success();
- } else {
- $this->error(__('Failed to modify the terminal configuration. Please modify the configuration file manually:%s', ['/config/buildadmin.php']));
- }
- }
- public function clearCache(): void
- {
- $type = $this->request->post('type');
- if ($type == 'tp' || $type == 'all') {
- Cache::clear();
- } else {
- $this->error(__('Parameter error'));
- }
- Event::trigger('cacheClearAfter', $this->app);
- $this->success(__('Cache cleaned~'));
- }
- /**
- * 终端
- * @throws Throwable
- */
- public function terminal(): void
- {
- Terminal::instance()->exec();
- }
- }
|