祝愿大家身体健康!

 站点注册  找回密码
 站点注册

QQ登录

只需一步,快速开始

查看: 107|回复: 0

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

[复制链接]

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

[复制链接]
ehxz

主题

0

回帖

62万

积分

管理员

积分
626011
贡献
在线时间
小时
2026-8-3 16:35:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?站点注册

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

  1. // 创建 JSON 对象
  2. uo_json ljson = create uo_json
  3. // 解析 JSON 字符串
  4. string ls_json = '{"name":"智子","age":25}'
  5. boolean lb_ok = ljson.Parse(ls_json)
  6. if lb_ok then
  7.     // 获取 name 值
  8.     string ls_name = ljson.GetString("name")
  9.     MessageBox("解析结果", "name = " + ls_name)
  10. else
  11.     MessageBox("解析失败", ljson.GetError())
  12. end if
  13. // 销毁对象
  14. destroy ljson
复制代码

2、获取值的三种方式

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

3、获取数组值

  1. string ls_json = '{"tags":["程序员","Java","PowerBuilder"]}'
  2. uo_json ljson = create uo_json
  3. ljson.Parse(ls_json)
  4. // 获取字符串数组
  5. string ls_tags[]
  6. ljson.Get("tags", ls_tags[])
  7. long ll_count = UpperBound(ls_tags)
  8. for li_i = 1 to ll_count
  9.     MessageBox("标签", ls_tags[li_i])
  10. next
  11. destroy ljson
复制代码

4、获取嵌套对象

  1. string ls_json = '{"user":{"name":"张三","age":25},"address":{"city":"北京","zip":"100000"}}'
  2. uo_json ljson = create uo_json
  3. ljson.Parse(ls_json)
  4. // 获取嵌套对象
  5. uo_json luser = create uo_json
  6. ljson.Get("user", luser)
  7. string ls_name = luser.GetString("name")
  8. long ll_age = luser.GetLong("age")
  9. uo_json laddress = create uo_json
  10. ljson.Get("address", laddress)
  11. string ls_city = laddress.GetString("city")
  12. destroy luser
  13. destroy laddress
  14. destroy ljson
复制代码

5、构建 JSON

  1. uo_json ljson = create uo_json
  2. ljson.Set("name", "张三")
  3. ljson.Set("age", 25)
  4. ljson.Set("email", "zhangsan@example.com")
  5. // 输出为字符串(true 表示格式化输出)
  6. string ls_result = ljson.ToString(true)
  7. MessageBox("JSON", ls_result)
  8. destroy ljson
复制代码

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

  1. uo_curl lcURL = create uo_curl
  2. // 设置 URL 和方法
  3. lcURL.SetUrl("https://api.example.com/data", HttpGet)
  4. // 设置请求头
  5. lcURL.SetHeader("Authorization", "Bearer your-token")
  6. lcURL.SetHeader("Accept", "application/json")
  7. // 设置超时(毫秒)
  8. lcURL.SetTimeout(10000)
  9. // 发送请求
  10. boolean lb_ok = lcURL.request()
  11. if lb_ok then
  12.     // 检查 HTTP 状态码
  13.     if lcURL.response.httpcode = 200 then
  14.         MessageBox("请求成功", lcURL.response.text)
  15.     else
  16.         MessageBox("请求失败", "状态码:" + string(lcURL.response.httpcode))
  17.     end if
  18. else
  19.     MessageBox("网络错误", lcURL.response.errtext)
  20. end if
  21. destroy lcURL
复制代码

2、POST 请求(JSON 数据)

  1. uo_curl lcURL = create uo_curl
  2. // 设置 URL
  3. lcURL.SetUrl("https://api.example.com/api/users", HttpPost)
  4. // 设置请求头
  5. lcURL.SetHeader("Content-Type", "application/json")
  6. lcURL.SetHeader("Accept", "application/json")
  7. // 构建 JSON 数据
  8. uo_json ljson = create uo_json
  9. ljson.Set("name", "张三")
  10. ljson.Set("age", 25)
  11. ljson.Set("email", "zhangsan@example.com")
  12. // 发送请求
  13. boolean lb_ok = lcURL.request(ljson)
  14. if lb_ok then
  15.     if lcURL.response.httpcode = 200 then
  16.         // 解析响应
  17.         uo_json lresp = create uo_json
  18.         lresp.Parse(lcURL.response.data)
  19.         string ls_id = lresp.GetString("id")
  20.         MessageBox("创建成功", "用户 ID:" + ls_id)
  21.         destroy lresp
  22.     else
  23.         MessageBox("请求失败", lcURL.response.errtext)
  24.     end if
  25. end if
  26. destroy ljson
  27. destroy lcURL
复制代码

