Beispiel #1
0
        static void Main(string[] args)
        {
            var channel = GrpcChannel.ForAddress("https://localhost:5001");

            var client = new ShopService.ShopServiceClient(channel);

            var request = new CreateCustomerRequest
            {
                Name = "Hello DotNED Saturday"
            };

            var response = client.CreateCustomer(request);

            var orderLine1 = new CreateOrderLine()
            {
                ProductId = 100,
                Price     = 10,
                Quantity  = 2
            };

            var orderLine2 = new CreateOrderLine()
            {
                ProductId = 200,
                Price     = 20,
                Quantity  = 3
            };

            var order = new CreateOrder()
            {
                Customer  = response.Id,
                OrderedAt = Timestamp.FromDateTime(DateTime.UtcNow),
            };

            order.OrderLines.Add(orderLine1);
            order.OrderLines.Add(orderLine2);

            client.AddOrder(order);

            Console.WriteLine($"Created {response.Name} with id {response.Id}");
            Console.ReadLine();
        }
        /// <summary>
        /// 创建订单
        /// </summary>
        /// <param name="reqdata"></param>
        /// <returns></returns>
        public string CreateOrder(string reqdata)
        {
            string res = "";

            try
            {
                Request_CreateOrder req = JsonConvert.DeserializeObject <Request_CreateOrder>(reqdata);

                #region  单参数
                CreateOrder_Request request = new CreateOrder_Request();
                request.key     = AppSettingUtil.InsideApiKey2;
                request.UnionId = req.UnionId;
                //request.PhoneNumber = req.PhoneNumber.Replace("15827002712", "18717764701");
                request.PhoneNumber = req.PhoneNumber;
                request.CompanyName = req.CompanyName;
                request.head        = new CreateOrderHead()
                {
                    VendorName        = req.CompanyName,
                    ReceiverName      = req.addr.alias,
                    ReceiverContactor = req.addr.name,
                    ReceiverMobile    = req.addr.phone,
                    ReceiverCountry   = string.IsNullOrEmpty(req.addr.country) ? "" : req.addr.country,
                    ReceiverProvince  = req.addr.province,
                    ReceiverCity      = req.addr.city,
                    ReceiverDistrict  = req.addr.area,
                    ReceiverAddress   = req.addr.address,
                    CreateDate        = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz"),
                    Remark            = req.oremark
                };
                request.lines = new List <CreateOrderLine>();
                foreach (var item in req.goods)
                {
                    CreateOrderLine crealine = new CreateOrderLine()
                    {
                        ProductCode    = item.ProductCode,
                        ProductDescrEN = item.ProductDescrEN,
                        SerialNumber   = item.SerialNumber,
                        ShipQty        = item.number,
                        LocationName   = item.LocationName
                    };
                    request.lines.Add(crealine);
                }
                #endregion

                CreateOrder_Response response = orderhttpdal.CreateOrder(JsonConvert.SerializeObject(request));
                if (response.code == 200)
                {
                    res = JsonConvert.SerializeObject(new { code = 0, msg = "ok", jobcode = response.data });
                    removecart(req);//移除购物车商品
                }
                else
                {
                    res = JsonConvert.SerializeObject(new { code = 10002, msg = response.msg.Replace("<br />", "") });
                }
            }
            catch (Exception ex)
            {
                res = JsonConvert.SerializeObject(new { code = 10003, msg = "系统故障", count = 0 });
                LogHelper.ErrorLog("CreateOrder异常:" + ex.Message + "," + ex.StackTrace);
            }
            return(res);
        }