ehxz 发表于 2026-8-3 16:35:04

PbIdea 实战教程 - 常用对象使用指南 QQ:3912810832提供

## PbIdea 实战教程 - 常用对象使用指南
作者:在神
---
前言
最近群里经常有朋友问 PbIdea 的各种用法,我把最近几天讨论的内容整理成这篇实战教程,方便大家参考学习。
---
一、uo_json - JSON 解析与操作
这是最常用的对象之一,几乎每个项目都会用到。
1、基本用法

// 创建 JSON 对象
uo_json ljson = create uo_json
// 解析 JSON 字符串
string ls_json = '{"name":"智子","age":25}'
boolean lb_ok = ljson.Parse(ls_json)
if lb_ok then
    // 获取 name 值
    string ls_name = ljson.GetString("name")
    MessageBox("解析结果", "name = " + ls_name)
else
    MessageBox("解析失败", ljson.GetError())
end if
// 销毁对象
destroy ljson

2、获取值的三种方式

uo_json ljson = create uo_json
ljson.Parse('{"name":"张三","age":25,"price":99.5}')
// 方式 1:快捷获取(推荐)
string ls_name = ljson.GetString("name")
long ll_age = ljson.GetLong("age")
double ld_price = ljson.GetDouble("price")
// 方式 2:常规获取(可判断是否成功)
string ls_name2
boolean lb_ok = ljson.Get("name", ls_name2)
// 方式 3:带默认值
string ls_name3 = ljson.GetString("name", "未知")
destroy ljson

3、获取数组值

string ls_json = '{"tags":["程序员","Java","PowerBuilder"]}'
uo_json ljson = create uo_json
ljson.Parse(ls_json)
// 获取字符串数组
string ls_tags[]
ljson.Get("tags", ls_tags[])
long ll_count = UpperBound(ls_tags)
for li_i = 1 to ll_count
    MessageBox("标签", ls_tags)
next
destroy ljson

4、获取嵌套对象

string ls_json = '{"user":{"name":"张三","age":25},"address":{"city":"北京","zip":"100000"}}'
uo_json ljson = create uo_json
ljson.Parse(ls_json)
// 获取嵌套对象
uo_json luser = create uo_json
ljson.Get("user", luser)
string ls_name = luser.GetString("name")
long ll_age = luser.GetLong("age")
uo_json laddress = create uo_json
ljson.Get("address", laddress)
string ls_city = laddress.GetString("city")
destroy luser
destroy laddress
destroy ljson

5、构建 JSON

uo_json ljson = create uo_json
ljson.Set("name", "张三")
ljson.Set("age", 25)
ljson.Set("email", "zhangsan@example.com")
// 输出为字符串(true 表示格式化输出)
string ls_result = ljson.ToString(true)
MessageBox("JSON", ls_result)
destroy ljson

---
二、uo_curl - HTTP 请求
基于 curl 库的 HTTP 客户端,功能强大。
1、GET 请求

uo_curl lcURL = create uo_curl
// 设置 URL 和方法
lcURL.SetUrl("https://api.example.com/data", HttpGet)
// 设置请求头
lcURL.SetHeader("Authorization", "Bearer your-token")
lcURL.SetHeader("Accept", "application/json")
// 设置超时(毫秒)
lcURL.SetTimeout(10000)
// 发送请求
boolean lb_ok = lcURL.request()
if lb_ok then
    // 检查 HTTP 状态码
    if lcURL.response.httpcode = 200 then
      MessageBox("请求成功", lcURL.response.text)
    else
      MessageBox("请求失败", "状态码:" + string(lcURL.response.httpcode))
    end if
else
    MessageBox("网络错误", lcURL.response.errtext)
end if
destroy lcURL

2、POST 请求(JSON 数据)

uo_curl lcURL = create uo_curl
// 设置 URL
lcURL.SetUrl("https://api.example.com/api/users", HttpPost)
// 设置请求头
lcURL.SetHeader("Content-Type", "application/json")
lcURL.SetHeader("Accept", "application/json")
// 构建 JSON 数据
uo_json ljson = create uo_json
ljson.Set("name", "张三")
ljson.Set("age", 25)
ljson.Set("email", "zhangsan@example.com")
// 发送请求
boolean lb_ok = lcURL.request(ljson)
if lb_ok then
    if lcURL.response.httpcode = 200 then
      // 解析响应
      uo_json lresp = create uo_json
      lresp.Parse(lcURL.response.data)
      string ls_id = lresp.GetString("id")
      MessageBox("创建成功", "用户 ID:" + ls_id)
      destroy lresp
    else
      MessageBox("请求失败", lcURL.response.errtext)
    end if
end if
destroy ljson
destroy lcURL

3、POST 请求(表单数据)

