Wechat.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zhengmingwei
  5. * Date: 2020/3/7
  6. * Time: 4:09 PM
  7. */
  8. namespace addons\unishop\extend;
  9. use addons\unishop\model\Config;
  10. use addons\unishop\model\UserExtend;
  11. use EasyWeChat\Factory;
  12. use think\Cache;
  13. use think\Session;
  14. class Wechat
  15. {
  16. public static function initEasyWechat($type = 'miniProgram')
  17. {
  18. $config = [
  19. // 必要配置
  20. 'app_id' => Config::getByName('app_id')['value'],
  21. 'secret' => Config::getByName('secret')['value'],
  22. // 下面为可选项
  23. // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  24. //'response_type' => 'array',
  25. // 'log' => [
  26. // 'level' => 'debug',
  27. // 'file' => __DIR__.'/wechat.log',
  28. // ],
  29. ];
  30. switch ($type) {
  31. case 'miniProgram':
  32. return Factory::miniProgram($config);
  33. break;
  34. case 'payment':
  35. $config['mch_id'] = Config::getByName('mch_id')['value'];
  36. $config['key'] = Config::getByName('key')['value'];
  37. // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
  38. $config['cert_path'] = Config::getByName('cert_path')['value']; // XXX: 绝对路径!!!!
  39. $config['key_path'] = Config::getByName('key_path')['value']; // XXX: 绝对路径!!!!
  40. $config['notify_url'] = Config::getByName('notify_url')['value'];; // 你也可以在下单时单独设置来想覆盖它
  41. return Factory::payment($config);
  42. break;
  43. }
  44. }
  45. /**
  46. * 小程序登录
  47. */
  48. public static function authSession($code)
  49. {
  50. $app = self::initEasyWechat('miniProgram');
  51. $result = $app->auth->session($code);
  52. if (isset($result['session_key']) && isset($result['openid'])) {
  53. //储存session_key 用来解密微信用户授权数据
  54. Session::set('session_key', $result['session_key']);
  55. Session::set('openid', $result['openid']);
  56. $result['userInfo'] = (new UserExtend())->getUserInfoByOpenid($result['openid']);
  57. $result['userInfo']['openid'] = $result['openid'];
  58. unset($result['session_key']);
  59. }
  60. return $result;
  61. }
  62. /**
  63. * 根据user_id获取用户Openid
  64. * @param $userId
  65. * @return bool|mixed
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. * @throws \think\exception\DbException
  69. */
  70. public static function getOpenidByUserId($userId)
  71. {
  72. $openid = Cache::get('openid_' . $userId);
  73. if (empty($openid)) {
  74. $userExtend = (new UserExtend())->where(['user_id' => $userId])->field('openid')->find();
  75. if (empty($userExtend['openid'])) {
  76. return false;
  77. }
  78. $openid = $userExtend['openid'];
  79. Cache::set('openid_' . $userId, $openid, 7200);
  80. }
  81. return $openid;
  82. }
  83. /**
  84. * 小程序调起支付数据签名
  85. * https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_7&index=5
  86. * @param array $params
  87. * @param string $key
  88. * @return string
  89. */
  90. public static function paySign($params, $key)
  91. {
  92. ksort($params);
  93. $string = "";
  94. foreach ($params as $k => $v) {
  95. if ($k != "sign" && $v != "" && !is_array($v)) {
  96. $string .= $k . "=" . $v . "&";
  97. }
  98. }
  99. $string = $string . "key=" . $key;
  100. //$String= "appId=xxxxx&nonceStr=xxxxx&package=prepay_id="xxxxx&signType=MD5&timeStamp=xxxxx&key=xxxxx"
  101. return strtoupper(md5($string));
  102. }
  103. /**
  104. * 判断H5页面是否在微信内
  105. */
  106. public static function h5InWechat()
  107. {
  108. if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'],'MicroMessenger') !== false ) {
  109. return true;
  110. }
  111. return false;
  112. }
  113. }