jiangqs
2023-04-20 437a8ae4bcca79e8886662a40c11f499fea1a25e
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
package com.hrt.system.service.impl;
 
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hrt.common.core.exception.ServiceException;
import com.hrt.common.core.utils.StringUtils;
import com.hrt.common.core.utils.uuid.UUID;
import com.hrt.common.security.utils.SecurityUtils;
import com.hrt.system.api.domain.SysUser;
import com.hrt.system.constant.AppErrorConstant;
import com.hrt.system.constant.SecurityConstant;
import com.hrt.system.domain.dto.AppMiniLoginDto;
import com.hrt.system.domain.dto.AppNearbyShopDto;
import com.hrt.system.domain.dto.AppUserAuthorizeDto;
import com.hrt.system.domain.poji.member.Member;
import com.hrt.system.domain.poji.shop.Shop;
import com.hrt.system.domain.vo.AppMiniLoginVo;
import com.hrt.system.domain.vo.AppNearbyShopVo;
import com.hrt.system.domain.vo.AppUserAuthorizeVo;
import com.hrt.system.mapper.member.MemberMapper;
import com.hrt.system.service.member.MemberService;
import com.hrt.system.service.shop.ShopService;
import com.hrt.system.service.user.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.Date;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jqs
 * @since 2023-04-19
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {
 
 
    @Resource
    private WxMaService wxMaService;
 
    @Resource
    private ISysUserService sysUserService;
 
    @Resource
    private MemberMapper memberMapper;
 
    @Resource
    private ShopService shopService;
 
    @Resource
    private StringRedisTemplate stringRedisTemplate;
 
    /**
     *
     * @param appMiniLoginDto
     * @return
     */
    @Override
    public AppMiniLoginVo getMemberByCode(AppMiniLoginDto appMiniLoginDto){
 
        AppMiniLoginVo appMiniLoginVo = new AppMiniLoginVo();
        WxMaJscode2SessionResult session = null;
        String unionid;
        String openid;
        String sessionKey = null;
        //获取session
        try {
            session = wxMaService.getUserService().getSessionInfo(appMiniLoginDto.getCode());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        if(session!=null&& StringUtils.isNotBlank(session.getOpenid())){
            unionid = session.getUnionid();
            openid = session.getOpenid();
            sessionKey = session.getSessionKey();
            //获取用户
            Member member = memberMapper.getOneByMiniOpenid(openid);
            SysUser sysUser;
            if(member==null){
                //创建新用户
                String memberId = UUID.randomUUID().toString();
                sysUser = new SysUser();
                sysUser.setUserName(memberId);
                sysUser.setNickName("微信用户");
                String password = "hongruitang";
                sysUser.setPassword(SecurityUtils.encryptPassword(password));
                sysUserService.registerUser(sysUser);
                member = new Member();
                member.setUserId(sysUser.getUserId());
                member.setDelFlag(0);
                member.setMemberId(memberId);
                member.setWxUnionid(unionid);
                member.setMiniOpenid(openid);
                member.setCreateTime(new Date());
                this.save(member);
            }else{
                sysUser = sysUserService.selectUserById(member.getUserId());
            }
            appMiniLoginVo.setMiniOpenid(openid);
            appMiniLoginVo.setWxUnionid(unionid);
            appMiniLoginVo.setSysUser(sysUser);
            appMiniLoginVo.setSessionKey(sessionKey);
            //sessionKey
            stringRedisTemplate.opsForValue().set(SecurityConstant.SESSION_KEY+openid,sessionKey);
        }else{
            return null;
        }
        return appMiniLoginVo;
    }
 
    /**
     * 获取授权信息
     * @param appUserAuthorizeDto
     * @return
     */
    @Override
    public AppUserAuthorizeVo getAppUserAuthorize(AppUserAuthorizeDto appUserAuthorizeDto){
        Member member = this.getById(appUserAuthorizeDto.getUserId());
        AppUserAuthorizeVo appUserAuthorizeVo = new AppUserAuthorizeVo();
        //获取code接口在redis里存放的sessionkey用于解密
        String sessionkey = stringRedisTemplate.opsForValue().get(SecurityConstant.SESSION_KEY+ member.getMiniOpenid());
        if(StringUtils.isBlank(sessionkey)){
            throw new ServiceException(AppErrorConstant.USER_NOT_LOGIN);
        }
        String mobile = null;
        try {
            //解密微信加密用户信息和手机号
            WxMaPhoneNumberInfo wxPhoneInfo;
            if(StringUtils.isNotBlank(appUserAuthorizeDto.getPhoneEncryptedData())&&StringUtils.isNotBlank(appUserAuthorizeDto.getPhoneIv())){
                wxPhoneInfo = wxMaService.getUserService().getPhoneNoInfo(sessionkey, appUserAuthorizeDto.getPhoneEncryptedData(), appUserAuthorizeDto.getPhoneIv());
            }else{
                throw new ServiceException(AppErrorConstant.AUTHORIZE_MISS);
            }
            if(StringUtils.isBlank(wxPhoneInfo.getPhoneNumber())){
                throw new ServiceException(AppErrorConstant.AUTHORIZE_FAILED);
            }
            mobile = wxPhoneInfo.getPhoneNumber();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //更新用户手机信息
        SysUser sysUser = sysUserService.selectUserById(appUserAuthorizeDto.getUserId());
        sysUser.setPhonenumber(mobile);
        sysUserService.updateUser(sysUser);
        member.setMobile(mobile);
        this.saveOrUpdate(member);
        appUserAuthorizeVo.setMobile(mobile);
        appUserAuthorizeVo.setNickName(sysUser.getNickName());
        appUserAuthorizeVo.setOpenid(member.getMiniOpenid());
        appUserAuthorizeVo.setUnionid(member.getWxUnionid());
        return appUserAuthorizeVo;
    }
 
    /**
     * 获取附近门店
     * @param appNearbyShopDto
     * @return
     */
    @Override
    public AppNearbyShopVo getNearbyShop(AppNearbyShopDto appNearbyShopDto){
        Member member = this.getById(appNearbyShopDto.getUserId());
        AppNearbyShopVo appNearbyShopVo = new AppNearbyShopVo();
        Shop shop = null;
        if(member.getRealtionShopId()!=null){
            //获取绑定商户
            shop = shopService.getById(member.getRealtionShopId());
        }else{
            //获取附近商户
            shop = shopService.getById(1L);
        }
        appNearbyShopVo.setShopId(shop.getShopId());
        appNearbyShopVo.setShopName(shop.getShopName());
        appNearbyShopVo.setShopAddress(shop.getShopAreaName()+shop.getShopAddress());
        appNearbyShopVo.setShopLatitude(shop.getShopLatitude());
        appNearbyShopVo.setShopLongitude(shop.getShopLongitude());
        return appNearbyShopVo;
    }
}