AppUserInfoController.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package com.platform.yijia.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.google.gson.Gson;
  4. import com.platform.yijia.param.request.AppUserInfoRequest;
  5. import com.platform.yijia.pojo.AppUserInfo;
  6. import com.platform.yijia.pojo.CustomerManage;
  7. import com.platform.yijia.pojo.CustomerPoints;
  8. import com.platform.yijia.service.AppUserInfoService;
  9. import com.platform.yijia.service.CustomerPointsService;
  10. import com.platform.yijia.service.PayOrderService;
  11. import com.platform.yijia.service.StationService;
  12. import com.platform.yijia.utils.CodeMsg;
  13. import com.platform.yijia.utils.ResultData;
  14. import com.platform.yijia.utils.redis.RedisCacheUtil;
  15. import com.platform.yijia.utils.weixinapp.AesDecryptUtil;
  16. import com.platform.yijia.utils.weixinapp.WeiXinUserUtil;
  17. import org.apache.commons.lang3.StringUtils;
  18. import org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Controller;
  22. import org.springframework.web.bind.annotation.*;
  23. import javax.annotation.Resource;
  24. import java.util.Date;
  25. import java.util.HashMap;
  26. import java.util.Map;
  27. @Controller
  28. @RequestMapping("/api")
  29. public class AppUserInfoController {
  30. private static Logger logger =(Logger) LoggerFactory.getLogger(AppUserInfoController.class);
  31. @Autowired
  32. private PayOrderService payOrderService;
  33. @Autowired
  34. private AppUserInfoService appUserInfoService;
  35. @Resource
  36. private StationService stationService;
  37. @Resource
  38. private CustomerPointsService customerPointsService;
  39. @Resource
  40. private RedisCacheUtil redisCacheUtil;
  41. @Resource
  42. private WeiXinUserUtil weiXinUserUtil;
  43. // String appId = "wxe1135cd390b38a54"; //微信小程序appID
  44. // String appSecret = "0532c7d9ae876c4ad636df0b1e3b9ddb"; //微信小程序密钥
  45. /*
  46. * 微信小程序根据code获取用户openId和sessionKey
  47. * @param code
  48. * @param stationId
  49. * @return
  50. */
  51. @ResponseBody
  52. @RequestMapping(value = "/getSessionKeyAndOpenID")
  53. public Map<String, Object> getSessionKeyAndOpenID(@RequestParam("code") String code, @RequestParam("stationId") Integer stationId){
  54. logger.info("微信小程序获取用户openId和sessionKey入参code和stationId: "+code +"-----" +stationId);
  55. String appId = ""; //微信小程序appID
  56. String appSecret = ""; //微信小程序密钥
  57. if(stationId !=null){
  58. Map<String, String> map = stationService.getStationAppIdAndAppSecret(stationId);
  59. appId = map.get("appId"); //微信小程序appID
  60. appSecret = map.get("appSecret"); //微信小程序密钥
  61. }
  62. //获取session_key 和 openid
  63. return weiXinUserUtil.getUserSessionKeyAndOpenID(appId, appSecret, code);
  64. }
  65. /*
  66. * 解密 encryptedData
  67. * @param sessionKey
  68. * @param encryptedData
  69. * @param iv
  70. * @return
  71. */
  72. @ResponseBody
  73. @RequestMapping(value = "/decryptEncryptedData", method = RequestMethod.POST, consumes = "application/json")
  74. public String decryptEncryptedData(@RequestBody JSONObject jsonObject){
  75. logger.info("解密获取手机号前台传入参数:" +jsonObject.toString());
  76. return AesDecryptUtil.decryptEncryptedData(jsonObject.get("encryptedData").toString(), jsonObject.get("sessionKey").toString(), jsonObject.get("iv").toString(), "UTF-8").toString();
  77. }
  78. //获取UnionId信息
  79. @RequestMapping(value = "/getUnionIdAndAccessToken", method = RequestMethod.GET)
  80. @ResponseBody
  81. public String redisCacheUtil(@RequestParam String openId, Integer stationId){
  82. Gson gson =new Gson();
  83. //返回结果集
  84. ResultData resultData = null;
  85. Map<String, String> map= new HashMap<>();
  86. if(openId !=null && stationId !=null){
  87. String gzhAppId ="";
  88. String gzhAppSecret ="";
  89. String accessToken ="";
  90. Map<String, String> m = stationService.getStationAppIdAndAppSecret(stationId);
  91. if(m !=null && m.containsKey("gzhAppId") && m.containsKey("gzhAppSecret")){
  92. gzhAppId = m.get("gzhAppId");
  93. gzhAppSecret = m.get("gzhAppSecret");
  94. accessToken = weiXinUserUtil.getTokenByRedisCache(gzhAppId, gzhAppSecret);
  95. net.sf.json.JSONObject unionIdInfo = weiXinUserUtil.getUnionIdInfo(accessToken, openId);
  96. if(unionIdInfo !=null && unionIdInfo.containsKey("nickname")){
  97. map.put("nickName", unionIdInfo.get("nickname").toString());
  98. }
  99. if(unionIdInfo !=null && unionIdInfo.containsKey("openid")){
  100. map.put("openId", unionIdInfo.get("openid").toString());
  101. }
  102. if(unionIdInfo !=null && unionIdInfo.containsKey("unionid")){
  103. map.put("unoinId", unionIdInfo.get("unionid").toString());
  104. }
  105. // if(unionIdInfo !=null && unionIdInfo.containsKey("headimgurl")){
  106. // map.put("headImgUrl", unionIdInfo.get("headimgurl").toString());
  107. // }
  108. map.put("accessToken", accessToken);
  109. resultData=ResultData.success(map);
  110. }
  111. }else {
  112. resultData=ResultData.success(CodeMsg.REQUEST_FAIL);
  113. }
  114. return gson.toJson(resultData);
  115. }
  116. /*
  117. * 添加用户信息
  118. * @param request
  119. * @return
  120. */
  121. @RequestMapping(value = "/addAppUserInfo", method = RequestMethod.POST)
  122. @ResponseBody
  123. public String addAppUserInfo(@RequestBody AppUserInfoRequest request){
  124. Gson gson =new Gson();
  125. ResultData resultData=null;
  126. try{
  127. AppUserInfo appUserInfo =new AppUserInfo();
  128. //CustomerPoints customerPoints = new CustomerPoints();
  129. String openId = ""; //此参数用于查询该用户是否已注册过
  130. if(request.getUserType()!=null){
  131. //1-微信公众号
  132. if(request.getUserType().equals("1") &&StringUtils.isNotBlank(request.getBlogOpenid())){
  133. appUserInfo.setUserType(request.getUserType());
  134. appUserInfo.setBlogToken(request.getBlogToken());
  135. openId = request.getBlogOpenid();
  136. appUserInfo.setBlogOpenid(openId);
  137. //customerPoints.setBlogOpenId(openId);
  138. //2-微信小程序
  139. }else if(request.getUserType().equals("2") &&StringUtils.isNotBlank(request.getMinaOpenid())){
  140. appUserInfo.setUserType(request.getUserType());
  141. appUserInfo.setMinaToken(request.getMinaToken());
  142. openId = request.getMinaOpenid();
  143. appUserInfo.setMinaOpenid(openId);
  144. //customerPoints.setMinaOpenId(openId);
  145. }
  146. }
  147. if(StringUtils.isNotBlank(request.getBlogNickName())){
  148. appUserInfo.setBlogNickName(request.getBlogNickName());
  149. //customerPoints.setCustomerName(request.getBlogNickName());
  150. }
  151. if(StringUtils.isNotBlank(request.getMobilePhone())){
  152. appUserInfo.setMobilePhone(request.getMobilePhone());
  153. //customerPoints.setMobilePhone(request.getMobilePhone());
  154. }
  155. if(StringUtils.isNotBlank(request.getBlogProfilePhoto())){
  156. appUserInfo.setBlogProfilePhoto(request.getBlogProfilePhoto());
  157. }
  158. if(StringUtils.isNotBlank(request.getUnionId())){
  159. appUserInfo.setUnionId(request.getUnionId());
  160. //customerPoints.setUnionId(request.getUnionId());
  161. }
  162. appUserInfo.setRegisteDate(new Date());
  163. appUserInfo.setUpdateDate(new Date());
  164. //customerPoints.setCreateTime(new Date());
  165. // customerPoints.setUnionId(request.getUnionId());
  166. // customerPoints.setPoints(0);
  167. // customerPoints.setConsumptionPoints(0);
  168. // customerPoints.setAccumulatePoints(0);
  169. // customerPoints.setInvalidPoints(0);
  170. if(StringUtils.isNotBlank(request.getSexFlag())){
  171. appUserInfo.setSexFlag(request.getSexFlag());
  172. }
  173. Map<String, Object> params = new HashMap<>();
  174. params.put("openId", openId); //注册用户openId
  175. params.put("userType", request.getUserType()); //用户类型
  176. //判断该用户是否已存在
  177. int count = appUserInfoService.isExistAppUser(params);
  178. if(count > 0){
  179. // boolean boo = customerPointsService.isExistCustomerPointsInfo(customerPoints);
  180. // if(!boo){
  181. // customerPointsService.insertCustomerPointsInfo(customerPoints);
  182. // }
  183. // if(appUserInfoService.getUserUnionId(appUserInfo)==null){
  184. // appUserInfoService.updateUnionId(appUserInfo);
  185. // }
  186. resultData = ResultData.error(CodeMsg.USER_EXSIST);
  187. }else {
  188. appUserInfoService.AddAppUserInfo(appUserInfo);
  189. // customerPointsService.insertCustomerPointsInfo(customerPoints);
  190. resultData = ResultData.success(CodeMsg.SUCCESS);
  191. }
  192. }catch (Exception e){
  193. resultData = ResultData.error(CodeMsg.INSERT_FAIL);
  194. e.printStackTrace();
  195. }
  196. return gson.toJson(resultData);
  197. }
  198. }