Просмотр исходного кода

苹果登录,appOpenId,appleKey

ct 1 неделя назад
Родитель
Сommit
32286c2f69

+ 4 - 0
fs-service/src/main/java/com/fs/his/domain/FsUser.java

@@ -186,6 +186,10 @@ public class FsUser extends BaseEntity
     /** app登录后不为null(表示是否下载app) */
     private String historyApp;
 
+    private String appOpenId;
+
+    private String appleKey; // 苹果key登陆验证
+
     public void setNickName(String nickname)
     {
         if(StringUtils.isNotEmpty(nickname)){

+ 8 - 0
fs-service/src/main/java/com/fs/his/mapper/FsUserMapper.java

@@ -471,4 +471,12 @@ public interface FsUserMapper
     List<FsUser> selectFsUserListByPhone(String phone);
 
     void updatePasswordByPhone(@Param("password")String password, @Param("encryptPhone")String encryptPhone);
+
+    /**
+     * 查询用户列表(用于积分管理,支持手机号和昵称模糊查询)
+     */
+    List<FsUser> selectFsUserListForIntegral(FsUser fsUser);
+
+    @Select("select * from fs_user where apple_key = #{appleKey}")
+    FsUser findUserByAppleKey(String appleKey);
 }

+ 23 - 1
fs-service/src/main/resources/mapper/his/FsUserMapper.xml

@@ -50,10 +50,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         <result property="qwUserId"    column="qw_user_id"    />
         <result property="appId"    column="app_id"    />
         <result property="level" column="level"/>
+        <result property="appOpenId"    column="app_open_id"    />
+        <result property="appleKey"    column="apple_key"    />
     </resultMap>
 
     <sql id="selectFsUserVo">
-        select user_id,qw_ext_id,sex,is_buy,`level`,course_ma_open_id,is_push,is_add_qw,source,login_device,is_individuation_push,store_open_id,password,jpush_id, is_vip,vip_start_date,vip_end_date,vip_level,vip_status,nick_name,integral_status, avatar, phone, integral,sign_num, status, tui_user_id, tui_time, tui_user_count, ma_open_id, mp_open_id, union_id, is_del, user_code, remark, create_time, update_time, last_ip, balance,is_weixin_auth,parent_id,qw_user_id,app_id,company_id,company_user_id,is_promoter,now_money,brokerage_price,spread_user_id, spread_time,pay_count, spread_count,user_type from fs_user
+        select user_id,qw_ext_id,sex,is_buy,`level`,course_ma_open_id,is_push,is_add_qw,source,login_device,is_individuation_push,store_open_id,password,jpush_id, is_vip,vip_start_date,vip_end_date,vip_level,vip_status,nick_name,integral_status, avatar, phone, integral,sign_num, status, tui_user_id, tui_time, tui_user_count, ma_open_id, mp_open_id, union_id, is_del, user_code, remark, create_time, update_time, last_ip, balance,is_weixin_auth,parent_id,qw_user_id,app_id,company_id,company_user_id,is_promoter,now_money,brokerage_price,spread_user_id, spread_time,pay_count, spread_count,user_type,app_open_id,apple_key from fs_user
     </sql>
 
     <select id="selectFsUserList" parameterType="FsUser" resultMap="FsUserResult">
@@ -589,6 +591,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="payCount != null">pay_count,</if>
             <if test="spreadCount != null">spread_count,</if>
             <if test="appId != null">app_id,</if>
+            <if test="appOpenId != null">app_open_id,</if>
+            <if test="appleKey != null">apple_key,</if>
          </trim>
         <trim prefix="values (" suffix=")" suffixOverrides=",">
             <if test="nickName != null">#{nickName},</if>
@@ -639,6 +643,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="payCount != null">#{payCount},</if>
             <if test="spreadCount != null">#{spreadCount},</if>
             <if test="appId != null">#{appId},</if>
+            <if test="appOpenId != null">#{appOpenId},</if>
+            <if test="appleKey != null">#{appleKey},</if>
          </trim>
     </insert>
 
@@ -691,6 +697,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="orderCount != null">order_count = #{orderCount},</if>
             <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
             <if test="appId != null">app_id = #{appId},</if>
+            <if test="appOpenId != null">app_open_id = #{appOpenId},</if>
+            <if test="appleKey != null">apple_key = #{appleKey},</if>
         </trim>
         where user_id = #{userId}
     </update>
@@ -2432,6 +2440,20 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     <select id="selectFsUserListByPhone" resultType="com.fs.his.domain.FsUser">
         select * from fs_user where phone=#{phone}
     </select>
+    <!-- 查询用户列表(用于积分管理) -->
+    <select id="selectFsUserListForIntegral" parameterType="FsUser" resultMap="FsUserResult">
+        select user_id, nick_name, phone, integral from fs_user
+        <where>
+            and is_del = 0
+            <if test="phone != null and phone != ''">
+                and phone like concat('%', #{phone}, '%')
+            </if>
+            <if test="nickName != null and nickName != ''">
+                and (nick_name like concat('%', #{nickName}, '%') or nickname like concat('%', #{nickName}, '%'))
+            </if>
+        </where>
+        order by user_id desc
+    </select>
 
 
 </mapper>

+ 128 - 9
fs-user-app/src/main/java/com/fs/app/controller/AppLoginController.java

@@ -3,6 +3,7 @@ package com.fs.app.controller;
 
 import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.date.DateTime;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.fs.app.annotation.Login;
 import com.fs.app.param.*;
 import com.fs.app.utils.WxUtil;
@@ -14,6 +15,8 @@ import com.fs.common.exception.ServiceException;
 import com.fs.common.service.ISmsService;
 import com.fs.common.utils.sign.Md5Utils;
 import com.fs.core.config.WxOpenProperties;
+import com.fs.course.domain.FsCoursePlaySourceConfig;
+import com.fs.course.service.IFsCoursePlaySourceConfigService;
 import com.fs.his.config.FsSysConfig;
 import com.fs.his.domain.FsUser;
 import com.fs.his.domain.FsUserNewTask;
@@ -65,6 +68,9 @@ public class AppLoginController extends AppBaseController{
 
     @Autowired
     private ISmsService smsService;
+    @Autowired
+    private IFsCoursePlaySourceConfigService fsCoursePlaySourceConfigService;
+
     @ApiOperation("注册app用户")
     @PostMapping("/register")
     @RepeatSubmit
@@ -232,8 +238,29 @@ public class AppLoginController extends AppBaseController{
             if (StringUtils.isBlank(param.getCode())) {
                 return R.error("code不存在");
             }
-            logger.info("zyp app微信登录,param:{},输出appid,{},secret:{}", param, openProperties.getAppId(), openProperties.getSecret());
-            Map result = WxUtil.getAccessToken(param.getCode(), openProperties.getAppId(), openProperties.getSecret());
+
+            String appId = param.getAppId();
+            String appSecret = "";
+            if (StringUtils.isBlank(appId)) {
+                List<FsCoursePlaySourceConfig> list = fsCoursePlaySourceConfigService.list(new QueryWrapper<FsCoursePlaySourceConfig>().eq("name", "app").eq("is_del", 0));
+                if(!list.isEmpty()){
+                    FsCoursePlaySourceConfig fsCoursePlaySourceConfig = list.get(0);
+                    appId = fsCoursePlaySourceConfig.getAppid();
+                    appSecret =  fsCoursePlaySourceConfig.getSecret();
+                }
+            } else {
+                FsCoursePlaySourceConfig fsCoursePlaySourceConfig = fsCoursePlaySourceConfigService.getOne(new QueryWrapper<FsCoursePlaySourceConfig>().eq("appid", appId).eq("is_del", 0));
+                if(fsCoursePlaySourceConfig != null){
+                    appId = fsCoursePlaySourceConfig.getAppid();
+                    appSecret =  fsCoursePlaySourceConfig.getSecret();
+                }
+            }
+            if (StringUtils.isBlank(appId) || StringUtils.isBlank(appSecret)) {
+                appId = openProperties.getAppId();
+                appSecret = openProperties.getSecret();
+            }
+            logger.info("zyp app微信登录,param:{},输出appid,{},secret:{}", param, appId, appSecret);
+            Map result = WxUtil.getAccessToken(param.getCode(),appId,appSecret);
             String accessToken = result.get("access_token").toString();
             String unionid = result.get("unionid").toString();
 
@@ -253,12 +280,13 @@ public class AppLoginController extends AppBaseController{
                 user.setSource(param.getSource()!=null ? param.getSource() : null);
                 user.setNickName(nickname);
                 user.setAvatar(avatar);
+                user.setAppOpenId(openid);
                 if (sex!=0){
                     user.setSex(sex);
                 }
                 user.setUnionId(unionid);
                 // 新用户 - 添加 appId
-                user.setAppId(openProperties.getAppId());
+                user.setAppId(appId);
                 user.setCreateTime(new Date());
                 user.setStatus(1);
                 if (StringUtils.isNotEmpty(param.getJpushId())) {
@@ -270,15 +298,16 @@ public class AppLoginController extends AppBaseController{
                 return R.ok(map);
             } else {
                 // 老用户 - 检查并添加appId(不重复添加)
-                String updatedAppId = addAppIdIfNotExists(user.getAppId(), openProperties.getAppId());
+                String updatedAppId = addAppIdIfNotExists(user.getAppId(), appId);
                 if (!updatedAppId.equals(user.getAppId())) {
                     FsUser userMap = new FsUser();
                     userMap.setUserId(user.getUserId());
                     userMap.setAppId(updatedAppId);
                     userService.updateFsUser(userMap);
                 }
-                
+
                 if (StringUtils.isNotEmpty(param.getJpushId())) {
+                    user.setAppOpenId(openid);
                     updateExistingUserJpushId(user, param.getJpushId());
                 }
                 if (StringUtils.isEmpty(user.getPhone())) {
@@ -301,6 +330,91 @@ public class AppLoginController extends AppBaseController{
 
     }
 
+    @ApiOperation("苹果登录")
+    @PostMapping("/loginByApple")
+    @Transactional
+    public R loginByApple(@Validated @RequestBody FsUserLoginByAppleParam param) {
+        try {
+            if (StringUtils.isEmpty(param.getAppleKey())) {
+                return R.error("获取苹果key失败");
+            }
+            // 根据苹果key查询用户
+            FsUser user = userMapper.findUserByAppleKey(param.getAppleKey());
+            Map<String, Object> map = new HashMap<>();
+            if (user == null) {
+                map.put("isNew", true);
+                return R.ok(map);
+            } else {
+                if (StringUtils.isNotEmpty(param.getJpushId())) {
+                    updateExistingUserJpushId(user, param.getJpushId());
+                }
+                if (StringUtils.isEmpty(user.getPhone())) {
+                    map.put("isNew", true);
+                    return R.ok(map);
+                }
+            }
+            /*if (user.getStatus()==0){
+                return R.error("登录失败,账户被禁用");
+            }*/
+            int isFirstLogin = userNewTaskService.performTaskOne(user.getUserId());
+            String token = jwtUtils.generateToken(user.getUserId());
+            redisCache.setCacheObject("userToken:" + user.getUserId(), token, 604800, TimeUnit.SECONDS);
+            map.put("token", token);
+            map.put("user", user);
+            map.put("isFirst",isFirstLogin);
+            return R.ok(map);
+        }catch (Exception e){
+            logger.error("zyp 苹果登录失败:{}", e.getMessage());
+            return R.error("登录失败");
+        }
+    }
+
+    private FsUser createNewAppleUser(FsUserEditPhoneParam param, String phoneNumber) {
+        FsUser newUser = new FsUser();
+        newUser.setLoginDevice(param.getLoginDevice()!=null ? param.getLoginDevice() : null);
+        newUser.setSource(param.getSource()!=null ? param.getSource() : null);
+        newUser.setAppleKey(param.getAppleKey());
+        newUser.setPhone(param.getPhone());
+        newUser.setPassword(Md5Utils.hash(param.getPassword()));
+        newUser.setNickName("苹果用户" + param.getPhone().substring(param.getPhone().length() - 4));
+        newUser.setCreateTime(new Date());
+        newUser.setStatus(1);
+        newUser.setAvatar("https://cos.his.cdwjyyh.com/fs/20240926/420728ee06e54575ba82665dedb4756b.png");
+        if (StringUtils.isNotEmpty(param.getJpushId())) {
+            newUser.setJpushId(param.getJpushId());
+        }
+        userService.insertFsUser(newUser);
+        return newUser;
+
+    }
+
+    @ApiOperation("绑定手机号")
+    @PostMapping("/setIPhoneNumber")
+    public R setIPhoneNumber(@Validated @RequestBody FsUserEditPhoneParam param) {
+        FsUser userMap = findUserByPhone(param.getPhone());
+        if (userMap != null) {
+            if (StringUtils.isNotEmpty(userMap.getAppleKey()) && !param.getAppleKey().equals(userMap.getAppleKey())) {
+                return R.error("该手机号已绑定其他账号");
+            }
+            if (param.getSimExist() == 0 && !Md5Utils.hash(param.getPassword()).equals(userMap.getPassword())) {
+                return R.error("密码不正确");
+            }
+        } else {
+            userMap = createNewAppleUser(param, param.getPhone());
+        }
+
+        userMap.setLoginDevice(param.getLoginDevice());
+        userMap.setSource(param.getSource());
+        if (userMap.getNickName().equals("匿名用户**")) {
+            userMap.setNickName("苹果用户" + param.getPhone().substring(param.getPhone().length() - 4));
+        }
+        userMap.setAppleKey(param.getAppleKey());
+        if (userService.updateFsUser(userMap)>0){
+            return generateTokenAndReturn(userMap);
+        }
+        return R.error("绑定手机号失败");
+    }
+
     @PostMapping("/loginByPhone")
     public R loginByPhone(@RequestBody Map<String,String> map){
         String phone = map.get("phone");
@@ -358,6 +472,7 @@ public class AppLoginController extends AppBaseController{
 
     @ApiOperation("绑定手机号")
     @PostMapping("/setPhone")
+    @Transactional(rollbackFor = Exception.class)
     public R setPhone(@Validated @RequestBody FsUserEditPhoneParam param) {
         logger.info("fcj 绑定手机号入参:{}", param);
         FsUser user = userService.selectFsUserByUnionid(param.getUnionId());
@@ -413,6 +528,7 @@ public class AppLoginController extends AppBaseController{
 
     @ApiOperation("绑定微信")
     @PostMapping("/bindWeChat")
+    @Transactional(rollbackFor = Exception.class)
     public R bindWeChat(@Validated @RequestBody FsUserEditUnionidParam param) {
         try {
             logger.info("zyp app绑定微信,param:{}", param);
@@ -473,7 +589,7 @@ public class AppLoginController extends AppBaseController{
                     user.setAvatar(avatar);
                     user.setSex(sex);
                     user.setUnionId(unionid);
-//                    user.setAppOpenId(openid);
+                    user.setAppOpenId(openid);
                     if (userService.updateFsUser(user)>0){
                         return generateTokenAndReturn(user);
                     }else {
@@ -492,6 +608,9 @@ public class AppLoginController extends AppBaseController{
         FsUser userMap = new FsUser();
         userMap.setUserId(user.getUserId());
         userMap.setJpushId(jpushId);
+        if (StringUtils.isNotEmpty(user.getAppOpenId())) {
+            userMap.setAppOpenId(user.getAppOpenId());
+        }
         userService.updateFsUser(userMap);
     }
 
@@ -803,12 +922,12 @@ public class AppLoginController extends AppBaseController{
         if (StringUtils.isEmpty(newAppId)) {
             return currentAppIds == null ? "" : currentAppIds;
         }
-        
+
         // 如果当前appId为空,直接返回新appId
         if (StringUtils.isEmpty(currentAppIds)) {
             return newAppId;
         }
-        
+
         // 检查是否已存在
         String[] appIdArray = currentAppIds.split(",");
         for (String appId : appIdArray) {
@@ -817,7 +936,7 @@ public class AppLoginController extends AppBaseController{
                 return currentAppIds;
             }
         }
-        
+
         // 不存在,追加到末尾
         return currentAppIds + "," + newAppId;
     }

+ 9 - 0
fs-user-app/src/main/java/com/fs/app/param/FsUserEditPhoneParam.java

@@ -21,4 +21,13 @@ public class FsUserEditPhoneParam implements Serializable {
     private String jpushId;
 
     private String source;
+
+    private String appleKey;
+
+    private String password;
+
+    /**
+     * 是否有SIM卡 0不存在 1存在
+     */
+    private Integer simExist;
 }

+ 17 - 0
fs-user-app/src/main/java/com/fs/app/param/FsUserLoginByAppleParam.java

@@ -0,0 +1,17 @@
+package com.fs.app.param;
+
+import lombok.Data;
+
+import java.io.Serializable;
+
+@Data
+public class FsUserLoginByAppleParam implements Serializable {
+    private String jpushId;
+
+    private String loginDevice;//当前登陆设备
+
+    private String source; //app来源
+
+    private String appleKey;
+
+}

+ 2 - 0
fs-user-app/src/main/java/com/fs/app/param/FsUserLoginByWeChatParam.java

@@ -17,4 +17,6 @@ public class FsUserLoginByWeChatParam implements Serializable {
     private String source; //app来源
 
     private Long userId;
+
+    private String appId; //主要用于app微信登录
 }