AppUserInfoController.java 38 KB

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