AppUserInfoController.java 30 KB

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