SettingController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\BaseAttachmentModel;
  4. use App\Models\ClassModel;
  5. use App\Models\Setting;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Storage;
  8. use Intervention\Image\Facades\Image;
  9. class SettingController extends Controller
  10. {
  11. protected $redirect_index = '/admin/Setting/index';
  12. protected $view_path = 'admin.settings.';
  13. protected $pre_uri = '/admin/Setting/';
  14. protected $model_name = '系统配置';
  15. protected $model;
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. $this->model = new Setting();
  20. }
  21. public function share()
  22. {
  23. $share_image = $this->model->firstOrCreate(['key' => 'share_image'], ['key_show' => '图片']);
  24. $share_text = $this->model->firstOrCreate(['key' => 'share_text'], ['key_show' => '文本标签']);
  25. $share_text_pos = $this->model->firstOrCreate(['key' => 'share_text_pos'], ['key_show' => '文本位置']);
  26. if(!empty($share_image) && !empty($share_image->value) && Storage::disk('upload')->exists($share_image->value)) {
  27. $image = Image::make(url($share_image->value));
  28. $width = $image->width();
  29. $height = $image->height();
  30. } else {
  31. $width = 0;
  32. $height = 0;
  33. }
  34. list($pre_uri, $model, $model_name) = array($this->pre_uri, $this->model, $this->model_name);
  35. return view($this->view_path . 'share', compact('pre_uri', 'model', 'model_name', 'share_image', 'share_text', 'share_text_pos', 'width', 'height'));
  36. }
  37. public function updateShare(Request $request)
  38. {
  39. if(!$request->isMethod('POST')) {
  40. return $this->showWarning('访问错误');
  41. }
  42. if(!empty($request->input('share_image'))) {
  43. $this->model->where('key', 'share_image')->update(['value' => $request->input('share_image')]);
  44. }
  45. if(!empty($request->input('share_text'))) {
  46. $this->model->where('key', 'share_text')->update(['value' => $request->input('share_text')]);
  47. }
  48. if(!empty($request->input('share_text_pos'))) {
  49. $this->model->where('key', 'share_text_pos')->update(['value' => $request->input('share_text_pos')]);
  50. }
  51. return $this->showMessage('操作成功');
  52. }
  53. public function system(Request $request)
  54. {
  55. $check_card_location = $this->model->firstOrCreate(['key' => 'check_card_location'], ['value' => '39.916527,116.397128']);
  56. $check_card_radius = $this->model->firstOrCreate(['key' => 'check_card_radius'], ['value' => '1000']);
  57. $check_position = $this->model->firstOrCreate(['key' => 'check_position'], ['value' => '1']);
  58. list($pre_uri, $model, $model_name) = array($this->pre_uri, $this->model, $this->model_name);
  59. return view($this->view_path . 'system', compact('pre_uri', 'model', 'model_name', 'check_card_location', 'check_card_radius', 'check_position'));
  60. }
  61. public function updateSystem(Request $request)
  62. {
  63. if(!$request->isMethod('POST')) {
  64. return $this->showWarning('访问错误');
  65. }
  66. dd($request->all());
  67. $items = collect(['check_card_location', 'check_card_radius', 'check_position']);
  68. foreach($items as $item) {
  69. if(!empty($request->input($item))) {
  70. $this->model->where('key', $item)->update(['value' => $request->input($item)]);
  71. }
  72. }
  73. return $this->showMessage('操作成功');
  74. }
  75. public function generateSharePhoto(Request $request)
  76. {
  77. $share_image = $this->model->where('key', 'share_image')->first();
  78. if(empty($share_image) || empty($share_image->value) || !Storage::disk('upload')->exists($share_image->value)) {
  79. return $this->showWarning('分享图片错误或未设置');
  80. }
  81. $share_text = $this->model->where('key', 'share_text')->first();
  82. if(empty($share_text) || empty($share_text->value)) {
  83. return $this->showWarning('分享文字错误或未设置');
  84. }
  85. $share_text_pos = $this->model->where('key', 'share_text_pos')->first();
  86. if(empty($share_text_pos) || empty($share_text_pos->value) || count($pos = explode(',', $share_text_pos->value)) < 2) {
  87. return $this->showWarning('分享文字位置错误或未设置');
  88. }
  89. $text = str_replace_array('{param}', [1000], $share_text->value);
  90. $image = Image::make(url($share_image->value));
  91. if($pos[0] > $image->width() || $pos[1] > $image->height()) {
  92. return $this->showWarning('分享文字位置错误');
  93. }
  94. $image = $image->text($text, $pos[0], $pos[1], function ($font) {
  95. $font->file('fonts/simsun.ttc');
  96. $font->size(18);
  97. $font->align('center');
  98. $font->color('#ffffff');
  99. });
  100. $size = $image->filesize();
  101. $name = uniqid();
  102. $dir = 'tmp/share/' . date("Ymd");
  103. $path = 'tmp/share/' . date("Ymd") . '/' . $name . '.png';
  104. $full_path = '/' . $path;
  105. if(!\File::isDirectory($dir)) {
  106. \File::makeDirectory($dir, 493, true);
  107. }
  108. $items = BaseAttachmentModel::where('class', '分享图片测试')->get();
  109. foreach($items as $item) {
  110. if(Storage::disk('upload')->exists($item->path)) {
  111. Storage::disk('upload')->delete($item->path);
  112. }
  113. $item->delete();
  114. }
  115. $image->save($path);
  116. BaseAttachmentModel::create([
  117. 'path' => $full_path,
  118. 'url' => $full_path,
  119. 'name' => $name,
  120. 'class' => '分享图片测试',
  121. 'size' => $size,
  122. ]);
  123. ClassModel::firstOrCreate(['class' => '分享图片测试']);
  124. return view($this->view_path . 'share-show', compact('full_path'));
  125. }
  126. }