zory 2 долоо хоног өмнө
parent
commit
a5f47ae6ab

+ 66 - 0
app/controller/admin/Config.php

@@ -0,0 +1,66 @@
+<?php
+
+namespace app\controller\admin;
+
+use app\extra\basic\Base;
+use app\middleware\AuthMiddleware;
+use app\model\system\SystemConfig;
+use LinFly\Annotation\Route\Controller;
+use LinFly\Annotation\Route\Route;
+use support\Request;
+use support\Response;
+use Webman\Annotation\Middleware;
+
+
+#[Controller(prefix: "/api/config"),Middleware(AuthMiddleware::class)]
+class Config extends Base
+{
+
+
+    /**
+     * 获取配置信息
+     */
+    #[Route(path: "list",methods: "get")]
+    public function getConfigList(Request $request): Response
+    {
+        try {
+            $type = $request->get("type","service");
+            $data = (new SystemConfig)->where("type",$type)->where("status",1)->field("name,value")->select()->toArray();
+            $result = [];
+            foreach ($data as $item) {
+                $result[$item['name']] = $item['value'];
+            }
+            if ($type == "sms") {
+                $result['channel'] = $this->getSmsChannel();
+                $result['login_sms'] = '您的验证码为:${code},请勿泄露于他人!';
+            }
+            return successTrans("success.data",$result);
+        } catch (\Throwable $throwable) {
+            return error($throwable->getMessage());
+        }
+    }
+
+
+    /**
+     * 保存通用配置
+     * @param Request $request
+     * @return Response
+     */
+    #[Route(path: "save",methods: "post")]
+    public function setConfigData(Request $request): Response
+    {
+        try {
+            $param = $request->post();
+            if (isset($param['data']['channel'])) unset($param['data']['channel']);
+            foreach ($param['data'] as $k => $v){
+                if(is_array($v)) $v = implode(",",$v);
+                sConf($param['type'].'.'.$k, $v);
+            }
+            return successTrans("success.data",[]);
+        } catch (\Throwable $exception){
+            return error($exception->getMessage());
+        }
+
+    }
+
+}

+ 41 - 0
app/controller/admin/Upload.php

@@ -0,0 +1,41 @@
+<?php
+
+namespace app\controller\admin;
+
+use app\extra\basic\Base;
+use app\extra\tools\UploadExtend;
+use app\middleware\AuthMiddleware;
+use LinFly\Annotation\Route\Controller;
+use LinFly\Annotation\Route\Middleware;
+use LinFly\Annotation\Route\Route;
+use support\Request;
+use support\Response;
+
+
+#[Controller(prefix: "/api/upload"),Middleware(AuthMiddleware::class)]
+class Upload extends Base
+{
+
+
+    /**
+     * 上传文件
+     * @param Request $request
+     * @return Response
+     */
+    #[Route(path: "data",methods: "post")]
+    public function upload2image(Request $request): Response
+    {
+        try {
+            $resp = UploadExtend::uploadFile();
+            if (!isset($resp[0]['url'])) return errorTrans(40010);
+            return successTrans("success.data",[
+                "fileName"  => $resp[0]['origin_name'],
+                "src"       => $resp[0]['url'],
+            ],200);
+        } catch (\Throwable $throwable) {
+            return error($throwable->getMessage());
+        }
+
+    }
+
+}

+ 13 - 0
app/extra/service/basic/KeyService.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace app\extra\service\basic;
+
+class KeyService
+{
+
+    public static function smsKey(int $mobile,string $scene = "login"): string
+    {
+        return "sms_{$mobile}_{$scene}";
+    }
+
+}

+ 53 - 0
app/extra/service/basic/SmsService.php

