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

[B端登录][移动塔台]:领导手机授权登录

release_toaliyun_real
Stormeye Wu 7 лет назад
Родитель
Сommit
a8877e23f8
7 измененных файлов: 306 добавлений и 114 удалений
  1. +24
    -4
      mallinkAdmin/src/main/java/com/iformall/controller/HomeController.java
  2. +113
    -2
      mallinkBApi/src/main/java/com/iformall/controller/WxInfoController.java
  3. +15
    -0
      mallinkService/src/main/java/com/iformall/domain/po/MallUserInfo.java
  4. +4
    -0
      mallinkService/src/main/java/com/iformall/mapper/MallUserInfoMapper.java
  5. +15
    -0
      mallinkService/src/main/java/com/iformall/service/MallUserInfoService.java
  6. +10
    -0
      mallinkService/src/main/java/com/iformall/service/impl/MallUserInfoServiceImpl.java
  7. +125
    -108
      mallinkService/src/main/resources/mapper/MallUserInfoMapper.xml

+ 24
- 4
mallinkAdmin/src/main/java/com/iformall/controller/HomeController.java Просмотреть файл

@@ -8,8 +8,10 @@ import com.iformall.common.ResultData;
import com.iformall.domain.po.MallRolePermission;
import com.iformall.domain.po.MallUserInfo;
import com.iformall.domain.po.MallUserRole;
import com.iformall.enums.EnumUserAdmin;
import com.iformall.exception.MallinkException;
import com.iformall.service.MallRolePermissionService;
import com.iformall.service.MallUserInfoService;
import com.iformall.service.MallUserRoleService;
import com.iformall.shiro.UserSession;
import com.iformall.utils.IPUtil;
@@ -17,6 +19,7 @@ import com.iformall.utils.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
@@ -24,7 +27,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@@ -49,6 +51,9 @@ public class HomeController extends BaseController {
@Autowired
private Producer producer;

@Autowired
private MallUserInfoService mallUserInfoService;

@Autowired
private MallUserRoleService mallUserRoleService;

@@ -89,7 +94,7 @@ public class HomeController extends BaseController {


ResultData data = new ResultData();
if (StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPassword())) {
if (StringUtils.isBlank(user.getUsername()) || StringUtils.isBlank(user.getPassword())) {
// throw new SystemException(ErrorCode.LOGIN_USER_OR_PWD_ERROR);
return new ResultData(ResultData.ERROR, "用户名或者密码错误");
}
@@ -137,10 +142,25 @@ public class HomeController extends BaseController {
public ResultData bLogin(@RequestBody MallUserInfo user) {
logger.debug("[" + getIpAddr() + "] HomeController::bHidLogin");
ResultData data = new ResultData();
if (StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPassword())) {
if(StringUtils.isNotBlank(user.getPhone())) {
// 领导登录
user = mallUserInfoService.getByOpenId(user.getOpenId());
if(!user.getIsAdmin().equals(EnumUserAdmin.ADMIN.getCode())) {
return new ResultData(ErrorCode.USER_NOT_ADMIN);
}
} else if(StringUtils.isNotBlank(user.getOpenId())) {
// 领导登录
user = mallUserInfoService.getByOpenId(user.getOpenId());
if(!user.getIsAdmin().equals(EnumUserAdmin.ADMIN.getCode())) {
return new ResultData(ErrorCode.USER_NOT_ADMIN);
}
} else {
if (StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPassword())) {
// throw new SystemException(ErrorCode.LOGIN_USER_OR_PWD_ERROR);
return new ResultData(ResultData.ERROR, "用户名或者密码错误");
return new ResultData(ResultData.ERROR, "用户名或者密码错误");
}
}

try {
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());


+ 113
- 2
mallinkBApi/src/main/java/com/iformall/controller/WxInfoController.java Просмотреть файл

@@ -3,9 +3,20 @@ package com.iformall.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import com.iformall.annotation.AuthIgnore;
import com.iformall.common.ErrorCode;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.domain.po.MallUserInfo;
import com.iformall.domain.po.WxAppinfo;
import com.iformall.domain.po.WxCUser;
import com.iformall.domain.po.WxMerchantBUser;
import com.iformall.enums.EnumAssignTagsTrigger;
import com.iformall.enums.EnumScoreType;
import com.iformall.enums.EnumUserAdmin;
import com.iformall.exception.MallinkException;
import com.iformall.service.MallUserInfoService;
import com.iformall.service.WxAppinfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@@ -35,6 +46,9 @@ public class WxInfoController extends BaseController {
@Autowired
WxAppinfoService wxAppinfoService;

@Autowired
MallUserInfoService mallUserInfoService;

@ApiOperation("获取OPENID")
@PostMapping("getOpenId")
public ResultData getOpenId(@RequestBody Map<String,String> param) {
@@ -46,6 +60,44 @@ public class WxInfoController extends BaseController {
if (StringUtils.isBlank(code)) {
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "code不能为空");
}
try {
Map<String, String> returnMap = new HashMap<>();
Map<String, String> retMap = getOpenIdInfo(appId, code);
returnMap.put("openId", retMap.get("openId"));
return new ResultData(returnMap);
} catch (Exception e) {
return new ResultData(Result.ERROR, "解密异常");
}
}

@AuthIgnore
@ApiOperation("获取超管OPENID")
@PostMapping("getSuperOpenId")
public ResultData getSuperOpenId(@RequestBody Map<String,String> param) {
String appId = param.get("appId");
String code = param.get("code");
if (StringUtils.isBlank(appId)) {
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "appId不能为空");
}
if (StringUtils.isBlank(code)) {
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "code不能为空");
}
try {
Map<String, String> retMap = getOpenIdInfo(appId, code);
String openId = retMap.get("openId");
MallUserInfo user = mallUserInfoService.getByOpenId(openId);

if(!user.getIsAdmin().equals(EnumUserAdmin.ADMIN.getCode())) {
return new ResultData(ErrorCode.USER_NOT_ADMIN, retMap);
}

return new ResultData(retMap);
} catch (Exception e) {
return new ResultData(Result.ERROR, "解密异常");
}
}

private Map<String,String> getOpenIdInfo(String appId, String code) {
WxAppinfo wxAppinfo = getAppInfo(appId);
WxMaService wxMaService = getWeappServiceByAppInfo(wxAppinfo);

@@ -86,14 +138,73 @@ public class WxInfoController extends BaseController {

try {
WxMaJscode2SessionResult session = wxMaService.jsCode2SessionInfo(code);
//获取会话密钥(session_key)
String session_key = session.getSessionKey();
String openId = session.getOpenid();
logger.info("openId: " + openId);

// update admin user by
Map<String,String> resultMap=new HashMap<>();
resultMap.put("openId",openId);
return new ResultData(resultMap);
resultMap.put("session_key", session_key);
return resultMap;
} catch (WxErrorException e) {
logger.error(e.getMessage(), e);
return new ResultData(ErrorCode.SESSION_KEY_DECODE_ERR);
throw new MallinkException(ErrorCode.SESSION_KEY_DECODE_ERR.getCode(), e.getMessage());
}
}

/**
* 授权后获取用户的手机号等信息
* @param map
* @return
*/
@AuthIgnore
@PostMapping("/getUserPhone")
@ApiOperation(value = "授权后获取用户的手机号", notes="{\"appId\":\"string\",\"encryptedData\":\"string\",\"iv\":\"string\",\"openId\":\"string\",\"session_key\":\"string\"}")
public ResultData getUserPhone(@RequestBody Map<String, String> map) {
logger.debug(map.toString());
Map resultMap = new HashMap();
String appId = map.get("appId");
String encryptedData = map.get("encryptedData");
String iv = map.get("iv");
String openId = map.get("openId");
String session_key = map.get("session_key");

//登录凭证不能为空
if (StringUtils.isBlank(encryptedData)) {
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "encryptedData 不能为空");
}
if (StringUtils.isBlank(iv)) {
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "iv 不能为空");
}
if (StringUtils.isBlank(openId)) {
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "openId 不能为空");
}

WxMaService wxMaService = getWeappService(appId);
try {
// 解密
WxMaPhoneNumberInfo phoneNoInfo = wxMaService.getUserService().getPhoneNoInfo(session_key, encryptedData, iv);
if (null != phoneNoInfo) {
logger.debug(phoneNoInfo.toString());
String phone = phoneNoInfo.getPhoneNumber();
resultMap.put("phone", phone);

MallUserInfo user = mallUserInfoService.getByPhone(phone);
if (user.getIsAdmin().equals(EnumUserAdmin.ADMIN.getCode())) {
resultMap.put("phone", user.getPhone());
resultMap.put("admin", "1");
return new ResultData(resultMap);
} else {
return new ResultData(ErrorCode.USER_NOT_ADMIN, resultMap);
}
} else {
return new ResultData(ErrorCode.PHONE_DECODE_ERR, resultMap);
}
} catch (Exception e) {
this.logger.error(e.getMessage(), e);
return new ResultData(ErrorCode.DB_FAIL.getCode(), "解密并保存出错", resultMap);
}
}



