parent
fef53ce0b6
commit
09cda3409b
@ -0,0 +1,29 @@
|
||||
package com.itmk.utils;
|
||||
|
||||
public class BusinessException extends RuntimeException {
|
||||
private Integer code;
|
||||
|
||||
private String message;
|
||||
|
||||
public BusinessException(Integer code, String message) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(Integer code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
package com.itmk.utils;
|
||||
|
||||
public enum BusinessExceptionEnum {
|
||||
SERVER_ERROR(500, "服务器异常!"),
|
||||
;
|
||||
|
||||
private Integer code;
|
||||
private String message;
|
||||
|
||||
BusinessExceptionEnum(Integer code, String message) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
package com.itmk.web.address.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@TableName("user_address")
|
||||
public class UserAddress {
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long addressId;
|
||||
private String openid;
|
||||
private String userName;
|
||||
private String phone;
|
||||
private String area;
|
||||
private String address;
|
||||
private String status;
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
package com.itmk.web.address.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.itmk.web.address.entity.UserAddress;
|
||||
|
||||
public interface UserAddressMapper extends BaseMapper<UserAddress> {
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
package com.itmk.web.address.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.itmk.web.address.entity.UserAddress;
|
||||
|
||||
public interface UserAddressService extends IService<UserAddress> {
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
package com.itmk.web.address.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.itmk.web.address.entity.UserAddress;
|
||||
import com.itmk.web.address.mapper.UserAddressMapper;
|
||||
import com.itmk.web.address.service.UserAddressService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements UserAddressService {
|
||||
}
|
||||
@ -0,0 +1,59 @@
|
||||
package com.itmk.web.wxapi.controller;
|
||||
|
||||
import com.itmk.utils.*;
|
||||
import com.itmk.web.wxapi.entity.Code2Session;
|
||||
import com.itmk.web.wxapi.entity.LoginParm;
|
||||
import com.itmk.web.wxapi.entity.LoginVo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/wxapi/user")
|
||||
public class WxLoginController {
|
||||
@Value("${Wechat.Applets.appId}")
|
||||
private String appId;
|
||||
@Value("${Wechat.Applets.appSecret}")
|
||||
private String appSecret;
|
||||
//小程序登录
|
||||
@PostMapping("/wxLogin")
|
||||
public ResultVo wxLogin(@RequestBody LoginParm parm){
|
||||
String code = parm.getCode();
|
||||
log.info("wxlogin - code: " + code);
|
||||
String url = "https://api.weixin.qq.com/sns/jscode2session";
|
||||
Map<String, String> param = new HashMap<>();
|
||||
param.put("appid", appId);
|
||||
param.put("secret", appSecret);
|
||||
param.put("js_code", code);
|
||||
param.put("grant_type", "authorization_code");
|
||||
log.info("param: " + param);
|
||||
try {
|
||||
//发送请求
|
||||
String wxResult = HttpClientUtil.doGet(url, param);
|
||||
//转换参数
|
||||
Code2Session userJson = FastJsonTools.getJson(wxResult, Code2Session.class);
|
||||
log.info("userJson: " + userJson);
|
||||
if(userJson == null) throw new BusinessException(500,"小程序获取open失败");
|
||||
String openid = userJson.getOpenid();
|
||||
String sessionKey = userJson.getSession_key();
|
||||
log.info("openid: " + openid);
|
||||
log.info("sessionKey: " + sessionKey);
|
||||
LoginVo vo = new LoginVo();
|
||||
vo.setOpenid(openid);
|
||||
vo.setSessionKey(sessionKey);
|
||||
return ResultUtils.success("获取成功",vo);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
log.error(String.format("WxLoginController.wxLogin Error", e));
|
||||
}
|
||||
return ResultUtils.error("小程序获取open失败");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
package com.itmk.web.wxapi.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class Code2Session {
|
||||
|
||||
private String openid; //用户唯一标识
|
||||
|
||||
private String session_key; //会话密钥
|
||||
|
||||
private String unionid; //用户在开放平台的唯一标识符,在满足 UnionID 下发条件的情况下会返回,详见 UnionID 机制说明。
|
||||
|
||||
private Integer errcode; //错误码
|
||||
|
||||
private String errmsg; //错误信息
|
||||
|
||||
}
|
||||
@ -0,0 +1,8 @@
|
||||
package com.itmk.web.wxapi.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class LoginParm {
|
||||
private String code;
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
package com.itmk.web.wxapi.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class LoginVo {
|
||||
private String openid;
|
||||
private String sessionKey;
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
|
||||
<mapper namespace="com.itmk.web.address.mapper.UserAddressMapper">
|
||||
|
||||
</mapper>
|
||||
Loading…
Reference in new issue