OpenIdTest.java 3.18 KB
package com.xkl;

import org.json.JSONObject;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by win7 on 2017/1/2.
 */
public class OpenIdTest {
    private static final String URL_OPEN_ID = Constants.URL_PREFIX + "/openId";
    private static final String URL_LOGIN = Constants.URL_PREFIX + "/openId/login";
    private static final String URL_COMPARE = Constants.URL_PREFIX + "/report/compare";

    /**
     * OpenId绑定接口
     */
    @Test
    public void testBind(){
        bindWithOpenIdAndUser();
    }

    public static String bindWithOpenIdAndUser(){
        Map<String,String> params = new HashMap<String, String>();
        params.put("username","user1");
        params.put("password",EncodeTools.encode("MD5","pass1"));
        params.put("openId","abcdefghijklmnopqrstuvwxyz123456");
        params.put("openIdType","0");

        params.put("t",HttpTools.getNow());
        params.put("type","1");
        params.put("sign",HttpTools.getSign(params));
        String response = HttpTools.requestByMap(URL_OPEN_ID,"POST",params);
        return response;
    }

    /**
     * OpenId解除绑定接口
     */
    @Test
    public void testUnBind(){
        Map<String,String> params = new HashMap<String, String>();
        params.put("username","user1");
        params.put("password",EncodeTools.encode("MD5","pass1"));
        params.put("openId","abcdefghijklmnopqrstuvwxyz123456");
        params.put("openIdType","0");

        params.put("t",HttpTools.getNow());
        params.put("type","1");
        params.put("sign",HttpTools.getSign(params));
        String response = HttpTools.requestByMap(URL_OPEN_ID,"DELETE",params);
        System.out.println(response);
    }

    /**
     * OpenId登陆接口
     */
    @Test
    public void testLogin(){
        System.out.println(bindWithOpenIdAndLoginAndGetToken("abcdefghijklmnopqrstuvwxyz123456","0"));
    }

    public static String bindWithOpenIdAndLoginAndGetToken(String openId, String openIdType){
        bindWithOpenIdAndUser();
        Map<String,String> params = new HashMap<String, String>();
        params.put("openId",openId);
        params.put("openIdType",openIdType);

        params.put("t",HttpTools.getNow());
        params.put("type","1");
        params.put("sign",HttpTools.getSign(params));
        String response = HttpTools.requestByMap(URL_LOGIN,"POST",params);

        System.out.println("response:"+ response);
        JSONObject jsonObject = new JSONObject(response);
        JSONObject content = jsonObject.getJSONObject("content");
        return content.getString("userId") + "_" + content.getString("token");
    }

    /**
     * 微信端体检报告分年龄比较接口
     */
    @Test
    public void testCompare(){
        String token = bindWithOpenIdAndLoginAndGetToken("abcdefghijklmnopqrstuvwxyz123456","0");
        Map<String,String> params = new HashMap<String, String>();
        params.put("report_id","77");

        params.put("t",HttpTools.getNow());
        params.put("type","1");
        params.put("sign",HttpTools.getSign(params));
        String response = HttpTools.requestByMapWithToken(URL_COMPARE,"POST",params,token);
        System.out.println(response);
    }
}