WeixinController.java 10.7 KB
Newer Older
苗卫卫 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
package com.boco.nbd.wios.wx.controller;

import cn.hutool.core.lang.Assert;
import com.boco.nbd.cams.core.config.RedisClient;
import com.boco.nbd.wios.downloadfile.service.FileSupportService;
import com.boco.nbd.wios.manage.contants.WiosConstant;
import com.boco.nbd.wios.manage.entity.settlement.vo.InvoiceVo;
import com.boco.nbd.wios.manage.entity.bo.OrderVo;
import com.boco.nbd.wios.manage.entity.bo.UserLoginInfoVO;
import com.boco.nbd.wios.manage.service.impl.InvoiceService;
import com.boco.nbd.wios.manage.service.impl.OrderService;
import com.boco.nbd.wios.manage.service.impl.PushService;
import com.boco.nbd.wios.manage.service.impl.UserService;
import com.boco.nbd.wios.manage.util.NetUtil;
import com.boco.nbd.wios.manage.util.NumberUtil;
import com.boco.nbd.wios.manage.util.SpringContextUtil;
import com.boco.nbd.wios.wx.permisson.WxPermission;
import com.boco.nbd.wios.wx.service.WxOrderService;
import com.boco.nbd.wios.wx.vo.WxBillVO;
import com.boco.nbd.wios.wx.vo.WxInvoiceVO;
import com.boco.nbd.wios.wx.vo.WxOrderVO;
import com.boco.nbd.wios.wx.weixin.WeixinHelper;
import com.boco.nbd.wios.wx.weixin.WeixinSignUtils;
import com.ihidea.component.api.v2.BaseResponse;
import com.ihidea.core.support.exception.ServiceException;
import com.ihidea.core.util.StringUtilsEx;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author kevin
 * @create 2020/10/28 20:59
 */
@Api(tags = "微信公众号相关接口")
@WxPermission
@RestController
@RequestMapping("/wx")
@ApiIgnore
public class WeixinController {

    private static final Logger logger= LoggerFactory.getLogger(WeixinController.class);

    private static final String USER_LOGIN_CODE_CACHE_KEY="userLoginCode:";

    @Autowired
    private RedisClient redisClient;

    @Autowired
    private PushService pushService;

    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private WxOrderService wxOrderService;

    @Autowired
    private InvoiceService invoiceService;

    @Autowired
    private FileSupportService fileSupportService;


    @WxPermission(require = false)
    @GetMapping(value = "/getOpenIdByCode")
    @ApiOperation(value = "根据code获取openId")
    @ApiImplicitParams({@ApiImplicitParam(name = "code", value = "微信code", dataType = "String", paramType = "query", required = true)})
    public BaseResponse<String> getOpenIdByCode(String code) {
        String openId = null;
        if(StringUtils.isNotBlank(code)){
            openId = WeixinHelper.getOpenIdByCode(code);
        }
        return new BaseResponse<>(openId);
    }

    @GetMapping(value = "/getWxPrams")
    @ApiOperation(value = "获取微信签名参数")
    public BaseResponse<Object> getWxPrams() {
        return new BaseResponse<>(WeixinSignUtils.getParam());
    }

    private String checkSendPhoneMsgCode(String phone) {
        String ip= NetUtil.getIpAddr(SpringContextUtil.getRequest());
        String checkCacheKey="sendPhoneMsgCode:";

        //防止IP频繁请求(不能设置太长,存在同IP不同账户发送)
        if(!redisClient.getLock(checkCacheKey+"ip:"+ip, 30L)) {
            throw new ServiceException("请求频繁,请稍后");
        }

        // 防止手机号频繁发送请求
        if(!redisClient.getLock(checkCacheKey+"phone:"+phone, 60L)) {
            throw new ServiceException("请求频繁,请稍后");
        }

        return ip;
    }

