gxc
2019-11-15 05ce435c3b58e53eeab04c672affdeeab75f3036
ZigbeeApp/GateWay.Ios/AppDelegate.cs
old mode 100755 new mode 100644
@@ -8,6 +8,8 @@
using Microsoft.AppCenter;
using Microsoft.AppCenter.Analytics;
using Microsoft.AppCenter.Crashes;
using Shared.IOS.TBL;
namespace GateWay.Ios
{
    // The UIApplicationDelegate for the application. This class is responsible for launching the
@@ -23,14 +25,18 @@
            set;
        }
        internal static UINavigationController rootViewController;
        public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
        {
            base.FinishedLaunching(application, launchOptions);
            //base.FinishedLaunching(application, launchOptions);
            Shared.Application.FontSize = 12;
            Window = new UIWindow(UIScreen.MainScreen.Bounds);
            var root = new UINavigationController(new MainViewController()) { NavigationBarHidden = true };
            Window.RootViewController = root;
            rootViewController = new UINavigationController(new MainViewController()) { NavigationBarHidden = true };
            Window.RootViewController = rootViewController;
            Window.MakeKeyAndVisible();
            Shared.IOS.TBL.WXApi.RegisterApp("wx2ec8f53f6fa36e82", "https://hdlcontrol.com/ZigbeeApp/");
            DeviceTokenAction += (deviceToken) =>
            {
@@ -208,7 +214,100 @@
            // Called when the application is about to terminate. Save data, if needed. See also DidEnterBackground.
            base.WillTerminate(application);
        }
        public static Action<string> RespAction;
        public static void WXLogin()
        {
            var req = new SendAuthReq { Scope = "snsapi_userinfo", State = "ZigbeeApp", OpenID = "0c806938e2413ce73eef92cc3" };
            WXApi.SendAuthReq(req, rootViewController, null, null);
        }
        MyWXApiDelegate myWXApiDelegate = new MyWXApiDelegate { };
        class MyWXApiDelegate : WXApiDelegate
        {
            public override void OnReq(BaseReq req)
            {
            }
            public override void OnResp(BaseResp resp)
            {
                if (resp is SendAuthResp)
                {
                    var sendAuthResp = resp as SendAuthResp;
                    switch (sendAuthResp.ErrCode)
                    {
                        case 0:
                            try
                            {
                                var result = new System.Net.WebClient { }.DownloadString($"https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx2ec8f53f6fa36e82&secret=a08585cd6ff2ce64570b9e7e6525dd8e&code={((SendAuthResp)resp).Code}&grant_type=authorization_code");
                                RespAction?.Invoke(result);
                            }
                            catch {
                                RespAction?.Invoke(null);
                            }
                            break;
                        default:
                            RespAction?.Invoke(null);
                            break;
                    }
                }
            }
        }
        public override bool ContinueUserActivity(UIApplication application, NSUserActivity userActivity, UIApplicationRestorationHandler completionHandler)
        {
            return WXApi.HandleOpenUniversalLink(userActivity, myWXApiDelegate);
        }
    }
}
namespace Shared
{
    public static class QRCode
    {
        static ZXing.Mobile.MobileBarcodeScanner scanner;
        public static async System.Threading.Tasks.Task ScanQRcode(Action<string> action, string topText = "", string bottomText = "", string cancel = "Cancel")
        {
            if (scanner == null)
            {
                scanner = new ZXing.Mobile.MobileBarcodeScanner(GateWay.Ios.AppDelegate.rootViewController) { FlashButtonText = "", TopText = topText, BottomText = bottomText, CancelButtonText = cancel };
            }
            var result = await scanner.Scan();
            if (result != null)
                action?.Invoke(result.Text);
            else
                action?.Invoke(null);
        }
        public static byte[] BytesFromText(string text, int width = 300, int height = 300)
        {
            var barcodeWriter = new ZXing.Mobile.BarcodeWriter
            {
                Format = ZXing.BarcodeFormat.QR_CODE,
                Options = new ZXing.Common.EncodingOptions
                {
                    Width = width,
                    Height = height,
                    Margin = 10
                }
            };
            barcodeWriter.Renderer = new ZXing.Mobile.BitmapRenderer();
            var uiImage = barcodeWriter.Write(text);
            var data = uiImage.AsJPEG();
            var resultBytes = new byte[data.Length];
            System.Runtime.InteropServices.Marshal.Copy(data.Bytes, resultBytes, 0, resultBytes.Length);
            return resultBytes;
        }
    }
}