WxController.java 3.17 KB
package com.example.controller;

import com.example.models.ImgInfo;
import com.example.service.ImgInfoService;
import com.example.service.http.ClientConfigService;
import com.example.service.http.ClientConfigServiceImpl;
import com.example.service.http.HttpPostService;
import com.example.support.model.MessageEntity;
import com.example.utils.file.DefaultFileUpload;
import com.example.utils.file.FileUploadInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.wink.client.ClientResponse;
import org.apache.wink.client.Resource;
import org.apache.wink.client.RestClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * Created by JA on 17/7/3.
 */
@RequestMapping("/kmr/wx")
@RestController
public class WxController {

//    String jssdkurl = "http://gfwp.gac-toyota.com.cn/GTMCfamily/index.php/campaign/api2/getjssdk";
//
//    String getopenidurl = "http://gfwp.gac-toyota.com.cn/GTMCfamily/index.php/campaign/api2/getjssdk";
//
//    @Autowired
//    private HttpPostService httpPostService;
//
//
//    @RequestMapping("/getjssdk")
//    public MessageEntity getjssdk(HttpServletRequest request, @RequestParam String url) {
//        MessageEntity.Builder builder = new MessageEntity.Builder(request);
//        String newUrl = jssdkurl + "?url="+ url;
//        Map<String, Object> map = null;
//        try {
//            map = httpPostService.httpPost(newUrl, null);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return builder.success(true).code("2000").content(map).create();
//    }


    @RequestMapping("/getUser")
    public MessageEntity oauth(HttpServletRequest request, @RequestParam String access_token, String openid) {
        MessageEntity.Builder builder = new MessageEntity.Builder(request);
            ClientConfigService configService = new ClientConfigServiceImpl();
            RestClient restClient = new RestClient(configService.getClientConfig());
            Resource resource = restClient.resource("https://api.weixin.qq.com/sns/userinfo");
            resource.queryParam("access_token", access_token);
            resource.queryParam("openid", openid);
            resource.queryParam("lang", "zh_CN");
            ClientResponse response = resource.get();
            String responseEntity = response.getEntity(String.class);
        Map readValue = null;
//        System.out.println("responseEntity:::"+responseEntity);
            ObjectMapper mapper = new ObjectMapper();
            try {
                readValue = mapper.readValue(responseEntity, Map.class);
                if (null != readValue.get("errcode")) {
                    return builder.success(false).code("999").content(readValue).create();
                }
            } catch (IOException e) {
                e.printStackTrace();;
            }
        return builder.success(true).code("2000").content(readValue).create();
    }


}