Browse Source

规范化代码

master
Binary Wang 6 years ago
parent
commit
73945bfb1a
3 changed files with 31 additions and 30 deletions
  1. +9
    -5
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpUserService.java
  2. +12
    -16
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpUserServiceImpl.java
  3. +10
    -9
      weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpUserServiceImplTest.java

+ 9
- 5
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpUserService.java View File

@@ -1,18 +1,22 @@
package me.chanjar.weixin.mp.api; package me.chanjar.weixin.mp.api;


import java.util.List;

import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.bean.WxMpUserQuery; import me.chanjar.weixin.mp.bean.WxMpUserQuery;
import me.chanjar.weixin.mp.bean.result.WxMpUser; import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.bean.result.WxMpUserList; import me.chanjar.weixin.mp.bean.result.WxMpUserList;


import java.util.List;

/** /**
* 用户管理相关操作接口
* 用户管理相关操作接口.
* *
* @author Binary Wang * @author Binary Wang
*/ */
public interface WxMpUserService { public interface WxMpUserService {
String USER_INFO_BATCH_GET_URL = "https://api.weixin.qq.com/cgi-bin/user/info/batchget";
String USER_GET_URL = "https://api.weixin.qq.com/cgi-bin/user/get";
String USER_INFO_URL = "https://api.weixin.qq.com/cgi-bin/user/info";
String USER_INFO_UPDATE_REMARK_URL = "https://api.weixin.qq.com/cgi-bin/user/info/updateremark";


/** /**
* <pre> * <pre>
@@ -61,9 +65,9 @@ public interface WxMpUserService {
* 接口地址:https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=ACCESS_TOKEN * 接口地址:https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=ACCESS_TOKEN
* </pre> * </pre>
* *
* @param openids 用户openid列表
* @param openidList 用户openid列表
*/ */
List<WxMpUser> userInfoList(List<String> openids) throws WxErrorException;
List<WxMpUser> userInfoList(List<String> openidList) throws WxErrorException;


/** /**
* <pre> * <pre>


+ 12
- 16
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpUserServiceImpl.java View File

@@ -1,5 +1,7 @@
package me.chanjar.weixin.mp.api.impl; package me.chanjar.weixin.mp.api.impl;


import java.util.List;

import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpService;
@@ -8,13 +10,12 @@ import me.chanjar.weixin.mp.bean.WxMpUserQuery;
import me.chanjar.weixin.mp.bean.result.WxMpUser; import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.bean.result.WxMpUserList; import me.chanjar.weixin.mp.bean.result.WxMpUserList;


import java.util.List;

/** /**
* Created by Binary Wang on 2016/7/21. * Created by Binary Wang on 2016/7/21.
*
* @author BinaryWang
*/ */
public class WxMpUserServiceImpl implements WxMpUserService { public class WxMpUserServiceImpl implements WxMpUserService {
private static final String API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/user";
private WxMpService wxMpService; private WxMpService wxMpService;


public WxMpUserServiceImpl(WxMpService wxMpService) { public WxMpUserServiceImpl(WxMpService wxMpService) {
@@ -23,11 +24,10 @@ public class WxMpUserServiceImpl implements WxMpUserService {


@Override @Override
public void userUpdateRemark(String openid, String remark) throws WxErrorException { public void userUpdateRemark(String openid, String remark) throws WxErrorException {
String url = API_URL_PREFIX + "/info/updateremark";
JsonObject json = new JsonObject(); JsonObject json = new JsonObject();
json.addProperty("openid", openid); json.addProperty("openid", openid);
json.addProperty("remark", remark); json.addProperty("remark", remark);
this.wxMpService.post(url, json.toString());
this.wxMpService.post(USER_INFO_UPDATE_REMARK_URL, json.toString());
} }


@Override @Override
@@ -37,32 +37,28 @@ public class WxMpUserServiceImpl implements WxMpUserService {


@Override @Override
public WxMpUser userInfo(String openid, String lang) throws WxErrorException { public WxMpUser userInfo(String openid, String lang) throws WxErrorException {
String url = API_URL_PREFIX + "/info";
lang = lang == null ? "zh_CN" : lang; lang = lang == null ? "zh_CN" : lang;
String responseContent = this.wxMpService.get(url,
String responseContent = this.wxMpService.get(USER_INFO_URL,
"openid=" + openid + "&lang=" + lang); "openid=" + openid + "&lang=" + lang);
return WxMpUser.fromJson(responseContent); return WxMpUser.fromJson(responseContent);
} }


@Override @Override
public WxMpUserList userList(String next_openid) throws WxErrorException {
String url = API_URL_PREFIX + "/get";
String responseContent = this.wxMpService.get(url,
next_openid == null ? null : "next_openid=" + next_openid);
public WxMpUserList userList(String nextOpenid) throws WxErrorException {
String responseContent = this.wxMpService.get(USER_GET_URL,
nextOpenid == null ? null : "next_openid=" + nextOpenid);
return WxMpUserList.fromJson(responseContent); return WxMpUserList.fromJson(responseContent);
} }


@Override @Override
public List<WxMpUser> userInfoList(List<String> openids)
public List<WxMpUser> userInfoList(List<String> openidList)
throws WxErrorException { throws WxErrorException {
return this.userInfoList(new WxMpUserQuery(openids));
return this.userInfoList(new WxMpUserQuery(openidList));
} }


@Override @Override
public List<WxMpUser> userInfoList(WxMpUserQuery userQuery) throws WxErrorException { public List<WxMpUser> userInfoList(WxMpUserQuery userQuery) throws WxErrorException {
String url = API_URL_PREFIX + "/info/batchget";
String responseContent = this.wxMpService.post(url,
userQuery.toJsonString());
String responseContent = this.wxMpService.post(USER_INFO_BATCH_GET_URL, userQuery.toJsonString());
return WxMpUser.fromJsonList(responseContent); return WxMpUser.fromJsonList(responseContent);
} }




+ 10
- 9
weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpUserServiceImplTest.java View File

@@ -1,5 +1,11 @@
package me.chanjar.weixin.mp.api.impl; package me.chanjar.weixin.mp.api.impl;


import java.util.ArrayList;
import java.util.List;

import org.testng.*;
import org.testng.annotations.*;

import com.google.inject.Inject; import com.google.inject.Inject;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpService;
@@ -8,11 +14,6 @@ import me.chanjar.weixin.mp.api.test.TestConfigStorage;
import me.chanjar.weixin.mp.bean.WxMpUserQuery; import me.chanjar.weixin.mp.bean.WxMpUserQuery;
import me.chanjar.weixin.mp.bean.result.WxMpUser; import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.bean.result.WxMpUserList; import me.chanjar.weixin.mp.bean.result.WxMpUserList;
import org.testng.*;
import org.testng.annotations.*;

import java.util.ArrayList;
import java.util.List;


/** /**
* 测试用户相关的接口 * 测试用户相关的接口
@@ -20,7 +21,7 @@ import java.util.List;
* @author chanjarster * @author chanjarster
* @author Binary Wang * @author Binary Wang
*/ */
@Test(groups = "userAPI")
@Test
@Guice(modules = ApiTestModule.class) @Guice(modules = ApiTestModule.class)
public class WxMpUserServiceImplTest { public class WxMpUserServiceImplTest {


@@ -68,9 +69,9 @@ public class WxMpUserServiceImplTest {
public void testUserList() throws WxErrorException { public void testUserList() throws WxErrorException {
WxMpUserList wxMpUserList = this.wxService.getUserService().userList(null); WxMpUserList wxMpUserList = this.wxService.getUserService().userList(null);
Assert.assertNotNull(wxMpUserList); Assert.assertNotNull(wxMpUserList);
Assert.assertFalse(wxMpUserList.getCount() == -1);
Assert.assertFalse(wxMpUserList.getTotal() == -1);
Assert.assertFalse(wxMpUserList.getOpenids().size() == -1);
Assert.assertNotEquals(-1, wxMpUserList.getCount());
Assert.assertNotEquals(-1, wxMpUserList.getTotal());
Assert.assertNotEquals(-1, wxMpUserList.getOpenids().size());
System.out.println(wxMpUserList); System.out.println(wxMpUserList);
} }




Loading…
Cancel
Save