添加修改用户头像,昵称

This commit is contained in:
huangzhuanghua
2016-09-18 11:57:01 +08:00
parent a09c8e329a
commit a80c577f1f
11 changed files with 411 additions and 75 deletions

View File

@ -3,8 +3,18 @@ package com.gh.common.util;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.gh.base.AppController;
import com.gh.common.constant.Config;
import com.gh.gamecenter.volley.extended.JsonObjectExtendedRequest;
import com.tencent.stat.StatConfig;
import org.json.JSONException;
import org.json.JSONObject;
@ -20,6 +30,58 @@ import java.util.Map;
public class TokenUtils {
// 注册设备
public static synchronized void register(final Context context) {
HashMap<String, String> params = new HashMap<>();
String android_id = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
if (!TextUtils.isEmpty(android_id)) {
params.put("ANDROID_ID", android_id);
}
String imei = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
if (!TextUtils.isEmpty(imei)) {
params.put("IMEI", imei);
}
WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
String mac = wm.getConnectionInfo().getMacAddress();
if (!TextUtils.isEmpty(mac)) {
params.put("MAC", mac);
}
String mid = StatConfig.getMid(context);
if (!TextUtils.isEmpty(mid) || !"0".equals(mid)) {
params.put("MTA_ID", mid);
}
JsonObjectExtendedRequest request = new JsonObjectExtendedRequest(Request.Method.POST,
"http://user.ghzhushou.com/v1d0/device/register", new JSONObject(params).toString(),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Utils.log("onResponse = " + response);
try {
// 保存device_id
saveDeviceId(context, response.getString("device_id"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Utils.log("onErrorResponse = " + error);
}
});
AppController.addToRequestQueue(request, TokenUtils.class);
}
// 保存用户设备id
public static synchronized void saveDeviceId(Context context, String device_id) {
}
// 获取用户设备id
public static synchronized String getDeviceId(Context context) {
return "57ddf9348a3200304f441112";
}
// 获取用户token
public static synchronized String getToken(Context context) {
SharedPreferences sp = context.getSharedPreferences(Config.PREFERENCE,
@ -30,40 +92,18 @@ public class TokenUtils {
long time = System.currentTimeMillis();
// 判断token是否过期
if (time < expire) {
// token未过期
return token;
}
}
// Map<String, String> params = DeviceUtils.getDeviceParams(context);
Map<String, String> params = new HashMap<String, String>();
String url = Config.HOST + "v1d45/token/visit";
//判断是否已登录
if (sp.getBoolean("isLogin", false)) {
// 已登录,获取用户名和密码自动登录
String username = sp.getString("username", null);
String password = sp.getString("password", null);
if (username == null || password == null) {
url = Config.HOST + "v2/token/user?time=" + System.currentTimeMillis();
params.put("username", username);
try {
String s = RSEUtils.encryptByPublic(password + username + System.currentTimeMillis() / 1000);
Utils.log("加密 = " + s);
params.put("password", s);
} catch (Exception e) {
e.printStackTrace();
}
} else {
url = Config.HOST + "v2/token/mobile?time=" + System.currentTimeMillis();
String mobile = sp.getString("mobile", null);
params.put("mobile_number", mobile);
}
}
// 重新获取token
String url = "http://user.ghzhushou.com/v1d0/login";
Map<String, String> params = new HashMap<>();
params.put("device_id", getDeviceId(context));
try {
JSONObject body = new JSONObject(params);
HttpURLConnection connection = (HttpURLConnection) new URL(url)
.openConnection();
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setConnectTimeout(5000);
@ -90,12 +130,15 @@ public class TokenUtils {
}
reader.close();
try {
JSONObject jsonObject = new JSONObject(builder.toString());
Editor editor = sp.edit();
editor.putString("token", jsonObject.getString("token"));
JSONObject jsonObject = new JSONObject(builder.toString());
editor.putString("user_name", jsonObject.getString("name"));
editor.putString("user_icon", jsonObject.getString("icon"));
jsonObject = jsonObject.getJSONObject("token");
editor.putString("token", jsonObject.getString("value"));
editor.putLong("token_expire", jsonObject.getLong("expire"));
editor.apply();
return jsonObject.getString("token");
return jsonObject.getString("value");
} catch (JSONException e) {
e.printStackTrace();
}
@ -103,7 +146,6 @@ public class TokenUtils {
} catch (IOException e) {
e.printStackTrace();
}
return null;
}