Przeglądaj źródła

Weenier 168otc项目部署 0630

wesmiler 3 lat temu
rodzic
commit
ee88132e0e

+ 17 - 0
app/Helpers/function.php

@@ -204,3 +204,20 @@ if (!function_exists('showJson')) {
         return json_encode($result, 256);
     }
 }
+
+
+if(!function_exists('getCode')){
+    /**
+     * 生成唯一字符串
+     * @param string $prefix 长度最小8位
+     * @param int $length 长度最小9位,越小唯一性越差,9位百万唯一
+     * @return false|string
+     */
+    function getCode($prefix='',$length=11){
+        $length = $length<9? 9 : $length;
+        $code = strtoupper(md5(uniqid(mt_rand(), true).time()));
+        $start = rand(1, strlen($code)-$length);
+        return $prefix.mb_substr($code, $start, $length);
+    }
+
+}

+ 51 - 0
app/Http/Controllers/Admin/ApiController.php

@@ -0,0 +1,51 @@
+<?php
+// +----------------------------------------------------------------------
+// | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
+// +----------------------------------------------------------------------
+// | 版权所有 2017~2021 LARAVEL研发中心
+// +----------------------------------------------------------------------
+// | 官方网站: http://www.laravel.cn
+// +----------------------------------------------------------------------
+// | Author: laravel开发员 <laravel.qq.com>
+// +----------------------------------------------------------------------
+
+namespace App\Http\Controllers\Admin;
+
+use App\Http\Validator\ApiValidator;
+use App\Services\Common\ApiService;
+use App\Services\Common\NoticeService;
+
+/**
+ * 通知公告-控制器
+ * Class ApiController
+ * @package App\Http\Controllers
+ */
+class ApiController extends Backend
+{
+    /**
+     * 构造函数
+     * ApiController constructor.
+     */
+    public function __construct()
+    {
+        parent::__construct();
+        $this->service = new ApiService();
+    }
+
+    /**
+     * 编辑或新增
+     * @return array|mixed
+     */
+    public function edit()
+    {
+        $validate = new ApiValidator();
+        $params = request()->post();
+        $params = $validate->check($params,'buy');
+        if(!is_array($params)){
+            return returnJson($params, false,[]);
+        }
+
+        return $this->service->saveData($this->userId, $params);
+    }
+
+}

+ 51 - 0
app/Http/Validator/ApiValidator.php

@@ -0,0 +1,51 @@
+<?php
+namespace App\Http\Validator;
+class ApiValidator extends BaseValidator
+{
+    // 当前模型所有验证规则
+    public static $rules = [
+        'id' => 'required',
+        'cost_type' => 'required',
+        'username' => 'required|username',
+        'account' => 'required',
+        'expired_at' => 'required',
+
+    ];
+
+    // 当前模型所有错误提示信息
+    public static $msgs = [
+        'required' => ':attribute不能为空',
+        'string' => ':attribute必须是字符串',
+        'min' => ':attribute长度不能小于:min位',
+        'max' => ':attribute长度不能大于:max位',
+        'exists' => ':attribute不存在',
+        'rule' => ':attribute格式不正确',
+        'username' => ':attribute格式不正确',
+    ];
+
+    // 当前模型所有验证字段
+    public static $fields = [
+        'id' => 'ID',
+        'username' => '账号',
+        'account' => '企业/个人名称',
+        'cost_type' => '套餐类型',
+        'expired_at' => '有效期',
+    ];
+
+    // 当前模型所有验证场景
+    public static $scenes = [
+        'info'=> ['id'],
+        'edit'=> ['cost_type','username','account','expired_at'],
+    ];
+
+    /**
+     * 验证
+     * @param $request
+     * @param string $scene
+     * @return int|mixed
+     */
+    public static function check($request, $scene=''){
+        $validator = new BaseValidator(self::$rules, self::$msgs, self::$fields, self::$scenes);
+        return $validator->checkParams($request, $scene);
+    }
+}

+ 46 - 0
app/Models/ApiModel.php

@@ -0,0 +1,46 @@
+<?php
+// +----------------------------------------------------------------------
+// | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
+// +----------------------------------------------------------------------
+// | 版权所有 2017~2021 LARAVEL研发中心
+// +----------------------------------------------------------------------
+// | 官方网站: http://www.laravel.cn
+// +----------------------------------------------------------------------
+// | Author: laravel开发员 <laravel.qq.com>
+// +----------------------------------------------------------------------
+
+namespace App\Models;
+
+/**
+ * 广告管理-模型
+ * Class ApiModel
+ * @package App\Models
+ */
+class ApiModel extends BaseModel
+{
+    // 设置数据表
+    protected $table = 'apis';
+
+    /**
+     * 获取广告信息
+     * @param int $id
+     * @return array|string
+     */
+    public function getInfo($id)
+    {
+        $info = parent::getInfo($id); // TODO: Change the autogenerated stub
+        if ($info) {
+            // 权限
+            if ($info['user_limits']) {
+                $info['user_limits'] = explode(',', $info['user_limits']);
+            }
+
+            // 有效期
+            if ($info['expired_at']) {
+                $info['expired_at'] = $info['expired_at'] > date('Y-m-d H:i:s')? $info['expired_at'] : '';
+            }
+        }
+        return $info;
+    }
+
+}

+ 71 - 0
app/Services/Common/ApiService.php

@@ -0,0 +1,71 @@
+<?php
+// +----------------------------------------------------------------------
+// | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
+// +----------------------------------------------------------------------
+// | 版权所有 2017~2021 LARAVEL研发中心
+// +----------------------------------------------------------------------
+// | 官方网站: http://www.laravel.cn
+// +----------------------------------------------------------------------
+// | Author: laravel开发员 <laravel.qq.com>
+// +----------------------------------------------------------------------
+
+namespace App\Services\Common;
+
+use App\Models\AdModel;
+use App\Models\ApiModel;
+use App\Services\BaseService;
+
+/**
+ * 接口管理-服务类
+ * Class ApiService
+ * @package App\Services\Common
+ */
+class ApiService extends BaseService
+{
+    // 静态对象
+    protected static $instance = null;
+
+
+    /**
+     * 构造函数
+     * ApiService constructor.
+     */
+    public function __construct()
+    {
+        $this->model = new ApiModel();
+    }
+
+    /**
+     * 静态入口
+     * @return static|null
+     */
+    public static function make()
+    {
+        if (!self::$instance) {
+            self::$instance = (new static());
+        }
+        return self::$instance;
+    }
+
+    /**
+     * 添加或编辑
+     * @return array
+     */
+    public function saveData($adminId, $data)
+    {
+        $data['admin_id'] = $adminId;
+        // 权限
+        if ($data['user_limits']) {
+            $data['user_limits'] = array_filter($data['user_limits']);
+            $data['user_limits'] = $data['user_limits']? implode(',', $data['user_limits']) : '';
+        }
+        // 结束时间
+        $id = isset($data['id'])? $data['id'] : 0;
+        if (!$id) {
+            $data['api_key'] = getCode('Ti', 12);
+        }
+
+        return parent::edit($data); // TODO: Change the autogenerated stub
+    }
+
+}