Browse Source

新增小程序绑定手机号

赵冬冬 4 years ago
parent
commit
c6c71d75b7

+ 31 - 8
carbon-h5/carbon-h5-service/src/main/java/com/hcloud/microserver/h5/controller/web/WechatAppController.java

@@ -4,6 +4,8 @@ import cn.binarywang.wx.miniapp.api.WxMaService;
 import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
 import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
 import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
 import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
 import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
 import com.github.binarywang.wxpay.constant.WxPayConstants;
@@ -22,6 +24,11 @@ import com.hcloud.microserver.commoncore.util.*;
 import com.hcloud.microserver.h5.facade.carbon.entity.CustomerInfo;
 import com.hcloud.microserver.h5.facade.carbon.forms.CustomerInfoForm;
 import com.hcloud.microserver.h5.facade.carbon.vo.MyWxMpUser;
+import com.hcloud.microserver.h5.service.CustomerService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiImplicitParam;
+import io.swagger.annotations.ApiImplicitParams;
+import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
 import me.chanjar.weixin.common.error.WxErrorException;
 import org.apache.commons.lang.StringUtils;
@@ -48,6 +55,7 @@ import java.util.concurrent.TimeUnit;
 @Slf4j
 @Controller
 @RequestMapping("/wechat/app")
+@Api(description = "微信小程序api服务")
 public class WechatAppController extends BaseController {
     private final Logger logger = LoggerFactory.getLogger(this.getClass());
 
@@ -58,9 +66,11 @@ public class WechatAppController extends BaseController {
     @Autowired
     private CustomerInfoService customerInfoService;
 
+
     /**
      * 登陆接口
      */
+    @ApiOperation(value = "微信登录", notes = "微信登录")
     @PostMapping("/login")
     @ResponseBody
     public ResponseBase login(@RequestBody LoginRequest request) {
@@ -76,8 +86,7 @@ public class WechatAppController extends BaseController {
             }
             // 解密用户信息
             WxMaUserInfo wxUserInfo = wxMaService.getUserService().getUserInfo(session.getSessionKey(), request.getEncryptedData(), request.getIv());
-            //解密手机号
-            WxMaPhoneNumberInfo phoneNoInfo = wxMaService.getUserService().getPhoneNoInfo(session.getSessionKey(), request.getEncryptedData(), request.getIv());
+
             if (null == wxUserInfo) {
                 throw new RuntimeException("wxUser not exist");
             }
@@ -88,7 +97,6 @@ public class WechatAppController extends BaseController {
             myWxMpUser.setNickname(wxUserInfo.getNickName());
             myWxMpUser.setHeadImgUrl(wxUserInfo.getAvatarUrl());
             myWxMpUser.setSex(Integer.parseInt(wxUserInfo.getGender()));
-            myWxMpUser.setPhone(phoneNoInfo.getPhoneNumber());
             //后台新增用户成功
             Map<String, String> tokenMap = customerInfoService.wxlogin(myWxMpUser);
             //登录系统获取本地
@@ -99,16 +107,31 @@ public class WechatAppController extends BaseController {
         }
     }
 
+    @Autowired
+    private CustomerService customerService;
+
     /**
      * 登陆接口
      */
-    @PostMapping("/login/test")
+    @ApiOperation(value = "小程序绑定手机号", notes = "小程序绑定手机号")
+    @PostMapping("/login/phone")
     @ResponseBody
-    public ResponseBase login(@RequestBody MyWxMpUser myWxMpUser) {
-        //后台新增用户成功
-        Map<String, String> tokenMap = customerInfoService.wxlogin(myWxMpUser);
+    public ResponseBase loginphone(@RequestBody LoginRequest request) throws WxErrorException {
+        // 获取微信用户session
+        WxMaJscode2SessionResult session = wxMaService.getUserService().getSessionInfo(request.getCode());
+        //解密手机号
+        WxMaPhoneNumberInfo phoneNoInfo = wxMaService.getUserService().getPhoneNoInfo(session.getSessionKey(), request.getEncryptedData(), request.getIv());
+
+        CustomerInfo customerInfo = new CustomerInfo();
+        CustomerInfoForm tokenUser = customerService.getCurrentMember();
+        customerInfo.setGuid(tokenUser.getGuid());
+        customerInfo.setPhone(phoneNoInfo.getPhoneNumber());
+        int i = customerInfoService.updateByPrimaryKeySelective(customerInfo);
+        if (i == 0) {
+            return resultVOError();
+        }
         //登录系统获取本地
-        return resultVOSuccess(tokenMap);
+        return resultVOSuccess();
     }
 
     private Map<String, Object> getStringObjectMap(String customerId) {

+ 1 - 5
carbon-h5/carbon-h5-service/src/main/java/com/hcloud/microserver/h5/controller/web/WechatAppPayController.java

@@ -34,6 +34,7 @@ import java.math.BigDecimal;
 @Slf4j
 @Controller
 @RequestMapping("/wechat/pay")
+@Api(description = "微信小程序支付服务")
 public class WechatAppPayController extends BaseController {
 
 
@@ -51,10 +52,6 @@ public class WechatAppPayController extends BaseController {
     private String wxpayCallbackUrl;
 
     @ApiOperation(value = "微信支付接口(二次支付)", notes = "微信支付接口请勿随意调用(二次支付)")
-    @ApiImplicitParams({
-            @ApiImplicitParam(name = "orderId", value = "订单编号", dataType = "String", paramType = "query"),
-            @ApiImplicitParam(name = "tradeType", value = "交易方式(NATIVE,APP,JSAPI,MWEB)", dataType = "String", paramType = "query")
-    })
     @ApiResponse(code = 500, message = "请求的配置参数有问题")
     @AuthCarbonValidate
     @PostMapping("/pay")
@@ -122,7 +119,6 @@ public class WechatAppPayController extends BaseController {
 
 
     @ApiOperation(value = "微信支付结果回调", notes = "微信支付结果回调请勿直接调用")
-    @ApiResponse(code = 500, message = "请求的配置参数有问题")
     @AuthCarbonValidate
     @PostMapping("/payResultNotified")
     public void payOrderResultNotify(HttpServletRequest request, HttpServletResponse response) throws IOException {

+ 9 - 1
carbon-h5/carbon-h5-service/src/main/java/com/hcloud/microserver/h5/entity/LoginRequest.java

@@ -1,21 +1,29 @@
 package com.hcloud.microserver.h5.entity;
 
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 
 @Data
+@ApiModel("小程序提交参数")
 public class LoginRequest {
     //用户登录凭证
+    @ApiModelProperty("用户登录凭证")
     String code;
 
     //原始数据字符串
+    @ApiModelProperty("原始数据字符串")
     String signature;
 
     //加密用户数据
+    @ApiModelProperty("加密用户数据")
     String encryptedData;
 
     //加密算法的初始向量
+    @ApiModelProperty("加密算法的初始向量")
     String iv;
 
     //校验用户信息字符串
-    Object rawData;
+    String rawData;
+
 }

+ 6 - 1
carbon-h5/carbon-h5-service/src/main/java/com/hcloud/microserver/h5/entity/WxPayFrom.java

@@ -1,14 +1,19 @@
 package com.hcloud.microserver.h5.entity;
 
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 
 @Data
+@ApiModel("微信支付参数")
 public class WxPayFrom {
-
+    @ApiModelProperty("订单id")
     private String orderId;
 
+    @ApiModelProperty("微信openid")
     private String openid;
 
+    @ApiModelProperty("是否开启测试一分钱 test一分钱 空 原价支付")
     private String tradeType;
 
 }

+ 4 - 0
carbon-h5/carbon-h5-service/src/main/java/com/hcloud/microserver/h5/service/CustomerInfoService.java

@@ -2,6 +2,7 @@ package com.hcloud.microserver.h5.service;
 
 import com.github.pagehelper.PageInfo;
 import com.hcloud.microserver.commoncore.base.BaseServie;
+import com.hcloud.microserver.h5.facade.carbon.entity.CustomerInfo;
 import com.hcloud.microserver.h5.facade.carbon.forms.CustomerInfoForm;
 import com.hcloud.microserver.h5.facade.carbon.vo.MyWxMpUser;
 
@@ -88,4 +89,7 @@ public interface CustomerInfoService extends BaseServie<CustomerInfoForm,String>
 
 
     Map<String, String> wxlogin(MyWxMpUser wxMpUser);
+
+
+    int updateByPrimaryKeySelective(CustomerInfo customerInfo);
 }

+ 7 - 0
carbon-h5/carbon-h5-service/src/main/java/com/hcloud/microserver/h5/service/impl/CustomerInfoServiceImpl.java

@@ -440,5 +440,12 @@ public class CustomerInfoServiceImpl implements CustomerInfoService {
         return customerInfoMapper.updateByPrimaryKey(customerInfo);
     }
 
+    @Override
+    public int updateByPrimaryKeySelective(CustomerInfo customerInfo) {
+       return customerInfoMapper.updateByPrimaryKeySelective(customerInfo);
+    }
+
+
+
 
 }

+ 1 - 1
carbon-h5/carbon-h5-service/src/main/java/com/hcloud/microserver/h5/service/impl/OrderInfoServiceImpl.java

@@ -526,7 +526,7 @@ public class OrderInfoServiceImpl implements OrderInfoService {
             throw new RuntimeException("获取登陆信息失败");
         }
         if(customerInfo.getCustomerType() != 1){
-            throw new RuntimeException("无法购买商品不是自然人");
+            throw new RuntimeException("不是自然人无法购买商品!");
         }
         String customerId = customerInfo.getGuid();
 

+ 38 - 0
common-core/src/main/java/com/hcloud/microserver/commoncore/base/BaseController.java

@@ -147,6 +147,44 @@ public class BaseController {
         return responseBase;
     }
 
+    /**
+     * 操作成功后,返回结果(前后端分离)
+     *
+     * @param
+     * @return
+     */
+    protected ResponseBase resultVOError() {
+        ResultVO resultVO=new ResultVO();
+        resultVO.setCode(1);
+        resultVO.setMsg("操作失败!");
+        ResponseBase responseBase = new ResponseBase(resultVO.getData());
+        RetHead retHead = new RetHead();
+        retHead.setErrCode(resultVO.getCode().shortValue());
+        retHead.setErrMsg(resultVO.getMsg());
+        retHead.setTotal(resultVO.getCount());
+        responseBase.setRetHead(retHead);
+        return responseBase;
+    }
+
+    /**
+     * 操作成功后,返回结果(前后端分离)
+     *
+     * @param
+     * @return
+     */
+    protected ResponseBase resultVOError(String msg) {
+        ResultVO resultVO=new ResultVO();
+        resultVO.setCode(1);
+        resultVO.setMsg(msg);
+        ResponseBase responseBase = new ResponseBase(resultVO.getData());
+        RetHead retHead = new RetHead();
+        retHead.setErrCode(resultVO.getCode().shortValue());
+        retHead.setErrMsg(resultVO.getMsg());
+        retHead.setTotal(resultVO.getCount());
+        responseBase.setRetHead(retHead);
+        return responseBase;
+    }
+
     /**
      * 操作成功后,返回结果(前后端分离)
      *