예제 #1
0
        public async Task <ActionResult <UnlockResponse> > Unlock(string data)
        {
            var(openId, type) = HttpContext.Session.GetExternId();
            _logger.LogInformation(string.Format("[WeChatAppController] [Unlock] OpenId:{0},Type:{1}", openId, type));
            if (string.IsNullOrEmpty(openId))
            {
                return(new UnlockResponse
                {
                    Code = Error.Codes.LoginRequired.AsString(),
                    Message = Error.Codes.LoginRequired.AsMessage(),
                });
            }

            var bindId = HttpContext.Session.GetBindId();

            _logger.LogInformation(string.Format("[WeChatAppController] [Unlock] bindId:{0}", bindId));
            if (string.IsNullOrEmpty(bindId))
            {
                var checkResult = _uniflow.CheckBind(
                    new ExternalIdRequest {
                    ExternalId = openId, Type = "WeChatAppOpenID"
                });
                _logger.LogInformation(string.Format("[WeChatAppController] [Unlock] [CheckBind] Code:{0}", checkResult.Value.Code));
                if (checkResult.Value.Code == "0")
                {
                    bindId = checkResult.Value.BindId;
                    HttpContext.Session.SetBindId(bindId);
                }

                return(new UnlockResponse
                {
                    Code = checkResult.Value.Code,
                    Message = checkResult.Value.Message,
                });
            }

            string key = settings["UniflowService:EncryptKey"];

            try
            {
                data = EncryptUtil.Decrypt(key, data);
            }
            catch (Exception ex)
            {
                return(new UnlockResponse
                {
                    Code = Error.Codes.DecryptError.AsString(),
                    Message = Error.Codes.DecryptError.AsMessage(ex.Message),
                });
            }
            var parts = data.Split('@');

            if (parts.Length < 4 ||
                !Uri.IsWellFormedUriString(parts[0], UriKind.Absolute) ||
                string.IsNullOrEmpty(parts[1]))
            {
                return(new UnlockResponse
                {
                    Code = Error.Codes.InvalidData.AsString(),
                    Message = Error.Codes.InvalidData.AsMessage(
                        "invalid BarcodeData format"),
                });
            }
            var serial = parts[1];

            var result = await _uniflow.Unlock(new UnlockRequest { BindId = bindId, Serial = serial });

            _logger.LogInformation(string.Format("[WeChatAppController] [Unlock] Code:{0}", result.Value.Code));
            if (result.Value.Code == "0")
            {
                return(new UnlockResponse
                {
                    PrinterName = serial,
                    PrinterStatus = "1",
                    Code = result.Value.Code,
                    Message = result.Value.Message,
                });
            }
            else
            {
                return(new UnlockResponse
                {
                    Code = result.Value.Code,
                    Message = result.Value.Message,
                });
            }
        }
예제 #2
0
        public async Task <ActionResult> Unlock(string data)
        {
            _logger.LogInformation(string.Format("[HomeController] [Unlock] data:{0}", data));
            var bindId = HttpContext.Session.GetBindId();

            if (string.IsNullOrEmpty(bindId))
            {
                if (bool.TryParse(settings["NoLogin"], out bool noLogin) && noLogin)
                {
                    return(View("Error", new ErrorViewModel {
                        Message = "会话已过期,请重新进入。"
                    }));
                }

                return(RedirectToAction("Login", new { backto = WebUtility.UrlEncode(Url.Action("Unlock", new { data })) }));
            }
            if (bindId.IsNoLoginBind())
            {
                return(View("Error", new ErrorViewModel {
                    Message = "暂不支持打印机扫码开机。"
                }));
            }

            string key = settings["UniflowService:EncryptKey"];

            var printerSN = HttpContext.Session.GetCurrentPrinterSN();

            if (!string.IsNullOrEmpty(data))
            {
                try
                {
                    data = EncryptUtil.Decrypt(key, data);
                }
                catch (Exception)
                {
                    return(View("Error", new ErrorViewModel {
                        Message = "二维码数据无效。"
                    }));
                }
                var parts = data.Split('@');
                if (parts.Length < 4 ||
                    !Uri.IsWellFormedUriString(parts[0], UriKind.Absolute) ||
                    string.IsNullOrEmpty(parts[1]))
                {
                    return(View("Error", new ErrorViewModel {
                        Message = "二维码数据无效。"
                    }));
                }
                var datetime = DateTime.ParseExact(parts[2], "MMddyyyyHHmmss", CultureInfo.InvariantCulture);
                if (datetime.AddMinutes(6).CompareTo(DateTime.Now) < 0)
                {
                    return(View("Error", new ErrorViewModel {
                        Message = "二维码已经过期。"
                    }));
                }

                printerSN = parts[1];
                HttpContext.Session.SetCurrentPrinterSN(printerSN);
            }

            if (string.IsNullOrEmpty(printerSN))
            {
                return(View("Error", new ErrorViewModel {
                    Message = "没有当前打印机,请扫描打印机二维码。"
                }));
            }

            var result = await _uniflow.Unlock(new UnlockRequest { BindId = bindId, Serial = printerSN });

            _logger.LogInformation(string.Format("[HomeController] [Unlock] result:{0},sn:", result.Value.Code, printerSN));
            ViewBag.Result = result.Value.Code == "0";
            return(View());
        }