123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
- namespace addons\unishop\extend;
- use addons\unishop\model\Config;
- use addons\unishop\model\UserExtend;
- use EasyWeChat\Factory;
- use think\Cache;
- use think\Session;
- class Wechat
- {
- public static function initEasyWechat($type = 'miniProgram')
- {
- $config = [
-
- 'app_id' => Config::getByName('app_id')['value'],
- 'secret' => Config::getByName('secret')['value'],
-
-
-
- ];
- switch ($type) {
- case 'miniProgram':
- return Factory::miniProgram($config);
- break;
- case 'payment':
- $config['mch_id'] = Config::getByName('mch_id')['value'];
- $config['key'] = Config::getByName('key')['value'];
-
- $config['cert_path'] = Config::getByName('cert_path')['value'];
- $config['key_path'] = Config::getByName('key_path')['value'];
- $config['notify_url'] = Config::getByName('notify_url')['value'];;
- return Factory::payment($config);
- break;
- }
- }
-
- public static function authSession($code)
- {
- $app = self::initEasyWechat('miniProgram');
- $result = $app->auth->session($code);
- if (isset($result['session_key']) && isset($result['openid'])) {
-
- Session::set('session_key', $result['session_key']);
- Session::set('openid', $result['openid']);
- $result['userInfo'] = (new UserExtend())->getUserInfoByOpenid($result['openid']);
- $result['userInfo']['openid'] = $result['openid'];
- unset($result['session_key']);
- }
- return $result;
- }
-
- public static function getOpenidByUserId($userId)
- {
- $openid = Cache::get('openid_' . $userId);
- if (empty($openid)) {
- $userExtend = (new UserExtend())->where(['user_id' => $userId])->field('openid')->find();
- if (empty($userExtend['openid'])) {
- return false;
- }
- $openid = $userExtend['openid'];
- Cache::set('openid_' . $userId, $openid, 7200);
- }
- return $openid;
- }
-
- public static function paySign($params, $key)
- {
- ksort($params);
- $string = "";
- foreach ($params as $k => $v) {
- if ($k != "sign" && $v != "" && !is_array($v)) {
- $string .= $k . "=" . $v . "&";
- }
- }
- $string = $string . "key=" . $key;
-
- return strtoupper(md5($string));
- }
-
- public static function h5InWechat()
- {
- if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'],'MicroMessenger') !== false ) {
- return true;
- }
- return false;
- }
- }
|