send sms

unit untSMS;

interface

uses
  System.Hash,
  System.Net.HttpClientComponent,
  System.Classes,
  System.Generics.Collections,
  System.SysUtils,
  System.JSON,
  System.NetEncoding;

type
  TSmsBaseRequest = class
  private
    FClient: TNetHTTPClient;
    FURL:string;
    FAccessKeyId: string;
    FAccessKeySecret: string;
    FResultMessage: string;
  protected
    function SpecialUrlEncode(const Value: string): string;
    function Post(const URL: string; const ASource: TStream): string;
  public
    constructor Create;
    destructor Destroy; override;
    function ParseResponse(const Response: string): Boolean;
    property URL:string read FURL write FURL;
    property AccessKeyId: string read FAccessKeyId write FAccessKeyId;
    property AccessKeySecret: string read FAccessKeySecret write FAccessKeySecret;
    property ResultMessage: string read FResultMessage write FResultMessage;
  end;

  TSmsRequest = class(TSmsBaseRequest)
  private
    FPhoneNumbers: string;
    FReport:string;
    FContent:string;
  public
    constructor Create;
    destructor Destroy; override;
    /// 
    /// 常见问题
    ///  1.相同号码一天有10条限制(可根据业务需求进行调整)
    ///  2.接口账号(使用API账号,密码 并非登录管理后台账号,密码)
    /// 
    function Execute: Boolean;
    property PhoneNumbers: string read FPhoneNumbers write FPhoneNumbers;
    property Report:string read FReport write FReport;
    property Content:string read FContent write FContent;
  end;
implementation

{ TSmsBaseRequest }

constructor TSmsBaseRequest.Create;
begin
  inherited;
  FClient := TNetHTTPClient.Create(nil);
  FClient.ContentType := 'application/json; charset=utf-8';
end;

destructor TSmsBaseRequest.Destroy;
begin
  inherited;
  FreeAndNil(FClient);
end;

function TSmsBaseRequest.ParseResponse(const Response: string): Boolean;
var
  JsonObject: TJSONObject;
begin
  //{"time": "20180327161820","msgId": "18032716182028886","errorMsg": "","code": "0"}
  Result := False;
  JsonObject := TJSONObject.ParseJSONValue(Response) as TJSONObject;
  try
    if JsonObject <> nil then
    begin
      FResultMessage := JsonObject.GetValue('errorMsg').Value;
      Result := JsonObject.GetValue('code').Value = '0';
    end
    else
      FResultMessage := Response;
  finally
    FreeAndNil(JsonObject);
  end;
end;

function TSmsBaseRequest.Post(const URL: string; const ASource: TStream): string;
begin
  Result := EmptyStr;
  try
    Result := FClient.Post(URL, ASource).ContentAsString(TEncoding.UTF8);
  except
    on E: Exception do
      FResultMessage := E.Message;
  end;
end;

function TSmsBaseRequest.SpecialUrlEncode(const Value: string): string;
begin
  Result := TNetEncoding.URL.Encode(Value).Replace('+', '%20').Replace('*', '%2A').Replace('%7E', '~');
end;

{ TSmsRequest }

constructor TSmsRequest.Create;
begin
  inherited;
end;

destructor TSmsRequest.Destroy;
begin
  inherited;
end;

function TSmsRequest.Execute: Boolean;
var
  AJson:TJSONObject;
  MemStream:TStream;
  mString: AnsiString;
begin
  Result := False;
  try
    AJson := TJSONObject.Create;
    AJson.AddPair('account', AccessKeyId);
    AJson.AddPair('password', AccessKeySecret);
    AJson.AddPair('msg', SpecialUrlEncode(Content));
    AJson.AddPair('phone', PhoneNumbers);
    AJson.AddPair('report', 'true');
    mString:=AJson.ToJSON;

    MemStream :=TMemoryStream.Create();
    MemStream.Write(PAnsiChar(mString)^, Length(mString));
    MemStream.Position :=0;

    Result := ParseResponse(Post(URL, MemStream));
  finally
    AJson.Free;
    MemStream.Free;
  end;
end;

end.

procedure TForm1.Button1Click(Sender: TObject);
var
  FSendSmsRequest: TSmsRequest;

begin
  FSendSmsRequest := TSmsRequest.Create;
  FSendSmsRequest.URL := 'http://xxxxx.com/msg/send/json';
  FSendSmsRequest.AccessKeyId := 'N2356172';
  FSendSmsRequest.AccessKeySecret := 'bTsnfNDWqH6ec6';
  FSendSmsRequest.PhoneNumbers := 'xxxx';
  Randomize;
  FSendSmsRequest.Content := '【xxxxx】您好,您的验证码是%%%%%%%%%%%%%<<<<<<<<<' + inttostr(Random(99999));
  Memo1.Lines.Add(FSendSmsRequest.Content);

  if FSendSmsRequest.Execute then
  begin
    Memo1.Lines.Add('短信发送成功!');
  end
  else
  begin
    Memo1.Lines.Add(FSendSmsRequest.ResultMessage);
  end;

end;

你可能感兴趣的:(Delphi)