zhaojs
2023-09-15 fc13938ff90213060532d99a600dea4a84456885
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using Quartz;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using CommonUtil;
using Operater.DAL;
using ScheduledTasks.Model;
using CommonUtil.Web;
using Operater.DbModel;
 
namespace ScheduledTasks.Handle
{
    public class WxMiniTokenHandle : IJob
    {
        /// <summary>
        /// 微信小程序token刷新
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task Execute(IJobExecutionContext context)
        {
            await Synchronize(context);
        }
        public static async Task Synchronize(IJobExecutionContext context)
        {
            try
            {
                Console.WriteLine($"开始查询是否更新token-{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
                var set = new AppsetDAL().ListGet(t => t.Id != null);
                if (set[0].FxWxToken.IsNullOrEmpty() || set[0].FxWxTokenExpires.IsNull() || (DateTime)set[0].FxWxTokenExpires <= DateTime.Now)
                {
                    Console.WriteLine($"开始更新-{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
                    FxWxTokenRefresh(set[0].Id);
                }
                Console.WriteLine($"执行完成-{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
            }
            catch (Exception e)
            {
                LogUtil.Info(e.ToString(), "微信小程序error");
            }
        }
 
        public static void FxWxTokenRefresh(string id)
        {
            string url = $"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx12f06ec7a21748bc&secret=97d96ad5ce87ab233da78df9364fbcb5";
            var resstr = new WebUtil().DoGet(url, null);
            TokenModel tokenModel = JSONUtil.JsonToObject<TokenModel>(resstr);
            if (tokenModel.IsNull() || tokenModel.access_token.IsNullOrEmpty())
            {
                return;
            }
            Appset appset = new Appset()
            {
                FxWxToken = tokenModel.access_token,
                FxWxTokenExpires = DateTime.Now.AddMinutes(100)
            };
            new AppsetDAL().Update(appset, i => new { i.FxWxToken, i.FxWxTokenExpires }, t => t.Id == id);
 
        }
    }
}