作者 邓超

模板

@@ -5,6 +5,7 @@ namespace App\Http\Controllers\Bside; @@ -5,6 +5,7 @@ namespace App\Http\Controllers\Bside;
5 use App\Enums\Common\Code; 5 use App\Enums\Common\Code;
6 use App\Exceptions\BsideGlobalException; 6 use App\Exceptions\BsideGlobalException;
7 use App\Models\Template\ATemplate; 7 use App\Models\Template\ATemplate;
  8 +use App\Models\Template\BSetting;
8 use App\Models\Template\BTemplate; 9 use App\Models\Template\BTemplate;
9 use Illuminate\Support\Facades\DB; 10 use Illuminate\Support\Facades\DB;
10 11
@@ -20,7 +21,7 @@ class TemplateController extends BaseController @@ -20,7 +21,7 @@ class TemplateController extends BaseController
20 21
21 22
22 /** 23 /**
23 - * 列表 24 + * 模板列表
24 * @return \Illuminate\Http\JsonResponse 25 * @return \Illuminate\Http\JsonResponse
25 * @author:dc 26 * @author:dc
26 * @time 2023/5/9 14:20 27 * @time 2023/5/9 14:20
@@ -30,7 +31,7 @@ class TemplateController extends BaseController @@ -30,7 +31,7 @@ class TemplateController extends BaseController
30 $limit = intval($this->param['limit']??20); 31 $limit = intval($this->param['limit']??20);
31 32
32 33
33 - $data = ATemplate::_bAll($limit); 34 + $data = ATemplate::_bAll($limit)->toArray();
34 35
35 36
36 37
@@ -39,6 +40,23 @@ class TemplateController extends BaseController @@ -39,6 +40,23 @@ class TemplateController extends BaseController
39 } 40 }
40 41
41 42
  43 + /**
  44 + * 当前使用的模板
  45 + * @author:dc
  46 + * @time 2023/5/9 15:19
  47 + */
  48 + public function info(){
  49 + $conf = BSetting::_get($this->user['project_id']);
  50 +
  51 + $data = ATemplate::_bFind($conf['template_id']);
  52 +
  53 + $this->success([
  54 + 'template_id' => $data['id']??0,
  55 + 'name' => $data['name']??'',
  56 + 'thumb' => $data['thumb']??'',
  57 + 'time' => $conf['time']
  58 + ]);
  59 + }
42 60
43 61
44 62
@@ -53,6 +53,32 @@ class ATemplate extends \App\Models\Base{ @@ -53,6 +53,32 @@ class ATemplate extends \App\Models\Base{
53 ->paginate($limit); 53 ->paginate($limit);
54 } 54 }
55 55
  56 + /**
  57 + * @param $id
  58 + * @return array
  59 + * @author:dc
  60 + * @time 2023/5/9 15:16
  61 + */
  62 + public static function _bFind($id)
  63 + {
  64 + $data = static::where('id',$id)->first();
  65 + if(!$data || $data->status === static::STATUS_DISABLED){
  66 + return [];
  67 + }
  68 + return $data;
  69 + }
  70 +
  71 +
  72 + /**
  73 + * 获取默认模板
  74 + * @return mixed
  75 + * @author:dc
  76 + * @time 2023/5/9 15:09
  77 + */
  78 + public static function _default()
  79 + {
  80 + return static::where(['status'=>static::STATUS_ACTIVE,'is_default'=>1])->first();
  81 + }
56 82
57 83
58 84
  1 +<?php
  2 +
  3 +namespace App\Models\Template;
  4 +
  5 +use Illuminate\Database\Eloquent\SoftDeletes;
  6 +
  7 +/**
  8 + * 当前用户的模板
  9 + * @author:dc
  10 + * @time 2023/5/9 15:03
  11 + * Class BSetting
  12 + * @package App\Models\Template
  13 + */
  14 +class BSetting extends \App\Models\Base{
  15 +
  16 +
  17 +
  18 + protected $table = 'gl_web_setting_template';
  19 +
  20 +
  21 +
  22 + /**
  23 + * b 端调用
  24 + * @param int $limit
  25 + * @return mixed
  26 + * @author:dc
  27 + * @time 2023/5/9 14:14
  28 + */
  29 + public static function _get(int $project_id)
  30 + {
  31 + $data = static::where('project_id',$project_id)->first();
  32 +
  33 + if($data){
  34 + return [
  35 + 'template_id' => $data['template_id'],
  36 + 'time' => $data['updated_at']
  37 + ];
  38 + }
  39 +
  40 + // 米有数据
  41 + // 读取默认的模板
  42 + $temp = ATemplate::_default();
  43 + // 保存
  44 + self::_save($project_id,$temp['id']);
  45 +
  46 + return [
  47 + 'template_id' => $temp['id'],
  48 + 'time' => date('Y-m-d H:i:s')
  49 + ];
  50 + }
  51 +
  52 +
  53 + /**
  54 + * 模板保存
  55 + * @param int $project_id
  56 + * @param int $template_id
  57 + * @return mixed
  58 + * @author:dc
  59 + * @time 2023/5/9 15:13
  60 + */
  61 + public static function _save(int $project_id, int $template_id)
  62 + {
  63 +
  64 + $data = static::where('project_id',$project_id)->first();
  65 + if(!$data){
  66 + $data = new static();
  67 + $data->project_id = $project_id;
  68 + }
  69 +
  70 + $data->template_id = $template_id;
  71 +
  72 + $data->save();
  73 +
  74 + return $data->id;
  75 +
  76 + }
  77 +
  78 +
  79 +
  80 +
  81 +}