123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- package com.platform.yijia.controller;
- import com.alibaba.fastjson.JSONObject;
- import com.google.gson.Gson;
- import com.platform.yijia.param.request.AppUserInfoRequest;
- import com.platform.yijia.pojo.AppUserInfo;
- import com.platform.yijia.pojo.CustomerManage;
- import com.platform.yijia.pojo.CustomerPoints;
- import com.platform.yijia.service.AppUserInfoService;
- import com.platform.yijia.service.CustomerPointsService;
- import com.platform.yijia.service.PayOrderService;
- import com.platform.yijia.service.StationService;
- import com.platform.yijia.utils.CodeMsg;
- import com.platform.yijia.utils.ResultData;
- import com.platform.yijia.utils.redis.RedisCacheUtil;
- import com.platform.yijia.utils.weixinapp.AesDecryptUtil;
- import com.platform.yijia.utils.weixinapp.WeiXinUserUtil;
- import org.apache.commons.lang3.StringUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.*;
- import javax.annotation.Resource;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
- @Controller
- @RequestMapping("/api")
- public class AppUserInfoController {
- private static Logger logger =(Logger) LoggerFactory.getLogger(AppUserInfoController.class);
- @Autowired
- private PayOrderService payOrderService;
- @Autowired
- private AppUserInfoService appUserInfoService;
- @Resource
- private StationService stationService;
- @Resource
- private CustomerPointsService customerPointsService;
- @Resource
- private RedisCacheUtil redisCacheUtil;
- @Resource
- private WeiXinUserUtil weiXinUserUtil;
- // String appId = "wxe1135cd390b38a54"; //微信小程序appID
- // String appSecret = "0532c7d9ae876c4ad636df0b1e3b9ddb"; //微信小程序密钥
- /*
- * 微信小程序根据code获取用户openId和sessionKey
- * @param code
- * @param stationId
- * @return
- */
- @ResponseBody
- @RequestMapping(value = "/getSessionKeyAndOpenID")
- public Map<String, Object> getSessionKeyAndOpenID(@RequestParam("code") String code, @RequestParam("stationId") Integer stationId){
- logger.info("微信小程序获取用户openId和sessionKey入参code和stationId: "+code +"-----" +stationId);
- String appId = ""; //微信小程序appID
- String appSecret = ""; //微信小程序密钥
- if(stationId !=null){
- Map<String, String> map = stationService.getStationAppIdAndAppSecret(stationId);
- appId = map.get("appId"); //微信小程序appID
- appSecret = map.get("appSecret"); //微信小程序密钥
- }
- //获取session_key 和 openid
- return weiXinUserUtil.getUserSessionKeyAndOpenID(appId, appSecret, code);
- }
- /*
- * 解密 encryptedData
- * @param sessionKey
- * @param encryptedData
- * @param iv
- * @return
- */
- @ResponseBody
- @RequestMapping(value = "/decryptEncryptedData", method = RequestMethod.POST, consumes = "application/json")
- public String decryptEncryptedData(@RequestBody JSONObject jsonObject){
- logger.info("解密获取手机号前台传入参数:" +jsonObject.toString());
- return AesDecryptUtil.decryptEncryptedData(jsonObject.get("encryptedData").toString(), jsonObject.get("sessionKey").toString(), jsonObject.get("iv").toString(), "UTF-8").toString();
- }
- //获取UnionId信息
- @RequestMapping(value = "/getUnionIdAndAccessToken", method = RequestMethod.GET)
- @ResponseBody
- public String redisCacheUtil(@RequestParam String openId, Integer stationId){
- Gson gson =new Gson();
- //返回结果集
- ResultData resultData = null;
- Map<String, String> map= new HashMap<>();
- if(openId !=null && stationId !=null){
- String gzhAppId ="";
- String gzhAppSecret ="";
- String accessToken ="";
- Map<String, String> m = stationService.getStationAppIdAndAppSecret(stationId);
- if(m !=null && m.containsKey("gzhAppId") && m.containsKey("gzhAppSecret")){
- gzhAppId = m.get("gzhAppId");
- gzhAppSecret = m.get("gzhAppSecret");
- accessToken = weiXinUserUtil.getTokenByRedisCache(gzhAppId, gzhAppSecret);
- net.sf.json.JSONObject unionIdInfo = weiXinUserUtil.getUnionIdInfo(accessToken, openId);
- if(unionIdInfo !=null && unionIdInfo.containsKey("nickname")){
- map.put("nickName", unionIdInfo.get("nickname").toString());
- }
- if(unionIdInfo !=null && unionIdInfo.containsKey("openid")){
- map.put("openId", unionIdInfo.get("openid").toString());
- }
- if(unionIdInfo !=null && unionIdInfo.containsKey("unionid")){
- map.put("unoinId", unionIdInfo.get("unionid").toString());
- }
- // if(unionIdInfo !=null && unionIdInfo.containsKey("headimgurl")){
- // map.put("headImgUrl", unionIdInfo.get("headimgurl").toString());
- // }
- map.put("accessToken", accessToken);
- resultData=ResultData.success(map);
- }
- }else {
- resultData=ResultData.success(CodeMsg.REQUEST_FAIL);
- }
- return gson.toJson(resultData);
- }
- /*
- * 添加用户信息
- * @param request
- * @return
- */
- @RequestMapping(value = "/addAppUserInfo", method = RequestMethod.POST)
- @ResponseBody
- public String addAppUserInfo(@RequestBody AppUserInfoRequest request){
- Gson gson =new Gson();
- ResultData resultData=null;
- try{
- AppUserInfo appUserInfo =new AppUserInfo();
- //CustomerPoints customerPoints = new CustomerPoints();
- String openId = ""; //此参数用于查询该用户是否已注册过
- if(request.getUserType()!=null){
- //1-微信公众号
- if(request.getUserType().equals("1") &&StringUtils.isNotBlank(request.getBlogOpenid())){
- appUserInfo.setUserType(request.getUserType());
- appUserInfo.setBlogToken(request.getBlogToken());
- openId = request.getBlogOpenid();
- appUserInfo.setBlogOpenid(openId);
- //customerPoints.setBlogOpenId(openId);
- //2-微信小程序
- }else if(request.getUserType().equals("2") &&StringUtils.isNotBlank(request.getMinaOpenid())){
- appUserInfo.setUserType(request.getUserType());
- appUserInfo.setMinaToken(request.getMinaToken());
- openId = request.getMinaOpenid();
- appUserInfo.setMinaOpenid(openId);
- //customerPoints.setMinaOpenId(openId);
- }
- }
- if(StringUtils.isNotBlank(request.getBlogNickName())){
- appUserInfo.setBlogNickName(request.getBlogNickName());
- //customerPoints.setCustomerName(request.getBlogNickName());
- }
- if(StringUtils.isNotBlank(request.getMobilePhone())){
- appUserInfo.setMobilePhone(request.getMobilePhone());
- //customerPoints.setMobilePhone(request.getMobilePhone());
- }
- if(StringUtils.isNotBlank(request.getBlogProfilePhoto())){
- appUserInfo.setBlogProfilePhoto(request.getBlogProfilePhoto());
- }
- if(StringUtils.isNotBlank(request.getUnionId())){
- appUserInfo.setUnionId(request.getUnionId());
- //customerPoints.setUnionId(request.getUnionId());
- }
- appUserInfo.setRegisteDate(new Date());
- appUserInfo.setUpdateDate(new Date());
- //customerPoints.setCreateTime(new Date());
- // customerPoints.setUnionId(request.getUnionId());
- // customerPoints.setPoints(0);
- // customerPoints.setConsumptionPoints(0);
- // customerPoints.setAccumulatePoints(0);
- // customerPoints.setInvalidPoints(0);
- if(StringUtils.isNotBlank(request.getSexFlag())){
- appUserInfo.setSexFlag(request.getSexFlag());
- }
- Map<String, Object> params = new HashMap<>();
- params.put("openId", openId); //注册用户openId
- params.put("userType", request.getUserType()); //用户类型
- //判断该用户是否已存在
- int count = appUserInfoService.isExistAppUser(params);
- if(count > 0){
- // boolean boo = customerPointsService.isExistCustomerPointsInfo(customerPoints);
- // if(!boo){
- // customerPointsService.insertCustomerPointsInfo(customerPoints);
- // }
- // if(appUserInfoService.getUserUnionId(appUserInfo)==null){
- // appUserInfoService.updateUnionId(appUserInfo);
- // }
- resultData = ResultData.error(CodeMsg.USER_EXSIST);
- }else {
- appUserInfoService.AddAppUserInfo(appUserInfo);
- // customerPointsService.insertCustomerPointsInfo(customerPoints);
- resultData = ResultData.success(CodeMsg.SUCCESS);
- }
- }catch (Exception e){
- resultData = ResultData.error(CodeMsg.INSERT_FAIL);
- e.printStackTrace();
- }
- return gson.toJson(resultData);
- }
- }
|