3、POST 请求(表单数据)

  1. uo_curl lcURL = create uo_curl
  2. lcURL.SetUrl("https://api.example.com/api/login", HttpPost)
  3. // 设置表单字段
  4. lcURL.SetForm("username", "admin")
  5. lcURL.SetForm("password", "123456")
  6. lcURL.SetForm("remember", "1")
  7. boolean lb_ok = lcURL.request()
  8. if lb_ok then
  9.     MessageBox("登录结果", lcURL.response.text)
  10. end if
  11. destroy lcURL
复制代码

4、文件下载

  1. uo_curl lcURL = create uo_curl
  2. lcURL.SetUrl("https://example.com/file.zip", HttpGet)
  3. lcURL.SetDownloadFile("C:\\download\\file.zip")
  4. boolean lb_ok = lcURL.request()
  5. if lb_ok then
  6.     MessageBox("下载成功", "文件已保存到 C:\\download\\file.zip")
  7. end if
  8. destroy lcURL
复制代码

5、文件上传

  1. uo_curl lcURL = create uo_curl
  2. lcURL.SetUrl("https://api.example.com/api/upload", HttpPost)
  3. // 设置上传文件
  4. lcURL.SetUploadFile("C:\\document.pdf")
  5. // 设置其他表单字段
  6. lcURL.SetForm("title", "重要文档")
  7. lcURL.SetForm("category", "报告")
  8. boolean lb_ok = lcURL.request()
  9. if lb_ok then
  10.     MessageBox("上传成功", lcURL.response.text)
  11. end if
  12. destroy lcURL
复制代码

6、Basic 认证

  1. uo_curl lcURL = create uo_curl
  2. lcURL.SetUrl("https://api.example.com/api/protected", HttpGet)
  3. lcURL.SetBasicAuth("username", "password")
  4. boolean lb_ok = lcURL.request()
  5. if lb_ok then
  6.     MessageBox("认证成功", lcURL.response.text)
  7. end if
  8. destroy lcURL
复制代码

7、JWT 认证

  1. uo_curl lcURL = create uo_curl
  2. lcURL.SetUrl("https://api.example.com/api/user", HttpGet)
  3. lcURL.SetJWTAuth("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...")
  4. boolean lb_ok = lcURL.request()
  5. if lb_ok then
  6.     MessageBox("请求成功", lcURL.response.text)
  7. end if
  8. destroy lcURL
复制代码

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

  1. string ls_key = "my-secret-key-123456"
  2. string ls_iv = "0123456789abcdef"
  3. blob lb_data = blob("敏感数据")
  4. // 加密
  5. blob lb_encrypted = Crypto(lb_data, ls_key, 1, "aes_256_cbc", ls_iv)
  6. // 解密
  7. blob lb_decrypted = Crypto(lb_encrypted, ls_key, 0, "aes_256_cbc", ls_iv)
  8. string ls_result = string(lb_decrypted)
  9. MessageBox("解密结果", ls_result)
复制代码

2、SM4 国密加密

  1. string ls_key = "1234567890abcdef"
  2. string ls_data = "这是需要加密的敏感数据"
  3. // 加密(decrypto=0 表示加密)
  4. blob lb_encrypted = gm_sm4(ls_data, ls_key, 0)
  5. // 解密(decrypto=1 表示解密)
  6. blob lb_decrypted = gm_sm4(lb_encrypted, ls_key, 1)
  7. string ls_result = string(lb_decrypted)
  8. MessageBox("解密结果", ls_result)
复制代码

3、SM2 国密加密

  1. // 生成 SM2 密钥对
  2. string ls_pubkey, ls_prikey
  3. GenerateSM2Key(ls_pubkey, ls_prikey)
  4. // 加密
  5. blob lb_encrypted = SM2Encrypt(ls_pubkey, "测试数据", 1)
  6. // 解密
  7. blob lb_decrypted = SM2Decrypt(ls_prikey, lb_encrypted, 1)
  8. // SM2 签名
  9. string ls_sign = SignWithSM2(ls_prikey, "测试数据", 1)
  10. // SM2 验签
  11. boolean lb_ok = VerifyWithSM2(ls_pubkey, "测试数据", ls_sign, 1)
复制代码

4、RSA 加密

  1. // 生成 RSA 密钥对
  2. string ls_pubkey, ls_prikey
  3. GenerateRSAKey(ls_pubkey, ls_prikey, 2048)
  4. // 加密
  5. blob lb_encrypted = EncryptByPublicKey(ls_pubkey, "敏感数据")
  6. // 解密
  7. blob lb_decrypted = DecryptByPrivateKey(ls_prikey, lb_encrypted)
  8. // RSA 签名
  9. string ls_sign = SignWithRSA(ls_prikey, "数据", "sha256")
  10. // RSA 验签
  11. boolean lb_ok = SignWithRSAVerify(ls_pubkey, "数据", ls_sign, "sha256")
