221 lines
6.7 KiB
ObjectPascal
221 lines
6.7 KiB
ObjectPascal
unit json_webservice;
|
|
|
|
interface
|
|
|
|
uses REST.Client, REST.Types, System.JSON
|
|
,system.NetEncoding
|
|
, uSafeLog;
|
|
|
|
const
|
|
ACCESS_KEY = '58297a8c75e21d218b1da2c610b6f62c';
|
|
|
|
type
|
|
TKJSon = class
|
|
private
|
|
FP_ORG_CODE:string; //廠區
|
|
FP_WORK_NUM:string; //工號
|
|
FP_PC:string; //製程
|
|
FP_LINE:string; //線別
|
|
FP_LINE_NUM:string; //線別編號
|
|
FP_Lot:string; //批号
|
|
FP_Goods_Num:string; //料号
|
|
FP_Lot_Num:Integer; //批号数量
|
|
FP_Traceability_ID:double; //主表ID
|
|
FThreadId:Integer;
|
|
FFixture_Code:string; //治具ID
|
|
FP_Token:string;
|
|
FOnLine:boolean;
|
|
public
|
|
function REST_production_workOrder_user_profile(var sError:string):String;
|
|
function PostJSONWithREST_Login(var sError:string):String;
|
|
procedure ClearData(); //清除 廠區 工號 製程 線別 信息
|
|
constructor Create; virtual;
|
|
destructor Destroy; override;
|
|
end;
|
|
|
|
function KJSon():TKJSon;
|
|
|
|
var
|
|
FKJSon:TKJSon;
|
|
|
|
implementation
|
|
|
|
function TKJSon. PostJSONWithREST_Login(var sError:string):String;
|
|
var
|
|
RESTClient: TRESTClient;
|
|
RESTRequest: TRESTRequest;
|
|
RESTResponse: TRESTResponse;
|
|
|
|
JsonString: string;
|
|
JsonObject: TJsonObject;
|
|
begin
|
|
///production/workOrder/list //查询工单列表
|
|
/// api/login
|
|
RESTClient := TRESTClient.Create('http://iot.mrpiot.com:8019/login');
|
|
// RESTClient := TRESTClient.Create('http://192.168.5.9:8080/login');
|
|
RESTRequest := TRESTRequest.Create(nil);
|
|
RESTResponse := TRESTResponse.Create(nil);
|
|
try
|
|
sError:= '1';
|
|
RESTRequest.Client := RESTClient;
|
|
RESTRequest.Response := RESTResponse;
|
|
RESTRequest.Method := rmPOST;
|
|
// RESTRequest.Method := rmGET;
|
|
// RESTRequest.AddBody('{"key":"value"}', ctAPPLICATION_JSON);
|
|
// 修正Content-Type设置方式(移除URL编码)
|
|
RESTRequest.AddParameter('Content-Type', 'application/json',
|
|
TRESTRequestParameterKind.pkHTTPHEADER, [poDoNotEncode]);
|
|
RESTRequest.AddParameter('charset', 'UTF-8',
|
|
TRESTRequestParameterKind.pkHTTPHEADER, [poDoNotEncode]);
|
|
RESTRequest.AddParameter('access-control-allow-credentials', 'true',
|
|
TRESTRequestParameterKind.pkHTTPHEADER, [poDoNotEncode]);
|
|
RESTRequest.AddParameter('access-control-allow-origin', '192.168.5.9',
|
|
TRESTRequestParameterKind.pkHTTPHEADER, [poDoNotEncode]);
|
|
// 统一设置请求头
|
|
|
|
|
|
//access-control-allow-credentials: true
|
|
//access-control-allow-origin: http://120.229.200.252
|
|
//content-type: application/json;charset=UTF-8
|
|
|
|
// RESTRequest.AddAuthParameter('Authorization', 'Basic ' +
|
|
// TNetEncoding.Base64.Encode('dyp:123456'), TRESTRequestParameterKind.pkHTTPHEADER);
|
|
RESTRequest.AddBody('{"username":"dyp","password":"123456"}', ctAPPLICATION_JSON);
|
|
// RESTRequest.AddBody('{"password":"123456"}', ctAPPLICATION_JSON);
|
|
RESTRequest.Execute;
|
|
|
|
JsonString:= RESTResponse.Content;
|
|
|
|
// 输出完整响应信息
|
|
WorkLog.MessageInfo('状态码: %d, 响应: %s',
|
|
[RESTResponse.StatusCode, JsonString]);
|
|
|
|
// 将字符串解析为 TJsonObject
|
|
JsonObject := TJsonObject.ParseJSONValue(JsonString) as TJsonObject;
|
|
if JsonObject <> nil then
|
|
begin
|
|
FP_Token:= JsonObject.Values['token'].Value;
|
|
// 访问 JSON 对象中的数据
|
|
WorkLog.MessageInfo('token: ' + JsonObject.Values['token'].Value);
|
|
// WorkLog.MessageInfo('Age: ' + JsonObject.Get('age').Value);
|
|
end;
|
|
|
|
// except
|
|
// on E:Exception do
|
|
// WorkLog.MessageError('API调用异常: ' + E.Message);
|
|
// ShowMessage(RESTResponse.Content);
|
|
finally
|
|
RESTResponse.Free;
|
|
RESTRequest.Free;
|
|
RESTClient.Free;
|
|
end;
|
|
end;
|
|
|
|
function TKJSon. REST_production_workOrder_user_profile(var sError:string):String;
|
|
var
|
|
RESTClient: TRESTClient;
|
|
RESTRequest: TRESTRequest;
|
|
RESTResponse: TRESTResponse;
|
|
|
|
JsonString: string;
|
|
JsonObject: TJsonObject;
|
|
begin
|
|
if FP_Token='' then
|
|
begin
|
|
Result:='';
|
|
exit;
|
|
end;
|
|
|
|
///production/workOrder/list //查询工单列表
|
|
/// api/login
|
|
/// system/user/profile
|
|
RESTClient := TRESTClient.Create('http://192.168.5.9:8080/system/user/profile');
|
|
RESTRequest := TRESTRequest.Create(nil);
|
|
RESTResponse := TRESTResponse.Create(nil);
|
|
try
|
|
sError:= '1';
|
|
RESTRequest.Client := RESTClient;
|
|
RESTRequest.Response := RESTResponse;
|
|
// RESTRequest.Method := rmPOST;
|
|
RESTRequest.Method := rmGET;
|
|
// RESTRequest.AddBody('{"key":"value"}', ctAPPLICATION_JSON);
|
|
// 修正Content-Type设置方式(移除URL编码)
|
|
RESTRequest.AddParameter('Content-Type', 'application/json',
|
|
TRESTRequestParameterKind.pkHTTPHEADER, [poDoNotEncode]);
|
|
RESTRequest.AddParameter('charset', 'UTF-8',
|
|
TRESTRequestParameterKind.pkHTTPHEADER, [poDoNotEncode]);
|
|
RESTRequest.AddParameter('access-control-allow-credentials', 'true',
|
|
TRESTRequestParameterKind.pkHTTPHEADER, [poDoNotEncode]);
|
|
RESTRequest.AddParameter('access-control-allow-origin', '192.168.5.9',
|
|
TRESTRequestParameterKind.pkHTTPHEADER, [poDoNotEncode]);
|
|
// 统一设置请求头
|
|
RESTRequest.AddParameter('Authorization',FP_Token,
|
|
TRESTRequestParameterKind.pkHTTPHEADER, [poDoNotEncode]);
|
|
|
|
|
|
//access-control-allow-credentials: true
|
|
//access-control-allow-origin: http://120.229.200.252
|
|
//content-type: application/json;charset=UTF-8
|
|
|
|
// RESTRequest.AddAuthParameter('Authorization', 'Basic ' +
|
|
// TNetEncoding.Base64.Encode('dyp:123456'), TRESTRequestParameterKind.pkHTTPHEADER);
|
|
// RESTRequest.AddBody('{"username":"dyp","password":"123456"}', ctAPPLICATION_JSON);
|
|
// RESTRequest.AddBody('{"password":"123456"}', ctAPPLICATION_JSON);
|
|
RESTRequest.Execute;
|
|
|
|
JsonString:= RESTResponse.Content;
|
|
|
|
// 输出完整响应信息
|
|
WorkLog.MessageInfo('状态码: %d, 响应: %s',
|
|
[RESTResponse.StatusCode, JsonString]);
|
|
|
|
// 将字符串解析为 TJsonObject
|
|
JsonObject := TJsonObject.ParseJSONValue(JsonString) as TJsonObject;
|
|
if JsonObject <> nil then
|
|
begin
|
|
// FP_Token:= JsonObject.Values['token'].Value;
|
|
// 访问 JSON 对象中的数据
|
|
WorkLog.MessageInfo('postGroup: ' + JsonObject.Values['postGroup'].Value);
|
|
|
|
end;
|
|
|
|
// except
|
|
// on E:Exception do
|
|
// WorkLog.MessageError('API调用异常: ' + E.Message);
|
|
// ShowMessage(RESTResponse.Content);
|
|
finally
|
|
RESTResponse.Free;
|
|
RESTRequest.Free;
|
|
RESTClient.Free;
|
|
end;
|
|
end;
|
|
|
|
function KJSon():TKJSon;
|
|
begin
|
|
if FKJSon = nil then
|
|
FKJSon := TKJSon.Create;
|
|
Result := FKJSon;
|
|
end;
|
|
|
|
constructor TKJSon.Create;
|
|
begin
|
|
inherited;
|
|
ClearData();
|
|
end;
|
|
|
|
destructor TKJSon.Destroy;
|
|
begin
|
|
inherited Destroy;
|
|
end;
|
|
|
|
procedure TKJSon.ClearData(); //清除 廠區 工號 製程 線別 信息
|
|
begin
|
|
// P_ORG_CODE:='';
|
|
// P_WORK_NUM:='';
|
|
// P_PC:='';
|
|
// P_LINE:='';
|
|
// FP_LINE_NUM:='';
|
|
end;
|
|
|
|
end.
|