|
|
@@ -2,11 +2,25 @@ package com.uas.sso.controller;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.alibaba.fastjson.TypeReference;
|
|
|
+import com.uas.account.ResultWrap;
|
|
|
import com.uas.account.entity.AppCreateInfo;
|
|
|
+import com.uas.account.entity.UserSpace;
|
|
|
+import com.uas.account.entity.UserSpaceDetail;
|
|
|
+import com.uas.account.support.Page;
|
|
|
+import com.uas.account.util.AccountUtils;
|
|
|
+import com.uas.sso.AuthToken;
|
|
|
+import com.uas.sso.SSOConfig;
|
|
|
+import com.uas.sso.SSOHelper;
|
|
|
+import com.uas.sso.common.util.HttpUtil;
|
|
|
+import com.uas.sso.core.Const;
|
|
|
+import com.uas.sso.core.Status;
|
|
|
import com.uas.sso.entity.App;
|
|
|
import com.uas.sso.entity.User;
|
|
|
import com.uas.sso.entity.Userspace;
|
|
|
+import com.uas.sso.service.AppService;
|
|
|
import com.uas.sso.service.ApplyUserSpaceService;
|
|
|
+import com.uas.sso.service.UserService;
|
|
|
import com.uas.sso.service.UserspaceService;
|
|
|
import com.uas.sso.util.FastjsonUtils;
|
|
|
import com.uas.sso.util.FileUrl;
|
|
|
@@ -18,9 +32,14 @@ import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
-import java.io.UnsupportedEncodingException;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.List;
|
|
|
+import java.io.*;
|
|
|
+import java.net.CookieHandler;
|
|
|
+import java.net.CookieManager;
|
|
|
+import java.net.CookiePolicy;
|
|
|
+import java.sql.Timestamp;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+import static com.alibaba.fastjson.JSON.parseObject;
|
|
|
|
|
|
/**
|
|
|
* 企业信息管理controller
|
|
|
@@ -35,6 +54,12 @@ public class UserspaceManagerController extends BaseController {
|
|
|
@Autowired
|
|
|
private UserspaceService userspaceService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private UserService userService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AppService appService;
|
|
|
+
|
|
|
@Autowired
|
|
|
private ApplyUserSpaceService applyUserSpaceService;
|
|
|
|
|
|
@@ -77,7 +102,7 @@ public class UserspaceManagerController extends BaseController {
|
|
|
|
|
|
/**
|
|
|
* 校验营业执照号是否被认证
|
|
|
- *
|
|
|
+ *
|
|
|
* @author wangmh
|
|
|
* @date 2018/1/11 10:35
|
|
|
* @param businessCode 营业执照号
|
|
|
@@ -287,10 +312,10 @@ public class UserspaceManagerController extends BaseController {
|
|
|
|
|
|
// 获取接收到的信息
|
|
|
List<User> users = JSON.parseArray(userInfos, User.class);
|
|
|
- Userspace userspace = JSONObject.parseObject(detail, Userspace.class);
|
|
|
+ Userspace userspace = parseObject(detail, Userspace.class);
|
|
|
|
|
|
// 企业注册
|
|
|
- userspace = userspaceService.register(userspace, userspace.getAdmin());
|
|
|
+ userspace = userspaceService.register(userspace);
|
|
|
|
|
|
// 将用户绑定绑定到注册企业
|
|
|
userspaceService.addUser(userspace, users);
|
|
|
@@ -323,9 +348,261 @@ public class UserspaceManagerController extends BaseController {
|
|
|
* @return keys:{listdata:数据}
|
|
|
*/
|
|
|
@RequestMapping(value = "/userSpaceDetail/keyword", method = RequestMethod.GET)
|
|
|
- @ResponseBody
|
|
|
public ModelMap getUserSpaceDetailByKeyword(String keyword, @RequestParam(defaultValue = "1") Integer pageNumber, @RequestParam(defaultValue = "30") Integer pageSize) {
|
|
|
return StringUtils.isEmpty(keyword) ? new ModelMap("listdata", "请先输入企业名称")
|
|
|
: new ModelMap("listdata", userspaceService.findByKeyword(keyword, pageNumber, pageSize).getContent());
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 同步旧企业数据到账户中心
|
|
|
+ * @param spaceUU 企业uu号,为空则同步所有
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequestMapping("/saveOldUserSpaces/{spaceUU}")
|
|
|
+ public ModelMap syncOldUserSpaces(@PathVariable Long spaceUU, @RequestParam(defaultValue = "1") int pageNumber,
|
|
|
+ @RequestParam(defaultValue = "20")int pageSize) {
|
|
|
+ // 由于旧数据问题,记录重复的企业名称和营业执照号
|
|
|
+ Map<String, Integer> repeatSpaceName = new HashMap<>();
|
|
|
+ Map<String, Integer> repeatBusinessCode = new HashMap<>();
|
|
|
+
|
|
|
+ Userspace userspace;
|
|
|
+ Page<UserSpaceDetail> page = null;
|
|
|
+ // 循环分页请求,如果是最后一页则退出循环
|
|
|
+ while (page == null || !page.isLast()) {
|
|
|
+ // 获取分页信息
|
|
|
+ page = getUserSpace(pageNumber++, pageSize);
|
|
|
+
|
|
|
+ // 循环遍历企业信息并保存
|
|
|
+ int i = 1;
|
|
|
+ for (UserSpaceDetail detail : page.getContent()) {
|
|
|
+ try {
|
|
|
+ userspace = saveUserspace(detail);
|
|
|
+ System.out.println(page.getNumber() + "\t" + page.getSize() + "\t" + i++ + "\t" + userspace.getSpaceName() + "\t" + userspace.getBusinessCode());
|
|
|
+ } catch (Exception e) {
|
|
|
+ writerToFile(page.getNumber() + "\t" + page.getSize() + "\t" + i++ + "\t" + detail.getName() + "\t" + detail.getBusinessCode(), e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ spaceNameUnique();
|
|
|
+ }
|
|
|
+ return success();
|
|
|
+ }
|
|
|
+
|
|
|
+ private Userspace saveUserspace(UserSpaceDetail detail) throws Exception {
|
|
|
+ Userspace userspace;
|
|
|
+ userspace = new Userspace();
|
|
|
+ // 设置企业秘钥
|
|
|
+ userspace.setAccessSecret(getAccessSecret(detail.getBusinessCode()));
|
|
|
+ // 设置管理员uu号
|
|
|
+ User user = userService.findByMobile(detail.getAdminTel());
|
|
|
+ userspace.setAdminUU(user == null ? null : user.getUserUU());
|
|
|
+ userspace.setBusinessCode(detail.getBusinessCode());
|
|
|
+ userspace.setBusinessCodeImage(detail.getBusinessCodeImage());
|
|
|
+ userspace.setCorporation(detail.getCorporation());
|
|
|
+ userspace.setDomain(detail.getDomain());
|
|
|
+ userspace.setLogoImage(detail.getLogoImage());
|
|
|
+ userspace.setProfession(detail.getProfession());
|
|
|
+ userspace.setRegAddress(detail.getAddress());
|
|
|
+ userspace.setRegCity(detail.getCity());
|
|
|
+ userspace.setRegDistrict(detail.getDistrict());
|
|
|
+ userspace.setRegProvince(detail.getProvince());
|
|
|
+ userspace.setRegStreet(detail.getStreet());
|
|
|
+ userspace.setRegisterDate(detail.getRegisterDate() == null ? null : new Timestamp(detail.getRegisterDate()));
|
|
|
+ userspace.setSpaceName(detail.getName());
|
|
|
+ // 设置企业uu号
|
|
|
+ userspace.setSpaceUU(getSpaceUU(detail.getBusinessCode()));
|
|
|
+ userspace.setTags(detail.getTags());
|
|
|
+ userspace.setTelephone(detail.getTel());
|
|
|
+ userspace.setValidCode((short) Status.AUTHENTICATED.getCode());
|
|
|
+ if (userspace.getSpaceUU() == null) {
|
|
|
+ throw new Exception("无uu号");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置企业用户
|
|
|
+ addUsersByBusinessCode(userspace);
|
|
|
+
|
|
|
+ // 设置默认应用
|
|
|
+ addDefaultApp(userspace);
|
|
|
+ userspaceService.save(userspace);
|
|
|
+ return userspace;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addDefaultApp(Userspace userspace) throws Exception {
|
|
|
+ if (userspace.getApps() == null) {
|
|
|
+ userspace.setApps(new ArrayList<App>());
|
|
|
+ }
|
|
|
+ String url = "http://192.168.253.66:8080/api/userspace";
|
|
|
+ url = url + "/" + userspace.getBusinessCode();
|
|
|
+ HttpUtil.ResponseWrap res = HttpUtil.doGet(url);
|
|
|
+ if (!res.isSuccess()) {
|
|
|
+ throw new Exception(res.getContent());
|
|
|
+ } else {
|
|
|
+ ResultWrap result = JSON.parseObject(res.getContent(), ResultWrap.class);
|
|
|
+ if (!result.isSuccess()) {
|
|
|
+ throw new Exception(result.getErrMsg());
|
|
|
+ } else if (result.getContent() != null){
|
|
|
+ List<UserSpace> userSpaceList = JSON.parseArray(result.getContent().toString(), UserSpace.class);
|
|
|
+ for (int i=0; i<userSpaceList.size(); i++) {
|
|
|
+ App app = appService.findOne(userSpaceList.get(i).getAppId());
|
|
|
+ if (app != null && !userspace.getApps().contains(app)) {
|
|
|
+ userspace.getApps().add(app);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ App app = appService.findOne("sso");
|
|
|
+ if (!userspace.getApps().contains(app)) {
|
|
|
+ userspace.getApps().add(app);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addUsersByBusinessCode(Userspace userspace) throws Exception {
|
|
|
+ if (userspace.getUsers() == null) {
|
|
|
+ userspace.setUsers(new ArrayList<User>());
|
|
|
+ }
|
|
|
+ int pageNumber = 1;
|
|
|
+ int pageSize = 20;
|
|
|
+ Page<com.uas.account.entity.User> page;
|
|
|
+ String url = "http://192.168.253.66:8080/api/user";
|
|
|
+ url = url + "/findByBusinessCode";
|
|
|
+ do {
|
|
|
+ ModelMap data = new ModelMap();
|
|
|
+ data.put("businessCode", userspace.getBusinessCode());
|
|
|
+ data.put("appId", "b2b");
|
|
|
+ data.put("pageNumber", pageNumber++);
|
|
|
+ data.put("pageSize", pageSize);
|
|
|
+ HttpUtil.ResponseWrap res = HttpUtil.doGet(url, data);
|
|
|
+ if (!res.isSuccess()) {
|
|
|
+ throw new Exception(res.getContent());
|
|
|
+ } else {
|
|
|
+ page = JSON.parseObject(res.getContent(), new TypeReference<Page<com.uas.account.entity.User>>() {});
|
|
|
+ List<com.uas.account.entity.User> users = page.getContent();
|
|
|
+ for (int i = 0; i < users.size(); i++) {
|
|
|
+ User user = users.get(i).getDialectUID() == null ? userService.findByMobile(users.get(i).getUid()) : userService.findOne(Long.valueOf(users.get(i).getDialectUID()));
|
|
|
+ if (user != null && !userspace.getUsers().contains(user)) {
|
|
|
+ userspace.getUsers().add(user);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } while (page == null || !page.isLast());
|
|
|
+ }
|
|
|
+
|
|
|
+ private Long getSpaceUU(String businessCode) throws Exception {
|
|
|
+ if (StringUtils.isEmpty(businessCode)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ String url = "http://192.168.253.66:8080/api/userspace/info";
|
|
|
+ url = url + "/" + businessCode;
|
|
|
+ HttpUtil.ResponseWrap res = null;
|
|
|
+ res = HttpUtil.doGet(url);
|
|
|
+ if(res.isSuccess() && !StringUtils.isEmpty(res.getContent())) {
|
|
|
+ JSONObject jsonObject = JSON.parseObject(res.getContent());
|
|
|
+ if (jsonObject.containsKey("dialectUID")) {
|
|
|
+ return Long.valueOf(jsonObject.get("dialectUID").toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Page<UserSpaceDetail> getUserSpace(@RequestParam(defaultValue = "1") int pageNumber, @RequestParam(defaultValue = "20") int pageSize) {
|
|
|
+ Page<UserSpaceDetail> page = null;
|
|
|
+ String url = "http://192.168.253.66:8080/api/partners";
|
|
|
+ if(!StringUtils.isEmpty(url)) {
|
|
|
+ HttpUtil.ResponseWrap res = null;
|
|
|
+ try {
|
|
|
+ ModelMap data = new ModelMap("_operate", "getUserSpaces");
|
|
|
+ data.put("pageNumber", Integer.valueOf(pageNumber));
|
|
|
+ data.put("pageSize", Integer.valueOf(pageSize));
|
|
|
+ res = HttpUtil.doGet(url, data);
|
|
|
+ if(res.isSuccess()) {
|
|
|
+ return (Page) parseObject(res.getContent(), new TypeReference<Page<UserSpaceDetail>>() {});
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从b2b获取erp企业秘钥
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/saveAccessSecret/{businessCode}")
|
|
|
+ public ModelMap saveAccessSecret(@PathVariable String businessCode) {
|
|
|
+ if (!StringUtils.isEmpty(businessCode)) {
|
|
|
+ getAccessSecret(businessCode);
|
|
|
+ }
|
|
|
+ return success();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从b2b获取密钥
|
|
|
+ * @param businessCode 营业执照号
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String getAccessSecret(String businessCode) {
|
|
|
+ String url = "http://192.168.253.66:8090/platform-b2b";
|
|
|
+ url = url + "/account/enterprise/info/" + businessCode;
|
|
|
+ String accessSecret = null;
|
|
|
+ try {
|
|
|
+ HttpUtil.ResponseWrap responseWrap = HttpUtil.doGet(url);
|
|
|
+ if (responseWrap.isSuccess()) {
|
|
|
+ JSONObject enterprise = parseObject(responseWrap.getContent());
|
|
|
+ accessSecret = (String) enterprise.get("accessSecret");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return accessSecret;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 写入日志
|
|
|
+ * @param businessCode 营业执照号
|
|
|
+ * @param accessSecret 密钥
|
|
|
+ */
|
|
|
+ private void writerToFile(String businessCode, String accessSecret) {
|
|
|
+ FileWriter fw = null;
|
|
|
+ try {
|
|
|
+ //如果文件存在,则追加内容;如果文件不存在,则创建文件
|
|
|
+ File f=new File("logs/userspace.log");
|
|
|
+ fw = new FileWriter(f, true);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ PrintWriter pw = new PrintWriter(fw);
|
|
|
+ pw.println(businessCode + "\t" + accessSecret);
|
|
|
+ pw.flush();
|
|
|
+ try {
|
|
|
+ fw.flush();
|
|
|
+ pw.close();
|
|
|
+ fw.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping("/spaceName/unique")
|
|
|
+ public ModelMap spaceNameUnique() {
|
|
|
+ List<String> repeatName = userspaceService.findRepeatName();
|
|
|
+ for (String name : repeatName) {
|
|
|
+ List<Userspace> userspaces = userspaceService.findAllBySpaceName(name);
|
|
|
+ for (int i=0; i<userspaces.size(); i++) {
|
|
|
+ Userspace userspace = userspaces.get(i);
|
|
|
+ userspace.setSpaceName(userspace.getSpaceName() + Const.REPEAT_SEPARATOR + (i + 1));
|
|
|
+ userspaceService.save(userspace);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return success();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从其他应用注册企业信息
|
|
|
+ * @param userspace 企业信息
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/register/other", method = RequestMethod.POST)
|
|
|
+ public void registerFromOther(Userspace userspace) {
|
|
|
+ userspaceService.register(userspace);
|
|
|
+ }
|
|
|
}
|