Skip to content

Commit

Permalink
微信用户管理功能
Browse files Browse the repository at this point in the history
  • Loading branch information
niefy committed Mar 7, 2020
1 parent 833067b commit 7424262
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 12 deletions.
5 changes: 5 additions & 0 deletions db/mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ INSERT INTO `sys_menu` VALUES (103, 6, '带参二维码', 'wx/wxqrcode', NULL, 1
INSERT INTO `sys_menu` VALUES (104, 103, '查看', NULL, 'wx:wxqrcode:list,wx:wxqrcode:info', 2, NULL, 6);
INSERT INTO `sys_menu` VALUES (105, 103, '新增', NULL, 'wx:wxqrcode:save', 2, NULL, 6);
INSERT INTO `sys_menu` VALUES (107, 103, '删除', NULL, 'wx:wxqrcode:delete', 2, NULL, 6);
INSERT INTO `sys_menu` VALUES (108, 6, '微信用户', 'wx/wx-user', NULL, 1, 'config', 6);
INSERT INTO `sys_menu` VALUES (109, 108, '查看', NULL, 'wx:user:list,wx:user:info', 2, NULL, 6);
INSERT INTO `sys_menu` VALUES (110, 108, '删除', NULL, 'wx:user:delete', 2, NULL, 6);


-- ----------------------------
-- Table structure for sys_oss
Expand Down Expand Up @@ -353,6 +357,7 @@ CREATE TABLE `wx_user` (
`province` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '省份',
`headimgurl` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '头像',
`subscribe_time` datetime(0) NULL DEFAULT NULL COMMENT '订阅时间',
`subscribe` tinyint(1) NULL DEFAULT 1 COMMENT '是否订阅',
PRIMARY KEY (`openid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '用户表' ROW_FORMAT = Dynamic;

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/github/niefy/modules/wx/dao/UserMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
@Mapper
@CacheNamespace(flushInterval = 300000L)//缓存五分钟过期
public interface UserMapper extends BaseMapper<User> {

void unsubscribe(String openid);
}
26 changes: 17 additions & 9 deletions src/main/java/com/github/niefy/modules/wx/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ public class User implements Serializable {

private static final long serialVersionUID = 1L;
@TableId(type = IdType.INPUT)
private String openid;
private String phone;
private String nickname;
private int sex;
private String city;
private String province;
private String headimgurl;
@JSONField(name="subscribe_time")
private String openid;
private String phone;
private String nickname;
private int sex;
private String city;
private String province;
private String headimgurl;
@JSONField(name="subscribe_time")
private Date subscribeTime;
private boolean subscribe;

public User() {
}
Expand Down Expand Up @@ -99,5 +100,12 @@ public Date getSubscribeTime() {
public void setSubscribeTime(Date subscribeTime) {
this.subscribeTime = subscribeTime;
}


public boolean isSubscribe() {
return subscribe;
}

public void setSubscribe(boolean subscribe) {
this.subscribe = subscribe;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.Map;

import com.github.niefy.modules.wx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import me.chanjar.weixin.common.session.WxSessionManager;
Expand All @@ -16,14 +18,16 @@
*/
@Component
public class UnsubscribeHandler extends AbstractHandler {

@Autowired
UserService userService;
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
Map<String, Object> context, WxMpService wxMpService,
WxSessionManager sessionManager) {
String openId = wxMessage.getFromUser();
this.logger.info("取消关注用户 OPENID: " + openId);
String openid = wxMessage.getFromUser();
this.logger.info("取消关注用户 OPENID: " + openid);
// TODO 可以更新本地数据库为取消关注状态
userService.unsubscribe(openid);
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.github.niefy.modules.wx.manage;

import java.util.Arrays;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.niefy.modules.wx.entity.User;
import com.github.niefy.modules.wx.service.UserService;
import com.github.niefy.common.utils.PageUtils;
import com.github.niefy.common.utils.R;



/**
* 用户表
*
* @author niefy
* @email [email protected]
* @date 2020-03-07 13:55:23
*/
@RestController
@RequestMapping("/manage/user")
public class UserManageController {
@Autowired
private UserService userService;

/**
* 列表
*/
@RequestMapping("/list")
@RequiresPermissions("wx:user:list")
public R list(@RequestParam Map<String, Object> params){
PageUtils page = userService.queryPage(params);

return R.ok().put("page", page);
}


/**
* 信息
*/
@RequestMapping("/info/{openid}")
@RequiresPermissions("wx:user:info")
public R info(@PathVariable("openid") String openid){
User user = userService.getById(openid);

return R.ok().put("user", user);
}

/**
* 保存
*/
@RequestMapping("/save")
@RequiresPermissions("wx:user:save")
public R save(@RequestBody User user){
userService.save(user);

return R.ok();
}

/**
* 修改
*/
@RequestMapping("/update")
@RequiresPermissions("wx:user:update")
public R update(@RequestBody User user){
userService.updateById(user);

return R.ok();
}

/**
* 删除
*/
@RequestMapping("/delete")
@RequiresPermissions("wx:user:delete")
public R delete(@RequestBody String[] ids){
userService.removeByIds(Arrays.asList(ids));

return R.ok();
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.github.niefy.modules.wx.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.github.niefy.common.utils.PageUtils;
import com.github.niefy.modules.wx.entity.User;

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

public interface UserService extends IService<User> {
PageUtils queryPage(Map<String, Object> params);
/**
* 根据openid更新用户信息
*
Expand Down Expand Up @@ -43,4 +46,10 @@ public interface UserService extends IService<User> {
* @param user
*/
void updateOrInsert(User user);

/**
* 取消关注,更新关注状态
* @param openid
*/
void unsubscribe(String openid);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.github.niefy.modules.wx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.niefy.common.utils.PageUtils;
import com.github.niefy.common.utils.Query;
import com.github.niefy.modules.wx.dao.UserMapper;
import com.github.niefy.modules.wx.entity.Article;
import com.github.niefy.modules.wx.entity.User;
import com.github.niefy.modules.wx.dto.PageSizeConstant;
import com.github.niefy.modules.wx.service.UserService;
Expand All @@ -13,8 +17,10 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

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

/**
* @author Nifury
Expand All @@ -28,6 +34,19 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements U
@Autowired
private WxMpService wxService;

@Override
public PageUtils queryPage(Map<String, Object> params) {
String openid = (String)params.get("openid");
String nickname = (String)params.get("nickname");
IPage<User> page = this.page(
new Query<User>().getPage(params),
new QueryWrapper<User>()
.eq(!StringUtils.isEmpty(openid),"openid",openid)
.like(!StringUtils.isEmpty(nickname),"nickname",nickname)
);

return new PageUtils(page);
}
/**
* 根据openid更新用户信息
*
Expand Down Expand Up @@ -107,4 +126,9 @@ public void updateOrInsert(User user) {
userMapper.insert(user);
}
}

@Override
public void unsubscribe(String openid) {
userMapper.unsubscribe(openid);
}
}

0 comments on commit 7424262

Please sign in to comment.