935090232@qq.com
2021-10-25 6fc998515141276d7b222c88d635f5863c0bd5ec
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package com.matrix.system.shopXcx.api.action;
 
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.matrix.system.common.init.UserCacheManager;
import com.matrix.component.tools.HttpCurlUtil;
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.activity.dao.ActivitySignReceiveRecordDao;
import com.matrix.system.common.bean.BusParameterSettings;
import com.matrix.system.common.dao.BusParameterSettingsDao;
import com.matrix.system.common.interceptor.HostInterceptor;
import com.matrix.system.constance.Dictionary;
import com.matrix.system.fenxiao.constant.FenxiaoSettingConstant;
import com.matrix.system.fenxiao.dao.ShopSalesmanApplyDao;
import com.matrix.system.fenxiao.dao.ShopSalesmanGradeDao;
import com.matrix.system.fenxiao.entity.ShopSalesmanApply;
import com.matrix.system.fenxiao.entity.ShopSalesmanGrade;
import com.matrix.system.fenxiao.service.ShopSalesmanApplyService;
import com.matrix.system.hive.bean.SysVipInfo;
import com.matrix.system.hive.dao.MoneyCardUseDao;
import com.matrix.system.hive.dao.SysVipInfoDao;
import com.matrix.system.hive.service.SysVipInfoService;
import com.matrix.system.score.dao.ScoreVipDetailDao;
import com.matrix.system.shopXcx.api.WeChatApiTools;
import com.matrix.system.shopXcx.api.dto.XcxUserSaveUserInfoDto;
import com.matrix.system.shopXcx.dao.ShopCouponRecordDao;
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.*;
 
import java.util.Collections;
 
/**
 * @author jyy
 * @description (用户表)
 * @date 2019-05-31 10:03
 */
@Controller
@RequestMapping(value = "wxapi/user")
public class WxUserAction extends BaseAction {
 
    @Autowired
    private SysVipInfoService sysVipInfoService;
 
    @Autowired
    private SysVipInfoDao sysVipInfoDao;
 
    @Autowired
    private ShopSalesmanApplyService shopSalesmanApplyService;
 
    @Autowired
    BusParameterSettingsDao busParameterSettingsDao;
    @Autowired
    private UserCacheManager userCacheManager;
    @Autowired
    ShopSalesmanApplyDao salesmanApplyDao;
 
    @Autowired
    WeChatApiTools weChatApiTools;
 
    @Autowired
    private ScoreVipDetailDao scoreVipDetailDao;
 
    @Autowired
    private MoneyCardUseDao moneyCardUseDao;
 
    @Autowired
    ShopCouponRecordDao shopCouponRecordDao;
 
    @Autowired
    ActivitySignReceiveRecordDao activitySignReceiveRecordDao;
    @Value("${debug}")
    private String isDebug;
 
