title: 微信小程序 meta:

  • name: description content: WeChat是一个基于Swoole 4.x全协程支持的微信SDK库
  • name: keywords content: swoole|swoole extension|swoole framework|easyswoole|wechat|miniProgram

微信小程序

获取小程序对象

  1. $wxa = new \EasySwoole\WeChat\MiniProgram\MiniProgram;
  2. $wxa->getConfig()->setAppId('your appid')->setAppSecret('your appsecret');

获取小程序session

详细信息请参阅 微信小程序登陆

  1. $code = '';
  2. $session = $wxa->auth()->session($code);

你会返回一个包含下面文档中提到的参数的数组 code2Session

创建二维码

参阅文档documentation

我们可以使用一下三种方式创建二维码

  1. /** 二维码 - 永久小程序码 Api: $wxa->qrCode()->getWxaCode()
  2. * @param $path 扫码进入的小程序页面路径,最大长度 128 字节
  3. * @param int $width 二维码的宽度
  4. * @param bool $autoColor 自动配置线条颜色
  5. * @param null $lineColor auto_color 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"} 十进制表示
  6. * @param bool $isHyaline 是否需要透明底色
  7. */
  8. function getWxaCode($path, $width = 430, $autoColor = false, $lineColor = null, $isHyaline = false)
  9. /**
  10. * 二维码 - 临时小程序码 Api: $wxa->qrCode()->getWxaCodeUnLimit()
  11. * @param $path 已经发布的小程序存在的页面
  12. * @param $scene 最大32个可见字符
  13. * @param int $width 二维码的宽度
  14. * @param bool $autoColor 自动配置线条颜色
  15. * @param null $lineColor auto_color 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"} 十进制表示
  16. * @param bool $isHyaline 是否需要透明底色
  17. */
  18. function getWxaCodeUnLimit($path, $scene, $width = 430, $autoColor = false, $lineColor = null, $isHyaline = false)
  19. /**
  20. * 二维码 - 永久二维码 Api: $wxa->qrCode()->createWxaQrCode()
  21. * @param $path 扫码进入的小程序页面路径,最大长度 128 字节
  22. * @param int $width 二维码的宽度
  23. */
  24. function createWxaQrCode($path, $width = 430)

获取帐号下已存在的模板列表

  1. $wxa->templateMsg()->getTemplateList(int $offset, int $count)

获取模板库某个模板标题下关键词库

  1. $wxa->templateMsg()->getTemplateLibraryById(string $id)

组合模板并添加至帐号下的个人模板库

  1. $wxa->templateMsg()->addTemplate(string $id, array $keywordIdList)

删除帐号下的某个模板

  1. $wxa->templateMsg()->deleteTemplate(string $templateId)

获取小程序模板库标题列表

  1. $wxa->templateMsg()->getTemplateLibraryList(int $offset, int $count)

发送模板消息

  1. //要传递的参数数组,下面实际方法中是传入Bean
  2. $templateMsg = [
  3. 'touser' => 'user-openid',
  4. 'template_id' => 'template-id',
  5. 'page' => 'index',
  6. 'form_id' => 'form-id',
  7. 'data' => [
  8. 'keyword1' => 'VALUE',
  9. 'keyword2' => 'VALUE2',
  10. ],
  11. ];
  12. $wxa->templateMsg()->send(TemplateMsgBean $templateMsg)

微信小程序消息解密(获取电话等功能,信息是加密的,需要解密)

  1. $wxa->encryptor()->decryptData(string $sessionKey, string $iv, string $encryptedData)

检查一段文本是否含有违法违规内容

  1. $wxa->checkFile()->msgSecCheck(string $content)

校验一张图片是否含有违法违规内容

  1. $wxa->checkFile()->imgSecCheck(ImgUploadBean $imgUpload)

异步校验图片/音频是否含有违法违规内容

  1. $wxa->checkFile()->mediaCheckAsync(string $mediaUrl ,int $mediaType)

