123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- package com.yingying.tourist.service.impl;
-
- import java.io.UnsupportedEncodingException;
-
- import org.springframework.stereotype.Service;
-
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.yingying.tourist.common.HttpRequestSimple;
- import com.yingying.tourist.domain.User;
- import com.yingying.tourist.service.WechatService;
-
- @Service
- public class WechatServiceImpl implements WechatService {
-
-
-
- // private static String appId = "wx00b0504317473c38";
- // private static String appSecret = "88d1172e0a53444298efe61c763bf894";
-
- /**
- * 微信测试号
- */
- private static String appId = "wx8a98f5f7df1e89b5";
- private static String appSecret = "f622f3300cb3d12d01f8e82ff1cce1ee";
-
- /**
- * 获取微信服务端token
- * @return
- */
- @Override
- public String getToken() {
- String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret="
- + appSecret;
- String result = HttpRequestSimple.doGet(url);
-
- JSONObject jsonObject = JSONObject.parseObject(result);
-
- String token = jsonObject.getString("access_token");
- return token;
- }
-
- /**
- * JS-SDK使用权限签名算法 获取ticket
- *
- * @return
- */
- @Override
- public String getTicket() {
-
- String token = getToken();
-
- String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + token + "&type=jsapi";
- String result = HttpRequestSimple.doGet(url);
-
- JSONObject jsonObject = JSONObject.parseObject(result);
-
- String ticket = jsonObject.getString("ticket");
- return ticket;
- }
-
- /**
- * 通过code换取网页授权access_token
- *
- * @param code
- * @return
- */
- @Override
- public String getAuthToken(String code) {
-
- String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appId + "&secret=" + appSecret + "&code=" + code + "&grant_type=authorization_code";
- String result = HttpRequestSimple.doGet(url);
-
- return result;
- }
-
-
- @Override
- public User getWechatUser(String token, String openId) throws UnsupportedEncodingException {
- String url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + token + "&openid=" + openId + "&lang=zh_CN";
- String result = HttpRequestSimple.doGet(url);
-
- String decodeResult = new String(result.getBytes("ISO-8859-1"), "UTF-8");
-
- User user = JSON.parseObject(decodeResult, User.class);
-
- return user;
-
-
- }
-
- }
|