新增商户端订单通知(通过公众号)
This commit is contained in:
@ -0,0 +1,117 @@
|
||||
package co.yixiang.modules.customer.rest;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import co.yixiang.annotation.AnonymousAccess;
|
||||
import co.yixiang.constant.ShopConstants;
|
||||
import co.yixiang.exception.BadRequestException;
|
||||
import co.yixiang.modules.mp.config.WxMpConfiguration;
|
||||
import co.yixiang.utils.RecodeUtil;
|
||||
import co.yixiang.utils.RedisUtil;
|
||||
import co.yixiang.utils.RedisUtils;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import io.swagger.annotations.Api;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.mp.api.WxMpService;
|
||||
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
|
||||
import me.chanjar.weixin.mp.bean.result.WxMpUser;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.net.URLEncoder;
|
||||
|
||||
/**
|
||||
* @author lioncity
|
||||
* @date 2020-03-21
|
||||
*/
|
||||
@Slf4j
|
||||
@Api(tags = "微信")
|
||||
@Controller
|
||||
@RequestMapping("/api/wxmp")
|
||||
@AllArgsConstructor
|
||||
public class QrCodeController {
|
||||
|
||||
private final RedisUtils redisUtils;
|
||||
/**
|
||||
* 生成微信图片二维码
|
||||
*
|
||||
* @param request
|
||||
* @param response
|
||||
* @
|
||||
*/
|
||||
@AnonymousAccess
|
||||
@GetMapping("/qrcode")
|
||||
public void qrcode(HttpServletRequest request, HttpServletResponse response, @RequestParam("key") String key) {
|
||||
String adminApiUrl = redisUtils.getY(ShopConstants.ADMIN_API_URL);
|
||||
if(StrUtil.isBlank(adminApiUrl)){
|
||||
throw new BadRequestException("请配置后台-->商城配置-->商城系统配置-->后台Api地址");
|
||||
}
|
||||
final WxMpService wxService = WxMpConfiguration.getWxMpService();
|
||||
if (wxService == null) {
|
||||
throw new IllegalArgumentException("未找到对应配置的服务,请核实!");
|
||||
}
|
||||
String state = key;
|
||||
String url = adminApiUrl + "/api/wxmp/userInfo";
|
||||
String redirectURL = wxService.getOAuth2Service().buildAuthorizationUrl(url, WxConsts.OAuth2Scope.SNSAPI_USERINFO, URLEncoder.encode(state));
|
||||
log.info("【微信网页授权】获取code,redirectURL={}", redirectURL);
|
||||
//调用工具类,生成二维码
|
||||
//180为图片高度和宽度
|
||||
RecodeUtil.creatRrCode(redirectURL, 180, 180, response);
|
||||
}
|
||||
|
||||
@AnonymousAccess
|
||||
@GetMapping("/wechatCode")
|
||||
public ResponseEntity wechatCode() {
|
||||
String wechatFollowImg = redisUtils.getY(ShopConstants.WECHAT_FOLLOW_IMG);
|
||||
if(StrUtil.isBlank(wechatFollowImg)){
|
||||
throw new BadRequestException("请配置后台-->微信管理-->公众号配置->关注二维码");
|
||||
}
|
||||
return ResponseEntity.ok(wechatFollowImg);
|
||||
}
|
||||
|
||||
@AnonymousAccess
|
||||
@ResponseBody
|
||||
@GetMapping("/userInfo")
|
||||
public void userInfo(HttpServletRequest request, @RequestParam("code") String code,
|
||||
@RequestParam("state") String key) throws Exception {
|
||||
log.info("【微信网页授权】code={}", code);
|
||||
log.info("【微信网页授权】state={}", key);
|
||||
final WxMpService wxService = WxMpConfiguration.getWxMpService();
|
||||
if (wxService == null) {
|
||||
throw new IllegalArgumentException("未找到对应配置的服务,请核实!");
|
||||
}
|
||||
try {
|
||||
WxMpOAuth2AccessToken wxMpOAuth2AccessToken = wxService.getOAuth2Service().getAccessToken(code);
|
||||
WxMpUser wxMpUser = wxService.getOAuth2Service().getUserInfo(wxMpOAuth2AccessToken, "zh_CN");
|
||||
RedisUtil.set("qrCode:" + key, wxMpOAuth2AccessToken.getOpenId() + ":" + wxMpUser.getNickname());
|
||||
log.info("【微信网页授权】wxMpUser={}", wxMpUser);
|
||||
} catch (WxErrorException e) {
|
||||
log.info("【微信网页授权】{}", e);
|
||||
throw new Exception(e.getError().getErrorMsg());
|
||||
}
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@GetMapping("/getOpenId")
|
||||
public ResponseEntity userInfo(HttpServletRequest request, @RequestParam("key") String key) {
|
||||
String openId = RedisUtil.get("qrCode:" + key);
|
||||
if (openId != null) {
|
||||
String[] str = openId.split(":");
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("openId", str[0]);
|
||||
json.put("nickName", str[1]);
|
||||
return new ResponseEntity(json, HttpStatus.OK);
|
||||
}
|
||||
return new ResponseEntity(HttpStatus.OK);
|
||||
}
|
||||
}
|
@ -0,0 +1,89 @@
|
||||
/**
|
||||
* Copyright (C) 2018-2020
|
||||
* All rights reserved, Designed By www.yixiang.co
|
||||
* 注意:
|
||||
* 本软件为www.yixiang.co开发研制,未经购买不得使用
|
||||
* 购买后可获得全部源代码(禁止转卖、分享、上传到码云、github等开源平台)
|
||||
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
|
||||
*/
|
||||
package co.yixiang.modules.customer.rest;
|
||||
import java.util.Arrays;
|
||||
import co.yixiang.dozer.service.IGenerator;
|
||||
import co.yixiang.exception.BadRequestException;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import lombok.AllArgsConstructor;
|
||||
import co.yixiang.logging.aop.log.Log;
|
||||
import co.yixiang.modules.customer.domain.YxStoreCustomer;
|
||||
import co.yixiang.modules.customer.service.YxStoreCustomerService;
|
||||
import co.yixiang.modules.customer.service.dto.YxStoreCustomerQueryCriteria;
|
||||
import co.yixiang.modules.customer.service.dto.YxStoreCustomerDto;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import io.swagger.annotations.*;
|
||||
import java.io.IOException;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import co.yixiang.domain.PageResult;
|
||||
/**
|
||||
* @author Bug
|
||||
* @date 2020-12-10
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
@Api(tags = "customer管理")
|
||||
@RestController
|
||||
@RequestMapping("/api/yxStoreCustomer")
|
||||
public class YxStoreCustomerController {
|
||||
|
||||
private final YxStoreCustomerService yxStoreCustomerService;
|
||||
private final IGenerator generator;
|
||||
|
||||
|
||||
@Log("导出数据")
|
||||
@ApiOperation("导出数据")
|
||||
@GetMapping(value = "/download")
|
||||
@PreAuthorize("@el.check('admin','yxStoreCustomer:list')")
|
||||
public void download(HttpServletResponse response, YxStoreCustomerQueryCriteria criteria) throws IOException {
|
||||
yxStoreCustomerService.download(generator.convert(yxStoreCustomerService.queryAll(criteria), YxStoreCustomerDto.class), response);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
@Log("查询customer")
|
||||
@ApiOperation("查询customer")
|
||||
@PreAuthorize("@el.check('admin','yxStoreCustomer:list')")
|
||||
public ResponseEntity<PageResult<YxStoreCustomerDto>> getYxStoreCustomers(YxStoreCustomerQueryCriteria criteria, Pageable pageable){
|
||||
return new ResponseEntity<>(yxStoreCustomerService.queryAll(criteria,pageable),HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@Log("新增customer")
|
||||
@ApiOperation("新增customer")
|
||||
@PreAuthorize("@el.check('admin','yxStoreCustomer:add')")
|
||||
public ResponseEntity<Object> create(@Validated @RequestBody YxStoreCustomer resources){
|
||||
int count = yxStoreCustomerService.count(new LambdaQueryWrapper<YxStoreCustomer>().eq(YxStoreCustomer::getOpenId, resources.getOpenId()));
|
||||
if (count > 0) throw new BadRequestException("当前用户已存在,请勿重复提交");
|
||||
return new ResponseEntity<>(yxStoreCustomerService.save(resources),HttpStatus.CREATED);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
@Log("修改customer")
|
||||
@ApiOperation("修改customer")
|
||||
@PreAuthorize("@el.check('admin','yxStoreCustomer:edit')")
|
||||
public ResponseEntity<Object> update(@Validated @RequestBody YxStoreCustomer resources){
|
||||
yxStoreCustomerService.updateById(resources);
|
||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||
}
|
||||
|
||||
@Log("删除customer")
|
||||
@ApiOperation("删除customer")
|
||||
@PreAuthorize("@el.check('admin','yxStoreCustomer:del')")
|
||||
@DeleteMapping
|
||||
public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
|
||||
Arrays.asList(ids).forEach(id->{
|
||||
yxStoreCustomerService.removeById(id);
|
||||
});
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user