完成登陆功能
<h3>改造后端登陆方法</h3>
<p>修改UserCOntroller.java中的代码</p>
<pre><code class="language-java">@PostMapping("login")
@ApiOperation("登录")
public R login(@RequestBody LoginForm form){
//表单校验
ValidatorUtils.validateEntity(form);
//用户登录
long userId = userService.login(form);
//生成token
String token = jwtUtils.generateToken(userId);
Map<String, Object> map = new HashMap<>();
map.put("token", token);
map.put("expire", jwtUtils.getExpire());
return R.ok(map);
}</code></pre>
<h3>绑定登陆页面</h3>
<p>创建LoginActivity.java,并且绑定login.html页面</p>
<pre><code class="language-java">public class LoginActivity extends AppCompatActivity {
private WebView login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login=findViewById(R.id.login);
WebSettings settings=login.getSettings();
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setDomStorageEnabled(true);
login.addJavascriptInterface(new LoginScript(),"login");
login.loadUrl("file:///android_asset/login.html");
}
public class LoginScript{
@JavascriptInterface
public void toOrderPage(){
Intent intent=new Intent();
intent.setClass(LoginActivity.this,OrderActivity.class);
startActivity(intent);
finish();
}
}
}</code></pre>