Bläddra i källkod

上传文件至 'service'

麦兜兜12138 6 år sedan
förälder
incheckning
39321a7092
1 ändrade filer med 539 tillägg och 0 borttagningar
  1. 539 0
      service/WxService.java

+ 539 - 0
service/WxService.java

@@ -0,0 +1,539 @@
+package com.boot.security.server.service;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+import java.util.UUID;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.mock.web.MockMultipartFile;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.boot.security.server.config.MyProps;
+import com.boot.security.server.config.TokenThread;
+import com.boot.security.server.dao.CusAuditInfoDao;
+import com.boot.security.server.dao.CusAuthInfoDao;
+import com.boot.security.server.dao.CusLoginInfoDao;
+import com.boot.security.server.dao.CusSelfInfoDao;
+import com.boot.security.server.dao.DictDao;
+import com.boot.security.server.dao.SmsVerifycodeDao;
+import com.boot.security.server.model.CusAuditInfo;
+import com.boot.security.server.model.CusAuthInfo;
+import com.boot.security.server.model.CusLoginInfo;
+import com.boot.security.server.model.CusSelfInfo;
+import com.boot.security.server.model.Dict;
+import com.boot.security.server.model.FileInfo;
+import com.boot.security.server.model.SmsVerifycode;
+import com.boot.security.server.utils.AesCbcUtil;
+import com.boot.security.server.utils.DateTimeUtil;
+import com.boot.security.server.utils.HttpUtil;
+import com.boot.security.server.utils.SMSUtil;
+import com.boot.security.server.utils.StrUtil;
+
+@Service("wxService")
+public class WxService {
+	
+	private static final Logger log = LoggerFactory.getLogger("adminLogger");
+	
+	@Autowired
+	private MyProps myProps;
+
+	@Autowired
+	private FileService fileService;
+	
+	@Autowired
+	private SmsVerifycodeDao smsVerifycodeDao;
+	
+	@Autowired
+	private CusLoginInfoDao cusLoginInfoDao;
+	
+	@Autowired
+	private CusAuthInfoDao cusAuthInfoDao;
+	
+	@Autowired
+	private CusSelfInfoDao cusSelfInfoDao;
+	
+	@Autowired
+	private CusAuditInfoDao cusAuditInfoDao;
+	
+	@Autowired
+	private DictDao dictDao;
+	
+	/**
+	  *  微信用户登录
+	 *@return  
+	 **/
+	public String wxLogin(Map<String,Object> paramMap) {
+		log.info("微信用户登录---------start-------");
+		Map<String,Object> resultMap = new HashMap<String,Object>();
+		String code = (String) paramMap.get("code");
+		String iv = (String) paramMap.get("iv");
+		String encryptedData = (String) paramMap.get("encryptedData");
+		if(StrUtil.isEmpty(code)) {
+			resultMap.put("data", "");
+			resultMap.put("code", "1");
+			resultMap.put("msg", "code不能为空!");
+			log.info("微信用户登录失败---------code为空-------");
+			return JSON.toJSONString(resultMap);
+		}
+		Map<String,Object> dataMap = new HashMap<String,Object>();
+		String token = "";
+		String session_key = "";
+		String openid = "";
+		String unionid = "";
+		
+		String appid = myProps.getWxAppid();
+		String secret = myProps.getWxSecret();
+		String auth = myProps.getWxAuth();
+		String url = myProps.getWxloginUrl();
+		log.info("微信登录url:"+url+"appid="+appid+"&secret="+secret+"&js_code="+code+"&grant_type="+auth );
+		String result = HttpUtil.sendGet(url+"appid="+appid+"&secret="+secret+"&js_code="+code+"&grant_type="+auth, "");
+		log.info("微信登录接口返回结果:"+result);
+		if("fail".equals(result)) {
+			resultMap.put("data", "");
+			resultMap.put("code", "1");
+			resultMap.put("msg", "fail");
+			log.info("微信用户登录失败---------fail-------");
+			return JSON.toJSONString(resultMap);
+		}else {
+			JSONObject reJo = JSON.parseObject(result);
+			if(StrUtil.isNotEmpty(reJo.getString("errcode"))) {
+				resultMap.put("data", "");
+				resultMap.put("code", "1");
+				resultMap.put("msg", reJo.getString("errcode"));
+				log.info("微信用户登录失败---------code失效-------");
+				return JSON.toJSONString(resultMap);
+			}else {
+				log.info("微信用户登录成功---------result:"+JSON.toJSONString(result));
+				token = UUID.randomUUID().toString().replaceAll("-", "");
+				session_key = reJo.getString("session_key");
+				openid = reJo.getString("openid");
+				unionid = reJo.getString("unionid");
+				// 查询登录信息表中是否有此openid
+				CusLoginInfo detailByOpenId = cusLoginInfoDao.getDetailByOpenId(openid);
+				// 存入微信用户登录信息表
+				CusLoginInfo cusLoginInfo = new CusLoginInfo();
+				if(detailByOpenId!=null) {
+					cusLoginInfo.setOpenId(openid);
+					cusLoginInfo.setToken(token);
+					cusLoginInfo.setSessionKey(session_key);
+					cusLoginInfo.setUnionId(unionid);
+					cusLoginInfo.setLastModTime(DateTimeUtil.getCurrentDateTime());
+					log.info("微信用户登录成功---------token更新-------");
+					cusLoginInfoDao.updateByOpenId(cusLoginInfo);
+				}else {
+					cusLoginInfo.setOpenId(openid);
+					cusLoginInfo.setToken(token);
+					cusLoginInfo.setSessionKey(session_key);
+					cusLoginInfo.setUnionId(unionid);
+					cusLoginInfo.setCreateTime(DateTimeUtil.getCurrentDateTime());
+					cusLoginInfo.setLastModTime(DateTimeUtil.getCurrentDateTime());
+					log.info("微信用户登录成功---------添加登录信息成功-------");
+					cusLoginInfoDao.save(cusLoginInfo);
+					// 解密用户基本信息,添加用户基本信息
+					try {
+						String resu = AesCbcUtil.decrypt(encryptedData, session_key, iv, "UTF-8");
+						JSONObject jo = JSON.parseObject(resu);
+						String nickName=jo.getString("nickName"); //用户昵称 
+						String jsonsds=jo.getString("avatarUrl"); //用户头像 
+						String sex = jo.getString("gender");//性别 
+						//String unionid = jo.getString("unionid");
+						//String city = jo.getString("city"); //城市 
+						//String province = jo.getString("province");//省份 
+						//String country = jo.getString("country"); //国家s
+						CusSelfInfo cusSelfInfo = new CusSelfInfo();
+						cusSelfInfo.setId(UUID.randomUUID().toString().replaceAll("-", ""));
+						cusSelfInfo.setNickName(nickName);
+						cusSelfInfo.setHeadImg(jsonsds);
+						cusSelfInfo.setOpenId(openid);
+						cusSelfInfo.setUserType("1");//普通用户
+						cusSelfInfo.setCreateTime(DateTimeUtil.getCurrentDateTime());
+						cusSelfInfo.setLastModBy(nickName);
+						cusSelfInfo.setCreateUser(nickName);
+						cusSelfInfo.setLastModTime(DateTimeUtil.getCurrentDateTime());
+						if("1".equals(sex)) {
+							cusSelfInfo.setSex("1");
+						}else if("2".equals(sex)) {
+							cusSelfInfo.setSex("0");
+						}else {
+							cusSelfInfo.setSex("2");// 未知
+						}
+						cusSelfInfoDao.save(cusSelfInfo);
+					} catch (Exception e) {
+						e.printStackTrace();
+					}
+				}
+				// 根据openid判断是否绑定手机
+				CusAuthInfo isAuth = cusAuthInfoDao.getByOpenId(openid);
+				if(isAuth!=null) {
+					dataMap.put("isAuth", true);
+				}else {
+					dataMap.put("isAuth", false);
+				}
+				// 根据openid判断是否是大V用户
+				CusAuditInfo isAudit = cusAuditInfoDao.getByOpenId(openid);
+				if(isAudit!=null) {
+					String auditStatus = isAudit.getAuditStatus();
+					dataMap.put("isAudit", auditStatus);
+				}else {
+					dataMap.put("isAudit", "3");// 未申请
+				}
+				// 将session_key\token返回给前端
+				dataMap.put("token", token);
+				dataMap.put("openid", openid);
+				dataMap.put("session_key", session_key);
+				resultMap.put("data", dataMap);
+				resultMap.put("code", "0");
+				resultMap.put("msg", "成功");
+			}
+		}
+		return JSON.toJSONString(resultMap);
+	}
+	
+	/**
+	 * 微信服务鉴权 
+	 */
+	public String commenAuth(Map<String, Object> paramMap) {
+		Map<String,Object> resultMap = new HashMap<String,Object>();
+		String openid = (String) paramMap.get("openId");
+		String token = (String) paramMap.get("token");
+		String msg = "";
+		if(StrUtil.isEmpty(token)) {
+			msg = "token不能为空";
+		}
+		if(StrUtil.isEmpty(openid)) {
+			msg = "openid不能为空";
+		}
+		if(StrUtil.isNotEmpty(msg)) {
+			resultMap.put("code", "1");
+			resultMap.put("msg", msg);
+			resultMap.put("data", "");
+			return JSON.toJSONString(resultMap);
+		}
+		CusLoginInfo byToken = cusLoginInfoDao.getDetailByToken(token);
+		if(StrUtil.isEmpty(byToken)) {
+			resultMap.put("code", "2");
+			resultMap.put("msg", "token非法");
+			resultMap.put("data", "");
+			return JSON.toJSONString(resultMap);
+		}
+		if(!openid.equals(byToken.getOpenId())) {
+			resultMap.put("code", "1");
+			resultMap.put("msg", "openid不一致");
+			resultMap.put("data", "");
+		}else {
+			resultMap.put("code", "0");
+			resultMap.put("msg", "验证通过");
+			resultMap.put("data", "");
+		}
+		return JSON.toJSONString(resultMap);
+	}
+	
+	/**
+	  *  获取短信验证码
+	 *@return  
+	 **/
+	public String getVerifyCode(HashMap<String, Object> paramMap) {
+		log.info("获取短信验证码---------start-------");
+		Map<String,Object> resultMap = new HashMap<String,Object>();
+		String openid = (String) paramMap.get("openId");
+		String phone = (String) paramMap.get("phone");
+		String msg = "";
+		if(StrUtil.isEmpty(openid)) {
+			msg = "openId不能为空";
+		}
+		if(StrUtil.isEmpty(phone)) {
+			msg = "mobile不能为空";
+		}else {
+			// 校验手机号
+			Pattern pattern = Pattern.compile("/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\\d{8}$/");
+			Matcher matcher = pattern.matcher(phone);
+			if(!matcher.matches()) {
+				msg = "手机号不正确";
+			}
+		}
+		if(StrUtil.isNotEmpty(msg)) {
+			resultMap.put("code", "1");
+			resultMap.put("msg", msg);
+			resultMap.put("data", "");
+			return JSON.toJSONString(resultMap);
+		}
+		// 校验手机号是否已经使用
+		// 校验是否曾经认证成功
+		CusAuthInfo selective = cusAuthInfoDao.getBySelective(paramMap);
+		if(selective!=null) {
+			resultMap.put("code", "1");
+			resultMap.put("msg", "您已经是认证用户,无需重新认证!");
+			resultMap.put("data", "");
+			return JSON.toJSONString(resultMap);
+		}
+		// 校验openid是否合法
+		CusLoginInfo detailByOpenId = cusLoginInfoDao.getDetailByOpenId(openid);
+		if(detailByOpenId==null) {
+			resultMap.put("code", "1");
+			resultMap.put("msg", "openid非法");
+			resultMap.put("data", "");
+			return JSON.toJSONString(resultMap);
+		}
+		// 调用56短信平台接口发送短信
+		String verifyCode = String.valueOf(new Random().nextInt(899999)+100000);
+		// 是否发送过验证码,且验证码有效
+		SmsVerifycode smsVerifycodeTemp = smsVerifycodeDao.SelectOne(phone);
+		if(smsVerifycodeTemp != null && "0".equals(smsVerifycodeTemp.getIsExpire())) {
+			verifyCode = smsVerifycodeTemp.getVerifycode();
+		}
+		String content = "您的验证码为:"+verifyCode+",该验证码有效期为5分钟,该码只能使用一次!";
+		
+		String smsUrl = myProps.getSmsUrl();
+		String comid = myProps.getComid();
+		String smsUname = myProps.getSmsUname();
+		String smsPwd = myProps.getSmsPwd();
+		
+		String result = "";
+		try {
+			String httpLink = smsUrl+"comid="+comid+"&username="+smsUname+"&userpwd="+smsPwd+"&handtel="+phone+"&sendcontent="+content+"&sendtime=定时时间&smsnumber=所用平台";
+			log.info("短信平台调用地址URL>>>>"+httpLink);
+			result = SMSUtil.invokeHttpSMS(httpLink);
+		} catch (Exception e) {
+			log.info("调用短信接口异常》》》》》",e);
+			resultMap.put("code", "1");
+			resultMap.put("msg", "短信接口异常");
+			resultMap.put("data", "");
+			return JSON.toJSONString(resultMap);
+		}
+		
+		if("1".equals(result)) {
+			// 发送成功,入库
+			SmsVerifycode smsVer = new SmsVerifycode();
+			smsVer.setId(UUID.randomUUID().toString().replaceAll("-", ""));
+			smsVer.setCreateTime(DateTimeUtil.getCurrentDateTime());
+			smsVer.setLastModTime(DateTimeUtil.getCurrentDateTime());
+			smsVer.setMobile(phone);
+			smsVer.setOpenId(openid);
+			smsVer.setSmsContent(content);
+			smsVer.setVerifycode(verifyCode);
+			smsVerifycodeDao.save(smsVer);
+			resultMap.put("code", "0");
+			resultMap.put("msg", "发送短信验证码成功");
+			resultMap.put("data", "");
+			return JSON.toJSONString(resultMap);
+		}else {
+			resultMap.put("code", "1");
+			resultMap.put("msg", "发送短信验证码失败");
+			resultMap.put("data", "");
+			return JSON.toJSONString(resultMap);
+		}
+	}
+
+	/**
+	  *  用户手机号绑定
+	 *@return  
+	 **/
+	public String bindMobile(HashMap<String, Object> paramMap) {
+		log.info("用户手机号绑定---------start-------");
+		// 微信服务鉴权
+		String auth = commenAuth(paramMap);
+		JSONObject jsonObject = JSON.parseObject(auth);
+		if(!"0".equals((String)jsonObject.get("code"))) {
+			return auth;
+		}
+		Map<String,Object> resultMap = new HashMap<String,Object>();
+		String openid = (String) paramMap.get("openId");
+		String iv = (String) paramMap.get("iv");
+		String encryptedData = (String) paramMap.get("encryptedData");
+		String sessionKey = (String) paramMap.get("sessionKey");
+		String mobile = "";
+		//String verifyCode = (String) paramMap.get("verifyCode");
+		//String mobile = (String) paramMap.get("mobile");
+		// 根据openid查询用户基本信息
+		CusLoginInfo loginDetaild = cusLoginInfoDao.getDetailByOpenId(openid);
+		if(loginDetaild==null) {
+			resultMap.put("code", "1");
+			resultMap.put("msg", "获取用户登录信息失败");
+			resultMap.put("data", "");
+			return JSON.toJSONString(resultMap);
+		}else {
+			// 解密手机号
+			String result;
+			try {
+				result = AesCbcUtil.decrypt(encryptedData, sessionKey, iv, "UTF-8");
+				if (null != result && result.length() > 0) { 
+					log.info("获取解密后数据————————————"+result);
+					// 将解密后的JSON格式字符串转化为对象 
+					JSONObject jo = JSON.parseObject(result);
+					mobile = jo.getString("phoneNumber");
+					log.info("获取到手机号为-------"+mobile);
+				}
+				if(StrUtil.isEmpty(mobile)) {
+					resultMap.put("code", "1");
+					resultMap.put("msg", "获取手机号为空");
+					resultMap.put("data", "");
+					return JSON.toJSONString(resultMap);
+				}
+				// 校验手机号是否绑定其他账号
+				// 校验短信验证码是否有效(暂时抛弃)
+				CusAuthInfo cusAuthInfo1 = cusAuthInfoDao.getDetailByMobile(mobile);
+				if(cusAuthInfo1!=null) {
+					resultMap.put("code", "1");
+					resultMap.put("msg", "该手机号已绑定其他账号");
+					resultMap.put("data", "");
+					return JSON.toJSONString(resultMap);
+				}else {
+					CusAuthInfo cusAuthInfo = new CusAuthInfo();
+					cusAuthInfo.setId(UUID.randomUUID().toString().replaceAll("-", ""));
+					cusAuthInfo.setOpenId(openid);
+					cusAuthInfo.setMobile(mobile);
+					cusAuthInfo.setCreateTime(DateTimeUtil.getCurrentDateTime());
+					cusAuthInfo.setLastModTime(DateTimeUtil.getCurrentDateTime());
+					cusAuthInfoDao.save(cusAuthInfo);
+					resultMap.put("code", "0");
+					resultMap.put("msg", "用户手机号绑定成功");
+					resultMap.put("data", "");
+					return JSON.toJSONString(resultMap);
+				}
+			} catch (Exception e) {
+				e.printStackTrace();
+			}
+		}
+		return JSON.toJSONString(resultMap);
+		
+	}
+
+	/**
+	 * 上传文件接口 
+	 */
+	public String uploadFile(MultipartFile file, String openid,String token) throws Exception{
+		Map<String,Object> resultMap = new HashMap<String,Object>();
+		HashMap<String,Object> paramMap = new HashMap<String,Object>();
+		paramMap.put("openId", openid);
+		paramMap.put("token", token);
+		// 微信服务鉴权
+		String auth = commenAuth(paramMap);
+		JSONObject jsonObject = JSON.parseObject(auth);
+		if(!"0".equals((String)jsonObject.get("code"))) {
+			return auth;
+		}
+		
+		FileInfo fileInfo = fileService.save(file);
+		resultMap.put("code", "0");
+		resultMap.put("msg", "上传成功");
+		resultMap.put("data", fileInfo);
+		
+		return JSON.toJSONString(resultMap);
+	}
+
+	/**
+	 * 大V认证接口 
+	 */
+	public String auditCheck(HashMap<String, Object> paramMap) {
+		Map<String,Object> resultMap = new HashMap<String,Object>();
+		String openid = (String) paramMap.get("openId");
+		String picPath = (String) paramMap.get("picPath");
+		// 微信服务鉴权
+		String auth = commenAuth(paramMap);
+		JSONObject jsonObject = JSON.parseObject(auth);
+		if(!"0".equals((String)jsonObject.get("code"))) {
+			return auth;
+		}
+		// 根据openid查询用户基本信息
+		CusSelfInfo selfInfo = cusSelfInfoDao.getByOpenId(openid);
+		if(selfInfo==null) {
+			resultMap.put("code", "1");
+			resultMap.put("msg", "获取用户基本信息失败");
+			resultMap.put("data", "");
+			return JSON.toJSONString(resultMap);
+		}else {
+			// 根据openid查看是否申请认证过
+			CusAuditInfo byOpenId = cusAuditInfoDao.getByOpenIdAndStatus(openid,"0");
+			// 存在申请中的数据
+			if(byOpenId!=null) {
+				resultMap.put("code", "1");
+				resultMap.put("msg", "请勿重复申请");
+				resultMap.put("data", byOpenId);
+			}else {
+				CusAuditInfo auditInfo = new CusAuditInfo();
+				auditInfo.setId(UUID.randomUUID().toString().replaceAll("-", ""));
+				auditInfo.setCusSelfId(selfInfo.getId());
+				auditInfo.setCertifiedImg(picPath);
+				auditInfo.setAuditStatus("0");
+				auditInfo.setCreateTime(DateTimeUtil.getCurrentDateTime());
+				auditInfo.setLastModTime(DateTimeUtil.getCurrentDateTime());
+				auditInfo.setCreateUser("用户申请");
+				auditInfo.setLastModBy("用户申请");
+				auditInfo.setOpenid(openid);
+				cusAuditInfoDao.save(auditInfo);
+				resultMap.put("code", "0");
+				resultMap.put("msg", "申请成功");
+				resultMap.put("data", auditInfo);
+			}
+			return JSON.toJSONString(resultMap);
+		}
+	}
+	
+	/**
+	 * 获取字典列表接口 
+	 * @param type
+	 */
+	public String getDictByType(HashMap<String, Object> paramMap) {
+		Map<String,Object> resultMap = new HashMap<String,Object>();
+		String type = (String) paramMap.get("type");
+		// 微信服务鉴权
+		String auth = commenAuth(paramMap); 
+		JSONObject jsonObject =JSON.parseObject(auth); 
+		if(!"0".equals((String)jsonObject.get("code"))) {
+		  return auth; 
+		}
+		List<Dict> dictList = dictDao.listByType(type);
+		resultMap.put("code", "0");
+		resultMap.put("msg", "获取字典列表成功");
+		resultMap.put("data", dictList);
+		return JSON.toJSONString(resultMap);
+	}
+
+	
+	public String createQRCode(String path,String width,String scene) {
+		Map<String,Object> resultMap = new HashMap<String,Object>();
+		String id = UUID.randomUUID().toString().replaceAll("-", "");
+		String picName = id+".jpg";
+		String url = myProps.getWxCreateQRCode();
+		log.info("微信登录url:"+url);
+		String accessToken = TokenThread.accessToken.getToken();
+		url = url+"access_token="+accessToken;
+		JSONObject json = new JSONObject();
+		json.put("path", path);
+		json.put("width", width);
+		json.put("scene", scene);
+		//json.put("is_hyaline", true);
+		InputStream instream;
+		try {
+			instream = HttpUtil.postResponse(url, json);
+			if(instream!=null) {
+				MultipartFile multipartFile = new MockMultipartFile(picName,picName,"", instream);
+				FileInfo save = fileService.save(multipartFile);
+				log.info("生成二维码成功----------------"+JSON.toJSONString(save));
+				resultMap.put("code", "0");
+				resultMap.put("msg", "生成二维码成功");
+				resultMap.put("data", save);
+			}else {
+				resultMap.put("code", "1");
+				resultMap.put("msg", "生成二维码失败");
+				resultMap.put("data", "");
+			}
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		
+		return JSON.toJSONString(resultMap);
+	}
+	
+}