登陆集成微信小程序登陆及其增加余额支付功能等
This commit is contained in:
1 parent
1d4d2b2573
commit
2c4b1ceaa2
5 files changed
+111
-29
No files matched your search
@@ -0,0 +1,28 @@
|
||||
package com.ruoyi.app.common.utils;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
*/
|
||||
public class JsonUtils {
|
||||
private static final ObjectMapper JSON = new ObjectMapper();
|
||||
|
||||
static {
|
||||
JSON.setSerializationInclusion(Include.NON_NULL);
|
||||
JSON.configure(SerializationFeature.INDENT_OUTPUT, Boolean.TRUE);
|
||||
}
|
||||
|
||||
public static String toJson(Object obj) {
|
||||
try {
|
||||
return JSON.writeValueAsString(obj);
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,33 @@
|
||||
package com.ruoyi.app.modular.auth.controller;
|
||||
|
||||
|
||||
|
||||
import cn.binarywang.wx.miniapp.api.WxMaService;
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.itmuch.lightsecurity.annotation.PreAuthorize;
|
||||
import com.itmuch.lightsecurity.jwt.JwtOperator;
|
||||
import com.itmuch.lightsecurity.jwt.User;
|
||||
import com.itmuch.lightsecurity.jwt.UserOperator;
|
||||
import com.ruoyi.app.common.R;
|
||||
import com.ruoyi.app.common.persistence.model.StoreMember;
|
||||
import com.ruoyi.app.common.utils.JsonUtils;
|
||||
import com.ruoyi.app.modular.member.service.IMemberService;
|
||||
import com.ruoyi.app.modular.member.service.vo.LoginVO;
|
||||
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
@@ -25,6 +36,7 @@ import java.util.HashMap;
|
||||
* @Date 2019/6/27
|
||||
**/
|
||||
//@RequestMapping(value = "/")
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
||||
@Api(value = "认证授权模块", tags = "认证授权模块", description = "认证授权模块")
|
||||
@@ -32,6 +44,7 @@ public class AuthController {
|
||||
private final UserOperator userOperator;
|
||||
private final JwtOperator operator;
|
||||
private final IMemberService memberService;
|
||||
private final WxMaService wxService;
|
||||
|
||||
/**
|
||||
* 获取当前登录用户信息
|
||||
@@ -53,25 +66,59 @@ public class AuthController {
|
||||
@PostMapping("/oauth/access_token")
|
||||
@ApiOperation(value = "获取token",notes = "获取token")
|
||||
public R loginReturnToken(@Validated @RequestBody LoginVO loginVO) {
|
||||
Boolean isProduct = false;
|
||||
String openid = null;
|
||||
Boolean isProduct = true; //true 开启真实小程序环境
|
||||
String openid = "orIMY4xGhMmipwFZoSL1vOhUNFZ0";
|
||||
WxMaUserInfo userInfo = null;
|
||||
try{
|
||||
if(isProduct){
|
||||
//todo 此处结合小程序API
|
||||
}else{
|
||||
openid = "orIMY4xGhMmipwFZoSL1vOhUNFZ0";
|
||||
WxMaJscode2SessionResult session = wxService.getUserService()
|
||||
.getSessionInfo(loginVO.getCode());
|
||||
//log.info(session.getSessionKey());
|
||||
//log.info(session.getOpenid());
|
||||
//TODO 可以增加自己的逻辑,关联业务相关数据
|
||||
|
||||
String sessionKey = session.getSessionKey();
|
||||
openid = session.getOpenid();
|
||||
// 解密用户信息
|
||||
userInfo = wxService.getUserService()
|
||||
.getUserInfo(sessionKey, loginVO.getEncrypted_data(), loginVO.getIv());
|
||||
|
||||
}
|
||||
|
||||
StoreMember member = memberService.login(openid);
|
||||
//System.out.println(member);
|
||||
User user = null;
|
||||
if(member == null){
|
||||
R.error(401,"用户登陆失败");
|
||||
}
|
||||
User user = User.builder()
|
||||
//新用户插入数据
|
||||
StoreMember newMember = new StoreMember();
|
||||
newMember.setOpenid(userInfo.getOpenId());
|
||||
newMember.setNickname(userInfo.getNickName());
|
||||
newMember.setHeadimg(userInfo.getAvatarUrl());
|
||||
newMember.setSex(userInfo.getGender());
|
||||
newMember.setCreateTime(new Date());
|
||||
memberService.save(newMember);
|
||||
user = User.builder()
|
||||
.id(newMember.getId().intValue())
|
||||
.username(newMember.getNickname())
|
||||
.build();
|
||||
}else{
|
||||
user = User.builder()
|
||||
.id(member.getId().intValue())
|
||||
.username(member.getNickname())
|
||||
//.roles(Arrays.asList("user", "admin"))
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
String token = operator.generateToken(user);
|
||||
HashMap<String,String> map = new HashMap<>();
|
||||
map.put("access_token",token);
|
||||
return R.success(map);
|
||||
} catch (WxErrorException e) {
|
||||
//System.out.println(e.getMessage());
|
||||
log.error(e.getMessage());
|
||||
return R.error(4000,e.getMessage());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -174,7 +174,7 @@ public class MallController {
|
||||
}
|
||||
|
||||
HashMap<String,String> map = new HashMap<>();
|
||||
map.put("orderSn",orderSn);
|
||||
map.put("order_no",orderSn);
|
||||
return R.success(map);
|
||||
}
|
||||
|
||||
|
||||
@@ -87,23 +87,24 @@ public class PayController {
|
||||
orderService.payYue(storeOrder);
|
||||
return R.success("余额支付成功");
|
||||
}else{
|
||||
return R.error(4000,"微信支付开发中");
|
||||
//todo 微信支付
|
||||
try {
|
||||
//WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
|
||||
orderRequest.setBody("主题");
|
||||
orderRequest.setOutTradeNo("订单号");
|
||||
orderRequest.setTotalFee(1);//元转成分
|
||||
orderRequest.setOpenid("openId");
|
||||
orderRequest.setSpbillCreateIp("userIp");
|
||||
//orderRequest.setTimeStart("yyyyMMddHHmmss");
|
||||
// orderRequest.setTimeExpire("yyyyMMddHHmmss");
|
||||
|
||||
return R.success(wxPayService.createOrder(orderRequest));
|
||||
} catch (Exception e) {
|
||||
log.error("微信支付失败!订单号:{},原因:{}", orderNo, e.getMessage());
|
||||
e.printStackTrace();
|
||||
return R.error(4000,"支付失败,请稍后重试!");
|
||||
}
|
||||
// try {
|
||||
// //WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
|
||||
// orderRequest.setBody("主题");
|
||||
// orderRequest.setOutTradeNo("订单号");
|
||||
// orderRequest.setTotalFee(1);//元转成分
|
||||
// orderRequest.setOpenid("openId");
|
||||
// orderRequest.setSpbillCreateIp("userIp");
|
||||
// //orderRequest.setTimeStart("yyyyMMddHHmmss");
|
||||
// // orderRequest.setTimeExpire("yyyyMMddHHmmss");
|
||||
//
|
||||
// return R.success(wxPayService.createOrder(orderRequest));
|
||||
// } catch (Exception e) {
|
||||
// log.error("微信支付失败!订单号:{},原因:{}", orderNo, e.getMessage());
|
||||
// e.printStackTrace();
|
||||
// return R.error(4000,"支付失败,请稍后重试!");
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -58,9 +58,9 @@ spring:
|
||||
#driver-class-name: com.mysql.jdbc.Driver
|
||||
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
|
||||
#driverClassName: com.mysql.cj.jdbc.Driver
|
||||
username: supply
|
||||
password: supply#@147
|
||||
url: jdbc:p6spy:mysql://localhost:3306/supply?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=UTC
|
||||
username: root
|
||||
password: root
|
||||
url: jdbc:p6spy:mysql://localhost:3306/ruoyi?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=UTC
|
||||
#url: jdbc:mysql://localhost:3306/ruoyi?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=UTC
|
||||
type: com.alibaba.druid.pool.DruidDataSource #这里是配置druid连接池,以下都是druid的配置信息
|
||||
druid:
|
||||
@@ -149,3 +149,9 @@ wx:
|
||||
subAppId:
|
||||
subMchId:
|
||||
keyPath:
|
||||
miniapp:
|
||||
appid: wx604d2ea4702620d2
|
||||
secret: 0beb4dad95fbd97cbaf2a0a4dec51575
|
||||
token:
|
||||
aesKey:
|
||||
msgDataFormat: JSON
|
||||
Reference in new issue
Block a user