admin
2021-05-13 4834f6ac27e483232eb0f6dae0ead378f8ea0f1a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
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;
        }
    }
}