LoginActivity.java 10.9 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 com.vhall.business.VhallSDK;
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());
        //查询直播状态
        OkHttpClient okHttpClient = new OkHttpClient();

        Request build = new Request.Builder()
                .url("http://192.168.0.142:8081/api/checkback?thirdId=538670945")
                .build();
        Call call = okHttpClient.newCall(build);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("OWN", "onFailure2: " + e.getMessage());
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                final String respStr = response.body().string();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        if (!respStr.isEmpty()) {
                            //对请求到的直播信息进行json解析
                            JSONObject jsonObject = null;
                            try {
                                jsonObject = new JSONObject(respStr);
                                String body = jsonObject.getString("Body");
                                JSONObject jsonObject1 = new JSONObject(body);
                                //直播频道logo
                                String liveLogo = jsonObject1.getString("homePageImg");
                                Log.d("图片路径", "run: "+liveLogo);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    }
                });

            }
        });


    }


   /* 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);
        } 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("http://192.168.0.142:8081/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
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(respStr);
                    if(jsonObject.getInt("code") != 0){
                        listener.onError(""+jsonObject.getInt("code"),jsonObject.getString("message"));
                    }else {
                        String body = jsonObject.getString("data");
                        JSONObject jsonObject1 = new JSONObject(body);
                        String json = jsonObject1.getString("thirdUserId");
                        listener.onSuccess();
                    }
                    //VhallSdkLogin(json, jsonObject1.getString("pass"));
                } 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(mUserId.getText().toString(), mTextInputUsername.getText().toString(),);

        }
    }

    @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;

    }

}