Ejemplo n.º 1
0
        /// <summary>
        /// 3 调起微信支付,(前提:微信已安装,且没有被应用回收,即正常人工可以使用)
        ///   新建一个包wxapi,建一个类名为WXPayEntryActivity作为接受微信的支付结果(回调名必须是:
        ///     你的包名+.wxapi.WXPayEntryActivity),添加如下代码到Mainfast里面就行了 exported = true
        ///      activity android:name=".wxapi.WXPayEntryActivity" android:exported="true" 实现onResp函数
        ///   不过,最终结果以服务器的返回为准notify_url,App等待并轮询结果,接下来就是要为了做安全性设计,
        ///      把一些重要的东西放到服务器生生成,这样是为防止客户端被反编译,建议GenPackageSign/genProductArgs方法在服务端生成
        /// </summary>
        public bool SendPayReq(PayReq pReq)
        {
            bool isOk = false;

            try {
                if (!msgApi.IsWXAppInstalled)
                {
                    Toast.MakeText(this.Context, "您还没有安装微信,暂不支持此功能!", ToastLength.Short).Show();
                }
                else if (!msgApi.IsWXAppSupportAPI)
                {
                    Toast.MakeText(this.Context, "你安装的微信版本不支持当前开放API!", ToastLength.Short).Show();
                }
                else
                {
                    msgApi.RegisterApp(Constants.APPID);
                    msgApi.OpenWXApp();
                    isOk = msgApi.SendReq(pReq);
                }
            }
            catch (Exception ex) {
                Toast.MakeText(this.Context, ex.GetAllMsg(), ToastLength.Short).Show();
                //throw;
            }

            return(isOk);
        }
