admin
2021-06-05 ddff7888bf7e754d12fb5fc85a58f3012f456490
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
package com.tejia.lijin.app.util.ui;
 
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.Toast;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.tejia.lijin.app.ShoppingApplication;
import com.tejia.lijin.app.ui.recommend.GoodsDetailActivity;
import com.tejia.lijin.app.util.user.UserUtil;
import com.wpc.library.util.security.MD5Utils;
import com.tejia.lijin.app.BasicTextHttpResponseHandler;
import com.tejia.lijin.app.ShoppingApi;
import com.tejia.lijin.app.entity.GiftCoupon;
import com.tejia.lijin.app.entity.TaoBaoGoodsBrief;
import com.tejia.lijin.app.entity.recommendinfo.ConvertLinkInfo;
import com.tejia.lijin.app.entity.recommendinfo.RecommendGoodsInfo;
import com.tejia.lijin.app.entity.recommendinfo.RecommendGoodsJumpInfo;
import com.tejia.lijin.app.ui.dialog.ConvertLinkDialog;
import com.tejia.lijin.app.ui.dialog.CopyLinkDialog;
import com.tejia.lijin.app.ui.dialog.CopyTitleDialog;
import com.tejia.lijin.app.ui.recommend.SearchResultActivity;
import com.tejia.lijin.app.util.ApkUtil;
import com.tejia.lijin.app.util.CopyGiftDialogUtils;
import com.tejia.lijin.app.util.DuplicateEventFilterUtil;
import com.tejia.lijin.app.util.ToolUtil;
import com.tejia.lijin.app.util.clipboard.ClipboardUtil;
import com.tejia.lijin.app.util.goods.GoodsDetailJumpUtil;
import com.tejia.lijin.app.util.umengCustomEvent.MainCustomEvent;
 
import org.apache.http.Header;
import org.json.JSONObject;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * 根据粘贴板的内容推荐
 */
