Commit 29455e09 authored by 张艳玲's avatar 张艳玲

Merge branch '2021080502_gxh' into 'master'

封装取关和关注公众号、禁用和切换会员

See merge request test-group/kejiji!92
parents 07383f9d 65b13a27
package com.kjj.utils;
package com.kjj.sql;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
public class MysqlUtils {
private static final String PREFIX = "dbseccode";
private static final String secret = "CNxgrtFG2nYQUfu";
import java.sql.*;
import java.util.*;
private static Map<String, MysqlUtils> pool=new HashMap<>();
public class KjyUserDB {
private static KjyUserDB user_Utils;
//数据库用户名
private String USERNAME;// = "root";
private String USERNAME = "auto_test";
//数据库密码
private String PASSWORD; //= "yanzi";
private String PASSWORD = "881QYHoGGgc09Ngv"; //= "yanzi";
//驱动信息
private String DRIVER="com.mysql.cj.jdbc.Driver";
//数据库地址
private String URL;// = "jdbc:mysql://localhost:3306/mydb";
private String USER_URL="jdbc:mysql://rm-bp11mkg584kvjr485.mysql.rds.aliyuncs.com:3306/kjy_user";
private Connection connection;
private PreparedStatement pstmt;
private ResultSet resultSet;
private MysqlUtils(String username, String password, String url){
USERNAME = username;
PASSWORD = decode(password);
URL = url;
MysqlUtils mu = pool.get(url);
if(mu!=null){
System.out.println("--------exists");
connection = mu.connection;
}else{
connection = getConnection();
pool.put(url,this);
}
// if(mu!=null) {
// try {
// mu.connection.close();
// } catch (Exception e) {
// System.out.println("mysql error:" + e.getMessage());
// }
// }
//// }else {
// connection = getConnection();
// pool.put(url,this);
public static synchronized KjyUserDB getInstance(){
if (Objects.nonNull(user_Utils)){
return user_Utils;
}
/**
*
* @param username 用户名
* @param password 密码
* @param ip 数据库ip
* @param port 端口号
* @param databaseName 数据库名
*/
public MysqlUtils(String username, String password, String ip, String port, String databaseName){
this(username,password,"jdbc:mysql://"+ip+":"+port+"/"+databaseName);
user_Utils = new KjyUserDB();
user_Utils.initConnection();
return user_Utils;
}
/**
* 阿里云数据库连接
* @param databaseName 数据库名字
*/
public MysqlUtils(String databaseName){
this("tuia_test","dbseccode2CJMouQ2xyuEXnhe4vBzGtrDpaDbuK8Lt","47.111.157.152","3306",databaseName);
}
public static MysqlUtils mysqlDuiba(String databaseName){
return new MysqlUtils("tuia_test","dbseccode2CJMouQ2xyuEXnhe4vGtrDpavyDbuK8Lt","47.111.157.152","3306",databaseName);
}
/**
* 获得数据库的连接
* @return
*/
public Connection getConnection(){
private Connection initConnection(){
try {
try {
Class.forName(DRIVER);
......@@ -86,10 +40,7 @@ public class MysqlUtils {
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
// "jdbc:mysql://"+ip+":"+port+"/"+databaseName);
String url = URL+"?autoReconnect=true&testOnBorrow=true&testWhileIdle=true";
String url = USER_URL+"?autoReconnect=true&testOnBorrow=true&testWhileIdle=true";
connection = DriverManager.getConnection(url,USERNAME,PASSWORD);
System.out.println("数据库连接成功!");
} catch (SQLException e) {
......@@ -97,36 +48,7 @@ public class MysqlUtils {
}
return connection;
}
/**
* 增加、删除、改
* @param sql sql语句
* @param params 参数,替换 ?
* @return true or false
* @throws SQLException
*/
public boolean update(String sql, Object ...params)throws SQLException{
if(!connection.isValid(3)){
System.out.println("mysql connect is not vaild,reconnect.");
connection.close();
connection = getConnection();
pool.put(URL,this);
}
boolean flag = false;
int result = -1;
pstmt = connection.prepareStatement(sql);
int index = 1;
if(params != null && !(params.length==0)){
for(int i=0; i<params.length; i++){
pstmt.setObject(index++, params[i]);
}
}
System.out.println("SQL语句"+pstmt.toString());
result = pstmt.executeUpdate();
flag = result > 0 ? true : false;
return flag;
}
/**
* 查询结果只有一行数据
* @param sql sql语句
......@@ -138,8 +60,7 @@ public class MysqlUtils {
if(!connection.isValid(3)){
System.out.println("mysql connect is not vaild,reconnect.");
connection.close();
connection = getConnection();
pool.put(URL,this);
initConnection();
}
Map<String, Object> map = new HashMap<String, Object>();
int index = 1;
......@@ -177,8 +98,7 @@ public class MysqlUtils {
if(!connection.isValid(3)){
System.out.println("mysql connect is not vaild,reconnect.");
connection.close();
connection = getConnection();
pool.put(URL,this);
initConnection();
}
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
int index = 1;
......@@ -218,22 +138,6 @@ public class MysqlUtils {
}
}
}
public static void main(String[] args) throws SQLException {
MysqlUtils jdbcUtils = new MysqlUtils("tuia_adver");
jdbcUtils.getConnection();
Map<String, Object> map = jdbcUtils.findSimpleResult("select * from account where company_name=?","兑吧科技有限公司");
jdbcUtils.releaseConn();
}
private static String decode(String value) {
if (value.startsWith(PREFIX)) {
value = value.substring(PREFIX.length());
return BlowfishUtils.decryptBlowfish(value, secret);
}
return value;
}
}
package com.kjj.sql;
public class UserSqlFactory {
private UserSqlFactory(){}
public static final String selectUserIdByUnionId = "select id from tb_users where union_id=?";
public static final String selectUserIdByOpenId = "SELECT user_id FROM `tb_users_wx_ext` WHERE `open_id` = ?";
}
package com.kjj.utils;
import com.kjj.sql.KjyUserDB;
import com.kjj.sql.UserSqlFactory;
import io.restassured.response.Response;
import org.testng.Assert;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;
import static com.kjj.config.BasicConfig.*;
import static com.kjj.utils.ThreadSleepUtils.sleep;
public class BaseUtils {
private static final NetworkUtils network = NetworkUtils.getInstance();
public static String tku;
/**
* 登录管理后台
......@@ -42,6 +46,102 @@ public class BaseUtils {
network.agentHeaders.put("X-Csrf-Token",ssoCookies.get("csrf_token"));
// 免登
network.agentCookies.put("sso_ticket", "autotest");
}
/**
* 小葡萄关注客集集微信公众号
*/
public static Object subscribe() throws SQLException {
//关注公众号
// network.agentHeaders.put("Content-Type","text/plain");
String str = "<xml><ToUserName><![CDATA[gh_3b9884c7b0ee]]></ToUserName><FromUserName><![CDATA[osi5w5kT-smQ42Fc-ByO4G8C-E1M]]></FromUserName><CreateTime>1628040971</CreateTime><MsgType><![CDATA[event]]></MsgType><Event><![CDATA[subscribe]]></Event></xml>";
Response response = network.postXMLResponse(str,MANAGER_oamsg);
System.out.println("关注\n"+response.body().asString());
sleep(1000);
network.agentHeaders.put("Content-Type","application/json");
Map<String, Object> result = KjyUserDB.getInstance().findSimpleResult(UserSqlFactory.selectUserIdByOpenId, "osi5w5kT-smQ42Fc-ByO4G8C-E1M");
Object userId = result.get("user_id");
Map<String, Object> params = new HashMap<String, Object>();
params.put("userId",userId);
params.put("validDays",365);
response = network.getResponse(params,MANAGER_encryptTku);
boolean success = response.jsonPath().getBoolean("success");
Assert.assertTrue(success,network.message(ROOKIEGUIDE_homeGuide,"接口返回失败",response.body().asString()));
tku = response.jsonPath().getString("data");
network.agentCookies.put("tku", tku);
System.out.println("小葡萄tku:" + tku);
return userId;
}
/**
* 小葡萄取关客集集微信公众号
*/
public static Object unsubscribe() throws SQLException {
//取关公众号
network.agentHeaders.put("Content-Type","text/plain");
String str = "<xml><ToUserName><![CDATA[gh_3b9884c7b0ee]]></ToUserName><FromUserName><![CDATA[osi5w5kT-smQ42Fc-ByO4G8C-E1M]]></FromUserName><CreateTime>1628040963</CreateTime><MsgType><![CDATA[event]]></MsgType><Event><![CDATA[unsubscribe]]></Event></xml>";
Response response = network.postXMLResponse(str,MANAGER_oamsg);
Map<String, Object> result = KjyUserDB.getInstance().findSimpleResult(UserSqlFactory.selectUserIdByOpenId, "osi5w5kT-smQ42Fc-ByO4G8C-E1M");
System.out.println("取关\n"+response.body().asString());
network.agentHeaders.put("Content-Type","application/json");
Object userId = result.get("user_id");
return userId;
}
/**
* 禁用
*/
public static void disableUser(long userId){
Map<String, Object> params = new HashMap<String, Object>();
params.put("userId",userId);
Response response = network.postResponse(params,MANAGER_disableUser);
boolean success = response.jsonPath().getBoolean("success");
boolean result = response.jsonPath().getBoolean("data");
System.out.println("禁用:"+response.body().asString());
Assert.assertTrue(success,network.message(params,MANAGER_disableUser,"接口返回失败",response.body().asString()));
Assert.assertTrue(result,network.message(params,MANAGER_disableUser,"返回数据为空",response.body().asString()));
}
/**
* 切换为会员
*/
public static void updateTypeOrDate(long userId){
//找到用户的sellerid
Map<String, Object> params = new HashMap<String, Object>();
Response response = network.getResponse(USER_INFO);
int sellerId = response.jsonPath().getInt("data.sellerId");
Assert.assertNotNull(response.jsonPath().getJsonObject("data"),network.message(USER_INFO,"获取用户信息失败",response.body().asString()));
//切换代理人公司
params.clear();
params.put("id",sellerId);
params.put("companyId",6);
response = network.getResponse(params,MANAGER_changeCompany);
System.out.println(params.toString());
boolean success = response.jsonPath().getBoolean("success");
boolean result = response.jsonPath().getBoolean("data");
System.out.println(response.body().asString());
Assert.assertTrue(success,network.message(params,MANAGER_changeCompany,"接口返回失败",response.body().asString()));
Assert.assertTrue(result,network.message(params,MANAGER_changeCompany,"返回数据为空",response.body().asString()));
//切换会员
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");//设置日期格式
Date date = new Date();// new Date()为获取当前系统时间
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.YEAR,1);
params.put("reason","测试账号");
params.put("sellerId",sellerId);
params.put("userVersion",2);
params.put("vipEndDate",df.format(date));
params.put("vipStartDate",df.format(cal.getTime()));
params.put("vipType",2);
response = network.postResponse(params,MANAGER_updateTypeOrDate);
success = response.jsonPath().getBoolean("success");
result = response.jsonPath().getBoolean("data");
Assert.assertTrue(success,network.message(params,MANAGER_updateTypeOrDate,"接口返回失败",response.body().asString()));
Assert.assertTrue(result,network.message(params,MANAGER_updateTypeOrDate,"返回数据为空",response.body().asString()));
}
}
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