SystemController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 Illuminate\Contracts\View\View;
  23. use Illuminate\Http\JsonResponse;
  24. use Illuminate\Http\RedirectResponse;
  25. use Illuminate\Http\Request;
  26. use Notification;
  27. use NotificationChannels\Telegram\TelegramChannel;
  28. class SystemController extends Controller
  29. {
  30. public function index(): View
  31. { // 系统设置
  32. return view('admin.config.system', array_merge([
  33. 'payments' => $this->getPayments(),
  34. 'captcha' => $this->getCaptcha(),
  35. 'channels' => $this->getNotifyChannels(),
  36. 'ddns_labels' => (new DDNS)->getLabels(),
  37. ], Config::pluck('value', 'name')->toArray()));
  38. }
  39. private function getPayments(): array
  40. {
  41. $paymentConfigs = [ // 支付渠道及其所需配置项映射
  42. 'f2fpay' => ['f2fpay_app_id', 'f2fpay_private_key', 'f2fpay_public_key'],
  43. 'codepay' => ['codepay_url', 'codepay_id', 'codepay_key'],
  44. 'epay' => ['epay_url', 'epay_mch_id', 'epay_key'],
  45. 'payjs' => ['payjs_mch_id', 'payjs_key'],
  46. 'bitpayx' => ['bitpay_secret'],
  47. 'paypal' => ['paypal_client_id', 'paypal_client_secret', 'paypal_app_id'],
  48. 'stripe' => ['stripe_public_key', 'stripe_secret_key'],
  49. 'paybeaver' => ['paybeaver_app_id', 'paybeaver_app_secret'],
  50. 'theadpay' => ['theadpay_mchid', 'theadpay_key'],
  51. ];
  52. $payment = [];
  53. // 遍历映射,检查配置项是否存在
  54. foreach ($paymentConfigs as $paymentName => $configKeys) {
  55. $allConfigsExist = array_reduce($configKeys, static function ($carry, $configKey) {
  56. return $carry && sysConfig($configKey);
  57. }, true);
  58. if ($allConfigsExist) {
  59. $payment[] = $paymentName;
  60. }
  61. }
  62. return $payment ?? [];
  63. }
  64. private function getCaptcha(): bool
  65. {
  66. return sysConfig('captcha_secret') && sysConfig('captcha_key');
  67. }
  68. private function getNotifyChannels(): array
  69. {
  70. $configs = [ // 支付渠道及其所需配置项映射
  71. 'bark' => ['bark_key'],
  72. 'dingTalk' => ['dingTalk_access_token'],
  73. 'iYuu' => ['iYuu_token'],
  74. 'pushDear' => ['pushDeer_key'],
  75. 'pushPlus' => ['pushplus_token'],
  76. 'serverChan' => ['server_chan_key'],
  77. 'telegram' => ['telegram_token'],
  78. 'tgChat' => ['tg_chat_token'],
  79. 'weChat' => ['wechat_cid', 'wechat_aid', 'wechat_secret', 'wechat_token', 'wechat_encodingAESKey'],
  80. ];
  81. $channels = ['database', 'mail'];
  82. // 遍历映射,检查配置项是否存在
  83. foreach ($configs as $channel => $configKeys) {
  84. $allConfigsExist = array_reduce($configKeys, static function ($carry, $configKey) {
  85. return $carry && sysConfig($configKey);
  86. }, true);
  87. if ($allConfigsExist) {
  88. $channels[] = $channel;
  89. }
  90. }
  91. return $channels;
  92. }
  93. public function setExtend(Request $request): RedirectResponse // 设置涉及到上传的设置
  94. {
  95. if ($request->hasAny(['website_home_logo', 'website_home_logo'])) { // 首页LOGO
  96. if ($request->hasFile('website_home_logo')) {
  97. $validator = validator()->make($request->all(), ['website_home_logo' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048']);
  98. if ($validator->fails()) {
  99. return redirect()->route('admin.system.index', '#other')->withErrors($validator->errors());
  100. }
  101. $file = $request->file('website_home_logo');
  102. $file->move('uploads/logo', $file->getClientOriginalName());
  103. if (Config::findOrNew('website_home_logo')->update(['value' => 'uploads/logo/'.$file->getClientOriginalName()])) {
  104. return redirect()->route('admin.system.index', '#other')->with('successMsg', trans('common.success_item', ['attribute' => trans('common.update')]));
  105. }
  106. }
  107. if ($request->hasFile('website_logo')) { // 站内LOGO
  108. $validator = validator()->make($request->all(), ['website_logo' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048']);
  109. if ($validator->fails()) {
  110. return redirect()->route('admin.system.index', '#other')->withErrors($validator->errors());
  111. }
  112. $file = $request->file('website_logo');
  113. $file->move('uploads/logo', $file->getClientOriginalName());
  114. if (Config::findOrNew('website_logo')->update(['value' => 'uploads/logo/'.$file->getClientOriginalName()])) {
  115. return redirect()->route('admin.system.index', '#other')->with('successMsg', trans('common.success_item', ['attribute' => trans('common.update')]));
  116. }
  117. }
  118. return redirect()->route('admin.system.index', '#other')->withErrors(trans('common.failed_item', ['attribute' => trans('common.update')]));
  119. }
  120. if ($request->hasAny(['alipay_qrcode', 'wechat_qrcode'])) {
  121. if ($request->hasFile('alipay_qrcode')) {
  122. $validator = validator()->make($request->all(), ['alipay_qrcode' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048']);
  123. if ($validator->fails()) {
  124. return redirect()->route('admin.system.index', '#payment')->withErrors($validator->errors());
  125. }
  126. $file = $request->file('alipay_qrcode');
  127. $file->move('uploads/images', $file->getClientOriginalName());
  128. if (Config::findOrNew('alipay_qrcode')->update(['value' => 'uploads/images/'.$file->getClientOriginalName()])) {
  129. return redirect()->route('admin.system.index', '#payment')->with('successMsg', trans('common.success_item', ['attribute' => trans('common.update')]));
  130. }
  131. }
  132. if ($request->hasFile('wechat_qrcode')) { // 站内LOGO
  133. $validator = validator()->make($request->all(), ['wechat_qrcode' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048']);
  134. if ($validator->fails()) {
  135. return redirect()->route('admin.system.index', '#payment')->withErrors($validator->errors());
  136. }
  137. $file = $request->file('wechat_qrcode');
  138. $file->move('uploads/images', $file->getClientOriginalName());
  139. if (Config::findOrNew('wechat_qrcode')->update(['value' => 'uploads/images/'.$file->getClientOriginalName()])) {
  140. return redirect()->route('admin.system.index', '#payment')->with('successMsg', trans('common.success_item', ['attribute' => trans('common.update')]));
  141. }
  142. }
  143. return redirect()->route('admin.system.index', '#payment')->withErrors(trans('common.failed_item', ['attribute' => trans('common.update')]));
  144. }
  145. return redirect()->route('admin.system.index');
  146. }
  147. public function setConfig(SystemRequest $request): JsonResponse // 设置某个配置项
  148. {
  149. $name = $request->input('name');
  150. $value = $request->input('value');
  151. if (empty($value) || $value === 'NaN') { // 关闭 或 空值 自动设NULL,减少系统设置存储
  152. $value = null;
  153. }
  154. // 支付设置判断
  155. if ($value !== null && in_array($name, ['is_AliPay', 'is_QQPay', 'is_WeChatPay'], true) && ! in_array($value, $this->getPayments(), true)) {
  156. return response()->json(['status' => 'fail', 'message' => trans('admin.system.params_required', ['attribute' => trans('admin.system.payment.attribute')])]);
  157. }
  158. if ($value > 1 && $name === 'is_captcha' && ! $this->getCaptcha()) {
  159. return response()->json(['status' => 'fail', 'message' => trans('admin.system.params_required', ['attribute' => trans('auth.captcha.attribute')])]);
  160. }
  161. // 演示环境禁止修改特定配置项
  162. if (config('app.env') === 'demo') {
  163. $denyConfig = [
  164. 'website_url',
  165. 'is_captcha',
  166. 'min_rand_traffic',
  167. 'max_rand_traffic',
  168. 'forbid_mode',
  169. 'website_security_code',
  170. 'website_security_code',
  171. 'username_type',
  172. ];
  173. if (in_array($name, $denyConfig, true)) {
  174. return response()->json(['status' => 'fail', 'message' => trans('admin.system.demo_restriction')]);
  175. }
  176. }
  177. // 如果是返利比例,则需要除100
  178. if ($name === 'referral_percent') {
  179. $value /= 100;
  180. }
  181. // 设置TG机器人
  182. if ($name === 'telegram_token' && $value) {
  183. $telegramService = new TelegramService($value);
  184. $telegramService->getMe();
  185. $telegramService->setWebhook(rtrim(sysConfig('website_url'), '/').'/api/telegram/webhook?access_token='.md5($value));
  186. }
  187. // 更新配置
  188. if (Config::findOrFail($name)->update(['value' => $value])) {
  189. return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.update')])]);
  190. }
  191. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.update')])]);
  192. }
  193. public function sendTestNotification(): JsonResponse // 推送通知测试
  194. {
  195. $channels = [
  196. 'serverChan' => ServerChanChannel::class,
  197. 'bark' => BarkChannel::class,
  198. 'telegram' => TelegramChannel::class,
  199. 'weChat' => WeChatChannel::class,
  200. 'tgChat' => TgChatChannel::class,
  201. 'pushPlus' => PushPlusChannel::class,
  202. 'iYuu' => iYuuChannel::class,
  203. 'pushDeer' => PushDeerChannel::class,
  204. 'dingTalk' => DingTalkChannel::class,
  205. ];
  206. $selectedChannel = request('channel');
  207. if (! array_key_exists($selectedChannel, $channels)) {
  208. return response()->json(['status' => 'fail', 'message' => trans('admin.system.notification.test.unknown_channel')]);
  209. }
  210. Notification::sendNow(auth()->user(), new Custom(trans('admin.system.notification.test.title'), sysConfig('website_name').' '.trans('admin.system.notification.test.content')), [$channels[$selectedChannel]]);
  211. return response()->json(['status' => 'success', 'message' => trans('admin.system.notification.test.success')]);
  212. }
  213. public function common(): View
  214. {
  215. return view('admin.config.common', [
  216. 'methods' => SsConfig::type(1)->get(),
  217. 'protocols' => SsConfig::type(2)->get(),
  218. 'categories' => GoodsCategory::all(),
  219. 'obfsList' => SsConfig::type(3)->get(),
  220. 'countries' => Country::all(),
  221. 'levels' => Level::all(),
  222. 'labels' => Label::with('nodes')->get(),
  223. ]);
  224. }
  225. }