|
|
@@ -1,11 +1,16 @@
|
|
|
package org.dromara.main.service.impl;
|
|
|
|
|
|
+import cn.dev33.satoken.stp.StpUtil;
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.core.util.RandomUtil;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
+import org.dromara.common.core.exception.ServiceException;
|
|
|
+import org.dromara.common.core.utils.StringUtils;
|
|
|
import org.dromara.common.mybatis.core.page.PageQuery;
|
|
|
import org.dromara.common.mybatis.core.page.TableDataInfo;
|
|
|
import org.dromara.main.domain.MainStudent;
|
|
|
@@ -14,16 +19,22 @@ import org.dromara.main.domain.MainStudentExperience;
|
|
|
import org.dromara.main.domain.MainStudentProject;
|
|
|
import org.dromara.main.domain.bo.MainStudentBo;
|
|
|
import org.dromara.main.domain.vo.MainStudentVo;
|
|
|
+
|
|
|
+import org.dromara.main.domain.vo.MiniappLoginVo;
|
|
|
import org.dromara.main.mapper.MainStudentEducationMapper;
|
|
|
import org.dromara.main.mapper.MainStudentExperienceMapper;
|
|
|
import org.dromara.main.mapper.MainStudentMapper;
|
|
|
import org.dromara.main.mapper.MainStudentProjectMapper;
|
|
|
import org.dromara.main.service.IMainStudentService;
|
|
|
+import org.dromara.main.utils.WechatMiniAppUtil;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
import java.util.Collection;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
+
|
|
|
@RequiredArgsConstructor
|
|
|
@Service
|
|
|
public class MainStudentServiceImpl implements IMainStudentService {
|
|
|
@@ -123,4 +134,83 @@ public class MainStudentServiceImpl implements IMainStudentService {
|
|
|
return baseMapper.updateById(student) > 0;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public MiniappLoginVo miniAppLogin(String code, String phoneCode) {
|
|
|
+ // 1. 调用微信接口, 用 code 换取 openid 和 session_key
|
|
|
+ JSONObject sessionData = WechatMiniAppUtil.code2Session(code);
|
|
|
+ if (sessionData == null) {
|
|
|
+ throw new ServiceException("微信登录失败,请重试");
|
|
|
+ }
|
|
|
+ String openid = sessionData.getStr("openid");
|
|
|
+ String sessionKey = sessionData.getStr("session_key");
|
|
|
+ String unionId = sessionData.getStr("unionid");
|
|
|
+ if (StringUtils.isBlank(openid)) {
|
|
|
+ throw new ServiceException("获取微信openid失败");
|
|
|
+ }
|
|
|
+ // 2. 获取手机号
|
|
|
+ JSONObject phoneInfo = WechatMiniAppUtil.getPhoneNumberByCode(phoneCode);
|
|
|
+ if (phoneInfo == null) {
|
|
|
+ throw new ServiceException("获取手机号失败,请重试");
|
|
|
+ }
|
|
|
+ String phoneNumber = phoneInfo.getStr("purePhoneNumber");
|
|
|
+ if (StringUtils.isBlank(phoneNumber)) {
|
|
|
+ throw new ServiceException("获取手机号失败");
|
|
|
+ }
|
|
|
+ // 3. 查询或创建学员账号
|
|
|
+ boolean isNewUser = false;
|
|
|
+
|
|
|
+ // 优先根据 openid 查询
|
|
|
+ MainStudent student = baseMapper.selectOne(
|
|
|
+ Wrappers.lambdaQuery(MainStudent.class).eq(MainStudent::getOpenid, openid)
|
|
|
+ );
|
|
|
+ if (student != null) {
|
|
|
+ // 已有记录,更新最新数据
|
|
|
+ student.setSessionKey(sessionKey);
|
|
|
+ student.setMobile(phoneNumber);
|
|
|
+ if (StringUtils.isNotBlank(unionId)) {
|
|
|
+ student.setUnionId(unionId);
|
|
|
+ }
|
|
|
+ baseMapper.updateById(student);
|
|
|
+ } else {
|
|
|
+ // openid 未查询到,根据手机号查询
|
|
|
+ student = baseMapper.selectOne(
|
|
|
+ Wrappers.lambdaQuery(MainStudent.class).eq(MainStudent::getMobile, phoneNumber)
|
|
|
+ );
|
|
|
+
|
|
|
+ if (student != null) {
|
|
|
+ // 绑定到已有手机号账号
|
|
|
+ student.setOpenid(openid);
|
|
|
+ student.setSessionKey(sessionKey);
|
|
|
+ if (StringUtils.isNotBlank(unionId)) {
|
|
|
+ student.setUnionId(unionId);
|
|
|
+ }
|
|
|
+ baseMapper.updateById(student);
|
|
|
+ } else {
|
|
|
+ // 新用户,执行创建
|
|
|
+ isNewUser = true;
|
|
|
+ student = new MainStudent();
|
|
|
+ student.setName("用户_" + RandomUtil.randomString(6));
|
|
|
+ student.setGender("");
|
|
|
+ student.setOpenid(openid);
|
|
|
+ student.setSessionKey(sessionKey);
|
|
|
+ student.setUnionId(unionId);
|
|
|
+ student.setMobile(phoneNumber);
|
|
|
+ student.setUserType("2"); // 2: 标识为普通用户/新学员
|
|
|
+ student.setStatus("0"); // 0: 正常状态
|
|
|
+ baseMapper.insert(student);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 4. 执行登录生成 token(如果你的项目用的是其他的 LoginHelper,请替换)
|
|
|
+ StpUtil.login(student.getId());
|
|
|
+ String token = StpUtil.getTokenValue();
|
|
|
+ // 5. 组合组装返回结果
|
|
|
+ MiniappLoginVo result = new MiniappLoginVo();
|
|
|
+ result.setToken(token);
|
|
|
+ result.setStudentId(student.getId());
|
|
|
+ result.setMobile(student.getMobile());
|
|
|
+ result.setName(student.getName());
|
|
|
+ result.setIsNewUser(isNewUser);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
}
|