    /**
     * 验证码的获取
     * @param phone
     * @throws Exception
     */
    @WxPermission(require = false)
    @ApiOperation("获取短信验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "captcha", value = "图形验证码", dataType = "String", paramType = "query", required = false)
    })
    @PostMapping("/getPhoneMsgCode")
    public BaseResponse getCode(String phone, String captcha) throws Exception{

        logger.debug("请求短信验证码,手机号:"+phone);

        Assert.notBlank(phone, "手机号不能为空");

        checkSendPhoneMsgCode(phone);

        int code = NumberUtil.getRandomNum(1000, 9999);
        logger.debug("发送短信验证码,手机号:"+phone+","+code);
        pushService.sendUserLoginCode(phone, Integer.toString(code));
        // 20分钟内有效
        redisClient.put(USER_LOGIN_CODE_CACHE_KEY+phone, String.valueOf(code), 1200);

        return new BaseResponse();
    }

    @WxPermission(require = false)
    @ApiOperation("用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "smsCode", value = "短信验证码", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "openId", value = "微信openId", dataType = "String", paramType = "query", required = false)
    })
    @PostMapping("/login")
    public BaseResponse<UserLoginInfoVO> login(String phone, String smsCode, String openId){
        Assert.notBlank(phone, "手机号不能为空");
        Assert.notBlank(smsCode, "验证码不能为空");
        Assert.isTrue(StringUtils.equals(smsCode, redisClient.get(USER_LOGIN_CODE_CACHE_KEY+phone, String.class)),"验证码错误");

        UserLoginInfoVO userInfo = userService.login(phone, openId);
        redisClient.remove(USER_LOGIN_CODE_CACHE_KEY+phone);
        return new BaseResponse(userInfo);
    }

    @ApiOperation("用户登出")
    @PostMapping("/logout")
    public BaseResponse logout(){
        userService.logout();
        return new BaseResponse();
    }

    @ApiOperation("工单数量信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = true),
    })
    @GetMapping("/orderCountInfo")
    public BaseResponse userInfo(String phone){
        // 查询客户总工单数
        int orderCnt=orderService.countByCamsPhone(phone);
        String orderId="";
        if(orderCnt == 1){
            List<OrderVo> orderList=orderService.selectByCamsPhone(phone);
            if(CollectionUtils.isNotEmpty(orderList)){
                orderId = orderList.get(0).getId();
            }
        }

        Map<String, Object> result =new HashMap<>(2);
        result.put("orderCnt", orderCnt);
        result.put("orderId", orderId);

        return new BaseResponse(result);
    }

    @ApiOperation("安装工单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = true),
    })
    @GetMapping("/orderList")
    public BaseResponse<List<WxOrderVO>> orderList(String phone){
        return new BaseResponse(wxOrderService.getList(phone));
    }

    @ApiOperation("安装工单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "工单编号", dataType = "String", paramType = "query", required = true),
    })
    @GetMapping("/orderDetail")
    public BaseResponse<WxOrderVO> orderDetail(String orderId){
        Assert.notBlank(orderId, "安装工单编号不能为空");
        return new BaseResponse(wxOrderService.getDetail(orderId));
    }



    @ApiOperation("安装订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = true),
    })
    @GetMapping("/billList")
    public BaseResponse<List<WxBillVO>> billList(String phone){
        return new BaseResponse(wxOrderService.getBillList(phone));
    }

    @ApiOperation("安装订单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "安装工单编号", dataType = "String", paramType = "query", required = true),
    })
    @GetMapping("/billDetail")
    public BaseResponse<WxBillVO> billDetail(String orderId){
        Assert.notBlank(orderId, "安装工单编号不能为空");
        return new BaseResponse(wxOrderService.getBillDetail(orderId));
    }


    @ApiOperation("安装订单支付")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "工单编号", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "openId", value = "微信openId", dataType = "String", paramType = "query", required = true),
    })
    @GetMapping("/billPay")
    public BaseResponse billPay(String orderId, String openId){
        return new BaseResponse(wxOrderService.billPay(orderId, openId));
    }

    @ApiOperation("安装订单发票申请")
    @PostMapping("/billInvoiceApply")
    public BaseResponse billInvoiceApply(WxInvoiceVO wxInvoiceVO) throws Exception{
        invoiceService.applyInvoice(wxInvoiceVO);
        return new BaseResponse();
    }

    @ApiOperation("安装订单发票详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "billId", value = "工单账单编号", dataType = "String", paramType = "query", required = true),
    })
    @GetMapping("/billInvoiceDetail")
    public BaseResponse<WxInvoiceVO> billInvoiceDetail(String billId) throws Exception{
        Assert.notBlank(billId, "工单账单编号不能为空");
        InvoiceVo invoice = invoiceService.getInvoiceByBillId(billId);
        return new BaseResponse(invoice);
    }

    @ApiOperation("上传微信图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mediaId", value = "多媒体编号,使用微信接口选择图片并上传到微信服务器后的serverId", dataType = "String", paramType = "query", required = true),
    })
    @GetMapping("/uploadImg")
    public BaseResponse<WxInvoiceVO> uploadImg(String mediaId) throws Exception{
        // 从微信服务器中下载图片
        byte[] content=WeixinHelper.getMediaContent(mediaId);
        // 将图片上传到OSS或本地图片服务器
        String fileId=fileSupportService.add(StringUtilsEx.getUUID() + ".jpg" , content, WiosConstant.OSS_DATA_STORE_NAME);
        return new BaseResponse(fileId);
    }

}