doum
2026-06-16 77094dd01f0c6ff59b4fb4fa1105addf34b2398c
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
package com.doumee.cloud.web;
 
import com.doumee.api.BaseController;
import com.doumee.config.annotation.LoginNoRequired;
import com.doumee.core.annotation.pr.PreventRepeat;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.ApiResponse;
import com.doumee.core.model.LoginUserInfo;
import com.doumee.core.model.PageData;
import com.doumee.core.model.PageWrap;
import com.doumee.core.utils.Constants;
import com.doumee.dao.business.dto.YwCustomerRechargeRecordVO;
import com.doumee.dao.business.dto.h5.*;
import com.doumee.dao.business.model.YwContract;
import com.doumee.dao.business.model.YwContractBill;
import com.doumee.dao.business.model.YwH5Banner;
import com.doumee.dao.business.model.YwWxPayOrder;
import com.doumee.dao.system.dto.LoginPhoneDTO;
import com.doumee.service.business.SmsEmailService;
import com.doumee.service.business.YwCustomerH5AuthService;
import com.doumee.service.business.YwCustomerH5BizService;
import com.doumee.service.business.YwCustomerWxPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
 
@Api(tags = "【公众号】商户H5")
@RestController
@Slf4j
@RequestMapping(Constants.CLOUD_SERVICE_URL_INDEX + "/web/customer")
public class YwCustomerH5Controller extends BaseController {
 
    @Autowired
    private YwCustomerH5BizService ywCustomerH5BizService;
    @Autowired
    private YwCustomerWxPayService ywCustomerWxPayService;
    @Autowired
    private YwCustomerH5AuthService ywCustomerH5AuthService;
    @Autowired
    private SmsEmailService smsEmailService;
 
    private LoginUserInfo requireCustomerUser(String token) {
        LoginUserInfo user = getLoginUser(token);
        if (user == null || !Constants.equalsInteger(user.getH5UserType(), LoginUserInfo.H5_USER_CUSTOMER)) {
            throw new RuntimeException("登录已失效");
        }
        return user;
    }
 
    @LoginNoRequired
    @PreventRepeat
    @ApiOperation("商户登录发送验证码")
    @PostMapping("/sendLoginSms")
    public ApiResponse<Integer> sendLoginSms(@RequestBody(required = false) LoginPhoneDTO dto) {
        try {
            if (dto == null || StringUtils.isBlank(dto.getPhone())) {
                return ApiResponse.failed(ResponseStatus.BAD_REQUEST.getCode(), "手机号不能为空");
            }
            return ApiResponse.success(smsEmailService.sendMerchantLoginSms(dto.getPhone().trim()));
        } catch (BusinessException e) {
            return ApiResponse.failed(e.getCode(), e.getMessage());
        } catch (Throwable e) {
            log.error("sendLoginSms failed", e);
            return ApiResponse.failed(ResponseStatus.SERVER_ERROR.getCode(), "发送验证码失败,请稍后重试");
        }
    }
 