@@ -0,0 +1,53 @@
+<?php
+
+namespace app\extra\service\basic;
+
+use app\extra\basic\Service;
+use app\model\system\SystemUser;
+use Hzdad\Codecheck\Codecheck;
+use Overtrue\EasySms\EasySms;
+use support\think\Cache;
+
+class SmsService extends Service
+{
+
+    /**
+     * 发送短信
+     * @param int $mobile
+     * @param string $scene
+     * @param bool $empty true 验证手机号是否注册
+     * @return array
+     */
+    public function sendSceneSms(int $mobile,string $scene = "login",bool $empty = false): array
+    {
+        try {
+            $mobileUser =(new SystemUser)->where("mobile",$mobile)->findOrEmpty();
+            if (!$empty && $mobileUser->isEmpty()) return [0,trans("error.mobile-empty")];
+            if ($empty && !$mobileUser->isEmpty()) return [0,trans("error.mobile-exist")];
+            $cacheCode = Cache::get(KeyService::smsKey($mobile,$scene));
+            if (!empty($cacheCode)) return [0,trans("error.sms-repeat")];
+            $code = (new Codecheck)->mobile($mobile)->scene($scene)->create();
+            if (!in_array($mobile,$this->mobileWhite)) {
+                $smsConfig = $this->smsConfig();
+                $resp = (new EasySms($smsConfig['config']))->send($mobile,[
+                    "template"  => $smsConfig['template'],
+                    "data"      => [
+                        "code"  => $code
+                    ]
+                ]);
+                if (!isset($resp[$smsConfig['config']['default']['gateways']]['status'])) return [0,trans("error.sms")];
+                $msg = trans("success.sms",['%mobile%' => hide_mobile($mobile)]);
+            } else {
+                $msg = "当前号码为测试号码,验证码为{$code}";
+            }
+            Cache::set(KeyService::smsKey($mobile,$scene),$code,60);
+            return [1,$msg];
+        } catch (\Throwable $throwable) {
+            echo $throwable->getMessage()."\n";
+            echo $throwable->getFile()."\n";
+            echo $throwable->getLine()."\n";
+            return [0,$throwable->getMessage()];
+        }
+    }
+
+}

+ 97 - 0
app/extra/service/basic/UploadService.php

@@ -0,0 +1,97 @@
+<?php
+
+namespace app\extra\service\basic;
+
+use app\model\system\SystemConfig;
+use Tinywan\Storage\Adapter\CosAdapter;
+use Tinywan\Storage\Adapter\LocalAdapter;
+use Tinywan\Storage\Adapter\QiniuAdapter;
+
+class UploadService
+{
+    /**
+     * 配置
+     * @return array
+     */
+    public function setConfigVal(): array
+    {
+        $data = (new SystemConfig)->where("type","storage")->column("value","name");
+        $config = [];
+        $config['storage']['default'] = $data['type'];
+        $config['storage']['include'] = !empty($data['allow_exts']) ? explode(",",$data['allow_exts']) : [];
+        $config['storage']['exclude'] = !empty($data['noallow_exts']) ? explode(",",$data['noallow_exts']) : [];
+        $config['storage']['nums'] = 10;
+        $config['storage']['single_limit'] = 1024 * 1024 * 200;
+        $config['storage']['total_limit'] = 1024 * 1024 * 200;
+        switch ($data['type'])
+        {
+            case "local":
+                $config['storage']['local'] = [
+                    'adapter' => LocalAdapter::class,
+                    'root' => public_path().'/uploads/storage/',
+                    'dirname' => function () {
+                        return date('Ymd');
+                    },
+                    'domain' => '//'.request()->host(),
+                    'uri' => '/uploads/storage/', // 如果 domain + uri 不在 public 目录下,请做好软链接,否则生成的url无法访问
+                    'algo' => 'sha1',
+                ];
+                break;
+            case "qiniu":
+                $config['storage']['qiniu'] = [
+                    'adapter' => QiniuAdapter::class,
+                    'accessKey' => $data['qiniu_access_key'],
+                    'secretKey' => $data['qiniu_secret_key'],
+                    'bucket' => $data['qiniu_bucket'],
+                    'dirname' => 'storage',
+                    'domain' => $this->setDomain($data['oss_http_protocol'],$data['qiniu_http_domain']),
+                ];
+                break;
+            case "oss":
+                $config['storage']['oss'] = [
+                    'adapter' => \Tinywan\Storage\Adapter\OssAdapter::class,
+                    'accessKeyId' => $data['oss_access_key'],
+                    'accessKeySecret' => $data['oss_secret_key'],
+                    'bucket' => $data['oss_bucket'],
+                    'dirname' => function () {
+                        return 'storage';
+                    },
+                    'domain' => $this->setDomain($data['oss_http_protocol'],$data['oss_http_domain']),
+                    'endpoint' => $data['oss_region'],
+                    'algo' => 'sha1',
+
+                ];
+                break;
+            case "cos":
+                $config['storage']['cos'] = [
+                    'adapter' => CosAdapter::class,
+                    'secretId' => $data['cos_access_key'],
+                    'secretKey' => $data['cos_secret_key'],
+                    'bucket' => $data['cos_bucket'],
+                    'dirname' => 'storage',
+                    'domain' => $this->setDomain($data['oss_http_protocol'],$data['cos_http_domain']),
+                    'region' => $data['cos_region'],
+                ];
+                break;
+            default:
+                break;
+        }
+        return $config;
+    }
+
+    /**
+     * 协议域名
+     * @param string $type
+     * @param string $domain
+     * @return string
+     */
+    protected function setDomain(string $type,string $domain): string
+    {
+        if ($type == "auto") {
+            return "//".$domain;
+        }
+        return $type."://".$domain;
+    }
+
+
+}