<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>COOL-ADMIN &#8211; 「马马虎虎」</title>
	<atom:link href="https://www.gek6.cn/category/web-service/cool-admin/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.gek6.cn</link>
	<description>极客蜗牛-开发效率很慢的...</description>
	<lastBuildDate>Wed, 26 Jul 2023 08:28:43 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.2</generator>

<image>
	<url>https://www.gek6.cn/wp-content/uploads/2021/12/20211205200322145-32x32.png</url>
	<title>COOL-ADMIN &#8211; 「马马虎虎」</title>
	<link>https://www.gek6.cn</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【cool-admin】接口参数校验Joi</title>
		<link>https://www.gek6.cn/%e3%80%90cool-admin%e3%80%91%e6%8e%a5%e5%8f%a3%e5%8f%82%e6%95%b0%e6%a0%a1%e9%aa%8c/</link>
					<comments>https://www.gek6.cn/%e3%80%90cool-admin%e3%80%91%e6%8e%a5%e5%8f%a3%e5%8f%82%e6%95%b0%e6%a0%a1%e9%aa%8c/#respond</comments>
		
		<dc:creator><![CDATA[lane]]></dc:creator>
		<pubDate>Wed, 26 Jul 2023 08:28:19 +0000</pubDate>
				<category><![CDATA[COOL-ADMIN]]></category>
		<guid isPermaLink="false">https://www.gek6.cn/?p=159</guid>

					<description><![CDATA[创建校验类 import { R&#46;&#46;&#46;]]></description>
										<content:encoded><![CDATA[<h3>创建校验类</h3>
<pre><code>import { Rule, RuleType } from &#039;@midwayjs/validate&#039;;

export class SendVerifyCodeValidator {
  // 手机号
  @Rule(RuleType.string().label(&#039;手机号&#039;).length(11).required())
  phoneNum: string;
}

export class BindPhoneValidator {
  // 手机号
  @Rule(RuleType.string().label(&#039;手机号&#039;).length(11).required())
  phoneNum: string;
  // 验证码
  @Rule(RuleType.string().label(&#039;验证码&#039;).length(6).required())
  verifyCode: string;
}</code></pre>
<h3>在控制器上加上  <code>@Validate()</code> 即可</h3>
<pre><code>  /**
   * 绑定手机号
   */
  @Post(&#039;/bindPhoneNum&#039;)
  @Validate({
    errorStatus: httpCodeEnum.PARAMS_ERROR,
  })
  async bindPhoneNum(@Body() bindPhoneData: BindPhoneValidator) {
    // TODO 绑定...
    return this.ok(null);
  }</code></pre>
<h1>重点：自定义错误提示语</h1>
<pre><code>  // 验证码
  @Rule(
    RuleType.string().label(&#039;验证码&#039;).length(6).required().messages({
      &#039;string.length&#039;: &#039;{{#label}}位数错误&#039;,
      &#039;string.empty&#039;: &#039;验证码不能为空&#039;,
      &#039;any.required&#039;: &#039;验证码必填&#039;,
    })
  )
  verifyCode: string;</code></pre>
<h3>这么写 是不行的！！</h3>
<h3>需要在这里去定义</h3>
<pre><code>  @Post(&#039;/bindPhoneNum&#039;)
  @Validate({
    errorStatus: httpCodeEnum.PARAMS_ERROR,
    validationOptions: {
      messages: {
      &#039;string.length&#039;: &#039;{{#label}}位数错误&#039;,
      &#039;string.empty&#039;: &#039;{{#label}}不能为空&#039;,
      &#039;any.required&#039;: &#039;{{#label}}必填&#039;,
      },
    },
  })
  async bindPhoneNum(@Body() bindPhoneData: BindPhoneValidator)</code></pre>
<p>但是这样会更改所有字段的对应异常提示<br />
比如这个接口要校验手机号和验证码两个参数<br />
当手机号校验11位未通过，会提示 手机号位数错误<br />
当验证码校验6位未通过，会提示 验证码位数错误</p>
<h3>如果需要自定义验证器 并 提示自定义的异常信息</h3>
<pre><code>  // 验证码校验规则
  @Rule(
    RuleType.string()
      .label(&#039;验证码&#039;)
      .required()
      .custom((value, helpers) =&gt; {
        const reg = /^\d{6}$/;
        if (reg.test(value)) {
          return value;
        }
        throw new Error(&#039;格式错误&#039;);
      })
  )
  verifyCode: string;

  // 控制器中校验装饰器
  @Validate({
    errorStatus: httpCodeEnum.PARAMS_ERROR,
    validationOptions: {
      messages: {
        &#039;any.custom&#039;: &#039;{{#label}}{{#error.message}}&#039;,
      },
    },
  })

  // 最终响应
  {&quot;code&quot;:5001,&quot;message&quot;:&quot;\&quot;验证码\&quot;格式错误&quot;}
</code></pre>
]]></content:encoded>
					
					<wfw:commentRss>https://www.gek6.cn/%e3%80%90cool-admin%e3%80%91%e6%8e%a5%e5%8f%a3%e5%8f%82%e6%95%b0%e6%a0%a1%e9%aa%8c/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Cool-Admin 开启跨域问题</title>
		<link>https://www.gek6.cn/cool-admin-%e5%bc%80%e5%90%af%e8%b7%a8%e5%9f%9f%e9%97%ae%e9%a2%98/</link>
					<comments>https://www.gek6.cn/cool-admin-%e5%bc%80%e5%90%af%e8%b7%a8%e5%9f%9f%e9%97%ae%e9%a2%98/#respond</comments>
		
		<dc:creator><![CDATA[lane]]></dc:creator>
		<pubDate>Tue, 04 Jul 2023 07:11:37 +0000</pubDate>
				<category><![CDATA[COOL-ADMIN]]></category>
		<guid isPermaLink="false">https://www.gek6.cn/?p=151</guid>

					<description><![CDATA[// /src/configur&#46;&#46;&#46;]]></description>
										<content:encoded><![CDATA[<pre><code>// /src/configuration.ts
import * as crossDomain from &#039;@midwayjs/cross-domain&#039;;
@Configuration({
  imports: [
    crossDomain,
  ],
  importConfigs: [join(__dirname, &#039;./config&#039;)],
})</code></pre>
<pre><code>// /src/config/config.default.ts

export default {
  cors: {
    // 允许跨域的方法，【默认值】为 GET,HEAD,PUT,POST,DELETE,PATCH
    allowMethods: &#039;*&#039;,
    // 设置 Access-Control-Allow-Origin 的值，【默认值】会获取请求头上的 origin
    // 也可以配置为一个回调方法，传入的参数为 request，需要返回 origin 值
    // 例如：http://test.midwayjs.org
    // 如果设置了 credentials，则 origin 不能设置为 *
    origin: &#039;*&#039;,
    // 设置 Access-Control-Allow-Headers 的值，【默认值】会获取请求头上的 Access-Control-Request-Headers
    allowHeaders: &#039;*&#039;,
    // 设置 Access-Control-Expose-Headers 的值
    exposeHeaders: &#039;*&#039;,
    // 设置 Access-Control-Allow-Credentials，【默认值】false
    // 也可以配置为一个回调方法，传入的参数为 request，返回值为 true 或 false
    credentials: false,
    // 是否在执行报错的时候，把跨域的 header 信息写入到 error 对的 headers 属性中，【默认值】false
    keepHeadersOnError: true,
    // 设置 Access-Control-Max-Age
    maxAge: 36000,
  },
}
</code></pre>
<h1>重点！后台管理的接口报错，经过排查是因为对 OPTIONS 请求未做特殊响应</h1>
<p><img decoding="async" src="https://foruda.gitee.com/images/1687833639383824912/a12f734e_1999285.png" alt="后台登录权鉴中间件修改" title="未命名1687833597.png" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.gek6.cn/cool-admin-%e5%bc%80%e5%90%af%e8%b7%a8%e5%9f%9f%e9%97%ae%e9%a2%98/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
