|
|
|
|
@ -1,20 +1,19 @@ |
|
|
|
|
package org.springblade.modules.weixin.controller; |
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
|
import com.alibaba.fastjson.TypeReference; |
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils; |
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
|
|
|
|
import com.wechat.pay.contrib.apache.httpclient.auth.Verifier; |
|
|
|
|
import io.swagger.annotations.Api; |
|
|
|
|
import lombok.AllArgsConstructor; |
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
import org.apache.http.util.EntityUtils; |
|
|
|
|
import org.springblade.common.cache.DictCache; |
|
|
|
|
import org.springblade.common.enums.DictEnum; |
|
|
|
|
import org.springblade.core.log.exception.ServiceException; |
|
|
|
|
import org.springblade.core.tool.api.R; |
|
|
|
|
import org.springblade.core.tool.utils.SpringUtil; |
|
|
|
|
import org.springblade.modules.auth.endpoint.BladeTokenEndPoint; |
|
|
|
|
import org.springblade.modules.weixin.config.WechatPayValidator; |
|
|
|
|
import org.springblade.modules.desk.entity.Order; |
|
|
|
|
import org.springblade.modules.desk.service.IOrderService; |
|
|
|
|
import org.springblade.modules.weixin.entity.WeChatPhone; |
|
|
|
|
import org.springblade.modules.weixin.entity.WeChatPhoneInfo; |
|
|
|
|
import org.springblade.modules.weixin.entity.WeChatUser; |
|
|
|
|
@ -23,12 +22,8 @@ import org.springblade.modules.weixin.utils.WeChatUtil; |
|
|
|
|
import org.springblade.modules.weixin.utils.WxPayUtils; |
|
|
|
|
import org.springframework.web.bind.annotation.*; |
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.concurrent.locks.ReentrantLock; |
|
|
|
|
|
|
|
|
|
@RestController |
|
|
|
|
@AllArgsConstructor |
|
|
|
|
@ -36,74 +31,85 @@ import java.util.concurrent.locks.ReentrantLock; |
|
|
|
|
@Slf4j |
|
|
|
|
public class AppEbLoginController { |
|
|
|
|
|
|
|
|
|
private final IWeChatUserService weChatUserService; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 获取openid |
|
|
|
|
*/ |
|
|
|
|
@PostMapping("/login") |
|
|
|
|
public R<Map> login(@RequestBody WeChatPhone weChatPhone) { |
|
|
|
|
//小程序 appId = "wx432c2efe6df3b97a" appSecret = "859df8b167e74223e9237dee1b344524"
|
|
|
|
|
String appId = DictCache.getValue(DictEnum.WECHAT_APP, "appId"); |
|
|
|
|
String appSecret = DictCache.getValue(DictEnum.WECHAT_APP, "appSecret"); |
|
|
|
|
|
|
|
|
|
//小程序需要传来一个code
|
|
|
|
|
cn.hutool.json.JSONObject accessTokenJson = WeChatUtil.getCode2Session(weChatPhone.getCode(), appId, appSecret); |
|
|
|
|
String openid = accessTokenJson.get("openid", String.class); |
|
|
|
|
System.out.println("accessTokenJson:" + accessTokenJson.toString()); |
|
|
|
|
|
|
|
|
|
Map<String, Object> map = new HashMap<>(); |
|
|
|
|
map.put("openid", openid); |
|
|
|
|
map.put("userInfo", null); |
|
|
|
|
//根据openid获取本地用户信息
|
|
|
|
|
WeChatUser user = weChatUserService.getOne(Wrappers.<WeChatUser>lambdaQuery().eq(WeChatUser::getOpenId, openid)); |
|
|
|
|
if (user != null) { |
|
|
|
|
map.put("userInfo", user); |
|
|
|
|
BladeTokenEndPoint point = SpringUtil.getBean(BladeTokenEndPoint.class); |
|
|
|
|
} |
|
|
|
|
private final IWeChatUserService weChatUserService; |
|
|
|
|
|
|
|
|
|
private final IOrderService orderService; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 获取openid |
|
|
|
|
*/ |
|
|
|
|
@PostMapping("/login") |
|
|
|
|
public R<Map> login(@RequestBody WeChatPhone weChatPhone) { |
|
|
|
|
//小程序 appId = "wx432c2efe6df3b97a" appSecret = "859df8b167e74223e9237dee1b344524"
|
|
|
|
|
String appId = DictCache.getValue(DictEnum.WECHAT_APP, "appId"); |
|
|
|
|
String appSecret = DictCache.getValue(DictEnum.WECHAT_APP, "appSecret"); |
|
|
|
|
|
|
|
|
|
//小程序需要传来一个code
|
|
|
|
|
cn.hutool.json.JSONObject accessTokenJson = WeChatUtil.getCode2Session(weChatPhone.getCode(), appId, appSecret); |
|
|
|
|
String openid = accessTokenJson.get("openid", String.class); |
|
|
|
|
System.out.println("accessTokenJson:" + accessTokenJson.toString()); |
|
|
|
|
|
|
|
|
|
Map<String, Object> map = new HashMap<>(); |
|
|
|
|
map.put("openid", openid); |
|
|
|
|
map.put("userInfo", null); |
|
|
|
|
//根据openid获取本地用户信息
|
|
|
|
|
WeChatUser user = weChatUserService.getOne(Wrappers.<WeChatUser>lambdaQuery().eq(WeChatUser::getOpenId, openid)); |
|
|
|
|
if (user != null) { |
|
|
|
|
map.put("userInfo", user); |
|
|
|
|
BladeTokenEndPoint point = SpringUtil.getBean(BladeTokenEndPoint.class); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//这里可以对用户信息进行一些操作
|
|
|
|
|
return R.data(map); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 用前端请求接口获取的code换取用户手机号 |
|
|
|
|
* 前端需要请求的接口:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html
|
|
|
|
|
* |
|
|
|
|
* @param weChatPhone |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
@PostMapping("/phone") |
|
|
|
|
public R getPhone(@RequestBody WeChatPhone weChatPhone) { |
|
|
|
|
//小程序appId appSecret
|
|
|
|
|
String appId = DictCache.getValue(DictEnum.WECHAT_APP, "appId"); |
|
|
|
|
String appSecret = DictCache.getValue(DictEnum.WECHAT_APP, "appSecret"); |
|
|
|
|
|
|
|
|
|
// 1.请求微信接口服务,获取accessToken
|
|
|
|
|
cn.hutool.json.JSONObject accessTokenJson = WeChatUtil.getAccessToken(appId, appSecret); |
|
|
|
|
System.out.println("accessTokenJson:" + accessTokenJson); |
|
|
|
|
String accessToken = accessTokenJson.get("access_token", String.class); |
|
|
|
|
System.out.println("accessToken:" + accessToken); |
|
|
|
|
|
|
|
|
|
// 2.请求微信接口服务,获取用户手机号信息
|
|
|
|
|
if (StringUtils.isBlank(accessToken)) { |
|
|
|
|
throw new ServiceException("获取access_token失败"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
cn.hutool.json.JSONObject phoneNumberJson = WeChatUtil.getPhoneNumber(weChatPhone.getCode(), accessToken); |
|
|
|
|
System.out.println("phoneNumberJson:" + phoneNumberJson); |
|
|
|
|
WeChatPhoneInfo phoneInfo = phoneNumberJson.get("phone_info", WeChatPhoneInfo.class); |
|
|
|
|
System.out.println("phoneInfo:" + phoneInfo.toString()); |
|
|
|
|
return R.data(phoneInfo.getPurePhoneNumber()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//这里可以对用户信息进行一些操作
|
|
|
|
|
return R.data(map); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 用前端请求接口获取的code换取用户手机号 |
|
|
|
|
* 前端需要请求的接口:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html
|
|
|
|
|
* |
|
|
|
|
* @param weChatPhone |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
@PostMapping("/phone") |
|
|
|
|
public R getPhone(@RequestBody WeChatPhone weChatPhone) { |
|
|
|
|
//小程序appId appSecret
|
|
|
|
|
String appId = DictCache.getValue(DictEnum.WECHAT_APP, "appId"); |
|
|
|
|
String appSecret = DictCache.getValue(DictEnum.WECHAT_APP, "appSecret"); |
|
|
|
|
|
|
|
|
|
// 1.请求微信接口服务,获取accessToken
|
|
|
|
|
cn.hutool.json.JSONObject accessTokenJson = WeChatUtil.getAccessToken(appId, appSecret); |
|
|
|
|
System.out.println("accessTokenJson:" + accessTokenJson); |
|
|
|
|
String accessToken = accessTokenJson.get("access_token", String.class); |
|
|
|
|
System.out.println("accessToken:" + accessToken); |
|
|
|
|
|
|
|
|
|
// 2.请求微信接口服务,获取用户手机号信息
|
|
|
|
|
if (StringUtils.isBlank(accessToken)) { |
|
|
|
|
throw new ServiceException("获取access_token失败"); |
|
|
|
|
/** |
|
|
|
|
* 微信下单 |
|
|
|
|
* |
|
|
|
|
* @param openId |
|
|
|
|
*/ |
|
|
|
|
@PostMapping("/createOrder") |
|
|
|
|
public R createOrder(@RequestParam String openId, @RequestParam String outTradeNo) { |
|
|
|
|
// 根据订单号查询订单
|
|
|
|
|
Order order = orderService.getOne(Wrappers.<Order>lambdaQuery().eq(Order::getOrderNo, outTradeNo)); |
|
|
|
|
if (order == null) { |
|
|
|
|
throw new ServiceException("该订单不存在"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
cn.hutool.json.JSONObject phoneNumberJson = WeChatUtil.getPhoneNumber(weChatPhone.getCode(), accessToken); |
|
|
|
|
System.out.println("phoneNumberJson:" + phoneNumberJson); |
|
|
|
|
WeChatPhoneInfo phoneInfo = phoneNumberJson.get("phone_info", WeChatPhoneInfo.class); |
|
|
|
|
System.out.println("phoneInfo:" + phoneInfo.toString()); |
|
|
|
|
return R.data(phoneInfo.getPurePhoneNumber()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 微信下单 |
|
|
|
|
* @param openId |
|
|
|
|
*/ |
|
|
|
|
@PostMapping("/createOrder") |
|
|
|
|
public R createOrder(@RequestParam String openId,@RequestParam String outTradeNo) { |
|
|
|
|
return R.data(WxPayUtils.getPayParam(openId,outTradeNo)); |
|
|
|
|
} |
|
|
|
|
Double totalPrice = order.getTotalPrice() * 100; |
|
|
|
|
int total = totalPrice.intValue(); |
|
|
|
|
return R.data(WxPayUtils.getPayParam(openId, outTradeNo, total)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|