+ 15
- 0
mallinkService/src/main/java/com/iformall/domain/po/MallUserInfo.java Просмотреть файл

@@ -1,5 +1,7 @@
package com.iformall.domain.po;

import lombok.Data;

import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
@@ -9,6 +11,7 @@ import java.util.Date;
import java.util.List;

@Table(name = "mall_user_info")
@Data
public class MallUserInfo implements Serializable {
private static final long serialVersionUID = 1L;
@@ -70,6 +73,9 @@ public class MallUserInfo implements Serializable {

@io.swagger.annotations.ApiModelProperty(value="手机",name="phone")
private String phone;
/** B端用户微信openid,领导看数据塔台用 */
@io.swagger.annotations.ApiModelProperty(value="openId",name="openId")
private String openId;

@Transient
private Long roleId;
@@ -201,6 +207,14 @@ public class MallUserInfo implements Serializable {
this.phone = phone;
}

public String getOpenId() {
return openId;
}

public void setOpenId(String openId) {
this.openId = openId;
}

public static enum Field
{
Id_ASC("`id` ASC"),Id_DESC("`id` DESC")
@@ -214,6 +228,7 @@ public class MallUserInfo implements Serializable {
,IsAdmin_ASC("`is_admin` ASC"),IsAdmin_DESC("`is_admin` DESC")
,NickName_ASC("`nick_name` ASC"),NickName_DESC("`nick_name` DESC")
,Phone_ASC("`phone` ASC"),Phone_DESC("`phone` DESC")
,OpenId_ASC("`open_id` ASC"),OpenId_DESC("`open_id` DESC")
;
private String value;
Field(String value){


+ 4
- 0
mallinkService/src/main/java/com/iformall/mapper/MallUserInfoMapper.java Просмотреть файл

@@ -12,6 +12,10 @@ public interface MallUserInfoMapper extends CommonMapper<MallUserInfo, String> {
List<MallUserInfo> findList(MallUserInfo mallUserInfo);

MallUserInfo selectByUserName(@Param("username") String username);

MallUserInfo selectByOpenId(@Param("openId") String openId);

MallUserInfo selectByPhone(@Param("phone") String phone);
Integer hasButtonPermission(@Param("userId") Long userId, @Param("permission") String permission);



+ 15
- 0
mallinkService/src/main/java/com/iformall/service/MallUserInfoService.java Просмотреть файл

@@ -81,4 +81,19 @@ public interface MallUserInfoService {

ResultData updatepwd(MallUserInfo user, String code);

/**
* 根据phone获得实体
*
* @param phone
* @return
*/
MallUserInfo getByPhone(String phone);
/**
* 根据OpenId获得实体
*
* @param openId
* @return
*/
MallUserInfo getByOpenId(String openId);

}

+ 10
- 0
mallinkService/src/main/java/com/iformall/service/impl/MallUserInfoServiceImpl.java Просмотреть файл

@@ -55,6 +55,16 @@ public class MallUserInfoServiceImpl implements MallUserInfoService {
return mallUserInfoMapper.selectByPrimaryKey(id);
}

@Override
public MallUserInfo getByOpenId(String openId) {
return mallUserInfoMapper.selectByOpenId(openId);
}

@Override
public MallUserInfo getByPhone(String phone) {
return mallUserInfoMapper.selectByPhone(phone);
}

@Override
public void saveOrUpdate(MallUserInfo record) {
if (record.getId() == null) {


+ 125
- 108
mallinkService/src/main/resources/mapper/MallUserInfoMapper.xml Просмотреть файл

@@ -1,102 +1,119 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.iformall.mapper.MallUserInfoMapper">
<resultMap id="BaseResultMap" type="com.iformall.domain.po.MallUserInfo">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="tenant_id" jdbcType="VARCHAR" property="tenantId" />
<result column="username" jdbcType="VARCHAR" property="username" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
<result column="last_login_time" jdbcType="TIMESTAMP" property="lastLoginTime" />
<result column="status" jdbcType="INTEGER" property="status" />
<result column="is_admin" jdbcType="INTEGER" property="isAdmin" />
<result column="nick_name" jdbcType="VARCHAR" property="nickName" />
<result column="phone" jdbcType="VARCHAR" property="phone" />
</resultMap>
<sql id="allColumns">
`id`,`tenant_id`,`username`,`name`,`password`,`create_time`,`last_login_time`,`status`,`is_admin`,`nick_name`,`phone`
</sql>
<resultMap id="BaseResultMap" type="com.iformall.domain.po.MallUserInfo">
<id column="id" jdbcType="BIGINT" property="id"/>
<result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
<result column="username" jdbcType="VARCHAR" property="username"/>
<result column="name" jdbcType="VARCHAR" property="name"/>
<result column="password" jdbcType="VARCHAR" property="password"/>
<result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
<result column="last_login_time" jdbcType="TIMESTAMP" property="lastLoginTime"/>
<result column="status" jdbcType="INTEGER" property="status"/>
<result column="is_admin" jdbcType="INTEGER" property="isAdmin"/>
<result column="nick_name" jdbcType="VARCHAR" property="nickName"/>
<result column="phone" jdbcType="VARCHAR" property="phone"/>
<result column="open_id" jdbcType="VARCHAR" property="openId"/>
</resultMap>

<sql id="dynamicWhereConditions">
where 1 = 1
<if test=" null != id ">
and `id` = #{id}
</if>
<if test=" null != tenantId ">
and `tenant_id` = #{tenantId}
</if>
<if test=" null != username ">
and `username` like concat('%', #{username},'%')
</if>
<if test=" null != nickName ">
and `nick_name` like concat('%', #{nickName},'%')
</if>
<if test=" null != name ">
and `name` like concat('%', #{name},'%')
</if>
<if test=" null != password ">
and `password` like concat('%', #{password},'%')
</if>
<if test=" null != createTime ">
and `create_time` = #{createTime}
</if>
<if test=" null != lastLoginTime ">
and `last_login_time` = #{lastLoginTime}
</if>
<if test=" null != status ">
and `status` = #{status}
</if>
<if test=" null != isAdmin ">
and `is_admin` = #{isAdmin}
</if>
<if test=" null != phone ">
and `phone` = #{phone}
</if>
<if test=" null != ids ">
and id in
<foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
#{idItem}
</foreach>
</if>
<if test=" null != sortColumns"> order by ${sortColumns} </if>
<sql id="allColumns">
`id`,`tenant_id`,`username`,`name`,`password`,`create_time`,`last_login_time`,`status`,`is_admin`,`nick_name`,`phone`,`open_id`
</sql>
<select id="findList" parameterType="com.iformall.domain.po.MallUserInfo" resultMap="BaseResultMap">
select <include refid="allColumns" /> from mall_user_info
<include refid="dynamicWhereConditions" />
</select>

<select id="selectByUserName" resultMap="BaseResultMap">
select <include refid="allColumns" /> from mall_user_info
where 1=1
<if test=" null != username ">
and username=#{username}
</if>
</select>

<sql id="dynamicWhereConditions">
where 1 = 1
<if test=" null != id ">
and `id` = #{id}
</if>

<if test=" null != tenantId ">
and `tenant_id` = #{tenantId}
</if>

<if test=" null != username ">
and `username` like concat('%', #{username},'%')
</if>

<if test=" null != nickName ">
and `nick_name` like concat('%', #{nickName},'%')
</if>

<if test=" null != name ">
and `name` like concat('%', #{name},'%')
</if>

<if test=" null != password ">
and `password` like concat('%', #{password},'%')
</if>

<if test=" null != createTime ">
and `create_time` = #{createTime}
</if>

<if test=" null != lastLoginTime ">
and `last_login_time` = #{lastLoginTime}
</if>

<if test=" null != status ">
and `status` = #{status}
</if>

<if test=" null != isAdmin ">
and `is_admin` = #{isAdmin}
</if>
<if test=" null != phone ">
and `phone` = #{phone}
</if>
<if test=" null != openId ">
and `open_id` = #{openId}
</if>

<if test=" null != ids ">
and id in
<foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
#{idItem}
</foreach>
</if>
<if test=" null != sortColumns">order by ${sortColumns}</if>
</sql>

<select id="findList" parameterType="com.iformall.domain.po.MallUserInfo" resultMap="BaseResultMap">
select
<include refid="allColumns"/>
from mall_user_info
<include refid="dynamicWhereConditions"/>
</select>

<select id="selectByUserName" resultMap="BaseResultMap">
select
<include refid="allColumns"/>
from mall_user_info
where 1=1
<if test=" null != username ">
and username=#{username}
</if>
</select>

<select id="selectByOpenId" resultMap="BaseResultMap">
select
<include refid="allColumns"/>
from mall_user_info
where 1=1
<if test=" null != openId ">
and `open_id`=#{openId}
</if>
</select>

<select id="selectByPhone" resultMap="BaseResultMap">
select
<include refid="allColumns"/>
from mall_user_info
where 1=1
<if test=" null != phone ">
and `phone`=#{phone}
</if>
</select>

<select id="hasButtonPermission" resultType="java.lang.Integer">
SELECT 1
FROM mall_user_role ur, mall_user_role_permission rp, mall_permission p
@@ -106,18 +123,18 @@
AND ur.uid = #{userId}
AND p.permission = #{permission}
</select>
<select id="cntByUserName" resultType="java.lang.Long">
select count(*) from mall_user_info where 1=1
<if test=" null != username ">
and username=#{username}
</if>
<if test=" null != phone ">
and `phone` = #{phone}
</if>
<if test=" null != id ">
and `id` != #{id}
</if>
</select>
<select id="cntByUserName" resultType="java.lang.Long">
select count(*) from mall_user_info where 1=1
<if test=" null != username ">
and username=#{username}
</if>
<if test=" null != phone ">
and `phone` = #{phone}
</if>
<if test=" null != id ">
and `id` != #{id}
</if>
</select>
</mapper>

Загрузка…
Отмена
Сохранить