SettingController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. list($pre_uri, $model, $model_name) = array($this->pre_uri, $this->model, $this->model_name);
  58. return view($this->view_path . 'system', compact('pre_uri', 'model', 'model_name', 'check_card_location', 'check_card_radius'));
  59. }
  60. public function updateSystem(Request $request)
  61. {
  62. if(!$request->isMethod('POST')) {
  63. return $this->showWarning('访问错误');
  64. }
  65. $items = collect(['check_card_location', 'check_card_radius']);
  66. foreach($items as $item) {
  67. if(!empty($request->input($item))) {
  68. $this->model->where('key', $item)->update(['value' => $request->input($item)]);
  69. }
  70. }
  71. return $this->showMessage('操作成功');
  72. }
  73. public function generateSharePhoto(Request $request)
  74. {
  75. $share_image = $this->model->where('key', 'share_image')->first();
  76. if(empty($share_image) || empty($share_image->value) || !Storage::disk('upload')->exists($share_image->value)) {
  77. return $this->showWarning('分享图片错误或未设置');
  78. }
  79. $share_text = $this->model->where('key', 'share_text')->first();
  80. if(empty($share_text) || empty($share_text->value)) {
  81. return $this->showWarning('分享文字错误或未设置');
  82. }
  83. $share_text_pos = $this->model->where('key', 'share_text_pos')->first();
  84. if(empty($share_text_pos) || empty($share_text_pos->value) || count($pos = explode(',', $share_text_pos->value)) < 2) {
  85. return $this->showWarning('分享文字位置错误或未设置');
  86. }
  87. $text = str_replace_array('{days}', [15], $share_text->value);
  88. $image = Image::make(url($share_image->value));
  89. if($pos[0] > $image->width() || $pos[1] > $image->height()) {
  90. return $this->showWarning('分享文字位置错误');
  91. }
  92. $image = $image->text($text, $pos[0], $pos[1], function ($font) {
  93. $font->file('fonts/simsun.ttc');
  94. });
  95. $size = $image->filesize();
  96. $name = uniqid();
  97. $dir = 'tmp/share/' . date("Ymd");
  98. $path = 'tmp/share/' . date("Ymd") . '/' . $name . '.png';
  99. $full_path = '/' . $path;
  100. if(!\File::isDirectory($dir)) {
  101. \File::makeDirectory($dir, 493, true);
  102. }
  103. $image->save($path);
  104. BaseAttachmentModel::create([
  105. 'path' => $full_path,
  106. 'name' => $name,
  107. 'class' => '分享图片',
  108. 'size' => $size,
  109. ]);
  110. ClassModel::firstOrCreate(['class' => '分享图片']);
  111. return redirect($path);
  112. }
  113. }