复制代码

5、MD5 哈希

  1. string ls_md5 = MD5("需要加密的字符串")
  2. string ls_filemd5 = MD5File("C:\\file.exe")
  3. MessageBox("MD5", ls_md5)
复制代码

6、SHA 哈希

  1. string ls_sha1 = Sha1("数据")
  2. string ls_sha256 = Sha256("数据")
  3. string ls_sha512 = Sha512("数据")
复制代码

7、Base64 编码解码

  1. blob lb_data = blob("测试数据")
  2. string ls_base64 = Base64(lb_data)
  3. blob lb_decoded = Base64Decode(ls_base64)
  4. string ls_result = string(lb_decoded)
复制代码

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

  1. uo_pdfview lpdf = create uo_pdfview
  2. // 打开 PDF 文件
  3. lpdf.Load("C:\\report.pdf")
  4. // 获取页数
  5. long ll_pagecount = lpdf.GetPageCount()
  6. // 获取指定页文本
  7. string ls_text = lpdf.GetPageText(1)
  8. // 打印
  9. lpdf.Print(false)  // false=不显示打印对话框
  10. destroy lpdf
复制代码

2、加载页面为图片

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

3、PDF 合并与分割

  1. uo_pdfview lpdf = create uo_pdfview
  2. // 合并多个 PDF
  3. string ls_pdfs[]
  4. ls_pdfs[1] = "C:\\file1.pdf"
  5. ls_pdfs[2] = "C:\\file2.pdf"
  6. lpdf.MeargePDF(ls_pdfs, "C:\\merged.pdf")
  7. // 按页分割 PDF
  8. string ls_result[]
  9. lpdf.SplitPDF("C:\\file.pdf", "C:\\output\\page_", ls_result[])
  10. destroy lpdf
复制代码

4、在 PDF 上绘图

  1. uo_pdfview lpdf = create uo_pdfview
  2. lpdf.Load("C:\\report.pdf")
  3. // 画线
  4. lpdf.DrawLine(1, 100, 100, 500, 100, RGB(255,0,0), 2)
  5. // 画框
  6. lpdf.DrawBox(1, 100, 200, 500, 300, RGB(0,0,255), 2, false)
  7. // 添加文字
  8. lpdf.DrawText("测试文字", 1, 100, 400, RGB(0,0,0), "宋体", 12, 0)
  9. // 保存
  10. lpdf.SaveAs("C:\\report_new.pdf")
  11. destroy lpdf
复制代码

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

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

2、国密算法

  1. uo_yibao lyibao = create uo_yibao
  2. // 生成 SM2 密钥对
  3. string ls_pubkey, ls_prikey
  4. lyibao.GM_createKey(ls_pubkey, ls_prikey)
  5. // SM2 签名
  6. string ls_data = "需要签名的数据"
  7. blob lb_sign = lyibao.sm2sign("1234567812345678", ls_prikey, ls_pubkey, ls_data)
  8. // SM2 验签
  9. boolean lb_ok = lyibao.sm2verify("1234567812345678", ls_pubkey, ls_data, lb_sign)
  10. // SM3 哈希
  11. blob lb_hash = lyibao.sm3(ls_data)
  12. // SM4 加密解密
  13. string ls_key = "1234567890abcdef"
  14. blob lb_encrypted = lyibao.sm4(ls_data, ls_key, 0)  // 加密
  15. blob lb_decrypted = lyibao.sm4(lb_encrypted, ls_key, 1)  // 解密
  16. 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 官方文档和实际使用经验整理,如有错误欢迎指正。
共享共进共赢
Sharing And Win-win Results
SYBASEBBS - 免责申明1、欢迎访问“SYBASEBBS.COM”,本文内容及相关资源来源于网络,版权归版权方所有!本站原创内容版权归本站所有,请勿转载!
2、本文内容仅代表作者观点,不代表本站立场,作者自负,本站资源仅供学习研究,请勿非法使用,否则后果自负!请下载后24小时内删除!
3、本文内容,包括但不限于源码、文字、图片等,仅供参考。本站不对其安全性,正确性等作出保证。但本站会尽量审核会员发表的内容。
4、如本帖侵犯到任何版权问题,请立即告知本站 ,本站将及时删除并致以最深的歉意!客服邮箱:admin@sybasebbs.com
您需要登录后才可以回帖 登录 | 站点注册

本版积分规则

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

Mail To:Admin@SybaseBbs.com

客服微信:18669893686
挂谷猜想 · 探索

QQ|Archiver|PowerBuilder(PB)BBS社区 ( 鲁ICP备2021027222号-1 )

GMT+8, 2026-8-15 01:21 , Processed in 0.020671 second(s), 9 queries , MemCached On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表