Wechat.php 4.4 KB

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