物流助手(小程序)

  1. /**
  2. * 绑定、解绑物流账号 Api: $wxa->logisticsProgram()->bindAccount()
  3. * @param BindAccount $bindAccount
  4. */
  5. function bindAccount(BindAccount $bindAccount)
  6. //例子
  7. //要传递的参数数组,下面实际方法中是传入Bean
  8. $bindAccount = [
  9. 'type' => 'bind', //bind表示绑定,unbind表示解除绑定
  10. 'biz_id' => '1', //快递公司客户编码
  11. 'delivery_id' => '1', //快递公司ID
  12. 'password' => '***', //快递公司客户密码
  13. 'remark_content'=> '测试' //备注内容(提交EMS审核需要)
  14. ];
  15. $wxa->logisticsProgram()->bindAccount($bindAccount);
  16. /**获取所有绑定的物流账号 Api: $wxa->logisticsProgram()->getAllAccount()
  17. *
  18. */
  19. function getAllAccount()
  20. /**
  21. * 获取电子面单余额。仅在使用加盟类快递公司时,才可以调用。 Api: $wxa->logisticsProgram()->getQuota()
  22. * @param string $deliveryId 快递公司ID
  23. * @param string $bizId 快递公司客户编码
  24. */
  25. function getQuota(string $deliveryId ,string $bizId)
  26. /**
  27. * 生成运单 Api: $wxa->logisticsProgram()->addOrder()
  28. * @param AddOrder $addOrder
  29. * 具体参数请参考文档: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/express/by-business/logistics.addOrder.html
  30. */
  31. function addOrder(AddOrder $addOrder)
  32. /**
  33. * 取消运单 Api: $wxa->logisticsProgram()->cancelOrder()
  34. * @param string $orderId 订单 ID,需保证全局唯一
  35. * @param string $openid 用户openid,当add_source=2时无需填写(不发送物流服务通知)
  36. * @param string $deliveryId 快递公司ID
  37. * @param string $waybillId 运单ID
  38. */
  39. function cancelOrder(string $orderId ,string $openid ,string $deliveryId ,string $waybillId)
  40. /**
  41. * 获取支持的快递公司列表 Api: $wxa->logisticsProgram()->getAllDelivery()
  42. */
  43. function getAllDelivery()
  44. /* 获取运单数据 Api: $wxa->logisticsProgram()->getOrder()
  45. * @param string $orderId 订单 ID,需保证全局唯一
  46. * @param string $openid 用户openid,当add_source=2时无需填写(不发送物流服务通知)
  47. * @param string $deliveryId 快递公司ID
  48. * @param string $waybillId 运单ID
  49. */
  50. function getOrder(string $orderId ,string $openid ,string $deliveryId ,string $waybillId)
  51. /* 查询运单轨迹 Api: $wxa->logisticsProgram()->getPath()
  52. * @param string $orderId 订单 ID,需保证全局唯一
  53. * @param string $openid 用户openid,当add_source=2时无需填写(不发送物流服务通知)
  54. * @param string $deliveryId 快递公司ID
  55. * @param string $waybillId 运单ID
  56. */
  57. function getPath(string $orderId ,string $openid ,string $deliveryId ,string $waybillId)
  58. //获取打印员。若需要使用微信打单 PC 软件,才需要调用。 Api: $wxa->logisticsProgram()->getPrinter()
  59. function getPrinter()
  60. /**
  61. * 配置面单打印员,若需要使用微信打单 PC 软件,才需要调用。 Api: $wxa->logisticsProgram()->updatePrinter()
  62. * @param string $openid 打印员 openid
  63. * @param string $updateType 更新类型
  64. * @param string $tagidList 用于平台型小程序设置入驻方的打印员面单打印权限,同一打印员最多支持10个tagid,使用逗号分隔,如填写123,456,表示该打印员可以拉取到tagid为123和456的下的单,非平台型小程序无需填写该字段
  65. */
  66. function updatePrinter(string $openid ,string $updateType ,string $tagidList)

物流助手(服务端)

  1. /**
  2. * 获取面单联系人信息 Api: $wxa->logisticsService()->getContact()
  3. * @param string $tokens 商户侧下单事件中推送的 Token 字段
  4. * @param string $waybillId 运单 ID
  5. */
  6. function getContact(string $tokens ,string $waybillId)
  7. /**
  8. * 预览面单模板。用于调试面单模板使用。 Api: $wxa->logisticsService()->previewTemplate()
  9. * @param string $waybillId 运单 ID
  10. * @param string $waybillTemplate 面单 HTML 模板内容(需经 Base64 编码)
  11. * @param string $waybillData 面单数据
  12. * @param AddOrder $addOrder 商户下单数据
  13. * 具体参数文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/express/by-provider/logistics.previewTemplate.html
  14. */
  15. function previewTemplate(string $waybillId ,string $waybillTemplate ,string $waybillData,AddOrder $addOrder)
  16. /**
  17. * 更新商户审核结果 Api: $wxa->logisticsService()->updateBusiness()
  18. * @param string $shopAppId 商户的小程序AppID
  19. * @param string $bizId 商户账户
  20. * @param int $resultCode 审核结果,0 表示审核通过,其他表示审核失败
  21. * @param string $resultMsg 审核错误原因,仅 result_code 不等于 0 时需要设置
  22. */
  23. function updateBusiness(string $shopAppId ,string $bizId ,int $resultCode ,string $resultMsg)
  24. /**
  25. * 更新运单轨迹 Api: $wxa->logisticsService()->updatePath()
  26. * @param UpdatePath $updatePath
  27. * 具体参数文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/express/by-provider/logistics.updatePath.html
  28. */
  29. function updatePath(UpdatePath $updatePath)

生物认证

  1. /**
  2. * SOTER 生物认证秘钥签名验证 Api: $wxa->attest()->verifySignature()
  3. * @param string $openid 用户 openid
  4. * @param string $jsonString 通过 wx.startSoterAuthentication 成功回调获得的 resultJSON 字段
  5. * @param string $jsonSignature 通过 wx.startSoterAuthentication 成功回调获得的 resultJSONSignature 字段
  6. */
  7. function verifySignature(string $openid ,string $jsonString ,string $jsonSignature)

