AppUserInfoController.java 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. package com.platform.yijia.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.alipay.api.*;
  4. import com.alipay.api.internal.util.AlipayEncrypt;
  5. import com.alipay.api.request.AlipaySystemOauthTokenRequest;
  6. import com.alipay.api.request.AlipayUserInfoAuthRequest;
  7. import com.alipay.api.request.AlipayUserInfoShareRequest;
  8. import com.alipay.api.response.AlipaySystemOauthTokenResponse;
  9. import com.alipay.api.response.AlipayUserInfoAuthResponse;
  10. import com.alipay.api.response.AlipayUserInfoShareResponse;
  11. import com.google.gson.Gson;
  12. import com.platform.yijia.config.YiJiaRequest;
  13. import com.platform.yijia.param.request.AppUserInfoRequest;
  14. import com.platform.yijia.pojo.*;
  15. import com.platform.yijia.service.*;
  16. import com.platform.yijia.utils.CodeMsg;
  17. import com.platform.yijia.utils.ResultData;
  18. import com.platform.yijia.utils.redis.RedisCacheUtil;
  19. import com.platform.yijia.utils.weixinapp.AesDecryptUtil;
  20. import com.platform.yijia.utils.weixinapp.WeiXinUserUtil;
  21. import com.sun.org.apache.bcel.internal.classfile.Code;
  22. import org.apache.commons.codec.digest.DigestUtils;
  23. import org.apache.commons.lang3.StringUtils;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.stereotype.Controller;
  28. import org.springframework.transaction.annotation.Transactional;
  29. import org.springframework.web.bind.annotation.*;
  30. import javax.annotation.Resource;
  31. import java.math.BigDecimal;
  32. import java.security.MessageDigest;
  33. import java.security.NoSuchAlgorithmException;
  34. import java.util.Date;
  35. import java.util.HashMap;
  36. import java.util.List;
  37. import java.util.Map;
  38. import java.util.concurrent.TimeUnit;
  39. @Controller
  40. @YiJiaRequest()
  41. public class AppUserInfoController {
  42. private static Logger logger =(Logger) LoggerFactory.getLogger(AppUserInfoController.class);
  43. @Autowired
  44. private PayOrderService payOrderService;
  45. @Autowired
  46. private AppUserInfoService appUserInfoService;
  47. @Resource
  48. private StationService stationService;
  49. @Resource
  50. private CustomerPointsService customerPointsService;
  51. @Resource
  52. private RedisCacheUtil redisCacheUtil;
  53. @Resource
  54. private ElectronicMembershipCardService electronicMembershipCardService;
  55. // String appId = "wxe1135cd390b38a54"; //微信小程序appID
  56. // String appSecret = "0532c7d9ae876c4ad636df0b1e3b9ddb"; //微信小程序密钥
  57. //支付宝小程序:公钥、私钥、解密钥
  58. String PRIVATE_KEY ="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCBVAR3NE/UNvnpXNhmx8xX6se8euxQefbbkWLSykgA9dkP7eaNDyllMtuxjNLaQ1VB26sh2us0VrzSsNA7nmX5aPXu7ckHXH6wBzChCeCdDCHVpoHVq5doehgwMQrvh6aUEcPId+szepvoMmKaSEbFmFapZrTxrPOthI6ENR3RRgs+29vYgru+qJtg3V+tsOr1gkhocU16dX9s7zYfSQQNOmFa01aAhtwI9pDKZVYtELrqOg1dP5WY+jMyXReaDLfAP40H3X7+sVuzTCLu4oVE9CMU2rR+hxwjs6rVWbgE4z13IbM3N8FoPjwJJK0+cXYuORE3yHd4eZj3pEFKvCajAgMBAAECggEADHnAXIdtU/lhrkNIlNGdQrXhQqtQ9IpvZ8rkj5T30S/mGoVGgkD+K0b2rm/5PxjTtqCsJ33oxom/eUuZKsci6yZW4w+JHu6rQevPoxlC06OB+Awx5hzBZFCy2KNCfMzHIu17x3DZ/YNMWt/RZzSlrxX89iZ0OgQHFav9238MO02z9ejRPQEWO4tNb7YyHmi1h56fH2Y+cvLh32QWc+t16tkr5HVY6I2ktMIHYpGU4lyfYEjZ01P7QruAVol2lDNiIYVFXfyDvtwqQNWol6iCswSrDFj0LFsdTWEA1OLZUdD/mfLuBJ9nUAHoCyIh3uR+Aag87DRURcFN92hG0cex4QKBgQDwzmdUC1upAKmRTVEcKUUyDxHZxDhfjRoyhZGf6s/paPvWAVo60VGDC2lQeYqQ8sMnWbpAYWq/yFT2bGK9iPD9UoZWKwLSJuR5RAhiet5WpCfvRmEqEVHJtCaZeV0Q4Al2dPZqu/7opFsrrlY5MvdKJZuLCtL/UBdmkz4BJyFwGQKBgQCJfPoIF5EI/4iYvAQM6FNEQ/kbtb3BF9In9m5GwaMP25ho8evkdI0q5H88FI+i+PdqtdM2gKhuNCXNK/LbF8AzEAu0Qr2clwOq6wQzPU4wl0RNrmdg6H/YC1uTgoy4cr9RdTrqOwXT2UyWghP7AjrifcstZVsrdamOxQHRQ/p0GwKBgQC/uHWH8msNyRs4NkiHlUwkKN9bc+A2Iw9iffmNStQxz4YBwp3mClVNcj5s9feeo8lAgS3KqETnYEZU08JhiK/l4FhYLnJWguSQr9vFHHH8pUGuPWiKp7NoVegNdRTlpJmkGEdAf2DaNFDqHClF7Q/5lquaLgoDf+7BmiVZoZ/7sQKBgF9aw1qLZba063rqo3CETQPClOnIAYQDTmkS++as87fS2ICokK9QunuuICR+KiSgwZBL7OIXFznfhvwmZ/fWICIKFeP+id0py4xmFPKoqSsALool1GMcvVYMgoXq/EgMpW4MfUDokKEeS4TzUII9SLO/BQRGEJYys9dmAV2u5OxTAoGAb5qCQVtmmhO+A0GJG4KZ4xd3i54ZiykrRyCLralBudpI6Vr5+N6yD4R5IdqN2iJKcokt1TxtcJzrunKhUHtOXEiLK/k872iWGQhPeMV+sqWcQXQpKLtRhn4dROOP2qgeOHtdkeLoPlIcfd6TjIsGJJCksXE2hNKr6aljS/zhoKo=";
  59. String PUBLIC_KEY ="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsXdLLSfp79oe89Q+i1KHx7DfNQjAPWlDaBVtHnwo+jiCkVcNzkeRtz5mZsM3lBURFG6T3eY3qUu0b3FUCSUWd1Q06J4bFI1y4ID3JBCyRi0Up5pcqZzwZqa22QxK3m5/2tabd56FL9C/PPSuQj8gPgT8eUbPUZFeLcnYCLSM0VKU/ZxlXRx9G0MEOZizMxiGdKVwNCE1BxJKTybFz2X0AyakpOWlAZZNQm3JIGfJVHEsc2lgBoeEt8T8d1AJiTdlzAayRGJWpX0nymRZ+RhwQQmuKlPGPvLJDNTV87YvvMttmzlXN32lobK6203PlOoDiLmenj0lxniZfC9tPcRhgwIDAQAB";
  60. String ALI_APP_ID = "2021002140684377";
  61. String ENCRYPT_KEY ="K5F2BgcYsRYo8YpRnNSTsg==";
  62. //获取支付宝用户信息
  63. @ResponseBody
  64. @RequestMapping(value = "/getAliUserInfo")
  65. public String getAliUserInfo(@RequestParam String code){
  66. Gson gson =new Gson();
  67. //返回结果集
  68. ResultData resultData = null;
  69. logger.info("获取用户信息所需code:"+code);
  70. if(StringUtils.isNotBlank(code)){
  71. try {
  72. AlipayClient alipayClient = new DefaultAlipayClient(
  73. "https://openapi.alipay.com/gateway.do",
  74. ALI_APP_ID,
  75. PRIVATE_KEY,
  76. "json",
  77. "UTF-8",
  78. PUBLIC_KEY,
  79. "RSA2");
  80. AlipaySystemOauthTokenRequest alipaySystemOauthTokenRequest = new AlipaySystemOauthTokenRequest();
  81. alipaySystemOauthTokenRequest.setGrantType("authorization_code");
  82. alipaySystemOauthTokenRequest.setCode(code);
  83. AlipaySystemOauthTokenResponse alipaySystemOauthTokenResponse = alipayClient.execute(alipaySystemOauthTokenRequest);
  84. if (alipaySystemOauthTokenResponse.isSuccess()){
  85. logger.info("支付宝小程序获取access_token:"+ alipaySystemOauthTokenResponse.getAccessToken());
  86. //获取用户信息
  87. AlipayUserInfoShareRequest alipayUserInfoShareRequest = new AlipayUserInfoShareRequest();
  88. AlipayUserInfoShareResponse alipayUserInfoShareResponse = alipayClient.execute(alipayUserInfoShareRequest, alipaySystemOauthTokenResponse.getAccessToken());
  89. resultData = ResultData.success(alipayUserInfoShareResponse);
  90. }else {
  91. resultData = ResultData.success(alipaySystemOauthTokenResponse);
  92. }
  93. } catch (AlipayApiException e) {
  94. e.printStackTrace();
  95. resultData =ResultData.error(CodeMsg.REQUEST_FAIL);
  96. }
  97. }else {
  98. resultData =ResultData.error(CodeMsg.PARAMS_NUll);
  99. }
  100. return gson.toJson(resultData);
  101. }
  102. //支付宝小程序用户手机号解密 encryptedData
  103. @ResponseBody
  104. @RequestMapping(value = "/alipayEncryptUserPhone", method = RequestMethod.POST, consumes = "application/json")
  105. public String alipayEncryptUserPhone(@RequestBody JSONObject jsonObject){
  106. Gson gson =new Gson();
  107. //返回结果集
  108. ResultData resultData = null;
  109. if(StringUtils.isNotBlank(jsonObject.get("encryptedData").toString())){
  110. try {
  111. //解密用户手机号数据
  112. String encryptedData = jsonObject.get("encryptedData").toString();
  113. logger.info("支付宝获取手机号要解密数据: " +encryptedData);
  114. String aes = AlipayEncrypt.decryptContent(encryptedData, "AES", ENCRYPT_KEY, "UTF-8");
  115. logger.info("支付宝获取手机号解密后的数据:" +aes);
  116. resultData = ResultData.success(aes);
  117. } catch (AlipayApiException e) {
  118. e.printStackTrace();
  119. resultData = ResultData.error(CodeMsg.REQUEST_FAIL);
  120. }
  121. }else {
  122. resultData = ResultData.error(CodeMsg.PARAMS_NUll);
  123. }
  124. return gson.toJson(resultData);
  125. }
  126. /*
  127. * 微信小程序根据code获取用户openId和sessionKey
  128. * @param code
  129. * @param stationId
  130. * @return
  131. */
  132. @ResponseBody
  133. @RequestMapping(value = "/getSessionKeyAndOpenID")
  134. public String getSessionKeyAndOpenID(@RequestParam String code, Integer stationId){
  135. logger.info("微信小程序获取用户openId和sessionKey入参code和stationId: "+code +"-----" +stationId);
  136. Gson gson =new Gson();
  137. //返回结果集
  138. ResultData resultData = null;
  139. if(stationId !=null && StringUtils.isNotBlank(code)){
  140. String appId = ""; //微信小程序appID
  141. String appSecret = ""; //微信小程序密钥
  142. Map<String, String> map = stationService.getStationAppIdAndAppSecret(stationId);
  143. appId = map.get("appId"); //微信小程序appID
  144. appSecret = map.get("appSecret"); //微信小程序密钥
  145. logger.info("油站信息:"+map.toString());
  146. Map<String, Object> userSessionKeyAndOpenID = WeiXinUserUtil.getUserSessionKeyAndOpenID(appId, appSecret, code);
  147. resultData = ResultData.success(userSessionKeyAndOpenID);
  148. }else {
  149. resultData = ResultData.error(CodeMsg.REQUEST_FAIL);
  150. }
  151. return gson.toJson(resultData);
  152. }
  153. /*
  154. * 解密 encryptedData
  155. * @param sessionKey
  156. * @param encryptedData
  157. * @param iv
  158. * @return
  159. */
  160. @ResponseBody
  161. @RequestMapping(value = "/decryptEncryptedData", method = RequestMethod.POST, consumes = "application/json")
  162. public String decryptEncryptedData(@RequestBody JSONObject jsonObject){
  163. logger.info("解密获取手机号前台传入参数:" +jsonObject.toString());
  164. Gson gson =new Gson();
  165. //返回结果集
  166. ResultData resultData = null;
  167. String encryptedData = jsonObject.get("encryptedData").toString();
  168. String sessionKey = jsonObject.get("sessionKey").toString();
  169. String iv = jsonObject.get("iv").toString();
  170. if(StringUtils.isNotBlank(encryptedData) && StringUtils.isNotBlank(sessionKey) && StringUtils.isNotBlank(iv) ){
  171. net.sf.json.JSONObject jsonObject1 = AesDecryptUtil.decryptEncryptedData(encryptedData, sessionKey, iv, "UTF-8");
  172. resultData = ResultData.success(jsonObject1);
  173. }else {
  174. resultData = ResultData.error(CodeMsg.REQUEST_FAIL);
  175. }
  176. return gson.toJson(resultData);
  177. }
  178. /*
  179. * JS-SDK使用权限签名算法
  180. * @param noncestr 随机字符串
  181. * @param timestamp 时间戳
  182. * @param url 页面访问路径
  183. * @param stationId 油站ID
  184. * @return
  185. */
  186. @RequestMapping(value = "/getSignature", method = RequestMethod.GET)
  187. @ResponseBody
  188. public String getSignature(@RequestParam String noncestr, String timestamp, String url, Integer stationId){
  189. Gson gson =new Gson();
  190. //返回结果集
  191. ResultData resultData = null;
  192. logger.info("JS-SDK使用权限签名算法前台传来参数: "+"油站ID:"+stationId+"; noncestr: " +noncestr+"; timestamp: " +timestamp + "; url: "+url);
  193. Map<String, String> m = stationService.getStationAppIdAndAppSecret(stationId);
  194. String gzhAppId ="";
  195. String gzhAppSecret ="";
  196. String accessToken ="";
  197. if(m !=null && m.containsKey("gzhAppId") && m.containsKey("gzhAppSecret")){
  198. gzhAppId = m.get("gzhAppId");
  199. gzhAppSecret = m.get("gzhAppSecret");
  200. accessToken = this.getTokenByRedisCache(gzhAppId, gzhAppSecret);
  201. //当前用户登录的公众号redis缓存中的jsapi_ticket
  202. String appIdTicket =gzhAppId + "-ticket";
  203. String jsapiTicket ="";
  204. if(redisCacheUtil.hasKey(appIdTicket)){
  205. jsapiTicket = redisCacheUtil.getCacheObject(appIdTicket);
  206. }else {
  207. //获取jsapi_ticket
  208. net.sf.json.JSONObject gzhJsapiTicket = WeiXinUserUtil.getGzhJsapiTicket(accessToken);
  209. if(gzhJsapiTicket.containsKey("ticket")){
  210. redisCacheUtil.setCacheObject(appIdTicket, gzhJsapiTicket.get("ticket"));
  211. redisCacheUtil.expire(appIdTicket, 7200, TimeUnit.SECONDS);
  212. jsapiTicket = gzhJsapiTicket.get("ticket").toString();
  213. }
  214. }
  215. String string1 = "jsapi_ticket="+jsapiTicket + "&noncestr=" +noncestr +"&timestamp=" +timestamp +"&url=" +url;
  216. logger.info("签名参数拼接:"+ string1);
  217. //sha1加密
  218. String signature = DigestUtils.sha1Hex(string1);
  219. logger.info("JS-SDK签名参数sha1加密后:"+ signature);
  220. resultData = ResultData.success(signature);
  221. }
  222. return gson.toJson(resultData);
  223. }
  224. /*
  225. * 判断使用小程序支付的用户是否关注公众号
  226. * @param unionId
  227. * @param stationId
  228. * @return
  229. */
  230. @RequestMapping(value = "/whetherFollowGzh", method = RequestMethod.GET)
  231. @ResponseBody
  232. public String whetherFollowGzh(@RequestParam String unionId, Integer stationId){
  233. Gson gson =new Gson();
  234. //返回结果集
  235. ResultData resultData = null;
  236. if (unionId !=null){
  237. Map<String, String> param = new HashMap<>();
  238. param.put("unionId", unionId);
  239. param.put("userType", "1");
  240. param.put("stationId", stationId.toString());
  241. String openId = appUserInfoService.whetherFollowGzh(param);
  242. if(openId !=null){
  243. Map<String, String> m = stationService.getStationAppIdAndAppSecret(stationId);
  244. String gzhAppId ="";
  245. String gzhAppSecret ="";
  246. String accessToken ="";
  247. if(m !=null && m.containsKey("gzhAppId") && m.containsKey("gzhAppSecret")){
  248. gzhAppId = m.get("gzhAppId");
  249. gzhAppSecret = m.get("gzhAppSecret");
  250. accessToken = this.getTokenByRedisCache(gzhAppId, gzhAppSecret);
  251. net.sf.json.JSONObject unionIdInfo = WeiXinUserUtil.getUnionIdInfo(accessToken, openId);
  252. logger.info("用户公众号信息:" + unionIdInfo);
  253. if(unionIdInfo.containsKey("subscribe") && unionIdInfo.get("subscribe").equals(1)){
  254. resultData = ResultData.success(CodeMsg.SUCCESS);
  255. }else {
  256. resultData = ResultData.error(CodeMsg.NO_FollowGzh);
  257. }
  258. }else {
  259. resultData = ResultData.error(CodeMsg.SEARCH_APPID_FAIL);
  260. }
  261. }else {
  262. resultData = ResultData.error(CodeMsg.USERGZHINFO_EXSIST);
  263. }
  264. }
  265. return gson.toJson(resultData);
  266. }
  267. /*
  268. * 获取公众号授权后的信息
  269. * @param code
  270. * @param stationId
  271. * @return
  272. */
  273. @RequestMapping(value = "/getGzhAuthorizeInfo", method = RequestMethod.GET)
  274. @ResponseBody
  275. public String getGzhAuthorizeInfo(@RequestParam String code, Integer stationId){
  276. logger.info("获取公众号授权后的信息前端传递参数:code:"+ code );
  277. logger.info("获取公众号授权后的信息前端传递参数:stationId:" +stationId);
  278. Gson gson =new Gson();
  279. //返回结果集
  280. ResultData resultData = null;
  281. if(code != null && stationId !=null){
  282. String gzhAppId ="";
  283. String gzhAppSecret ="";
  284. Map<String, String> m = stationService.getStationAppIdAndAppSecret(stationId);
  285. if(m !=null && m.containsKey("gzhAppId") && m.containsKey("gzhAppSecret")){
  286. gzhAppId = m.get("gzhAppId");
  287. gzhAppSecret = m.get("gzhAppSecret");
  288. net.sf.json.JSONObject gzhAuthorizeInfo = WeiXinUserUtil.getGzhAuthorizeInfo(gzhAppId, gzhAppSecret, code);
  289. logger.info("授权获取到信息:" + gzhAuthorizeInfo.toString());
  290. resultData = ResultData.success(gzhAuthorizeInfo);
  291. }else {
  292. resultData = ResultData.error(CodeMsg.REQUEST_FAIL);
  293. }
  294. }else {
  295. resultData = ResultData.error(CodeMsg.REQUEST_FAIL);
  296. }
  297. return gson.toJson(resultData);
  298. }
  299. /*
  300. * 测试用来
  301. * 获取公众号授权后的信息测试
  302. * @param code
  303. * @param stationId
  304. * @return
  305. */
  306. @RequestMapping(value = "/getGzhAuthorizeInfoNew", method = RequestMethod.GET)
  307. @ResponseBody
  308. public String getGzhAuthorizeInfoNew(@RequestParam String code, Integer stationId){
  309. logger.info("获取公众号授权后的信息前端传递参数:code:"+ code );
  310. logger.info("获取公众号授权后的信息前端传递参数:stationId:" +stationId);
  311. Gson gson =new Gson();
  312. //返回结果集
  313. ResultData resultData = null;
  314. if(code != null && stationId !=null){
  315. String gzhAppId ="";
  316. String gzhAppSecret ="";
  317. Map<String, String> m = stationService.getStationAppIdAndAppSecret(stationId);
  318. if(m !=null && m.containsKey("gzhAppId") && m.containsKey("gzhAppSecret")){
  319. gzhAppId = m.get("gzhAppId");
  320. gzhAppSecret = m.get("gzhAppSecret");
  321. net.sf.json.JSONObject gzhAuthorizeInfo = WeiXinUserUtil.getGzhAuthorizeInfo(gzhAppId, gzhAppSecret, code);
  322. logger.info("授权获取到信息:" + gzhAuthorizeInfo.toString());
  323. if(gzhAuthorizeInfo.containsKey("openid")){
  324. String openId = gzhAuthorizeInfo.get("openid").toString();
  325. String accessToken = this.getTokenByRedisCache(gzhAppId, gzhAppSecret);
  326. net.sf.json.JSONObject unionIdInfo = null;
  327. unionIdInfo= WeiXinUserUtil.getUnionIdInfo(accessToken, openId);
  328. if(unionIdInfo.containsKey("errcode")){
  329. //删除原来key
  330. redisCacheUtil.deleteObject(gzhAppId);
  331. String token = WeiXinUserUtil.getToken(gzhAppId, gzhAppSecret).getAccessToken();
  332. redisCacheUtil.setCacheObject(gzhAppId, token);
  333. redisCacheUtil.expire(gzhAppId, 7200, TimeUnit.SECONDS);
  334. unionIdInfo= WeiXinUserUtil.getUnionIdInfo(token, openId);
  335. }
  336. AppUserInfo appUserInfo = new AppUserInfo();
  337. if(unionIdInfo.containsKey("openid")){
  338. appUserInfo.setBlogOpenid(unionIdInfo.get("openid").toString());
  339. }
  340. if(unionIdInfo.containsKey("nickname")){
  341. appUserInfo.setBlogNickName(unionIdInfo.get("nickname").toString());
  342. }
  343. if(unionIdInfo.containsKey("sex")){
  344. appUserInfo.setSexFlag(unionIdInfo.get("sex").toString());
  345. }
  346. if(unionIdInfo.containsKey("headimgurl")){
  347. appUserInfo.setBlogProfilePhoto(unionIdInfo.get("headimgurl").toString());
  348. }
  349. if(unionIdInfo.containsKey("unionid")){
  350. appUserInfo.setUnionId(unionIdInfo.get("unionid").toString());
  351. }
  352. appUserInfo.setUserType("1");
  353. Map<String, Object> params = new HashMap<>();
  354. params.put("openId", openId); //注册用户openId
  355. params.put("userType", appUserInfo.getUserType()); //用户类型
  356. //判断该用户是否已存在
  357. int count = appUserInfoService.isExistAppUser(params);
  358. if(count > 0){
  359. appUserInfoService.updateAppUserInfo(appUserInfo);
  360. }else {
  361. appUserInfoService.AddAppUserInfo(appUserInfo);
  362. }
  363. resultData = ResultData.success(unionIdInfo);
  364. }
  365. }else {
  366. resultData = ResultData.error(CodeMsg.REQUEST_FAIL);
  367. }
  368. }else {
  369. resultData = ResultData.error(CodeMsg.REQUEST_FAIL);
  370. }
  371. return gson.toJson(resultData);
  372. }
  373. /*
  374. * 获取公众号信息
  375. * @param openId
  376. * @param stationId
  377. * @return
  378. */
  379. @RequestMapping(value = "/getGzhUserInfo", method = RequestMethod.GET)
  380. @ResponseBody
  381. public String getGzhUserInfo(@RequestParam String openId, Integer stationId){
  382. Gson gson =new Gson();
  383. //返回结果集
  384. ResultData resultData = null;
  385. if(openId !=null && stationId !=null){
  386. String gzhAppId ="";
  387. String gzhAppSecret ="";
  388. String accessToken ="";
  389. Map<String, String> m = stationService.getStationAppIdAndAppSecret(stationId);
  390. if(m !=null && m.containsKey("gzhAppId") && m.containsKey("gzhAppSecret")){
  391. gzhAppId = m.get("gzhAppId");
  392. gzhAppSecret = m.get("gzhAppSecret");
  393. accessToken = this.getTokenByRedisCache(gzhAppId, gzhAppSecret);
  394. net.sf.json.JSONObject unionIdInfo = null;
  395. unionIdInfo= WeiXinUserUtil.getUnionIdInfo(accessToken, openId);
  396. if(unionIdInfo.containsKey("errcode")){
  397. //删除原来key
  398. redisCacheUtil.deleteObject(gzhAppId);
  399. String token = WeiXinUserUtil.getToken(gzhAppId, gzhAppSecret).getAccessToken();
  400. redisCacheUtil.setCacheObject(gzhAppId, token);
  401. redisCacheUtil.expire(gzhAppId, 7200, TimeUnit.SECONDS);
  402. unionIdInfo= WeiXinUserUtil.getUnionIdInfo(token, openId);
  403. }
  404. resultData=ResultData.success(unionIdInfo);
  405. }
  406. }else {
  407. resultData=ResultData.error(CodeMsg.REQUEST_FAIL);
  408. }
  409. return gson.toJson(resultData);
  410. }
  411. /*
  412. * 从Redis缓存中获取 tokenCache
  413. * @param appId
  414. * @param appSecret
  415. * @return
  416. */
  417. public String getTokenByRedisCache(String appId, String appSecret){
  418. logger.info("appId参数: "+ appId+" ;appSecret参数: " +appSecret);
  419. if(!redisCacheUtil.hasKey(appId) || redisCacheUtil.getCacheObject(appId) ==null || redisCacheUtil.getCacheObject(appId).equals("")){
  420. String token = WeiXinUserUtil.getToken(appId, appSecret).getAccessToken();
  421. redisCacheUtil.setCacheObject(appId, token);
  422. redisCacheUtil.expire(appId, 7200, TimeUnit.SECONDS);
  423. logger.info("Redis缓存中token信息: " + token);
  424. return token;
  425. }else {
  426. return redisCacheUtil.getCacheObject(appId);
  427. }
  428. }
  429. /*
  430. * 添加用户信息
  431. * @param request
  432. * @return
  433. */
  434. @RequestMapping(value = "/addAppUserInfo", method = RequestMethod.POST)
  435. @ResponseBody
  436. @Transactional
  437. public String addAppUserInfo(@RequestBody AppUserInfoRequest request){
  438. Gson gson =new Gson();
  439. ResultData resultData=null;
  440. try{
  441. logger.info("前端传入参数:" +request.toString());
  442. AppUserInfo appUserInfo =new AppUserInfo();
  443. CustomerPoints customerPoints = new CustomerPoints();
  444. CustomerElectronicCard customerElectronicCard = new CustomerElectronicCard(); //客户电子卡
  445. String openId = request.getOpenId(); //此参数用于查询该用户是否已注册过
  446. if(StringUtils.isNotBlank(request.getBlogNickName())){
  447. appUserInfo.setBlogNickName(request.getBlogNickName());
  448. customerPoints.setCustomerName(request.getBlogNickName());
  449. customerElectronicCard.setCustomerName(request.getBlogNickName());
  450. }
  451. if(StringUtils.isNotBlank(request.getBlogProfilePhoto())){
  452. appUserInfo.setBlogProfilePhoto(request.getBlogProfilePhoto());
  453. }
  454. if(StringUtils.isNotBlank(request.getUnionId())){
  455. appUserInfo.setUnionId(request.getUnionId());
  456. customerPoints.setUnionId(request.getUnionId());
  457. customerElectronicCard.setUnionId(request.getUnionId());
  458. }
  459. appUserInfo.setStationId(request.getStationId());
  460. customerPoints.setCreateTime(new Date());
  461. customerPoints.setUnionId(request.getUnionId());
  462. customerPoints.setPoints(0);
  463. customerPoints.setConsumptionPoints(0);
  464. customerPoints.setAccumulatePoints(0);
  465. customerPoints.setInvalidPoints(0);
  466. customerPoints.setStationId(request.getStationId());
  467. customerPoints.setStationName(request.getStationName());
  468. customerElectronicCard.setStationId(request.getStationId());
  469. customerElectronicCard.setStationName(request.getStationName());
  470. if(StringUtils.isNotBlank(request.getSexFlag())){
  471. appUserInfo.setSexFlag(request.getSexFlag());
  472. }
  473. if(StringUtils.isNotBlank(request.getMobilePhone())){
  474. appUserInfo.setMobilePhone(request.getMobilePhone());
  475. customerPoints.setMobilePhone(request.getMobilePhone());
  476. customerElectronicCard.setMobilePhone(request.getMobilePhone());
  477. }
  478. if(request.getUserType()!=null){
  479. String userType = request.getUserType();
  480. appUserInfo.setUserType(userType);
  481. switch (userType){
  482. case "1": //当为微信用户时
  483. AppUserInfo wxInfo = new AppUserInfo();
  484. wxInfo.setStationId(request.getStationId());
  485. //判断当前登录是否存在手机号
  486. if(StringUtils.isNotBlank(request.getMobilePhone())){
  487. appUserInfo.setMinaOpenid(openId);
  488. wxInfo.setMobilePhone(request.getMobilePhone());
  489. AppUserInfo wxInfoResult = appUserInfoService.getAppUserInfo(wxInfo);
  490. if(wxInfoResult !=null){
  491. //如果不为空,则说明存在小程序用户信息;
  492. appUserInfo.setUpdateDate(new Date());
  493. appUserInfoService.updateAppUserInfo(appUserInfo);
  494. }else {
  495. //否则,根据unionID查询用户信息
  496. wxInfo.setUnionId(request.getUnionId());
  497. wxInfo.setMobilePhone(null);
  498. AppUserInfo appUserInfoByUnionID = appUserInfoService.getAppUserInfo(wxInfo);
  499. if(appUserInfoByUnionID !=null){
  500. //如果不为空,则说明存在公众号用户信息
  501. appUserInfo.setUpdateDate(new Date());
  502. appUserInfoService.updateAppUserInfo(appUserInfo);
  503. }else {
  504. //否则,新增用户信息
  505. appUserInfo.setRegisteDate(new Date());
  506. appUserInfoService.AddAppUserInfo(appUserInfo);
  507. }
  508. }
  509. }else {
  510. appUserInfo.setBlogOpenid(openId);
  511. //如果没有手机号,则说明是用户公众号登录
  512. wxInfo.setUnionId(request.getUnionId());
  513. AppUserInfo appUserInfoNoPhone = appUserInfoService.getAppUserInfo(wxInfo);
  514. if(appUserInfoNoPhone !=null){
  515. appUserInfo.setUpdateDate(new Date());
  516. appUserInfoService.updateAppUserInfo(appUserInfo);
  517. }else {
  518. appUserInfo.setRegisteDate(new Date());
  519. appUserInfoService.AddAppUserInfo(appUserInfo);
  520. }
  521. }
  522. break;
  523. case "2":
  524. AppUserInfo aLiInfo =new AppUserInfo();
  525. aLiInfo.setStationId(request.getStationId());
  526. if(StringUtils.isNotBlank(request.getMobilePhone())){
  527. //如果手机号不为空,则说明是支付宝小程序登录
  528. aLiInfo.setMobilePhone(request.getMobilePhone());
  529. AppUserInfo appUserInfos = appUserInfoService.getAppUserInfo(aLiInfo);
  530. if(appUserInfos !=null){
  531. //如果不为空,则说明用户存在小程序用户信息
  532. appUserInfo.setUpdateDate(new Date());
  533. appUserInfoService.updateAppUserInfo(appUserInfo);
  534. //删除用户支付宝的公众号信息
  535. //appUserInfoService.delAliInfo();
  536. }else {
  537. //如果为空
  538. aLiInfo.setALiId(request.getALiId());
  539. AppUserInfo appUserInfoByAliId = appUserInfoService.getAppUserInfo(aLiInfo);
  540. if(appUserInfoByAliId !=null){
  541. //如果不为空则说明存在支付宝公众号
  542. appUserInfo.setUpdateDate(new Date());
  543. appUserInfoService.updateAppUserInfo(appUserInfo);
  544. }else {
  545. appUserInfo.setRegisteDate(new Date());
  546. appUserInfoService.AddAppUserInfo(appUserInfo);
  547. }
  548. }
  549. }else {
  550. //如果手机号为空,则说明是公众号登录
  551. aLiInfo.setALiId(request.getALiId());
  552. AppUserInfo appUserInfoNoPhone = appUserInfoService.getAppUserInfo(aLiInfo);
  553. if(appUserInfoNoPhone !=null){
  554. //说明存在支付宝小程序用户信息
  555. appUserInfo.setUpdateDate(new Date());
  556. appUserInfoService.updateAppUserInfo(appUserInfo);
  557. }else {
  558. //不存在
  559. appUserInfo.setRegisteDate(new Date());
  560. appUserInfoService.AddAppUserInfo(appUserInfo);
  561. }
  562. }
  563. break;
  564. }
  565. }
  566. StationInfo stationInfo = new StationInfo();
  567. stationInfo.setStationId(request.getStationId());
  568. //获取油站功能信息
  569. StationInfo stationInfos = stationService.selectStationInfo(stationInfo);
  570. if(stationInfos !=null && stationInfos.getStationName() !=null){
  571. customerPoints.setStationName(stationInfos.getStationName());
  572. customerElectronicCard.setStationName(stationInfos.getStationName());
  573. }
  574. //是否开起积分功能
  575. if(stationInfos !=null && stationInfos.getIntegralFlag().equals("1")){
  576. this.insertCustomerPointsInfo(customerPoints, request);
  577. }
  578. //是否开起电子卡功能
  579. if(stationInfos !=null && stationInfos.getCardEnabledFlag().equals("1")){
  580. this.insertCustomerElectronicCard(customerElectronicCard, request, stationInfos);
  581. }
  582. resultData = ResultData.success(CodeMsg.SUCCESS);
  583. }catch (Exception e){
  584. resultData = ResultData.error(CodeMsg.INSERT_FAIL);
  585. e.printStackTrace();
  586. }
  587. return gson.toJson(resultData);
  588. }
  589. //客户积分表信息操作
  590. public void insertCustomerPointsInfo(CustomerPoints customerPoints, AppUserInfoRequest request){
  591. CustomerPoints customerPointsInfo = customerPointsService.getCustomerPointsInfo(customerPoints);
  592. if(customerPointsInfo !=null){
  593. CustomerPoints c = new CustomerPoints();
  594. c.setUnionId(customerPoints.getUnionId());
  595. c.setStationId(request.getStationId());
  596. c.setMobilePhone(request.getMobilePhone());
  597. c.setStationId(request.getStationId());
  598. customerPointsService.updateCustomerPointsInfo(c);
  599. }else {
  600. customerPointsService.insertCustomerPointsInfo(customerPoints);
  601. }
  602. }
  603. //客户电子会员卡表操作
  604. public void insertCustomerElectronicCard(CustomerElectronicCard customerElectronicCard, AppUserInfoRequest request, StationInfo stationInfos){
  605. CustomerCardSetting customerCardSetting = new CustomerCardSetting();
  606. customerCardSetting.setStationId(request.getStationId());
  607. if(stationInfos.getCardRuleFlag().equals("1")){
  608. //表示共享则使用集团ID为查询条件
  609. customerCardSetting.setStationId(stationInfos.getGroupId());
  610. }
  611. List<CustomerCardSetting> cardRechargeSettingList = electronicMembershipCardService.getCardRechargeSettingList(customerCardSetting);
  612. if(cardRechargeSettingList !=null && cardRechargeSettingList.size() >0){
  613. String cardOilsTypeStr = cardRechargeSettingList.get(0).getCardOilsType();
  614. String[] split = cardOilsTypeStr.split(",");
  615. for(int i=0; i<split.length; i++){
  616. customerElectronicCard.setCardOilsType(String.valueOf(split[i])); //卡的类型
  617. List<CustomerElectronicCard> electronicCardList = electronicMembershipCardService.getElectronicCardList(customerElectronicCard);
  618. if(electronicCardList !=null && electronicCardList.size() ==1){
  619. electronicMembershipCardService.updateElectronicCardInfoByUnionId(customerElectronicCard);
  620. }else {
  621. String currentTime = String.valueOf(System.currentTimeMillis());
  622. String customerNo = request.getStationId() + currentTime;
  623. customerElectronicCard.setCustomerNo(customerNo);
  624. customerElectronicCard.setAmt(new BigDecimal(0));
  625. customerElectronicCard.setCreateTime(new Date());
  626. electronicMembershipCardService.insetElectronicCard(customerElectronicCard);
  627. }
  628. }
  629. }
  630. }
  631. }