LoginActivity.java 10.4 KB
package com.vhall.guangfa;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.vhall.business.VhallSDK;
import com.vhall.business.data.UserInfo;
import com.vhall.business.data.source.UserInfoDataSource;
import com.vhall.uilibs.Param;
import com.vhall.uilibs.util.VhallUtil;
import com.vhall.uilibs.watch.EncrypedThirdUserIdInfo;
import com.vhall.uilibs.watch.OnVhallLoginResuleListener;
import com.vhall.uilibs.watch.VWatchActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LoginActivity extends Activity  {

    private static final String TAG = "LoginActivity";
    private EditText mTextInputUsername;
    private EditText mTextInputPassword;
    private EditText mWatchId;
    public AlertDialog alertDialog;
    private TextView mVerTextView;
    private Param param;
    private EditText mUserId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);
        mTextInputUsername = this.findViewById(R.id.text_input_username);
        mTextInputPassword = this.findViewById(R.id.text_input_password);
        mUserId = this.findViewById(R.id.userId);
        mWatchId = this.findViewById(R.id.watchId);
        mVerTextView = this.findViewById(R.id.label_ver);

        mVerTextView.setText(VhallSDK.getVersion());

    }


   /* private void createUser() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("third_user_id", mUserId.getText().toString());
        hashMap.put("pass", mTextInputPassword.getText().toString());
        hashMap.put("name", mTextInputUsername.getText().toString());
        hashMap.put("account", "s58620696");
        hashMap.put("password", "123456");
        hashMap.put("auth_type", "1");
        com.vhall.guangfa.http.VHAPI.post("http://e.vhall.com/api/vhallapi/v2/user/register", hashMap, (new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("login_", "onFailure: " + e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Log.d(TAG, "run: "+response.body().string());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        mTextInputUsername.setText(mUserId.getText().toString());//一开始输入的userid
                        login(mUserId.getText().toString(), mTextInputUsername.getText().toString(), mTextInputPassword.getText().toString(), mWatchId.getText().toString());
                    }
                });

            }
        }));
    }*/


   //登录
    public void VhallLogin(EncrypedThirdUserIdInfo encrypedThirdUserIdInfo, String username, OnVhallLoginResuleListener listener) {
        //开启线程来发起网络请求
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        JSONObject json = new JSONObject();
        try {
            json.put("iv", encrypedThirdUserIdInfo.getIv());
            json.put("key", encrypedThirdUserIdInfo.getKey());
            json.put("cipher", encrypedThirdUserIdInfo.getCipher());
            json.put("name", username);
            /*json.put("iv", "30818a02210086be9b0de4546ddd5faa4774cf8eafdc11f5108858497f018b86921301a14f6d022100eaf14deb353a5e3267f63893cda96676070a62dd461799525e36f0494e8fc8c9042003b8129a43cb1c00a63f708bef9dda2da828df6e4e9f604aa4e4c2b9707e796804208460474f52833b3022cd16dc579ed40396493b3888c53721b543cc470ea5f60e");
            json.put("key", "308188022019056faae3f9f257c2a1df778900d230d73590a6a1387528fb51a06efbeef34002201098a27cbf4362806c4f3d809a865c08a046bd206882f7ba61c866ee469afae2042005202d8ed208e48c43ef831917cf9f92d57e3d67e6cfb00c25ab2a53fa2e188d04207d7c263c168f477e045d467c7990376cf13151b901ceacebbdb19a8761af0fd9");
            json.put("cipher", "7753277346061b7e84ecfb8fec3fb122b5cd210e43a431cc4c1e45e0c5e2e6b8162f1540525b616f85fde4792ef3f10094c4f368e68816ca0b136786604f1928945900b030a4f35fbd2dffc0948d848d4dad1ed210b78cfe49844a2c06b91d11ebc2d96a0a9bba1ec30ee5fa60560b7e59ba87f1f41cf253bfa80bbbb3155e7c34ddd5503d2bb92a697269fb66ca67b2150fc60ab2d92dc82d6ac454e583875eca92eb45b99c41e8c2a6959c5855b17e");
            json.put("name", "username");*/
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //1 . 拿到OkHttpClient对象
        OkHttpClient client = new OkHttpClient();
        //创建一个RequestBody(参数1:数据类型 参数2传递的json串)
        RequestBody requestBody = RequestBody.create(JSON, String.valueOf(json));
        //3 . 构建Request,将FormBody作为Post方法的参数传入
        Request request = new Request.Builder()
                .url("https://guangfaapi.vhall.com/api/user/login")
                .post(requestBody)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG, "onFailure: "+e.getMessage());
                listener.onError("500",e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String respStr = response.body().string();
                //解析json//body
                try {
                    JSONObject jsonObject = new JSONObject(respStr);
                    if(Integer.parseInt(jsonObject.getString("code")) == 500){
                        listener.onError(""+jsonObject.getInt("code"),jsonObject.getString("message"));
                    }else {
                        String body = jsonObject.getString("data");
                        Log.d(TAG, "打印" + body);
                        JSONObject jsonObject1 = new JSONObject(body);
                        String json = jsonObject1.getString("thirdUserId");
                        VhallSdkLogin(json, jsonObject1.getString("pass"));
                        listener.onSuccess();
                    }

                } catch (Exception e) {
                    e.printStackTrace();

                }

            }
        });

    }

    private void VhallSdkLogin(String username, String userpass) {
        VhallSDK.login(username, userpass, new UserInfoDataSource.UserInfoCallback() {
            @Override
            public void onSuccess(UserInfo userInfo) {
                Toast.makeText(LoginActivity.this, R.string.login_success, Toast.LENGTH_SHORT).show();
                VhallApplication.param.key = userpass;
                //todo
                param = new Param();
                param.watchId = mWatchId.getText().toString();
                VhallApplication.setParam(param);
                VhallApplication.getParam(mWatchId.getText().toString());
                skipMain();
            }

            @Override
            public void onError(int errorCode, String reason) {
                Toast.makeText(LoginActivity.this, reason, Toast.LENGTH_SHORT).show();
            }
        });
    }


    public void loginClick(View view) {
        checkUserInfo();
    }

    public void registClick(View view) {
        if (alertDialog == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage(getString(R.string.regist_phone_number, getString(R.string.phone_number)));
            builder.setPositiveButton(R.string.call, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    Uri uri = Uri.parse(getString(R.string.phone_number_uri, getString(R.string.phone_number)));
                    Intent intent = new Intent(Intent.ACTION_DIAL, uri);
                    startActivity(intent);
                }
            });
            builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            alertDialog = builder.create();
        }
        alertDialog.show();
    }

    public void customerClick(View view) {
        skipMain();
    }

    private void skipMain() {
        Intent intent = new Intent(this, VWatchActivity.class);
        intent.putExtra("param", param);
        intent.putExtra("liveback",mWatchId.getText().toString());
        intent.putExtra("type", VhallUtil.WATCH_LIVE);
        startActivity(intent);
    }

    public void checkUserInfo() {
        String username = mTextInputUsername.getText().toString();
        String password = mTextInputPassword.getText().toString();
        String userId = mUserId.getText().toString();
        if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password) && !TextUtils.isEmpty(userId)) {
           // createUser();//点击登录先调用注册接口
          //  VhallLogin(null,"",null);

        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            if (checkDoubleClick()) {
                return true;
            }
        }
        return super.dispatchTouchEvent(ev);
    }


    /**
     * 判断是否是快速点击
     */
    private static long lastClickTime;

    public static boolean checkDoubleClick() {
        //点击时间
        long clickTime = SystemClock.uptimeMillis();
        //如果当前点击间隔小于500毫秒
        if (lastClickTime >= clickTime - 500) {
            return true;
        }
        //记录上次点击时间
        lastClickTime = clickTime;
        return false;

    }

}