1
0

SystemController.php 11 KB

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