snsapi userinfo php,修改新版ECTOUCH微信登录snsapi_userinfo改为snsapi_base

Wechat.class.

[code lang=”

}

$result = $this->http_get(self::API_BASE_URL_PREFIX.self::OAUTH_USERINFO_URL.’access_token=’.$access_token.’&openid=’.$openid."&lang=zh_CN");

//var_dump($result);

if ($result)

{

$json = json_decode($result,true);

if (!$json || !empty($json[‘errcode’])) {

$this->errCode = $json[‘errcode’];

$this->errMsg = $json[‘errmsg’];

return false;

}

return $json;

}

return false;

}

public function get_token(){//

WechatController.class.php

[code lang=”php”]

static function do_oauth()

{

// 默认公众号信息

$wxinfo = model(‘Base’)->model->table(‘wechat’)

->field(‘id, token, appid, appsecret, oauth_redirecturi, type, oauth_status’)

->where(‘default_wx = 1 and status = 1’)

->find();

if (! empty($wxinfo) && $wxinfo[‘type’] == 2) {

$config[‘token’] = $wxinfo[‘token’];

$config[‘appid’] = $wxinfo[‘appid’];

$config[‘appsecret’] = $wxinfo[‘appsecret’];

// 微信通验证

$weObj = new Wechat($config);

// 微信浏览器浏览

if (self::is_wechat_browser() && ($_SESSION[‘user_id’] === 0 || empty($_SESSION[‘openid’])) && !empty($wxinfo[‘oauth_status’])) {

if (! isset($_SESSION[‘redirect_url’])) {

session(‘redirect_url’, __HOST__ . $_SERVER[‘REQUEST_URI’]);

}

$url = $weObj->getOauthRedirect($wxinfo[‘oauth_redirecturi’], 1,’snsapi_base’);

if (isset($_GET[‘code’]) && !empty($_GET[‘code’])) {

$token = $weObj->getOauthAccessToken();

if ($token) {

$userinfo = $weObj->getOauthUserinfo($token[‘access_token’], $token[‘openid’],’snsapi_base’);

//var_dump($userinfo);

// exit;

self::update_weixin_user($userinfo, $wxinfo[‘id’], $weObj);

if (! empty($_SESSION[‘redirect_url’])) {

$redirect_url = session(‘redirect_url’);

header(‘Location:’ . $redirect_url, true, 302);

exit();

}

} else {

header(‘Location:’ . $url, true, 302);

exit();

}

} else {

// var_dump($url);

header(‘Location:’ . $url);

exit();

}

}

}

}

[/code]

这里去掉 ‘snsapi_base’ 就变为原来的 snsapi_userinfo 授权页面登录。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
由于微信小程序获取手机号授权用户登录和注册功能需要与微信开放平台进行交互,因此需要使用微信开放平台提供的API接口进行开发。以下是Java实现微信、手机号登录的示例代码。 微信登录: ``` @RequestMapping(value = "/wxLogin", method = RequestMethod.POST) public ResponseEntity<Map<String, Object>> wxLogin(@RequestBody Map<String, String> params) { String code = params.get("code"); String encryptedData = params.get("encryptedData"); String iv = params.get("iv"); // 调用微信开放平台接口获取用户信息 String sessionKey = getWxSessionKey(code); String userInfo = decryptUserInfo(encryptedData, sessionKey, iv); // 解析用户信息并进行登录操作 // ... // 返回登录结果 Map<String, Object> result = new HashMap<>(); result.put("code", 0); result.put("msg", "登录成功"); return ResponseEntity.ok(result); } private String getWxSessionKey(String code) { String url = "https://api.weixin.qq.com/sns/jscode2session?appid={APPID}&secret={SECRET}&js_code={CODE}&grant_type=authorization_code"; url = url.replace("{APPID}", APPID) .replace("{SECRET}", SECRET) .replace("{CODE}", code); RestTemplate restTemplate = new RestTemplate(); String response = restTemplate.getForObject(url, String.class); JSONObject json = JSONObject.parseObject(response); return json.getString("session_key"); } private String decryptUserInfo(String encryptedData, String sessionKey, String iv) { try { byte[] encryptedDataByte = Base64.decodeBase64(encryptedData); byte[] sessionKeyByte = Base64.decodeBase64(sessionKey); byte[] ivByte = Base64.decodeBase64(iv); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); SecretKeySpec secretKeySpec = new SecretKeySpec(sessionKeyByte, "AES"); IvParameterSpec ivParameterSpec = new IvParameterSpec(ivByte); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec); byte[] resultByte = cipher.doFinal(encryptedDataByte); return new String(resultByte, StandardCharsets.UTF_8); } catch (Exception e) { throw new RuntimeException("解密用户信息失败", e); } } ``` 手机号登录: ``` @RequestMapping(value = "/phoneLogin", method = RequestMethod.POST) public ResponseEntity<Map<String, Object>> phoneLogin(@RequestBody Map<String, String> params) { String phone = params.get("phone"); String code = params.get("code"); // 验证手机号和验证码是否匹配 // ... // 进行登录操作 // ... // 返回登录结果 Map<String, Object> result = new HashMap<>(); result.put("code", 0); result.put("msg", "登录成功"); return ResponseEntity.ok(result); } ``` 注册功能: ``` @RequestMapping(value = "/register", method = RequestMethod.POST) public ResponseEntity<Map<String, Object>> register(@RequestBody Map<String, String> params) { String phone = params.get("phone"); String code = params.get("code"); String password = params.get("password"); // 验证手机号和验证码是否匹配 // ... // 注册用户 // ... // 返回注册结果 Map<String, Object> result = new HashMap<>(); result.put("code", 0); result.put("msg", "注册成功"); return ResponseEntity.ok(result); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值