SystemController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Channels\BarkChannel;
  4. use App\Channels\DingTalkChannel;
  5. use App\Channels\iYuuChannel;
  6. use App\Channels\PushDeerChannel;
  7. use App\Channels\PushPlusChannel;
  8. use App\Channels\ServerChanChannel;
  9. use App\Channels\TgChatChannel;
  10. use App\Channels\WeChatChannel;
  11. use App\Http\Controllers\Controller;
  12. use App\Http\Requests\Admin\SystemRequest;
  13. use App\Models\Config;
  14. use App\Models\Country;
  15. use App\Models\GoodsCategory;
  16. use App\Models\Label;
  17. use App\Models\Level;
  18. use App\Models\SsConfig;
  19. use App\Notifications\Custom;
  20. use App\Services\TelegramService;
  21. use App\Utils\DDNS;
  22. use App\Utils\Payments\PaymentManager;
  23. use Illuminate\Contracts\View\View;
  24. use Illuminate\Http\JsonResponse;
  25. use Illuminate\Http\RedirectResponse;
  26. use Illuminate\Http\Request;
  27. use Notification;
  28. use NotificationChannels\Telegram\TelegramChannel;
  29. class SystemController extends Controller
  30. {
  31. public function index(): View
  32. { // 系统设置
  33. function parseTime(string|array $inputs): array
  34. {
  35. $items = is_array($inputs) ? $inputs : json_decode($inputs, true);
  36. $result = [];
  37. foreach ($items as $key => $duration) {
  38. preg_match('/-(\d+)\s+(\w+)/', $duration, $m);
  39. $result[$key] = [
  40. 'num' => $m[1] ?? 1,
  41. 'unit' => $m[2] ?? 'hours',
  42. ];
  43. }
  44. return $result;
  45. }
  46. // 获取所有配置项
  47. $config = Config::pluck('value', 'name')->toArray();
  48. // 预处理复杂数据
  49. // 解析时间类配置
  50. $config['tasks_clean'] = parseTime($config['tasks_clean']);
  51. $config['tasks_close'] = parseTime($config['tasks_close']);
  52. $paymentForms = PaymentManager::getSettingsFormData();
  53. return view('admin.config.system', [
  54. 'configs' => $config,
  55. 'payments' => PaymentManager::getAvailable(),
  56. 'paymentForms' => $paymentForms,
  57. 'paymentTabs' => [...array_keys($paymentForms), 'manual'],
  58. 'paymentLists' => [
  59. 'ali' => PaymentManager::getPaymentsByMethod('ali'),
  60. 'wechat' => PaymentManager::getPaymentsByMethod('wechat'),
  61. 'qq' => PaymentManager::getPaymentsByMethod('qq'),
  62. 'other' => PaymentManager::getPaymentsByMethod('other'),
  63. ],
  64. 'captcha' => $this->getCaptcha(),
  65. 'notifies' => $this->getNotifyChannels(),
  66. 'notifyForms' => $this->getNotifyForms(),
  67. 'ddns_labels' => (new DDNS)->getLabels(),
  68. ]);
  69. }
  70. private function getCaptcha(): bool
  71. {
  72. return sysConfig('captcha_secret') && sysConfig('captcha_key');
  73. }
  74. private function getNotifyChannels(): array
  75. {
  76. $configMap = [ // 支付渠道及其所需配置项映射
  77. 'bark' => ['bark_key'],
  78. 'dingTalk' => ['dingTalk_access_token'],
  79. 'iYuu' => ['iYuu_token'],
  80. 'pushDear' => ['pushDeer_key'],
  81. 'pushPlus' => ['pushplus_token'],
  82. 'serverChan' => ['server_chan_key'],
  83. 'telegram' => ['telegram_token'],
  84. 'tgChat' => ['tg_chat_token'],
  85. 'weChat' => ['wechat_cid', 'wechat_aid', 'wechat_secret', 'wechat_token', 'wechat_encodingAESKey'],
  86. ];
  87. $channels = ['database', 'mail'];
  88. // 遍历映射,检查配置项是否存在
  89. foreach ($configMap as $channel => $configKeys) {
  90. $allConfigsExist = array_reduce($configKeys, static function ($carry, $configKey) {
  91. return $carry && sysConfig($configKey);
  92. }, true);
  93. if ($allConfigsExist) {
  94. $channels[] = $channel;
  95. }
  96. }
  97. return $channels;
  98. }
  99. private function getNotifyForms(): array
  100. {
  101. return [
  102. 'server_chan_key' => ['test' => 'serverChan'],
  103. 'pushDeer_key' => ['test' => 'pushDeer'],
  104. 'iYuu_token' => ['test' => 'iYuu'],
  105. 'bark_key' => ['test' => 'bark'],
  106. 'telegram_token' => ['test' => 'telegram'],
  107. 'pushplus_token' => ['test' => 'pushPlus'],
  108. 'dingTalk_access_token' => null,
  109. 'dingTalk_secret' => ['test' => 'dingTalk'],
  110. 'wechat_cid' => null,
  111. 'wechat_aid' => null,
  112. 'wechat_secret' => ['test' => 'weChat'],
  113. 'wechat_token' => ['url' => route('wechat.verify')],
  114. 'wechat_encodingAESKey' => null,
  115. 'tg_chat_token' => ['test' => 'tgChat'],
  116. ];
  117. }
  118. public function setExtend(Request $request): RedirectResponse // 设置涉及到上传的设置
  119. {
  120. if ($request->hasAny(['website_home_logo', 'website_home_logo'])) { // 首页LOGO
  121. if ($request->hasFile('website_home_logo')) {
  122. $validator = validator()->make($request->all(), ['website_home_logo' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048']);
  123. if ($validator->fails()) {
  124. return redirect()->route('admin.system.index', '#other')->withErrors($validator->errors());
  125. }
  126. $file = $request->file('website_home_logo');
  127. $file->move('uploads/logo', $file->getClientOriginalName());
  128. if (Config::findOrNew('website_home_logo')->update(['value' => 'uploads/logo/'.$file->getClientOriginalName()])) {
  129. return redirect()->route('admin.system.index', '#other')->with('successMsg', trans('common.success_item', ['attribute' => trans('common.update')]));
  130. }
  131. }
  132. if ($request->hasFile('website_logo')) { // 站内LOGO
  133. $validator = validator()->make($request->all(), ['website_logo' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048']);
  134. if ($validator->fails()) {
  135. return redirect()->route('admin.system.index', '#other')->withErrors($validator->errors());
  136. }
  137. $file = $request->file('website_logo');
  138. $file->move('uploads/logo', $file->getClientOriginalName());
  139. if (Config::findOrNew('website_logo')->update(['value' => 'uploads/logo/'.$file->getClientOriginalName()])) {
  140. return redirect()->route('admin.system.index', '#other')->with('successMsg', trans('common.success_item', ['attribute' => trans('common.update')]));
  141. }
  142. }
  143. return redirect()->route('admin.system.index', '#other')->withErrors(trans('common.failed_item', ['attribute' => trans('common.update')]));
  144. }
  145. if ($request->hasAny(['alipay_qrcode', 'wechat_qrcode'])) {
  146. if ($request->hasFile('alipay_qrcode')) {
  147. $validator = validator()->make($request->all(), ['alipay_qrcode' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048']);
  148. if ($validator->fails()) {
  149. return redirect()->route('admin.system.index', '#payment')->withErrors($validator->errors());
  150. }
  151. $file = $request->file('alipay_qrcode');
  152. $file->move('uploads/images', $file->getClientOriginalName());
  153. if (Config::findOrNew('alipay_qrcode')->update(['value' => 'uploads/images/'.$file->getClientOriginalName()])) {
  154. return redirect()->route('admin.system.index', '#payment')->with('successMsg', trans('common.success_item', ['attribute' => trans('common.update')]));
  155. }
  156. }
  157. if ($request->hasFile('wechat_qrcode')) { // 站内LOGO
  158. $validator = validator()->make($request->all(), ['wechat_qrcode' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048']);
  159. if ($validator->fails()) {
  160. return redirect()->route('admin.system.index', '#payment')->withErrors($validator->errors());
  161. }
  162. $file = $request->file('wechat_qrcode');
  163. $file->move('uploads/images', $file->getClientOriginalName());
  164. if (Config::findOrNew('wechat_qrcode')->update(['value' => 'uploads/images/'.$file->getClientOriginalName()])) {
  165. return redirect()->route('admin.system.index', '#payment')->with('successMsg', trans('common.success_item', ['attribute' => trans('common.update')]));
  166. }
  167. }
  168. return redirect()->route('admin.system.index', '#payment')->withErrors(trans('common.failed_item', ['attribute' => trans('common.update')]));
  169. }
  170. return redirect()->route('admin.system.index');
  171. }
  172. public function setConfig(SystemRequest $request): JsonResponse // 设置某个配置项
  173. {
  174. $name = $request->input('name');
  175. $value = $request->input('value');
  176. if (empty($value) || $value === 'NaN') { // 关闭 或 空值 自动设NULL,减少系统设置存储
  177. $value = null;
  178. }
  179. // 支付设置判断
  180. if ($value !== null && in_array($name, ['is_AliPay', 'is_QQPay', 'is_WeChatPay'], true) && ! in_array($value, PaymentManager::getAvailable(), true)) {
  181. return response()->json(['status' => 'fail', 'message' => trans('admin.system.params_required', ['attribute' => trans('admin.system.payment.attribute')])]);
  182. }
  183. if ($value > 1 && $name === 'is_captcha' && ! $this->getCaptcha()) {
  184. return response()->json(['status' => 'fail', 'message' => trans('admin.system.params_required', ['attribute' => trans('auth.captcha.attribute')])]);
  185. }
  186. // 演示环境禁止修改特定配置项
  187. if (config('app.env') === 'demo') {
  188. $denyConfig = [
  189. 'website_url',
  190. 'is_captcha',
  191. 'forbid_mode',
  192. 'website_security_code',
  193. 'website_security_code',
  194. 'username_type',
  195. ];
  196. if (in_array($name, $denyConfig, true)) {
  197. return response()->json(['status' => 'fail', 'message' => trans('admin.system.demo_restriction')]);
  198. }
  199. }
  200. // 如果是返利比例,则需要除100
  201. if ($name === 'referral_percent') {
  202. $value /= 100;
  203. }
  204. // 设置TG机器人
  205. if ($name === 'telegram_token' && $value) {
  206. $telegramService = new TelegramService($value);
  207. $telegramService->getMe();
  208. $telegramService->setWebhook(rtrim(sysConfig('website_url'), '/').'/api/telegram/webhook?access_token='.md5($value));
  209. }
  210. // 更新配置
  211. if (Config::findOrFail($name)->update(['value' => $value])) {
  212. return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.update')])]);
  213. }
  214. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.update')])]);
  215. }
  216. public function sendTestNotification(): JsonResponse // 推送通知测试
  217. {
  218. $channels = [
  219. 'serverChan' => ServerChanChannel::class,
  220. 'bark' => BarkChannel::class,
  221. 'telegram' => TelegramChannel::class,
  222. 'weChat' => WeChatChannel::class,
  223. 'tgChat' => TgChatChannel::class,
  224. 'pushPlus' => PushPlusChannel::class,
  225. 'iYuu' => iYuuChannel::class,
  226. 'pushDeer' => PushDeerChannel::class,
  227. 'dingTalk' => DingTalkChannel::class,
  228. ];
  229. $selectedChannel = request('channel');
  230. if (! array_key_exists($selectedChannel, $channels)) {
  231. return response()->json(['status' => 'fail', 'message' => trans('admin.system.notification.test.unknown_channel')]);
  232. }
  233. Notification::sendNow(auth()->user(), new Custom(trans('admin.system.notification.test.title'), sysConfig('website_name').' '.trans('admin.system.notification.test.content')), [$channels[$selectedChannel]]);
  234. return response()->json(['status' => 'success', 'message' => trans('admin.system.notification.test.success')]);
  235. }
  236. public function common(): View
  237. {
  238. return view('admin.config.common', [
  239. 'methods' => SsConfig::type(1)->get(),
  240. 'protocols' => SsConfig::type(2)->get(),
  241. 'categories' => GoodsCategory::all(),
  242. 'obfsList' => SsConfig::type(3)->get(),
  243. 'countries' => Country::all(),
  244. 'levels' => Level::all(),
  245. 'labels' => Label::with('nodes')->get(),
  246. ]);
  247. }
  248. }