uo_curl lcURL = create uo_curl
lcURL.SetUrl("https://api.example.com/api/login", HttpPost)
// 设置表单字段
lcURL.SetForm("username", "admin")
lcURL.SetForm("password", "123456")
lcURL.SetForm("remember", "1")
boolean lb_ok = lcURL.request()
if lb_ok then
    MessageBox("登录结果", lcURL.response.text)
end if
destroy lcURL

4、文件下载

uo_curl lcURL = create uo_curl
lcURL.SetUrl("https://example.com/file.zip", HttpGet)
lcURL.SetDownloadFile("C:\\download\\file.zip")
boolean lb_ok = lcURL.request()
if lb_ok then
    MessageBox("下载成功", "文件已保存到 C:\\download\\file.zip")
end if
destroy lcURL

5、文件上传

uo_curl lcURL = create uo_curl
lcURL.SetUrl("https://api.example.com/api/upload", HttpPost)
// 设置上传文件
lcURL.SetUploadFile("C:\\document.pdf")
// 设置其他表单字段
lcURL.SetForm("title", "重要文档")
lcURL.SetForm("category", "报告")
boolean lb_ok = lcURL.request()
if lb_ok then
    MessageBox("上传成功", lcURL.response.text)
end if
destroy lcURL

6、Basic 认证

uo_curl lcURL = create uo_curl
lcURL.SetUrl("https://api.example.com/api/protected", HttpGet)
lcURL.SetBasicAuth("username", "password")
boolean lb_ok = lcURL.request()
if lb_ok then
    MessageBox("认证成功", lcURL.response.text)
end if
destroy lcURL

7、JWT 认证

uo_curl lcURL = create uo_curl
lcURL.SetUrl("https://api.example.com/api/user", HttpGet)
lcURL.SetJWTAuth("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...")
boolean lb_ok = lcURL.request()
if lb_ok then
    MessageBox("请求成功", lcURL.response.text)
end if
destroy lcURL

---
三、uo_crypto - 加密与安全
支持几乎所有常见加密算法,包括国密 SM2/SM3/SM4。
1、AES 加密解密

string ls_key = "my-secret-key-123456"
string ls_iv = "0123456789abcdef"
blob lb_data = blob("敏感数据")
// 加密
blob lb_encrypted = Crypto(lb_data, ls_key, 1, "aes_256_cbc", ls_iv)
// 解密
blob lb_decrypted = Crypto(lb_encrypted, ls_key, 0, "aes_256_cbc", ls_iv)
string ls_result = string(lb_decrypted)
MessageBox("解密结果", ls_result)

2、SM4 国密加密

string ls_key = "1234567890abcdef"
string ls_data = "这是需要加密的敏感数据"
// 加密(decrypto=0 表示加密)
blob lb_encrypted = gm_sm4(ls_data, ls_key, 0)
// 解密(decrypto=1 表示解密)
blob lb_decrypted = gm_sm4(lb_encrypted, ls_key, 1)
string ls_result = string(lb_decrypted)
MessageBox("解密结果", ls_result)

3、SM2 国密加密

// 生成 SM2 密钥对
string ls_pubkey, ls_prikey
GenerateSM2Key(ls_pubkey, ls_prikey)
// 加密
blob lb_encrypted = SM2Encrypt(ls_pubkey, "测试数据", 1)
// 解密
blob lb_decrypted = SM2Decrypt(ls_prikey, lb_encrypted, 1)
// SM2 签名
string ls_sign = SignWithSM2(ls_prikey, "测试数据", 1)
// SM2 验签
boolean lb_ok = VerifyWithSM2(ls_pubkey, "测试数据", ls_sign, 1)

4、RSA 加密

// 生成 RSA 密钥对
string ls_pubkey, ls_prikey
GenerateRSAKey(ls_pubkey, ls_prikey, 2048)
// 加密
blob lb_encrypted = EncryptByPublicKey(ls_pubkey, "敏感数据")
// 解密
blob lb_decrypted = DecryptByPrivateKey(ls_prikey, lb_encrypted)
// RSA 签名
string ls_sign = SignWithRSA(ls_prikey, "数据", "sha256")
// RSA 验签
boolean lb_ok = SignWithRSAVerify(ls_pubkey, "数据", ls_sign, "sha256")

5、MD5 哈希

string ls_md5 = MD5("需要加密的字符串")
string ls_filemd5 = MD5File("C:\\file.exe")
MessageBox("MD5", ls_md5)

6、SHA 哈希

string ls_sha1 = Sha1("数据")
string ls_sha256 = Sha256("数据")
string ls_sha512 = Sha512("数据")

7、Base64 编码解码

blob lb_data = blob("测试数据")
string ls_base64 = Base64(lb_data)
blob lb_decoded = Base64Decode(ls_base64)
string ls_result = string(lb_decoded)

