初始化页面和订单页面
This commit is contained in:
parent
f8126d6042
commit
bd3de7896d
|
|
@ -25,16 +25,18 @@ import android.util.Log;
|
|||
import android.view.LayoutInflater;
|
||||
import android.widget.ListView;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.bonus.canteen.R;
|
||||
import com.bonus.canteen.adapter.initialization.IniListAdapter;
|
||||
import com.bonus.canteen.adapter.initialization.entity.ListBean;
|
||||
import com.bonus.canteen.adapter.utils.OkHttpService;
|
||||
import com.bonus.canteen.adapter.utils.ThreadPoolManager;
|
||||
import com.bonus.canteen.adapter.utils.WorkConfig;
|
||||
import com.bonus.canteen.adapter.menu.utils.OkHttpService;
|
||||
import com.bonus.canteen.adapter.menu.utils.ThreadPoolManager;
|
||||
import com.bonus.canteen.adapter.menu.utils.WorkConfig;
|
||||
import com.bonus.canteen.core.BaseActivity;
|
||||
import com.bonus.canteen.databinding.InitializationMainBinding;
|
||||
import com.bonus.canteen.db.AppDatabase;
|
||||
import com.bonus.canteen.db.entity.base.SecretKeyInfo;
|
||||
import com.bonus.canteen.db.entity.user.CustInfo;
|
||||
import com.bonus.canteen.utils.MemoryUtils;
|
||||
import com.google.gson.Gson;
|
||||
|
|
@ -59,6 +61,7 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
|
||||
public class InitializationActivity extends BaseActivity<InitializationMainBinding> {
|
||||
|
|
@ -97,8 +100,116 @@ public class InitializationActivity extends BaseActivity<InitializationMainBindi
|
|||
// //获取人员信息
|
||||
// iniListAdapter.addItem(new ListBean("开始更新人员信息...",0));
|
||||
// getPersonMessage();
|
||||
getSecretKey();
|
||||
}
|
||||
|
||||
private void getDeviceLogin(String encryptionSn,SecretKeyInfo secretKeyInfo){
|
||||
|
||||
List<SecretKeyInfo> dbList = AppDatabase.getDatabase(InitializationActivity.this).SecretKeyDao().geSecretKey();
|
||||
Log.i("getSecretKey dbList", dbList.size() + "");
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("deviceSn",encryptionSn);
|
||||
String jsonString = json.toString();
|
||||
Log.i("getSecretKey jsonString", jsonString);
|
||||
// 定义 JSON 的 MediaType
|
||||
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
|
||||
// 创建 RequestBody
|
||||
RequestBody body = RequestBody.create(JSON, jsonString);
|
||||
String url = WorkConfig.getBaseUrl() + "/oauth/device/login";
|
||||
Request request = new Request.Builder().url(url)
|
||||
.addHeader("Authorization", OkHttpService.getToken())
|
||||
.addHeader("X-Security-Server-Encrypted-Client-Key", secretKeyInfo.getServerEncryptClientKey())
|
||||
.post(body).build();
|
||||
try {
|
||||
String result = service.httpPost(url, body, InitializationActivity.this,request);
|
||||
JSONObject firstJson = JSONObject.parseObject(result);
|
||||
String code = firstJson.getString("code");
|
||||
if("200".equals(code)){
|
||||
String dataJson = firstJson.getString("data");
|
||||
JSONObject jsonObject = JSONObject.parseObject(dataJson);
|
||||
if(jsonObject.containsKey("tenantId") && jsonObject.containsKey("mqClientAddress")){
|
||||
String tenantId = jsonObject.getString("tenantId");
|
||||
String mqClientAddress = jsonObject.getString("mqClientAddress");
|
||||
String deviceToken = jsonObject.getString("deviceToken");
|
||||
String securityTokenSign = jsonObject.getString("securityTokenSign");
|
||||
secretKeyInfo.setTenantId(tenantId);
|
||||
secretKeyInfo.setMqClientAddress(mqClientAddress);
|
||||
secretKeyInfo.setDeviceToken(deviceToken);
|
||||
secretKeyInfo.setSecurityTokenSign(securityTokenSign);
|
||||
if(secretKeyInfo !=null){
|
||||
SecretKeyInfo sk = AppDatabase.getDatabase(InitializationActivity.this).SecretKeyDao().geSecretKeyBySn(secretKeyInfo.getSn());
|
||||
if(sk !=null){
|
||||
AppDatabase.getDatabase(InitializationActivity.this).SecretKeyDao().update(secretKeyInfo);
|
||||
sendMessage("获取设备基础信息、系统卡信息、设备自定义信息成功", 0);
|
||||
}
|
||||
}
|
||||
}else{
|
||||
sendMessage("获取设备基础信息、系统卡信息、设备自定义信息失败!", 1);
|
||||
}
|
||||
|
||||
}else{
|
||||
sendMessage("获取设备基础信息、系统卡信息、设备自定义信息失败!", 1);
|
||||
}
|
||||
Log.i("getDeviceLogin result", result);
|
||||
}catch (Exception e){
|
||||
sendMessage("获取设备基础信息、系统卡信息、设备自定义信息失败!", 1);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void getSecretKey() {
|
||||
String sn = "2AD2252000231631";
|
||||
String clickKey = "BIuPMoBHXV9RN/swO2HOtdlG0yMRSv7SZLxzhaYj2iy/9ltZ5MtiHsjecpKFv2IjPpRNmmoxO6sezu6P/wrOaeAfpckjTxg4hGPwbKu0m8s3CdzMDjs1TM8HnNuIPobIeLjmEOUkscJiIm67Mq7FVAU=";
|
||||
ThreadPoolManager.getExecutor().execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
SecretKeyInfo s = new SecretKeyInfo();
|
||||
s.setSn(sn);
|
||||
AppDatabase.getDatabase(InitializationActivity.this).SecretKeyDao().delete(s);
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("clientKey",clickKey);
|
||||
String jsonString = json.toString();
|
||||
Log.i("getSecretKey jsonString", jsonString);
|
||||
// 定义 JSON 的 MediaType
|
||||
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
|
||||
// 创建 RequestBody
|
||||
RequestBody body = RequestBody.create(JSON, jsonString);
|
||||
String result = service.httpPost(WorkConfig.getBaseUrl() + "/exchange/key", body, InitializationActivity.this);
|
||||
Log.i("getPersonMessage result", result);
|
||||
try {
|
||||
SecretKeyInfo secretKeyInfo = new SecretKeyInfo();
|
||||
JSONObject jsonObject = JSONObject.parseObject(result);
|
||||
if(jsonObject.containsKey("serverEncryptClientKey") && jsonObject.containsKey("sign")){
|
||||
String serverEncryptClientKey = jsonObject.getString("serverEncryptClientKey");
|
||||
String sign = jsonObject.getString("sign");
|
||||
secretKeyInfo.setServerEncryptClientKey(serverEncryptClientKey);
|
||||
secretKeyInfo.setSign(sign);
|
||||
secretKeyInfo.setSn(sn);
|
||||
if(secretKeyInfo !=null){
|
||||
SecretKeyInfo sk = AppDatabase.getDatabase(InitializationActivity.this).SecretKeyDao().geSecretKeyBySn(secretKeyInfo.getSn());
|
||||
if(sk ==null){
|
||||
AppDatabase.getDatabase(InitializationActivity.this).SecretKeyDao().insert(secretKeyInfo);
|
||||
sendMessage("获取密钥成功", 0);
|
||||
getDeviceLogin(clickKey,secretKeyInfo);
|
||||
}else{
|
||||
getDeviceLogin(clickKey,sk);
|
||||
}
|
||||
}
|
||||
}else{
|
||||
sendMessage("获取密钥失败", 1);
|
||||
}
|
||||
|
||||
|
||||
}catch (Exception e){
|
||||
sendMessage("获取密钥失败", 1);
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private void getPersonMessage() {
|
||||
ThreadPoolManager.getExecutor().execute(new Runnable() {
|
||||
|
|
|
|||
|
|
@ -36,8 +36,12 @@ public class OkHttpService {
|
|||
|
||||
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
|
||||
|
||||
private String token = "eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjozODAyNDc4MDU2MTQxMDA0ODAsInVzZXJfa2V5IjoiMmQyYmI2ODUtYmMzYS00NDhkLWI2MWYtMjkyMzMzY2RjNTg1IiwidXNlcm5hbWUiOiI3NWE5M2NmMDlkYTRkNTA2YzI2YzAxN2RhNjU4ZTkzMCJ9.lSjQAPQ_4CN_HUz88tYnjSTPzbnxA5WU2keiW2stc1F5tSg7rJWWMPdTHaTkaX_tOW0Vk8bFy8qjLjN-OnMLbA";
|
||||
private static String token = "eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjozNzkyNTg1Mjk2NDQ1NDgwOTYsInVzZXJfa2V5IjoiZjhlNmQ1MTktMGFhNC00MjRhLWIyYjMtMjM3NmM0YmY3NTM2IiwidXNlcm5hbWUiOiJkZTU4ZjYzNjU3Y2RhOGVkYjZiZTg3NTExMGUyMDNmNCJ9.D76MEwBazAwbVQnHG_7_TTf--jtQkBOpvNLOZrsDtps7fJIce1_YdCYg1k6Cc-bw_eLV9G13QOKHWm1vXofPqA";
|
||||
|
||||
|
||||
public static String getToken(){
|
||||
return token;
|
||||
}
|
||||
private volatile static OkHttpClient client;
|
||||
|
||||
public String encrypt(String v) {
|
||||
|
|
@ -203,6 +207,48 @@ public class OkHttpService {
|
|||
}
|
||||
return "";
|
||||
}
|
||||
public String httpPost(String url, RequestBody body, Context context,Request request) {
|
||||
try {
|
||||
OkHttpClient client = new OkHttpClient().newBuilder().connectTimeout(60000, TimeUnit.MILLISECONDS)
|
||||
.readTimeout(60000, TimeUnit.MILLISECONDS)
|
||||
.build();
|
||||
Response response = client.newCall(request).execute();
|
||||
int statueCode = response.code();
|
||||
Log.e(TAG, statueCode + "");
|
||||
if (statueCode == 200) {
|
||||
String json = response.body().string();
|
||||
Log.e(TAG, "json=" + response.toString());
|
||||
if (!StringHelper.isEmpty(json) && json.contains("OK")) {
|
||||
return json;
|
||||
}
|
||||
return json;
|
||||
}else if (statueCode==401){
|
||||
ThreadPoolManager.executeInUIThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// 在这里执行UI操作,如创建并显示AlertDialog
|
||||
// 使用JSON解析库解析主体内容
|
||||
JSONObject jsonResponse = null;
|
||||
try {
|
||||
String responseBody = response.body().string();
|
||||
jsonResponse = new JSONObject(responseBody);
|
||||
String message = jsonResponse.getString("message");
|
||||
if (message.contains("过期")){
|
||||
XToastUtils.warning("登录过期,请重新登录");
|
||||
}
|
||||
} catch (JSONException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.i(TAG, e.toString());
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public String httpPost(String url, RequestBody body, Context context) {
|
||||
try {
|
||||
|
|
|
|||
|
|
@ -2,17 +2,23 @@ package com.bonus.canteen.db;
|
|||
|
||||
import android.content.Context;
|
||||
|
||||
import com.bonus.canteen.db.dao.base.SecretKeyDao;
|
||||
import com.bonus.canteen.db.dao.user.CustInfoDao;
|
||||
import com.bonus.canteen.db.entity.base.SecretKeyInfo;
|
||||
import com.bonus.canteen.db.entity.user.CustInfo;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.room.Database;
|
||||
import androidx.room.Room;
|
||||
import androidx.room.RoomDatabase;
|
||||
import androidx.room.migration.Migration;
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase;
|
||||
|
||||
@Database(entities = {CustInfo.class}, version = 1)
|
||||
@Database(entities = {CustInfo.class, SecretKeyInfo.class}, version = 1)
|
||||
public abstract class AppDatabase extends RoomDatabase {
|
||||
|
||||
public abstract CustInfoDao custInfoDao(); // 提供 DAO 实例
|
||||
public abstract SecretKeyDao SecretKeyDao();
|
||||
|
||||
private static volatile AppDatabase INSTANCE;
|
||||
|
||||
|
|
@ -29,4 +35,5 @@ public abstract class AppDatabase extends RoomDatabase {
|
|||
return INSTANCE;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
package com.bonus.canteen.db.dao.base;
|
||||
|
||||
|
||||
import com.bonus.canteen.db.entity.base.SecretKeyInfo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import androidx.room.Dao;
|
||||
import androidx.room.Delete;
|
||||
import androidx.room.Insert;
|
||||
import androidx.room.Query;
|
||||
import androidx.room.Update;
|
||||
|
||||
@Dao
|
||||
public interface SecretKeyDao {
|
||||
|
||||
@Query("select * from secret_info where sn=:sn")
|
||||
SecretKeyInfo geSecretKeyBySn(String sn);
|
||||
|
||||
|
||||
@Query("select * from secret_info")
|
||||
List<SecretKeyInfo> geSecretKey();
|
||||
|
||||
@Insert
|
||||
void insert(SecretKeyInfo user); // 插入数据
|
||||
|
||||
@Update
|
||||
void update(SecretKeyInfo user); // 更新数据
|
||||
|
||||
@Delete
|
||||
void delete(SecretKeyInfo user); // 删除数据
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,103 @@
|
|||
package com.bonus.canteen.db.entity.base;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import androidx.room.ColumnInfo;
|
||||
import androidx.room.Entity;
|
||||
import androidx.room.PrimaryKey;
|
||||
|
||||
@Entity(tableName = "secret_info")
|
||||
public class SecretKeyInfo {
|
||||
|
||||
@PrimaryKey
|
||||
@NotNull
|
||||
private String sn;
|
||||
@ColumnInfo
|
||||
private String serverEncryptClientKey;
|
||||
|
||||
@ColumnInfo
|
||||
private String sign;
|
||||
|
||||
@ColumnInfo
|
||||
private String tenantId;
|
||||
|
||||
@ColumnInfo
|
||||
private String mqClientAddress;
|
||||
|
||||
@ColumnInfo
|
||||
private String deviceToken;
|
||||
|
||||
@ColumnInfo
|
||||
private String securityTokenSign;
|
||||
|
||||
@NotNull
|
||||
public String getServerEncryptClientKey() {
|
||||
return serverEncryptClientKey;
|
||||
}
|
||||
|
||||
public void setServerEncryptClientKey(@NotNull String serverEncryptClientKey) {
|
||||
this.serverEncryptClientKey = serverEncryptClientKey;
|
||||
}
|
||||
|
||||
public String getSign() {
|
||||
return sign;
|
||||
}
|
||||
|
||||
public void setSign(String sign) {
|
||||
this.sign = sign;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getSn() {
|
||||
return sn;
|
||||
}
|
||||
|
||||
public void setSn(@NotNull String sn) {
|
||||
this.sn = sn;
|
||||
}
|
||||
|
||||
public String getTenantId() {
|
||||
return tenantId;
|
||||
}
|
||||
|
||||
public void setTenantId(String tenantId) {
|
||||
this.tenantId = tenantId;
|
||||
}
|
||||
|
||||
public String getMqClientAddress() {
|
||||
return mqClientAddress;
|
||||
}
|
||||
|
||||
public void setMqClientAddress(String mqClientAddress) {
|
||||
this.mqClientAddress = mqClientAddress;
|
||||
}
|
||||
|
||||
public String getDeviceToken() {
|
||||
return deviceToken;
|
||||
}
|
||||
|
||||
public void setDeviceToken(String deviceToken) {
|
||||
this.deviceToken = deviceToken;
|
||||
}
|
||||
|
||||
public String getSecurityTokenSign() {
|
||||
return securityTokenSign;
|
||||
}
|
||||
|
||||
public void setSecurityTokenSign(String securityTokenSign) {
|
||||
this.securityTokenSign = securityTokenSign;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SecretKeyInfo{" +
|
||||
"sn='" + sn + '\'' +
|
||||
", serverEncryptClientKey='" + serverEncryptClientKey + '\'' +
|
||||
", sign='" + sign + '\'' +
|
||||
", tenantId='" + tenantId + '\'' +
|
||||
", mqClientAddress='" + mqClientAddress + '\'' +
|
||||
", deviceToken='" + deviceToken + '\'' +
|
||||
", securityTokenSign='" + securityTokenSign + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue