SystemController.php 11 KB

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