插件管理

  1. /**
  2. *向插件开发者发起使用插件的申请 Api: $wxa->plugin()->applyPlugin()
  3. * @param string $action 此接口下填写 "apply"
  4. * @param string $pluginAppid 插件 appId
  5. * @param string $reason 申请使用理由
  6. */
  7. function applyPlugin(string $action = 'apply' ,string $pluginAppid ,string $reason = '')
  8. /**
  9. * 获取当前所有插件使用方(供插件开发者调用)Api: $wxa->plugin()->getPluginDevApplyList()
  10. * @param string $action 此接口下填写 "dev_apply_list"
  11. * @param int $page 要拉取第几页的数据
  12. * @param int $num 每页的记录数
  13. */
  14. function getPluginDevApplyList(string $action = 'dev_apply_list' ,int $page = 1 ,int $num = 10)
  15. /**
  16. * 查询已添加的插件 Api: $wxa->plugin()->getPluginList()
  17. * @param string $action 此接口下填写 "list"
  18. */
  19. function getPluginList(string $action = 'list')
  20. /**
  21. * 修改插件使用申请的状态(供插件开发者调用)Api: $wxa->plugin()->setDevPluginApplyStatus()
  22. * @param string $action 修改操作
  23. * @param string $appid 使用者的 appid。同意申请时填写。
  24. * @param string $reason 拒绝理由。拒绝申请时填写
  25. */
  26. function setDevPluginApplyStatus(string $action ,string $appid = '' ,string $reason = '')
  27. /**
  28. * 删除已添加的插件 Api: $wxa->plugin()->unbindPlugin()
  29. * @param string $action 此接口下填写 "unbind"
  30. * @param string $pluginAppid 插件 appId
  31. */
  32. function unbindPlugin(string $action = 'unbind' ,string $pluginAppid)

附近的小程序

  1. /**
  2. * 添加地点 Api: $wxa->program()->add()
  3. * @param ProgramBean $program
  4. * 参数文档: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/nearby-poi/nearbyPoi.add.html
  5. */
  6. function add(ProgramBean $program)
  7. //例子
  8. $program = [
  9. 'kf_info' => '{"open_kf":true,"kf_headimg":"http://mmbiz.qpic.cn/mmbiz_jpg/kKMgNtnEfQzDKpLXYhgo3W3Gndl34gITqmP914zSwhajIEJzUPpx40P7R8fRe1QmicneQMhFzpZNhSLjrvU1pIA/0?wx_fmt=jpeg","kf_name":"Harden"}',
  10. 'pic_list' => '{"list":["http://mmbiz.qpic.cn/mmbiz_jpg/kKMgNtnEfQzDKpLXYhgo3W3Gndl34gITqmP914zSwhajIEJzUPpx40P7R8fRe1QmicneQMhFzpZNhSLjrvU1pIA/0?wx_fmt=jpeg","http://mmbiz.qpic.cn/mmbiz_jpg/kKMgNtnEfQzDKpLXYhgo3W3Gndl34gITRneE5FS9uYruXGMmrtmhsBySwddEWUGOibG8Ze2NT5E3Dyt79I0htNg/0?wx_fmt=jpeg"]}',
  11. 'service_infos' => '{"service_infos":[{"id":2,"type":1,"name":"快递","appid":"wx1373169e494e0c39","path":"index"},{"id":0,"type":2,"name":"测试","appid":"wx1373169e494e0c39","path":"index"}]}',
  12. 'store_name' => '测试测试',
  13. 'contract_phone' => '1769360227',
  14. 'hour' => '00:00-11:11',
  15. 'company_name' => '测试数据啊',
  16. 'credential' => '13082741523645',
  17. 'address' => 'xxxxxxxx',
  18. 'qualification_list' => '3LaLzqiTrQcD20DlX_o-OV1-nlYMu7sdVAL7SV2PrxVyjZFZZmB3O6LPGaYXlZWq',
  19. ];
  20. $wxa->program()->add($program);
  21. /**
  22. * 删除地点 Api: $wxa->program()->delete()
  23. * @param string $poiId 附近地点 ID
  24. */
  25. function delete(string $poiId)
  26. /**
  27. * 查看地点列表 Api: $wxa->program()->getList()
  28. * @param int $page 起始页id(从1开始计数)
  29. * @param int $pageRows 每页展示个数(最多1000个)
  30. */
  31. function getList(int $page = 1 ,int $pageRows)
  32. /**
  33. * 展示/取消展示附近小程序 Api: $wxa->program()->setShowStatus()
  34. * @param string $poiId 附近地点 ID
  35. * @param int $status 是否展示 0: 不展示,1:展示
  36. */
  37. function setShowStatus(string $poiId ,int $status)