Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Y
YGOMobile
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
fallenstardust
YGOMobile
Commits
873b5119
Commit
873b5119
authored
May 19, 2025
by
wangfugui
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
重构登录、登录校验
parent
1b20a7be
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
169 additions
and
32 deletions
+169
-32
mobile/src/main/java/cn/garymb/ygomobile/deck_square/DeckSquareApiUtil.java
...va/cn/garymb/ygomobile/deck_square/DeckSquareApiUtil.java
+118
-22
mobile/src/main/java/cn/garymb/ygomobile/deck_square/LoginDialog.java
...ain/java/cn/garymb/ygomobile/deck_square/LoginDialog.java
+3
-1
mobile/src/main/java/cn/garymb/ygomobile/deck_square/MyDeckListAdapter.java
...va/cn/garymb/ygomobile/deck_square/MyDeckListAdapter.java
+10
-6
mobile/src/main/java/cn/garymb/ygomobile/deck_square/api_response/LoginRequest.java
...rymb/ygomobile/deck_square/api_response/LoginRequest.java
+2
-2
mobile/src/main/java/cn/garymb/ygomobile/deck_square/api_response/LoginToken.java
...garymb/ygomobile/deck_square/api_response/LoginToken.java
+35
-0
mobile/src/main/java/cn/garymb/ygomobile/utils/OkhttpUtil.java
...e/src/main/java/cn/garymb/ygomobile/utils/OkhttpUtil.java
+1
-1
No files found.
mobile/src/main/java/cn/garymb/ygomobile/deck_square/DeckSquareApiUtil.java
View file @
873b5119
...
...
@@ -13,12 +13,14 @@ import cn.garymb.ygomobile.deck_square.api_response.DeckIdResponse;
import
cn.garymb.ygomobile.deck_square.api_response.DownloadDeckResponse
;
import
cn.garymb.ygomobile.deck_square.api_response.LoginRequest
;
import
cn.garymb.ygomobile.deck_square.api_response.LoginResponse
;
import
cn.garymb.ygomobile.deck_square.api_response.LoginToken
;
import
cn.garymb.ygomobile.deck_square.api_response.MyDeckResponse
;
import
cn.garymb.ygomobile.deck_square.api_response.PushCardJson
;
import
cn.garymb.ygomobile.deck_square.api_response.PushDeckResponse
;
import
cn.garymb.ygomobile.deck_square.api_response.SquareDeckResponse
;
import
cn.garymb.ygomobile.utils.LogUtil
;
import
cn.garymb.ygomobile.utils.OkhttpUtil
;
import
cn.garymb.ygomobile.utils.SharedPreferenceUtil
;
import
cn.garymb.ygomobile.utils.YGOUtil
;
import
okhttp3.MediaType
;
import
okhttp3.OkHttpClient
;
...
...
@@ -30,6 +32,34 @@ public class DeckSquareApiUtil {
private
static
final
String
TAG
=
DeckSquareListAdapter
.
class
.
getSimpleName
();
public
static
LoginToken
getLoginData
()
{
String
serverToken
=
SharedPreferenceUtil
.
getServerToken
();
Integer
serverUserId
=
SharedPreferenceUtil
.
getServerUserId
();
if
(
serverToken
==
null
||
serverUserId
==
-
1
)
{
YGOUtil
.
showTextToast
(
"Please login first!"
);
return
null
;
}
return
new
LoginToken
(
serverUserId
,
serverToken
);
}
/**
* 如果未登录(不存在token),显示toast提示用户。如果已登录,返回token
*
* @return
*/
public
static
String
getServerToken
()
{
String
serverToken
=
SharedPreferenceUtil
.
getServerToken
();
//todo serverToken要外部传入还是此处获取?考虑
if
(
serverToken
==
null
)
{
YGOUtil
.
showTextToast
(
"Please login first!"
);
return
null
;
}
else
{
return
serverToken
;
}
}
public
static
SquareDeckResponse
getSquareDecks
()
throws
IOException
{
SquareDeckResponse
result
=
null
;
String
url
=
"http://rarnu.xyz:38383/api/mdpro3/deck/list"
;
...
...
@@ -50,24 +80,23 @@ public class DeckSquareApiUtil {
* 阻塞方法
* 获取指定用户的卡组列表(只能用于获取登录用户本人的卡组)
*
* @param serverUserId
* @param serverToken
* @param loginToken
* @return
*/
public
static
MyDeckResponse
getUserDecks
(
Integer
serverUserId
,
String
server
Token
)
throws
IOException
{
public
static
MyDeckResponse
getUserDecks
(
LoginToken
login
Token
)
throws
IOException
{
if
(
server
Token
==
null
)
{
if
(
login
Token
==
null
)
{
YGOUtil
.
showTextToast
(
"Login first"
,
Toast
.
LENGTH_LONG
);
return
null
;
}
MyDeckResponse
result
=
null
;
String
url
=
"http://rarnu.xyz:38383/api/mdpro3/sync/"
+
serverUserId
+
"/nodel"
;
String
url
=
"http://rarnu.xyz:38383/api/mdpro3/sync/"
+
loginToken
.
getUserId
()
+
"/nodel"
;
Map
<
String
,
String
>
headers
=
new
HashMap
<>();
headers
.
put
(
"ReqSource"
,
"MDPro3"
);
headers
.
put
(
"token"
,
serverToken
);
headers
.
put
(
"token"
,
loginToken
.
getServerToken
()
);
Response
response
=
OkhttpUtil
.
synchronousGet
(
url
,
null
,
headers
);
String
responseBodyString
=
response
.
body
().
string
();
...
...
@@ -83,7 +112,7 @@ public class DeckSquareApiUtil {
/**
* 阻塞方法
* 根据卡组ID查询一个卡组
* 根据卡组ID查询一个卡组
,不需要传入token,可以查询已登录用户或其它未登录用户的卡组
*
* @param deckId
* @return
...
...
@@ -111,26 +140,23 @@ public class DeckSquareApiUtil {
/**
* 阻塞方法
* 先同步推送,之后异步推送。首先
获取卡组id,之后将卡组id设置到ydk中,之后将其
上传
* 阻塞方法
,用于推送新卡组。首先从服务器请求一个新的卡组id,之后将卡组上传到服务器
* 先同步推送,之后异步推送。首先
调用服务端api获取卡组id,之后将卡组id设置到ydk中,之后调用服务器api将卡组
上传
*
* @param deckPath
* @param deckName
* @param userId
*/
public
static
PushDeckResponse
pushDeck
(
String
deckPath
,
String
deckName
,
Integer
userId
,
String
serverToken
)
throws
IOException
{
PushDeckResponse
result
=
null
;
public
static
PushDeckResponse
requestIdAndPushDeck
(
String
deckPath
,
String
deckName
,
LoginToken
loginToken
)
throws
IOException
{
if
(
server
Token
==
null
)
{
if
(
login
Token
==
null
)
{
return
null
;
}
String
url
=
"http://rarnu.xyz:38383/api/mdpro3/sync/single"
;
String
getDeckIdUrl
=
"http://rarnu.xyz:38383/api/mdpro3/deck/deckId"
;
Map
<
String
,
String
>
headers
=
new
HashMap
<>();
headers
.
put
(
"ReqSource"
,
"MDPro3"
);
headers
.
put
(
"token"
,
serverToken
);
headers
.
put
(
"token"
,
loginToken
.
getServerToken
()
);
Gson
gson
=
new
Gson
();
...
...
@@ -143,14 +169,45 @@ public class DeckSquareApiUtil {
LogUtil
.
i
(
TAG
,
"deck id result:"
+
deckIdResult
.
toString
());
}
if
(
deckIdResult
==
null
)
{
return
null
;
}
String
deckId
=
deckIdResult
.
getDeckId
();
//从服务器获取
if
(
deckId
==
null
)
{
return
null
;
}
return
pushDeck
(
deckPath
,
deckName
,
loginToken
,
deckId
);
}
/**
* 将对应于deckId、deckName的卡组内容json推送到服务器。
* 如果在服务器上不存在deckId、deckName对应的记录,则创建新卡组
* 如果在服务器存在deckId相同的记录,则更新卡组,deckName会覆盖服务器上的卡组名
* 如果在服务器存在deckName相同、deckId不同的记录,则更新失败
*
* @param deckPath
* @param deckName
* @param loginToken
* @param deckId
* @return
* @throws IOException
*/
public
static
PushDeckResponse
pushDeck
(
String
deckPath
,
String
deckName
,
LoginToken
loginToken
,
String
deckId
)
throws
IOException
{
String
deckContent
=
DeckSquareFileUtil
.
setDeckId
(
deckPath
,
loginToken
.
getUserId
(),
deckId
);
PushDeckResponse
result
=
null
;
String
url
=
"http://rarnu.xyz:38383/api/mdpro3/sync/single"
;
Map
<
String
,
String
>
headers
=
new
HashMap
<>();
headers
.
put
(
"ReqSource"
,
"MDPro3"
);
headers
.
put
(
"token"
,
loginToken
.
getServerToken
());
String
deckContent
=
DeckSquareFileUtil
.
setDeckId
(
deckPath
,
userId
,
deckId
);
Gson
gson
=
new
Gson
();
PushCardJson
pushCardJson
=
new
PushCardJson
();
pushCardJson
.
setDeckContributor
(
userId
.
toString
());
pushCardJson
.
setUserId
(
userId
);
pushCardJson
.
setDeckContributor
(
loginToken
.
getUserId
()
.
toString
());
pushCardJson
.
setUserId
(
loginToken
.
getUserId
()
);
PushCardJson
.
DeckData
deckData
=
new
PushCardJson
.
DeckData
();
deckData
.
setDeckId
(
deckId
);
...
...
@@ -171,7 +228,6 @@ public class DeckSquareApiUtil {
return
result
;
}
/**
...
...
@@ -198,14 +254,14 @@ public class DeckSquareApiUtil {
}
public
static
LoginResponse
login
(
String
userId
,
String
password
)
throws
IOException
{
public
static
LoginResponse
login
(
Integer
userId
,
String
password
)
throws
IOException
{
LoginResponse
result
=
null
;
String
url
=
"https://sapi.moecube.com:444/accounts/signin"
;
String
baseUrl
=
"https://sapi.moecube.com:444/accounts/signin"
;
// Create request body using Gson
Gson
gson
=
new
Gson
();
userId
=
"1076306278@qq.com"
;
userId
=
107630627
;
password
=
"Qbz95qbz96"
;
LoginRequest
loginRequest
=
new
LoginRequest
(
userId
,
password
);
...
...
@@ -242,4 +298,44 @@ public class DeckSquareApiUtil {
}
public
static
PushDeckResponse
deleteDeck
(
String
deckId
,
LoginToken
loginToken
)
throws
IOException
{
PushDeckResponse
result
=
null
;
String
url
=
"http://rarnu.xyz:38383/api/mdpro3/sync/single"
;
Map
<
String
,
String
>
headers
=
new
HashMap
<>();
headers
.
put
(
"ReqSource"
,
"MDPro3"
);
headers
.
put
(
"token"
,
loginToken
.
getServerToken
());
Gson
gson
=
new
Gson
();
PushCardJson
pushCardJson
=
new
PushCardJson
();
PushCardJson
.
DeckData
deckData
=
new
PushCardJson
.
DeckData
();
deckData
.
setDeckId
(
deckId
);
deckData
.
setDelete
(
true
);
pushCardJson
.
setDeck
(
deckData
);
pushCardJson
.
setUserId
(
loginToken
.
getUserId
());
String
json
=
gson
.
toJson
(
pushCardJson
);
Response
response
=
OkhttpUtil
.
postJson
(
url
,
json
,
headers
,
1000
);
String
responseBodyString
=
response
.
body
().
string
();
// Convert JSON to Java object using Gson
result
=
gson
.
fromJson
(
responseBodyString
,
PushDeckResponse
.
class
);
LogUtil
.
i
(
TAG
,
"push deck response:"
+
responseBodyString
);
return
result
;
}
/**
* 管理员使用,删除某卡组(听说可以删除别人的卡组,没试过)
* 该api没有权限校验,慎用
*/
public
static
void
adminDelete
(
String
deckId
)
{
String
url
=
"http://rarnu.xyz:38383/api/mdpro3/deck/"
+
deckId
;
}
}
\ No newline at end of file
mobile/src/main/java/cn/garymb/ygomobile/deck_square/LoginDialog.java
View file @
873b5119
...
...
@@ -12,6 +12,7 @@ import android.widget.ProgressBar;
import
android.widget.Toast
;
import
cn.garymb.ygomobile.deck_square.api_response.LoginResponse
;
import
cn.garymb.ygomobile.deck_square.api_response.LoginToken
;
import
cn.garymb.ygomobile.lite.R
;
import
cn.garymb.ygomobile.ui.plus.VUiKit
;
import
cn.garymb.ygomobile.utils.LogUtil
;
...
...
@@ -69,7 +70,7 @@ public class LoginDialog extends Dialog {
VUiKit
.
defer
().
when
(()
->
{
LogUtil
.
d
(
TAG
,
"start fetch"
);
LoginResponse
result
=
DeckSquareApiUtil
.
login
(
""
,
""
);
LoginResponse
result
=
DeckSquareApiUtil
.
login
(
1
,
""
);
SharedPreferenceUtil
.
setServerToken
(
result
.
token
);
SharedPreferenceUtil
.
setServerUserId
(
result
.
user
.
id
);
return
result
;
...
...
@@ -96,4 +97,5 @@ public class LoginDialog extends Dialog {
});
}
}
\ No newline at end of file
mobile/src/main/java/cn/garymb/ygomobile/deck_square/MyDeckListAdapter.java
View file @
873b5119
...
...
@@ -7,10 +7,12 @@ import android.widget.ImageView;
import
com.chad.library.adapter.base.BaseQuickAdapter
;
import
com.chad.library.adapter.base.viewholder.BaseViewHolder
;
import
java.util.ArrayList
;
import
java.util.List
;
import
cn.garymb.ygomobile.deck_square.api_response.
MyOnlineDeckDetail
;
import
cn.garymb.ygomobile.deck_square.api_response.
LoginToken
;
import
cn.garymb.ygomobile.deck_square.api_response.MyDeckResponse
;
import
cn.garymb.ygomobile.deck_square.api_response.MyOnlineDeckDetail
;
import
cn.garymb.ygomobile.lite.R
;
import
cn.garymb.ygomobile.loader.ImageLoader
;
import
cn.garymb.ygomobile.ui.plus.VUiKit
;
...
...
@@ -29,17 +31,19 @@ public class MyDeckListAdapter extends BaseQuickAdapter<MyDeckItem, BaseViewHold
}
public
void
loadData
()
{
List
<
MyDeckItem
>
localDecks
=
DeckSquareFileUtil
.
getMyDeckItem
();
List
<
MyDeckItem
>
localDecks
=
new
ArrayList
<>
();
// final DialogPlus dialog_read_ex = DialogPlus.show(getContext(), null, getContext().getString(R.string.fetch_ex_card));
String
serverToken
=
SharedPreferenceUtil
.
getServerToken
();
Integer
serverUserId
=
SharedPreferenceUtil
.
getServerUserId
();
LoginToken
loginToken
=
DeckSquareApiUtil
.
getLoginData
();
if
(
loginToken
==
null
){
return
;
}
VUiKit
.
defer
().
when
(()
->
{
LogUtil
.
d
(
TAG
,
"start fetch"
+
serverToken
+
" "
+
serverUserId
);
MyDeckResponse
result
=
DeckSquareApiUtil
.
getUserDecks
(
serverUserId
,
serverToken
);
MyDeckResponse
result
=
DeckSquareApiUtil
.
getUserDecks
(
loginToken
);
if
(
result
==
null
)
{
return
null
;
...
...
mobile/src/main/java/cn/garymb/ygomobile/deck_square/api_response/LoginRequest.java
View file @
873b5119
package
cn.garymb.ygomobile.deck_square.api_response
;
public
class
LoginRequest
{
public
String
account
;
public
Integer
account
;
public
String
password
;
public
LoginRequest
(
String
account
,
String
password
)
{
public
LoginRequest
(
Integer
account
,
String
password
)
{
this
.
account
=
account
;
this
.
password
=
password
;
}
...
...
mobile/src/main/java/cn/garymb/ygomobile/deck_square/api_response/LoginToken.java
0 → 100644
View file @
873b5119
package
cn.garymb.ygomobile.deck_square.api_response
;
public
class
LoginToken
{
Integer
userId
;
String
serverToken
;
public
LoginToken
(
Integer
userId
,
String
serverToken
)
{
this
.
userId
=
userId
;
this
.
serverToken
=
serverToken
;
}
public
Integer
getUserId
()
{
return
userId
;
}
public
void
setUserId
(
Integer
userId
)
{
this
.
userId
=
userId
;
}
public
String
getServerToken
()
{
return
serverToken
;
}
public
void
setServerToken
(
String
serverToken
)
{
this
.
serverToken
=
serverToken
;
}
@Override
public
String
toString
()
{
return
"LoginToken{"
+
"userId="
+
userId
+
", serverToken='"
+
serverToken
+
'\''
+
'}'
;
}
}
mobile/src/main/java/cn/garymb/ygomobile/utils/OkhttpUtil.java
View file @
873b5119
...
...
@@ -283,6 +283,7 @@ public class OkhttpUtil {
}
/**
* 阻塞方法,POST推送json
* @param url
* @param json 可以传入null或空字符串,均代表不需要发送json
* @param headers 可以传入null
...
...
@@ -314,7 +315,6 @@ public class OkhttpUtil {
Log
.
e
(
"OkhttpUtil"
,
json
+
" 状态 "
+
request
.
build
());
return
okHttpClient
.
newCall
(
request
.
build
()).
execute
();
//okHttpClient.newCall(request.build()).enqueue(callback);
}
/**
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment