Commit 24a54ae4 authored by mlchun's avatar mlchun

1.添加微信登录,返回token

parent 447b86e6
...@@ -27,7 +27,7 @@ public class SwaggerConfig { ...@@ -27,7 +27,7 @@ public class SwaggerConfig {
@Bean @Bean
public Docket createRestApi() { public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2) return new Docket(DocumentationType.SWAGGER_2)
.host("byyl.zjhmit.com") // .host("byyl.zjhmit.com")
.apiInfo(apiInfo()) .apiInfo(apiInfo())
.select() .select()
//加了ApiOperation注解的类,才生成接口文档 //加了ApiOperation注解的类,才生成接口文档
......
...@@ -13,6 +13,7 @@ import io.hmit.service.UserService; ...@@ -13,6 +13,7 @@ import io.hmit.service.UserService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore; import springfox.documentation.annotations.ApiIgnore;
...@@ -24,6 +25,8 @@ import java.util.Map; ...@@ -24,6 +25,8 @@ import java.util.Map;
* *
* @author zsh 408538940@qq.com * @author zsh 408538940@qq.com
*/ */
@Slf4j
@RestController @RestController
@RequestMapping("/api") @RequestMapping("/api")
@Api(tags = "登录接口") @Api(tags = "登录接口")
...@@ -48,6 +51,17 @@ public class ApiLoginController { ...@@ -48,6 +51,17 @@ public class ApiLoginController {
return new Result().ok(map); return new Result().ok(map);
} }
@RequestMapping("login")
@ApiOperation("登录")
public Result<Map<String, Object>> login(@RequestParam("openid") String openid) {
log.info("【微信网页授权】openId={}", openid);
//用户登录
Map<String, Object> map = userService.login(openid);
return new Result().ok(map);
}
@Login @Login
@PostMapping("logout") @PostMapping("logout")
@ApiOperation("退出") @ApiOperation("退出")
......
package io.hmit.controller;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.net.URLEncoder;
/**
* @Description :
* @Author : Shen Yuanfeng
* @Date: 2021/3/17 15:32
*/
@Slf4j
@Controller
@RequestMapping("/wechat")
public class WeChatController {
@Autowired
private WxMpService wxMpService;
@GetMapping("/authorize")
public String authorize(/*@RequestParam("returnUrl") String returnUrl*/) {
String returnUrl = "http://byyl.zjhmit.com/hmit-api/api/login";
String url = "http://byyl.zjhmit.com/hmit-api/wechat/authcode";
String redirectUrl = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAUTH2_SCOPE_BASE, URLEncoder.encode(returnUrl));
log.info("【微信网页授权】获取code,redirectUrl={}", redirectUrl);
return "redirect:" + redirectUrl;
}
@GetMapping("/authcode")
public String userInfo(@RequestParam("code") String code,
@RequestParam("state") String returnUrl) {
log.info("【微信网页授权】code={}, returnUrl={}", code, returnUrl);
WxMpOAuth2AccessToken wxMpOAuth2AccessToken = new WxMpOAuth2AccessToken();
try {
wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
}catch (WxErrorException e){
log.error("【微信网页授权】错误{}",e.getMessage());
}
String openid = wxMpOAuth2AccessToken.getOpenId();
return "redirect:" + returnUrl + "?openid=" + openid;
}
}
...@@ -18,6 +18,8 @@ public interface UserDao extends BaseDao<UserEntity> { ...@@ -18,6 +18,8 @@ public interface UserDao extends BaseDao<UserEntity> {
UserEntity getUserByUserId(Long userId); UserEntity getUserByUserId(Long userId);
UserEntity getUserByOpenId(String openId);
List<Long> getAllOrganizationStaff(Long userId); List<Long> getAllOrganizationStaff(Long userId);
} }
...@@ -226,7 +226,8 @@ public class PensionOrderController { ...@@ -226,7 +226,8 @@ public class PensionOrderController {
try { try {
todto = URLDecoder.decode(dto,"UTF-8"); todto = URLDecoder.decode(dto,"UTF-8");
}catch (Exception ignored){} }catch (Exception ignored){}
StaffFinishDTO staffFinishDTO = JSON.parseObject(todto.substring(4), StaffFinishDTO.class); // StaffFinishDTO staffFinishDTO = JSON.parseObject(todto.substring(4), StaffFinishDTO.class);
StaffFinishDTO staffFinishDTO = JSON.parseObject(todto, StaffFinishDTO.class);
pensionOrderService.staffFinishedOrder(staffFinishDTO); pensionOrderService.staffFinishedOrder(staffFinishDTO);
return new Result(); return new Result();
} }
...@@ -317,7 +318,7 @@ public class PensionOrderController { ...@@ -317,7 +318,7 @@ public class PensionOrderController {
try { try {
todto = URLDecoder.decode(dto,"UTF-8"); todto = URLDecoder.decode(dto,"UTF-8");
}catch (Exception ignored){} }catch (Exception ignored){}
ReservationDTO reservationDTO = JSON.parseObject(todto.substring(4), ReservationDTO.class); ReservationDTO reservationDTO = JSON.parseObject(todto, ReservationDTO.class);
PensionOrderDTO data = new PensionOrderDTO().assemblePensionOrder(reservationDTO,user); PensionOrderDTO data = new PensionOrderDTO().assemblePensionOrder(reservationDTO,user);
data.setCommunityId(user.getCommunityId()); data.setCommunityId(user.getCommunityId());
......
...@@ -74,7 +74,7 @@ public class PensionOrderEvaluationController { ...@@ -74,7 +74,7 @@ public class PensionOrderEvaluationController {
try { try {
todto = URLDecoder.decode(dto,"UTF-8"); todto = URLDecoder.decode(dto,"UTF-8");
}catch (Exception ignored){} }catch (Exception ignored){}
PensionOrderEvaluationDTO pensionOrderEvaluationDTO = JSON.parseObject(todto.substring(4), PensionOrderEvaluationDTO.class); PensionOrderEvaluationDTO pensionOrderEvaluationDTO = JSON.parseObject(todto, PensionOrderEvaluationDTO.class);
pensionOrderEvaluationDTO.setFile(StringUtils.join(pensionOrderEvaluationDTO.getFiles(), ",")); pensionOrderEvaluationDTO.setFile(StringUtils.join(pensionOrderEvaluationDTO.getFiles(), ","));
pensionOrderEvaluationService.save(pensionOrderEvaluationDTO); pensionOrderEvaluationService.save(pensionOrderEvaluationDTO);
......
...@@ -89,7 +89,7 @@ public class PensionOrderServiceImpl extends CrudServiceImpl<PensionOrderDao, Pe ...@@ -89,7 +89,7 @@ public class PensionOrderServiceImpl extends CrudServiceImpl<PensionOrderDao, Pe
save(pensionOrderDTO); save(pensionOrderDTO);
PensionOrderServiceDTO orderServiceDTO = new PensionOrderServiceDTO().assemblePensionOrderService(pensionOrderDTO); PensionOrderServiceDTO orderServiceDTO = new PensionOrderServiceDTO().assemblePensionOrderService(pensionOrderDTO);
pensionOrderServiceService.save(orderServiceDTO); pensionOrderServiceService.save(orderServiceDTO);
// pushMessage.sendWeChatMsg(pensionOrderDTO, userService.getUserByUserId(pensionOrderDTO.getCreator()).getOpenid()); pushMessage.sendWeChatMsg(pensionOrderDTO, userService.getUserByUserId(pensionOrderDTO.getCreator()).getOpenid());
// pushMessage.sendSMSMsg(pensionOrderDTO, userService.getUserByUserId(pensionOrderDTO.getCreator()).getOpenid()); // pushMessage.sendSMSMsg(pensionOrderDTO, userService.getUserByUserId(pensionOrderDTO.getCreator()).getOpenid());
} }
......
...@@ -17,6 +17,8 @@ public interface UserService extends BaseService<UserEntity> { ...@@ -17,6 +17,8 @@ public interface UserService extends BaseService<UserEntity> {
UserEntity getByMobile(String mobile); UserEntity getByMobile(String mobile);
UserEntity getByOpenId(String mobile);
UserEntity getUserByUserId(Long userId); UserEntity getUserByUserId(Long userId);
/** /**
...@@ -27,6 +29,14 @@ public interface UserService extends BaseService<UserEntity> { ...@@ -27,6 +29,14 @@ public interface UserService extends BaseService<UserEntity> {
*/ */
Map<String, Object> login(LoginDTO dto); Map<String, Object> login(LoginDTO dto);
/**
* 用户登录
*
* @param openid 免登
* @return 返回登录信息
*/
Map<String, Object> login(String openid);
PageData<UserEntity> getOrganizationAllUsers(Map<String, Object> params, Long userId); PageData<UserEntity> getOrganizationAllUsers(Map<String, Object> params, Long userId);
} }
...@@ -45,11 +45,16 @@ public class PushMessageImpl implements PushMessage { ...@@ -45,11 +45,16 @@ public class PushMessageImpl implements PushMessage {
templateMessage.setTemplateId(wechatAccountConfig.getTemplateId()); templateMessage.setTemplateId(wechatAccountConfig.getTemplateId());
templateMessage.setToUser(openId); templateMessage.setToUser(openId);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
String appointmentTime = sdf.format(pensionOrderDTO.getAppointmentTime());
List<WxMpTemplateData> data = Arrays.asList( List<WxMpTemplateData> data = Arrays.asList(
new WxMpTemplateData("first", "订单预约成功!"), new WxMpTemplateData("first", "【智慧养老】您的订单提醒"),
new WxMpTemplateData("keyword1", pensionOrderDTO.getServiceName()), new WxMpTemplateData("keyword1", pensionOrderDTO.getServiceName()),
new WxMpTemplateData("keyword2", pensionOrderDTO.getAppointmentTime().toString()), new WxMpTemplateData("keyword2", pensionOrderDTO.getId().toString()),
new WxMpTemplateData("keyword3", pensionOrderDTO.getStatusName()) new WxMpTemplateData("keyword3", pensionOrderDTO.getServiceName()),
new WxMpTemplateData("keyword4", appointmentTime),
new WxMpTemplateData("remark", "备注:" + pensionOrderDTO.getRemark())
); );
templateMessage.setData(data); templateMessage.setData(data);
try{ try{
...@@ -62,8 +67,6 @@ public class PushMessageImpl implements PushMessage { ...@@ -62,8 +67,6 @@ public class PushMessageImpl implements PushMessage {
@Override @Override
public void sendSMSMsg(PensionOrderDTO pensionOrderDTO, String receiver) { public void sendSMSMsg(PensionOrderDTO pensionOrderDTO, String receiver) {
try { try {
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdfYMD = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdfYMD = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdfHms = new SimpleDateFormat("HH:mm:ss"); SimpleDateFormat sdfHms = new SimpleDateFormat("HH:mm:ss");
String ymd = sdfYMD.format(pensionOrderDTO.getAppointmentTime()); String ymd = sdfYMD.format(pensionOrderDTO.getAppointmentTime());
......
...@@ -36,6 +36,11 @@ public class UserServiceImpl extends BaseServiceImpl<UserDao, UserEntity> implem ...@@ -36,6 +36,11 @@ public class UserServiceImpl extends BaseServiceImpl<UserDao, UserEntity> implem
return baseDao.getUserByMobile(mobile); return baseDao.getUserByMobile(mobile);
} }
@Override
public UserEntity getByOpenId(String openId) {
return baseDao.getUserByOpenId(openId);
}
@Override @Override
public UserEntity getUserByUserId(Long userId) { public UserEntity getUserByUserId(Long userId) {
return baseDao.getUserByUserId(userId); return baseDao.getUserByUserId(userId);
...@@ -43,7 +48,6 @@ public class UserServiceImpl extends BaseServiceImpl<UserDao, UserEntity> implem ...@@ -43,7 +48,6 @@ public class UserServiceImpl extends BaseServiceImpl<UserDao, UserEntity> implem
@Override @Override
public Map<String, Object> login(LoginDTO dto) { public Map<String, Object> login(LoginDTO dto) {
UserEntity user = getByMobile(dto.getMobile()); UserEntity user = getByMobile(dto.getMobile());
AssertUtils.isNull(user, ErrorCode.ACCOUNT_PASSWORD_ERROR); AssertUtils.isNull(user, ErrorCode.ACCOUNT_PASSWORD_ERROR);
...@@ -62,6 +66,27 @@ public class UserServiceImpl extends BaseServiceImpl<UserDao, UserEntity> implem ...@@ -62,6 +66,27 @@ public class UserServiceImpl extends BaseServiceImpl<UserDao, UserEntity> implem
return map; return map;
} }
/**
* 用户登录
*
* @param openId 免登
* @return 返回登录信息
*/
@Override
public Map<String, Object> login(String openId) {
UserEntity user = getByOpenId(openId);
AssertUtils.isNull(user, ErrorCode.ACCOUNT_PASSWORD_ERROR);
//获取登录token
TokenEntity tokenEntity = tokenService.createToken(user.getId());
Map<String, Object> map = new HashMap<>(2);
map.put("token", tokenEntity.getToken());
map.put("expire", tokenEntity.getExpireDate().getTime() - System.currentTimeMillis());
return map;
}
@Override @Override
public PageData<UserEntity> getOrganizationAllUsers(Map<String, Object> params, Long userId) { public PageData<UserEntity> getOrganizationAllUsers(Map<String, Object> params, Long userId) {
IPage<UserEntity> page = getPage(params, Constant.CREATE_DATE, false); IPage<UserEntity> page = getPage(params, Constant.CREATE_DATE, false);
......
...@@ -38,11 +38,11 @@ spring: ...@@ -38,11 +38,11 @@ spring:
max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制) max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
max-idle: 10 # 连接池中的最大空闲连接 max-idle: 10 # 连接池中的最大空闲连接
min-idle: 5 # 连接池中的最小空闲连接 min-idle: 5 # 连接池中的最小空闲连接
mvc: # mvc:
static-path-pattern: /static/** # static-path-pattern: /static/**
resources: # resources:
static-locations: file:${QR.download-path},classpath:/META-INF/resources/, classpath:/resources/, classpath:/static/, classpath:/public/ # static-locations: file:${QR.download-path},classpath:/META-INF/resources/, classpath:/resources/, classpath:/static/, classpath:/public/
#静态资源对外暴露的访问路径 # #静态资源对外暴露的访问路径
hmit: hmit:
redis: redis:
...@@ -72,9 +72,9 @@ mybatis-plus: ...@@ -72,9 +72,9 @@ mybatis-plus:
jdbc-type-for-null: 'null' jdbc-type-for-null: 'null'
QR: QR:
# download-path: D:/123/ /home/websoft/java_jar/pension_service/myfile/ # download-path: D:/123/ /home/websoft/java_jar/pension_service/myfile/
download-path: /home/websoft/java_jar/pension_service/myfile/ download-path: D:/123/
wechat: wechat:
mpAppId: wxfa2386639794d998 mpAppId: wx6e3288074243a47f
mpAppSecret: 8f624f83c9585baa9d1697e24fdffd81 mpAppSecret: 603d6ebf4db906bdb24eb9a1c0cbb444
templateId: GsGydVGl-vzOv5aA0LaIdA-FcMM88TEpe37U-lZ6HJ8 templateId: _7PQQku6jHU4yE42NZIbX9cyvRWeeMSAGKhhs0uzWYM
\ No newline at end of file \ No newline at end of file
...@@ -11,6 +11,10 @@ ...@@ -11,6 +11,10 @@
select * from tb_user where id = #{value} select * from tb_user where id = #{value}
</select> </select>
<select id="getUserByOpenId" resultType="io.hmit.entity.UserEntity">
select * from tb_user where openid = #{value}
</select>
<select id="getAllOrganizationStaff" resultType="Long"> <select id="getAllOrganizationStaff" resultType="Long">
SELECT user_id FROM pension_organization_user SELECT user_id FROM pension_organization_user
WHERE organization_id = (SELECT organization_id FROM pension_organization_user WHERE organization_id = (SELECT organization_id FROM pension_organization_user
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment