Commit 2ba83ed9 authored by 廖旭伟's avatar 廖旭伟

微信小程序登录以及手机号绑定;员工名片增加页面展示设置

parent be35ac55
......@@ -40,7 +40,7 @@
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
<version>2.8.7</version>
</dependency>
<dependency>
......
package com.mortals.xhx.base.login.web;
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.service.IAuthTokenService;
import com.mortals.framework.util.DateUtils;
import com.mortals.framework.web.BaseJsonBodyController;
import com.mortals.xhx.base.system.user.model.UserEntity;
import com.mortals.xhx.base.system.user.model.UserQuery;
import com.mortals.xhx.base.system.user.service.UserService;
import com.mortals.xhx.common.utils.WechatTools;
import com.mortals.xhx.module.staff.model.StaffEntity;
import com.mortals.xhx.module.staff.model.StaffQuery;
import com.mortals.xhx.module.staff.service.StaffService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@RestController
@Slf4j
@RequestMapping("wechat")
public class WechatLoginController extends BaseJsonBodyController implements InitializingBean {
@Autowired
private UserService userService;
@Autowired
private StaffService staffService;
@Autowired
private IAuthTokenService authTokenService;
@Value("${wechat.appId:wxdb349cd58dd0600b}")
private String AppId;
@Value("${wechat.secret:21eb0f1bdb87c79b9fb6f38db2b1a357}")
private String AppSecret;
@RequestMapping("login")
@UnAuth
public String getWxInfo(@RequestBody JSONObject obj) {
JSONObject ret = new JSONObject();
JSONObject wxJson = JSONObject.parseObject(WechatTools.getOpenid(obj.getString("code"), AppId, AppSecret));
log.info("微信的返回值{}", wxJson);
if(StringUtils.isEmpty(wxJson.getString("openid"))){
ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
ret.put(KEY_RESULT_MSG, wxJson.getString("errmsg"));
ret.put("errcode", wxJson.getString("errcode"));
return ret.toJSONString();
}else {
String ip = super.getRequestIP(request);
UserEntity userEntity = userService.wechatLogin(wxJson.getString("openid"),ip);
JSONObject data = new JSONObject();
data.put("userId", userEntity.getId());
if(userEntity.getStaffId()==null){
data.put("staffId", -1);
}else {
data.put("staffId", userEntity.getStaffId());
}
userEntity.setLoginTime(System.currentTimeMillis());
userEntity.setToken(IdUtil.fastSimpleUUID());
userEntity.setExpireTime(DateUtils.addCurrDate(7).getTime());
String token = authTokenService.createToken(userEntity);
data.put("token", token);
ret.put(KEY_RESULT_DATA, data);
ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
ret.put(KEY_RESULT_MSG, "用户登录系统成功!");
return ret.toJSONString();
}
}
@RequestMapping("getPhoneNumber")
public String getPhoneNumber(@RequestBody JSONObject obj){
JSONObject ret = new JSONObject();
String tokenUrl =String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s",AppId,AppSecret);
JSONObject token = JSONObject.parseObject(HttpUtil.createGet(tokenUrl).execute().body());
log.info("微信的返回值{}", token);
String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token="+token.getString("access_token");
JSONObject param =new JSONObject();
param.put("code",obj.getString("code"));
String body = HttpUtil.createPost(url)
.contentType("application/json").body(param.toJSONString()).execute().body();
log.info("微信的返回值{}", body);
JSONObject jsonObject = JSONObject.parseObject(body);
if(jsonObject.getIntValue("errcode")==0){
JSONObject data = new JSONObject();
String phoneNumber = jsonObject.getJSONObject("phone_info").getString("phoneNumber");
data.put("phoneNumber", phoneNumber);
//UserEntity userEntity = this.getContext().getUser().getId();
if(this.getContext()!=null && this.getContext().getUser()!=null){
UserEntity userEntity = userService.get(this.getContext().getUser().getId());
if(userEntity!=null){
StaffEntity staffEntity = staffService.selectOne(new StaffQuery().phoneNumber(phoneNumber));
if(staffEntity!=null){
userEntity.setStaffId(staffEntity.getId());
data.put("staffId", staffEntity.getId());
data.put("staff",staffEntity);
}else {
data.put("staffId", -1);
}
userEntity.setPhone(phoneNumber);
userEntity.setUpdateTime(new Date());
userEntity.setUpdateUserId(userEntity.getId());
userService.update(userEntity);
}
}
ret.put(KEY_RESULT_DATA, data);
ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
ret.put(KEY_RESULT_MSG, "用户登录系统成功!");
return ret.toJSONString();
}else {
ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
ret.put(KEY_RESULT_MSG, jsonObject.getString("errmsg"));
ret.put("errcode", jsonObject.getIntValue("errcode"));
return ret.toJSONString();
}
}
@Override
public void afterPropertiesSet() throws Exception {
}
}
......@@ -118,4 +118,12 @@ public interface UserService extends ICRUDService<UserEntity,Long> {
UserDao getUserDao();
/***
* 微信小程序登录 openId未绑定则创建新用户
* @param openId
* @param ip
* @return
*/
UserEntity wechatLogin(String openId,String ip);
}
\ No newline at end of file
......@@ -332,4 +332,27 @@ public class UserServiceImpl extends AbstractCRUDServiceImpl<UserDao, UserEntity
super.removeAfter(ids, context, result);
}
@Override
public UserEntity wechatLogin(String openId,String ip) {
UserEntity userEntity = this.selectOne(new UserQuery().openId(openId));
if(userEntity == null){
userEntity = new UserEntity();
userEntity.setStatus(1);
userEntity.setOpenId(openId);
userEntity.setUserType(1);
userEntity.setRealName("微信小程序访客");
userEntity.setCreateTime(new Date());
userEntity.setCreateUserId(1L);
userEntity.setCreateUserName("系统管理员");
userEntity.setLastLoginTime(userEntity.getCreateTime());
userEntity.setLastLoginAddress(ip);
this.save(userEntity);
}else {
userEntity.setLastLoginAddress(ip);
userEntity.setLastLoginTime(new Date());
this.update(userEntity);
}
return userEntity;
}
}
\ No newline at end of file
package com.mortals.xhx.common.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HttpUtils {
public static String getRequest(String httpurl) {
HttpURLConnection connection = null;
InputStream is = null;
BufferedReader br = null;
String result = null;// 返回结果字符串
try {
// 创建远程url连接对象
URL url = new URL(httpurl);
// 通过远程url连接对象打开一个连接,强转成httpURLConnection类
connection = (HttpURLConnection) url.openConnection();
// 设置连接方式:get
connection.setRequestMethod("GET");
// 设置连接主机服务器的超时时间:15000毫秒
connection.setConnectTimeout(15000);
// 设置读取远程返回的数据时间:60000毫秒
connection.setReadTimeout(60000);
// 发送请求
connection.connect();
// 通过connection连接,获取输入流
if (connection.getResponseCode() == 200) {
is = connection.getInputStream();
// 封装输入流is,并指定字符集
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
// 存放数据
StringBuffer sbf = new StringBuffer();
String temp = null;
while ((temp = br.readLine()) != null) {
sbf.append(temp);
sbf.append("\r\n");
}
result = sbf.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭资源
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
connection.disconnect();// 关闭远程连接
}
return result;
}
}
package com.mortals.xhx.common.utils;
public class WechatTools {
public static String getOpenid(String code,String appid,String secret) {
// 调用接口必要的参数
StringBuilder data=new StringBuilder();
// appid、secret定义在配置文件中,注入到项目里
data.append("appid="+appid+"&");
data.append("secret="+ secret+"&");
data.append("js_code="+ code+"&");
data.append("grant_type="+ "authorization_code");
String response = HttpUtils.getRequest("https://api.weixin.qq.com/sns/jscode2session?" + data);
return response;
}
}
......@@ -14,7 +14,7 @@ import lombok.Data;
* 名片基本信息实体对象
*
* @author zxfei
* @date 2023-10-10
* @date 2024-11-19
*/
@Data
public class BussinesscardEntity extends BussinesscardVo {
......@@ -100,6 +100,10 @@ public class BussinesscardEntity extends BussinesscardVo {
* 发送名片次数
*/
private Integer sendBusinessCardTimes;
/**
* 名片展示设置
*/
private String setting;
@Override
public int hashCode() {
return this.getId().hashCode();
......@@ -137,5 +141,6 @@ public class BussinesscardEntity extends BussinesscardVo {
this.sumViews = 0;
this.viewsByDay = 0;
this.sendBusinessCardTimes = 0;
this.setting = "";
}
}
\ No newline at end of file
......@@ -61,6 +61,7 @@
<result property="updateUserId" column="updateUserId" />
<result property="updateTime" column="updateTime" />
<result property="sendBusinessCardTimes" column="sendBusinessCardTimes" />
<result property="setting" column="setting" />
</resultMap>
......@@ -156,7 +157,7 @@
<!-- 子表所有列 -->
<sql id="_columns_sub">
<trim suffixOverrides="," suffix="">
b.id,b.staffId,b.staffName,b.name,b.gender,b.birthday,b.photoPath,b.email,b.phoneNumber,b.idCard,b.companyId,b.companyName,b.positionId,b.positionName,b.bio,b.socialMedia,b.qRCode,b.remark,b.sumViews,b.viewsByDay,b.createUserId,b.createTime,b.updateUserId,b.updateTime,b.sendBusinessCardTimes,
b.id,b.staffId,b.staffName,b.name,b.gender,b.birthday,b.photoPath,b.email,b.phoneNumber,b.idCard,b.companyId,b.companyName,b.positionId,b.positionName,b.bio,b.socialMedia,b.qRCode,b.remark,b.sumViews,b.viewsByDay,b.createUserId,b.createTime,b.updateUserId,b.updateTime,b.sendBusinessCardTimes,b.setting,
</trim>
</sql>
<!-- 新增 区分主键自增加还是业务插入 -->
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment