Kaynağa Gözat

小程序端接口

Althars123 3 yıl önce
ebeveyn
işleme
a140f6a9ed

+ 2 - 26
twzd-admin/src/main/java/com/miaxis/app/controller/wx/WxController.java

@@ -9,7 +9,6 @@ import com.miaxis.common.core.controller.BaseController;
 import com.miaxis.common.core.domain.Response;
 import com.miaxis.common.core.domain.entity.SysDictData;
 import com.miaxis.common.core.domain.entity.UserInfo;
-import com.miaxis.common.enums.OrderStatusEnum;
 import com.miaxis.common.exception.CustomException;
 import com.miaxis.common.utils.SecurityUtils;
 import com.miaxis.common.utils.uuid.CommonUtils;
@@ -106,37 +105,14 @@ public class WxController extends BaseController {
         String orderCode = getOrderCode(student.getId());
         order.setOutTradeNo(orderCode);
         order.setOpenid(student.getOpenid());
-        order.setOrderStatus(OrderStatusEnum.PROCESSING.getCode());
+        order.setUnionId(student.getUnionId());
         Double dprice = Double.valueOf(values[0]);
         Double v =dprice* 100;
         order.setTotal(v.intValue());
-        order.setOrderDataJson(values[2]);
+//        order.setOrderDataJson(values[2]);
         return order;
     }
 
