UserInfoServiceImpl.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. package com.izouma.awesomeadmin.service.impl;
  2. import com.izouma.awesomeadmin.constant.AppConstant;
  3. import com.izouma.awesomeadmin.dao.*;
  4. import com.izouma.awesomeadmin.dto.Page;
  5. import com.izouma.awesomeadmin.model.AlipayTemp;
  6. import com.izouma.awesomeadmin.model.UserInfo;
  7. import com.izouma.awesomeadmin.service.OSSFileService;
  8. import com.izouma.awesomeadmin.service.UserInfoService;
  9. import com.izouma.awesomeadmin.util.MD5Util;
  10. import com.izouma.awesomeadmin.util.PropertiesFileLoader;
  11. import com.izouma.awesomeadmin.util.WeixinUtil;
  12. import io.jsonwebtoken.Claims;
  13. import io.jsonwebtoken.Jwt;
  14. import io.jsonwebtoken.Jwts;
  15. import io.jsonwebtoken.security.Keys;
  16. import io.rong.RongCloud;
  17. import io.rong.models.SMSVerifyCodeResult;
  18. import org.apache.commons.lang.StringUtils;
  19. import org.apache.log4j.Logger;
  20. import org.json.JSONException;
  21. import org.json.JSONObject;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.stereotype.Service;
  24. import javax.crypto.SecretKey;
  25. import java.io.IOException;
  26. import java.net.HttpURLConnection;
  27. import java.net.URL;
  28. import java.text.SimpleDateFormat;
  29. import java.util.*;
  30. /**
  31. * user_info service接口实现类
  32. * Tue Apr 17 10:32:49 CST 2018 Suo Chen Cheng
  33. */
  34. @Service
  35. public class UserInfoServiceImpl implements UserInfoService {
  36. private static Logger logger = Logger.getLogger(UserInfoServiceImpl.class);
  37. private RongCloud rongCloud = RongCloud.getInstance(PropertiesFileLoader.getProperties("rongyunappkey"), PropertiesFileLoader.getProperties("rongyunappsecret"));
  38. @Autowired
  39. private UserInfoMapper userInfoMapper;
  40. @Autowired
  41. private SysRoleMapper sysRoleMapper;
  42. @Autowired
  43. private DepartInfoMapper departInfoMapper;
  44. @Autowired
  45. private SysAppTokenMapper sysAppTokenMapper;
  46. @Autowired
  47. private OSSFileService ossFileService;
  48. @Autowired
  49. private AlipayTempMapper alipayTempMapper;
  50. @Override
  51. public List<UserInfo> getUserInfoList(UserInfo record) {
  52. logger.info("getUserInfoList");
  53. try {
  54. return userInfoMapper.queryAllUserInfo(record);
  55. } catch (Exception e) {
  56. logger.error("getUserInfoList", e);
  57. }
  58. return null;
  59. }
  60. @Override
  61. public List<UserInfo> getUserInfoByPage(Page page, UserInfo record) {
  62. logger.info("getUserInfoByPage");
  63. try {
  64. Map<String, Object> parameter = new HashMap<String, Object>();
  65. parameter.put("record", record);
  66. parameter.put(AppConstant.PAGE, page);
  67. return userInfoMapper.queryUserInfosByPage(parameter);
  68. } catch (Exception e) {
  69. logger.error("getUserInfoByPage", e);
  70. }
  71. return null;
  72. }
  73. @Override
  74. public UserInfo getUserInfoById(String id) {
  75. logger.info("getUserInfoById");
  76. try {
  77. return userInfoMapper.selectByPrimaryKey(Integer.valueOf(id));
  78. } catch (Exception e) {
  79. logger.error("getUserInfoById", e);
  80. }
  81. return null;
  82. }
  83. @Override
  84. public UserInfo getUserInfo(UserInfo record) {
  85. logger.info("getUserInfo");
  86. try {
  87. return userInfoMapper.queryUserInfo(record);
  88. } catch (Exception e) {
  89. logger.error("getUserInfo", e);
  90. }
  91. return null;
  92. }
  93. @Override
  94. public UserInfo getSingleUserInfo(UserInfo record) {
  95. logger.info("getSingleUserInfo");
  96. try {
  97. return userInfoMapper.querySingleUserInfo(record);
  98. } catch (Exception e) {
  99. logger.error("getSingleUserInfo", e);
  100. }
  101. return null;
  102. }
  103. @Override
  104. public boolean createUserInfo(UserInfo record) {
  105. logger.info("createUserInfo");
  106. try {
  107. if (StringUtils.isNotEmpty(record.getPassword())) {
  108. record.setPassword(MD5Util.getMD5(record.getPassword()));
  109. }
  110. int updates = userInfoMapper.insertSelective(record);
  111. if (updateUserRolesAndDeparts(record, updates)) return true;
  112. } catch (Exception e) {
  113. logger.error("createUserInfo", e);
  114. }
  115. return false;
  116. }
  117. private boolean updateUserRolesAndDeparts(UserInfo record, int updates) {
  118. if (updates > 0) {
  119. if (record.getRoleId() != null) {
  120. sysRoleMapper.clearUserRoles(record.getId());
  121. sysRoleMapper.setUserRoles(record.getId(), Arrays.asList(record.getRoleId().split(",")));
  122. }
  123. return true;
  124. }
  125. return false;
  126. }
  127. @Override
  128. public boolean deleteUserInfo(String id) {
  129. logger.info("deleteUserInfo");
  130. try {
  131. int updates = userInfoMapper.delete(id);
  132. departInfoMapper.clearUserDeparts(Integer.valueOf(id));
  133. sysRoleMapper.clearUserRoles(Integer.valueOf(id));
  134. if (updates > 0) {
  135. return true;
  136. }
  137. } catch (Exception e) {
  138. logger.error("deleteUserInfo", e);
  139. }
  140. return false;
  141. }
  142. @Override
  143. public boolean updateUserInfo(UserInfo record) {
  144. logger.info("updateUserInfo");
  145. try {
  146. if (StringUtils.isNotEmpty(record.getPassword())) {
  147. record.setPassword(MD5Util.getMD5(record.getPassword()));
  148. }
  149. int updates = userInfoMapper.updateByPrimaryKeySelective(record);
  150. if (updateUserRolesAndDeparts(record, updates)) return true;
  151. } catch (Exception e) {
  152. logger.error("updateUserInfo", e);
  153. }
  154. return false;
  155. }
  156. @Override
  157. public boolean updatePassword(UserInfo record) {
  158. logger.info("updatePassword");
  159. try {
  160. if (StringUtils.isNotEmpty(record.getPassword())) {
  161. record.setPassword(MD5Util.getMD5(record.getPassword()));
  162. }
  163. int updates = userInfoMapper.updatePassword(record);
  164. if (updates > 0) {
  165. return true;
  166. }
  167. } catch (Exception e) {
  168. logger.error("updatePassword", e);
  169. }
  170. return false;
  171. }
  172. @Override
  173. public UserInfo login(String username, String password) {
  174. logger.info("login");
  175. try {
  176. Map<String, Object> map = new HashMap<>();
  177. map.put("username", username);
  178. map.put("password", MD5Util.getMD5(password));
  179. UserInfo result = userInfoMapper.login(map);
  180. return result;
  181. } catch (Exception e) {
  182. logger.error("login", e);
  183. }
  184. return null;
  185. }
  186. @Override
  187. public UserInfo loginSms(String phone, String code, String sessionId) throws LoginException {
  188. logger.info("loginSms");
  189. // SMSVerifyCodeResult sMSVerifyCodeResult;
  190. // try {
  191. // sMSVerifyCodeResult = rongCloud.sms.verifyCode(sessionId, code);
  192. // } catch (Exception e) {
  193. // e.printStackTrace();
  194. // throw new LoginException("验证码错误");
  195. // }
  196. try {
  197. if (code.toLowerCase().equals(sessionId)) {
  198. UserInfo userInfo = new UserInfo();
  199. userInfo.setPhone(phone);
  200. userInfo = getUserInfo(userInfo);
  201. if (userInfo == null) {
  202. userInfo = new UserInfo();
  203. userInfo.setPhone(phone);
  204. userInfo.setUsername(phone);
  205. userInfo.setNickname(phone);
  206. userInfo.setIcon("https://microball.oss-cn-hangzhou.aliyuncs.com/huanbaojia/icon_morentouxiang.png");
  207. if (!createUserInfo(userInfo)) {
  208. throw new LoginException("登录失败");
  209. }
  210. }
  211. return userInfo;
  212. }
  213. } catch (Exception e) {
  214. e.printStackTrace();
  215. throw new LoginException("验证码错误");
  216. }
  217. throw new LoginException("验证码错误");
  218. }
  219. @Override
  220. public UserInfo loginAppToken(String token) {
  221. logger.info("loginAppToken");
  222. UserInfo userInfo = null;
  223. try {
  224. // AppToken appToken = sysAppTokenMapper.getToken(token);
  225. // if (appToken != null) {
  226. SecretKey key = Keys.hmacShaKeyFor(Base64.getDecoder().decode(PropertiesFileLoader.getProperties("jwtsecret").getBytes()));
  227. Jwt jwt = Jwts.parser()
  228. .setSigningKey(key)
  229. .parse(token);
  230. Claims claims = (Claims) jwt.getBody();
  231. if (claims.getExpiration() != null) {
  232. if (claims.getExpiration().before(new Date())) {
  233. return null;
  234. }
  235. }
  236. if (claims.getSubject().equals("guest")) {
  237. userInfo = new UserInfo();
  238. } else {
  239. userInfo = getUserInfoById(claims.getSubject());
  240. }
  241. // }
  242. } catch (Exception e) {
  243. logger.error("loginAppToken", e);
  244. }
  245. return userInfo;
  246. }
  247. @Override
  248. public List<String> findDepartLeader(String userId) {
  249. logger.info("findDepartLeader");
  250. try {
  251. return userInfoMapper.findDepartLeader(userId);
  252. } catch (Exception e) {
  253. logger.error("findDepartLeader", e);
  254. }
  255. return null;
  256. }
  257. @Override
  258. public List<String> findUserByRoleName(String roleName) {
  259. logger.info("findUserByRoleName");
  260. try {
  261. return userInfoMapper.findUserByRoleName(roleName);
  262. } catch (Exception e) {
  263. logger.error("findUserByRoleName", e);
  264. }
  265. return null;
  266. }
  267. public class LoginException extends Exception {
  268. public LoginException(String message) {
  269. super(message);
  270. }
  271. }
  272. @Override
  273. public UserInfo loginWechat(String code) {
  274. try {
  275. final String APP_ID = PropertiesFileLoader.getProperties("weixinappid");
  276. final String APP_SECRET = PropertiesFileLoader.getProperties("weixinsecret");
  277. String accessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + APP_ID + "&secret=" + APP_SECRET + "&code=" + code
  278. + "&grant_type=authorization_code";
  279. JSONObject data = WeixinUtil.loadJSON(accessTokenUrl);
  280. logger.debug("微信授权获取access_token:\n" + data.toString(4));
  281. String openId = data.getString("openid");
  282. String access_token = data.getString("access_token");
  283. String userDataUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openId;
  284. JSONObject userData = WeixinUtil.loadJSON(userDataUrl);
  285. logger.debug("微信授权获取用户信息:\n" + userData.toString(4));
  286. UserInfo userInfo = new UserInfo();
  287. userInfo.setOpenId(openId);
  288. userInfo = getUserInfo(userInfo);
  289. if (userInfo != null) {
  290. return userInfo;
  291. }
  292. userInfo = new UserInfo();
  293. userInfo.setOpenId(openId);
  294. String nickname = userData.getString("nickname");
  295. userInfo.setNickname(nickname);
  296. userInfo.setUsername(nickname);
  297. try {
  298. String country = userData.getString("country");
  299. userInfo.setCountry(country);
  300. } catch (JSONException ignored) {
  301. }
  302. try {
  303. String province = userData.getString("province");
  304. userInfo.setProvince(province);
  305. } catch (JSONException ignored) {
  306. }
  307. try {
  308. String city = userData.getString("city");
  309. userInfo.setCity(city);
  310. } catch (JSONException ignored) {
  311. }
  312. int sex = 1;
  313. try {
  314. sex = userData.getInt("sex");
  315. } catch (JSONException ignored) {
  316. }
  317. userInfo.setSex(sex == 1 ? "男" : "女");
  318. String headimgurl = null;
  319. try {
  320. headimgurl = userData.getString("headimgurl");
  321. } catch (JSONException ignored) {
  322. }
  323. userInfo.setIcon(saveAvatar(headimgurl));
  324. if (createUserInfo(userInfo)) {
  325. return userInfo;
  326. }
  327. } catch (Exception e) {
  328. logger.error("loginWechat", e);
  329. }
  330. return null;
  331. }
  332. private String saveAvatar(String url) {
  333. String path;
  334. HttpURLConnection httpUrl = null;
  335. URL iconUrl = null;
  336. try {
  337. iconUrl = new URL(url);
  338. httpUrl = (HttpURLConnection) iconUrl.openConnection();
  339. httpUrl.connect();
  340. Random random = new Random();
  341. StringBuilder randomCode = new StringBuilder();
  342. for (int i = 0; i < 8; i++) {
  343. randomCode.append(Integer.toString(random.nextInt(36), 36));
  344. }
  345. String uploadPath = String.format("images/%s-%s.jpg", new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss").format(new Date()), randomCode);
  346. path = ossFileService.upload(httpUrl.getInputStream(), uploadPath);
  347. } catch (IOException e) {
  348. path = "https://microball.oss-cn-hangzhou.aliyuncs.com/huanbaojia/icon_morentouxiang.png";
  349. e.printStackTrace();
  350. } finally {
  351. if (httpUrl != null) {
  352. httpUrl.disconnect();
  353. }
  354. }
  355. return path;
  356. }
  357. /**
  358. * <p>支付宝支付成功回调接口。充值</p>
  359. *
  360. * @param tradeNo
  361. * @param tradeStatus
  362. */
  363. @Override
  364. public void alipaySuccess(String tradeNo, String tradeStatus) {
  365. logger.info("alipaySuccess:支付宝支付成功回调接口 tradeNo:" + tradeNo + ", tradeStatus:" + tradeStatus);
  366. try {
  367. AlipayTemp alipayTemp = new AlipayTemp();
  368. alipayTemp.setOutTradeNo(tradeNo);
  369. alipayTemp = alipayTempMapper.queryAlipayTemp(alipayTemp);
  370. if (alipayTemp != null) {
  371. if (!AppConstant.Aliapi.TRADE_SUCCESS.equals(alipayTemp.getTradeStatus())) {
  372. UserInfo userInfo = this.getUserInfoById(alipayTemp.getOrderId());
  373. int cashPledge = userInfo.getCashPledge();
  374. UserInfo updateUser = new UserInfo();
  375. updateUser.setId(userInfo.getId());
  376. updateUser.setCashPledge(cashPledge + alipayTemp.getTotalAmount().intValue());
  377. if (userInfo.getApproveStep() == 3) {
  378. //如果是交押金状态,更新为完成。
  379. updateUser.setApproveStep(4);
  380. }
  381. this.updateUserInfo(updateUser);
  382. alipayTemp.setTradeStatus(tradeStatus);
  383. alipayTempMapper.updateByPrimaryKeySelective(alipayTemp);
  384. }
  385. }
  386. } catch (Exception e) {
  387. logger.error("alipaySuccess:支付宝支付成功回调接口异常 tradeNo:" + tradeNo + ", tradeStatus:" + tradeStatus, e);
  388. }
  389. }
  390. }