下文只是简单实现,客户端以Android端的实现为例:
用户表Account:
package com.microsoft.ecodrive.model; public class Account { @com.google.gson.annotations.SerializedName("id") public String id; @com.google.gson.annotations.SerializedName("username") public String username; @com.google.gson.annotations.SerializedName("password") public String password; @Override public boolean equals(Object o) { return o instanceof Account && ((Account) o).id == id; } }
一、登录
1、服务端新建api:login,将其script替换如下:
exports.post = function(request, response) { // Use "request.service" to access features of your mobile service, e.g.: // var tables = request.service.tables; // var push = request.service.push; response.send(statusCodes.OK, { message : "post" }); }; exports.get = function(request, response) { var myTable = request.service.tables.getTable('Account'); myTable.where({ username: request.param('username'), password:request.param('password') }).read({ success: checkPermissions }); function checkPermissions(results) { if (results.length <= 0) { response.send(statusCodes.BAD_REQUEST, 'Not such user.'); } else { response.send(statusCodes.OK, { message : 'sucess.' }); } } };
2、客户端登录代码如下:
private void login(final String name,final String pwd) { List<Pair<String, String>> parameters = new ArrayList<Pair<String, String>>(); parameters.add(new Pair<String, String>("username", name)); parameters.add(new Pair<String, String>("password", pwd)); mClient.invokeApi("login","get", parameters, APIResult.class, new ApiOperationCallback<APIResult>() { @Override public void onCompleted(APIResult result, Exception exception, ServiceFilterResponse response) { int code = response.getStatus().getStatusCode(); Log.i(TAG, "StatusCode:"+code); if (exception == null) { showToast("Login sucess!"); } else if (code==400) { showToast("Username or password is wrong, please try again."); }else{ showToast(exception.getMessage()); } } }); }
注:由于是从项目中摘出来的,一些方法需要自己去初始化。
二、注册
方法一:api方式
1、服务端新建api:register,将其script替换如下:
exports.post = function(request, response) { var myTable = request.service.tables.getTable('Account'); myTable.where({ username: request.param('username') }).read({ success: checkPermissions }); function checkPermissions(results) { if (results.length <= 0) { var toInsert ={username:request.param('username'),password:request.param('password')}; myTable.insert(toInsert, { success: function () { response.send(statusCodes.OK, { message : 'Register sucess!'}); } }); } else { console.log('User %s Already exist.', request.param('username')); response.send(statusCodes.BAD_REQUEST, 'Already exist.'); } } }; exports.get = function(request, response) { response.send(statusCodes.OK, { message : 'Hello World!' }); };
2、客户端代码如下:
//use api private void register1(final String name,final String pwd) { List<Pair<String, String>> parameters = new ArrayList<Pair<String, String>>(); parameters.add(new Pair<String, String>("username", name)); parameters.add(new Pair<String, String>("password", pwd)); mClient.invokeApi("register","post", parameters, APIResult.class, new ApiOperationCallback<APIResult>() { @Override public void onCompleted(APIResult result, Exception exception, ServiceFilterResponse response) { int code = response.getStatus().getStatusCode(); Log.i(TAG, "StatusCode:"+code); if (exception == null) { //Sucess showToast(result.message); } else if (code==400) { showToast(response.getContent()); }else{ showToast(exception.getMessage()); } } }); }
方法二:修改Accout表的insert的script。
1、替换Accout表的insert的script如下:
function insert(item, user, request) { var permissionsTable = tables.getTable('Account'); permissionsTable.where({ username: item.username }).read({ success: checkPermissions }); function checkPermissions(results) { if (results.length <= 0) { request.execute(); } else { console.log('User %s Already exist.', item.username); request.respond(statusCodes.BAD_REQUEST, 'User already exist'); } } }
2、客户端代码如下:
//use insert private void register(final String name,final String pwd) { Account account = new Account(); account.username = name; account.password = pwd; mAccountTable.insert(account,new TableOperationCallback<Account>() { @Override public void onCompleted(Account result, Exception exception, ServiceFilterResponse response) { int code = response.getStatus().getStatusCode(); Log.i(TAG, "StatusCode:"+code); if (exception == null) { //Sucess showToast("Register sucess!"); } else if (code==400) { showToast(response.getContent().replace("\"", "")); }else{ showToast(exception.getMessage()); } } }); }
另,APIResult类如下:
package com.microsoft.ecodrive.model; import com.google.gson.annotations.SerializedName; public class APIResult { @SerializedName("count") public int mCount; public int getCount() { return mCount; } public String message; }
Windows Azure Mobiles Services实现客户端的登录注册
时间: 2024-10-13 08:23:09