PersManageAction.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. package com.minpay.shouhuo;
  2. import com.google.zxing.WriterException;
  3. import com.min.util.OffSetUtil;
  4. import com.minpay.common.bean.User;
  5. import com.minpay.common.constant.Constant;
  6. import com.minpay.common.constant.ServConstant;
  7. import com.minpay.common.format.IFormatService;
  8. import com.minpay.common.service.ILogService;
  9. import com.minpay.common.service.IPropertiesService;
  10. import com.minpay.common.service.IPublicService;
  11. import com.minpay.common.service.IQrcodeService;
  12. import com.minpay.common.service.impl.QrcodeServiceImpl;
  13. import com.minpay.common.util.*;
  14. import com.minpay.db.table.mapper.VmPersonInfMapper;
  15. import com.minpay.db.table.mapper.VmPersonRoleInfMapper;
  16. import com.minpay.db.table.model.VmPersonInf;
  17. import com.minpay.db.table.model.VmPersonInfExample;
  18. import com.minpay.db.table.model.VmPersonRoleInf;
  19. import com.minpay.db.table.model.VmPersonRoleInfExample;
  20. import com.minpay.db.table.own.mapper.PersonManageMapper;
  21. import com.minpay.db.table.own.mapper.SequenceMapper;
  22. import com.minpay.huicai.util.EquCodeCreateUtil;
  23. import com.startup.minpay.frame.business.IMINAction;
  24. import com.startup.minpay.frame.business.MINHttpServletRequestContext;
  25. import com.startup.minpay.frame.business.res.MINActionResult;
  26. import com.startup.minpay.frame.constant.IMINBusinessConstant;
  27. import com.startup.minpay.frame.constant.IMINTransactionEnum;
  28. import com.startup.minpay.frame.data.format.MINCopyFormat;
  29. import com.startup.minpay.frame.exception.MINBusinessException;
  30. import com.startup.minpay.frame.jdbc.MINRowBounds;
  31. import com.startup.minpay.frame.service.base.IMINDataBaseService;
  32. import com.startup.minpay.frame.service.base.Service;
  33. import com.startup.minpay.frame.session.MINSession;
  34. import com.startup.minpay.frame.target.MINAction;
  35. import com.startup.minpay.frame.target.MINComponent;
  36. import com.startup.minpay.frame.target.MINParam;
  37. import net.sf.json.JSONObject;
  38. import org.apache.commons.fileupload.FileItem;
  39. import org.apache.commons.fileupload.FileItemFactory;
  40. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  41. import javax.servlet.http.HttpServletResponse;
  42. import java.io.*;
  43. import java.net.URLDecoder;
  44. import java.net.URLEncoder;
  45. import java.util.HashMap;
  46. import java.util.List;
  47. import java.util.Map;
  48. /**
  49. * 用户管理
  50. * @author xbh
  51. *
  52. */
  53. @MINComponent
  54. public class PersManageAction implements IMINAction {
  55. /**用户查询**/
  56. public final static String PERSON_QUERY = "personQuery";
  57. /**管理员添加**/
  58. public final static String PERSON_ADD = "personAdd";
  59. /**冻结**/
  60. public final static String PERSON_CANCELLATION = "personCancellation";
  61. /**解冻*/
  62. private final static String PERSON_THAW = "personThaw";
  63. /** 生成包含字符串信息的二维码图片**/
  64. public static final String CREATE_QRCODE = "createQrCode";
  65. /**
  66. * 用户查询
  67. * @param page
  68. * @param limit
  69. * @param dates
  70. * @param perName
  71. * @param state
  72. * @param fapRequest
  73. * @return
  74. * @throws MINBusinessException
  75. */
  76. @MINAction(value = PERSON_QUERY)
  77. public MINActionResult personQuery(
  78. @MINParam(key = "page", defaultValue = "1") int page,
  79. @MINParam(key = "limit", defaultValue = "3") int limit,
  80. @MINParam(key = "dates") String dates,
  81. @MINParam(key = "perName") String perName,
  82. @MINParam(key = "state") String state,
  83. @MINParam(key = "roleid") String roleid,
  84. MINSession session,
  85. MINHttpServletRequestContext fapRequest
  86. ) throws MINBusinessException {
  87. MINActionResult res = new MINActionResult();
  88. // 创建查询条件
  89. Map<String, String> p = new HashMap<String, String>();
  90. User u = session.getUser();
  91. p.put("dates", dates); // 创建日期
  92. p.put("perName", perName); //用户名称
  93. p.put("state", state); //状态
  94. if(!u.getBranchId().equals(Constant.ADMINISTRATION_SYSTEM_NUMBER)){
  95. p.put("branchId", u.getBranchId());
  96. }
  97. p.put("channel", u.getChannel());//渠道
  98. p.put("roleid", roleid);
  99. MINRowBounds rows = new MINRowBounds(page, limit);
  100. // 查询
  101. List<Map<String, String>> ls = Service.lookup(IMINDataBaseService.class)
  102. .getMybatisMapper(PersonManageMapper.class).personQuery(p,rows);
  103. ls = Service.lookup(IFormatService.class).formatDateTime(ls, "createTime");
  104. ls = Service.lookup(IFormatService.class).formatDateTime(ls, "modifyTime");
  105. ls = new MINCopyFormat("{state:'stateDesc',channel:'channelDesc',type:'typeDesc',role:'roleDesc'}").format(ls);
  106. ls = Service.lookup(IFormatService.class).formatEnum(ls, "{stateDesc:'PERSON_STATE',channelDesc:'PERSON_CHANNEL',typeDesc:'PERSON_TYPE',roleDesc:'USER_ROLE'}");
  107. // 设置返回值
  108. res.set(IMINBusinessConstant.F_PAGING_LAY, ls);
  109. res.set(IMINBusinessConstant.F_PAGING_COUNT, rows.getCount());
  110. return res;
  111. }
  112. /**
  113. * 注销
  114. * @param session
  115. * @param id
  116. * @return
  117. * @throws MINBusinessException
  118. */
  119. @MINAction(value = PERSON_CANCELLATION)
  120. public MINActionResult personCancellation(
  121. MINSession session,
  122. @MINParam(key = "id") String id
  123. ) throws MINBusinessException {
  124. //获取当前用户
  125. User user = session.getUser();
  126. MINActionResult res = new MINActionResult();
  127. VmPersonInf ps = new VmPersonInf();
  128. ps.setId(id);
  129. ps.setStatus("01");//冻结
  130. ps.setModifyTime(DateUtil.getCurrentDateTimeString());//修改时间
  131. ps.setModifyUser(session.getUser().getId()); //修改人
  132. Service.lookup(IMINDataBaseService.class)
  133. .getMybatisMapper(VmPersonInfMapper.class).updateByPrimaryKeySelective(ps);
  134. String logInfo = "操作员:"+user.getName()+",注销用户,id:"+id;
  135. Service.lookup(ILogService.class).logging(session, logInfo);
  136. return res;
  137. }
  138. /**
  139. * 解冻
  140. * @param session
  141. * @param id
  142. * @return
  143. * @throws MINBusinessException
  144. */
  145. @MINAction(value = PERSON_THAW)
  146. public MINActionResult personThaw(
  147. MINSession session,
  148. @MINParam(key = "id") String id
  149. ) throws MINBusinessException {
  150. //获取当前用户
  151. User user = session.getUser();
  152. MINActionResult res = new MINActionResult();
  153. VmPersonInf ps = new VmPersonInf();
  154. ps.setId(id);
  155. ps.setStatus("00");//正常
  156. ps.setModifyTime(DateUtil.getCurrentDateTimeString());//修改时间
  157. ps.setModifyUser(session.getUser().getId()); //修改人
  158. Service.lookup(IMINDataBaseService.class)
  159. .getMybatisMapper(VmPersonInfMapper.class).updateByPrimaryKeySelective(ps);
  160. String logInfo = "操作员:"+user.getName()+",解冻用户,id:"+id;
  161. Service.lookup(ILogService.class).logging(session, logInfo);
  162. return res;
  163. }
  164. @MINAction(value = PERSON_ADD,session = false)
  165. public MINActionResult personAdd(
  166. @MINParam(key = "code") String code,
  167. @MINParam(key = "type") String type,
  168. @MINParam(key = "channel") String channel,
  169. @MINParam(key = "role") String role,
  170. @MINParam(key = "branchId") String branchId,
  171. HttpServletResponse response,
  172. MINHttpServletRequestContext request
  173. ) throws MINBusinessException {
  174. MINActionResult res = new MINActionResult();
  175. VmPersonInf person = new VmPersonInf();
  176. MINActionResult result = null;
  177. String nowTime = DateUtil.getCurrentDateTimeString();
  178. if("WX".equals(type)){
  179. result = this.getWxPayOpenid(channel, code);
  180. }else if("AL".equals(type)){
  181. //result = this.getALiPayOpenid(channel, code);
  182. }
  183. if(CommonUtil.isEmpty((String) result.get("openId"))) {
  184. throw new MINBusinessException("JA0001", "非法登录!");
  185. }
  186. //用户表可以存在 角色表不可以
  187. VmPersonInfExample personex = new VmPersonInfExample();
  188. personex.createCriteria().andChannelEqualTo(channel).andTypeEqualTo(type).andOpenidEqualTo((String) result.get("openId"));
  189. List<VmPersonInf> perLst = Service.lookup(IMINDataBaseService.class).getMybatisMapper(VmPersonInfMapper.class).selectByExample(personex);
  190. if(perLst.size() > 1 ){
  191. throw new MINBusinessException("用户异常,请联系管理员!");
  192. }
  193. if(perLst.size() == 0) { //新用户,新角色
  194. /**添加用户*/
  195. //获取用户主键
  196. String perId = Service.lookup(IMINDataBaseService.class).getMybatisMapper(SequenceMapper.class).getSequence("VM_PERSON_INF_NO");
  197. person.setId(perId);
  198. person.setName((String) result.get("nickName"));
  199. person.setHeadpath((String) result.get("headImgurl"));
  200. person.setOpenid((String) result.get("openId"));
  201. person.setStatus("00");//正常
  202. person.setChannel(channel);
  203. person.setIsUser("1");//是否商户0否1是
  204. person.setPhone("00000000000");
  205. person.setType(type);//类型:WX微信AL支付宝
  206. person.setCreateUser(perId);
  207. person.setCreateTime(nowTime);
  208. person.setModifyUser(perId);
  209. person.setModifyTime(nowTime);
  210. person.setRole(role);
  211. person.setUserId(branchId);
  212. Service.lookup(IMINDataBaseService.class).getMybatisMapper(VmPersonInfMapper.class).insertSelective(person);
  213. /**添加角色*/
  214. String roleId = Service.lookup(IMINDataBaseService.class).getMybatisMapper(SequenceMapper.class).getSequence("VM_PERSON_ROLE_ID");
  215. VmPersonRoleInf vmPersonRoleInf = new VmPersonRoleInf();
  216. vmPersonRoleInf.setId(roleId);
  217. vmPersonRoleInf.setBranchId(branchId);
  218. vmPersonRoleInf.setPersonId(perId);
  219. vmPersonRoleInf.setRole(role);
  220. Service.lookup(IMINDataBaseService.class).getMybatisMapper(VmPersonRoleInfMapper.class).insertSelective(vmPersonRoleInf);
  221. }else{ //有用户,无角色
  222. VmPersonInf vmPersonInf = perLst.get(0);
  223. if("0".equals(vmPersonInf.getIsUser())){
  224. vmPersonInf.setIsUser("1");
  225. Service.lookup(IMINDataBaseService.class).getMybatisMapper(VmPersonInfMapper.class).updateByPrimaryKeySelective(person);
  226. }else{
  227. /**查询是否有此角色*/
  228. VmPersonRoleInfExample vmPersonRoleInfExample = new VmPersonRoleInfExample();
  229. vmPersonRoleInfExample.createCriteria().andRoleEqualTo(role).andPersonIdEqualTo(vmPersonInf.getId()).andStateEqualTo("00");
  230. List<VmPersonRoleInf> vmPersonRoleInfList = Service.lookup(IMINDataBaseService.class).getMybatisMapper(VmPersonRoleInfMapper.class).selectByExample(vmPersonRoleInfExample);
  231. if(vmPersonRoleInfList.size() > 0){
  232. throw new MINBusinessException("已有此角色,无需再次添加");
  233. }
  234. /**添加角色*/
  235. String roleId = Service.lookup(IMINDataBaseService.class).getMybatisMapper(SequenceMapper.class).getSequence("VM_PERSON_ROLE_ID");
  236. VmPersonRoleInf vmPersonRoleInf = new VmPersonRoleInf();
  237. vmPersonRoleInf.setId(roleId);
  238. vmPersonRoleInf.setBranchId(branchId);
  239. vmPersonRoleInf.setPersonId(vmPersonInf.getId());
  240. vmPersonRoleInf.setRole(role);
  241. Service.lookup(IMINDataBaseService.class).getMybatisMapper(VmPersonRoleInfMapper.class).insertSelective(vmPersonRoleInf);
  242. }
  243. }
  244. return res;
  245. }
  246. /***
  247. * 微信用户授权,获取用户信息
  248. * @param code
  249. * @param channel
  250. * @return
  251. * @throws MINBusinessException
  252. */
  253. private MINActionResult getWxPayOpenid(String channel ,String code) throws MINBusinessException {
  254. MINActionResult res = new MINActionResult();
  255. //在微信的appId
  256. String appId = Service.lookup(IPublicService.class).getSysParValue(channel + "_APP_ID");
  257. //在微信的secret
  258. String appSecret = Service.lookup(IPublicService.class).getSysParValue(channel + "_APP_SECRET");
  259. //微信通用请求路径
  260. String openUrl = "https://api.weixin.qq.com/sns/oauth2/access_token";
  261. //配置参数
  262. StringBuffer paramStr = new StringBuffer();
  263. paramStr.append("appid=");
  264. paramStr.append(appId);
  265. paramStr.append("&secret=");
  266. paramStr.append(appSecret);
  267. paramStr.append("&code=");
  268. paramStr.append(code);
  269. paramStr.append("&grant_type=authorization_code");
  270. try {
  271. //发送请求
  272. String jsonObj = HttpPostUtil.sendPost(openUrl, paramStr.toString());
  273. JSONObject js = JSONObject.fromObject(jsonObj);
  274. //获取openId
  275. String openId = js.getString("openid");//用户唯一标识,请注意,在未关注公众号时,用户访问公众号的网页,也会产生一个用户和公众号唯一的OpenID
  276. //微信通用请求路径
  277. String openUrl2 = "https://api.weixin.qq.com/sns/userinfo";
  278. //配置参数
  279. StringBuffer paramStr2 = new StringBuffer();
  280. paramStr2.append("access_token=");
  281. paramStr2.append(js.getString("access_token"));
  282. paramStr2.append("&openid=");
  283. paramStr2.append(openId);
  284. paramStr2.append("&lang=zh_CN");
  285. //发送请求
  286. String jsonObj2 = HttpPostUtil.sendPost(openUrl2, paramStr2.toString());
  287. JSONObject js2 = JSONObject.fromObject(jsonObj2);
  288. String nickName = EmojiFilter.filterEmoji(js2.getString("nickname"));
  289. // String nickName = js2.getString("nickname");
  290. String headImgurl = js2.getString("headimgurl");
  291. res.set("openId", openId);
  292. res.set("headImgurl", headImgurl);
  293. res.set("nickName", nickName);
  294. } catch (Exception e) {
  295. throw new MINBusinessException("PAY10006", "微信获取openId失败!");
  296. }
  297. return res;
  298. }
  299. /**
  300. * 生成包含字符串信息的二维码图片
  301. * @param flash 是否刷新
  302. */
  303. @MINAction(value = CREATE_QRCODE, transaction = IMINTransactionEnum.CMT)
  304. public MINActionResult createQrCode(
  305. @MINParam(key = "flash") String flash,
  306. @MINParam(key = "role") String role,
  307. MINSession session)throws MINBusinessException, FileNotFoundException, WriterException, IOException {
  308. MINActionResult res = new MINActionResult();
  309. //获取当前用户信息
  310. User u = session.getUser();
  311. //获取用户id
  312. String userId = u.getBranchId();
  313. //刷新,生成
  314. //开发环境修改配置
  315. String basePath = "/home/images/"+ userId+".jpg"; //与nginx配置相同
  316. String os = System.getProperty("os.name");
  317. if(os.toLowerCase().startsWith("win")){
  318. System.out.println(os + " can't gunzip");
  319. basePath = "D:/images/"+ userId+".jpg"; //与nginx配置相同
  320. }
  321. File file = new File(basePath);
  322. //获取父目录
  323. File fileParent = file.getParentFile();
  324. //判断是否存在
  325. if (!fileParent.exists()) {
  326. fileParent.mkdirs();
  327. }
  328. //获取渠道号
  329. String channel = u.getChannel();
  330. String address = Service.lookup(IPropertiesService.class)
  331. .getSystemProperties().get(channel+"_TO_MACHINE_ERWERMA").getKey();// 为nginx根路径
  332. // 加密客户号
  333. OutputStream outputStream = new FileOutputStream(file);
  334. String openUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?";
  335. //String redirect_uri = "http://xubing2.minpay.cc/adm/admin/wxuserManage/addUserTransfer.html?role=".concat(role).concat("&branchId=").concat(userId);
  336. String redirect_uri = Service.lookup(IPropertiesService.class)
  337. .getSystemProperties().get(channel+"_ADM_URl").getKey();
  338. redirect_uri = redirect_uri.concat("/admin/wxuserManage/addUserTransfer.html?role=").concat(role).concat("&branchId=").concat(userId);
  339. redirect_uri = URLEncoder.encode(redirect_uri, "GBK");
  340. //在微信的appId
  341. String appId = Service.lookup(IPublicService.class).getSysParValue(channel + "_APP_ID");
  342. //配置参数
  343. StringBuffer paramStr = new StringBuffer();
  344. paramStr.append(openUrl);
  345. paramStr.append("appid=");
  346. paramStr.append(appId);
  347. paramStr.append("&redirect_uri=");
  348. paramStr.append(redirect_uri);
  349. paramStr.append("&response_type=code&scope=snsapi_userinfo&state=STATE");
  350. //String urlString = URLEncoder.encode(paramStr.toString(), "GBK");
  351. String admUrl = Service.lookup(IPropertiesService.class)
  352. .getSystemProperties().get(channel+"_ADM_URl").getKey();
  353. admUrl = admUrl.concat("/admin/h5AddUser.html?role=").concat(role).concat("&branchId=").concat(userId);;
  354. IQrcodeService iQrcodeService = new QrcodeServiceImpl();
  355. String urls = iQrcodeService.createQrCode("-1",paramStr.toString(),"-1");
  356. //String urlString = URLEncoder.encode(, "GBK");
  357. http://shjservices.personalpay.cn/adm
  358. // 生成二维码图片到本地+
  359. try {
  360. EquCodeCreateUtil.createQrCode(outputStream,urls,900,"JPEG");
  361. } catch (com.google.zxing.WriterException e) {
  362. // TODO Auto-generated catch block
  363. e.printStackTrace();
  364. }
  365. outputStream.close();
  366. // 上传图片到服务器
  367. InputStream inputSteam = new FileInputStream(file);
  368. String ambient = Service.lookup(IPublicService.class)
  369. .getSysParValue("AMBIENT");//当前环境
  370. FileItem fileItem = createFileItem(file ,"itemFileName");
  371. Map<String, Object> map = null;
  372. if("00".equals(ambient)){//测试
  373. map =Service.lookup(IPublicService.class).uploadFile(fileItem, "00", 0, true,"V01");
  374. }
  375. if("01".equals(ambient)){//生产
  376. map =Service.lookup(IPublicService.class).uploadFileBD(fileItem, "00", 0, true,"V01");
  377. }
  378. String url = String.valueOf(map.get("url"));
  379. inputSteam.close();
  380. // 删除本地图片
  381. file.delete();
  382. res.set("url", url);
  383. return res;
  384. }
  385. /*
  386. 创建FileItem
  387. */
  388. private FileItem createFileItem(File file, String fieldName) {
  389. FileItemFactory factory = new DiskFileItemFactory(16, null);
  390. FileItem item = factory.createItem(fieldName, "text/plain", true, file.getName());
  391. int bytesRead = 0;
  392. byte[] buffer = new byte[8192];
  393. try {
  394. FileInputStream fis = new FileInputStream(file);
  395. OutputStream os = item.getOutputStream();
  396. while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
  397. os.write(buffer, 0, bytesRead);
  398. }
  399. os.close();
  400. fis.close();
  401. } catch (IOException e) {
  402. e.printStackTrace();
  403. }
  404. return item;
  405. }
  406. }