ConfigController.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Requests\Admin\ConfigSave;
  4. use App\Jobs\SendEmailJob;
  5. use App\Services\TelegramService;
  6. use Illuminate\Http\Request;
  7. use App\Utils\Dict;
  8. use App\Http\Controllers\Controller;
  9. use Illuminate\Support\Facades\Mail;
  10. class ConfigController extends Controller
  11. {
  12. public function getEmailTemplate()
  13. {
  14. $path = resource_path('views/mail/');
  15. $files = array_map(function ($item) use ($path) {
  16. return str_replace($path, '', $item);
  17. }, glob($path . '*'));
  18. return response([
  19. 'data' => $files
  20. ]);
  21. }
  22. public function getThemeTemplate()
  23. {
  24. $path = public_path('theme/');
  25. $files = array_map(function ($item) use ($path) {
  26. return str_replace($path, '', $item);
  27. }, glob($path . '*'));
  28. return response([
  29. 'data' => $files
  30. ]);
  31. }
  32. public function testSendMail(Request $request)
  33. {
  34. $obj = new SendEmailJob([
  35. 'email' => $request->session()->get('email'),
  36. 'subject' => 'This is v2board test email',
  37. 'template_name' => 'notify',
  38. 'template_value' => [
  39. 'name' => config('v2board.app_name', 'V2Board'),
  40. 'content' => 'This is v2board test email',
  41. 'url' => config('v2board.app_url')
  42. ]
  43. ]);
  44. return response([
  45. 'data' => true,
  46. 'log' => $obj->handle()
  47. ]);
  48. }
  49. public function setTelegramWebhook(Request $request)
  50. {
  51. $appUrl = config('v2board.app_url');
  52. if (!$appUrl) abort(500, '请先配置正确的站点地址');
  53. $telegramService = new TelegramService($request->input('telegram_bot_token'));
  54. $telegramService->getMe();
  55. $telegramService->setWebhook(
  56. $appUrl . '/api/v1/guest/telegram/webhook?access_token=' . md5(config('v2board.telegram_bot_token', $request->input('telegram_bot_token')))
  57. );
  58. return response([
  59. 'data' => true
  60. ]);
  61. }
  62. public function fetch(Request $request)
  63. {
  64. $key = $request->input('key');
  65. $data = [
  66. 'invite' => [
  67. 'invite_force' => (int)config('v2board.invite_force', 0),
  68. 'invite_commission' => config('v2board.invite_commission', 10),
  69. 'invite_gen_limit' => config('v2board.invite_gen_limit', 5),
  70. 'invite_never_expire' => config('v2board.invite_never_expire', 0),
  71. 'commission_first_time_enable' => config('v2board.commission_first_time_enable', 1),
  72. 'commission_auto_check_enable' => config('v2board.commission_auto_check_enable', 1),
  73. 'commission_withdraw_limit' => config('v2board.commission_withdraw_limit', 100),
  74. 'commission_withdraw_method' => config('v2board.commission_withdraw_method', Dict::WITHDRAW_METHOD_WHITELIST_DEFAULT),
  75. 'withdraw_close_enable' => config('v2board.withdraw_close_enable', 0),
  76. 'commission_distribution_enable' => config('v2board.commission_distribution_enable', 0),
  77. 'commission_distribution_l1' => config('v2board.commission_distribution_l1'),
  78. 'commission_distribution_l2' => config('v2board.commission_distribution_l2'),
  79. 'commission_distribution_l3' => config('v2board.commission_distribution_l3')
  80. ],
  81. 'site' => [
  82. 'safe_mode_enable' => (int)config('v2board.safe_mode_enable', 0),
  83. 'stop_register' => (int)config('v2board.stop_register', 0),
  84. 'email_verify' => (int)config('v2board.email_verify', 0),
  85. 'app_name' => config('v2board.app_name', 'V2Board'),
  86. 'app_description' => config('v2board.app_description', 'V2Board is best!'),
  87. 'app_url' => config('v2board.app_url'),
  88. 'subscribe_url' => config('v2board.subscribe_url'),
  89. 'try_out_plan_id' => (int)config('v2board.try_out_plan_id', 0),
  90. 'try_out_hour' => (int)config('v2board.try_out_hour', 1),
  91. 'email_whitelist_enable' => (int)config('v2board.email_whitelist_enable', 0),
  92. 'email_whitelist_suffix' => config('v2board.email_whitelist_suffix', Dict::EMAIL_WHITELIST_SUFFIX_DEFAULT),
  93. 'email_gmail_limit_enable' => config('v2board.email_gmail_limit_enable', 0),
  94. 'recaptcha_enable' => (int)config('v2board.recaptcha_enable', 0),
  95. 'recaptcha_key' => config('v2board.recaptcha_key'),
  96. 'recaptcha_site_key' => config('v2board.recaptcha_site_key'),
  97. 'tos_url' => config('v2board.tos_url'),
  98. 'currency' => config('v2board.currency', 'CNY'),
  99. 'currency_symbol' => config('v2board.currency_symbol', '¥'),
  100. 'register_limit_by_ip_enable' => (int)config('v2board.register_limit_by_ip_enable', 0),
  101. 'register_limit_count' => config('v2board.register_limit_count', 3),
  102. 'register_limit_expire' => config('v2board.register_limit_expire', 60)
  103. ],
  104. 'subscribe' => [
  105. 'plan_change_enable' => (int)config('v2board.plan_change_enable', 1),
  106. 'reset_traffic_method' => (int)config('v2board.reset_traffic_method', 0),
  107. 'surplus_enable' => (int)config('v2board.surplus_enable', 1),
  108. 'new_order_event_id' => (int)config('v2board.new_order_event_id', 0),
  109. 'renew_order_event_id' => (int)config('v2board.renew_order_event_id', 0),
  110. 'change_order_event_id' => (int)config('v2board.change_order_event_id', 0),
  111. ],
  112. 'frontend' => [
  113. 'frontend_theme' => config('v2board.frontend_theme', 'v2board'),
  114. 'frontend_theme_sidebar' => config('v2board.frontend_theme_sidebar', 'light'),
  115. 'frontend_theme_header' => config('v2board.frontend_theme_header', 'dark'),
  116. 'frontend_theme_color' => config('v2board.frontend_theme_color', 'default'),
  117. 'frontend_background_url' => config('v2board.frontend_background_url'),
  118. 'frontend_admin_path' => config('v2board.frontend_admin_path', 'admin'),
  119. 'frontend_customer_service_method' => config('v2board.frontend_customer_service_method', 0),
  120. 'frontend_customer_service_id' => config('v2board.frontend_customer_service_id'),
  121. ],
  122. 'server' => [
  123. 'server_token' => config('v2board.server_token'),
  124. 'server_license' => config('v2board.server_license'),
  125. 'server_log_enable' => config('v2board.server_log_enable', 0),
  126. 'server_v2ray_domain' => config('v2board.server_v2ray_domain'),
  127. 'server_v2ray_protocol' => config('v2board.server_v2ray_protocol'),
  128. ],
  129. 'email' => [
  130. 'email_template' => config('v2board.email_template', 'default'),
  131. 'email_host' => config('v2board.email_host'),
  132. 'email_port' => config('v2board.email_port'),
  133. 'email_username' => config('v2board.email_username'),
  134. 'email_password' => config('v2board.email_password'),
  135. 'email_encryption' => config('v2board.email_encryption'),
  136. 'email_from_address' => config('v2board.email_from_address')
  137. ],
  138. 'telegram' => [
  139. 'telegram_bot_enable' => config('v2board.telegram_bot_enable', 0),
  140. 'telegram_bot_token' => config('v2board.telegram_bot_token'),
  141. 'telegram_discuss_link' => config('v2board.telegram_discuss_link')
  142. ],
  143. 'app' => [
  144. 'windows_version' => config('v2board.windows_version'),
  145. 'windows_download_url' => config('v2board.windows_download_url'),
  146. 'macos_version' => config('v2board.macos_version'),
  147. 'macos_download_url' => config('v2board.macos_download_url'),
  148. 'android_version' => config('v2board.android_version'),
  149. 'android_download_url' => config('v2board.android_download_url')
  150. ]
  151. ];
  152. if ($key && isset($data[$key])) {
  153. return response([
  154. 'data' => [
  155. $key => $data[$key]
  156. ]
  157. ]);
  158. };
  159. // TODO: default should be in Dict
  160. return response([
  161. 'data' => $data
  162. ]);
  163. }
  164. public function save(ConfigSave $request)
  165. {
  166. $data = $request->validated();
  167. $array = \Config::get('v2board');
  168. foreach ($data as $k => $v) {
  169. if (!in_array($k, array_keys($request->validated()))) {
  170. abort(500, '参数' . $k . '不在规则内,禁止修改');
  171. }
  172. $array[$k] = $v;
  173. }
  174. $data = var_export($array, 1);
  175. if (!\File::put(base_path() . '/config/v2board.php', "<?php\n return $data ;")) {
  176. abort(500, '修改失败');
  177. }
  178. if (function_exists('opcache_reset')) {
  179. if (opcache_reset() === false) {
  180. abort(500, '缓存清除失败,请卸载或检查opcache配置状态');
  181. }
  182. }
  183. \Artisan::call('config:cache');
  184. return response([
  185. 'data' => true
  186. ]);
  187. }
  188. }