wpc
2018-11-27 680fbc9e73da3e11988557cf88fd935efd3e0b1e
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
package com.haicaojie.android.ui.mine.weex.usermodule;
 
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.net.Uri;
import android.util.Base64;
import android.widget.Toast;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.haicaojie.android.ui.invite.ShareBrowserActivity;
import com.haicaojie.android.ui.mine.weex.WeexApplicationActivity;
import com.haicaojie.android.ui.recommend.GoodsBrowserActivity;
import com.haicaojie.android.ui.recommend.GoodsDetailBrowerActivity;
import com.haicaojie.android.ui.recommend.SearchActivity;
import com.haicaojie.android.util.Tools;
import com.lcjian.library.util.SingleToast;
import com.lcjian.library.util.security.MD5Utils;
import com.taobao.weex.annotation.JSMethod;
import com.taobao.weex.common.WXModule;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
 
import org.json.JSONException;
import org.json.JSONObject;
 
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
 
/**
 * Created by weikou2015 on 2018/6/14.
 */
 
public class UtilModule extends WXModule {
 
    @JSMethod
    public String getSign(String data) {
 
        List<String> list = new ArrayList<>();
        try {
            JSONObject obj = null;
            try {
                obj = new JSONObject(data);
            } catch (JSONException e) {
            }
            Iterator<String> its = obj.keys();
            while (its.hasNext()) {
                String key = its.next();
                String value = obj.optString(key);
                list.add(key + "=" + value);
            }
        } catch (Exception e) {
 
        }
        Collections.sort(list);
        String sign = "";
        for (String str : list) {
            sign += str + "&";
        }
        sign = MD5Utils.getMD532(sign + "buXiNjie2017!");
        return sign;
 
    }
 
    @JSMethod
    public void jumpSearch(String key) {
        Intent intent = new Intent(mWXSDKInstance.getContext(), SearchActivity.class);
        intent.putExtra("key", key);
        mWXSDKInstance.getContext().startActivity(intent);
    }
 
    @JSMethod
    public void jumpGoodsSplash(long id) {
        Intent intent = new Intent(mWXSDKInstance.getContext(), GoodsBrowserActivity.class);
        intent.putExtra("id", id + "");
        mWXSDKInstance.getContext().startActivity(intent);
    }
 
    @JSMethod
    public void jumpGoodsDetail(long id) {
        Intent intent = new Intent(mWXSDKInstance.getContext(), GoodsDetailBrowerActivity.class);
        intent.putExtra("id", id);
        intent.putExtra("usWeex", true);
        mWXSDKInstance.getContext().startActivity(intent);
    }
 
    @JSMethod
    public void jumpWeb(String url) {
        Intent intent = new Intent(mWXSDKInstance.getContext(), ShareBrowserActivity.class);
        intent.putExtra("url", url);
        mWXSDKInstance.getContext().startActivity(intent);
    }
 
