diff --git a/src/main/java/com/rymcu/vertical/entity/WxUser.java b/src/main/java/com/rymcu/vertical/entity/WxUser.java new file mode 100644 index 0000000..e9d4103 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/entity/WxUser.java @@ -0,0 +1,49 @@ +package com.rymcu.vertical.entity; + +import lombok.Data; + +import javax.persistence.Column; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; + +/** + * @author ronger + */ +@Data +@Table(name = "vertical_wx_user") +public class WxUser { + + @Id + @Column(name = "id") + @GeneratedValue(generator = "JDBC") + private Integer idWxUser; + + private Boolean subscribe; + + private String openId; + + private String nickname; + + private String sexDesc; + + private Integer sex; + + private String language; + + private String city; + + private String province; + + private String country; + + private String headImgUrl; + + private Long subscribeTime; + + private String unionId; + + private String appId; + + private String actToken; +} diff --git a/src/main/java/com/rymcu/vertical/mapper/WxUserMapper.java b/src/main/java/com/rymcu/vertical/mapper/WxUserMapper.java new file mode 100644 index 0000000..9e941d3 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/mapper/WxUserMapper.java @@ -0,0 +1,10 @@ +package com.rymcu.vertical.mapper; + +import com.rymcu.vertical.core.mapper.Mapper; +import com.rymcu.vertical.entity.WxUser; + +/** + * @author ronger + */ +public interface WxUserMapper extends Mapper { +} diff --git a/src/main/java/com/rymcu/vertical/service/WxUserService.java b/src/main/java/com/rymcu/vertical/service/WxUserService.java new file mode 100644 index 0000000..59e8527 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/service/WxUserService.java @@ -0,0 +1,14 @@ +package com.rymcu.vertical.service; + +import com.rymcu.vertical.core.service.Service; +import com.rymcu.vertical.entity.WxUser; +import me.chanjar.weixin.mp.bean.result.WxMpUser; + +/** + * @author ronger + */ +public interface WxUserService extends Service { + + WxUser saveUser(WxMpUser wxMpUser, String appId); + +} diff --git a/src/main/java/com/rymcu/vertical/service/impl/WxUserServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/WxUserServiceImpl.java new file mode 100644 index 0000000..3309f0d --- /dev/null +++ b/src/main/java/com/rymcu/vertical/service/impl/WxUserServiceImpl.java @@ -0,0 +1,62 @@ +package com.rymcu.vertical.service.impl; + +import com.rymcu.vertical.core.service.AbstractService; +import com.rymcu.vertical.entity.WxUser; +import com.rymcu.vertical.mapper.WxUserMapper; +import com.rymcu.vertical.service.WxUserService; +import me.chanjar.weixin.mp.bean.result.WxMpUser; +import org.apache.commons.lang.StringUtils; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.List; + +/** + * @author ronger + */ +@Service +public class WxUserServiceImpl extends AbstractService implements WxUserService { + + @Resource + private WxUserMapper wxUserMapper; + + @Override + public WxUser saveUser(WxMpUser wxMpUser, String appId) { + WxUser searchWxUser = new WxUser(); + if (StringUtils.isBlank(wxMpUser.getUnionId())) { + searchWxUser.setUnionId(wxMpUser.getUnionId()); + } else { + searchWxUser.setAppId(appId); + searchWxUser.setOpenId(searchWxUser.getOpenId()); + } + List wxUsers = wxUserMapper.select(searchWxUser); + WxUser wxUser; + if (wxUsers.isEmpty()) { + wxUser = new WxUser(); + wxUser.setAppId(appId); + wxUser = copyWxUser(wxMpUser,wxUser); + wxUserMapper.insertSelective(wxUser); + } else { + wxUser = wxUsers.get(0); + wxUser = copyWxUser(wxMpUser,wxUser); + wxUserMapper.updateByPrimaryKeySelective(wxUser); + } + return wxUser; + } + + private WxUser copyWxUser(WxMpUser wxMpUser, WxUser wxUser) { + wxUser.setNickname(wxMpUser.getNickname()); + wxUser.setHeadImgUrl(wxMpUser.getHeadImgUrl()); + wxUser.setCountry(wxMpUser.getCountry()); + wxUser.setProvince(wxMpUser.getProvince()); + wxUser.setCity(wxMpUser.getCity()); + wxUser.setSex(wxMpUser.getSex()); + wxUser.setSubscribe(wxMpUser.getSubscribe()); + wxUser.setSubscribeTime(wxMpUser.getSubscribeTime()); + wxUser.setUnionId(wxMpUser.getUnionId()); + wxUser.setOpenId(wxMpUser.getOpenId()); + wxUser.setLanguage(wxMpUser.getLanguage()); + wxUser.setSexDesc(wxMpUser.getSexDesc()); + return wxUser; + } +} diff --git a/src/main/java/mapper/WxUserMapper.xml b/src/main/java/mapper/WxUserMapper.xml new file mode 100644 index 0000000..228a592 --- /dev/null +++ b/src/main/java/mapper/WxUserMapper.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file