Search in sources :

Example 1 with ObtainSessionWithJsCodeModel

use of build.dream.catering.models.miniprogram.ObtainSessionWithJsCodeModel in project erp-catering by liuyandong33.

the class MiniProgramController method obtainSessionWithJsCode.

@RequestMapping(value = "/obtainSessionWithJsCode")
@ResponseBody
public String obtainSessionWithJsCode() {
    ApiRest apiRest = null;
    Map<String, String> requestParameters = ApplicationHandler.getRequestParameters();
    try {
        ObtainSessionWithJsCodeModel obtainSessionWithJsCodeModel = ApplicationHandler.instantiateObject(ObtainSessionWithJsCodeModel.class, requestParameters);
        obtainSessionWithJsCodeModel.validateAndThrow();
        Map<String, String> obtainSessionRequestParameters = new HashMap<String, String>();
        obtainSessionRequestParameters.put("appid", "wxc375e036b110a28f");
        obtainSessionRequestParameters.put("secret", "415627f3ec7bf51497a98e94546affa7");
        obtainSessionRequestParameters.put("js_code", obtainSessionWithJsCodeModel.getCode());
        obtainSessionRequestParameters.put("grant_type", "authorization_code");
        String obtainSessionResult = WebUtils.doGetWithRequestParameters("https://api.weixin.qq.com/sns/jscode2session", obtainSessionRequestParameters);
        JSONObject obtainSessionResultJsonObject = JSONObject.fromObject(obtainSessionResult);
        Validate.isTrue(!obtainSessionResultJsonObject.has("errcode"), obtainSessionResultJsonObject.optString("errmsg"));
        apiRest = new ApiRest();
        apiRest.setData(obtainSessionResultJsonObject);
        apiRest.setMessage("处理成功!");
        apiRest.setSuccessful(true);
    } catch (Exception e) {
        LogUtils.error("code换取session_key失败", controllerSimpleName, "obtainSessionWithJsCode", e, requestParameters);
        apiRest = new ApiRest(e);
    }
    return GsonUtils.toJson(apiRest);
}
Also used : JSONObject(net.sf.json.JSONObject) HashMap(java.util.HashMap) ApiRest(build.dream.common.api.ApiRest) ObtainSessionWithJsCodeModel(build.dream.catering.models.miniprogram.ObtainSessionWithJsCodeModel) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

ObtainSessionWithJsCodeModel (build.dream.catering.models.miniprogram.ObtainSessionWithJsCodeModel)1 ApiRest (build.dream.common.api.ApiRest)1 HashMap (java.util.HashMap)1 JSONObject (net.sf.json.JSONObject)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1