This commit is contained in:
2026-01-12 14:42:46 +08:00
parent 2fe0381987
commit 247a332ff3
9 changed files with 113 additions and 82 deletions
@@ -48,6 +48,7 @@ class InitActivity : BaseActivity() {
App.deviceId = AppUtil.getUDID( this) App.deviceId = AppUtil.getUDID( this)
// App.deviceId = "4787e213-90ab-3e32-88e0-ac271a937751"
Log.d(TAG, "onCreate: deviceId = ${App.deviceId}") Log.d(TAG, "onCreate: deviceId = ${App.deviceId}")
SpTool.put(SpTool.DEVICE_ID, App.deviceId) SpTool.put(SpTool.DEVICE_ID, App.deviceId)
App.configUrl = UrlConfig.BASE_URL App.configUrl = UrlConfig.BASE_URL
@@ -9,8 +9,8 @@ data class ShelfModel(
var goodsId: String = "", var goodsId: String = "",
var goodsName: String? = null, var goodsName: String? = null,
var deviceId: String? = null, var deviceId: String? = null,
var weight: Double? = null, var weight: Double = 0.0,
var weightBak: Double? = null, var weightBak: Double = 0.0,
var createTime: String? = null, var createTime: String? = null,
var putCabinetDay: String? = null, var putCabinetDay: String? = null,
var updateTime: String? = null, var updateTime: String? = null,
@@ -3,7 +3,6 @@ package com.shuwei.intelligent.shelves.net
import com.shuwei.intelligent.shelves.App import com.shuwei.intelligent.shelves.App
import com.shuwei.intelligent.shelves.model.DeviceConfigInfo import com.shuwei.intelligent.shelves.model.DeviceConfigInfo
import com.shuwei.intelligent.shelves.model.GoodsModel import com.shuwei.intelligent.shelves.model.GoodsModel
import com.shuwei.intelligent.shelves.model.GoodsRecord
import com.shuwei.intelligent.shelves.model.ShelfBody import com.shuwei.intelligent.shelves.model.ShelfBody
import com.shuwei.intelligent.shelves.model.ShelfResult import com.shuwei.intelligent.shelves.model.ShelfResult
import retrofit2.http.Body import retrofit2.http.Body
@@ -29,24 +28,19 @@ interface ApiService {
@GET("/equipment/stEquipment/queryByEquipmentCode") @GET("/equipment/stEquipment/queryByEquipmentCode")
suspend fun getDeviceInfo( suspend fun getDeviceInfo(
@Query("equipmentCode") equipmentCode: String, @Query("equipmentCode") equipmentCode: String,
@Query("appVersion") appVersion: String = App.appVersion, @Query("appVersion") appVersion: String = App.appVersion
@Header("X-Access-Token") token: String
): RespData<DeviceConfigInfo> ): RespData<DeviceConfigInfo>
@GET @GET
suspend fun getAccessToken( suspend fun getAccessToken(
@Url url: String = UrlConfig.GET_ACCESS_TOKEN, @Url url: String = UrlConfig.GET_ACCESS_TOKEN,
@Query("qrcodeId") qrcodeId: String, @Query("qrcodeId") qrcodeId: String
@Query("x-access-token") token: String = App.accessToken
): RespData<String?> ): RespData<String?>
@GET @GET
suspend fun getShelfList( suspend fun getShelfList(
@Url url: String = UrlConfig.GET_SHELF_LIST, @Url url: String = UrlConfig.GET_SHELF_LIST,
@Query("deviceId") deviceId: String, @Query("deviceId") deviceId: String
@Query("x-access-token") token: String = App.accessToken,
@Header("X-DEVICE-CODE") deviceCode:String = App.deviceId,
@Header("authorization") authorization: String = "57ee87183f2a4fa59683ec9ef41c8f5d"
): RespData<ShelfResult> ): RespData<ShelfResult>
@GET @GET
@@ -55,19 +49,13 @@ interface ApiService {
@Query("canteenId") canteenId: String = "0", @Query("canteenId") canteenId: String = "0",
@Query("goodsName") goodsName: String? = null, @Query("goodsName") goodsName: String? = null,
@Query("pageNo") pageNo: Int = 1, @Query("pageNo") pageNo: Int = 1,
@Query("pageSize") pageSize: Int = 50, @Query("pageSize") pageSize: Int = 50
@Query("x-access-token") token: String = App.accessToken,
@Header("X-DEVICE-CODE") deviceCode:String = App.deviceId,
@Header("authorization") authorization: String = "57ee87183f2a4fa59683ec9ef41c8f5d"
): RespData<MutableList<GoodsModel>?> ): RespData<MutableList<GoodsModel>?>
@POST @POST
suspend fun saveShelfGoodsList( suspend fun saveShelfGoodsList(
@Url url: String = UrlConfig.SAVE_SHELF_GOODS_LIST, @Url url: String = UrlConfig.SAVE_SHELF_GOODS_LIST,
@Body body: ShelfBody, @Body body: ShelfBody
@Query("x-access-token") token: String = App.accessToken,
@Header("X-DEVICE-CODE") deviceCode:String = App.deviceId,
@Header("authorization") authorization: String = "57ee87183f2a4fa59683ec9ef41c8f5d"
): RespData<Any?> ): RespData<Any?>
} }
@@ -33,7 +33,7 @@ class HttpManager private constructor() {
writeTimeout(15, TimeUnit.SECONDS) writeTimeout(15, TimeUnit.SECONDS)
sslSocketFactory(createSSLSocketFactory(), TrustAllCerts()) sslSocketFactory(createSSLSocketFactory(), TrustAllCerts())
hostnameVerifier { _, _ -> true } hostnameVerifier { _, _ -> true }
addInterceptor(LoggingInterceptor()) // addInterceptor(LoggingInterceptor())
addNetworkInterceptor(HttpLoggingInterceptor(logger = { addNetworkInterceptor(HttpLoggingInterceptor(logger = {
Log.d("HttpManager","okhttp logger ==>${it}") Log.d("HttpManager","okhttp logger ==>${it}")
}).apply { }).apply {
@@ -50,6 +50,7 @@ class HttpManager private constructor() {
// .build() // .build()
// chain.proceed(request) // chain.proceed(request)
// } // }
addInterceptor(RequestInterceptor())
} }
.build() .build()
} }
@@ -60,18 +61,18 @@ class HttpManager private constructor() {
// fun newCall(request: Request): Call = client.newCall(request) // fun newCall(request: Request): Call = client.newCall(request)
inner class LoggingInterceptor : Interceptor { // inner class LoggingInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response { // override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request() // val request = chain.request()
// 打印请求日志 // // 打印请求日志
Log.d("HttpManager", "--> ${request.method},${request.url},${request.body}") // Log.d("HttpManager", "--> ${request.method},${request.url},${request.body}")
//
val response = chain.proceed(request) // val response = chain.proceed(request)
// 打印响应日志 // // 打印响应日志
Log.d("HttpManager", "<-- ${response.body}") // Log.d("HttpManager", "<-- ${response.body}")
return response // return response
} // }
} // }
// 信任所有证书的TrustManager实现 // 信任所有证书的TrustManager实现
@SuppressLint("CustomX509TrustManager") @SuppressLint("CustomX509TrustManager")
@@ -45,7 +45,7 @@ class NetViewModel : ViewModel() {
_getDeviceTokenUiState.value = UiState.Loading _getDeviceTokenUiState.value = UiState.Loading
runCatching { runCatching {
val response = apiService.getDeviceToken(qrcodeId = deviceId) val response = apiService.getDeviceToken(qrcodeId = deviceId)
if (response.code == 0) { if (response.isSuccess()) {
_getDeviceTokenUiState.value = UiState.Success(response) _getDeviceTokenUiState.value = UiState.Success(response)
} else { } else {
_getDeviceTokenUiState.value = UiState.Error(response.msg ?: "请求失败") _getDeviceTokenUiState.value = UiState.Error(response.msg ?: "请求失败")
@@ -61,8 +61,8 @@ class NetViewModel : ViewModel() {
_getDeviceTokenUiState.value = UiState.Loading _getDeviceTokenUiState.value = UiState.Loading
runCatching { runCatching {
val response = val response =
apiService.getDeviceInfo(equipmentCode = deviceId, token = deviceToken) apiService.getDeviceInfo(equipmentCode = deviceId)
if (response.code == 0) { if (response.isSuccess()) {
_getDeviceTokenUiState.value = UiState.Success(response) _getDeviceTokenUiState.value = UiState.Success(response)
} else { } else {
_getDeviceTokenUiState.value = UiState.Error(response.msg ?: "请求失败") _getDeviceTokenUiState.value = UiState.Error(response.msg ?: "请求失败")
@@ -78,7 +78,7 @@ class NetViewModel : ViewModel() {
_getAccessTokenUiState.value = UiState.Loading _getAccessTokenUiState.value = UiState.Loading
runCatching { runCatching {
val response = apiService.getAccessToken(qrcodeId = deviceId) val response = apiService.getAccessToken(qrcodeId = deviceId)
if (response.code == 0) { if (response.isSuccess()) {
_getAccessTokenUiState.value = UiState.Success(response) _getAccessTokenUiState.value = UiState.Success(response)
} else { } else {
_getAccessTokenUiState.value = UiState.Error(response.msg ?: "请求失败") _getAccessTokenUiState.value = UiState.Error(response.msg ?: "请求失败")
@@ -94,7 +94,7 @@ class NetViewModel : ViewModel() {
_getShelfListUiState.value = UiState.Loading _getShelfListUiState.value = UiState.Loading
runCatching { runCatching {
val response = apiService.getShelfList(deviceId = deviceId) val response = apiService.getShelfList(deviceId = deviceId)
if (response.code == 0) { if (response.isSuccess()) {
_getShelfListUiState.value = UiState.Success(response) _getShelfListUiState.value = UiState.Success(response)
} else { } else {
_getShelfListUiState.value = UiState.Error(response.msg ?: "请求失败") _getShelfListUiState.value = UiState.Error(response.msg ?: "请求失败")
@@ -139,7 +139,7 @@ class NetViewModel : ViewModel() {
pageNo = pageNo, pageNo = pageNo,
pageSize = pageSize pageSize = pageSize
) )
if (response.code == 0) { if (response.isSuccess()) {
_getGoodsListUiState.value = UiState.Success(response) _getGoodsListUiState.value = UiState.Success(response)
} else { } else {
_getGoodsListUiState.value = UiState.Error(response.msg ?: "请求失败") _getGoodsListUiState.value = UiState.Error(response.msg ?: "请求失败")
@@ -0,0 +1,35 @@
package com.shuwei.intelligent.shelves.net
import android.text.TextUtils
import com.shuwei.intelligent.shelves.App
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
/**
* 请求拦截器
*/
class RequestInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val originalRequest = chain.request()
val requestBuilder = originalRequest.newBuilder()
.header("Content-Type", "application/json")
.header("Accept", "application/json")
.header("X-Access-Token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjYW50ZWVuSWQiOiJiZTE1NDgzMS0zNDY2LTNiYTItYTJlYS01NzY1MmM5MTlmZWQiLCJ0eXBlIjoiNCIsInVzZXJJZCI6IjAwMDAwMDAwMDAwMDAwMDAwMDEifQ.sN40cOC-O5WQFrF4IDUs8fFlkNdUKLbJt_rHyTsgYYM")
.header("X-DEVICE-CODE", App.deviceId)
.header("authorization", "57ee87183f2a4fa59683ec9ef41c8f5d")
val newRequest = requestBuilder.build()
return chain.proceed(newRequest)
}
// private fun getToken(originRequest: Request): String {
// val tokenParam = originRequest.header("X-Access-Token")
// if (!TextUtils.isEmpty(tokenParam)) return tokenParam!!
// // 从本地获取token的逻辑
// val spUtil = SPUtil.getInstance(context = App.getContext())
// return spUtil.get(GlobalKey.KEY_TOKEN, "") as String
//// return "eyJhbGciOiJIUzUxMiJ9.eyJpZCI6MTQ2LCJ1c2VyTmFtZSI6IjEzNjgxNDQ4ODU2IiwibmFtZSI6IuW-kOejiiIsInBhc3N3b3JkIjoiOTllOTQ1ZmVjZmZjNWIzNDI4MmUwNDRlODYyMzdjM2UxZjU5OWY5OCIsInNhbHQiOiI0NmEzMzUzYWU4OTA0MDYxYjMzODU5ZWNlYTBlMGE2NyIsInBob25lIjoiMTM2ODE0NDg4NTYiLCJzdGF0dXMiOjEsInVzZXJUeXBlIjoyLCJjcmVhdGVVc2VyTm8iOiIxNDEiLCJjcmVhdGVUaW1lIjoxNjI2OTQzNDE2MDAwLCJ1cGRhdGVVc2VyTm8iOiIxNDEiLCJ1cGRhdGVUaW1lIjoxNjI2OTQzNDE2MDAwLCJpc0RlbCI6ZmFsc2UsImVhSWQiOjk5LCJlYUlkTGlzdCI6Ijk5IiwiaXNTaG9wTWFuYWdlciI6dHJ1ZSwidXNlck5vIjoiMWY5Nzk5ZWMtODlkYi00MWYyLTk1YTEtY2UzNTA3Y2QyMTU2In0.f7wImPgBOYMV0AqRchnXGPkUWZN9dFJ9gLPsaB8uNldd21IfXLjJl8y-FiWVuVUvlwUvGpgqGDFR1JKj5H7amw"
// }
}
@@ -3,7 +3,9 @@ package com.shuwei.intelligent.shelves.net
data class RespData<T>( data class RespData<T>(
// val success: Boolean = false, // val success: Boolean = false,
val msg: String? = null, val msg: String? = null,
val code: Int = 0, val code: String? = null,
val data: T? = null, val data: T? = null,
val total: Int // val total: Int
) ) {
fun isSuccess() = code == "00000"
}
@@ -48,15 +48,19 @@ class SyncTask(appContext: Context, workerParams: WorkerParameters) :
// it.humidity = HomeActivity.showHumidity // it.humidity = HomeActivity.showHumidity
// } // }
val body = ShelfBody().also { val body = ShelfBody().also {
it.deviceId = App.deviceId ?: "" it.deviceId = App.deviceId
it.canteenId = App.canteenId it.canteenId = App.canteenId
it.temperature = HomeActivity.showTemperatureC it.temperature = HomeActivity.showTemperatureC
it.humidity = HomeActivity.showHumidity it.humidity = HomeActivity.showHumidity
it.goodsList = submitList it.goodsList = submitList
} }
Log.d(TAG, "performSync: body:${body.toJsonString()}") Log.d(TAG, "performSync: body:${body.toJsonString()}")
val resp = apiService.saveShelfGoodsList(body = body) try {
Log.d(TAG, "performSync: resp:${resp.toJsonString()}") val resp = apiService.saveShelfGoodsList(body = body)
Log.d(TAG, "performSync: resp:${resp.toJsonString()}")
} catch (e: Exception) {
e.printStackTrace()
}
} }
} }
@@ -406,45 +406,45 @@ public class AppUtil {
*/ */
@SuppressLint("MissingPermission") @SuppressLint("MissingPermission")
public static String getUDID(Context context) { public static String getUDID(Context context) {
return "SWZC-HG-001"; // return "SWZC-HG-001";
//// //"test111"; //// //"test111";
// String androidID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); String androidID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
// if (!androidID.equals("")) { if (!androidID.equals("")) {
// try { try {
// if (!"9774d56d682e549c".equals(androidID)) { if (!"9774d56d682e549c".equals(androidID)) {
// androidID = UUID.nameUUIDFromBytes(androidID.getBytes("utf8")).toString(); androidID = UUID.nameUUIDFromBytes(androidID.getBytes("utf8")).toString();
// } else { } else {
// @SuppressLint("MissingPermission") final String deviceId = ((TelephonyManager) context.getSystemService(TELEPHONY_SERVICE)).getDeviceId(); @SuppressLint("MissingPermission") final String deviceId = ((TelephonyManager) context.getSystemService(TELEPHONY_SERVICE)).getDeviceId();
// androidID = deviceId != null ? UUID.nameUUIDFromBytes(deviceId.getBytes("utf8")).toString() : UUID.randomUUID().toString(); androidID = deviceId != null ? UUID.nameUUIDFromBytes(deviceId.getBytes("utf8")).toString() : UUID.randomUUID().toString();
// } }
// } catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
// throw new RuntimeException(e); throw new RuntimeException(e);
// } }
// return androidID; return androidID;
// } }
//
// //需要权限 android.permission.READ_PHONE_STATE //需要权限 android.permission.READ_PHONE_STATE
// TelephonyManager TelephonyMgr = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE); TelephonyManager TelephonyMgr = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);
// String szImei = TelephonyMgr.getDeviceId(); String szImei = TelephonyMgr.getDeviceId();
// if (!szImei.equals("")) { if (!szImei.equals("")) {
// return szImei; return szImei;
// } }
//
// //需要权限 android.permission.ACCESS_WIFI_STATE //需要权限 android.permission.ACCESS_WIFI_STATE
// WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
// String m_szWLANMAC = wm.getConnectionInfo().getMacAddress(); String m_szWLANMAC = wm.getConnectionInfo().getMacAddress();
// if (!m_szWLANMAC.equals("")) { if (!m_szWLANMAC.equals("")) {
// return m_szWLANMAC; return m_szWLANMAC;
// } }
//
// //需要权限 android.permission.BLUETOOTH //需要权限 android.permission.BLUETOOTH
// BluetoothAdapter m_BluetoothAdapter = null; BluetoothAdapter m_BluetoothAdapter = null;
// m_BluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); m_BluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// String m_szBTMAC = m_BluetoothAdapter.getAddress(); String m_szBTMAC = m_BluetoothAdapter.getAddress();
// if (!m_szBTMAC.equals("")) { if (!m_szBTMAC.equals("")) {
// return m_szBTMAC; return m_szBTMAC;
// } }
// return getUniquePsuedoID(); return getUniquePsuedoID();
} }
//获得 Psuedo ID //获得 Psuedo ID