    @JSMethod
    public void share(String type, String contentJsonBase64) {
        byte raw[] = Base64.decode(contentJsonBase64, Base64.DEFAULT);
        JSONObject object = null;
        try {
            object = new JSONObject(new String(raw));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Gson gson = new GsonBuilder().serializeNulls().create();
        imgs = gson.fromJson(object.optJSONArray("imgs").toString(), new TypeToken<List<String>>() {
        }.getType());
        link = object.optString("link");
        description = object.optString("description");
        title = object.optString("title");
        if (type.equalsIgnoreCase("wx")) {
            shareImgs(0);
        } else if (type.equalsIgnoreCase("wxcircle")) {
            shareImgs(1);
        } else if (type.equalsIgnoreCase("qq")) {
            shareImgs(2);
        } else if (type.equalsIgnoreCase("qqzone")) {
//                        UMWeb web = new UMWeb(link);
//                        String des = object.optString("description");
//                        web.setDescription(des);
//                        web.setTitle(title);
//                        UMImage shareImage = new UMImage(ShareBrowserActivity.this, imgs.get(0));//网络图片
//                        web.setThumb(shareImage);
//                        new ShareAction(ShareBrowserActivity.this).withText(des).withMedia(web)
//                                .setPlatform(SHARE_MEDIA.QZONE).setCallback(shareListener).share();
            UMImage shareImage = new UMImage(mWXSDKInstance.getContext(), imgs.get(0));//网络图片
            if (getActivity() != null) {
                new ShareAction(getActivity()).withMedia(shareImage)
                        .setPlatform(SHARE_MEDIA.QZONE).setCallback(shareListener).share();
            } else {
                SingleToast.showToast(mWXSDKInstance.getContext(), "当前不能进行QQ空间分享");
            }
 
        } else if (type.equalsIgnoreCase("sina")) {
 
//                        Log.i("mResult", "title:" + title + "des:" + des + "图片:" + imgs.get(0));
//                        UMImage shareImage = new UMImage(ShareBrowserActivity.this, imgs.get(0));
//                        String des = object.optString("description");
//                        new ShareAction(ShareBrowserActivity.this).withText(des).withMedia(shareImage)
//                                .setPlatform(SHARE_MEDIA.SINA).setCallback(shareListener).share();
            shareImgs(3);
        }
    }
 
    /**
     * 多张图片分享 platform--0 微信好友
     * platform--1 朋友圈
     * platform--2 QQ
     *
     * @param platfrom
     */
    private List<String> imgs = new ArrayList<>();
    private List<File> files = new ArrayList<>();
    String title = "";
    String description = "";
    String link = "";
 
    private void shareImgs(final int platfrom) {
        int avilibleValue = 0;
        if (platfrom == 0 || platfrom == 1) {
            avilibleValue = Tools.isWeixinAvilible(mWXSDKInstance.getContext());
        } else if (platfrom == 2) {
            avilibleValue = Tools.isQQAvilible(mWXSDKInstance.getContext());
        } else if (platfrom == 3) {
            avilibleValue = Tools.isSinaAvilible(mWXSDKInstance.getContext());
        }
        if ((platfrom == 0 || platfrom == 1) && (avilibleValue == 1 || avilibleValue == 2)) {
            if (avilibleValue == 2)
                Toast.makeText(mWXSDKInstance.getContext(), "您还没有安装微信", Toast.LENGTH_SHORT).show();
        } else if (platfrom == 2 && (avilibleValue == 1 || avilibleValue == 2)) {
            if (avilibleValue == 2)
                Toast.makeText(mWXSDKInstance.getContext(), "您还没有安装QQ", Toast.LENGTH_SHORT).show();
        } else if (platfrom == 3 && (avilibleValue == 1 || avilibleValue == 2)) {
            if (avilibleValue == 2)
                Toast.makeText(mWXSDKInstance.getContext(), "您还没有安装微博", Toast.LENGTH_SHORT).show();
        } else {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    files.clear();
                    for (int i = 0; i < imgs.size(); i++) {
                        File file = Tools.saveImageToSdCard(mWXSDKInstance.getContext(), imgs.get(i));
                        files.add(file);
                    }
                    Intent intent = new Intent();
                    ComponentName comp;
                    if (platfrom == 0) {
                        comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
                    } else if (platfrom == 1) {
                        comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
                        intent.putExtra("Kdescription", title);
                    } else {
                        comp = new ComponentName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
                    }
                    intent.setAction(Intent.ACTION_SEND_MULTIPLE);
                    intent.setType("image/*");
 
                    ArrayList<Uri> imageUris = new ArrayList<Uri>();
                    for (File f : files) {
                        imageUris.add(Uri.fromFile(f));
                    }
                    intent.setComponent(comp);
                    intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
                    mWXSDKInstance.getContext().startActivity(intent);
                }
            }).start();
        }
 
    }
 
    UMShareListener shareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {
 
        }
 
        @Override
        public void onResult(SHARE_MEDIA share_media) {
            Toast.makeText(mWXSDKInstance.getContext(), "分享成功!", Toast.LENGTH_LONG).show();
        }
 
        @Override
        public void onError(SHARE_MEDIA share_media, final Throwable throwable) {
            SingleToast.showToast(mWXSDKInstance.getContext(), "分享出错!");
 
        }
 
        @Override
        public void onCancel(SHARE_MEDIA share_media) {
            Toast.makeText(mWXSDKInstance.getContext(), "分享取消!", Toast.LENGTH_LONG).show();
        }
    };
 
    @JSMethod
    public void finishPage() {
        if (getActivity() != null) {
            getActivity().finish();
        } else {
            SingleToast.showToast(mWXSDKInstance.getContext(), "请按返回键退初出当前页面");
        }
    }
 
    private Activity getActivity() {
        Context context = mWXSDKInstance.getContext();
        while (!(context instanceof Activity) && context instanceof ContextWrapper) {
            context = ((ContextWrapper) context).getBaseContext();
        }
 
        if (context instanceof Activity) {
            return (Activity) context;
        }
        return null;
    }
}