From 8579b406d34456348cba282b7c5210577c38f057 Mon Sep 17 00:00:00 2001 From: kira <164851225@qq.com> Date: Tue, 17 Jul 2018 17:51:56 +0800 Subject: [PATCH] update --- .../manage/mappers/openim/OpenimMapper.java | 26 ------ .../manage/openim/OpenimController.java | 16 +++- .../manage/openim/beans/OpenimUserVO.java | 26 ++++++ .../openim/core/CustomerServiceService.java | 8 ++ .../manage/openim/core/OpenimClient.java | 43 +++------- .../core/impl/CustomerServiceServiceImpl.java | 84 +++++++++++++++---- .../openim/core/impl/OpenimApiImpl.java | 19 ++--- src/main/ui/service_client.html | 4 - 8 files changed, 132 insertions(+), 94 deletions(-) delete mode 100644 src/main/java/au/com/royalpay/payment/manage/mappers/openim/OpenimMapper.java create mode 100644 src/main/java/au/com/royalpay/payment/manage/openim/beans/OpenimUserVO.java diff --git a/src/main/java/au/com/royalpay/payment/manage/mappers/openim/OpenimMapper.java b/src/main/java/au/com/royalpay/payment/manage/mappers/openim/OpenimMapper.java deleted file mode 100644 index 16fc11adf..000000000 --- a/src/main/java/au/com/royalpay/payment/manage/mappers/openim/OpenimMapper.java +++ /dev/null @@ -1,26 +0,0 @@ -package au.com.royalpay.payment.manage.mappers.openim; - -import com.alibaba.fastjson.JSONObject; - -import org.apache.ibatis.annotations.Param; - -import cn.yixblog.support.mybatis.autosql.annotations.AutoMapper; -import cn.yixblog.support.mybatis.autosql.annotations.AutoSql; -import cn.yixblog.support.mybatis.autosql.annotations.SqlType; - -/** - * Created by yixian on 2016-10-27. - */ -@AutoMapper(tablename = "sys_openim", pkName = "id") -public interface OpenimMapper { - @AutoSql(type = SqlType.SELECT) - JSONObject findOne(@Param("user_id") String user_id); - @AutoSql(type = SqlType.SELECT) - JSONObject findByAccountId(@Param("account_id") String account_id,@Param("account_type") String account_type); - - @AutoSql(type = SqlType.UPDATE) - void update(JSONObject member); - - @AutoSql(type = SqlType.INSERT) - void save(JSONObject member); -} diff --git a/src/main/java/au/com/royalpay/payment/manage/openim/OpenimController.java b/src/main/java/au/com/royalpay/payment/manage/openim/OpenimController.java index ae3331c78..be6f48884 100644 --- a/src/main/java/au/com/royalpay/payment/manage/openim/OpenimController.java +++ b/src/main/java/au/com/royalpay/payment/manage/openim/OpenimController.java @@ -1,5 +1,6 @@ package au.com.royalpay.payment.manage.openim; +import au.com.royalpay.payment.manage.openim.beans.OpenimUserVO; import au.com.royalpay.payment.manage.openim.core.CustomerServiceService; import au.com.royalpay.payment.manage.permission.manager.RequireManager; import au.com.royalpay.payment.tools.CommonConsts; @@ -9,8 +10,11 @@ import com.alibaba.fastjson.JSONObject; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import java.util.List; + import javax.annotation.Resource; /** @@ -24,11 +28,19 @@ public class OpenimController { @Resource private CustomerServiceService customerServiceService; - - @RequestMapping(value = "/check",method = RequestMethod.POST) + @RequestMapping(value = "/check", method = RequestMethod.POST) @RequireManager public void sendMsg(@ModelAttribute(CommonConsts.MANAGER_STATUS) JSONObject manager) { customerServiceService.checkAndSave(manager); } + @RequestMapping(value = "/findOne", method = RequestMethod.GET) + public OpenimUserVO findOne(@RequestParam int clientId, @RequestParam String userName) { + return customerServiceService.findOne(clientId, userName); + } + + @RequestMapping(value = "/list",method = RequestMethod.GET) + public List query(@RequestParam String clientMoniker){ + return customerServiceService.query(clientMoniker); + } } diff --git a/src/main/java/au/com/royalpay/payment/manage/openim/beans/OpenimUserVO.java b/src/main/java/au/com/royalpay/payment/manage/openim/beans/OpenimUserVO.java new file mode 100644 index 000000000..a00e73df7 --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/openim/beans/OpenimUserVO.java @@ -0,0 +1,26 @@ +package au.com.royalpay.payment.manage.openim.beans; + +/** + * @author kira + * @date 2018/7/17 + */ +public class OpenimUserVO { + private String nick; + private String userid; + + public String getNick() { + return nick; + } + + public void setNick(String nick) { + this.nick = nick; + } + + public String getUserid() { + return userid; + } + + public void setUserid(String userid) { + this.userid = userid; + } +} diff --git a/src/main/java/au/com/royalpay/payment/manage/openim/core/CustomerServiceService.java b/src/main/java/au/com/royalpay/payment/manage/openim/core/CustomerServiceService.java index ef6dd0c88..2b041a92d 100644 --- a/src/main/java/au/com/royalpay/payment/manage/openim/core/CustomerServiceService.java +++ b/src/main/java/au/com/royalpay/payment/manage/openim/core/CustomerServiceService.java @@ -1,7 +1,11 @@ package au.com.royalpay.payment.manage.openim.core; +import au.com.royalpay.payment.manage.openim.beans.OpenimUserVO; + import com.alibaba.fastjson.JSONObject; +import java.util.List; + /** * @author kira * @date 2018/7/11 @@ -9,4 +13,8 @@ import com.alibaba.fastjson.JSONObject; public interface CustomerServiceService { void checkAndSave(JSONObject account); + + OpenimUserVO findOne(int clientId,String userName); + + List query(String clientMoniker); } diff --git a/src/main/java/au/com/royalpay/payment/manage/openim/core/OpenimClient.java b/src/main/java/au/com/royalpay/payment/manage/openim/core/OpenimClient.java index 21d402e7f..bb0487553 100644 --- a/src/main/java/au/com/royalpay/payment/manage/openim/core/OpenimClient.java +++ b/src/main/java/au/com/royalpay/payment/manage/openim/core/OpenimClient.java @@ -20,7 +20,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; -import java.util.ArrayList; import java.util.List; /** @@ -30,8 +29,8 @@ import java.util.List; @Service public class OpenimClient { Logger logger = LoggerFactory.getLogger(getClass()); -// private final String appkey = "24960261"; -// private final String secret = "7639427973bd671be15c9d0c1e9c90b4"; + // private final String appkey = "24960261"; + // private final String secret = "7639427973bd671be15c9d0c1e9c90b4"; private final String appkey = "24962653"; private final String secret = "1c1a1320c4e6f24df24e3fe41c3fc00a"; private final String url = "https://eco.taobao.com/router/rest"; @@ -47,7 +46,11 @@ public class OpenimClient { } catch (ApiException e) { logger.info("openim get user fail", e); } - return JSONObject.parseObject(rsp.getBody()); + JSONObject openInfo = JSONObject.parseObject(rsp.getBody()); + if (openInfo.getJSONObject("openim_users_get_response").getJSONObject("userinfos").size() < 1) { + return null; + } + return openInfo; } public void addUser(List users) { @@ -61,9 +64,12 @@ public class OpenimClient { logger.info("openim add user fail", e); } JSONObject result = JSONObject.parseObject(rsp.getBody()); - if (result.getJSONObject("openim_users_add_response").getJSONObject("fail_msg").size() > 0) { - logger.info("openim add user fail reason:" - + result.getJSONObject("openim_users_add_response").getJSONObject("fail_msg").getJSONArray("string").toJSONString()); + JSONObject failMsg = result.getJSONObject("openim_users_add_response").getJSONObject("fail_msg"); + if (failMsg.size() > 0) { + if ("data exist".equals(failMsg.getJSONArray("string").get(0))) { + return; + } + logger.info("openim add user fail reason:" + failMsg.getJSONArray("string").toJSONString()); throw new ServerErrorException("System error"); } } @@ -103,27 +109,4 @@ public class OpenimClient { throw new ServerErrorException("System error"); } } - - -// public static void main(String[] args) { -// String appkey = "24960261"; -// String secret = "7639427973bd671be15c9d0c1e9c90b4"; -// String url = "https://eco.taobao.com/router/rest"; -// TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret); -// OpenimUsersAddRequest req = new OpenimUsersAddRequest(); -// List list2 = new ArrayList(); -// Userinfos obj3 = new Userinfos(); -// list2.add(obj3); -// obj3.setNick("im123"); -// obj3.setUserid("im123"); -// obj3.setPassword("xxxxxx"); -// req.setUserinfos(list2); -// OpenimUsersAddResponse rsp = null; -// try { -// rsp = client.execute(req); -// } catch (ApiException e) { -// e.printStackTrace(); -// } -// System.out.println(rsp.getBody()); -// } } diff --git a/src/main/java/au/com/royalpay/payment/manage/openim/core/impl/CustomerServiceServiceImpl.java b/src/main/java/au/com/royalpay/payment/manage/openim/core/impl/CustomerServiceServiceImpl.java index 2179dc562..7c2868cfa 100644 --- a/src/main/java/au/com/royalpay/payment/manage/openim/core/impl/CustomerServiceServiceImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/openim/core/impl/CustomerServiceServiceImpl.java @@ -1,14 +1,21 @@ package au.com.royalpay.payment.manage.openim.core.impl; -import au.com.royalpay.payment.manage.mappers.openim.OpenimMapper; +import au.com.royalpay.payment.manage.mappers.system.ClientAccountMapper; +import au.com.royalpay.payment.manage.merchants.core.ClientManager; +import au.com.royalpay.payment.manage.openim.beans.OpenimUserVO; import au.com.royalpay.payment.manage.openim.core.CustomerServiceService; import au.com.royalpay.payment.manage.openim.core.OpenimApi; +import au.com.royalpay.payment.tools.exceptions.BadRequestException; +import au.com.royalpay.payment.tools.exceptions.ForbiddenException; import com.alibaba.fastjson.JSONObject; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; +import java.util.ArrayList; +import java.util.List; + import javax.annotation.Resource; /** @@ -18,14 +25,21 @@ import javax.annotation.Resource; @Service public class CustomerServiceServiceImpl implements CustomerServiceService { - @Resource - private OpenimMapper openimMapper; @Resource private OpenimApi openimApi; + @Resource + private ClientManager clientManager; + @Resource + private ClientAccountMapper clientAccountMapper; + private final String password = "XXXXXX"; @Override public void checkAndSave(JSONObject account) { + JSONObject client = clientManager.getClientInfo(account.getIntValue("client_id")); + if (client == null) { + throw new BadRequestException("Merchant Not Found"); + } boolean isPartner = true; if (StringUtils.isNotEmpty(account.getString("account_id"))) { isPartner = true; @@ -33,21 +47,55 @@ public class CustomerServiceServiceImpl implements CustomerServiceService { if (StringUtils.isNotEmpty(account.getString("manager_id"))) { isPartner = false; } - String uid = account.getString("username")+ (isPartner ? "_partner": "_manager"); - JSONObject record = openimMapper.findOne(uid); - if(record==null){ - JSONObject saveRecord = new JSONObject(); - saveRecord.put("nick",account.getString("display_name")); - if(isPartner){ - saveRecord.put("user_id",account.getString("username")+"_partner"); - saveRecord.put("account_type","partner"); - saveRecord.put("account_id",account.getString("account_id")); - }else { - saveRecord.put("user_id",account.getString("username")+"_manager"); - saveRecord.put("account_type","manager"); - saveRecord.put("account_id",account.getString("manager_id")); - } - openimApi.addUser(saveRecord); + String uid = account.getString("username") + (isPartner ? "(" + client.getString("client_moniker") + ")" : ""); + JSONObject saveRecord = new JSONObject(); + saveRecord.put("nick", account.getString("display_name")); + if (isPartner) { + saveRecord.put("user_id", uid); + saveRecord.put("account_type", "partner"); + saveRecord.put("account_id", account.getString("account_id")); + } else { + saveRecord.put("user_id", uid); + saveRecord.put("account_type", "manager"); + saveRecord.put("account_id", account.getString("manager_id")); + } + saveRecord.put("password", password); + openimApi.addUser(saveRecord); + } + + @Override + public OpenimUserVO findOne(int clientId, String userName) { + JSONObject client = clientManager.getClientInfo(clientId); + if (client == null) { + throw new BadRequestException("Merchant Not Found"); + } + JSONObject account = clientAccountMapper.findByUsername(userName); + if (account == null) { + throw new BadRequestException("Account Not Found"); + } + if (account.getIntValue("client_id") != clientId) { + throw new ForbiddenException("No Rights"); + } + OpenimUserVO result = new OpenimUserVO(); + result.setNick(account.getString("display_name")); + result.setUserid(account.getString("username") + "(" + client.getString("client_moniker") + ")"); + return result; + } + + @Override + public List query(String clientMoniker) { + JSONObject client = clientManager.getClientInfoByMoniker(clientMoniker); + if (client == null) { + throw new BadRequestException("Merchant Not Found"); } + List accounts = clientAccountMapper.listPartnerAccounts(client.getIntValue("client_id")); + List result = new ArrayList<>(accounts.size()); + accounts.forEach(p -> { + JSONObject openimUser = new JSONObject(); + openimUser.put("nick", p.getString("display_name")); + openimUser.put("userid", p.getString("username") + "(" + client.getString("client_moniker") + ")"); + result.add(openimUser); + }); + return result; } } diff --git a/src/main/java/au/com/royalpay/payment/manage/openim/core/impl/OpenimApiImpl.java b/src/main/java/au/com/royalpay/payment/manage/openim/core/impl/OpenimApiImpl.java index 8e97ed004..509f14f8c 100644 --- a/src/main/java/au/com/royalpay/payment/manage/openim/core/impl/OpenimApiImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/openim/core/impl/OpenimApiImpl.java @@ -1,9 +1,7 @@ package au.com.royalpay.payment.manage.openim.core.impl; -import au.com.royalpay.payment.manage.mappers.openim.OpenimMapper; import au.com.royalpay.payment.manage.openim.core.OpenimApi; import au.com.royalpay.payment.manage.openim.core.OpenimClient; -import au.com.royalpay.payment.tools.utils.id.IdUtil; import com.alibaba.fastjson.JSONObject; import com.taobao.api.domain.Userinfos; @@ -24,27 +22,20 @@ public class OpenimApiImpl implements OpenimApi { @Resource private OpenimClient openimClient; - @Resource - private OpenimMapper openimMapper; @Override public void addUser(JSONObject user) { + JSONObject opemInfo = openimClient.getUser(user.getString("user_id")); + if(opemInfo!=null){ + return; + } List saveList = new ArrayList<>(); Userinfos record = new Userinfos(); record.setUserid(user.getString("user_id")); record.setNick(user.getString("nick")); - record.setPassword("XXXXXX"); + record.setPassword(user.getString("password")); saveList.add(record); openimClient.addUser(saveList); - - JSONObject openim = new JSONObject(); - openim.put("id", IdUtil.getId()); - openim.put("user_id", record.getUserid()); - openim.put("password_aes", record.getPassword()); - openim.put("account_id", user.getString("account_id")); - openim.put("account_type", user.getString("account_type")); - openimMapper.save(openim); - } @Override diff --git a/src/main/ui/service_client.html b/src/main/ui/service_client.html index c4572749a..fda85abbf 100644 --- a/src/main/ui/service_client.html +++ b/src/main/ui/service_client.html @@ -19,10 +19,6 @@ appkey: 24960261, credential: 'xxxxxx', touid: window.touid, - logo:'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1531805672266&di=2b24a1579255192f7456d64872c65b39&imgtype=0&src=http%3A%2F%2Fimage.coolapk.com%2Ffeed%2F2016%2F1113%2F661664_1479013815_7771.jpg.m.jpg', - toAvatar:'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1531805672266&di=2b24a1579255192f7456d64872c65b39&imgtype=0&src=http%3A%2F%2Fimage.coolapk.com%2Ffeed%2F2016%2F1113%2F661664_1479013815_7771.jpg.m.jpg', - Avatar:'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1531805672266&di=2b24a1579255192f7456d64872c65b39&imgtype=0&src=http%3A%2F%2Fimage.coolapk.com%2Ffeed%2F2016%2F1113%2F661664_1479013815_7771.jpg.m.jpg', - pluginUrl: 'http://192.168.0.43:9001/service_contact_list.html?uid=' + window.uid, onLoginSuccess: function (data) {