index.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * @author tuonian
  3. * @date 2023/7/5
  4. */
  5. import {AutoTypeInputProps, isNull, uniqueKey} from 'planning-tools'
  6. import './index.less'
  7. import config from './config.json'
  8. import {IContentProps, NgxBasicInput, registerInput} from "../basic";
  9. import {Input, Tooltip} from "antd";
  10. type DataType = {
  11. /**
  12. * 多域名时的随机key
  13. */
  14. key?: string
  15. origins?: string[]
  16. methods?: string[]
  17. headers?: string[]
  18. preflight?: boolean
  19. credentials?: boolean
  20. maxAge?: number
  21. }
  22. export const CorsInput = ({...props}: AutoTypeInputProps)=>{
  23. const ShowContent = ({data}: IContentProps<DataType>)=>{
  24. if (data.origins?.length && data.methods?.length){
  25. const lines = renderLines(data);
  26. const httpLines = renderHttpLines(data)
  27. let hint = ''
  28. if (httpLines.length){
  29. hint = '# map \n'+ httpLines.join('\n') +'\n'
  30. }
  31. hint +='# location\n'
  32. hint += lines.join('\n')
  33. return (<Tooltip
  34. destroyTooltipOnHide
  35. overlayClassName="cors-config-overlay"
  36. trigger="click"
  37. placement="topLeft"
  38. autoAdjustOverflow
  39. title={<Input.TextArea disabled rows={Math.min(10,lines.length + httpLines.length + 3)} value={hint} />}>
  40. <span className="config-status has-config">已配置</span>
  41. </Tooltip>)
  42. }
  43. return <span className="config-status">未完成配置</span>
  44. }
  45. const renderHttpLines = (values: DataType = {})=>{
  46. const lines: string[] = []
  47. if (!values.origins?.length){
  48. return lines
  49. }
  50. if (values.origins.length < 2){
  51. return lines
  52. }
  53. lines.push(`map $http_origin ${values.key} {`)
  54. lines.push(` default 0;`)
  55. values.origins.forEach(host=>{
  56. lines.push(` "~${host}" ${host};`)
  57. })
  58. lines.push(`}`)
  59. return lines
  60. }
  61. const renderLines = (values: DataType = {})=>{
  62. const lines: string[] = []
  63. if (!values.key){
  64. values.key = `$cors_${uniqueKey(20)}`
  65. }
  66. if (!values.origins?.length){
  67. return lines
  68. }
  69. if (values.origins.length === 1){
  70. lines.push(`add_header 'Access-Control-Allow-Origin' '${values.origins[0]}';`)
  71. }else {
  72. lines.push(`add_header 'Access-Control-Allow-Origin' ${values.key};`)
  73. }
  74. if (values.methods?.length){
  75. lines.push(`add_header 'Access-Control-Allow-Origin' '${values.methods.join(',')}';`)
  76. }
  77. if (values.headers?.length){
  78. lines.push(`add_header 'Access-Control-Allow-Headers' '${values.headers.join(',')}';`)
  79. }
  80. if (!isNull(values.credentials)){
  81. lines.push(`add_header Access-Control-Allow-Credentials '${values.credentials ? 'true': 'false'}';`)
  82. }
  83. if (!isNull(values.preflight)){
  84. lines.push(`if ($request_method = 'OPTIONS') {
  85. return 204;
  86. }`)
  87. }
  88. return lines;
  89. }
  90. return <NgxBasicInput
  91. {...props}
  92. columns={config.form}
  93. renderLines={renderLines}
  94. renderHttpLines={renderHttpLines}
  95. content={ShowContent}
  96. overlayClassName="cors-page-overlay"
  97. labelCol={4}
  98. />
  99. }
  100. registerInput('cors', CorsInput)