| | |
| | | package com.ruoyi.study.service.impl; |
| | | |
| | | import cn.hutool.core.util.RandomUtil; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.common.core.constant.Constants; |
| | | import com.ruoyi.common.core.constant.RedisConstants; |
| | | import com.ruoyi.common.core.constant.TokenConstants; |
| | | import com.ruoyi.common.core.exception.GlobalException; |
| | | import com.ruoyi.common.security.service.TokenService; |
| | | import com.ruoyi.study.domain.TUser; |
| | | import com.ruoyi.study.dto.AppUserQuery; |
| | | import com.ruoyi.study.mapper.TUserMapper; |
| | | import com.ruoyi.study.service.ITUserService; |
| | | import com.ruoyi.study.utils.RandomVxUtil; |
| | | import com.ruoyi.study.utils.SignatureUtil; |
| | | import com.ruoyi.study.vo.AppUserVO; |
| | | import org.apache.http.HttpEntity; |
| | | import org.apache.http.HttpResponse; |
| | | import org.apache.http.HttpStatus; |
| | | import org.apache.http.client.HttpClient; |
| | | import org.apache.http.client.methods.HttpGet; |
| | | import org.apache.http.impl.client.HttpClients; |
| | | import org.apache.http.util.EntityUtils; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.io.IOException; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | /** |
| | |
| | | private RedisTemplate<String, Object> redisTemplate; |
| | | @Resource |
| | | private TokenService tokenService; |
| | | |
| | | /** |
| | | * 微信公众号的appid |
| | | */ |
| | | @Value("wx.appId") |
| | | private String appId; |
| | | |
| | | /** |
| | | * 微信公众号的appSecret |
| | | */ |
| | | @Value("wx.secret") |
| | | private String secret; |
| | | |
| | | @Override |
| | | public List<AppUserVO> listAll(AppUserQuery query) { |
| | |
| | | @Override |
| | | public Boolean isVip() { |
| | | TUser user = lambdaQuery().eq(TUser::getId, tokenService.getLoginUserStudy().getUserid()) |
| | | .eq(TUser::getDisabled, 0).one(); |
| | | .eq(TUser::getDisabled, 0).eq(TUser::getState,1).one(); |
| | | // 是否为vip 逻辑 |
| | | if (null == user) { |
| | | return false; |
| | |
| | | // vip过期时间,字段为空也表示 当前用户不是vip |
| | | Date vipEndTime = user.getVipEndTime(); |
| | | return null != vipEndTime && System.currentTimeMillis() <= vipEndTime.getTime(); |
| | | } |
| | | |
| | | @Override |
| | | public Map<String, Object> weiXinShare(String url) { |
| | | // 初始化微信API,并注册AppID |
| | | long timestamp = System.currentTimeMillis() / 1000; |
| | | String noncestr = RandomVxUtil.createRandomString(16); |
| | | String ticket = getTicket(getToken(appId, secret)); |
| | | String str = "jsapi_ticket=" + ticket + "&noncestr=" + noncestr + "×tamp=" + timestamp + "&url=" + url; |
| | | // 获取加密后的签名 |
| | | String signature = SignatureUtil.getSignature(str); |
| | | Map<String, Object> map = new HashMap<>(); |
| | | map.put("appId", appId); |
| | | map.put("timestamp", timestamp); |
| | | map.put("nonceStr", noncestr); |
| | | map.put("signature", signature); |
| | | return map; |
| | | } |
| | | |
| | | public String getToken(String appid, String secret) { |
| | | //拼接访问地址 |
| | | String apiUrl = String.format(TokenConstants.VX_TOKEN_API_PREFIX, appid, secret); |
| | | HttpClient client = HttpClients.createDefault(); |
| | | //get请求 |
| | | HttpGet get = new HttpGet(apiUrl); |
| | | // 初始化解析json格式的对象 |
| | | String result = null; |
| | | try { |
| | | HttpResponse res = client.execute(get); |
| | | // 初始化响应内容 |
| | | String responseContent; |
| | | HttpEntity entity = res.getEntity(); |
| | | //设置编码格式 |
| | | responseContent = EntityUtils.toString(entity, "UTF-8"); |
| | | // 将json字符串转换为json对象 |
| | | JSONObject json = JSONObject.parseObject(responseContent); |
| | | if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { |
| | | if (json.get(Constants.ERR_CODE) != null) { |
| | | // 错误时微信会返回错误码等信息,{"errcode":40013,"errmsg":"invalid appid"} |
| | | throw new GlobalException("微信授权失败!"); |
| | | } else { |
| | | // 正常情况下{"access_token":"ACCESS_TOKEN","expires_in":7200} |
| | | result = String.valueOf(json.get("access_token")); |
| | | } |
| | | } |
| | | return result; |
| | | } catch (IOException e) { |
| | | throw new GlobalException("微信授权失败!"); |
| | | } |
| | | } |
| | | |
| | | public static String getTicket(String accessToken) { |
| | | String turl = String.format(TokenConstants.VX_TICKET_API_PREFIX, accessToken); |
| | | HttpClient client = HttpClients.createDefault(); |
| | | HttpGet get = new HttpGet(turl); |
| | | String result = null; |
| | | try { |
| | | HttpResponse res = client.execute(get); |
| | | // 响应内容 |
| | | String responseContent; |
| | | HttpEntity entity = res.getEntity(); |
| | | responseContent = EntityUtils.toString(entity, "UTF-8"); |
| | | JSONObject jsonObject = JSONObject.parseObject(responseContent); |
| | | // 将json字符串转换为json对象 |
| | | if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { |
| | | if (jsonObject.get(Constants.ERR_CODE) == null) { |
| | | // 错误时微信会返回错误码等信息,{"errcode":40013,"errmsg":"invalid appid"} |
| | | throw new GlobalException("微信授权失败!"); |
| | | } else { |
| | | // 正常情况下{"access_token":"ACCESS_TOKEN","expires_in":7200} |
| | | result = String.valueOf(jsonObject.get("ticket")); |
| | | } |
| | | } |
| | | return result; |
| | | } catch (IOException e) { |
| | | throw new GlobalException("微信授权失败!"); |
| | | } |
| | | } |
| | | |
| | | } |