Ejemplo n.º 2
0
        public async void ShareText()
        {
            var shareScene = ComboBox.SelectionBoxItem as ShareScene;

            if (shareScene == null)
            {
                return;
            }

            try
            {
                var scene   = shareScene.Scene;
                var message = new WXTextMessage
                {
                    Title       = "Sharing a text title!",
                    Text        = "This is text content",
                    Description = "This is a text message.这是一个文本消息。",
                    ThumbData   = null
                };
                SendMessageToWX.Req req = new SendMessageToWX.Req(message, scene);
                IWXAPI api     = WXAPIFactory.CreateWXAPI("[YOUR APP ID]");
                var    isValid = await api.SendReq(req);
            }
            catch (WXException ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
Ejemplo n.º 3
0
        //发送授权请求的处理函数,SDK1.0暂不支持,可忽略
        private void btnAuth_Click(object sender, RoutedEventArgs e)
        {
            string AppID = ConstString.APPID;

            SendAuth.Req req = new SendAuth.Req(ConstString.SCOPE, ConstString.STATE);

            IWXAPI api = WXAPIFactory.CreateWXAPI(AppID);

            api.SendReq(req);
        }
Ejemplo n.º 4
0
 public static void SendReqInternal(SendMessageToWX.Req req)
 {
     try
     {
         mApi.SendReq(req);
     }
     catch (WXException e)
     {
         System.Diagnostics.Debugger.Log(0, "[asWXHelper]", e.ToString());
     }
 }
Ejemplo n.º 5
0
        //网页类型分享
        private void BtnHtml_Click(object sender, EventArgs e)
        {
            WXWebpageObject webObj = new WXWebpageObject();

            webObj.WebpageUrl = "https://www.xamarin.com/";

            WXMediaMessage msg = new WXMediaMessage(webObj);

            msg.Title       = "Xamarin官网";
            msg.Description = "官方网站描述";

            //分享的缩略图
            Bitmap       thumb = BitmapFactory.DecodeResource(this.Resources, Resource.Drawable.Icon);
            MemoryStream ms    = new MemoryStream();

            thumb.Compress(Bitmap.CompressFormat.Png, 0, ms);
            byte[] bytes = ms.ToArray();

            //构造一个Req请求
            SendMessageToWX.Req req = new SendMessageToWX.Req();
            //唯一的请求标志
            req.Transaction = System.Guid.NewGuid().ToString();
            req.Message     = msg;
            req.Scene       = SendMessageToWX.Req.WXSceneTimeline;

            //发送数据
            api.SendReq(req);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 页面分享
        /// </summary>
        /// <param name="text"></param>
        /// <param name="title"></param>
        /// <param name="thumb"></param>
        public async void WebPageShareByUserChooseRequest(string url, string title, byte[] thumb)
        {
            var message = new WXWebpageMessage
            {
                WebpageUrl = url,
                ThumbData  = thumb,
                Title      = title
            };

            SendMessageToWX.Req request = new SendMessageToWX.Req(message, SendMessageToWX.Req.WXSceneChooseByUser);
            IWXAPI api = WXAPIFactory.CreateWXAPI(_appId);
            await api.SendReq(request);
        }
Ejemplo n.º 7
0
 /// <summary>
 /// 授权登录
 /// </summary>
 public static async void Login()
 {
     try
     {
         SendAuth.Req req     = new SendAuth.Req("", "test");
         IWXAPI       api     = WXAPIFactory.CreateWXAPI(APP_ID);
         var          isValid = await api.SendReq(req);
     }
     catch (WXException ex)
     {
         Debug.WriteLine(ex.Message);
     }
 }
Ejemplo n.º 8
0
 /// <summary>
 /// 分享一段文字
 /// </summary>
 /// <param name="shareContent">参数array注释</param>
 /// <param name="isWXSceneTimeline">true:分享到朋友圈;false:分享给好友</param>
 /// <returns>无</returns>
 public void ShareText(string shareContent, bool isWXSceneTimeline)
 {
     if (!TextUtils.IsEmpty(shareContent))
     {
         WXTextObject textObj = new WXTextObject()
         {
             Text = shareContent,
         };
         WXMediaMessage msg = new WXMediaMessage()
         {
             MyMediaObject = textObj,
             Description   = shareContent,
         };
         SendMessageToWX.Req req = new SendMessageToWX.Req()
         {
             Transaction = buildTransaction("text"),
             Message     = msg,
             Scene       = isWXSceneTimeline ? WECHAT_SHARE_TYPE_FRENDS
                 : WECHAT_SHARE_TYPE_TALK,
         };
         mWXApi.SendReq(req);
     }
 }
Ejemplo n.º 9
0
        public async void SendAuth()
        {
            try
            {
                SendAuth.Req req = new SendAuth.Req(AppSettings.Scope, "test");

                IWXAPI api     = WXAPIFactory.CreateWXAPI(AppSettings.AppId);
                var    isValid = await api.SendReq(req);
            }
            catch (WXException ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 文本分享到微信朋友圈
        /// </summary>
        /// <param name="text"></param>
        /// <param name="title"></param>
        /// <param name="thumb"></param>
        public async void TextShare2TimelineRequest(string text, string title, byte[] thumb)
        {
            var message = new WXTextMessage
            {
                Text      = text,
                ThumbData = thumb,
                Title     = title
            };

            SendMessageToWX.Req request = new SendMessageToWX.Req(message, SendMessageToWX.Req.WXSceneTimeline);
            IWXAPI api = WXAPIFactory.CreateWXAPI(_appId);

            await api.SendReq(request);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// 分享网页
        /// </summary>
        /// <param name="title"></param>
        /// <param name="description"></param>
        /// <param name="url"></param>
        /// <param name="thumbnailUri"></param>
        /// <returns></returns>
        public static async Task ShareWebAsync(string title, string description, string url, string thumbnailUri)
        {
            if (!DeviceUtils.IsMobile)
            {
                ToastService.SendToast("PC上不支持微信分享");
                return;
            }

            try
            {
                var scene = SendMessageToWX.Req.WXSceneChooseByUser;

                var message = new WXWebpageMessage
                {
                    WebpageUrl  = url,
                    Title       = title,
                    Description = description
                };

                if (!string.IsNullOrEmpty(thumbnailUri))
                {
                    IBuffer buffer = await HttpBaseService.GetBytesAsync(thumbnailUri);

                    message.ThumbData = WindowsRuntimeBufferExtensions.ToArray(buffer);
                }
                else
                {
                    var thumbnail = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Images/ic_logo.png"));

                    using (var stream = await thumbnail.OpenReadAsync())
                    {
                        byte[] pic = new byte[stream.Size];
                        await stream.AsStream().ReadAsync(pic, 0, pic.Length);

                        message.ThumbData = pic;
                    }
                }

                SendMessageToWX.Req req = new SendMessageToWX.Req(message, scene);
                IWXAPI api = WXAPIFactory.CreateWXAPI(APP_ID);
                await api.SendReq(req);
            }
            catch (WXException ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
Ejemplo n.º 12
0
 public static async Task SendWechatShareToUserChoiceRequestAsync(string url, string title, byte[] thumb, string desc = "", ShareType toTimeLine = ShareType.WechatTimeLine)
 {
     try {
         var message = new WXWebpageMessage {
             WebpageUrl  = url,
             Title       = title,
             Description = desc,
             ThumbData   = thumb
         };
         var requset = new SendMessageToWX.Req(
             message,
             toTimeLine == ShareType.WechatTimeLine ?
             SendMessageToWX.Req.WXSceneTimeline :
             SendMessageToWX.Req.WXSceneSession);
         IWXAPI api = WXAPIFactory.CreateWXAPI("wxdfa382a79b754759");
         var    tes = await api.SendReq(requset);
     } catch (WXException e) { System.Diagnostics.Debug.WriteLine(e.StackTrace); }
 }
Ejemplo n.º 13
0
        /// <summary>
        /// 分享图片
        /// </summary>
        /// <param name="title"></param>
        /// <param name="description"></param>
        /// <param name="imageUrl"></param>
        /// <param name="thumbnail"></param>
        public static async Task ShareImageAsync(string title, string description, string imageUrl, StorageFile thumbnail = null)
        {
            if (!DeviceUtils.IsMobile)
            {
                ToastService.SendToast("PC上不支持微信分享");
                return;
            }

            try
            {
                var scene = SendMessageToWX.Req.WXSceneChooseByUser;

                var message = new WXImageMessage
                {
                    Title       = title,
                    Description = description,
                    ImageUrl    = imageUrl
                };

                if (thumbnail == null)
                {
                    thumbnail = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Images/ic_logo.png"));
                }

                if (thumbnail != null)
                {
                    using (var stream = await thumbnail.OpenReadAsync())
                    {
                        var pic = new byte[stream.Size];
                        await stream.AsStream().ReadAsync(pic, 0, pic.Length);

                        message.ThumbData = pic;
                    }
                }

                SendMessageToWX.Req req = new SendMessageToWX.Req(message, scene);
                IWXAPI api = WXAPIFactory.CreateWXAPI(APP_ID);
                await api.SendReq(req);
            }
            catch (WXException ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
Ejemplo n.º 14
0
 const string APP_ID = "wx167b8355f065339c";//wxc6d5cff9be60265d //--读读日报
 public async void ShareLink(string url, string title, string description, byte[] thumb, int scene = SendMessageToWX.Req.WXSceneChooseByUser)
 {
     try
     {
         var message = new WXWebpageMessage
         {
             WebpageUrl  = url,
             Title       = title,
             Description = description,
             ThumbData   = thumb
         };
         SendMessageToWX.Req req = new SendMessageToWX.Req(message, scene);
         IWXAPI api     = WXAPIFactory.CreateWXAPI(APP_ID);
         var    isValid = await api.SendReq(req);
     }
     catch (WXException)
     {
     }
 }
Ejemplo n.º 15
0
 public async void ShareSong()
 {
     try
     {
         var scene   = SendMessageToWX.Req.WXSceneTimeline;
         var message = new WXTextMessage
         {
             Title       = "Sharing a text title!",
             Text        = "This is text content",
             Description = "This is a text message.这是一个文本消息。",
             ThumbData   = null
         };
         SendMessageToWX.Req req = new SendMessageToWX.Req(message, scene);
         IWXAPI api     = WXAPIFactory.CreateWXAPI("[YOUR APP ID]");
         var    isValid = await api.SendReq(req);
     }
     catch (WXException ex)
     {
         Debug.WriteLine(ex.Message);
     }
 }
Ejemplo n.º 16
0
        //拉起微信支付
        private void btnPay_Click(object sender, RoutedEventArgs e)
        {
            if (mReqDict == null || mReqDict.Count == 0)
            {
                MessageBox.Show("please get prepayid first");
                return;
            }
            string AppID = mReqDict["appid"];
            // Summary:
            //      随机串,防重发
            string NonceStr = mReqDict["noncestr"];
            //
            // Summary:
            //      商家根据财付通文档填写的数据和签名
            string Package = mReqDict["packageforsdk"];
            //
            // Summary:
            //      商家向财付通申请的商家id
            string PartnerId = ConstString.PARTNER_ID;
            //
            // Summary:
            //      预支付订单 预支付订单
            string PrepayId = mReqDict["prepayid"];
            //
            // Summary:
            //      商家根据微信开放平台文档对数据做的签名
            string Sign = mReqDict["signsha1"];
            //
            // Summary:
            //      时间戳,防重发
            uint TimeStamp = uint.Parse(mReqDict["timestamp"]);

            SendPay.Req req = new SendPay.Req(PartnerId, PrepayId, NonceStr, TimeStamp, Package, Sign);
            IWXAPI      api = WXAPIFactory.CreateWXAPI(AppID);

            api.SendReq(req);
        }
Ejemplo n.º 17
0
        public async Task <Boolean> ShareAnswerToWeChatTimeline(String wechatAppId, String title, String description, String pageUrl)
        {
            try
            {
                var scene = SendMessageToWX.Req.WXSceneTimeline;

                var folder = await Package.Current.InstalledLocation.GetFolderAsync("Resource");

                var file = await folder.GetFileAsync("zhihu_m.jpg");

                using (var stream = await file.OpenReadAsync())
                {
                    var pic = new byte[stream.Size];
                    await stream.AsStream().ReadAsync(pic, 0, pic.Length);

                    var message = new WXWebpageMessage
                    {
                        WebpageUrl  = pageUrl,
                        Title       = title,
                        Description = description,
                        ThumbData   = pic,
                    };

                    var    req     = new SendMessageToWX.Req(message, scene);
                    IWXAPI api     = WXAPIFactory.CreateWXAPI(wechatAppId);
                    var    isValid = await api.SendReq(req);

                    return(!String.IsNullOrEmpty(isValid));
                }
            }
            catch (WXException ex)
            {
                await UmengAnalytics.TrackException(ex, "ShareAnswerToWeChatTimeline: " + ex.Message);

                return(false);
            }
        }
Ejemplo n.º 18
0
        public async void ShareImage()
        {
            var shareScene = ComboBox.SelectionBoxItem as ShareScene;

            if (shareScene == null)
            {
                return;
            }
            try
            {
                var scene = shareScene.Scene;
                var file  = await Package.Current.InstalledLocation.GetFileAsync("1.png");

                using (var stream = await file.OpenReadAsync())
                {
                    var pic = new byte[stream.Size];
                    await stream.AsStream().ReadAsync(pic, 0, pic.Length);

                    var message = new WXImageMessage
                    {
                        Title       = "Sharing a picture!",
                        Description = "This is a image message.这是一个图片消息",
                        ThumbData   = pic,
                        ImageUrl    = "http://tp3.sinaimg.cn/1882347990/180/5725518284/1"
                    };

                    SendMessageToWX.Req req = new SendMessageToWX.Req(message, scene);
                    IWXAPI api     = WXAPIFactory.CreateWXAPI("[YOUR APP ID]");
                    var    isValid = await api.SendReq(req);
                }
            }
            catch (WXException ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
Ejemplo n.º 19
0
        public async void ShareLink()
        {
            var shareScene = ComboBox.SelectionBoxItem as ShareScene;

            if (shareScene == null)
            {
                return;
            }

            try
            {
                var scene = shareScene.Scene;
                var file  = await Package.Current.InstalledLocation.GetFileAsync("1.png");

                using (var stream = await file.OpenReadAsync())
                {
                    var pic = new byte[stream.Size];
                    await stream.AsStream().ReadAsync(pic, 0, pic.Length);

                    var message = new WXWebpageMessage
                    {
                        WebpageUrl  = "http://www.baidu.com",
                        Title       = "Sharing a link!",
                        Description = "This is a link message.这是一个链接消息",
                        ThumbData   = pic
                    };
                    SendMessageToWX.Req req = new SendMessageToWX.Req(message, scene);
                    IWXAPI api     = WXAPIFactory.CreateWXAPI("[YOUR APP ID]");
                    var    isValid = await api.SendReq(req);
                }
            }
            catch (WXException ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
Ejemplo n.º 20
0
        public void share(string argsJSON)
        {
            var args    = JsonHelper.Deserialize <string[]>(argsJSON);
            var options = JsonHelper.Deserialize <WeChatShareOptions>(args[0]);

            if (options == null)
            {
                dispatchResult(PluginResult.Status.JSON_EXCEPTION, ERR_INVALID_OPTIONS);
                return;
            }

            WXBaseMessage message = null;

            var messageOptions = options.message;

            if (messageOptions != null)
            {
                switch (messageOptions.type)
                {
                case WeChatShareType.app:
                    break;

                case WeChatShareType.emotion:
                    break;

                case WeChatShareType.file:
                    break;

                case WeChatShareType.image:
                    if (!String.IsNullOrEmpty(messageOptions.url))
                    {
                        message = new WXImageMessage(messageOptions.url);
                    }
                    else if (!String.IsNullOrEmpty(messageOptions.data))
                    {
                        message = new WXImageMessage(Convert.FromBase64String(messageOptions.data));
                    }
                    else
                    {
                        dispatchResult(PluginResult.Status.ERROR, ERR_INVALID_OPTIONS);
                        return;
                    }
                    break;

                case WeChatShareType.music:
                    break;

                case WeChatShareType.video:
                    break;

                case WeChatShareType.webpage:
                default:
                    message = new WXWebpageMessage(messageOptions.url);
                    break;
                }

                if (message == null)
                {
                    dispatchResult(PluginResult.Status.ERROR, ERR_UNSUPPORTED_MEDIA_TYPE);
                    return;
                }

                message.Title       = messageOptions.title;
                message.Description = messageOptions.description;

                if (!String.IsNullOrEmpty(messageOptions.thumbData))
                {
                    message.ThumbData = Convert.FromBase64String(messageOptions.thumbData);
                }
            }
            else if (options.text != null)
            {
                message = new WXTextMessage(options.text);
            }
            else
            {
                dispatchResult(PluginResult.Status.ERROR, ERR_INVALID_OPTIONS);
                return;
            }

            var scene = options.scene;

            if (scene == null)
            {
                scene = WeChatScene.timeline;
            }

            try {
                var request = new SendMessageToWX.Req(message, (int)scene);
                api.SendReq(request);
            } catch (WXException e) {
                dispatchResult(PluginResult.Status.ERROR, e.Message);
                return;
            }
        }
Ejemplo n.º 21
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource   = Resource.Layout.Toolbar;

            Resources.DisplayMetrics.ScaledDensity = 2;//告诉android不要把自己大小单位缩放
            //double systemDensity = DeviceDisplay.MainDisplayInfo.Density;
            //Resources.DisplayMetrics.Density = (float)systemDensity / 2.55F;
            //var temp = Resources.DisplayMetrics;
            //var device = DeviceDisplay.MainDisplayInfo;
            App.ScreenWidth  = Resources.DisplayMetrics.WidthPixels;
            App.ScreenHeight = Resources.DisplayMetrics.HeightPixels;

            if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
            {
                //透明状态栏
                //Window.AddFlags(WindowManagerFlags.TranslucentStatus);
                Window.SetStatusBarColor(Android.Graphics.Color.LightGray);
                //不遮挡导航栏
                Window.AddFlags(WindowManagerFlags.ForceNotFullscreen);
            }

            Rg.Plugins.Popup.Popup.Init(this, savedInstanceState); //弹出框
            CarouselViewRenderer.Init();                           //轮播图
            CachedImageRenderer.Init(true);

            //支付宝
            MessagingCenter.Subscribe <object, string>(this, "Pay", (sender, sign) =>
            {
                try
                {
                    //Func<string, string> test = Pay;
                    //IAsyncResult asyncResult = test.BeginInvoke(sign, null, null);
                    //string result = test.EndInvoke(asyncResult);
                    //
                    //Console.WriteLine(result);
                    //Status = "";
                    Thread the = new Thread(new ParameterizedThreadStart(Pay));
                    the.Start(sign);
                    //the.Join();
                    //Console.WriteLine(Pay(sign));

                    //Task<string> task = new Task<string>(async () => await Pay(sign));
                    //var result = await Pay(sign);
                    //task.Wait();
                    //task.RunSynchronously();
                    //Console.WriteLine(result);

                    //PayDelegate payDelegate = Pay;
                    //Task<string> task = Task<string>.Factory.FromAsync(payDelegate.BeginInvoke(sign, Callback, "a delegate asynchronous call"), payDelegate.EndInvoke);
                    //Task<string> task = Task<string>.Factory.FromAsync(payDelegate.BeginInvoke, payDelegate.EndInvoke, sign, "a delegate asynchronous call");
                    //task.ContinueWith(t => MessagingCenter.Send(new object(), "PaySuccess", t.Result));
                }
                catch (ThreadAbortException)
                {
                    MessagingCenter.Send(new object(), "PaySuccess", Status);
                }
            });

            //微信相关
            //注册
            MessagingCenter.Subscribe <object>(this, "Register", d =>
            {
                var result = RegToWx();
                // MessagingCenter.Send(new object(), "Registered", result);//广播注册的结果
            });

            MessagingCenter.Subscribe <object>(this, "Login", d =>
            {
                // send oauth request
                SendAuth.Req req = new SendAuth.Req();
                req.Scope        = "snsapi_userinfo";
                req.State        = "wechat_sdk_demo_test";
                bool result      = wxApi.SendReq(req);
            });

            //分享小程序给朋友
            MessagingCenter.Subscribe <object, string>(this, "ShareMiniProgramToFriend", (sender, arg) =>
            {
                WXMiniProgramObject miniProgramObj = new WXMiniProgramObject();
                miniProgramObj.WebpageUrl          = "http://www.qq.com";                        // 兼容低版本的网页链接
                miniProgramObj.MiniprogramType     = WXMiniProgramObject.MiniptogramTypeRelease; // 正式版:0,测试版:1,体验版:2
                miniProgramObj.UserName            = "******";                          // 小程序原始id
                miniProgramObj.Path = "/pages/media";                                            //小程序页面路径;对于小游戏,可以只传入 query 部分,来实现传参效果,如:传入 "?foo=bar"

                WXMediaMessage msg = new WXMediaMessage(miniProgramObj);
                msg.Title          = "小程序消息Title";           // 小程序消息title
                msg.Description    = "小程序消息Desc";            // 小程序消息desc
                //msg.ThumbData = getThumb();                      // 小程序消息封面图片,小于128k

                SendMessageToWX.Req req = new SendMessageToWX.Req
                {
                    //Transaction = DateTime.Now.ToFileTimeUtc().ToString(),
                    Message = msg,
                    Scene   = SendMessageToWX.Req.WXSceneSession // 目前只支持会话
                };

                wxApi.SendReq(req);
            });

            //分享文字给朋友
            MessagingCenter.Subscribe <object, string>(this, "ShareToFriend", (sender, arg) =>
            {
                WXTextObject textObj = new WXTextObject {
                    Text = arg
                };                                                     //定义wx文本对象
                WXMediaMessage msg = new WXMediaMessage {
                    MyMediaObject = textObj, Description = arg
                };                                                                                     //定义wxmsg对象
                SendMessageToWX.Req req = new SendMessageToWX.Req()
                {
                    Transaction = DateTime.Now.ToFileTimeUtc().ToString(),
                    Message     = msg,
                    Scene       = SendMessageToWX.Req.WXSceneSession//分享到对话
                };
                wxApi.SendReq(req);
            });
            //分享文字到朋友圈
            MessagingCenter.Subscribe <object, string>(this, "ShareToTimeline", (sender, arg) =>
            {
                WXTextObject textObj = new WXTextObject {
                    Text = arg
                };                                                     //定义wx文本对象
                WXMediaMessage msg = new WXMediaMessage {
                    MyMediaObject = textObj, Description = arg
                };                                                                                     //定义wxmsg对象
                SendMessageToWX.Req req = new SendMessageToWX.Req()
                {
                    Transaction = DateTime.Now.ToFileTimeUtc().ToString(),
                    Message     = msg,
                    Scene       = SendMessageToWX.Req.WXSceneTimeline//分享到朋友圈
                };
                wxApi.SendReq(req);
            });

            /*
             * //分享网页给朋友
             * MessagingCenter.Subscribe<object, string>(this, "ShareToFriend", (sender, arg) =>
             * {
             *  //初始化一个WXWebpageObject,填写url
             *  WXWebpageObject webpage = new WXWebpageObject();
             *  webpage.WebpageUrl = "网页url";
             *
             *  //用 WXWebpageObject 对象初始化一个 WXMediaMessage 对象
             *  WXMediaMessage msg = new WXMediaMessage(webpage);
             *  msg.Title = "网页标题 ";
             *  msg.Description = "网页描述";
             *  Bitmap thumbBmp = BitmapFactory.decodeResource(getResources(), R.drawable.send_music_thumb);
             *  msg.ThumbData = Util.bmpToByteArray(thumbBmp, true);
             *
             *  //构造一个Req
             *  SendMessageToWX.Req req = new SendMessageToWX.Req();
             *  req.Transaction = buildTransaction("webpage");
             *  req.Message = msg;
             *  req.Scene = mTargetScene;
             *  req.UserOpenId = getOpenId();
             *
             *  //调用api接口,发送数据到微信
             *  wxApi.sendReq(req);
             * });*/

            base.OnCreate(savedInstanceState);
            Instance = this;
            Platform.Init(this, savedInstanceState); // add this line to your code, it may also be called: bundle
            Forms.Init(this, savedInstanceState);

            LoadApplication(new App());
        }
Ejemplo n.º 22
0
        //所有发送数据的按钮的处理函数,根据点击不同,组织不同数据发送给微信
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("############################");
            System.Diagnostics.Debug.WriteLine("Button_Click_1 in");
            string        AppID   = ConstString.APPID;
            int           scene   = SendMessageToWX.Req.WXSceneSession; //发给微信朋友
            WXBaseMessage message = null;

            //拉起微信
            if (btnLaunch == sender)
            {
                WXAPIFactory.CreateWXAPI(AppID).OpenWXApp();
                return;
            }

            //读取实例数据,示例数据都是从res中读取,实现时根据你的应用实际情况取得数据
            byte[] png = readRes("Res/1.png"); //thumb
            byte[] jpg = readRes("Res/1.jpg"); //pic
            byte[] gif = readRes("Res/1.gif"); //emoji
            byte[] txt = readRes("Res/1.txt"); //text
            byte[] doc = readRes("Res/1.doc"); //word file

            //发送到朋友圈,设置scene为WXSceneTimeline
            if (sender == btnTxtSns || sender == btnImgSns || sender == btnUrlSns || sender == btnMusicSns)
            {
                scene = SendMessageToWX.Req.WXSceneTimeline;
            }

            //用户选择发给好友还是朋友圈,设置scene为WXSceneChooseByUser
            if (sender == btnTxt_ChooseByUser)
            {
                scene = SendMessageToWX.Req.WXSceneChooseByUser;
            }

            //文本数据的组装
            if (sender == btnTxt || sender == btnTxtSns)
            {
                WXTextMessage msg = new WXTextMessage();
                msg.Title     = "text";
                msg.ThumbData = null;
                msg.Text      = "this is a text";
                message       = msg;
            }

            //图片数据的组装
            if (sender == btnImg || sender == btnImgSns)
            {
                WXImageMessage msg = new WXImageMessage();
                msg.Title       = "picture";
                msg.Description = "this is a picture";
                msg.ThumbData   = png;

                //图片的byte[]数据
                //msg.ImageData = jpg;

                //图片的网络链接,ImageUrl和ImageData二者取其一,不要同时都填值
                msg.ImageUrl = "http://soso7.gtimg.cn/sosopic/0/4085165533549977883/150";

                message = msg;
            }

            //动态表情数据的组装
            if (sender == btnEmj)
            {
                WXEmojiMessage msg = new WXEmojiMessage();
                msg.Title       = "Gif";
                msg.Description = "this is a Gif";
                msg.ThumbData   = png;

                msg.EmojiData = gif;

                message = msg;
            }

            //文件数据的组装
            if (sender == btnFile)
            {
                WXFileMessage msg = new WXFileMessage();
                msg.Title       = "file";
                msg.Description = "this is a file";
                msg.ThumbData   = png;

                msg.FileData = doc;
                msg.FileName = "1.doc";

                message = msg;
            }

            //链接数据的组装
            if (sender == btnUrl || sender == btnUrlSns)
            {
                WXWebpageMessage msg = new WXWebpageMessage();
                msg.Title       = "qq homepage";
                msg.Description = "goto qq homepage";
                msg.ThumbData   = png;

                msg.WebpageUrl = "http://www.qq.com";

                message = msg;
            }

            //音乐数据的组装
            if (sender == btnMusic || sender == btnMusicSns || sender == btnTxt_ChooseByUser)
            {
                WXMusicMessage msg = new WXMusicMessage();
                msg.Title       = "music";
                msg.Description = "by XXX";
                msg.ThumbData   = png;

                msg.MusicUrl        = "http://y.qq.com/i/song.html#p=7B22736F6E675F4E616D65223A2220E998BFE5A3ABE58CB9E781B5222C22736F6E675F5761704C69766555524C223A22687474703A2F2F74736D7573696334382E74632E71712E636F6D2F586B30305156374F4141414134674141414250657376594E4D7A55327A4441784E654D35586A4A63366F76484F415942443174754251414F4347384439394B343965776F6244733D2F31313134383332342E6D34613F7569643D3233343931313534393826616D703B63743D3026616D703B636869643D30222C22736F6E675F5769666955524C223A22687474703A2F2F73747265616D31312E71716D757369632E71712E636F6D2F33313134383332342E6D7033222C226E657454797065223A2277696669222C22736F6E675F416C62756D223A22E88BB1E79A87E7B2BEE9808932303038222C22736F6E675F4944223A313134383332342C22736F6E675F54797065223A312C22736F6E675F53696E676572223A22E99988E5A595E8BF85222C22736F6E675F576170446F776E4C6F616455524C223A22687474703A2F2F74736D757369633132382E74632E71712E636F6D2F586C464E4D31376C414141415977414141442F6954452B656E7A457A6D6A59324D4D55314F444C5230483937504B344142713850446D3550476C56436366394D536661644D5464704E7A3163665451344D673D3D2F33313134383332342E6D70333F7569643D3233343931313534393826616D703B63743D3026616D703B636869643D3026616D703B73747265616D5F706F733D35227D";
                msg.MusicLowBandUrl = "http://y.qq.com/i/song.html#p=7B22736F6E675F4E616D65223A2220E998BFE5A3ABE58CB9E781B5222C22736F6E675F5761704C69766555524C223A22687474703A2F2F74736D7573696334382E74632E71712E636F6D2F586B30305156374F4141414134674141414250657376594E4D7A55327A4441784E654D35586A4A63366F76484F415942443174754251414F4347384439394B343965776F6244733D2F31313134383332342E6D34613F7569643D3233343931313534393826616D703B63743D3026616D703B636869643D30222C22736F6E675F5769666955524C223A22687474703A2F2F73747265616D31312E71716D757369632E71712E636F6D2F33313134383332342E6D7033222C226E657454797065223A2263617272696572222C22736F6E675F416C62756D223A22E88BB1E79A87E7B2BEE9808932303038222C22736F6E675F4944223A313134383332342C22736F6E675F54797065223A312C22736F6E675F53696E676572223A22E99988E5A595E8BF85222C22736F6E675F576170446F776E4C6F616455524C223A22687474703A2F2F74736D757369633132382E74632E71712E636F6D2F586C464E4D31376C414141415977414141442F6954452B656E7A457A6D6A59324D4D55314F444C5230483937504B344142713850446D3550476C56436366394D536661644D5464704E7A3163665451344D673D3D2F33313134383332342E6D70333F7569643D3233343931313534393826616D703B63743D3026616D703B636869643D3026616D703B73747265616D5F706F733D35227D";

                message = msg;
            }

            //视频数据的组装
            if (sender == btnVedio)
            {
                WXVideoMessage msg = new WXVideoMessage();
                msg.Title       = "music";
                msg.Description = "by XXX";
                msg.ThumbData   = png;

                msg.VideoUrl        = "http://y.qq.com/i/song.html#p=7B22736F6E675F4E616D65223A2220E998BFE5A3ABE58CB9E781B5222C22736F6E675F5761704C69766555524C223A22687474703A2F2F74736D7573696334382E74632E71712E636F6D2F586B30305156374F4141414134674141414250657376594E4D7A55327A4441784E654D35586A4A63366F76484F415942443174754251414F4347384439394B343965776F6244733D2F31313134383332342E6D34613F7569643D3233343931313534393826616D703B63743D3026616D703B636869643D30222C22736F6E675F5769666955524C223A22687474703A2F2F73747265616D31312E71716D757369632E71712E636F6D2F33313134383332342E6D7033222C226E657454797065223A2277696669222C22736F6E675F416C62756D223A22E88BB1E79A87E7B2BEE9808932303038222C22736F6E675F4944223A313134383332342C22736F6E675F54797065223A312C22736F6E675F53696E676572223A22E99988E5A595E8BF85222C22736F6E675F576170446F776E4C6F616455524C223A22687474703A2F2F74736D757369633132382E74632E71712E636F6D2F586C464E4D31376C414141415977414141442F6954452B656E7A457A6D6A59324D4D55314F444C5230483937504B344142713850446D3550476C56436366394D536661644D5464704E7A3163665451344D673D3D2F33313134383332342E6D70333F7569643D3233343931313534393826616D703B63743D3026616D703B636869643D3026616D703B73747265616D5F706F733D35227D";
                msg.VideoLowBandUrl = "http://y.qq.com/i/song.html#p=7B22736F6E675F4E616D65223A2220E998BFE5A3ABE58CB9E781B5222C22736F6E675F5761704C69766555524C223A22687474703A2F2F74736D7573696334382E74632E71712E636F6D2F586B30305156374F4141414134674141414250657376594E4D7A55327A4441784E654D35586A4A63366F76484F415942443174754251414F4347384439394B343965776F6244733D2F31313134383332342E6D34613F7569643D3233343931313534393826616D703B63743D3026616D703B636869643D30222C22736F6E675F5769666955524C223A22687474703A2F2F73747265616D31312E71716D757369632E71712E636F6D2F33313134383332342E6D7033222C226E657454797065223A2263617272696572222C22736F6E675F416C62756D223A22E88BB1E79A87E7B2BEE9808932303038222C22736F6E675F4944223A313134383332342C22736F6E675F54797065223A312C22736F6E675F53696E676572223A22E99988E5A595E8BF85222C22736F6E675F576170446F776E4C6F616455524C223A22687474703A2F2F74736D757369633132382E74632E71712E636F6D2F586C464E4D31376C414141415977414141442F6954452B656E7A457A6D6A59324D4D55314F444C5230483937504B344142713850446D3550476C56436366394D536661644D5464704E7A3163665451344D673D3D2F33313134383332342E6D70333F7569643D3233343931313534393826616D703B63743D3026616D703B636869643D3026616D703B73747265616D5F706F733D35227D";

                message = msg;
            }

            //把数据发送给微信,数据错误或发送错误,会抛出WXException,调试时,可从WXException中得知是什么错误。
            //发布应用时,可直接向用户提示错误,也可以根据自己实际需要定制错误的处理
            try
            {
                SendMessageToWX.Req req = new SendMessageToWX.Req(message, scene);
                IWXAPI api = WXAPIFactory.CreateWXAPI(AppID);

                Console.WriteLine("api.SendReq in");
                api.SendReq(req);
            }
            catch (WXException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Ejemplo n.º 23
0
        //发起预支付请求
        public void sendpayorder()
        {
            string TimeStamp = getTimestamp();
            //随机字符串
            string NonceStr = getNoncestr();
            //创建支付应答对象
            var packageReqHandler = new RequestHandler();

            //初始化
            packageReqHandler.init();

            packageReqHandler.setParameter("body", "会员公司费用"); //商品信息 127字符
            packageReqHandler.setParameter("appid", appid);
            packageReqHandler.setParameter("mch_id", mchid);
            packageReqHandler.setParameter("nonce_str", NonceStr.ToLower());
            packageReqHandler.setParameter("notify_url", "http://XXX/pay.aspx");

            packageReqHandler.setParameter("out_trade_no", orderid);           //商家订单号
            packageReqHandler.setParameter("spbill_create_ip", "192.168.1.1"); //用户的公网ip,不是商户服务器IP
            //  packageReqHandler.setParameter("total_fee", (am.Aconfiginfo.Vipmoney*100).ToString()); //商品金额,以分为单位(money * 100).ToString()
            packageReqHandler.setParameter("total_fee", "1");
            packageReqHandler.setParameter("trade_type", "APP");

            packageReqHandler.setParameter("attach", "");//自定义参数 127字符

            string Sign = packageReqHandler.CreateMd5Sign("key", AppKey);

            packageReqHandler.setParameter("sign", Sign);
            string data      = packageReqHandler.parseXML();
            string prepayXml = HttpUtil.Send(data, "https://api.mch.weixin.qq.com/pay/unifiedorder");


            var xdoc = new XmlDocument();

            xdoc.LoadXml(prepayXml);
            XmlNode     xn  = xdoc.SelectSingleNode("xml");
            XmlNodeList xnl = xn.ChildNodes;

            if (xnl.Count > 7)
            {
                string PrepayId = xnl[7].InnerText;
                TimeStamp = getTimestamp();
                //随机字符串
                NonceStr = getNoncestr();



                PayReq payRequest = new PayReq();
                payRequest.AppId        = appid;
                payRequest.PrepayId     = PrepayId;
                payRequest.PartnerId    = mchid;
                payRequest.PackageValue = "Sign=WXPay";
                payRequest.NonceStr     = NonceStr;
                payRequest.TimeStamp    = TimeStamp;

                var paySignReqHandler = new RequestHandler();
                paySignReqHandler.setParameter("appid", payRequest.AppId);
                paySignReqHandler.setParameter("noncestr", payRequest.NonceStr);
                paySignReqHandler.setParameter("package", "Sign=WXPay");
                paySignReqHandler.setParameter("partnerid", payRequest.PartnerId);
                paySignReqHandler.setParameter("prepayid", payRequest.PrepayId);
                paySignReqHandler.setParameter("timestamp", payRequest.TimeStamp);
                // paySignReqHandler.setParameter("signType", "MD5");
                string PaySign = paySignReqHandler.CreateMd5Sign("key", AppKey);
                payRequest.Sign = PaySign;
                Toast.MakeText(this, "正在调起支付", ToastLength.Short).Show();
                wxApi.RegisterApp(appid);

                //这个返回 一直都是true 哪怕你参数都是错的 我也是醉了 我被这个状态迷惑了好久在找其他问题
                bool issceuss = wxApi.SendReq(payRequest);
                //  Toast.MakeText(this, "状态"+ issceuss, ToastLength.Short).Show();
            }
        }
Ejemplo n.º 24
0
        private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            try
            {
                try
                {
                    SendAuth.Req req = new SendAuth.Req("[YOUR SCOPE]", "test");

                    IWXAPI api  = WXAPIFactory.CreateWXAPI("[YOUR APP ID]");
                    var    isok = await api.SendReq(req);
                }
                catch (WXException)
                {
                }

                //int scene = SendMessageToWX.Req.WXSceneTimeline; //发给微信朋友

                //var file = await Package.Current.InstalledLocation.GetFileAsync("1.png");
                //using (var stream = await file.OpenReadAsync())
                //{
                //    var pic = new byte[stream.Size];
                //    await stream.AsStream().ReadAsync(pic, 0, pic.Length);

                //    var message = new WXWebpageMessage
                //    {
                //        WebpageUrl = "http://www.baidu.com",
                //        Title = "title",
                //        Description = "desc",
                //        ThumbData = pic
                //    };

                //    try
                //    {
                //        SendMessageToWX.Req req = new SendMessageToWX.Req(message, scene);
                //        IWXAPI api = WXAPIFactory.CreateWXAPI("[YOUR APP ID]");
                //        var isok = await api.SendReq(req);
                //    }
                //    catch (WXException)
                //    {
                //    }
                //}

                //var message = new WXTextMessage();
                //message.Title = "flora";
                //message.Text = "这是一段文本内容";
                //message.ThumbData = null;
                //SendMessageToWX.Req req = new SendMessageToWX.Req(message, scene);
                //IWXAPI api = WXAPIFactory.CreateWXAPI("[YOUR APP ID]");
                //var isok = await api.SendReq(req);

                //WXImageMessage message = new WXImageMessage();
                //var file = await Package.Current.InstalledLocation.GetFileAsync("1.png");
                //using (var stream = await file.OpenReadAsync())
                //{
                //    var pic = new byte[stream.Size];
                //    await stream.AsStream().ReadAsync(pic, 0, pic.Length);
                //    message.Title = "tupian";
                //    message.Description = "desc";
                //    message.ThumbData = pic;
                //    message.ImageUrl = "http://tp3.sinaimg.cn/1882347990/180/5725518284/1";

                //    try
                //    {
                //        SendMessageToWX.Req req = new SendMessageToWX.Req(message, scene);
                //        IWXAPI api = WXAPIFactory.CreateWXAPI("[YOUR APP ID]");
                //        var isok = await api.SendReq(req);
                //    }
                //    catch (WXException)
                //    {
                //    }
                //}
            }
            catch (Exception ex)
            {
                //new MessageDialog(ex.Message).ShowAsync();
            }
        }