    /**
     * 小程序登录
     *
     * @param code
     * @return
     * @throws Exception
     * @author JIANGYOUYAO
     * @email 935090232@qq.com
     * @date 2018年5月23日
     */
    @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, HostInterceptor.getCompanyId());
            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);
            // 查询用户是否存在
            SysVipInfo sysVipInfo = null;
            synchronized (this) {
                sysVipInfo = sysVipInfoService.findByOpenId(openId);
                if (sysVipInfo == null) {
                    // 新增用户
                    sysVipInfo = new SysVipInfo();
                    sysVipInfo.setIsSales(SysVipInfo.NOT_SALES);
                    sysVipInfo.setOpenId(openId);
                    sysVipInfo.setSessionKey(sessionKey);
                    sysVipInfo.setCompanyId(HostInterceptor.getCompanyId());
                    sysVipInfo.setArrivalWay("微商城");
                    sysVipInfo.setVipName("微信用户");
                    sysVipInfo.setVipState(Dictionary.VIP_STATE_HY);
                    sysVipInfo.setVipType(Dictionary.VIP_TYPE_NOCARD);
                    sysVipInfo.setIsDeal(SysVipInfo.UNDEAL_VIP);
                    sysVipInfo.setVipNo(openId);
                    sysVipInfoService.add(sysVipInfo);
 
                } else {
                    sysVipInfo.setSessionKey(sessionKey);
                    sysVipInfoDao.update(sysVipInfo);
                }
            }
            // 存放redis
            sysVipInfo.setLevelCard(null);
            String token = userCacheManager.saveUserInfo(sysVipInfo);
            LogUtil.info("用户token={}", token);
            res.putInMap("token", token);
            res.putInMap("userInfo", sysVipInfo);
            res.setStatus(AjaxResult.STATUS_SUCCESS);
        } else {
            res.setStatus(AjaxResult.STATUS_FAIL);
            res.setInfo("自动登录失败");
        }
        return res;
    }
 
 
    @Autowired
    ShopSalesmanGradeDao shopSalesmanGradeDao;
 
    /**
     * 查询用户信息
     */
    @RequestMapping(value = "/findUserInfo")
    public @ResponseBody
    AjaxResult findUserInfo() {
        SysVipInfo loginUser = userCacheManager.getLoginUser();
        SysVipInfo sysVipInfo = sysVipInfoService.findById(loginUser.getId());
        sysVipInfo.setPointAll(scoreVipDetailDao.selectUserTotalScore(loginUser.getId()));
        sysVipInfo.setBalance(moneyCardUseDao.selectVipCardTotalMoney(loginUser.getId()));
        AjaxResult res = new AjaxResult();
        res.putInMap("couponCount", shopCouponRecordDao.countUnuseCouponRecord(loginUser.getId()));
        res.putInMap("prizeCount", activitySignReceiveRecordDao.getSignAwardReceiveCount(loginUser.getId(), loginUser.getCompanyId()));
        res.putInMap("userInfo", sysVipInfo);
        //查询会员等级
        if (sysVipInfo.getSalesmanGrade() != null) {
            ShopSalesmanGrade shopSalesmanGrade = shopSalesmanGradeDao.selectById(sysVipInfo.getSalesmanGrade());
            res.putInMap("vipLevel", shopSalesmanGrade.getName());
        } else if (sysVipInfo.getVipLevel() != null) {
            res.putInMap("vipLevel", sysVipInfo.getVipLevel().getLevelName());
        } else {
            res.putInMap("vipLevel", "初级会员");
        }
        res.setStatus(AjaxResult.STATUS_SUCCESS);
        return res;
    }
 
    /**
     * 根据OpenID查询用户信息
     */
    @GetMapping(value = "/findUserInfo/{openId}")
    public @ResponseBody
    AjaxResult findUserInfo(@PathVariable String openId) {
        SysVipInfo sysVipInfo = sysVipInfoService.findByOpenId(openId);
        SysVipInfo bizUserQuery = new SysVipInfo();
        bizUserQuery.setNickName(sysVipInfo.getNickName());
        bizUserQuery.setAvatarUrl(sysVipInfo.getAvatarUrl());
        bizUserQuery.setOpenId(openId);
        return AjaxResult.buildSuccessInstance(bizUserQuery);
    }
 
 
    /**
     * 接收用户信息
     */
    @PostMapping(value = "/saveUserInfo")
    public @ResponseBody
    AjaxResult saveUserInfo(@RequestBody XcxUserSaveUserInfoDto xcxUserSaveUserInfoDto) {
 
        SysVipInfo loginUser = userCacheManager.getLoginUser();
        SysVipInfo sysVipInfo = new SysVipInfo();
        sysVipInfo.setId(loginUser.getId());
 
        BeanUtil.copyProperties(xcxUserSaveUserInfoDto, sysVipInfo);
        sysVipInfo.setPhone(xcxUserSaveUserInfoDto.getPhoneNumber());
        if (StringUtils.isBlank(loginUser.getVipName()) || loginUser.getVipName().equals("微信用户")) {
            sysVipInfo.setVipName(xcxUserSaveUserInfoDto.getNickName());
        }
        if (StringUtils.isBlank(loginUser.getSex())) {
            sysVipInfo.setSex(xcxUserSaveUserInfoDto.getGender() == 1 ? "男" : "女");
        }
 
        int i = sysVipInfoDao.update(sysVipInfo);
 
        checkNeedToBeSalesman(sysVipInfo, loginUser);
        if (i == 0) {
            return new AjaxResult(AjaxResult.STATUS_FAIL, "更新失败");
        }
        return new AjaxResult(AjaxResult.STATUS_SUCCESS, Collections.singletonList(sysVipInfo));
    }
 
 
    void checkNeedToBeSalesman(SysVipInfo sysVipInfo, SysVipInfo loginUser) {
        //查询是否需要自动成为分销员
        if ((loginUser.getIsSales() == null || loginUser.getIsSales() == 2)
                && StringUtils.isNotBlank(sysVipInfo.getNickName())) {//目前还不是分销员
            BusParameterSettings fxSwith = busParameterSettingsDao.selectCompanyParamByCode(FenxiaoSettingConstant.FX_SWITCH, HostInterceptor.getCompanyId());
            if (fxSwith!=null && fxSwith.getParamValue().equals("1")) {//开启分销
                BusParameterSettings applyWay = busParameterSettingsDao.selectCompanyParamByCode(FenxiaoSettingConstant.FX_APPLY_WAY, loginUser.getCompanyId());
                if (applyWay.getParamValue().equals(FenxiaoSettingConstant.FX_APPLY_WAY_AUTO)) {//自动成为分销员
                    QueryWrapper<ShopSalesmanApply> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("user_id", loginUser.getId());
                    queryWrapper.eq("apply_status", ShopSalesmanApply.APPLY_STATUS_DSH);
                    ShopSalesmanApply shopSalesmanApply = salesmanApplyDao.selectOne(queryWrapper);
                    if (shopSalesmanApply == null) {//没有待审核记录
                        shopSalesmanApplyService.applyToBeAnSalesman(loginUser.getId(), null, null, 2);
                    }
                }
            }
        }
    }
 
 
}