后台服务
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

239 行
9.0 KiB

  1. package com.iformall.controller;
  2. import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.google.code.kaptcha.Producer;
  5. import com.iformall.annotation.AuthIgnore;
  6. import com.iformall.annotation.TenantIgnore;
  7. import com.iformall.common.ErrorCode;
  8. import com.iformall.common.Result;
  9. import com.iformall.common.ResultData;
  10. import com.iformall.domain.po.*;
  11. import com.iformall.domain.po.sm.InviteCodeInfo;
  12. import com.iformall.enums.*;
  13. import com.iformall.exception.MallinkException;
  14. import com.iformall.service.*;
  15. import com.iformall.service.cuser.CUserServiceFactory;
  16. import com.iformall.service.sm.InviteCodeInfoService;
  17. import com.iformall.service.sm.InviteCodeService;
  18. import com.iformall.service.sm.UserConsumptionPackageService;
  19. import com.iformall.service.sm.UserCreateVideoNumService;
  20. import com.iformall.utils.Constant;
  21. import com.iformall.utils.IPUtil;
  22. import com.iformall.utils.PasswordHelper;
  23. import com.iformall.utils.RedisCacheUtils;
  24. import io.swagger.annotations.Api;
  25. import io.swagger.annotations.ApiImplicitParam;
  26. import io.swagger.annotations.ApiImplicitParams;
  27. import io.swagger.annotations.ApiOperation;
  28. import org.apache.commons.io.IOUtils;
  29. import org.apache.commons.lang3.StringUtils;
  30. import org.slf4j.Logger;
  31. import org.slf4j.LoggerFactory;
  32. import org.springframework.beans.factory.annotation.Autowired;
  33. import org.springframework.beans.factory.annotation.Qualifier;
  34. import org.springframework.data.redis.core.RedisTemplate;
  35. import org.springframework.web.bind.annotation.*;
  36. import org.springframework.web.context.request.RequestContextHolder;
  37. import org.springframework.web.context.request.ServletRequestAttributes;
  38. import javax.imageio.ImageIO;
  39. import javax.servlet.ServletOutputStream;
  40. import javax.servlet.http.HttpServletRequest;
  41. import javax.servlet.http.HttpServletResponse;
  42. import java.awt.image.BufferedImage;
  43. import java.io.IOException;
  44. import java.util.HashMap;
  45. import java.util.List;
  46. import java.util.Map;
  47. @RestController
  48. @RequestMapping("/api/miniApp")
  49. @Api(description = "用户登陆相关接口")
  50. public class MiniAppUserController extends BaseController {
  51. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  52. @Autowired
  53. private CUserServiceFactory cuserFactory;
  54. @Autowired
  55. @Qualifier("objectCommonRedisTemplate")
  56. RedisTemplate<String, Object> redisTemplate;
  57. @Autowired
  58. private WxCUserBasicInfoService wxCUserBasicInfoService;
  59. @Autowired
  60. WxAppinfoService wxAppinfoService;
  61. /**
  62. * 用户登录
  63. * 小程序登陆
  64. *
  65. * @param map
  66. * @return
  67. */
  68. @AuthIgnore
  69. @TenantIgnore
  70. @PostMapping("/login")
  71. @ApiOperation(value = "用户登录", notes = "{\"appId\":\"string\",\"code\":\"string\",\"scene\":\"string\",\"sceneAddress\":\"string\",\"latitude\":\"string\",\"longitude\":\"string\",\"systemInfo\":\"string\"}")
  72. public ResultData userLogin(@RequestBody Map<String, String> map) {
  73. logger.info("dologin >>>>>>>>>>>>>>>>>"+map.toString());
  74. Map resultMap = new HashMap();
  75. String appId = map.get("appId");
  76. String code = map.get("code");
  77. //登录凭证不能为空
  78. if (StringUtils.isBlank(appId)) {
  79. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "appId不能为空");
  80. }
  81. if (StringUtils.isBlank(code)) {
  82. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "code不能为空");
  83. }
  84. WxAppinfo wxAppinfo = wxAppinfoService.getOnlyByAppIdFromRedis(appId);
  85. if(wxAppinfo == null){
  86. return new ResultData(ErrorCode.APP_ID_NOT_FOUND);
  87. }
  88. if(!wxAppinfo.getEnable().equals(EnumEnableType.Enable.getCode())){
  89. return new ResultData(ErrorCode.APP_ID_NOT_ENABLE);
  90. }
  91. EnumAppPlat appPlat = EnumAppPlat.getByCode(wxAppinfo.getPlat());
  92. if(appPlat == null){
  93. return new ResultData(ErrorCode.APP_PLAT_ERROR);
  94. }
  95. String session_key = null;
  96. String openId = null;
  97. String unionId = null;
  98. try {
  99. WxMaJscode2SessionResult session = cuserFactory.getCUserService(appPlat).jsCode2SessionInfo(code, wxAppinfo, false);
  100. logger.info("sessionResult: " + session);
  101. if (null != session) {
  102. logger.info("sessionResultDetail: [openid]" + session.getOpenid()+",[sessionkey]"+session.getSessionKey()+",[unionid]"+session.getUnionid());
  103. }
  104. //获取会话密钥(session_key)
  105. session_key = session.getSessionKey();
  106. openId = session.getOpenid();
  107. unionId = session.getUnionid();
  108. logger.info("session_key: " + session_key + ", openId: " + openId + ", unionId: " + unionId);
  109. resultMap.put("openId", openId);
  110. } catch (Exception e) {
  111. logger.error(e.getMessage(), e);
  112. return new ResultData(ErrorCode.SESSION_KEY_DECODE_ERR);
  113. }
  114. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  115. // request.setAttribute(Constant.TENANT_ID, wxMall.getTenantId());
  116. // request.setAttribute(Constant.PARENT_TENANT_ID, wxMall.getParentTenantId());
  117. String ipaddress = IPUtil.getIpAddr(request);
  118. CUser cUser = new CUser();
  119. cUser.setAppId(appId);
  120. cUser.setOpenId(openId);
  121. cUser.setUnionId(unionId);
  122. cUser.setRegisterIp(ipaddress);
  123. cUser.setSessionKey(session_key);
  124. //处理登陆用户
  125. cUser = cuserFactory.getCUserService(appPlat).handleLoginUser(cUser);
  126. if(cUser.getUserId() != null){
  127. WxCUserBasicInfo basicInfo = wxCUserBasicInfoService.getById(cUser.getUserId(), getTenantInfo().getTenantId());
  128. if(basicInfo != null){
  129. wxCUserBasicInfoService.handleLoginUser(basicInfo);
  130. resultMap.put("token", basicInfo.getToken());
  131. }
  132. }
  133. return new ResultData(resultMap);
  134. }
  135. /**
  136. * 授权登陆
  137. *
  138. * @param map
  139. * @return
  140. */
  141. @AuthIgnore
  142. @PostMapping("/loginPhone")
  143. @ApiOperation(value = "授权后获取用户的手机号", notes = "{\"encryptedData\":\"string\",\"iv\":\"string\"}")
  144. public ResultData getUserPhone(@RequestBody Map<String, String> map) {
  145. logger.info(map.toString());
  146. String appId = map.get("appId");
  147. String openId = map.get("openId");
  148. String encryptedData = map.get("encryptedData");
  149. String iv = map.get("iv");
  150. if(StringUtils.isBlank(appId)){
  151. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "appId 不能为空");
  152. }
  153. if(StringUtils.isBlank(openId)){
  154. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "openId 不能为空");
  155. }
  156. WxAppinfo wxAppinfo = wxAppinfoService.getOnlyByAppIdFromRedis(appId);
  157. if(wxAppinfo == null){
  158. return new ResultData(ErrorCode.APP_ID_NOT_FOUND);
  159. }
  160. if(!wxAppinfo.getEnable().equals(EnumEnableType.Enable.getCode())){
  161. return new ResultData(ErrorCode.APP_ID_NOT_ENABLE);
  162. }
  163. EnumAppPlat appPlat = EnumAppPlat.getByCode(wxAppinfo.getPlat());
  164. if(appPlat == null){
  165. return new ResultData(ErrorCode.APP_PLAT_ERROR);
  166. }
  167. //登录凭证不能为空
  168. if (StringUtils.isBlank(encryptedData)) {
  169. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "encryptedData 不能为空");
  170. }
  171. if (StringUtils.isBlank(iv)) {
  172. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "iv 不能为空");
  173. }
  174. String uid = map.get("uid");
  175. UserBasicFrom from = new UserBasicFrom();
  176. from.setPlat(appPlat.getCode());
  177. from.setFromProject(EnumProject.PROJECT_5.getCode());
  178. if(StringUtils.isNotBlank(uid)){
  179. try {
  180. from.setFromUserId(Long.parseLong(uid));
  181. from.setFromType(EnumUserBasicFrom.FROM_3.getCode());
  182. }catch(Exception e){}
  183. }
  184. Map resultMap = new HashMap();
  185. try {
  186. CUser user = cuserFactory.getCUserService(appPlat).getByOpenId(openId, getTenantInfo().getTenantId());
  187. if (user == null) {
  188. return new ResultData(ErrorCode.USER_IS_EMPTY);
  189. }
  190. // logger.info("ceshi "+user.getAppPlat()+"======"+user.getClass());
  191. // 解密 并注册
  192. WxCUserBasicInfo basicInfo = cuserFactory.getCUserService(appPlat).decryptPhoneNoInfo(encryptedData, iv, user, wxAppinfo, false,from);
  193. wxCUserBasicInfoService.handleLoginUser(basicInfo);
  194. resultMap.put("token", basicInfo.getToken());
  195. } catch (MallinkException e) {
  196. return new ResultData(e.getErrorCode(), e.getMessage());
  197. }catch (Exception e) {
  198. this.logger.error(e.getMessage(), e);
  199. return new ResultData(ErrorCode.DB_FAIL.getCode(), "解密并保存出错", resultMap);
  200. }
  201. return new ResultData(resultMap);
  202. }
  203. }