---
四、uo_pdfview - PDF 查看与编辑
1、基本用法

uo_pdfview lpdf = create uo_pdfview
// 打开 PDF 文件
lpdf.Load("C:\\report.pdf")
// 获取页数
long ll_pagecount = lpdf.GetPageCount()
// 获取指定页文本
string ls_text = lpdf.GetPageText(1)
// 打印
lpdf.Print(false)// false=不显示打印对话框
destroy lpdf

2、加载页面为图片

uo_pdfview lpdf = create uo_pdfview
lpdf.Load("C:\\report.pdf")
// 获取指定页的 BITMAP 数据
blob lb_page = lpdf.LoadPage(1, 800, 1000)
// 保存页面为图片文件
lpdf.LoadPage(1, 800, 1000, "C:\\page1.png")
destroy lpdf

3、PDF 合并与分割

uo_pdfview lpdf = create uo_pdfview
// 合并多个 PDF
string ls_pdfs[]
ls_pdfs = "C:\\file1.pdf"
ls_pdfs = "C:\\file2.pdf"
lpdf.MeargePDF(ls_pdfs, "C:\\merged.pdf")
// 按页分割 PDF
string ls_result[]
lpdf.SplitPDF("C:\\file.pdf", "C:\\output\\page_", ls_result[])
destroy lpdf

4、在 PDF 上绘图

uo_pdfview lpdf = create uo_pdfview
lpdf.Load("C:\\report.pdf")
// 画线
lpdf.DrawLine(1, 100, 100, 500, 100, RGB(255,0,0), 2)
// 画框
lpdf.DrawBox(1, 100, 200, 500, 300, RGB(0,0,255), 2, false)
// 添加文字
lpdf.DrawText("测试文字", 1, 100, 400, RGB(0,0,0), "宋体", 12, 0)
// 保存
lpdf.SaveAs("C:\\report_new.pdf")
destroy lpdf

---
五、uo_yibao - 医保接口
医保专用接口对象,支持多个省份的医保接口调用。
1、基本用法

uo_yibao lyibao = create uo_yibao
// 设置医保类型(0=甘肃,1=黑龙江,2=吉林,3=江苏,4=山西)
lyibao.SetYiBao_Option(0, 1)
// 准备请求参数
string ls_args = "action=card_auth&card_no=123456"
string ls_headers = "Content-Type: application/x-www-form-urlencoded"
string ls_body = "param=加密参数"
string ls_result
// 发起请求
boolean lb_ok = lyibao.YiBao_Request(ls_args, ls_headers, ls_body, ls_result)
if lb_ok then
    MessageBox("请求成功", ls_result)
end if
destroy lyibao

2、国密算法

uo_yibao lyibao = create uo_yibao
// 生成 SM2 密钥对
string ls_pubkey, ls_prikey
lyibao.GM_createKey(ls_pubkey, ls_prikey)
// SM2 签名
string ls_data = "需要签名的数据"
blob lb_sign = lyibao.sm2sign("1234567812345678", ls_prikey, ls_pubkey, ls_data)
// SM2 验签
boolean lb_ok = lyibao.sm2verify("1234567812345678", ls_pubkey, ls_data, lb_sign)
// SM3 哈希
blob lb_hash = lyibao.sm3(ls_data)
// SM4 加密解密
string ls_key = "1234567890abcdef"
blob lb_encrypted = lyibao.sm4(ls_data, ls_key, 0)// 加密
blob lb_decrypted = lyibao.sm4(lb_encrypted, ls_key, 1)// 解密
destroy lyibao

---
六、注意事项
1、对象必须销毁
所有 uo_* 对象使用完毕后必须 destroy,否则内存泄漏。
2、字符编码
字符串转 blob 时注意字符集,PB10+ 默认 UTF-16LE。
3、HTTP 请求
request() 返回的是调用是否成功,不代表接口处理是否正确,需要检查 response.httpcode。
4、加密算法
SM4 使用 16 字节(128 位)密钥,字符串格式为 16 个字符。
5、线程安全
数据库连接池对象(uo_database_pool)专为多线程设计。
---
七、总结
PbIdea 提供了丰富的功能对象,让 PowerBuilder 开发更加便捷。本文整理了最近几天群里讨论最多的几个对象,希望能帮助大家快速上手。
如有问题欢迎在群里讨论。
---
作者:在神
本文基于 PbIdea 官方文档和实际使用经验整理,如有错误欢迎指正。
页: [1]
查看完整版本: PbIdea 实战教程 - 常用对象使用指南 QQ:3912810832提供

免责声明:
本站所发布的一切破解补丁、注册机和注册信息及软件的解密分析文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:Admin@SybaseBbs.com