SettingController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. list($pre_uri, $model, $model_name) = array($this->pre_uri, $this->model, $this->model_name);
  27. return view($this->view_path . 'share', compact('pre_uri', 'model', 'model_name', 'share_image', 'share_text', 'share_text_pos'));
  28. }
  29. public function updateShare(Request $request)
  30. {
  31. if(!$request->isMethod('POST')) {
  32. return $this->showWarning('访问错误');
  33. }
  34. if(!empty($request->input('share_image'))) {
  35. $this->model->where('key', 'share_image')->update(['value' => $request->input('share_image')]);
  36. }
  37. if(!empty($request->input('share_text'))) {
  38. $this->model->where('key', 'share_text')->update(['value' => $request->input('share_text')]);
  39. }
  40. if(!empty($request->input('share_text_pos'))) {
  41. $this->model->where('key', 'share_text_pos')->update(['value' => $request->input('share_text_pos')]);
  42. }
  43. return $this->showMessage('操作成功');
  44. }
  45. public function system(Request $request)
  46. {
  47. $check_card_location = $this->model->firstOrCreate(['key' => 'check_card_location'], ['value' => '39.916527,116.397128']);
  48. $check_card_radius = $this->model->firstOrCreate(['key' => 'check_card_radius'], ['value' => '1000']);
  49. list($pre_uri, $model, $model_name) = array($this->pre_uri, $this->model, $this->model_name);
  50. return view($this->view_path . 'system', compact('pre_uri', 'model', 'model_name', 'check_card_location', 'check_card_radius'));
  51. }
  52. public function updateSystem(Request $request)
  53. {
  54. if(!$request->isMethod('POST')) {
  55. return $this->showWarning('访问错误');
  56. }
  57. $items = collect(['check_card_location', 'check_card_radius']);
  58. foreach($items as $item) {
  59. if(!empty($request->input($item))) {
  60. $this->model->where('key', $item)->update(['value' => $request->input($item)]);
  61. }
  62. }
  63. return $this->showMessage('操作成功');
  64. }
  65. public function generateSharePhoto(Request $request)
  66. {
  67. $share_image = $this->where('key', 'share_image')->first();
  68. if(empty($share_image) || empty($share_image->value) || !Storage::disk('upload')->exists($share_image->value)) {
  69. return $this->showWarning('分享图片错误或未设置');
  70. }
  71. $share_text = $this->where('key', 'share_text')->first();
  72. if(empty($share_text) || empty($share_text->value)) {
  73. return $this->showWarning('分享文字错误或未设置');
  74. }
  75. $share_text_pos = $this->where('key', 'share_text_pos')->first();
  76. if(empty($share_text_pos) || empty($share_text_pos->value) || count($pos = explode(',', $share_text_pos->value)) < 2) {
  77. return $this->showWarning('分享文字位置错误或未设置');
  78. }
  79. $text = str_replace_array('{days}', [15], $share_text_pos->value);
  80. $image = Image::make($share_image->value)->text($text, $pos[0], $pos[1]);
  81. $size = $image->filesize();
  82. $name = uniqid();
  83. $path = 'tmp/share/' . date("Ymd") . '/' . $name . '.png';
  84. $full_path = '/' . $path;
  85. if(!\File::isDirectory($path)) {
  86. \File::makeDirectory($path, 493, true);
  87. }
  88. $image->save($path);
  89. BaseAttachmentModel::create([
  90. 'path' => $full_path,
  91. 'name' => $name,
  92. 'type' => 'png',
  93. 'class' => '分享图片',
  94. 'size' => $size,
  95. ]);
  96. ClassModel::firstOrCreate(['class' => '分享图片']);
  97. return redirect($path);
  98. }
  99. }