package com.tejia.lijin.app.ui.mine;
|
|
import android.content.Context;
|
import android.content.Intent;
|
import android.content.SharedPreferences;
|
import android.database.Cursor;
|
import android.database.sqlite.SQLiteDatabase;
|
import android.graphics.Color;
|
import android.os.Build;
|
import android.os.Bundle;
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
import androidx.recyclerview.widget.GridLayoutManager;
|
import androidx.recyclerview.widget.RecyclerView;
|
import android.util.Log;
|
import android.util.TypedValue;
|
import android.view.Gravity;
|
import android.view.LayoutInflater;
|
import android.view.View;
|
import android.view.ViewGroup;
|
import android.view.Window;
|
import android.view.WindowManager;
|
import android.widget.EditText;
|
import android.widget.ImageView;
|
import android.widget.LinearLayout;
|
import android.widget.TextView;
|
|
import com.google.gson.Gson;
|
import com.google.gson.GsonBuilder;
|
import com.google.gson.reflect.TypeToken;
|
import com.wpc.library.recyclerviewhelper.DividerItemDecoration;
|
import com.wpc.library.util.common.DimenUtils;
|
import com.wpc.library.util.common.StringUtils;
|
import com.tejia.lijin.app.BasicTextHttpResponseHandler;
|
import com.tejia.lijin.app.R;
|
import com.tejia.lijin.app.ShoppingApi;
|
import com.tejia.lijin.app.entity.CustomService;
|
import com.tejia.lijin.app.sqlite.CustomServiceSQLiteOpenHelper;
|
import com.tejia.lijin.app.ui.BaseActivity;
|
import com.tejia.lijin.app.ui.invite.ShareBrowserActivity;
|
|
import org.apache.http.Header;
|
import org.json.JSONObject;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
/**
|
* 联系客服
|
* Created by weikou2015 on 2018/1/10.
|
*/
|
|
public class ContactUsActivity extends BaseActivity implements View.OnClickListener {
|
|
TextView tv_left, tv_middle, tv_category_search;
|
ImageView iv_top_bar_right;
|
SwipeRefreshLayout srl_contact_us;
|
RecyclerView rv_contact_us;
|
LinearLayout ll_recommend;
|
private List<CustomService> mlist = new ArrayList<>();
|
SQLiteDatabase db;
|
private EditText et_content;
|
CustomServiceAdapter adapter;
|
private String TYPE = "kefu";
|
private TextView tv_notice_content;
|
private LinearLayout ll_notice;
|
private ImageView iv_close;
|
LinearLayout ll_content;
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
setContentView(R.layout.activity_contact_us);
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
Window window = getWindow();
|
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
|
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
|
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
|
window.setStatusBarColor(Color.WHITE);
|
//设置状态栏文字颜色及图标为深色
|
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
|
}
|
initView();
|
initValues();
|
}
|
|
private void initView() {
|
tv_left = findViewById(R.id.tv_top_bar_left);
|
tv_middle = findViewById(R.id.tv_top_bar_middle);
|
et_content = findViewById(R.id.et_content);
|
srl_contact_us = findViewById(R.id.srl_contact_us);
|
rv_contact_us = findViewById(R.id.rv_contact_us);
|
ll_recommend = findViewById(R.id.ll_recommend);
|
iv_top_bar_right = findViewById(R.id.iv_top_bar_right);
|
tv_category_search = findViewById(R.id.tv_category_search);
|
ll_content = findViewById(R.id.ll_content);
|
rv_contact_us.setLayoutManager(new GridLayoutManager(this, 1));
|
DividerItemDecoration hDivider = new DividerItemDecoration(DividerItemDecoration.HORIZONTAL);
|
hDivider.setColor(getResources().getColor(R.color.register_gray));
|
hDivider.setSize(DimenUtils.dip2px(this, 2));
|
rv_contact_us.addItemDecoration(hDivider);
|
tv_notice_content = findViewById(R.id.tv_notice_content);
|
ll_notice = findViewById(R.id.ll_notice);
|
iv_close = findViewById(R.id.iv_close);
|
ll_content.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
|
@Override
|
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
|
if (oldBottom != -1 && oldBottom > bottom) {
|
rv_contact_us.requestLayout();
|
rv_contact_us.post(new Runnable() {
|
@Override
|
public void run() {
|
rv_contact_us.getLayoutManager().scrollToPosition(adapter.getItemCount() - 1);
|
}
|
});
|
}
|
}
|
});
|
getAppPageNotification();
|
}
|
|
private int page = 1;
|
|
private void initValues() {
|
iv_top_bar_right.setVisibility(View.VISIBLE);
|
iv_top_bar_right.setOnClickListener(this);
|
tv_left.setOnClickListener(this);
|
tv_category_search.setOnClickListener(this);
|
tv_middle.setText("联系客服");
|
CustomServiceSQLiteOpenHelper openHelper = new CustomServiceSQLiteOpenHelper(this);
|
db = openHelper.getWritableDatabase();
|
getServiceData();
|
adapter = new CustomServiceAdapter(this, mlist, rv_contact_us);
|
View bottom = LayoutInflater.from(this).inflate(R.layout.item_recyclerview_bottom, null);
|
adapter.addFooterView(bottom);
|
bottom.setVisibility(View.GONE);
|
rv_contact_us.setAdapter(adapter);
|
rv_contact_us.getLayoutManager().scrollToPosition(mlist.size());
|
srl_contact_us.setColorSchemeColors(getResources().getColor(R.color.theme));
|
srl_contact_us.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
@Override
|
public void onRefresh() {
|
page++;
|
getServiceData();
|
adapter.notifyDataSetChanged();
|
srl_contact_us.setRefreshing(false);
|
}
|
});
|
getKeys();
|
}
|
|
private void getAppPageNotification() {
|
ShoppingApi.getAppPageNotification(this, TYPE, new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
if (jsonObject.optInt("code") == 0) {
|
boolean isClose = jsonObject.optJSONObject("data").optBoolean("canClose");
|
String content = jsonObject.optJSONObject("data").optString("content");
|
final String contentUrl = jsonObject.optJSONObject("data").optString("contentUrl");
|
final String md5 = jsonObject.optJSONObject("data").optString("md5");
|
|
tv_notice_content.setText(content);
|
final SharedPreferences sp = getSharedPreferences("notice", Context.MODE_PRIVATE);
|
if (!sp.getBoolean(md5, false)) {//false 展示,true不展示
|
ll_notice.setVisibility(View.VISIBLE);
|
} else {
|
ll_notice.setVisibility(View.GONE);
|
}
|
|
if (isClose) {
|
iv_close.setImageDrawable(getResources().getDrawable(R.drawable.ic_notice_close));
|
iv_close.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
ll_notice.setVisibility(View.GONE);
|
SharedPreferences.Editor editor = sp.edit();
|
editor.putBoolean(md5, true);
|
editor.commit();
|
}
|
});
|
} else {
|
iv_close.setImageDrawable(getResources().getDrawable(R.drawable.ic_notice_input));
|
}
|
ll_notice.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
if (StringUtils.isEmpty(contentUrl))
|
return;
|
Intent intent = new Intent(ContactUsActivity.this, ShareBrowserActivity.class);
|
intent.putExtra("url", contentUrl);
|
startActivity(intent);
|
}
|
});
|
} else {
|
ll_notice.setVisibility(View.GONE);
|
}
|
}
|
});
|
}
|
|
private void getKeys() {
|
ShoppingApi.getKeys(this, new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
if (jsonObject.optInt("code") == 0) {
|
ll_recommend.removeAllViews();
|
Gson gson = new GsonBuilder().serializeNulls().create();
|
final List<String> list = gson.fromJson(
|
jsonObject.optJSONObject("data").optJSONArray("keys").toString(),
|
new TypeToken<List<String>>() {
|
}.getType());
|
|
for (int i = 0; i < list.size(); i++) {
|
TextView tv = new TextView(ContactUsActivity.this);
|
tv.setBackgroundResource(R.drawable.shape_custom_service_recommend);
|
tv.setPadding(DimenUtils.dip2px(ContactUsActivity.this, 10f), DimenUtils.dip2px(ContactUsActivity.this, 4f), DimenUtils.dip2px(ContactUsActivity.this, 10f), DimenUtils.dip2px(ContactUsActivity.this, 5f));
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
params.leftMargin = DimenUtils.dip2px(ContactUsActivity.this, 10f);
|
params.gravity = Gravity.CENTER;
|
tv.setLayoutParams(params);
|
tv.setGravity(Gravity.CENTER);
|
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
|
tv.setTextColor(getResources().getColor(R.color.main_text_color));
|
final String key = list.get(i);
|
tv.setText(key);
|
tv.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
keyStorage(key);
|
getAnswer(key);
|
}
|
});
|
ll_recommend.addView(tv);
|
}
|
}
|
}
|
});
|
}
|
|
private void getServiceData() {
|
Cursor cursor = db.rawQuery("select * from custom_service order by id DESC limit 20 offset " + (page - 1) * 20, null);
|
while (cursor.moveToNext()) {
|
int id = cursor.getInt(cursor.getColumnIndex("id"));
|
int from = cursor.getInt(cursor.getColumnIndex("stemfrom"));
|
String key = cursor.getString(cursor.getColumnIndex("key"));
|
String content = cursor.getString(cursor.getColumnIndex("content"));
|
String contenType = cursor.getString(cursor.getColumnIndex("contenttype"));
|
String status = cursor.getString(cursor.getColumnIndex("status"));
|
long createTime = cursor.getLong(cursor.getColumnIndex("createtime"));
|
Log.i("mResult", "id:" + id + "from:" + from + " key:" + key + " content:"
|
+ content + " contenType:" + contenType + " status:" + status
|
+ " createTime:" + createTime);
|
CustomService info = new CustomService();
|
info.setId(id);
|
info.setStemFrom(from);
|
info.setKey(key);
|
info.setContent(content);
|
info.setContentType(contenType);
|
info.setStatus(status);
|
info.setCreateTime(createTime);
|
mlist.add(0, info);
|
}
|
cursor.close();
|
if (mlist.size() == 0) {
|
String content = "请问有什么可以帮到您呢?";
|
answerStorage("", content, "text", "1");
|
CustomService info = new CustomService();
|
info.setStemFrom(1);
|
info.setContent(content);
|
info.setContentType("text");
|
info.setStatus("1");
|
info.setCreateTime(System.currentTimeMillis());
|
mlist.add(info);
|
}
|
|
}
|
|
/**
|
* 搜索关键字存储
|
*
|
* @param key
|
*/
|
private void keyStorage(String key) {
|
long cTime = System.currentTimeMillis();
|
db.execSQL("insert into custom_service(stemfrom,'key',createtime) values(?,?,?)", new String[]{"0", key, cTime + ""});
|
CustomService info = new CustomService();
|
info.setStemFrom(0);
|
info.setKey(key);
|
info.setCreateTime(cTime);
|
mlist.add(info);
|
adapter.notifyDataSetChanged();
|
rv_contact_us.getLayoutManager().scrollToPosition(mlist.size());
|
}
|
|
/**
|
* 服务器应答数据存储
|
*
|
* @param key
|
*/
|
private void answerStorage(String key, String content, String contentType, String status) {
|
db.execSQL("insert into custom_service(stemfrom,'key',content,contenttype,status,createtime) values(?,?,?,?,?,?)",
|
new String[]{"1", key, content, contentType, status, System.currentTimeMillis() + ""});
|
}
|
|
private void getAnswer(String key) {
|
ShoppingApi.getAnswer(ContactUsActivity.this, key, new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
if (jsonObject.optInt("code") == 0) {
|
answerStorage(jsonObject.optJSONObject("data").getString("key"), jsonObject.optJSONObject("data").getString("content"),
|
jsonObject.optJSONObject("data").getString("contentType"), "1");
|
CustomService info = new CustomService();
|
info.setStemFrom(1);
|
info.setKey(jsonObject.optJSONObject("data").getString("key"));
|
info.setContent(jsonObject.optJSONObject("data").getString("content"));
|
info.setContentType(jsonObject.optJSONObject("data").getString("contentType"));
|
info.setStatus("1");
|
info.setCreateTime(System.currentTimeMillis());
|
mlist.add(info);
|
adapter.notifyDataSetChanged();
|
rv_contact_us.getLayoutManager().scrollToPosition(mlist.size());
|
}
|
}
|
});
|
}
|
|
@Override
|
public void onClick(View view) {
|
switch (view.getId()) {
|
case R.id.tv_top_bar_left:
|
finish();
|
break;
|
case R.id.tv_category_search:
|
if (et_content.getText() != null && !StringUtils.isEmpty(et_content.getText().toString())) {
|
keyStorage(et_content.getText().toString());
|
getAnswer(et_content.getText().toString());
|
et_content.setText("");
|
}
|
break;
|
case R.id.iv_top_bar_right:
|
SharedPreferences sp = getSharedPreferences("user", Context.MODE_PRIVATE);
|
Intent intent1 = new Intent(ContactUsActivity.this, ShareBrowserActivity.class);
|
intent1.putExtra("title", "帮助中心");
|
intent1.putExtra("url", sp.getString("help", ""));
|
startActivity(intent1);
|
break;
|
}
|
}
|
}
|