微信网页授权移植

This commit is contained in:
Qi 2025-05-17 14:02:26 +08:00
parent 523ee99574
commit 91d78d656a
4 changed files with 246 additions and 0 deletions

View File

@ -8,8 +8,12 @@ import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpService;
import org.jeecg.common.api.vo.Result; import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.entity.WxH5OuthrizeForm;
import org.jeecg.modules.entity.WxUser;
import org.jeecg.modules.utils.Constant;
import org.jeecg.modules.utils.CookieUtil; import org.jeecg.modules.utils.CookieUtil;
import org.jeecg.modules.utils.MD5Util; import org.jeecg.modules.utils.MD5Util;
import org.jeecg.modules.utils.SHA1Util;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;

View File

@ -0,0 +1,85 @@
package org.jeecg.modules.entity;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import lombok.Data;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.jeecg.modules.utils.Json;
import org.springframework.util.StringUtils;
import java.io.Serializable;
import java.util.Date;
/**
* 微信粉丝
* @author Nifury
* @date 2017-9-27
*/
@Data
@TableName("wx_user")
public class WxUser implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(type = IdType.INPUT)
private String openid;
private String appid;
private String phone;
private String nickname;
private int sex;
private String city;
private String province;
private String headimgurl;
@JSONField(name = "subscribe_time")
private Date subscribeTime;
private boolean subscribe;
private String unionid;
private String remark;
private JSONArray tagidList;
private String subscribeScene;
private String qrSceneStr;
public WxUser() {
}
public WxUser(String openid) {
this.openid = openid;
}
public WxUser(WxMpUser wxMpUser,String appid) {
this.openid = wxMpUser.getOpenId();
this.appid = appid;
this.subscribe=wxMpUser.getSubscribe();
if(wxMpUser.getSubscribe()){
this.nickname = wxMpUser.getNickname();
this.headimgurl = wxMpUser.getHeadImgUrl();
this.subscribeTime = new Date(wxMpUser.getSubscribeTime()*1000);
this.unionid=wxMpUser.getUnionId();
this.remark=wxMpUser.getRemark();
this.tagidList=JSONArray.parseArray(JSONObject.toJSONString(wxMpUser.getTagIds()));
this.subscribeScene=wxMpUser.getSubscribeScene();
String qrScene = wxMpUser.getQrScene();
this.qrSceneStr= !StringUtils.hasText(qrScene) ? wxMpUser.getQrSceneStr() : qrScene;
}
}
public WxUser(WxOAuth2UserInfo wxMpUser, String appid) {
this.openid = wxMpUser.getOpenid();
this.appid = appid;
this.subscribe=wxMpUser.getNickname()!=null;
if(this.subscribe){
this.nickname = wxMpUser.getNickname();
this.headimgurl = wxMpUser.getHeadImgUrl();
this.unionid=wxMpUser.getUnionId();
}
}
@Override
public String toString() {
return Json.toJsonString(this);
}
}

View File

@ -0,0 +1,122 @@
package org.jeecg.modules.utils;
/**
* 常量
* @author Mark sunlightcs@gmail.com
*/
public class Constant {
/** 超级管理员ID */
public static final int SUPER_ADMIN = 1;
/**
* 当前页码
*/
public static final String PAGE = "page";
/**
* 每页显示记录数
*/
public static final String LIMIT = "limit";
/**
* 排序字段
*/
public static final String ORDER_FIELD = "sidx";
/**
* 排序方式
*/
public static final String ORDER = "order";
/**
* 升序
*/
public static final String ASC = "asc";
/**
* 请求header中的微信用户端源链接参数
*/
public static final String WX_CLIENT_HREF_HEADER = "wx-client-href";
/**
* 菜单类型
* @author chenshun
* @email sunlightcs@gmail.com
* @date 2016年11月15日 下午1:24:29
*/
public enum MenuType {
/**
* 目录
*/
CATALOG(0),
/**
* 菜单
*/
MENU(1),
/**
* 按钮
*/
BUTTON(2);
private int value;
MenuType(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
/**
* 定时任务状态
* @author chenshun
* @email sunlightcs@gmail.com
* @date 2016年12月3日 上午12:07:22
*/
public enum ScheduleStatus {
/**
* 正常
*/
NORMAL(0),
/**
* 暂停
*/
PAUSE(1);
private int value;
ScheduleStatus(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
/**
* 云服务商
*/
public enum CloudService {
/**
* 七牛云
*/
QINIU(1),
/**
* 阿里云
*/
ALIYUN(2),
/**
* 腾讯云
*/
QCLOUD(3);
private int value;
CloudService(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
}

View File

@ -0,0 +1,35 @@
package org.jeecg.modules.utils;
import java.security.MessageDigest;
/**
* SHA1加密工具类
*/
public class SHA1Util {
public static String sha1(String s) {
char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
try {
byte[] btInput = s.getBytes();
// 获得SHA1摘要算法的 MessageDigest 对象
MessageDigest mdInst = MessageDigest.getInstance("sha-1");
// 使用指定的字节更新摘要
mdInst.update(btInput);
// 获得密文
byte[] md = mdInst.digest();
// 把密文转换成十六进制的字符串形式
int j = md.length;
char[] str = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}