|
@@ -1,10 +1,32 @@
|
|
|
package com.yh.saas.plugin.yiliangyiyun.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.lang.UUID;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.mapper.EntityWrapper;
|
|
|
+import com.yh.saas.plugin.yiliangyiyun.entity.CacheComponent;
|
|
|
import com.yh.saas.plugin.yiliangyiyun.entity.CommonUser;
|
|
|
+import com.yh.saas.plugin.yiliangyiyun.exception.AppServiceException;
|
|
|
+import com.yh.saas.plugin.yiliangyiyun.exception.ExceptionDefinition;
|
|
|
+import com.yh.saas.plugin.yiliangyiyun.exception.ServiceException;
|
|
|
import com.yh.saas.plugin.yiliangyiyun.mapper.CommonUserMapper;
|
|
|
+import com.yh.saas.plugin.yiliangyiyun.notify.SMSClient;
|
|
|
+import com.yh.saas.plugin.yiliangyiyun.notify.SMSResult;
|
|
|
import com.yh.saas.plugin.yiliangyiyun.service.ICommonUserService;
|
|
|
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
|
|
|
+import com.yh.saas.plugin.yiliangyiyun.util.Const;
|
|
|
+import com.yh.saas.plugin.yiliangyiyun.util.GeneratorUtil;
|
|
|
+import com.yh.saas.plugin.yiliangyiyun.util.MD5Util;
|
|
|
+import org.apache.commons.codec.digest.Md5Crypt;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import java.sql.*;
|
|
|
+import java.util.Date;
|
|
|
|
|
|
/**
|
|
|
* <p>
|
|
@@ -16,5 +38,294 @@ import org.springframework.stereotype.Service;
|
|
|
*/
|
|
|
@Service
|
|
|
public class CommonUserServiceImpl extends ServiceImpl<CommonUserMapper, CommonUser> implements ICommonUserService {
|
|
|
+ private static String VERIFY_CODE_PREFIX = "VERIFY_CODE_";
|
|
|
+ @Autowired
|
|
|
+ private CacheComponent cacheComponent;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SMSClient smsClient;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CommonUserMapper commonUserMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private StringRedisTemplate userRedisTemplate;
|
|
|
+
|
|
|
+ Connection coon=null;
|
|
|
+ Connection coon1=null;
|
|
|
+ @Value("com.mysql.jdbc.Driver")
|
|
|
+ private String driverClassName;
|
|
|
+ @Value("jdbc:mysql://47.100.3.209:3306/talk-portal?createDatabaseIfNotExist=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true")
|
|
|
+ private String url;
|
|
|
+ @Value("root")
|
|
|
+ private String username;
|
|
|
+ @Value("Ccj841968545")
|
|
|
+ private String password;
|
|
|
+ @Value("wxce2e964b11803018")
|
|
|
+ private String gzhAppId;
|
|
|
+ @Value("122059c777309ff3efc7448ec8794fd5")
|
|
|
+ private String gzhAppSecret;
|
|
|
+ @Value("${spring.liangxin.url1}jdbc:mysql://47.100.3.209:3306/talk-core?createDatabaseIfNotExist=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true")
|
|
|
+ private String url1;
|
|
|
+ @Value("root")
|
|
|
+ private String username1;
|
|
|
+ @Value("Ccj841968545")
|
|
|
+ private String password1;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 注册
|
|
|
+ * @param commonUser
|
|
|
+ * @return
|
|
|
+ * @throws ServiceException
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public String register(CommonUser commonUser) throws ServiceException {
|
|
|
+ //1.校验验证码
|
|
|
+ checkVerifyCode(commonUser.getPhone(), commonUser.getVerifyCode());
|
|
|
+ //2.校验用户是否存在
|
|
|
+ Integer count = this.selectCount(
|
|
|
+ new EntityWrapper<CommonUser>()
|
|
|
+ .eq("phone", commonUser.getPhone()));
|
|
|
+ if (count > 0) {
|
|
|
+ throw new AppServiceException(ExceptionDefinition.USER_PHONE_HAS_EXISTED);
|
|
|
+ }
|
|
|
+ //3.校验成功,注册用户
|
|
|
+ CommonUser user = new CommonUser();
|
|
|
+ user.setPhone(commonUser.getPhone());
|
|
|
+ user.setPassword(Md5Crypt.md5Crypt(commonUser.getPassword().getBytes(), "$1$" + commonUser.getPhone().substring(0, 7)));
|
|
|
+ this.insert(user);
|
|
|
+ //返回用户DTO
|
|
|
+ cacheComponent.del(VERIFY_CODE_PREFIX + commonUser.getPhone());
|
|
|
+ return commonUser.getPhone();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送验证码
|
|
|
+ * @param phone
|
|
|
+ * @return
|
|
|
+ * @throws ServiceException
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public String sendVerifyCode(String phone) throws ServiceException {
|
|
|
+ String verifyCode = GeneratorUtil.genSixVerifyCode();
|
|
|
+ System.out.println("verifyCode:"+verifyCode);
|
|
|
+ SMSResult res = smsClient.sendRegisterVerify(phone, verifyCode);
|
|
|
+ if (res.isSucc()) {
|
|
|
+ cacheComponent.putRaw(VERIFY_CODE_PREFIX + phone, verifyCode, 300);
|
|
|
+ return "ok";
|
|
|
+ } else {
|
|
|
+ throw new AppServiceException(res.getMsg(), ExceptionDefinition.USER_SEND_VERIFY_FAILED.getCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 登录
|
|
|
+ * @param commonUser
|
|
|
+ * @return
|
|
|
+ * @throws ServiceException
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public CommonUser login(CommonUser commonUser) throws ServiceException {
|
|
|
+ String cryptPassword = Md5Crypt.md5Crypt(commonUser.getPassword().getBytes(), "$1$" + commonUser.getPhone().substring(0, 7));
|
|
|
+ CommonUser user = commonUserMapper.login(commonUser.getPhone(), cryptPassword);
|
|
|
+
|
|
|
+ if (user == null) {
|
|
|
+ throw new AppServiceException(ExceptionDefinition.USER_PHONE_OR_PASSWORD_NOT_CORRECT);
|
|
|
+ }
|
|
|
+ CommonUser userDO = this.selectById(user.getId());
|
|
|
+ //检查帐号是否已经冻结
|
|
|
+ if (userDO.getStatus() == 0) {
|
|
|
+ throw new AppServiceException(ExceptionDefinition.USER_CAN_NOT_ACTICE);
|
|
|
+ }
|
|
|
+ if(commonUser.getPhone() != null){
|
|
|
+ DBConnection();
|
|
|
+ String sUserId = null;
|
|
|
+ password = null;
|
|
|
+ if(excSelect("select * from t_user t where t.MOBILE = '"+commonUser.getPhone()+"'",this.coon) > 0){
|
|
|
+ excUpdate("update t_user t set t.LAST_LOGIN_TIME = now() where t.MOBILE = '"+commonUser.getPhone()+"'",this.coon);
|
|
|
+ sUserId=excSelect1("select * from t_user t where t.MOBILE = '"+commonUser.getPhone()+"'",this.coon);
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ password = MD5Util.encrypt(commonUser.getPhone(), "123456");
|
|
|
+ excInsert("insert into t_user (USERNAME,PASSWORD,REALNAME,STATUS,MOBILE,CREATE_TIME,MODIFY_TIME,LAST_LOGIN_TIME,AVATAR,OPER_ID) " +
|
|
|
+ "VALUES ('"+commonUser.getPhone()+"','"+ password +"','"+userDO.getUserName()+"',1,'"+commonUser.getPhone()+"',NOW(),NOW(),NOW(),'https://liangxin.zthymaoyi.com/upload/default.jpg','"
|
|
|
+ + UUID.randomUUID().toString(true)+"')",this.coon);
|
|
|
+ Long user_id = excSelect("select * from t_user t where t.MOBILE = '"+commonUser.getPhone()+"'",this.coon);
|
|
|
+ sUserId=excSelect1("select * from t_user t where t.MOBILE = '"+commonUser.getPhone()+"'",this.coon);
|
|
|
+ excInsert("insert into t_user_role (USER_ID,ROLE_ID) " +
|
|
|
+ "VALUES ("+user_id+",7)",this.coon);
|
|
|
+ excInsert("insert into t_user_config (USER_ID,THEME,LAYOUT,MULTI_PAGE,FIX_SIDERBAR,FIX_HEADER,COLOR) " +
|
|
|
+ "VALUES ("+user_id+",'dark','side','0','1','1','rgb(66, 185, 131)')",this.coon);
|
|
|
+
|
|
|
+ }
|
|
|
+ close();
|
|
|
+ DBConnection1();
|
|
|
+ if( excSelect2("select * from s_user t where t.id = '"+sUserId+"'",this.coon1)!= null){
|
|
|
+ excUpdate("update s_user t set t.last_oper_time = now() where t.id = '"+sUserId+"'",this.coon1);
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ excInsert("insert into s_user (id,nick_name,user_name,password,avatar,money,status,oper_time,last_oper_time) " +
|
|
|
+ "VALUES ('"+sUserId+"','"+ userDO.getUserName()+"','"+commonUser.getPhone()+"','"+password+"','https://liangxin.zthymaoyi.com/upload/default.jpg','0.0',0,NOW(),NOW())",this.coon1);
|
|
|
+ }
|
|
|
+ close1();
|
|
|
+ userDO.setLiangxinId(sUserId);
|
|
|
+ this.updateById(userDO);
|
|
|
+ }
|
|
|
+ //返回用户DTO
|
|
|
+ cacheComponent.del(VERIFY_CODE_PREFIX + commonUser.getPhone());
|
|
|
+ String accessToken = GeneratorUtil.genSessionId();
|
|
|
+ BeanUtils.copyProperties(userDO,user);
|
|
|
+ user.setAccessToken(accessToken);
|
|
|
+ //放入SESSION专用Redis数据源中
|
|
|
+ userRedisTemplate.opsForValue().set(Const.USER_REDIS_PREFIX + accessToken, JSONObject.toJSONString(user));
|
|
|
+ this.updateById(userDO);
|
|
|
+ return user;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void DBConnection(){
|
|
|
+ try{
|
|
|
+ //加载驱动程序
|
|
|
+ Class.forName(driverClassName);
|
|
|
+ coon=(Connection) DriverManager.getConnection(url,username,password);
|
|
|
+ if(!coon.isClosed()){
|
|
|
+ System.out.println("成功连接数据库!");
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询s_user表
|
|
|
+ public String excSelect2(String sql,Connection coon){
|
|
|
+ try{
|
|
|
+ Statement stmt=(Statement)coon.createStatement();
|
|
|
+ ResultSet rs=(ResultSet)stmt.executeQuery(sql);//得到的是结果的集合
|
|
|
+
|
|
|
+ while(rs.next()){
|
|
|
+ String id=rs.getString("ID");
|
|
|
+ if(id != null && !id.isEmpty()){
|
|
|
+ return id;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ stmt.close();
|
|
|
+ return null;
|
|
|
+ }catch(Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 增加数据
|
|
|
+ public void excInsert(String sql,Connection coon){
|
|
|
+ try{
|
|
|
+ PreparedStatement preStmt=(PreparedStatement)coon.prepareStatement(sql);
|
|
|
+ preStmt.executeUpdate();
|
|
|
+ System.out.println("插入数据成功!");
|
|
|
+ preStmt.close();
|
|
|
+ }catch(Exception e){
|
|
|
+ System.out.println("粮信插入数据异常!");
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 查询t_user表
|
|
|
+ public String excSelect1(String sql,Connection coon){
|
|
|
+ try{
|
|
|
+ Statement stmt=(Statement)coon.createStatement();
|
|
|
+ ResultSet rs=(ResultSet)stmt.executeQuery(sql);//得到的是结果的集合
|
|
|
+
|
|
|
+ while(rs.next()){
|
|
|
+ String id=rs.getString("OPER_ID");
|
|
|
+ if(id != null && !id.isEmpty()){
|
|
|
+ return id;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ stmt.close();
|
|
|
+ return null;
|
|
|
+ }catch(Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void close(){
|
|
|
+ try{
|
|
|
+ this.coon.close();
|
|
|
+ System.out.println("成功关闭数据库连接!");
|
|
|
+ }catch(Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ System.out.println("关闭数据库连接异常!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void close1(){
|
|
|
+ try{
|
|
|
+ this.coon1.close();
|
|
|
+ System.out.println("成功关闭数据库连接!");
|
|
|
+ }catch(Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ System.out.println("关闭数据库连接异常!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 更改数据
|
|
|
+ public void excUpdate(String sql,Connection coon){
|
|
|
+ try{
|
|
|
+ PreparedStatement prestmt=(PreparedStatement)coon.prepareStatement(sql);
|
|
|
+ prestmt.executeUpdate();
|
|
|
+ System.out.println("更改数据成功!");
|
|
|
+ prestmt.close();
|
|
|
+ }catch(Exception e){
|
|
|
+ System.out.println("粮信更改数据异常!");
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询t_user表
|
|
|
+ public Long excSelect(String sql,Connection coon){
|
|
|
+ try{
|
|
|
+ Statement stmt=(Statement)coon.createStatement();
|
|
|
+ ResultSet rs=(ResultSet)stmt.executeQuery(sql);//得到的是结果的集合
|
|
|
|
|
|
+ while(rs.next()){
|
|
|
+ Long name=rs.getLong("USER_ID");
|
|
|
+ if(name != null && name > 0){
|
|
|
+ return name;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ stmt.close();
|
|
|
+ return -1l;
|
|
|
+ }catch(Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ return -1l;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public void DBConnection1(){
|
|
|
+ try{
|
|
|
+ //加载驱动程序
|
|
|
+ Class.forName(driverClassName);
|
|
|
+ coon1=(Connection)DriverManager.getConnection(url1,username1,password1);
|
|
|
+ if(!coon1.isClosed()){
|
|
|
+ System.out.println("成功连接数据库!");
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 验证码抽取校验
|
|
|
+ *
|
|
|
+ * @param phone
|
|
|
+ * @param verifyCode
|
|
|
+ * @throws ServiceException
|
|
|
+ */
|
|
|
+ private void checkVerifyCode(String phone, String verifyCode) throws ServiceException {
|
|
|
+ String raw = cacheComponent.getRaw(VERIFY_CODE_PREFIX + phone);
|
|
|
+ if (StringUtils.isEmpty(raw)) {
|
|
|
+ throw new AppServiceException(ExceptionDefinition.USER_VERIFY_CODE_NOT_EXIST);
|
|
|
+ }
|
|
|
+ if (!raw.equals(verifyCode)) {
|
|
|
+ throw new AppServiceException(ExceptionDefinition.USER_VERIFY_CODE_NOT_CORRECT);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|