Crypt.php 4.5 KB

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