ningshuxia
2024-05-27 f51ccee7e76f598c1f718190d216f96b5ea1ca46
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
namespace Yw.Application
{
    /// <summary>
    /// Tool
    /// </summary>
    [AllowAnonymous]
    [Route("Auth/Tool")]
    [ApiDescriptionSettings("Auth", Name = "权限工具", Order = 100000)]
    public class Tool_Controller : IDynamicApiController, ITransient
    {
        private readonly ICaptcha _captcha;//验证码组件
 
        /// <summary>
        /// 
        /// </summary>
        public Tool_Controller(ICaptcha captcha)
        {
            _captcha = captcha;
        }
 
 
 
        /// <summary>
        /// 获取图片验证码
        /// </summary>
        [Route("GetCaptcha@V1.0")]
        [HttpGet]
        public dynamic GetCaptcha()
        {
            var codeId = YitIdHelper.NextId();
            var captcha = _captcha.Generate(codeId.ToString());
            return new { Id = codeId, Img = captcha.Base64 };
        }
 
        /// <summary>
        /// 验证图片验证码
        /// </summary>
        /// <param name="Id">验证码id</param>
        /// <param name="Code">验证码</param>
        /// <returns></returns>
        [Route("VerifyCaptcha@V1.0")]
        [HttpGet]
        public bool VerifyCaptcha
            (
                [Required]
                long Id, //验证码id
                [Required,DataValidation(AllowEmptyStrings = false)]
                string Code//验证码
            )
        {
            // 判断验证码
            if (!_captcha.Validate(Id.ToString(), Code))
            {
                return false;
            }
            return true;
        }
 
    }
}