-    /**
-     * 微信支付获取预订单id
-     */
-    @PostMapping(value = "/prepareOrder-test")
-    @ApiOperation("微信支付下单-测试")
-    public Response<JSONObject> getPrepareOrderTest() throws Exception{
-
-
-        //创建本地微信订单
-        WxOrder order = new WxOrder();
-        UserInfo student = SecurityUtils.getLoginUser().getStudent();
-        String orderCode = getOrderCode(student.getId());
-        order.setOutTradeNo(orderCode);
-        order.setOpenid(student.getOpenid());
-        order.setGoodsType("1");
-        wxOrderService.createVipOrder(order);
-        //wxOrderService.save(order);
-        return Response.success(placeWxOrder(order, "学车vip充值", "http://jpcj-h5.zzxcx.net.zzxcx.net/prod-api/open-api/wx/notify/wxpay1"));
-
-
-    }
-
-
     //下单
     private JSONObject placeWxOrder(WxOrder order, String goodsName, String notifyUrl) throws Exception {
         HttpPost httpPost = initHttpPost();

+ 65 - 0
twzd-admin/src/main/java/com/miaxis/app/controller/wx/XcXUserInfoController.java

@@ -0,0 +1,65 @@
+package com.miaxis.app.controller.wx;
+
+import com.miaxis.common.constant.Constants;
+import com.miaxis.common.core.controller.BaseController;
+import com.miaxis.common.core.domain.Response;
+import com.miaxis.common.core.domain.entity.UserInfo;
+import com.miaxis.common.utils.SecurityUtils;
+import com.miaxis.common.utils.StringUtils;
+import com.miaxis.user.dto.UserInfoUpdateDto;
+import com.miaxis.user.service.IUserInfoService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+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;
+
+
+@RestController
+@RequiredArgsConstructor
+@RequestMapping(Constants.STUDENT_PREFIX+"/xcx/user")
+@Api(tags = {"【小程序-用户信息】"})
+@Slf4j
+public class XcXUserInfoController extends BaseController {
+
+    @Autowired
+    private IUserInfoService userInfoService;
+
+
+
+    /**
+     * 修改用户信息
+     */
+    @PostMapping
+    @ApiOperation("修改用户信息")
+    public Response edit(@RequestBody UserInfoUpdateDto userInfoUpdateDto){
+
+        UserInfo userInfo= SecurityUtils.getLoginUser().getStudent();
+        if (userInfo!= null){
+            if (!StringUtils.isEmpty(userInfoUpdateDto.getHeadImage())){
+                userInfo.setHeadImage(userInfoUpdateDto.getHeadImage());
+            }
+            if (!StringUtils.isEmpty(userInfoUpdateDto.getNickName())){
+                userInfo.setNickName(userInfoUpdateDto.getNickName());
+            }
+        }
+        //更新数据库
+        userInfoService.updateById(userInfo);
+        return Response.success("更新成功");
+    }
+
+
+
+
+
+
+
+
+
+
+
+}

+ 191 - 0
twzd-admin/src/main/java/com/miaxis/app/controller/wx/XcxWxController.java

@@ -0,0 +1,191 @@
+package com.miaxis.app.controller.wx;
+
+import com.alibaba.fastjson.JSONObject;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.miaxis.common.config.WxpayConfig;
+import com.miaxis.common.constant.Constants;
+import com.miaxis.common.core.controller.BaseController;
+import com.miaxis.common.core.domain.Response;
+import com.miaxis.common.core.domain.entity.SysDictData;
+import com.miaxis.common.core.domain.entity.UserInfo;
+import com.miaxis.common.exception.CustomException;
+import com.miaxis.common.utils.SecurityUtils;
+import com.miaxis.common.utils.uuid.CommonUtils;
+import com.miaxis.system.service.ISysDictDataService;
+import com.miaxis.wx.domain.WxOrder;
+import com.miaxis.wx.dto.WxOrderDTO;
+import com.miaxis.wx.service.IWxOrderService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang.RandomStringUtils;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.util.EntityUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.core.io.ClassPathResource;
+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.io.ByteArrayOutputStream;
+import java.io.File;
+import java.security.PrivateKey;
+import java.security.Signature;
+import java.util.Base64;
+
+import static com.miaxis.common.utils.OrderCodeFactory.getOrderCode;
+
+/**
+ * 【小程序-微信支付】Controller
+ *
+ * @author miaxis
+ * @date 2021-03-10
+ */
+@RestController
+@RequiredArgsConstructor
+@RequestMapping(Constants.STUDENT_PREFIX+"/xcx/wx")
+@Api(tags = {"【小程序-微信支付】"})
+@Slf4j
+public class XcxWxController extends BaseController {
+
+    @Autowired
+    private HttpClient httpClient;
+
+    @Autowired
+    private WxpayConfig wxpayConfig;
+
+
+    @Autowired
+    private ISysDictDataService dictDataService;
+
+
+    @Autowired
+    private IWxOrderService wxOrderService;
+
+
+    @Value("${xcx.appId}")
+    private String appId;
+    @Value("${wxpay.xcxNotifyUrl}")
+    private  String xcxNotifyUrl ;
+
+
+
+    /**
+     * 微信支付获取预订单id
+     */
+    @PostMapping(value = "/prepareOrder")
+    @ApiOperation("微信支付下单")
+    public Response<JSONObject> getXcxPrepareOrder(@RequestBody WxOrderDTO wxOrderDTO) throws Exception{
+
+
+        SysDictData sysDictData = dictDataService.selectDictDataById(wxOrderDTO.getDictCode());
+        String[] values = sysDictData.getDictValue().split(",");
+        //创建本地微信订单
+        WxOrder order = createOrder(values);
+        wxOrderService.save(order);
+        return Response.success(placeWxOrder(order, sysDictData.getDictLabel(),xcxNotifyUrl));
+
+
+    }
+
+    private WxOrder createOrder(String[] values) {
+        WxOrder order = new WxOrder();
+        UserInfo student = SecurityUtils.getLoginUser().getStudent();
+        String orderCode = getOrderCode(student.getId());
+        order.setOutTradeNo(orderCode);
+        order.setXcxOpenid(student.getXcxOpenid());
+        order.setUnionId(student.getUnionId());
+        Double dprice = Double.valueOf(values[0]);
+        Double v =dprice* 100;
+        order.setTotal(v.intValue());
+        //order.setOrderDataJson(values[2]);
+        return order;
+    }
+
+    //下单
+    private JSONObject placeWxOrder(WxOrder order, String goodsName, String notifyUrl) throws Exception {
+        HttpPost httpPost = initHttpPost();
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        ObjectMapper objectMapper = new ObjectMapper();
+        ObjectNode rootNode = objectMapper.createObjectNode();
+        rootNode.put("mchid",wxpayConfig.getMerchantId())
+                .put("appid", appId)
+                .put("description", goodsName)
+                .put("notify_url", notifyUrl)
+                .put("out_trade_no", order.getOutTradeNo());
+        rootNode.putObject("amount")
+                .put("total", order.getTotal());
+        rootNode.putObject("payer")
+                .put("openid", order.getXcxOpenid());
+        objectMapper.writeValue(bos, rootNode);
+        httpPost.setEntity(new StringEntity(rootNode.toString(), "utf-8"));
+        HttpResponse response = httpClient.execute(httpPost);
+        String bodyAsString = EntityUtils.toString(response.getEntity());
+        if (JSONObject.parseObject(bodyAsString).get("prepay_id") == null){
+            throw new CustomException(JSONObject.parseObject(bodyAsString).get("message").toString());
+        }
+        return getWxParamJson(bodyAsString,order.getOutTradeNo());
+
+    }
+
+    // 生成调用调起微信支付所需参数
+    private JSONObject getWxParamJson(String bodyAsString, String orderCode) throws Exception {
+        String  packageStr = "prepay_id="+JSONObject.parseObject(bodyAsString).get("prepay_id");
+        JSONObject jsonObject = new JSONObject();
+        jsonObject.put("package",packageStr);
+        String nonce_str = RandomStringUtils.randomAlphanumeric(32);
+        jsonObject.put("nonceStr",nonce_str);
+        long timestamp =  System.currentTimeMillis()/1000;
+        jsonObject.put("timeStamp",String.valueOf(timestamp));
+        jsonObject.put("signType","RSA");
+        StringBuffer sb = new StringBuffer();
+        sb.append(appId + "\n");
+        sb.append(timestamp + "\n");
+        sb.append(nonce_str + "\n");
+        sb.append(packageStr+ "\n");
+        System.out.println(sb);
+
+        File file = new ClassPathResource("wechatpay/apiclient_key.pem").getFile();
+        String realPath =file.getAbsolutePath();
+        PrivateKey privateKey = CommonUtils.getPrivateKey(realPath);
+        // 进行签名服务
+        Signature signature = Signature.getInstance("SHA256withRSA");
+        signature.initSign(privateKey);
+        signature.update(sb.toString().getBytes("UTF-8"));
+        byte[] signedData = signature.sign();
+        String base64Str =  Base64.getEncoder().encodeToString(signedData);
+        jsonObject.put("paySign",base64Str);
+        jsonObject.put("outTradeNo",orderCode);
+        return jsonObject;
+    }
+
+
+
+
+
+
+    private HttpPost initHttpPost() {
+        HttpPost httpPost = new HttpPost(wxpayConfig.getV3url());
+        httpPost.addHeader("Accept", "application/json");
+        httpPost.addHeader("Content-type","application/json; charset=utf-8");
+        return httpPost;
+    }
+
+
+
+
+
+
+
+
+
+
+
+}

+ 2 - 0
twzd-admin/src/main/resources/application-dev.yml

@@ -99,6 +99,8 @@ wxpay:
     v3key: 8604f2a6eb6338cfa64e7df4ec2c08b3
     notifyUrl: http://1.15.26.233:8080/twzd-admin/open-api/wx/notify/wxpay
     notifyUrlRefund: http://1.15.26.233:8080/twzd-admin/open-api/wx/notify/refund
+    xcxNotifyUrl: http://1.15.26.233:8080/twzd-admin/open-api/wx/notify/wxpay
+    xcxNotifyUrlRefund: http://1.15.26.233:8080/twzd-admin/open-api/wx/notify/refund
     appId: wx67ca1b8c9816ef28
 
 # 腾讯cos

+ 2 - 0
twzd-admin/src/main/resources/application-prod.yml

@@ -97,6 +97,8 @@ wxpay:
     v3key: 8604f2a6eb6338cfa64e7df4ec2c08b3
     notifyUrl: http://jpcj-h5.zzxcx.net/prod-api/open-api/wx/notify/wxpay
     notifyUrlRefund: http://jpcj-h5.zzxcx.net/prod-api/open-api/wx/notify/refund
+    xcxNotifyUrl: http://jpcj-h5.zzxcx.net/prod-api/open-api/wx/notify/wxpay
+    xcxNotifyUrlRefund: http://jpcj-h5.zzxcx.net/prod-api/open-api/wx/notify/refund
 
 
 # 腾讯cos

+ 2 - 0
twzd-admin/src/main/resources/application-prodtest.yml

@@ -97,6 +97,8 @@ wxpay:
     v3key: 8604f2a6eb6338cfa64e7df4ec2c08b3
     notifyUrl: http://jpcj-h5.zzxcx.net/prod-api/open-api/wx/notify/wxpay
     notifyUrlRefund: http://jpcj-h5.zzxcx.net/prod-api/open-api/wx/notify/refund
+    xcxNotifyUrl: http://jpcj-h5.zzxcx.net/prod-api/open-api/wx/notify/wxpay
+    xcxNotifyUrlRefund: http://jpcj-h5.zzxcx.net/prod-api/open-api/wx/notify/refund
 
 
 # 腾讯cos

+ 28 - 0
twzd-service/src/main/java/com/miaxis/user/dto/UserInfoUpdateDto.java

@@ -0,0 +1,28 @@
+package com.miaxis.user.dto;
+
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.miaxis.common.annotation.Excel;
+import com.miaxis.common.core.domain.BaseBusinessEntity;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+/**
+ * @Author wwl
+ * @Date 2020/12/24
+ * @Version 1.0
+ */
+@Data
+public class UserInfoUpdateDto extends BaseBusinessEntity {
+    /** 小程序头像地址 */
+    @Excel(name = "小程序头像地址")
+    @TableField("head_image")
+    @ApiModelProperty(value = "小程序头像地址")
+    private String headImage;
+
+    /** 小程序昵称 */
+    @Excel(name = "小程序昵称")
+    @TableField("nick_name")
+    @ApiModelProperty(value = "小程序昵称")
+    private String nickName;
+
+}

+ 13 - 0
twzd-service/src/main/java/com/miaxis/wx/domain/WxOrder.java

@@ -104,6 +104,19 @@ public class WxOrder extends BaseBusinessEntity {
     @ApiModelProperty(value = "用户在直连商户appid下的唯一标识")
     private String openid;
 
+    /** 驾校班型Id */
+    @Excel(name = "联合id")
+    @TableField("union_id")
+    @ApiModelProperty(value = "联合id")
+    private String unionId;
+
+    /** 微信openid */
+    @Excel(name = "小程序openid")
+    @TableField("xcx_openid")
+    @ApiModelProperty(value = "小程序openid")
+    private String xcxOpenid;
+
+
     /** 商品名称 */
     @Excel(name = "商品类型")
     @TableField("goods_type")