java微信接口

java

浏览数:633

2019-1-8

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.developerworld.commons.lang.web.RequestUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.url.util.UrlUtil;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONObject;

@Controller("/prj/shareController")
@RequestMapping("/prj/share")
public class ShareController{
    /**
     * 获取access_token
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ResponseBody
    public String getAccessToken() throws Exception {
        String data = "";
        try {
            final String access_token_url = "http://wxwx.com/api/common/external/getAccessToken?shvKey=xxxxxxxxxxxxxxx";
            Map<String, Object> map = UrlUtil.httpGetM(access_token_url);
            Map<String, Object> obj = (Map<String, Object>) map.get("data");
            data = (String) obj.get("access_token");
        } catch (Exception e) {
            e.getMessage();
        }
        return data;
    }

    /**
     * 获取微信jsapi_ticket
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getjsapiticket", method = RequestMethod.GET)
    @ResponseBody
    public String getJsapiTicket() throws Exception {
        String ticket = "";
        try {
            final String access_token_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";
            String requestUrl = access_token_url.replace("ACCESS_TOKEN", getAccessToken());
            JSONObject jsonObject = UrlUtil.httpRequest(requestUrl, "GET", null);
            if (Integer.valueOf(jsonObject.getString("errcode")) != 0)
                jsonObject = UrlUtil.httpRequest(requestUrl, "GET", null);
            ticket = jsonObject.getString("ticket");
        } catch (Exception e) {
            e.getMessage();
        }
        return ticket;
    }
    /**
     * 获取微信签名
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getsignature", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getSignature(HttpServletRequest request,String url) throws Exception {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        Map<String, Object> map = new HashMap<String, Object>();
        RequestUtils requestUtils = new RequestUtils(request);
        Map<String, Object> requestMap = requestUtils.toSmartMap();
        url = (String) requestMap.get("url");
        String signature = "";
        try {
            String jsapi_ticket = getJsapiTicket();
            String noncestr = UUID.randomUUID().toString().replaceAll("-", "");
            //String url = request.getRequestURL().toString();
            //if(request.getQueryString() != null){
            //  url+= "?" + request.getQueryString();
            //}
            String timestamp = Long.toString(System.currentTimeMillis() / 1000);
            //参数名必须全部小写,且必须有序
            String tempString = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + noncestr + "&timestamp=" + timestamp + "&url=" + url;
            //sha1加密
            signature = DigestUtils.shaHex(tempString);
            //signature = DigestUtils.sha1Hex(jsapi_ticket + "jsapi_ticket" + noncestr + "&noncestr" + timestamp + "&timestamp" + url + "&url");
            map.put("jsapi_ticket", jsapi_ticket);
            map.put("noncestr", noncestr);
            map.put("timestamp", timestamp);
            map.put("url", url);
            map.put("signature", signature);

        } catch (Exception e) {
            e.getMessage();
        }
        return map;
    }
}