| | |
| | | import com.sinata.appointment.common.Result; |
| | | import com.sinata.appointment.common.ResultUtil; |
| | | import com.sinata.appointment.dto.MiniLoginResponse; |
| | | import com.sinata.appointment.dto.MiniOfficialBindPhoneRequest; |
| | | import com.sinata.appointment.dto.MiniOfficialLoginRequest; |
| | | import com.sinata.appointment.dto.MiniOfficialLoginResponse; |
| | | import com.sinata.appointment.dto.MiniPhoneLoginRequest; |
| | | import com.sinata.appointment.dto.MiniProfileUpdateRequest; |
| | | import com.sinata.appointment.dto.MiniWxLoginRequest; |
| | | import com.sinata.appointment.entity.MpUser; |
| | | import com.sinata.appointment.entity.SysDept; |
| | |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.web.bind.annotation.RequestParam; |
| | | import org.springframework.web.bind.annotation.GetMapping; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.PutMapping; |
| | | import org.springframework.web.bind.annotation.RequestBody; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import com.sinata.appointment.mapper.SysDeptMapper; |
| | | import com.sinata.appointment.service.LocalImageUploadService; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | @Tag(name = "小程序认证") |
| | | @RestController |
| | |
| | | |
| | | private final MiniAuthApplicationService miniAuthApplicationService; |
| | | private final SysDeptMapper sysDeptMapper; |
| | | private final LocalImageUploadService localImageUploadService; |
| | | |
| | | @Operation( |
| | | summary = "微信登录", |
| | | description = |
| | | "需同时传 wx.login 的 js_code 与 getPhoneNumber 返回的 phoneCode;" |
| | | + "员工须预先在 mp_user 登记且分配部门,手机号与登记一致;与平台 sys_user 无关联。") |
| | | "wxCode 必传,先按 openId 直接登录;若 openId 未匹配到用户,再补传 getPhoneNumber 的 phoneCode 完成绑定/登录。") |
| | | @PostMapping("/login") |
| | | public Result<MiniLoginResponse> login(@Valid @RequestBody MiniWxLoginRequest request) { |
| | | return ResultUtil.data(miniAuthApplicationService.loginByWx(request)); |
| | | } |
| | | |
| | | @Operation(summary = "临时手机号登录(H5联调)") |
| | | @Operation(summary = "临时手机号登录") |
| | | @PostMapping("/phone-login") |
| | | public Result<MiniLoginResponse> phoneLogin(@Valid @RequestBody MiniPhoneLoginRequest request) { |
| | | return ResultUtil.data(miniAuthApplicationService.loginByPhone(request.getMobile())); |
| | | } |
| | | |
| | | @Operation(summary = "公众号静默授权地址") |
| | | @GetMapping("/official-oauth-url") |
| | | public Result<Map<String, String>> officialOAuthUrl( |
| | | @RequestParam("redirectUri") String redirectUri, |
| | | @RequestParam(value = "state", required = false) String state) { |
| | | return ResultUtil.data( |
| | | Map.of("url", miniAuthApplicationService.buildOfficialOAuthUrl(redirectUri, state))); |
| | | } |
| | | |
| | | @Operation(summary = "公众号静默授权登录") |
| | | @PostMapping("/official-login") |
| | | public Result<MiniOfficialLoginResponse> officialLogin( |
| | | @Valid @RequestBody MiniOfficialLoginRequest request) { |
| | | return ResultUtil.data(miniAuthApplicationService.loginByOfficialCode(request.getCode())); |
| | | } |
| | | |
| | | @Operation(summary = "公众号首次登录手机号绑定") |
| | | @PostMapping("/official-bind-phone") |
| | | public Result<MiniLoginResponse> officialBindPhone( |
| | | @Valid @RequestBody MiniOfficialBindPhoneRequest request) { |
| | | return ResultUtil.data( |
| | | miniAuthApplicationService.bindOfficialPhoneAndLogin( |
| | | request.getBindToken(), request.getMobile())); |
| | | } |
| | | |
| | | @Operation(summary = "当前小程序用户") |
| | | @GetMapping("/me") |
| | | public Result<Map<String, Object>> me() { |
| | | MpUser u = miniAuthApplicationService.currentMpUser(); |
| | | Map<String, Object> m = new HashMap<>(); |
| | | m.put("mpUserId", u.getId()); |
| | | m.put("userType", u.getUserType()); |
| | | m.put("deptId", u.getDeptId()); |
| | | SysDept dept = u.getDeptId() == null ? null : sysDeptMapper.selectById(u.getDeptId()); |
| | | m.put("deptName", dept == null ? null : dept.getDeptName()); |
| | | m.put("deptLeaderFlag", Boolean.TRUE.equals(u.getDeptLeaderFlag())); |
| | | m.put("nickName", u.getNickName()); |
| | | m.put("avatarUrl", u.getAvatarUrl()); |
| | | m.put("mobile", u.getMobile()); |
| | | return ResultUtil.data(m); |
| | | return ResultUtil.data(buildMeResponse(miniAuthApplicationService.currentMpUser())); |
| | | } |
| | | |
| | | @Operation(summary = "更新当前小程序用户资料") |
| | | @PutMapping("/profile") |
| | | public Result<Map<String, Object>> updateProfile( |
| | | @Valid @RequestBody MiniProfileUpdateRequest request) { |
| | | return ResultUtil.data(buildMeResponse(miniAuthApplicationService.updateProfile(request))); |
| | | } |
| | | |
| | | @Operation(summary = "小程序用户图片上传") |
| | | @PostMapping("/upload/image") |
| | | public Result<Map<String, String>> uploadImage(@RequestParam("file") MultipartFile file) { |
| | | return ResultUtil.data(Map.of("url", localImageUploadService.uploadImage(file))); |
| | | } |
| | | |
| | | @Operation(summary = "退出") |
| | |
| | | miniAuthApplicationService.logout(); |
| | | return ResultUtil.success(); |
| | | } |
| | | |
| | | @Operation(summary = "解除当前小程序微信绑定") |
| | | @PostMapping("/unbind-openid") |
| | | public Result<Void> unbindOpenId() { |
| | | miniAuthApplicationService.unbindMiniOpenIdAndLogout(); |
| | | return ResultUtil.success(); |
| | | } |
| | | |
| | | private Map<String, Object> buildMeResponse(MpUser u) { |
| | | Map<String, Object> m = new HashMap<>(); |
| | | m.put("mpUserId", u.getId()); |
| | | m.put("userType", u.getUserType()); |
| | | m.put("deptId", u.getDeptId()); |
| | | SysDept dept = u.getDeptId() == null ? null : sysDeptMapper.selectById(u.getDeptId()); |
| | | m.put("deptName", dept == null ? null : dept.getDeptName()); |
| | | m.put("deptLeaderFlag", false); |
| | | m.put("nickName", u.getNickName()); |
| | | m.put("avatarUrl", u.getAvatarUrl()); |
| | | m.put("mobile", u.getMobile()); |
| | | m.put("gender", u.getGender()); |
| | | m.put("companyName", u.getCompanyName()); |
| | | return m; |
| | | } |
| | | } |
| | | |