姜友瑶
2022-08-09 2c62440d1071c5358d167af84fad0223044b4353
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
package com.matrix.system.shopXcx.api.action;
 
import com.alibaba.fastjson.JSON;
import com.matrix.component.tools.AES;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.tools.LogUtil;
import com.matrix.core.tools.StringUtils;
import com.matrix.system.common.init.UserCacheManager;
import com.matrix.system.hive.bean.SysVipInfo;
import com.matrix.system.hive.dao.SysVipInfoDao;
import com.matrix.system.shopXcx.api.pojo.BindingPhoneNumberPOJO;
import com.matrix.system.shopXcx.api.tools.SMSVerifiTools;
import com.matrix.system.shopXcx.api.vo.DataDecipheringVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.RandomStringUtils;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;
 
/**
 * @author jiangyouyao
 * @description 绑定手机号
 * @date 2019-07-17 10:15
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
@RequestMapping(value = "wxapi/shopBindingPhoneNumber")
public class WxBindingPhoneNumber {
    @Autowired
    private SMSVerifiTools sMSVerifiTools;
    @Autowired
    private SysVipInfoDao sysVipInfoDao;
 
    @Autowired
    private UserCacheManager userCacheManager;
 
 
    @Value("${evn}")
    private String evn;
 
    /**
     * 获取手机验证码
     *
     * @param
     * @return
     */
    @PostMapping("/getPhonVerifyCode/{phoneNumber}")
    @ResponseBody
    public AjaxResult getPhonVerifyCode(HttpServletRequest request, HttpServletResponse response, @PathVariable("phoneNumber") String phoneNumber) {
        //首先判断该手机号是否被绑定过
        if (StringUtils.isBlank(phoneNumber)) {
            return new AjaxResult(AjaxResult.STATUS_FAIL, "手机号码不能为空");
        }
        SysVipInfo sysVipInfo = new SysVipInfo();
        sysVipInfo.setPhone(phoneNumber);
        List<SysVipInfo> bizUsers = sysVipInfoDao.selectByModel(sysVipInfo);
        if (CollectionUtils.isNotEmpty(bizUsers) && bizUsers.size() > 0) {
            return new AjaxResult(AjaxResult.STATUS_FAIL, "该手机号已被绑定");
        }
        //生成短信验证码
        String randomCode = RandomStringUtils.randomNumeric(4);
        //将短信验证码保存到redis中,设置30分钟失效
        //TODO 修复这个功能
        //redisClient.saveValue("phonVerifyCode" + phoneNumber, randomCode, 1800);
 
        try {
            //编辑短信内容
            String msg = "您的验证码为:" + randomCode + ",该验证码仅用于身份验证,30分钟内有效,请勿泄露于他人。";
            //调用工具类发送短信发送短信码
            sMSVerifiTools.sendMsg(phoneNumber, msg);
            LogUtil.info("调用了短信接口");
        } catch (Exception e) {
            LogUtil.info("手机验证码发送失败。。。", e);
            return new AjaxResult(AjaxResult.STATUS_FAIL, "手机验证码发送失败");
        }
        AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, "手机验证码发送成功");
        return result;
    }
 
 
    /**
     * @param bindingPhoneNumber
     * @return 返回类型 AjaxResult
     *  绑定手机号码
     * @author:jiangyouyao
     */
    @PostMapping("/bindingPhoneNumber")
    @ResponseBody
    public AjaxResult bindingPhoneNumber(@RequestBody BindingPhoneNumberPOJO bindingPhoneNumber) {
        if (StringUtils.isBlank(bindingPhoneNumber.getPhoneNumber())) {
            return new AjaxResult(AjaxResult.STATUS_FAIL, "手机号码不能为空");
        }
        SysVipInfo loginUser = userCacheManager.getLoginUser();
 
 
        SysVipInfo oldeUser = sysVipInfoDao.selectByPhone(bindingPhoneNumber.getPhoneNumber(), loginUser.getCompanyId());
        if(oldeUser!=null && !Objects.equals(oldeUser.getId(),loginUser.getId())){
            oldeUser.setIsSales(loginUser.getIsSales());
            oldeUser.setOpenId(loginUser.getOpenId());
            oldeUser.setSessionKey(loginUser.getSessionKey());
            oldeUser.setPhoto(loginUser.getPhoto());
            oldeUser.setNickName(loginUser.getNickName());
            oldeUser.setAvatarUrl(loginUser.getAvatarUrl());
            sysVipInfoDao.update(oldeUser);
            LogUtil.info("绑定手机号码删除老会员openId。。"+ JSON.toJSONString(loginUser));
//            sysVipInfoDao.deleteLogicByIds(Arrays.asList(loginUser.getId()));
            loginUser.setOpenId("--");
            loginUser.setPhone("--");
            sysVipInfoDao.update(loginUser);
            String token = userCacheManager.saveUserInfo(oldeUser);
            AjaxResult result =   AjaxResult.buildSuccessInstance("绑定成功");
            result.putInMap("token",token);
            result.putInMap("userInfo",oldeUser);
            return result;
        }else{
 
            //验证通过将手机号加入相应的用户数据中
            SysVipInfo sysVipInfo = new SysVipInfo();
 
            sysVipInfo.setId(loginUser.getId());
            if(loginUser.getShopId()==null){
                sysVipInfo.setShopId(bindingPhoneNumber.getShopId());
            }
            sysVipInfo.setPhone(bindingPhoneNumber.getPhoneNumber());
            sysVipInfoDao.update(sysVipInfo);
            return new AjaxResult(AjaxResult.STATUS_SUCCESS, "绑定成功");
        }
 
 
    }
 
 
 
    /**
     * 手机号码解密
     *
     * @param dataDecipheringVo
     * @return
     * @author JIANGYOUYAO
     * @email 935090232@qq.com
     * @date 2018年8月28日
     */
    @RequestMapping(value = "/deciphering")
    public @ResponseBody
    AjaxResult deciphering(@RequestBody DataDecipheringVo dataDecipheringVo) {
        SysVipInfo loginUser = userCacheManager.getLoginUser();
        String session_key = loginUser.getSessionKey();
        LogUtil.debug(dataDecipheringVo.toString());
        String jsonResult = AES.wxDecrypt(dataDecipheringVo.getEncryptedData(), session_key, dataDecipheringVo.getIv());
        AjaxResult result = new AjaxResult();
        result.setStatus(AjaxResult.STATUS_SUCCESS);
        result.putInMap("decipher", jsonResult);
        return result;
    }
 
 
}