using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Library;
using Tea;
using Tea.Utils;

namespace MySystem
{
    public class AliyunOcr
    {
        private static string AccessKeyID = "LTAI5tBNY8CzWquEUx8HGqUX";
        private static string AccessKeySecret = "VOGf7ibdydKeKqkb2BfEwp7Nh4gPRJ";
        
        /**
         * 使用AK&SK初始化账号Client
         * @return Client
         * @throws Exception
         */
        public static AlibabaCloud.SDK.Ocr_api20210707.Client CreateClient()
        {
            // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
            // 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378671.html。
            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            {
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                AccessKeyId = AccessKeyID,
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                AccessKeySecret = AccessKeySecret,
            };
            // Endpoint 请参考 https://api.aliyun.com/product/ocr-api
            config.Endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
            return new AlibabaCloud.SDK.Ocr_api20210707.Client(config);
        }

        public static string Check(string Url)
        {
            string result = "";
            AlibabaCloud.SDK.Ocr_api20210707.Client client = CreateClient();
            AlibabaCloud.SDK.Ocr_api20210707.Models.RecognizeBusinessLicenseRequest recognizeBusinessLicenseRequest = new AlibabaCloud.SDK.Ocr_api20210707.Models.RecognizeBusinessLicenseRequest
            {
                Url = Url,
            };
            AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
            try
            {
                // 复制代码运行请自行打印 API 的返回值
                var res = client.RecognizeBusinessLicenseWithOptions(recognizeBusinessLicenseRequest, runtime);
                result = res.Body.Data;
            }
            catch (TeaException error)
            {
                // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
                // 错误 message
                function.WriteLog(error.Message, "OCR异常");
                // 诊断地址
                function.WriteLog(error.Data["Recommend"].ToString(), "OCR异常");
                AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
            }
            catch (Exception _error)
            {
                TeaException error = new TeaException(new Dictionary<string, object>
                {
                    { "message", _error.Message }
                });
                // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
                // 错误 message
                function.WriteLog(error.Message, "OCR异常");
                // 诊断地址
                function.WriteLog(error.Data["Recommend"].ToString(), "OCR异常");
                AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
            }
            return result;
        }

        
    }
}