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
63
64
65
66
67
68
69
70
71
72
73
using CommonUtil;
using CommonUtil.Web;
using Operater.DAL;
using Operater.DbModel;
using Quartz;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using YouZanSDKStandard.Api;
 
namespace ScheduledTasks.Handle
{
    /// <summary>
    /// 有赞和1688token更新
    /// </summary>
    public class TokenUpdateHandle : IJob
    {
        public async Task Execute(IJobExecutionContext context)
        {
            await Synchronize(context);
        }
 
        public async Task Synchronize(IJobExecutionContext context)
        {
            try
            {
                Console.WriteLine("更新有赞token");
                var appSet = new AppsetDAL().ListGet(t => t.Id != null);
                YzTokenRefresh(appSet[0]);
                Console.WriteLine("更新有赞token-任务完成");
 
            }
            catch (Exception e)
            {
                LogUtil.Info($"主线程错误:{e.ToString()}", "有赞和1688token更新error");
            }
        }
 
        private void YzTokenRefresh(Appset set)
        {
            try
            {
                if (set.YzTokenExpires.IsNull() || set.YzTokenExpires <= DateTime.Now.AddDays(2))
                {
                    //刷新有赞token
                    var yzRsp = YouZanRefreshToken.TopRefresh(true, set.YzShopid);
                    if (yzRsp.IsNull())
                    {
                        LogUtil.Info($"有赞token刷新error:接口错误", "有赞token更新error");
                    }
                    else
                    {
                        Appset appset = new Appset()
                        {
                            YzToken = yzRsp.AccessToken,
                            YzTokenExpires = DateTime.Now.AddDays(7)
                        };
                        new AppsetDAL().Update(appset, i => new { i.YzToken, i.YzTokenExpires }, t => t.Id == set.Id);
                        LogUtil.Info($"有赞token刷新成功", "token刷新日志");
                    }
                }
 
            }
            catch (Exception e)
            {
                LogUtil.Info($"主线程错误:{e.ToString()}", "有赞token更新error");
            }
        }
 
 
    }
}