public class ClipboardContentRecommendUtil {
 
 
    /**
     * 构建商品推荐弹框
     *
     * @param activity
     * @param info
     * @return
     */
    public static CopyLinkDialog buildCopyLinkDialog(final Activity activity, final RecommendGoodsInfo info) {
        CopyLinkDialog.Builder builder = new CopyLinkDialog.Builder(activity);
        builder.setMessage(info);
        builder.setPositiveButton("", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                MainCustomEvent.rmdTbgoodsRecommendResultClick(activity, "商品");
                Intent intent = new Intent(activity, GoodsDetailActivity.class);
                intent.putExtra("goodsId", info.getGoods().getGoodsId());
                intent.putExtra("goodsType", info.getGoods().getGoodsType());
                intent.putExtra("title", info.getGoods().getTitle());
                intent.putExtra("from", "猜你喜欢");
                activity.startActivity(intent);
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        return builder.create();
    }
 
 
    /**
     * 猜你喜欢弹窗
     */
    private static void isCopyLink(RecommendGoodsInfo info, Activity context) {
        dialog = ClipboardContentRecommendUtil.buildCopyLinkDialog(context, info);
        dialog.show();
    }
 
    /**
     * 10 // 类型:10-免单券   11-奖励券   12-红包
     * 弹窗
     *
     * @param giftCoupon
     */
    private static void isGiftCoupon(final GiftCoupon giftCoupon, String type, Activity context) {
        CopyGiftDialogUtils utils = new CopyGiftDialogUtils(context);
        dialog = new Dialog(context);
        utils.showCopyGiftDialog(giftCoupon, UserUtil.getUid(ShoppingApplication.application), dialog, type);
    }
 
 
    private static void addSearchHistory(String content, int page, Context context) {
        List<String> mList = new ArrayList<>();
        SharedPreferences sp = null;
        if (page == 1) {
            sp = context.getSharedPreferences("searchHistory", Context.MODE_PRIVATE);
        } else if (page == 2) {
            sp = context.getSharedPreferences("searchHistory_jd", Context.MODE_PRIVATE);
        } else if (page == 3) {
            sp = context.getSharedPreferences("searchHistory_pdd", Context.MODE_PRIVATE);
        } else if (page == 4) {
            sp = context.getSharedPreferences("searchHistory_vip", Context.MODE_PRIVATE);
        } else if (page == 5) {
            sp = context.getSharedPreferences("searchHistory_suning", Context.MODE_PRIVATE);
        } else {
            sp = context.getSharedPreferences("searchHistory", Context.MODE_PRIVATE);
        }
        int formNums = sp.getInt("searchNums", 0);
        for (int i = 0; i < formNums; i++) {
            String formItem = sp.getString("searchItem_" + (formNums - 1 - i), null);
            mList.add(formItem);
        }
        SharedPreferences.Editor editor = sp.edit();
        if (mList.contains(content)) {
            mList.remove(content);
        } else {
            if (formNums >= 10) {
                mList.remove(0);
            }
        }
        mList.add(content);
        editor.putInt("searchNums", mList.size());
        for (int i = 0; i < mList.size(); i++) {
            editor.putString("searchItem_" + i, mList.get(mList.size() - 1 - i));
        }
        editor.commit();
    }
 
    /**
     * 智能搜索弹窗
     *
     * @param title
     */
    private static void isCopyTitle(final String title, final Activity context) {
        CopyTitleDialog.Builder builder = new CopyTitleDialog.Builder(context);
        builder.setMessage(title);
        builder.setPositiveButton("", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ClipboardUtil.emptyClipboard(context);
                MainCustomEvent.rmdTbgoodsRecommendResultClick(context, "标题");
                addSearchHistory(title, which, context);//淘宝//京东//拼多多
 
                Intent intent = new Intent(context, SearchResultActivity.class);
                intent.putExtra("content", title);
                intent.putExtra("type", "normal");
                intent.putExtra("page", (which - 1));
                intent.putExtra("from", "recommendSearch");
                context.startActivity(intent);
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ClipboardUtil.emptyClipboard(context);
                dialog.dismiss();
            }
        });
        dialog = builder.create();
        dialog.show();
    }
 
    private static Dialog dialog = null;
 
    public static void getRecommendInfo(String description, final Activity context) {
        final String key = "clipboardRecommend-" + MD5Utils.getMD532(description);
        if (!DuplicateEventFilterUtil.allowEvent(key, 2000))
            return;
        if (dialog != null && dialog.isShowing())
            return;
        ShoppingApi.getNewGoodsInfo(context, description, UserUtil.getUid(ShoppingApplication.application), new BasicTextHttpResponseHandler() {
            @Override
            public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                //防止重复弹框
                if (jsonObject.optInt("code") == 0) {
                    JSONObject data = jsonObject.getJSONObject("data");
                    String type = data.optString("type");
                    MainCustomEvent.rmdTbgoodsRecommendResult(context, type.equalsIgnoreCase("1") ? "商品" : "标题");
 
                    if (type.equalsIgnoreCase("3")) {//猜你喜欢
                        Gson gson = new GsonBuilder().serializeNulls().create();
                        TaoBaoGoodsBrief taoBaoGoodsBrief = gson.fromJson(
                                data.optJSONObject("goods").toString(), new TypeToken<TaoBaoGoodsBrief>() {
                                }.getType());
                        JSONObject left = data.optJSONObject("left");
                        JSONObject right = data.optJSONObject("right");
                        RecommendGoodsJumpInfo leftJump = null;
                        RecommendGoodsJumpInfo rightJump = null;
                        if (left != null) {
                            leftJump = gson.fromJson(left.toString(), RecommendGoodsJumpInfo.class);
                        }
                        if (right != null) {
                            rightJump = gson.fromJson(right.toString(), RecommendGoodsJumpInfo.class);
                        }
                        isCopyLink(new RecommendGoodsInfo(taoBaoGoodsBrief, leftJump, rightJump), context);
                    } else if (type.equalsIgnoreCase("4")) {/**未加入淘宝联盟的情况(智能搜索弹窗-猜你喜欢)*/
                        Gson gson = new GsonBuilder().serializeNulls().create();
                        TaoBaoGoodsBrief taoBaoGoodsBrief = gson.fromJson(
                                data.optJSONObject("goods").toString(), new TypeToken<TaoBaoGoodsBrief>() {
                                }.getType());
                        JSONObject left = data.optJSONObject("left");
                        JSONObject right = data.optJSONObject("right");
                        RecommendGoodsJumpInfo leftJump = null;
                        RecommendGoodsJumpInfo rightJump = null;
                        if (left != null) {
                            leftJump = gson.fromJson(left.toString(), RecommendGoodsJumpInfo.class);
                        }
                        if (right != null) {
                            rightJump = gson.fromJson(right.toString(), RecommendGoodsJumpInfo.class);
                        }
                        dialog = ToolUtil.isNotTBAlliance(new RecommendGoodsInfo(taoBaoGoodsBrief, leftJump, rightJump), type, context);
                    } else if (type.equalsIgnoreCase("2")) {// 智能搜索弹窗
                        isCopyTitle(data.optJSONObject("data").optString("title"), context);
                    } else if (type.equalsIgnoreCase("10") ||
                            type.equalsIgnoreCase("11") ||
                            type.equalsIgnoreCase("13")) {// 类型:10 // 类型:10-免单券   11-奖励券   13-红包
                        Gson gson = new GsonBuilder().serializeNulls().create();
                        GiftCoupon giftCoupon = gson.fromJson(jsonObject.optJSONObject("data")
                                .toString(), new TypeToken<GiftCoupon>() {
                        }.getType());
                        isGiftCoupon(giftCoupon, type, context);//弹窗10-免单券   11-奖励券   12-红包
                    } else if (type.equalsIgnoreCase("20")) {
                        data = jsonObject.optJSONObject("data").optJSONObject("data");
                        ConvertLinkInfo convertLinkInfo = new Gson().fromJson(data.toString(), ConvertLinkInfo.class);
                        dialog = new ConvertLinkDialog.Builder(context).setConvertLinkInfo(convertLinkInfo).create();
                        dialog.show();
                    }
                } else if (jsonObject.optInt("code") == 1001) {// 用户未登录提示
                    Toast.makeText(context, jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
                    ApkUtil.setEmptyClipboardManager(context);
                }
            }
        });
 
 
    }
}