    @LoginNoRequired
    @PreventRepeat
    @ApiOperation("商户短信验证码登录")
    @PostMapping("/loginByPhone")
    public ApiResponse<String> loginByPhone(@Validated @RequestBody LoginPhoneDTO dto) {
        try {
            return ApiResponse.success(ywCustomerH5AuthService.loginByPhone(dto));
        } catch (BusinessException e) {
            return ApiResponse.failed(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return ApiResponse.failed(ResponseStatus.SERVER_ERROR);
        }
    }
 
    @ApiOperation("获取当前商户登录信息")
    @GetMapping("/getUserInfo")
    public ApiResponse<LoginUserInfo> getUserInfo(@RequestHeader(Constants.HEADER_USER_TOKEN) String token) {
        LoginUserInfo user = this.getLoginUser(token);
        if (user == null || !Constants.equalsInteger(user.getH5UserType(), LoginUserInfo.H5_USER_CUSTOMER)) {
            return ApiResponse.failed("登录已失效");
        }
        return ApiResponse.success(ywCustomerH5AuthService.buildLoginUserInfo(user.getCustomerId()));
    }
 
    @ApiOperation("工作台轮播图")
    @GetMapping("/banners")
    @LoginNoRequired
    public ApiResponse<List<YwH5Banner>> banners() {
        return ApiResponse.success(ywCustomerH5BizService.listBanners());
    }
 
    @ApiOperation("工作台首页")
    @GetMapping("/home")
    public ApiResponse<Map<String, Object>> home(@RequestHeader(Constants.HEADER_USER_TOKEN) String token) {
        LoginUserInfo user = requireCustomerUser(token);
        return ApiResponse.success(ywCustomerH5BizService.home(user.getCustomerId()));
    }
 
    @ApiOperation("交电费设备列表")
    @PostMapping("/device/page")
    public ApiResponse<PageData<CustomerDeviceH5VO>> devicePage(
            @RequestBody PageWrap<CustomerDeviceQueryDTO> pageWrap,
            @RequestHeader(Constants.HEADER_USER_TOKEN) String token) {
        LoginUserInfo user = requireCustomerUser(token);
        return ApiResponse.success(ywCustomerH5BizService.devicePage(pageWrap, user.getCustomerId()));
    }
 
    @ApiOperation("设备详情")
    @GetMapping("/device/detail")
    public ApiResponse<CustomerDeviceH5VO> deviceDetail(
            @RequestParam Integer deviceType,
            @RequestParam Integer deviceId,
            @RequestHeader(Constants.HEADER_USER_TOKEN) String token) {
        LoginUserInfo user = requireCustomerUser(token);
        return ApiResponse.success(ywCustomerH5BizService.deviceDetail(deviceType, deviceId, user.getCustomerId()));
    }
 
    @ApiOperation("充值记录")
    @PostMapping("/rechargeRecord/page")
    public ApiResponse<PageData<YwCustomerRechargeRecordVO>> rechargeRecordPage(
            @RequestBody PageWrap<CustomerRechargeRecordH5QueryDTO> pageWrap,
            @RequestHeader(Constants.HEADER_USER_TOKEN) String token) {
        LoginUserInfo user = requireCustomerUser(token);
        return ApiResponse.success(ywCustomerH5BizService.rechargeRecordPage(pageWrap, user.getCustomerId()));
    }
 
    @ApiOperation("合同列表")
    @PostMapping("/contract/page")
    public ApiResponse<PageData<YwContract>> contractPage(
            @RequestBody PageWrap<CustomerContractQueryDTO> pageWrap,
            @RequestHeader(Constants.HEADER_USER_TOKEN) String token) {
        LoginUserInfo user = requireCustomerUser(token);
        return ApiResponse.success(ywCustomerH5BizService.contractPage(pageWrap, user.getCustomerId()));
    }
 
    @ApiOperation("合同详情")
    @GetMapping("/contract/{id}")
    public ApiResponse<Map<String, Object>> contractDetail(
            @PathVariable("id") Integer id,
            @RequestParam(value = "billType", required = false, defaultValue = "0") Integer billType,
            @RequestHeader(Constants.HEADER_USER_TOKEN) String token) {
        LoginUserInfo user = requireCustomerUser(token);
        return ApiResponse.success(ywCustomerH5BizService.contractDetail(id, user.getCustomerId(), billType));
    }
 
    @ApiOperation("账单列表")
    @PostMapping("/bill/page")
    public ApiResponse<PageData<YwContractBill>> billPage(
            @RequestBody PageWrap<CustomerBillQueryDTO> pageWrap,
            @RequestHeader(Constants.HEADER_USER_TOKEN) String token) {
        LoginUserInfo user = requireCustomerUser(token);
        return ApiResponse.success(ywCustomerH5BizService.billPage(pageWrap, user.getCustomerId()));
    }
 
    @ApiOperation("账单详情")
    @GetMapping("/bill/{id}")
    public ApiResponse<Map<String, Object>> billDetail(
            @PathVariable("id") Integer id,
            @RequestHeader(Constants.HEADER_USER_TOKEN) String token) {
        LoginUserInfo user = requireCustomerUser(token);
        return ApiResponse.success(ywCustomerH5BizService.billDetail(id, user.getCustomerId()));
    }
 
    @ApiOperation("创建支付订单")
    @PostMapping("/pay/createOrder")
    public ApiResponse<Map<String, String>> createOrder(
            @RequestBody CustomerPayCreateDTO dto,
            @RequestHeader(Constants.HEADER_USER_TOKEN) String token,
            HttpServletRequest request) {
        LoginUserInfo user = requireCustomerUser(token);
        String ip = request.getHeader("X-Forwarded-For");
        if (ip != null && ip.contains(",")) {
            ip = ip.split(",")[0].trim();
        }
        if (ip == null) {
            ip = request.getRemoteAddr();
        }
        return ApiResponse.success(ywCustomerWxPayService.createOrder(dto, user, ip));
    }
 
    @ApiOperation("查询支付结果")
    @GetMapping("/pay/query/{orderNo}")
    public ApiResponse<YwWxPayOrder> queryPay(
            @PathVariable String orderNo,
            @RequestHeader(Constants.HEADER_USER_TOKEN) String token) {
        LoginUserInfo user = requireCustomerUser(token);
        return ApiResponse.success(ywCustomerWxPayService.queryOrder(orderNo, user.getCustomerId()));
    }
 
    @ApiOperation("微信支付回调")
    @PostMapping("/pay/notify")
    @LoginNoRequired
    public String payNotify(@RequestBody String xmlBody) {
        return ywCustomerWxPayService.handleNotify(xmlBody);
    }
}