|
|
@@ -1,29 +1,42 @@
|
|
|
package com.yyjc.fbs.service.biz;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.yyjc.fbs.bean.*;
|
|
|
+import com.yyjc.fbs.config.UserPassInfoAuthenticationToken;
|
|
|
+import com.yyjc.fbs.config.WechatAppConfig;
|
|
|
+import com.yyjc.fbs.constant.ConstantKey;
|
|
|
+import com.yyjc.fbs.service.ICompanyBiz;
|
|
|
+import com.yyjc.fbs.util.MyHttpUtils;
|
|
|
+import com.yyjc.fbs.vo.*;
|
|
|
+import io.jsonwebtoken.Jwts;
|
|
|
+import io.jsonwebtoken.SignatureAlgorithm;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.security.core.Authentication;
|
|
|
+import org.springframework.security.core.GrantedAuthority;
|
|
|
+import org.springframework.security.core.userdetails.UserDetails;
|
|
|
+import org.springframework.security.core.userdetails.UserDetailsService;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
-import com.yyjc.fbs.bean.Company;
|
|
|
-import com.yyjc.fbs.bean.Org;
|
|
|
-import com.yyjc.fbs.bean.User;
|
|
|
-import com.yyjc.fbs.bean.UserTypeEnum;
|
|
|
import com.yyjc.fbs.bean.mapper.CompanyMapper;
|
|
|
-import com.yyjc.fbs.vo.LoginUser;
|
|
|
-import com.yyjc.fbs.vo.Role;
|
|
|
-import com.yyjc.fbs.vo.UserCondition;
|
|
|
-import com.yyjc.fbs.vo.UserOfRole;
|
|
|
import com.github.pagehelper.PageHelper;
|
|
|
|
|
|
@Service
|
|
|
public class UserToolService extends BasicService {
|
|
|
+ @Autowired
|
|
|
+ private UserDetailsService userDetailsService;
|
|
|
|
|
|
@Autowired
|
|
|
private CompanyMapper companyMapper;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private ICompanyBiz iCompanyBiz;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private WechatAppConfig wechatAppConfig;
|
|
|
+
|
|
|
public boolean registerUser(LoginUser user) {
|
|
|
this.userMapper.saveUser(user);
|
|
|
return true;
|
|
|
@@ -156,4 +169,102 @@ public class UserToolService extends BasicService {
|
|
|
});
|
|
|
return users;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ /***
|
|
|
+ * 根据openId获取用户信息
|
|
|
+ * @param openId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public User findUserExtByWxOpenId(String openId) {
|
|
|
+ UserExt userExt = userMapper.findUserExtByWxOpenId(openId,0);
|
|
|
+ if (userExt != null) {
|
|
|
+ User user = findUserById(userExt.getUserid());
|
|
|
+ return user;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /***
|
|
|
+ * 更新用户扩展表信息
|
|
|
+ * @param input
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean AddOrUpdateUserExt(UserExt input) {
|
|
|
+ UserExt userExt = userMapper.findUserExtByWxOpenId("",input.getUserid());
|
|
|
+ if (userExt != null) {
|
|
|
+ input.setId(userExt.getId());
|
|
|
+ userMapper.updateUserWxOpenId(userExt);
|
|
|
+ }else{
|
|
|
+ userMapper.insertUserExt(input);
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /***
|
|
|
+ * 微信的code登录
|
|
|
+ * @param user
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public LoginResult LoginByWxCode(User user,WxLoginInfo wxLoginInfo) {
|
|
|
+ UserDetails userDetails = userDetailsService.loadUserByUsername(user.getUsername());
|
|
|
+ if (userDetails == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Collection<? extends GrantedAuthority> authorities = userDetails.getAuthorities();
|
|
|
+ LoginInfo info = new LoginInfo();
|
|
|
+ info.setLogin_name(userDetails.getUsername());
|
|
|
+ // 定义存放角色集合的对象
|
|
|
+ for (GrantedAuthority grantedAuthority : authorities) {
|
|
|
+ info.getRoles().add(grantedAuthority.getAuthority());
|
|
|
+ }
|
|
|
+ // 设置过期时间
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(new Date());
|
|
|
+ calendar.add(Calendar.DAY_OF_MONTH, 30);
|
|
|
+ Date time = calendar.getTime();
|
|
|
+ info.setUser_detail(user);
|
|
|
+ String token = Jwts.builder().setSubject(JSONObject.toJSONString(info)).setExpiration(time)
|
|
|
+ .signWith(SignatureAlgorithm.HS512, ConstantKey.SIGNING_KEY) // 设置算法
|
|
|
+ .compact();
|
|
|
+ LoginResult result = getResult(user.getUsername(), token, user,wxLoginInfo);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ private LoginResult getResult(String loginName, String token, BasicUser user,WxLoginInfo wxLoginInfo) {
|
|
|
+ LoginResult result = new LoginResult();
|
|
|
+ result.setAuthName(loginName);
|
|
|
+ result.setAuth(ConstantKey.JWT_PREFIX + token);
|
|
|
+ result.setUserType(user.getUser_type());
|
|
|
+ result.setEnterpriseId(user.getEnterprise_id());
|
|
|
+ result.setWxLoginInfo(wxLoginInfo);
|
|
|
+ if (UserTypeEnum.company.getType() == user.getUser_type()) {
|
|
|
+ Company com = iCompanyBiz.selectByid(Integer.valueOf(user.getEnterprise_id()));
|
|
|
+ if (null != com) {
|
|
|
+ result.setLoginMsg(com.getName());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Org org = iCompanyBiz.selectOrgById(Integer.valueOf(user.getEnterprise_id()));
|
|
|
+ if (null != org && StringUtils.isNotBlank(org.getDescription())) {
|
|
|
+ result.setLoginMsg(org.getDescription());
|
|
|
+ } else {
|
|
|
+ result.setLoginMsg("餐饮油烟在线监控系统");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public String getWxOpenId(String wxCode) throws Exception {
|
|
|
+ String url = String.format("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code" +
|
|
|
+ "=%s&grant_type" +
|
|
|
+ "=authorization_code",wechatAppConfig.getAppId(),wechatAppConfig.getAppSecret(),wxCode);
|
|
|
+ String res = MyHttpUtils.sendGet(url);
|
|
|
+ Object parse = JSONObject.parse(res);
|
|
|
+ String openId = "";
|
|
|
+ if (parse instanceof JSONObject) {
|
|
|
+ JSONObject jsonObject = (JSONObject) parse;
|
|
|
+ openId= jsonObject.getString("openid");
|
|
|
+ }
|
|
|
+ return openId;
|
|
|
+ }
|
|
|
}
|