本帖最后由 真的小白 于 2021-10-5 17:16 编辑
易语言原帖:https://www.52pojie.cn/thread-1522668-1-1.html
小白练习做了一个C#版本
[C#] 纯文本查看 复制代码 using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace QueryTheBalanceOfUnicomPhoneNumber
{
internal class Program
{
private static async Task Main(string[] args)
{
double balance;
string phoneNumber;
WindowHelper.SetConsoleCenterOfScreen();
Console.Title = "联通手机号话费查询";
while (true)
{
Console.Clear();
Console.Write("请输入联通手机号码:");
phoneNumber = Console.ReadLine();
if (!Regex.IsMatch(phoneNumber, @"^1\d{10}$"))
{
Console.WriteLine();
Console.WriteLine("手机号码输入有误,请重试!");
CmdHelper.Pause();
continue;
}
try
{
balance = await UnicomHelper.GetBalanceByPhoneNumber(phoneNumber);
Console.WriteLine();
if (balance >= 0.0)
{
Console.Write("当前余额为:");
Console.ForegroundColor = ConsoleColor.Green;
Console.Write($"{balance:0.00}");
}
else
{
Console.Write("当前欠费:");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write($"{Math.Abs(balance):0.00}");
}
Console.ResetColor();
Console.WriteLine("元");
}
catch (ArgumentException e)
{
Console.WriteLine();
Console.WriteLine(e.Message);
}
finally
{
CmdHelper.Pause();
}
}
}
}
}
[C#] 纯文本查看 复制代码 using System;
using System.Runtime.InteropServices;
namespace QueryTheBalanceOfUnicomPhoneNumber
{
internal static class WindowHelper
{
private const string Kernel32 = "Kernel32.dll";
private const string User32 = "User32.dll";
private static readonly int ScreenWidth;
private static readonly int ScreenHeight;
private static readonly int SM_CXFULLSCREEN = 16;
private static readonly int SM_CYFULLSCREEN = 17;
static WindowHelper()
{
ScreenWidth = GetSystemMetrics(SM_CXFULLSCREEN);
ScreenHeight = GetSystemMetrics(SM_CYFULLSCREEN);
}
internal static void SetConsoleCenterOfScreen()
{
IntPtr consoleHWnd = GetConsoleWindow();
_ = GetWindowRect(consoleHWnd, out Rect consoleRect);
int consoleWidth = consoleRect.Right - consoleRect.Left;
int consoleHeight = consoleRect.Bottom - consoleRect.Top;
_ = MoveWindow(consoleHWnd,
(ScreenWidth - consoleWidth) >> 1,
(ScreenHeight - consoleHeight) >> 1,
consoleWidth,
consoleHeight,
true);
}
[DllImport(Kernel32)]
private static extern IntPtr GetConsoleWindow();
[DllImport(User32)]
private static extern int GetWindowRect(IntPtr hWnd, out Rect lpRect);
[DllImport(User32)]
private static extern int GetSystemMetrics(int nIndex);
[DllImport(User32)]
private static extern int MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, [MarshalAs(UnmanagedType.Bool)] bool bRepaint);
private struct Rect
{
internal int Left;
internal int Top;
internal int Right;
internal int Bottom;
}
}
}
[C#] 纯文本查看 复制代码 using System.Runtime.InteropServices;
namespace QueryTheBalanceOfUnicomPhoneNumber
{
internal static class CmdHelper
{
private const string Msvcrt = "msvcrt.dll";
private static readonly string PauseCommand = "pause";
internal static void Pause()
{
_ = _wsystem(PauseCommand);
}
[DllImport(Msvcrt, CharSet = CharSet.Unicode)]
private static extern int _wsystem(string str);
}
}
[C#] 纯文本查看 复制代码 using System;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;
namespace QueryTheBalanceOfUnicomPhoneNumber
{
internal static class UnicomHelper
{
internal class UnicomPhoneNumber
{
public string phone { get; set; }
}
private static readonly HttpClient HttpClient = new HttpClient();
private static readonly Uri PostUri = new Uri("https://weixin.10010js.com/app/charge/qryRealFee");
private static readonly UnicomPhoneNumber PostPhoneNumber = new UnicomPhoneNumber();
static UnicomHelper()
{
HttpClient.DefaultRequestHeaders.Referrer = new Uri("https://weixin.10010js.com/actPage/activity/index28.html?");
}
internal static async Task<double> GetBalanceByPhoneNumber(string phoneNumber)
{
PostPhoneNumber.phone = phoneNumber;
HttpResponseMessage responseMessage = await HttpClient.PostAsync(PostUri, JsonContent.Create(PostPhoneNumber));
responseMessage.EnsureSuccessStatusCode();
string responseString = await responseMessage.Content.ReadAsStringAsync();
if (string.IsNullOrEmpty(responseString))
{
throw new ArgumentException("请输入正确的联通手机号码!");
}
return Math.Round(double.Parse(responseString) / 100.0, 2);
}
}
} |