国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

Crypt.php 3.8KB

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