| | |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.alipay.api.internal.util.codec.Base64; |
| | | import com.baomidou.mybatisplus.mapper.EntityWrapper; |
| | | import com.baomidou.mybatisplus.service.impl.ServiceImpl; |
| | | import com.stylefeng.guns.core.common.constant.JwtConstants; |
| | |
| | | import org.apache.shiro.util.ByteSource; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.http.HttpEntity; |
| | | import org.springframework.http.HttpMethod; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.LinkedMultiValueMap; |
| | | import org.springframework.util.MultiValueMap; |
| | | import org.springframework.web.bind.annotation.ResponseBody; |
| | | import org.springframework.web.client.RestTemplate; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.io.*; |
| | | import java.math.BigDecimal; |
| | | import java.security.SecureRandom; |
| | | import java.util.*; |
| | | |
| | | |
| | |
| | | |
| | | @Resource |
| | | private DriverMapper driverMapper; |
| | | @Autowired |
| | | private RestTemplate restTemplate; |
| | | @Resource |
| | | private InviteMapper inviteMapper; |
| | | |
| | | @Resource |
| | | private DriverActivityRegisteredMapper driverActivityRegisteredMapper; |
| | |
| | | * @return |
| | | */ |
| | | @Override |
| | | public ResultUtil<LoginWarpper> captchaLogin(String phone, String code, String registIp, String registAreaCode,String loginType) throws Exception { |
| | | public ResultUtil<LoginWarpper> captchaLogin(String phone, String code, String registIp, String registAreaCode,String loginType |
| | | ,Integer uid,Integer userType |
| | | ) throws Exception { |
| | | boolean b = this.checkCaptcha(phone, code); |
| | | if(!b){ |
| | | return ResultUtil.error("验证码无效"); |
| | |
| | | userInfo.setRegistAreaCode(registAreaCode); |
| | | } |
| | | this.insert(userInfo); |
| | | |
| | | this.addCoupon(userInfo);//添加优惠券 |
| | | |
| | | UserInfo finalUserInfo = userInfo; |
| | |
| | | } |
| | | } |
| | | }).start(); |
| | | |
| | | if (uid!=null&&userType!=null){ |
| | | // 生成邀请记录 |
| | | Invite invite = new Invite(); |
| | | invite.setInviteUserId(uid); |
| | | invite.setUserId(userInfo.getId()); |
| | | invite.setRegisterTime(new Date()); |
| | | invite.setUseType(userType); |
| | | inviteMapper.insert(invite); |
| | | if (userType==1){ |
| | | // 只有用户邀请用户会获得优惠券 |
| | | List<Map<String, Object>> query = userActivityInviteMapper.query(userInfo.getCompanyId()); |
| | | Date date = new Date(); |
| | | for(Map<String, Object> map : query){ |
| | | Double lavePrice = Double.valueOf(map.get("lavePrice").toString()); |
| | | for(int i = Integer.valueOf(String.valueOf(map.get("totalNum"))); i > 0; i--){ |
| | | //判断当前优惠券金额是否大于可发放剩余总金额 |
| | | if(Double.valueOf(String.valueOf(map.get("money"))).compareTo(lavePrice) > 0){ |
| | | break; |
| | | } |
| | | UserCouponRecord userCouponRecord = new UserCouponRecord(); |
| | | userCouponRecord.setActivityType(3); |
| | | userCouponRecord.setCouponActivityId(Integer.valueOf(String.valueOf(map.get("id")))); |
| | | userCouponRecord.setCouponId(Integer.valueOf(String.valueOf(map.get("couponId")))); |
| | | userCouponRecord.setState(1); |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + Integer.valueOf(String.valueOf(map.get("effective")))); |
| | | userCouponRecord.setExpirationTime(calendar.getTime()); |
| | | userCouponRecord.setCouponType(Integer.valueOf(String.valueOf(map.get("couponType")))); |
| | | userCouponRecord.setCouponUseType(Integer.valueOf(String.valueOf(map.get("couponUseType")))); |
| | | userCouponRecord.setInsertTime(date); |
| | | userCouponRecord.setFullMoney(Double.valueOf(String.valueOf(map.get("fullMoney")))); |
| | | userCouponRecord.setMoney(Double.valueOf(String.valueOf(map.get("money")))); |
| | | userCouponRecord.setCompanyId(userInfo.getCompanyId()); |
| | | userCouponRecord.setUserId(uid); |
| | | userCouponRecordService.insert(userCouponRecord); |
| | | //修改剩余可发放总金额 |
| | | lavePrice -= Double.valueOf(String.valueOf(map.get("money"))); |
| | | } |
| | | UserActivityInvite uai = userActivityInviteMapper.selectById(Integer.valueOf(map.get("id").toString())); |
| | | uai.setLavePrice(new BigDecimal(lavePrice).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue()); |
| | | userActivityInviteMapper.updateById(uai); |
| | | } |
| | | } |
| | | } |
| | | this.addCoupon(userInfo);//添加优惠券 |
| | | } |
| | | if(userInfo.getState() == 2){ |
| | | return ResultUtil.error("账号被冻结"); |
| | | } |
| | | |
| | | |
| | | //调用单点登录的逻辑 |
| | | this.singlePointLogin(userInfo.getId(),loginType); |
| | |
| | | } |
| | | @Override |
| | | public synchronized ResultUtil<LoginWarpper> captchaLogin(String phone, String code, Integer uid, Integer type, Integer userType,String loginType) throws Exception { |
| | | ResultUtil<LoginWarpper> resultUtil = this.captchaLogin(phone, code, null, null,loginType); |
| | | ResultUtil<LoginWarpper> resultUtil = this.captchaLogin(phone, code, null, null,loginType,null,null); |
| | | if(resultUtil.getCode() == 200 && null != uid){ |
| | | if(type == 2){//司机分享 |
| | | Driver driver = driverMapper.selectById(uid); |
| | |
| | | */ |
| | | @Override |
| | | public ResultUtil<LoginWarpper> wxLogin(Integer type, String openid, String unionid, String jscode, String registIp, |
| | | String registAreaCode, Integer sex, String nickName, String avatar,String loginType) throws Exception { |
| | | String registAreaCode, Integer sex, String nickName, String avatar,String loginType, |
| | | Integer uid,Integer userType) throws Exception { |
| | | UserInfo userInfo = null; |
| | | if(type == 2){//小程序 |
| | | if(ToolUtil.isEmpty(jscode)){ |
| | |
| | | userInfo = userInfoMapper.queryByOpenid(openid); |
| | | } |
| | | if(null == userInfo){ |
| | | // 如果为空 需要生成小程序二维码并携带用户id |
| | | userInfo = new UserInfo(); |
| | | userInfo.setPassWord(ShiroKit.md5("", salt)); |
| | | userInfo.setRegistIp(registIp); |
| | | userInfo.setSex(sex); |
| | | userInfo.setNickName(ToolUtil.isNotEmpty(nickName) ? nickName : this.getDefaultName()); |
| | | userInfo.setAvatar(avatar); |
| | | |
| | | if(type == 2){ |
| | | userInfo.setAppletsOpenId(openid); |
| | | }else{ |
| | |
| | | userInfo.setRegistAreaCode(registAreaCode); |
| | | } |
| | | this.insert(userInfo); |
| | | |
| | | if (uid!=null&&userType!=null){ |
| | | // 生成邀请记录 |
| | | Invite invite = new Invite(); |
| | | invite.setInviteUserId(uid); |
| | | invite.setUserId(userInfo.getId()); |
| | | invite.setRegisterTime(new Date()); |
| | | invite.setUseType(userType); |
| | | inviteMapper.insert(invite); |
| | | if (userType==1){ |
| | | // 只有用户邀请用户会获得优惠券 |
| | | List<Map<String, Object>> query = userActivityInviteMapper.query(userInfo.getCompanyId()); |
| | | Date date = new Date(); |
| | | for(Map<String, Object> map : query){ |
| | | Double lavePrice = Double.valueOf(map.get("lavePrice").toString()); |
| | | for(int i = Integer.valueOf(String.valueOf(map.get("totalNum"))); i > 0; i--){ |
| | | //判断当前优惠券金额是否大于可发放剩余总金额 |
| | | if(Double.valueOf(String.valueOf(map.get("money"))).compareTo(lavePrice) > 0){ |
| | | break; |
| | | } |
| | | UserCouponRecord userCouponRecord = new UserCouponRecord(); |
| | | userCouponRecord.setActivityType(3); |
| | | userCouponRecord.setCouponActivityId(Integer.valueOf(String.valueOf(map.get("id")))); |
| | | userCouponRecord.setCouponId(Integer.valueOf(String.valueOf(map.get("couponId")))); |
| | | userCouponRecord.setState(1); |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + Integer.valueOf(String.valueOf(map.get("effective")))); |
| | | userCouponRecord.setExpirationTime(calendar.getTime()); |
| | | userCouponRecord.setCouponType(Integer.valueOf(String.valueOf(map.get("couponType")))); |
| | | userCouponRecord.setCouponUseType(Integer.valueOf(String.valueOf(map.get("couponUseType")))); |
| | | userCouponRecord.setInsertTime(date); |
| | | userCouponRecord.setFullMoney(Double.valueOf(String.valueOf(map.get("fullMoney")))); |
| | | userCouponRecord.setMoney(Double.valueOf(String.valueOf(map.get("money")))); |
| | | userCouponRecord.setCompanyId(userInfo.getCompanyId()); |
| | | userCouponRecord.setUserId(uid); |
| | | userCouponRecordService.insert(userCouponRecord); |
| | | //修改剩余可发放总金额 |
| | | lavePrice -= Double.valueOf(String.valueOf(map.get("money"))); |
| | | } |
| | | UserActivityInvite uai = userActivityInviteMapper.selectById(Integer.valueOf(map.get("id").toString())); |
| | | uai.setLavePrice(new BigDecimal(lavePrice).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue()); |
| | | userActivityInviteMapper.updateById(uai); |
| | | } |
| | | } |
| | | } |
| | | this.addCoupon(userInfo);//添加优惠券 |
| | | |
| | | UserInfo finalUserInfo = userInfo; |
| | | new Thread(new Runnable() { |
| | | @Override |
| | |
| | | jsonObject.put(userInfo.getId().toString(), userInfo.getAppletsOpenId()); |
| | | redisUtil.setStrValue("appletOpenId", jsonObject.toJSONString()); |
| | | } |
| | | generateQrCode(userInfo); |
| | | |
| | | //调用单点登录的逻辑 |
| | | this.singlePointLogin(userInfo.getId(),loginType); |
| | |
| | | return ResultUtil.success(loginWarpper); |
| | | } |
| | | |
| | | |
| | | private void generateQrCode(UserInfo userInfo) { |
| | | if (userInfo.getCode()!=null){ |
| | | return; |
| | | } |
| | | String accessToken = weChatUtil.getAccessToken(); |
| | | InputStream inputStream = null; |
| | | OutputStream outputStream = null; |
| | | try { |
| | | String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken; |
| | | Map<String, Object> param = new HashMap<>(); |
| | | // param.put("page", "pageA/houseDetail"); |
| | | param.put("check_path", false); |
| | | // 用户id 用于分享 |
| | | param.put("scene", "uid="+userInfo.getId()+"userType=1"); |
| | | param.put("env_version", "trial"); |
| | | param.put("width", 200); //二维码尺寸 |
| | | param.put("is_hyaline", true); // 是否需要透明底色, is_hyaline 为true时,生成透明底色的小程序码 参数仅对小程序码生效 |
| | | param.put("auto_color", true); // 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调 参数仅对小程序码生效 |
| | | Map<String, Object> line_color = new HashMap<>(); |
| | | line_color.put("r", 0); |
| | | line_color.put("g", 0); |
| | | line_color.put("b", 0); |
| | | param.put("line_color", line_color); |
| | | System.err.println("调用生成微信URL接口传参:" + param); |
| | | MultiValueMap<String, String> headers = new LinkedMultiValueMap<>(); |
| | | HttpEntity requestEntity = new HttpEntity(param, headers); |
| | | ResponseEntity<byte[]> entity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]); |
| | | System.err.println("调用小程序生成微信永久小程序码URL接口返回结果:" + entity.getBody()); |
| | | byte[] result = entity.getBody(); |
| | | System.err.println(Base64.encodeBase64String(result)); |
| | | |
| | | inputStream = new ByteArrayInputStream(result); |
| | | String finalFileName = System.currentTimeMillis() + "" + new SecureRandom().nextInt(0x0400) + ".jpeg"; |
| | | MultipartFile multipartFile = convertInputStreamToMultipartFile(inputStream, finalFileName, "image/jpeg"); |
| | | String pictureName = OssUploadUtil.ossUploadCode(multipartFile); |
| | | System.err.println(pictureName); |
| | | userInfo.setCode(pictureName); |
| | | this.updateById(userInfo); |
| | | |
| | | } catch (Exception e) { |
| | | System.err.println("调用小程序生成微信永久小程序码URL接口异常" + e); |
| | | } finally { |
| | | if (inputStream != null) { |
| | | try { |
| | | inputStream.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | if (outputStream != null) { |
| | | try { |
| | | outputStream.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | public MultipartFile convertInputStreamToMultipartFile(InputStream inputStream, String fileName, String contentType) throws IOException { |
| | | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| | | byte[] buffer = new byte[1024]; |
| | | int bytesRead; |
| | | while ((bytesRead = inputStream.read(buffer)) != -1) { |
| | | byteArrayOutputStream.write(buffer, 0, bytesRead); |
| | | } |
| | | byte[] data = byteArrayOutputStream.toByteArray(); |
| | | return new CustomMultipartFile(data, fileName, contentType); |
| | | } |
| | | |
| | | /** |
| | | * 忘记密码操作 |
| | |
| | | if(null == userInfo){ |
| | | return ResultUtil.error("获取用户信息失败"); |
| | | } |
| | | return this.captchaLogin(userInfo.get("phone"), "1234", null, registAreaCode, loginType); |
| | | return this.captchaLogin(userInfo.get("phone"), "1234", null, registAreaCode, loginType,null,null); |
| | | }else{ |
| | | return ResultUtil.error(jsonObject.getString("msg")); |
| | | } |
| | |
| | | jsonObject.put(userId.toString(), openid); |
| | | redisUtil.setStrValue("appletOpenId", jsonObject.toJSONString()); |
| | | return ResultUtil.success(); |
| | | } |
| | | |
| | | @Override |
| | | public UserInfo generateCode(UserInfo userInfo) { |
| | | generateQrCode(userInfo); |
| | | return userInfo; |
| | | } |
| | | |
| | | public Map<String, String> getUserInfo(String accessToken){ |
| | |
| | | //添加系统消息 |
| | | systemNoticeService.addSystemNotice(1, "您已获得" + num + "张优惠券,点击查看", userInfo.getId(), 2); |
| | | } |
| | | /** |
| | | * 邀请注册时查询活动添加优惠券 |
| | | * @param userInfo |
| | | * @throws Exception |
| | | */ |
| | | private synchronized void addInviteCoupon(UserInfo userInfo) throws Exception{ |
| | | //添加优惠券 |
| | | List<Map<String, Object>> list = userActivityInviteMapper.query(userInfo.getCompanyId()); |
| | | int num = 0; |
| | | for(Map<String, Object> map : list){ |
| | | if(null != map){ |
| | | Integer totalNum = Integer.valueOf(String.valueOf(map.get("totalNum"))); |
| | | Double lavePrice = Double.valueOf(String.valueOf(map.get("lavePrice"))); |
| | | for(int i = totalNum; i > 0; i--){ |
| | | //判断当前发放的优惠券是否大于剩余可发送总金额 |
| | | if(Double.valueOf(String.valueOf(map.get("money"))).compareTo(lavePrice) > 0){ |
| | | break; |
| | | } |
| | | UserCouponRecord userCouponRecord = new UserCouponRecord(); |
| | | userCouponRecord.setUserId(userInfo.getId()); |
| | | userCouponRecord.setCompanyId(Integer.valueOf(String.valueOf(map.get("companyId")))); |
| | | userCouponRecord.setMoney(Double.valueOf(String.valueOf(map.get("money")))); |
| | | userCouponRecord.setFullMoney(Double.valueOf(String.valueOf(map.get("fullMoney")))); |
| | | userCouponRecord.setInsertTime(new Date()); |
| | | userCouponRecord.setState(1); |
| | | userCouponRecord.setCouponUseType(Integer.valueOf(String.valueOf(map.get("couponUseType")))); |
| | | userCouponRecord.setCouponType(Integer.valueOf(String.valueOf(map.get("couponType")))); |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(new Date()); |
| | | Integer integer = Integer.valueOf(String.valueOf(null != map.get("effective") ? map.get("effective") : "0")); |
| | | calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + integer); |
| | | userCouponRecord.setExpirationTime(calendar.getTime()); |
| | | userCouponRecord.setCouponId(Integer.valueOf(String.valueOf(map.get("couponId")))); |
| | | userCouponRecord.setCouponActivityId(Integer.valueOf(String.valueOf(map.get("id")))); |
| | | userCouponRecord.setActivityType(2); |
| | | userCouponRecordService.insert(userCouponRecord); |
| | | //修改剩余可发放总金额 |
| | | lavePrice -= Double.valueOf(String.valueOf(map.get("money"))); |
| | | num++; |
| | | } |
| | | UserActivityRegistered uar = userActivityRegisteredService.selectById(Integer.valueOf(map.get("id").toString())); |
| | | uar.setLavePrice(new BigDecimal(lavePrice).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue()); |
| | | userActivityRegisteredService.updateById(uar); |
| | | } |
| | | } |
| | | //添加系统消息 |
| | | systemNoticeService.addSystemNotice(1, "您已获得" + num + "张优惠券,点击查看", userInfo.getId(), 2); |
| | | } |
| | | |
| | | |
| | | /** |