Browse Source

微信公众号接口开发

chenyongxing 8 years ago
parent
commit
d0836afa03

+ 0 - 5
svr/svr-base/src/main/java/com/yihu/jw/wx/controller/WxMenuController.java

@ -18,10 +18,6 @@ import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
@ -145,7 +141,6 @@ public class WxMenuController extends EnvelopRestController {
            @ApiParam(name = "wechatCode", value = "", defaultValue = "")
            @RequestParam(value = "wechatCode", required = true)String wechatCode){
        try{
            String params ="";
            JSONObject result = wxMenuService.createWechatMenu(wechatCode);
            if(result != null && result.get("errcode").toString().equals("0") && result.getString("errmsg").equals("ok")){
                return Envelop.getSuccess("创建成功",result );

+ 29 - 27
svr/svr-base/src/main/java/com/yihu/jw/wx/service/WxAccessTokenService.java

@ -17,6 +17,7 @@ import org.springframework.util.StringUtils;
import org.springside.modules.utils.Clock;
import java.util.List;
import java.util.UUID;
/**
 * Created by Administrator on 2017/5/18 0018.
@ -47,34 +48,35 @@ public class WxAccessTokenService extends BaseJpaService<WxAccessToken, WxAccess
                        break;
                    }
                }
                String token_url = "https://api.weixin.qq.com/cgi-bin/token";
                String appId="";
                String appSecret="";
                //根据wechatCode查找出appid和appSecret
                WxWechat wxWechat = wechatDao.findByCode(wechatCode);
                if(wxWechat==null){
                    return null;
                }
                appId = wxWechat.getAppId();
                appSecret = wxWechat.getAppSecret();
                String params = "grant_type=client_credential&appid=" + appId + "&secret=" + appSecret;
                String result = HttpUtil.sendGet(token_url, params);
                JSONObject json = new JSONObject(result);
                if (json.has("access_token")) {
                    String token = json.get("access_token").toString();
                    String expires_in = json.get("expires_in").toString();
                    WxAccessToken newaccessToken = new WxAccessToken();
                    newaccessToken.setAccessToken(token);
                    newaccessToken.setExpiresIn(Long.parseLong(expires_in));
                    newaccessToken.setAddTimestamp(System.currentTimeMillis());
                    newaccessToken.setCzrq(new Clock.DefaultClock().getCurrentDate());
                    wxAccessTokenDao.save(newaccessToken);
                    return newaccessToken;
                } else {
                    return null;
                }
            }
            return null;
            String token_url = "https://api.weixin.qq.com/cgi-bin/token";
            String appId="";
            String appSecret="";
            //根据wechatCode查找出appid和appSecret
            WxWechat wxWechat = wechatDao.findByCode(wechatCode);
            if(wxWechat==null){
                return null;
            }
            appId = wxWechat.getAppId();
            appSecret = wxWechat.getAppSecret();
            String params = "grant_type=client_credential&appid=" + appId + "&secret=" + appSecret;
            String result = HttpUtil.sendGet(token_url, params);
            JSONObject json = new JSONObject(result);
            if (json.has("access_token")) {
                String token = json.get("access_token").toString();
                String expires_in = json.get("expires_in").toString();
                WxAccessToken newaccessToken = new WxAccessToken();
                newaccessToken.setAccessToken(token);
                newaccessToken.setExpiresIn(Long.parseLong(expires_in));
                newaccessToken.setAddTimestamp(System.currentTimeMillis());
                newaccessToken.setCzrq(new Clock.DefaultClock().getCurrentDate());
                newaccessToken.setCode(UUID.randomUUID().toString().replace("-",""));
                newaccessToken.setWechatCode(wechatCode);
                wxAccessTokenDao.save(newaccessToken);
                return newaccessToken;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;