1
935090232@qq.com
2020-12-01 611146e69aaa62296cf84f2ccb5aca5ebba17677
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
package com.matrix.system.shopXcx.api;
 
import com.alibaba.fastjson.JSON;
import com.matrix.biz.bean.BizUser;
import com.matrix.biz.dao.BizUserDao;
import com.matrix.biz.service.BizUserService;
import com.matrix.component.redis.RedisUserLoginUtils;
import com.matrix.component.tools.HttpCurlUtil;
import com.matrix.component.wechat.externalInterface.weixinUtil.WeixinServiceUtil;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.tools.LogUtil;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.web.BaseAction;
import com.matrix.system.common.constance.AppConstance;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
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.ResponseBody;
 
import java.util.Collections;
import java.util.Date;
 
/**
 * @description (用户表)
 * @author jyy
 * @date 2019-05-31 10:03
 */
@Controller
@RequestMapping(value = "wxapi/user")
public class WxUserAction extends BaseAction {
 
    @Autowired
    private BizUserService bizUserService;
 
 
    @Autowired
    private RedisUserLoginUtils redisUserLoginUtils;
    @Autowired
    private BizUserDao bizUserDao;
 
 
 
 
 
    @Autowired
    private WeixinServiceUtil weixinServiceUtil;
 
    @Value("${debug}")
    private String isDebug;
 
    /**
     * 小程序登录
     * 
     * @author JIANGYOUYAO
     * @email 935090232@qq.com
     * @date 2018年5月23日
     * @param code
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/login/{keys}")
    public @ResponseBody
    AjaxResult wxLogin(@PathVariable("keys") String code) throws Exception {
        AjaxResult res = new AjaxResult();
        LogUtil.info("code:{}" + code);
        if (StringUtils.isNotBlank(code)) {
            String requrl = WeChatApiTools.getXcxLoginUrl(code);
            String reslutData = HttpCurlUtil.sendGetHttp(requrl, null);
            JSONObject json = JSONObject.fromObject(reslutData);
            LogUtil.debug("微信登录获取到登录信息={}", json);
 
            if(json.containsKey("errcode")){
                res.setStatus(AjaxResult.STATUS_FAIL);
                res.setInfo("自动登录失败");
                LogUtil.debug("微信登录获取到异常信息errcode");
                return res;
            }
 
            String openId = json.getString("openid");
            String sessionKey = json.getString("session_key");
            LogUtil.debug("openId={},sessionKey={}", openId, sessionKey);
            // 查询用户是否存在
            BizUser bizUser = null;
            synchronized(this){
                bizUser = bizUserService.findByOpenId(openId);
                if (bizUser == null) {
                    // 新增用户
                    bizUser = new BizUser();
                    bizUser.setSessionKey(sessionKey);
                    bizUser.setOpenId(openId);
                    bizUser.setLastLoginTime(new Date());
                    bizUser.setUserType(AppConstance.USER_TYPE_CUSTOMER);
                    bizUserService.add(bizUser);
 
                } else {
                    bizUser.setSessionKey(sessionKey);
                    bizUser.setLastLoginTime(new Date());
                    // 更新用户sessionKey
                    bizUserService.modifyByModel(bizUser);
                }
            }
            // 存放redis
            String token = redisUserLoginUtils.saveUserInfo(bizUser);
            LogUtil.info("用户token={}", token);
            res.putInMap("token", token);
            res.putInMap("userInfo", bizUser);
            res.setStatus(AjaxResult.STATUS_SUCCESS);
        } else {
            res.setStatus(AjaxResult.STATUS_FAIL);
            res.setInfo("自动登录失败");
        }
        return res;
    }
 
    /**
     * 查询用户信息
     */
    @RequestMapping(value = "/findUserInfo")
    public @ResponseBody
    AjaxResult findUserInfo() {
        BizUser loginUser = redisUserLoginUtils.getLoginUser(BizUser.class);
        String openId = loginUser.getOpenId();
        BizUser bizUser = bizUserService.findByOpenId(openId);
        
        //处理用户当前所在店铺
        /*
        if(bizUser.getStaff()!=null){
            //当前用户是员工账号
            bizUser.setBusiness(bussinesssDao.selectById(bizUser.getStaff().getBusinessId()));
            bizUser.setRole(bizUser.getStaff().getRole());
        }else{
            //当前用户是boss账号
            bizUser.setBusiness(bussinesssDao.findByUserOpenId(bizUser.getOpenId()));
            bizUser.setRole(AppConstance.User_ROLE_BOSS);
        }*/
        
        AjaxResult res = new AjaxResult();
        res.putInMap("userInfo", bizUser);
        res.setStatus(AjaxResult.STATUS_SUCCESS);
        return res;
    }
    
    /**
     * 获取其他用户简化信息
     * @author JIANGYOUYAO
     * @email 935090232@qq.com
     * @date 2018年9月5日
     * @param openId
     * @return
     */
    @RequestMapping(value = "/getOtherUserInfo/{openId}")
    public @ResponseBody
    AjaxResult getOtherUserInfo(@PathVariable String openId) {
        BizUser bizUser = bizUserService.findByOpenId(openId);
        AjaxResult res = new AjaxResult();
        res.putInMap("userInfo", bizUser);
        res.setStatus(AjaxResult.STATUS_SUCCESS);
        return res;
    }
    
 
    /**
     * 接收用户信息
     */
    @RequestMapping(value = "/saveUserInfo")
    public @ResponseBody
    AjaxResult saveUserInfo(@RequestBody BizUser bizUser) {
 
        BizUser loginUser = redisUserLoginUtils.getLoginUser(BizUser.class);
        bizUser.setUserId(loginUser.getUserId());
        // TODO 赋值操作
        int i = bizUserService.saveUserInfo(bizUser);
 
        if (i == 0) {
            return new AjaxResult(AjaxResult.STATUS_FAIL, "更新失败");
        }
        return new AjaxResult(AjaxResult.STATUS_SUCCESS, Collections.singletonList(bizUser));
    }
 
 
 
 
}