通过短信验证码注册

This commit is contained in:
BA7LZD 2020-05-27 18:53:42 +08:00
parent f3dfe74b42
commit b837e20d90
7 changed files with 115 additions and 6 deletions

View File

@ -4,6 +4,7 @@ package com.yuxihan.sdu.data;
import com.yuxihan.sdu.data.model.DataBean;
import com.yuxihan.sdu.data.model.LoginParams;
import com.yuxihan.sdu.data.model.RegParams;
import com.yuxihan.sdu.data.model.SMSParams;
import com.yuxihan.sdu.data.model.UpdateNicknameParams;
import com.yuxihan.sdu.data.model.UpdateUserHeadParams;
@ -36,4 +37,10 @@ public interface UpdateService {
Call<DataBean> updateNickname(
@Body UpdateNicknameParams params
);
@POST("/sendSms")
Call<DataBean> sendSMS(
@Body SMSParams params
);
}

View File

@ -7,7 +7,7 @@ public class LoggedInUser {
private String userId;
private String displayName;
boolean loginSuccess = true;
private boolean loginSuccess = true;
public LoggedInUser(String userId, String displayName) {
this.userId = userId;

View File

@ -0,0 +1,23 @@
package com.yuxihan.sdu.data.model;
import com.yuxihan.sdu.comm.network.BaseRequestParams;
public class SMSParams extends BaseRequestParams {
private String phoneNum;
public SMSParams(String phoneNum) {
this.phoneNum = phoneNum;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
}

View File

@ -0,0 +1,21 @@
package com.yuxihan.sdu.data.model;
/**
* Data class that captures user information for logged in users retrieved from LoginRepository
*/
public class SendSMSResult {
private boolean sendSuccess = true;
public SendSMSResult(boolean sendSuccess) {
this.sendSuccess = sendSuccess;
}
public boolean isSendSuccess() {
return sendSuccess;
}
public void setSendSuccess(boolean sendSuccess) {
this.sendSuccess = sendSuccess;
}
}

View File

@ -63,7 +63,7 @@ public class LoginViewModel extends ViewModel {
public void onFailure(Call<DataBean> call, Throwable t) {
//请求失败
Log.e("TAG", "请求失败:" + t.getMessage());
loggedInUser.setValue(new LoggedInUser(username, "\"服务器开小差了!\"", false));
loggedInUser.setValue(new LoggedInUser(username, "服务器开小差了!", false));
}
});
}

View File

@ -24,7 +24,9 @@ import androidx.lifecycle.ViewModelProvider;
import com.gyf.immersionbar.ImmersionBar;
import com.yuxihan.sdu.R;
import com.yuxihan.sdu.comm.BaseActivity;
import com.yuxihan.sdu.comm.SDUApp;
import com.yuxihan.sdu.data.model.LoggedInUser;
import com.yuxihan.sdu.data.model.SendSMSResult;
import com.yuxihan.sdu.ui.login.LoginActivity;
public class RegActivity extends BaseActivity {
@ -53,7 +55,26 @@ public class RegActivity extends BaseActivity {
@Override
public void onClick(View v) {
//TODO:请求发送验证码
btGetVerification.setText("验证码已发送");
InputMethodManager manager =
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE));
if (manager != null) {
manager.hideSoftInputFromWindow(loginButton.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
loadingProgressBar.setVisibility(View.VISIBLE);
regViewModel.sendSMS(usernameEditText.getText().toString());
}
});
regViewModel.getSendSMSResult().observe(this, new Observer<SendSMSResult>() {
@Override
public void onChanged(SendSMSResult sendSMSResult) {
if (sendSMSResult.isSendSuccess()){
loadingProgressBar.setVisibility(View.GONE);
Toast.makeText(SDUApp.getAppContext(), "验证码发送成功", Toast.LENGTH_SHORT).show();
btGetVerification.setText("验证码已发送");
}
}
});

View File

@ -2,6 +2,8 @@ package com.yuxihan.sdu.ui.reg;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
@ -13,6 +15,8 @@ import com.yuxihan.sdu.data.UpdateService;
import com.yuxihan.sdu.data.model.DataBean;
import com.yuxihan.sdu.data.model.LoggedInUser;
import com.yuxihan.sdu.data.model.RegParams;
import com.yuxihan.sdu.data.model.SMSParams;
import com.yuxihan.sdu.data.model.SendSMSResult;
import retrofit2.Call;
import retrofit2.Callback;
@ -21,6 +25,11 @@ import retrofit2.Response;
public class RegViewModel extends ViewModel {
private MutableLiveData<LoggedInUser> loggedInUser = new MutableLiveData<>();
private MutableLiveData<RegFormState> regFormState = new MutableLiveData<>();
private MutableLiveData<SendSMSResult> sendSMSResult = new MutableLiveData<>();
public MutableLiveData<SendSMSResult> getSendSMSResult() {
return sendSMSResult;
}
public MutableLiveData<LoggedInUser> getLoggedInUser() {
return loggedInUser;
@ -30,10 +39,37 @@ public class RegViewModel extends ViewModel {
return regFormState;
}
public void sendSMS(String phoneNum) {
UpdateService updateService = SDUApp.getRetrofit().create(UpdateService.class);
Call<DataBean> call = updateService.sendSMS(new SMSParams(phoneNum));
call.enqueue(new Callback<DataBean>() {
@Override
public void onResponse(Call<DataBean> call, Response<DataBean> response) {
//请求成功返回是一个封装为DataBean的响应
Log.e("TAG", "接口返回内容 ===== " + response.body().getResult());
if ("0".equals(response.body().getErrCode())) {
String token = response.body().getResult().getToken();
sendSMSResult.setValue(new SendSMSResult(true));
} else {
Toast.makeText(SDUApp.getAppContext(), response.body().getErrMsg(),
Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<DataBean> call, Throwable t) {
//请求失败
Log.e("TAG", "请求失败:" + t.getMessage());
Toast.makeText(SDUApp.getAppContext(), "服务器开小差了!",
Toast.LENGTH_LONG).show();
}
});
}
public void reg(String username, String password, String verificationCode) {
UpdateService updateService = SDUApp.getRetrofit().create(UpdateService.class);
Call<DataBean> call = updateService.reg(new RegParams(username, password,verificationCode));
Call<DataBean> call = updateService.reg(new RegParams(username, password,
verificationCode));
call.enqueue(new Callback<DataBean>() {
@Override
public void onResponse(Call<DataBean> call, Response<DataBean> response) {
@ -43,7 +79,8 @@ public class RegViewModel extends ViewModel {
String token = response.body().getResult().getToken();
loggedInUser.setValue(new LoggedInUser(username, username));
} else {
loggedInUser.setValue(new LoggedInUser(username, response.body().getErrMsg(), false));
loggedInUser.setValue(new LoggedInUser(username, response.body().getErrMsg(),
false));
}
}
@ -51,7 +88,7 @@ public class RegViewModel extends ViewModel {
public void onFailure(Call<DataBean> call, Throwable t) {
//请求失败
Log.e("TAG", "请求失败:" + t.getMessage());
loggedInUser.setValue(new LoggedInUser(username, "\"服务器开小差了!\"", false));
loggedInUser.setValue(new LoggedInUser(username, "服务器开小差了!", false));
}
});
}