根据物流单号取得签收信息和运单联照片。
主要思路是建立socket对接某平台的开放api接口,得到response的json,用阿里巴巴的fastjson.jar解析回传json,物流签收信息回写到erp单据上; 回传照片是base64编码,这里是把base64存在临时txt文件上,然后用Linux命令把txt文件转成对应图片文件,保存在服务器上 # Prog. Version..: '5.25.03-11.07.14(00006)' # # # Pattern name...: aws_send_kywl.4gl # Descriptions...: # Date & Author..: 2022/11/03 By lipd 15547 # Usage..........: api接口发送post请求给跨越物流 IMPORT os IMPORT util IMPORT XML IMPORT JAVA java.lang.String IMPORT JAVA java.lang.Byte IMPORT JAVA java.net.URLEncoder IMPORT JAVA java.net.Socket IMPORT JAVA java.io.OutputStream IMPORT JAVA java.io.PrintWriter IMPORT JAVA java.io.BufferedReader IMPORT JAVA java.io.InputStreamReader IMPORT JAVA java.lang.StringBuffer IMPORT JAVA java.lang.Object IMPORT JAVA java.security.MessageDigest IMPORT JAVA java.lang.Integer IMPORT JAVA java.lang.Boolean IMPORT JAVA javax.net.ssl.SSLSocketFactory IMPORT JAVA java.lang.Character IMPORT JAVA java.lang.String IMPORT JAVA com.alibaba.fastjson.JSON IMPORT JAVA com.alibaba.fastjson.JSONArray IMPORT JAVA com.alibaba.fastjson.JSONObject IMPORT JAVA java.security.MessageDigest IMPORT JAVA org.apache.commons.codec.binary.Base64 DATABASE ds GLOBALS "../../../tiptop/config/top.global" DEFINE g_kywl_code STRING DEFINE g_kywl_msg STRING DEFINE g_erp_key STRING DEFINE g_erp_prog STRING DEFINE g_jke00 LIKE type_file.chr20 DEFINE g_send_str STRING DEFINE g_upd_str STRING DEFINE g_respone_str STRING DEFINE g_operateByName STRING DEFINE g_operateEndTime STRING DEFINE g_pickupTime STRING DEFINE g_goodsType STRING DEFINE g_count STRING DEFINE g_freightWeight STRING DEFINE g_serviceMode STRING DEFINE g_waybillDelivery_person STRING DEFINE g_waybillDelivery_mobile STRING DEFINE g_waybillDelivery_address STRING DEFINE g_waybillPickup_person STRING DEFINE g_waybillPickup_mobile STRING DEFINE g_waybillPickup_address STRING ###################################################### #p_erp_key erp单据主键值 ###################################################### FUNCTION aws_send_kywl(p_erp_key,p_erp_prog) DEFINE str_send STRING DEFINE p_erp_key STRING DEFINE p_erp_prog STRING DEFINE http_str STRING DEFINE li_len INTEGER DEFINE li_idx1 INTEGER DEFINE li_idx2 INTEGER DEFINE ls_line STRING DEFINE ls_res_body STRING DEFINE log_err_str STRING DEFINE req_timestamp STRING DEFINE req_appsecret STRING DEFINE req_format STRING DEFINE req_method STRING DEFINE req_sign STRING DEFINE req_appkey STRING DEFINE req_token STRING DEFINE lj_str java.lang.String DEFINE lb_str base.StringBuffer DEFINE lb_isread BOOLEAN DEFINE lj_socket java.net.Socket DEFINE lj_os java.io.OutputStream DEFINE lj_out java.io.PrintWriter DEFINE lj_isr java.io.InputStreamReader DEFINE lj_in java.io.BufferedReader DEFINE lj_sb java.lang.StringBuffer DEFINE lj_int INT DEFINE lj_s STRING DEFINE lj_type CHAR DEFINE lj_count INT DEFINE l_timestamp LIKE type_file.chr100 DEFINE l_erp_key LIKE type_file.chr100 #erp单号 DEFINE l_kywl_fey LIKE type_file.chr100 #快递单号 DEFINE l_str STRING LET g_send_str = '' LET g_kywl_code = '' #回传状态码 LET g_kywl_msg = '' #回传提示 LET g_operateByName = '' #签收人姓名 LET g_operateEndTime = '' #签收时间 # ### # LET p_erp_prog = 'axmt620_icd_2' # LET p_erp_key = 'S301-2209010001' # ### LET g_success = 'Y' IF cl_null(p_erp_prog) OR cl_null(p_erp_key) THEN LET g_success = 'N' RETURN END IF LET g_erp_prog = p_erp_prog LET g_erp_key = p_erp_key LET l_erp_key = g_erp_key CASE p_erp_prog WHEN 'axmt620_icd' SELECT ta_ogaud19 INTO l_kywl_fey FROM oga_file WHERE oga01 = l_erp_key WHEN 'axmt620_icd_2' SELECT ta_ogaud19 INTO l_kywl_fey FROM oga_file WHERE oga01 = l_erp_key OTHERWISE LET g_success = 'N' CALL cl_err('暂无'||p_erp_prog||'接口程序','!',1) RETURN END CASE IF cl_null(l_kywl_fey) THEN LET g_success = 'N' CALL cl_err('单据无跨越快递单号','!',1) RETURN END IF #准备请求头数据 SELECT TO_CHAR(ROUND((SYSDATE - TO_DATE('1970-1-1 8', 'YYYY-MM-DD HH24')) * 86400,0))||'000' #时间戳 INTO l_timestamp FROM DUAL LET req_timestamp = l_timestamp #时间戳 LET req_appkey = "xxxx" #应用key LET req_format = "json" #回传报文格式 CASE g_erp_prog WHEN 'axmt620_icd' LET req_method = "open.api.openCommon.getWaybillBaseInfo" #请求APIcode #请求报文body (customerCode固定xxxx) LET str_send = '{"customerCode":"xxxx","waybillNumber":["',l_kywl_fey,'"]}' WHEN 'axmt620_icd_2' LET req_method = "open.api.openCommon.queryWaybillPicture" #请求APIcode #请求报文body (customerCode固定xxxx) LET str_send = '{"customerCode":"xxxx","waybillNumber":"',l_kywl_fey,'","pictureType":"10"}' END CASE LET req_appsecret = "xxxx" #密钥 LET req_sign = req_appsecret,req_timestamp,str_send #签名 LET req_sign = aws_send_kywl_MD5(req_sign) #签名MD5加密 IF g_success = 'N' THEN CALL cl_err('请求头签名MD5加密失败,请联系支持部!','!',1) RETURN END IF LET req_sign = req_sign.toUpperCase() #签名String转大写 LET req_token = aws_send_kywl_token() IF g_success = 'N' THEN CALL cl_err('获取请求头的token出错,请联系支持部!','!',1) RETURN END IF # LET req_token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJYLXVzZXJuYW1lIjoiODE2MDciLCJYLXVpZCI6IjgxNjA3IiwianRpIjoiNjllZTlkMWYtN2M2OS00MThkLWFmOWMtY2I5Y2RmNDhlNTc1In0.kqwX-uo0_DpT-FHFTX5llG_8eNW3IsHDEEyO8UEzfh8IN2qikgFUdemiQjFb_2Pqc7SQQCNVUI5iW6MTWmCIdBHNrsu0MsKlCyCCBQGGt4dgrwSVCBI4bLTYCGVz-gaJ8ve3p5C6eYD_yPFl_RRvQ6WnNjte3-6yMEXmVcI6WgY" LET lb_str = base.StringBuffer.create() LET lb_isread = FALSE CALL lb_str.append("POST /router/rest HTTP/1.1\r\n") CALL lb_str.append("Host: open.ky-express.com\r\n") CALL lb_str.append("Content-Type: application/json;charset=UTF-8"||"\r\n") # http content type CALL lb_str.append("appkey: "||req_appkey||"\r\n") CALL lb_str.append("format: "||req_format||"\r\n") CALL lb_str.append("timestamp: "||req_timestamp||"\r\n") CALL lb_str.append("method: "||req_method||"\r\n") CALL lb_str.append("sign: "||req_sign||"\r\n") CALL lb_str.append("token: "||req_token||"\r\n") CALL lb_str.append("Content-Length: "||str_send.getLength()||"\r\n") CALL lb_str.append("\r\n") # header end; CALL lb_str.append(str_send|| "\r\n") LET http_str = lb_str.toString() LET g_send_str = g_send_str,http_str #全局send_str,记录在log LET lj_str = String.create(http_str) # 建立 socket LET lj_socket = SSLSocketFactory.getDefault().createSocket("212.64.63.137", 443) #https # LET lj_socket = Socket.create("212.64.63.137",80) #http #link.syncrock.com link.syncpo.com CALL lj_socket.setSoTimeout(5000) #set after 5 second timeout; LET lj_os = lj_socket.getOutputStream() #取得串流 LET lj_out = PrintWriter.create(lj_os, true) LET lj_isr = InputStreamReader.create(lj_socket.getInputStream()) LET lj_in = BufferedReader.create(lj_isr); CALL lj_out.println(lj_str) #送出request # 取得回传(response) LET lj_sb = StringBuffer.create("") LET li_len = 0 LET li_idx1 = 0 LET lb_isread = TRUE LET lj_type = 'Y' LET lj_count = 0 IF g_erp_prog = 'axmt620_icd' THEN WHILE lb_isread TRY LET lj_int = lj_in.read() #https请求回传的数据最后一行无结束符号,字符流读取readLine()改成read(),读到最后一个json的'}'就结束循环 IF (lj_int = -1) THEN LET lb_isread = FALSE EXIT WHILE END IF # IF lj_int <= 0 THEN # CONTINUE WHILE # END IF LET lj_s = String.valueOf(Character.toChars(lj_int)) #read()返回的是int,int强制转换str IF lj_s = '{' THEN LET lj_count = lj_count + 1 LET lj_type = 'Y' END IF IF lj_s = '}' THEN LET lj_count = lj_count - 1 #第一个'{',结束 IF lj_count = 0 THEN LET lj_type = 'N' END IF END IF CALL lj_sb.append(lj_s) IF lj_count = 0 AND lj_type = 'N' THEN LET lb_isread = FALSE EXIT WHILE END IF LET li_idx1 = li_idx1 + 1 IF li_idx1 > 2000 THEN #暂定2000个字符限制 LET lb_isread = FALSE EXIT WHILE END IF CATCH CALL lj_sb.append(lj_str) LET lb_isread = FALSE EXIT WHILE END TRY END WHILE ELSE WHILE lb_isread TRY LET ls_line = lj_in.readLine() IF (ls_line IS NULL) THEN LET lb_isread = FALSE EXIT WHILE END IF IF ls_line <> 0 THEN CONTINUE WHILE END IF LET l_str = ls_line IF l_str.getLength() <= 10 THEN CONTINUE WHILE END IF CALL lj_sb.append(ls_line) LET li_idx1 = li_idx1 + 1 IF li_idx1 > 1000 THEN #暂定限制一千行 LET lb_isread = FALSE EXIT WHILE END IF CATCH CALL lj_sb.append(ls_line) LET lb_isread = FALSE EXIT WHILE END TRY END WHILE END IF CALL lj_in.close() CALL lj_socket.close() LET li_idx1 = lj_sb.length() IF (li_idx1 > 0) THEN LET li_idx1 = lj_sb.indexOf("{") #第一个 LET li_idx2 = lj_sb.lastIndexOf("}")+1 #最后一个 IF (li_idx1 > 0) AND (li_idx2 > 1) THEN #get http response body content; LET ls_res_body = lj_sb.substring(li_idx1,li_idx2) #获取{...}json CASE g_erp_prog WHEN 'axmt620_icd' CALL aws_kywl_json_resolution(ls_res_body) WHEN 'axmt620_icd_2' CALL aws_kywl_json_resolution2(ls_res_body) END CASE LET g_respone_str = g_respone_str,ls_res_body ELSE LET g_success = 'N' END IF ELSE LET g_success = 'N' END IF IF (g_kywl_code = '0' OR g_kywl_code = '10000') AND g_success = 'Y' THEN #查询成功 IF g_bgjob = 'Y' THEN CALL aws_send_kywl_update() ELSE CASE p_erp_prog WHEN 'axmt620_icd' IF NOT cl_confirm(g_kywl_msg||',签收人:'||g_operateByName||',签收时间:'||g_operateEndTime||',是否更新签收信息于ERP单据中?') THEN LET g_success = 'N' RETURN END IF OTHERWISE EXIT CASE END CASE CALL aws_send_kywl_update() IF g_success = 'N' THEN RETURN END IF END IF ELSE LET g_success = 'N' IF NOT cl_null(g_kywl_msg) THEN CALL cl_err("查询失败!"||g_kywl_msg,'!',1) ELSE CALL cl_err("查询失败!",'!',1) END IF END IF RETURN END FUNCTION #################### #解析json #################### FUNCTION aws_kywl_json_resolution(p_json_str) DEFINE json_resolution RECORD code STRING, msg STRING, success STRING, data JSONArray END RECORD DEFINE p_json_str STRING DEFINE json_str STRING DEFINE operateByName STRING DEFINE operateEndTime STRING DEFINE i INTEGER DEFINE json_obj JSONObject DEFINE json_obj_2 JSONObject DEFINE pickupTime STRING DEFINE goodsType STRING DEFINE count STRING DEFINE freightWeight STRING DEFINE serviceMode STRING DEFINE waybillDelivery STRING DEFINE waybillPickup STRING DEFINE json_obj_3 JSONObject DEFINE json_obj_4 JSONObject DEFINE waybillDelivery_person STRING DEFINE waybillDelivery_mobile STRING DEFINE waybillDelivery_address STRING DEFINE waybillPickup_person STRING DEFINE waybillPickup_mobile STRING DEFINE waybillPickup_address STRING LET json_str = p_json_str IF cl_null(json_str) THEN LET g_success = 'N' RETURN END IF LET json_obj = com.alibaba.fastjson.JSON.parseObject(json_str) TRY LET json_resolution.code = json_obj.getString("code") LET json_resolution.msg = json_obj.getString("msg") LET json_resolution.success = json_obj.getString("success") LET json_resolution.data = json_obj.getJSONArray("data") CATCH LET g_success = 'N' RETURN END TRY LET g_kywl_code = json_resolution.code LET g_kywl_msg = json_resolution.msg IF json_resolution.code <> '0' OR cl_null(json_resolution.msg) THEN LET g_success = 'N' RETURN END IF FOR i = 0 TO json_resolution.data.size()-1 #理论上只会有一笔 LET json_obj_2 = json_resolution.data.getJSONObject(i) LET operateByName = json_obj_2.getString("operateByName") #签收人 LET operateEndTime = json_obj_2.getString("operateEndTime") #签收时间 LET pickupTime = json_obj_2.getString("pickupTime") #寄件时间 LET goodsType = json_obj_2.getString("goodsType") #托寄物 LET count = json_obj_2.getString("count") #件数 LET freightWeight = json_obj_2.getString("freightWeight") #计费重量 LET serviceMode = json_obj_2.getString("serviceMode") #服务方式 LET waybillDelivery = json_obj_2.getString("waybillDelivery") #寄件信息 LET waybillPickup = json_obj_2.getString("waybillPickup") #收件信息 END FOR LET json_obj_3 = com.alibaba.fastjson.JSON.parseObject(waybillDelivery) TRY LET waybillDelivery_person = json_obj_3.getString("person") LET waybillDelivery_mobile = json_obj_3.getString("mobile") LET waybillDelivery_address = json_obj_3.getString("address") CATCH LET waybillDelivery_person = '' LET waybillDelivery_mobile = '' LET waybillDelivery_address = '' END TRY LET json_obj_4 = com.alibaba.fastjson.JSON.parseObject(waybillPickup) TRY LET waybillPickup_person = json_obj_4.getString("person") LET waybillPickup_mobile = json_obj_4.getString("mobile") LET waybillPickup_address = json_obj_4.getString("address") CATCH LET waybillPickup_person = '' LET waybillPickup_mobile = '' LET waybillPickup_address = '' END TRY IF cl_null(operateByName) OR cl_null(operateEndTime) THEN LET g_kywl_msg = '暂无签收信息' LET g_success = 'N' RETURN END IF LET g_operateByName = operateByName LET g_operateEndTime = operateEndTime LET g_pickupTime = pickupTime LET g_goodsType = goodsType LET g_count = count LET g_freightWeight = freightWeight LET g_serviceMode = serviceMode LET g_waybillDelivery_person = waybillDelivery_person LET g_waybillDelivery_mobile = waybillDelivery_mobile LET g_waybillDelivery_address = waybillDelivery_address LET g_waybillPickup_person = waybillPickup_person LET g_waybillPickup_mobile = waybillPickup_mobile LET g_waybillPickup_address = waybillPickup_address END FUNCTION FUNCTION aws_send_kywl_MD5(l_str) DEFINE l_str java.lang.String #要加密的明文 DEFINE l_digest java.security.MessageDigest #加密演算法物件 DEFINE l_ength INTEGER #加密後長度(應該為16 Byte) DEFINE l_i SMALLINT DEFINE l_byte java.lang.Byte #加密後每一個Byte值(有可能為負數) DEFINE l_encrypt STRING #以MD5來說加密後就是32位16進制的數值字串 DEFINE l_tmp STRING DEFINE l_int INTEGER LET l_encrypt = "" IF l_str IS NULL THEN display "Encryption string is null." LET g_success = 'N' RETURN END IF TRY #指定加密演算法 LET l_digest = java.security.MessageDigest.getInstance("MD5") #將字串轉換成byte,並傳送要演算的字串 CALL l_digest.update(l_str.getBytes()) #原則上MD5加密後長度應該會是16 bytes LET l_ength = l_digest.getDigestLength() #因為在java裡無法做byte與java.lang.Byte的型態轉換 #而且在4gl code中又無法引用java裡byte這個基本型態 #因此只能利用for迴圈逐一抓出byte的值,順便做16進制的轉換 FOR l_i = 1 TO l_ength #逐一取出每一個byte加密後的值 LET l_byte = java.lang.Byte.valueOf(l_digest.digest()[l_i]) #DISPLAY "digest[", l_i, "]: ", l_byte, ";" #java的toHexString參數是int,所以這裡還需做型態轉換 #心得:利用4gl寫java要特別注意型態,一但型態錯誤就無法做api的呼叫 LET l_int = l_byte.intValue() #因為每一個byte加密後的值有可能是負數,所以需做補數轉換 #java code本是這樣做:(l_byte & 0XFF)就可以了 #4gl code就直接將負數加256吧(因為1個byte是8 bit,所以要加上2的8次方=256) IF l_byte < 0 THEN LET l_int = l_byte.intValue() + 256 ELSE LET l_int = l_byte.intValue() END IF #進行16進位轉碼,因為需固定取出二位字串故先在前面多加個"0"字串 #等後面再做subString取出二個長度的字串 LET l_tmp = java.lang.Integer.toHexString(l_int) LET l_tmp = "0", l_tmp #取出二位字串 #範例1,有可能出來的是0: "0", "0" = "00" ===> subString(1, 2) #範例2,有可能出來的是FF: "0", "FF" = "0FF" ===> subString(2, 3) LET l_tmp = l_tmp.subString(l_tmp.getLength() - 1, l_tmp.getLength()) #組合加密後之16進制的32位元數值字串 LET l_encrypt = l_encrypt, l_tmp #重新傳送要演算的字串 #要這樣做的原因就在於在for圈裡會再執行l_digest.digest() #呼叫digest()時會重新再一次用演算法進行加密 #這樣下一個再取出的byte值就會和第一次計算的不一樣 #所以這裡要再重新傳送要演算的字串 CALL l_digest.update(l_str.getBytes()) END FOR DISPLAY "Encrypt Finish." CATCH LET l_encrypt = "" DISPLAY "Error:", STATUS display "Generate MD5 key failed." LET g_success = 'N' RETURN END TRY RETURN l_encrypt END FUNCTION ############################################## #跨越物流平台的api接口,请求头的token是有时效的, #一般为两天,现在规定为隔天去对应token的api接口刷新一次token #用表的appkey和appsecret去刷新,得到新的token和refresh_token #同时更新到数据库的tc_kywl_file ############################################## FUNCTION aws_send_kywl_token() DEFINE l_tc_kywl02 LIKE tc_kywl_file.tc_kywl02 #token DEFINE l_tc_kywl03 LIKE tc_kywl_file.tc_kywl03 #refresh_token DEFINE l_tc_kywl04 LIKE tc_kywl_file.tc_kywl04 #expire_time DEFINE l_tc_kywl05 LIKE tc_kywl_file.tc_kywl05 #refresh_date DEFINE str_send STRING DEFINE http_str STRING DEFINE li_len INTEGER DEFINE li_idx1 INTEGER DEFINE li_idx2 INTEGER DEFINE ls_line STRING DEFINE ls_res_body STRING DEFINE lj_str java.lang.String DEFINE lb_str base.StringBuffer DEFINE lb_isread BOOLEAN DEFINE lj_socket java.net.Socket DEFINE lj_os java.io.OutputStream DEFINE lj_out java.io.PrintWriter DEFINE lj_isr java.io.InputStreamReader DEFINE lj_in java.io.BufferedReader DEFINE lj_sb java.lang.StringBuffer DEFINE lj_int INT DEFINE lj_s STRING DEFINE lj_type CHAR DEFINE lj_count INT SELECT tc_kywl02,tc_kywl03,tc_kywl04,tc_kywl05 INTO l_tc_kywl02,l_tc_kywl03,l_tc_kywl04,l_tc_kywl05 FROM tc_kywl_file WHERE tc_kywl01 = '001' IF STATUS THEN LET g_success = 'N' RETURN '' END IF IF cl_null(l_tc_kywl02) OR cl_null(l_tc_kywl03) OR cl_null(l_tc_kywl04) OR cl_null(l_tc_kywl05) THEN LET g_success = 'N' RETURN '' END IF #l_tc_kywl04是有效秒数,当天减去上次更新日期小于等于有效天数的一半,就return token IF g_today - l_tc_kywl05 <= l_tc_kywl04/60/60/24/2 THEN RETURN l_tc_kywl02 END IF LET lb_str = base.StringBuffer.create() LET lb_isread = FALSE # LET str_send = '{ "refresh_token": "',l_tc_kywl03,'"}' LET str_send = '{"appkey":"xxxx","appsecret": "xxxx"}' # CALL lb_str.append("POST /security/token/refresh HTTP/1.1\r\n") CALL lb_str.append("POST /security/token HTTP/1.1\r\n") CALL lb_str.append("Host: open.ky-express.com\r\n") CALL lb_str.append("Content-Type: application/json;charset=UTF-8"||"\r\n") # http content type CALL lb_str.append("Content-Length: "||str_send.getLength()||"\r\n") CALL lb_str.append("\r\n") # header end; CALL lb_str.append(str_send|| "\r\n") LET http_str = lb_str.toString() LET g_send_str = http_str,"\r\n\r\n" #全局send_str,记录在log LET lj_str = String.create(http_str) # 建立 socket LET lj_socket = SSLSocketFactory.getDefault().createSocket("212.64.63.137", 443) # LET lj_socket = Socket.create("212.64.63.137",80) #link.syncrock.com link.syncpo.com CALL lj_socket.setSoTimeout(5000) #set after 5 second timeout; LET lj_os = lj_socket.getOutputStream() #取得串流 LET lj_out = PrintWriter.create(lj_os, true) LET lj_isr = InputStreamReader.create(lj_socket.getInputStream(),'UTF-8') LET lj_in = BufferedReader.create(lj_isr); CALL lj_out.println(lj_str) #送出request # 取得回传(response) LET lj_sb = StringBuffer.create("") LET li_len = 0 LET li_idx1 = 0 LET lb_isread = TRUE LET lj_type = 'Y' LET lj_count = 0 WHILE lb_isread TRY LET lj_int = lj_in.read() #https请求回传的数据最后一行无结束符号,字符流读取readLine()改成read(),读到最后一个json的'}'就结束循环 IF (lj_int = -1) THEN LET lb_isread = FALSE EXIT WHILE END IF # IF lj_int <= 0 THEN # CONTINUE WHILE # END IF LET lj_s = String.valueOf(Character.toChars(lj_int)) #read()返回的是int,int强制转换str IF lj_s = '{' THEN LET lj_count = lj_count + 1 LET lj_type = 'Y' END IF IF lj_s = '}' THEN LET lj_count = lj_count - 1 #第一个'{',结束 IF lj_count = 0 THEN LET lj_type = 'N' END IF END IF CALL lj_sb.append(lj_s) IF lj_count = 0 AND lj_type = 'N' THEN LET lb_isread = FALSE EXIT WHILE END IF LET li_idx1 = li_idx1 + 1 IF li_idx1 > 2000 THEN #暂定2000个字符限制 LET lb_isread = FALSE EXIT WHILE END IF CATCH CALL lj_sb.append(lj_str) LET lb_isread = FALSE EXIT WHILE END TRY END WHILE CALL lj_in.close() CALL lj_socket.close() LET li_idx1 = lj_sb.length() IF (li_idx1 > 0) THEN LET li_idx1 = lj_sb.indexOf("{") #第一个 LET li_idx2 = lj_sb.lastIndexOf("}")+1 #最后一个 IF (li_idx1 > 0) AND (li_idx2 > 1) THEN #get http response body content; LET ls_res_body = lj_sb.substring(li_idx1,li_idx2) #获取{...}json LET l_tc_kywl02 = '' CALL aws_kywl_json_resolution_token(ls_res_body) RETURNING l_tc_kywl02 LET g_respone_str = ls_res_body,"\r\n\r\n" IF g_success = 'N' OR cl_null(l_tc_kywl02) THEN LET g_success = 'N' RETURN '' END IF RETURN l_tc_kywl02 ELSE LET g_success = 'N' RETURN '' END IF ELSE LET g_success = 'N' RETURN '' END IF END FUNCTION ###################################### #解析刷新token的api接口回传的json ###################################### FUNCTION aws_kywl_json_resolution_token(p_json_str) DEFINE json_resolution RECORD code STRING, msg STRING, data STRING, traceId STRING, success STRING END RECORD DEFINE p_json_str STRING DEFINE json_str STRING DEFINE requestid STRING DEFINE token STRING DEFINE expire_time STRING DEFINE refresh_token STRING DEFINE i INTEGER DEFINE json_obj JSONObject DEFINE json_obj_2 JSONObject DEFINE l_tc_kywl02 LIKE tc_kywl_file.tc_kywl02 #token DEFINE l_tc_kywl03 LIKE tc_kywl_file.tc_kywl03 #refresh_token DEFINE l_tc_kywl04 LIKE tc_kywl_file.tc_kywl04 #expire_time LET json_str = p_json_str IF cl_null(json_str) THEN LET g_success = 'N' RETURN '' END IF LET token = '' LET expire_time = '' LET refresh_token = '' LET json_obj = com.alibaba.fastjson.JSON.parseObject(json_str) TRY LET json_resolution.code = json_obj.getString("code") LET json_resolution.msg = json_obj.getString("msg") LET json_resolution.traceId = json_obj.getString("traceId") LET json_resolution.success = json_obj.getString("success") LET json_resolution.data = json_obj.getString("data") LET json_obj_2 = com.alibaba.fastjson.JSON.parseObject(json_resolution.data) LET token = json_obj_2.getString("token") LET expire_time = json_obj_2.getString("expire_time") LET refresh_token = json_obj_2.getString("refresh_token") CATCH LET g_success = 'N' RETURN '' END TRY IF json_resolution.code <> '0' OR json_resolution.msg <> 'OK' THEN LET g_success = 'N' RETURN '' END IF IF cl_null(token) OR cl_null(expire_time) OR cl_null(refresh_token) THEN LET g_success = 'N' RETURN '' END IF LET l_tc_kywl02 = token LET l_tc_kywl03 = refresh_token LET l_tc_kywl04 = expire_time UPDATE tc_kywl_file SET tc_kywl02 = l_tc_kywl02, tc_kywl03 = l_tc_kywl03, tc_kywl04 = l_tc_kywl04, tc_kywl05 = g_today, tc_kywldate = g_today, tc_kywlmodu = g_user WHERE tc_kywl01 = '001' IF SQLCA.sqlcode OR SQLCA.sqlerrd[3] = 0 THEN LET g_success = 'N' RETURN '' END IF LET g_upd_str = "UPDATE tc_kywl_file SET tc_kywl02 = '"||l_tc_kywl02||"' ", " tc_kywl03 = '"||l_tc_kywl03||"',", " tc_kywl04 = "||l_tc_kywl04||",", " tc_kywl05 = "||g_today||",", " tc_kywldate = "||g_today||",", " tc_kywlmodu = '"||g_user||"',", " WHERE tc_kywl01 = '001' ", "\r\n\r\n" RETURN l_tc_kywl02 END FUNCTION FUNCTION aws_send_kywl_update() DEFINE l_erp_key LIKE type_file.chr100 #erp主键 DEFINE l_operateEndTime LIKE type_file.chr100 DEFINE l_ta_ogaud16 LIKE oga_file.ta_ogaud16 DEFINE l_timestamp LIKE type_file.chr30 DEFINE send_str TEXT DEFINE upd_str TEXT DEFINE respone_str TEXT DEFINE l_jke11 LIKE type_file.chr500 DEFINE l_jke10 LIKE type_file.chr500 DEFINE l_operateByName LIKE oga_file.ta_ogaud33 DEFINE l_ta_ogaud26 LIKE oga_file.ta_ogaud26 #ta_ogaud26 寄件时间 pickupTime DEFINE l_ta_ogaud27 LIKE oga_file.ta_ogaud27 #ta_ogaud27 寄件人 person DEFINE l_ta_ogaud28 LIKE oga_file.ta_ogaud28 #ta_ogaud28 寄件人联系方式 mobile DEFINE l_ta_ogaud29 LIKE oga_file.ta_ogaud29 #ta_ogaud29 寄件地址 address DEFINE l_ta_ogaud30 LIKE oga_file.ta_ogaud30 #ta_ogaud30 收件人 person DEFINE l_ta_ogaud31 LIKE oga_file.ta_ogaud31 #ta_ogaud31 收件人联系方式 mobile DEFINE l_ta_ogaud32 LIKE oga_file.ta_ogaud32 #ta_ogaud32 收件地址 address DEFINE l_ta_ogaud33 LIKE oga_file.ta_ogaud33 #ta_ogaud33 签收人 operateByName DEFINE l_ta_ogaud34 LIKE oga_file.ta_ogaud34 #ta_ogaud34 签收时间 operateEndTime DEFINE l_ta_ogaud35 LIKE oga_file.ta_ogaud35 #ta_ogaud35 托寄物 goodsType DEFINE l_ta_ogaud36 LIKE oga_file.ta_ogaud36 #ta_ogaud36 件数 count DEFINE l_ta_ogaud37 LIKE oga_file.ta_ogaud37 #ta_ogaud37 计费重量(kg) freightWeight DEFINE l_ta_ogaud38 LIKE oga_file.ta_ogaud38 #ta_ogaud38 服务方式 serviceMode BEGIN WORK LET l_erp_key = g_erp_key LET l_operateEndTime = g_operateEndTime LET l_ta_ogaud26 = g_pickupTime #寄件时间 LET l_ta_ogaud27 = g_waybillDelivery_person #寄件人 LET l_ta_ogaud28 = g_waybillDelivery_mobile #寄件人联系方式 LET l_ta_ogaud29 = g_waybillDelivery_address #寄件地址 LET l_ta_ogaud30 = g_waybillPickup_person #收件人 LET l_ta_ogaud31 = g_waybillPickup_mobile #收件人联系方式 LET l_ta_ogaud32 = g_waybillPickup_address #收件地址 LET l_ta_ogaud33 = g_operateByName #签收人 LET l_ta_ogaud34 = g_operateEndTime #签收时间 LET l_ta_ogaud35 = g_goodsType #托寄物 LET l_ta_ogaud36 = g_count #件数 LET l_ta_ogaud37 = g_freightWeight #计费重量(kg) LET l_ta_ogaud38 = g_serviceMode #服务方式 CASE g_erp_prog WHEN 'axmt620_icd' SELECT ta_ogaud16 INTO l_ta_ogaud16 FROM oga_file WHERE oga01 = l_erp_key IF cl_null(l_ta_ogaud16) THEN SELECT TO_DATE(l_operateEndTime,'YYYY-MM-DD HH24:MI:SS') INTO l_ta_ogaud16 FROM DUAL IF cl_null(l_ta_ogaud16) THEN LET g_success = 'N' ROLLBACK WORK RETURN END IF UPDATE oga_file SET ta_ogaud16 = l_ta_ogaud16, ta_ogaud26 = l_ta_ogaud26, ta_ogaud27 = l_ta_ogaud27, ta_ogaud28 = l_ta_ogaud28, ta_ogaud29 = l_ta_ogaud29, ta_ogaud30 = l_ta_ogaud30, ta_ogaud31 = l_ta_ogaud31, ta_ogaud32 = l_ta_ogaud32, ta_ogaud33 = l_ta_ogaud33, ta_ogaud34 = l_ta_ogaud34, ta_ogaud35 = l_ta_ogaud35, ta_ogaud36 = l_ta_ogaud36, ta_ogaud37 = l_ta_ogaud37, ta_ogaud38 = l_ta_ogaud38 WHERE oga01 = l_erp_key IF SQLCA.sqlcode OR SQLCA.sqlerrd[3] = 0 THEN LET g_success = 'N' ROLLBACK WORK CALL cl_err('单据更新失败!','!',1) RETURN END IF ELSE UPDATE oga_file SET ta_ogaud26 = l_ta_ogaud26, ta_ogaud27 = l_ta_ogaud27, ta_ogaud28 = l_ta_ogaud28, ta_ogaud29 = l_ta_ogaud29, ta_ogaud30 = l_ta_ogaud30, ta_ogaud31 = l_ta_ogaud31, ta_ogaud32 = l_ta_ogaud32, ta_ogaud33 = l_ta_ogaud33, ta_ogaud34 = l_ta_ogaud34, ta_ogaud35 = l_ta_ogaud35, ta_ogaud36 = l_ta_ogaud36, ta_ogaud37 = l_ta_ogaud37, ta_ogaud38 = l_ta_ogaud38 WHERE oga01 = l_erp_key IF SQLCA.sqlcode OR SQLCA.sqlerrd[3] = 0 THEN LET g_success = 'N' ROLLBACK WORK CALL cl_err('单据更新失败!','!',1) RETURN END IF END IF LET g_upd_str = g_upd_str,"UPDATE oga_file SET ta_ogaud16 = "||l_ta_ogaud16||" WHERE oga01 = '"||l_erp_key||"' " OTHERWISE ROLLBACK WORK RETURN END CASE #暂时mark写日志,不太需要 # #写日志 # PREPARE aws_kywl_jke00 FROM "SELECT LPAD(COUNT(1) + 1, 9, '0') || SUBSTR(DBMS_RANDOM.VALUE, 2, 11) FROM JKE_FILE " # EXECUTE aws_kywl_jke00 INTO g_jke00 #使用全局变量记录日志主键 # IF SQLCA.sqlcode THEN # ROLLBACK WORK # LET g_success = 'N' # CALL cl_err('数据写入日志失败,请联系支持部!','!',1) # RETURN # END IF # # SELECT to_char(SYSTIMESTAMP,'yyyymmdd HH24:MI:SS.FF') INTO l_timestamp FROM dual # PREPARE aws_kywl_insjke FROM "INSERT INTO JKE_FILE(JKE00, JKE01, JKE02, JKE03) VALUES('"||g_jke00||"', to_timestamp('"||l_timestamp||"','yyyy-mm-dd hh24:mi:ss.ff'),SYSTIMESTAMP,'send_kywl')" # EXECUTE aws_kywl_insjke # IF SQLCA.sqlcode THEN # LET g_success = 'N' # ROLLBACK WORK # CALL cl_err('数据写入日志失败,请联系支持部!','!',1) # RETURN # END IF # # LOCATE send_str IN FILE # LET send_str = g_send_str # UPDATE jke_file SET jke04 = send_str, JKE02 = SYSTIMESTAMP WHERE jke00 = g_jke00 # IF SQLCA.sqlcode THEN # LET g_success = 'N' # ROLLBACK WORK # CALL cl_err('数据写入日志失败,请联系支持部!','!',1) # RETURN # END IF # # LOCATE upd_str IN FILE # LOCATE respone_str IN FILE # LET upd_str = g_upd_str # LET respone_str = g_respone_str # LET l_jke10 = g_kywl_code # LET l_jke11 = g_kywl_msg # UPDATE jke_file # SET jke08 = upd_str,jke09 = respone_str, # jke10 = l_jke10,jke11 = l_jke11 # WHERE jke00 = g_jke00 # IF SQLCA.sqlcode THEN # LET g_success = 'N' # ROLLBACK WORK # CALL cl_err('数据写入日志失败,请联系支持部!','!',1) # RETURN # END IF IF g_success = 'Y' THEN COMMIT WORK ELSE ROLLBACK WORK RETURN END IF END FUNCTION #################### #解析json #################### FUNCTION aws_kywl_json_resolution2(p_json_str) DEFINE json_resolution RECORD code STRING, msg STRING, success STRING, data STRING END RECORD DEFINE json_resolution_2 RECORD waybillNumber STRING, filePictureInfoRes JSONArray END RECORD DEFINE p_json_str STRING DEFINE json_str STRING DEFINE extendName STRING DEFINE originalName STRING DEFINE size STRING DEFINE picture STRING DEFINE i INTEGER DEFINE json_obj JSONObject DEFINE json_obj_2 JSONObject DEFINE json_obj_3 JSONObject DEFINE path STRING DEFINE filename STRING DEFINE l_cmd STRING DEFINE l_result INT DEFINE unix_path STRING DEFINE window_path STRING DEFINE l_str STRING LET json_str = p_json_str IF cl_null(json_str) THEN LET g_success = 'N' RETURN END IF LET json_obj = com.alibaba.fastjson.JSON.parseObject(json_str) TRY LET json_resolution.code = json_obj.getString("code") LET json_resolution.msg = json_obj.getString("msg") LET json_resolution.success = json_obj.getString("success") LET json_resolution.data = json_obj.getString("data") CATCH LET g_success = 'N' RETURN END TRY LET json_obj_2 = com.alibaba.fastjson.JSON.parseObject(json_resolution.data) TRY LET json_resolution_2.waybillNumber = json_obj_2.getString("waybillNumber") LET json_resolution_2.filePictureInfoRes = json_obj_2.getJSONArray("filePictureInfoRes") CATCH LET g_success = 'N' RETURN END TRY LET g_kywl_code = json_resolution.code LET g_kywl_msg = json_resolution.msg IF json_resolution.code <> '10000' OR cl_null(json_resolution.msg) THEN LET g_success = 'N' RETURN END IF FOR i = 0 TO json_resolution_2.filePictureInfoRes.size()-1 #理论上只会有一笔 LET json_obj_3 = json_resolution_2.filePictureInfoRes.getJSONObject(i) LET extendName = json_obj_3.getString("extendName") LET originalName = json_obj_3.getString("originalName") LET size = json_obj_3.getString("size") LET picture = json_obj_3.getString("picture") END FOR IF cl_null(extendName) OR cl_null(originalName) OR cl_null(picture) THEN LET g_kywl_msg = '暂无图片信息' LET g_success = 'N' RETURN END IF # LET path = '$TOP/../topcust/cxm/4gl' LET path = '/u1/toptest/topcust/cxm/4gl' LET filename = originalName LET filename = cl_replace_str(filename, "waybill_", "") #命名调整 LET filename = cl_replace_str(filename, "."||extendName, "") # CALL aws_send_kywl_base64('data:image/jpeg;base64,'||picture,'decode','') RETURNING l_result,l_str # CALL aws_write_txt('/u1/toptest/topcust/cxm/4gl/lipd.jpg',l_str,'w') RETURNING l_result CALL aws_write_txt(path||'/'||filename||'.txt',picture,'w') RETURNING l_result #把base64编码写入txt文件 IF l_result AND os.Path.exists(path||'/'||filename||'.txt') THEN #是否写入成功 LET l_cmd = 'cd '||path||';'||'base64 -d '||filename||'.txt'||' > '||filename||'.'||extendName #用linux命令把txt转成对应文件 RUN l_cmd IF os.Path.exists(path||'/'||filename||'.'||extendName) THEN #是否转成功 LET l_cmd = 'cd '||path||';'||'rm '||filename||'.txt' #删了txt RUN l_cmd LET unix_path = path||'/'||filename||'.'||extendName #文件在服务器的路径 LET window_path = cl_browse_dir() #获取用户端文件路径 LET window_path = window_path,"\\",filename||'.'||extendName #路径与文件名拼接 LET status = cl_download_file(unix_path, window_path) #文件下载到用户端指定的路径 IF status THEN CALL cl_err(filename||'.'||extendName,"cic-997",1) DISPLAY "Download OK!!" ELSE CALL cl_err(filename||'.'||extendName,"cic-996",1) DISPLAY "Download fail!!" END IF LET l_cmd = 'cd '||path||';'||'rm '||filename||'.'||extendName #文件从服务器删除 RUN l_cmd END IF ELSE CALL cl_err('图片编码写入文件失败','!',1) LET g_success = 'N' END IF END FUNCTION FUNCTION aws_write_txt(srcfile,src,mode) DEFINE srcfile STRING DEFINE src STRING DEFINE mode STRING DEFINE ch base.Channel DEFINE result INT DEFINE mystatus INT DEFINE idx,old INT DEFINE line STRING LET ch=base.channel.create() CALL ch.setDelimiter("") WHENEVER ERROR CONTINUE CALL ch.openFile(srcfile,mode) LET mystatus = status WHENEVER ERROR stop IF mystatus <> 0 THEN LET result=0 ELSE LET old=1 LET src = cl_replace_str(src, "\r\n", "") # WHILE (idx:=src.getIndexOf("\n",old))>0 # LET line=src.subString(old,idx-1) # LET src=src.subString(idx+1,src.getLength()) # CALL ch.writeLine(line) # END WHILE LET idx = src.getLength() IF src.getLength()>0 THEN CALL ch.write(src) END IF LET result=TRUE CALL ch.close() END IF RETURN result END FUNCTION ######################################################### #@desc 利用 commons-codec-1.10.jar 套件 實做base64轉碼 #@input p_str :轉碼字串 #@input p_type :轉碼類,encode,decode #@input p_charset :字串編碼,預設UTF-8 #@return lb_ret : 轉碼是否成功 #@return ls_ret : 轉碼成功字串 or 轉碼失敗錯誤訊息; #@create 2018/07/11 ######################################################### FUNCTION aws_send_kywl_base64(p_str,p_type,p_charset) DEFINE p_str STRING #轉碼字串 DEFINE p_type STRING #encode,decode DEFINE p_charset STRING #編碼,預設UTF-8 DEFINE lb_ret BOOLEAN #回傳結果 DEFINE ls_ret STRING #回傳結果 DEFINE ls_enc STRING #base64加密字串 DEFINE ls_dec STRING #base64解碼字串 DEFINE l_java_str java.lang.String #java string 字串暫存 DEFINE lo_base64 org.apache.commons.codec.binary.Base64 #base64轉碼工具 # # init # LET lo_base64 = Base64.create() LET l_java_str = String.create(p_str) LET lb_ret = FALSE IF (p_charset IS NULL) OR p_charset.equals("") THEN LET p_charset = "UTF-8" END IF # TRY CASE p_type WHEN "decode" LET ls_dec = java.lang.String.create(lo_base64.decode(l_java_str), p_charset) #base64 解密 #DISPLAY "(", p_str , ").base64decode :" #debug #DISPLAY ls_dec , "\n" #debug LET ls_ret = ls_dec LET lb_ret = TRUE exit case WHEN "encode" LET ls_enc = lo_base64.encodeToString(l_java_str.getBytes(p_charset)) #base64 加密 #DISPLAY "(", p_str , ").base64encode :" #debug #DISPLAY ls_enc , "\n" #debug LET ls_ret = ls_enc LET lb_ret = TRUE exit case OTHERWISE LET ls_ret = "Unknow method ",p_type, ",please set encode or decode;" exit case END CASE CATCH LET ls_ret = "[error]SQLCA.SQLCODE = ",SQLCA.SQLCODE,"\n" , "STATUS = ",STATUS,"\n" , "SQLCA.SQLERRM = ",SQLCA.SQLERRM END TRY RETURN lb_ret,ls_ret END FUNCTION |
|
沙发#
发布于:2023-02-07 10:33
先赞一个
|
|