Crypt.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | WeChatDeveloper
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2023 ThinkAdmin [ thinkadmin.top ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免责声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/WeChatDeveloper
  13. // | github 代码仓库:https://github.com/zoujingli/WeChatDeveloper
  14. // +----------------------------------------------------------------------
  15. namespace WeMini;
  16. use WeChat\Contracts\BasicWeChat;
  17. use WeChat\Contracts\Tools;
  18. use WeChat\Exceptions\InvalidDecryptException;
  19. use WeChat\Exceptions\InvalidResponseException;
  20. use WXBizDataCrypt;
  21. /**
  22. * 数据加密处理
  23. * Class Crypt
  24. * @package WeMini
  25. */
  26. class Crypt extends BasicWeChat
  27. {
  28. /**
  29. * 数据签名校验
  30. * @param string $iv
  31. * @param string $sessionKey
  32. * @param string $encryptedData
  33. * @return bool|array
  34. */
  35. public function decode($iv, $sessionKey, $encryptedData)
  36. {
  37. require_once __DIR__ . DIRECTORY_SEPARATOR . 'crypt' . DIRECTORY_SEPARATOR . 'wxBizDataCrypt.php';
  38. $pc = new WXBizDataCrypt($this->config->get('appid'), $sessionKey);
  39. $errCode = $pc->decryptData($encryptedData, $iv, $data);
  40. if ($errCode == 0) {
  41. return json_decode($data, true);
  42. }
  43. return false;
  44. }
  45. /**
  46. * 登录凭证校验
  47. * @param string $code 登录时获取的 code
  48. * @return array
  49. * @throws \WeChat\Exceptions\LocalCacheException
  50. */
  51. public function session($code)
  52. {
  53. $appid = $this->config->get('appid');
  54. $secret = $this->config->get('appsecret');
  55. $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code";
  56. return json_decode(Tools::get($url), true);
  57. }
  58. /**
  59. * 换取用户信息
  60. * @param string $code 用户登录凭证(有效期五分钟)
  61. * @param string $iv 加密算法的初始向量
  62. * @param string $encryptedData 加密数据( encryptedData )
  63. * @return array
  64. * @throws \WeChat\Exceptions\InvalidDecryptException
  65. * @throws \WeChat\Exceptions\InvalidResponseException
  66. * @throws \WeChat\Exceptions\LocalCacheException
  67. */
  68. public function userInfo($code, $iv, $encryptedData)
  69. {
  70. $result = $this->session($code);
  71. if (empty($result['session_key'])) {
  72. throw new InvalidResponseException('Code 换取 SessionKey 失败', 403);
  73. }
  74. $userinfo = $this->decode($iv, $result['session_key'], $encryptedData);
  75. if (empty($userinfo)) {
  76. throw new InvalidDecryptException('用户信息解析失败', 403);
  77. }
  78. return array_merge($result, $userinfo);
  79. }
  80. /**
  81. * 通过授权码换取手机号
  82. * @param string $code
  83. * @return array
  84. * @throws \WeChat\Exceptions\InvalidResponseException
  85. * @throws \WeChat\Exceptions\LocalCacheException
  86. */
  87. public function getPhoneNumber($code)
  88. {
  89. $url = 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=ACCESS_TOKEN';
  90. $this->registerApi($url, __FUNCTION__, func_get_args());
  91. return $this->httpPostForJson($url, ['code' => $code], true);
  92. }
  93. /**
  94. * 用户支付完成后,获取该用户的 UnionId
  95. * @param string $openid 支付用户唯一标识
  96. * @param null|string $transaction_id 微信支付订单号
  97. * @param null|string $mch_id 微信支付分配的商户号,和商户订单号配合使用
  98. * @param null|string $out_trade_no 微信支付商户订单号,和商户号配合使用
  99. * @return array
  100. * @throws \WeChat\Exceptions\InvalidResponseException
  101. * @throws \WeChat\Exceptions\LocalCacheException
  102. */
  103. public function getPaidUnionId($openid, $transaction_id = null, $mch_id = null, $out_trade_no = null)
  104. {
  105. $url = "https://api.weixin.qq.com/wxa/getpaidunionid?access_token=ACCESS_TOKEN&openid={$openid}";
  106. if (is_null($mch_id)) $url .= "&mch_id={$mch_id}";
  107. if (is_null($out_trade_no)) $url .= "&out_trade_no={$out_trade_no}";
  108. if (is_null($transaction_id)) $url .= "&transaction_id={$transaction_id}";
  109. $this->registerApi($url, __FUNCTION__, func_get_args());
  110. return $this->callGetApi($url);
  111. }
  112. }