Example #1
0
        void DD(String code, Guid projectId)
        {
            var accessToken = SubjectDingtalkActivity.AccessToken(projectId);
            var url         = String.Format("https://oapi.dingtalk.com/user/getuserinfo?access_token={1}&code={0}", code, accessToken.AccessToken);

            System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient();
            var data = UMC.Data.JSON.Deserialize(httpClient.GetStringAsync(url)
                                                 .Result) as Hashtable;

            if (data.ContainsKey("userid"))
            {
                var userId = data["userid"] as string;

                var url2   = String.Format("https://oapi.dingtalk.com/user/get?access_token={1}&userid={0}", userId, accessToken.AccessToken);
                var data2  = UMC.Data.JSON.Deserialize(httpClient.GetStringAsync(url2).Result) as Hashtable;
                var mgurl  = data2["avatar"] as string;
                var mobile = data2["mobile"] as string;
                if (String.IsNullOrEmpty(mobile))
                {
                    Utility.Debug("login", data, data2);
                    this.Prompt("未开通获取手机号码权限,请联系钉钉管理员");
                }

                var open = new UMC.Data.Entities.Account()
                {
                    Type = UMC.Security.Account.MOBILE_ACCOUNT_KEY,
                    Name = mobile
                };

                Security.Account.GetRelation(open);


                var nickname = data2["name"] as string;
                var user     = UMC.Security.Membership.Instance().Identity(open.user_id.Value);
                if (user == null)
                {
                    user = UMC.Security.Membership.Instance().CreateUser(open.user_id.Value, "@" + mobile, Utility.Guid(open.user_id.Value), nickname);
                }
                UMC.Security.AccessToken.Login(user
                                               , UMC.Security.AccessToken.Token.Value, 0, "DingTalkPC", false).Put("DingTalk-Setting", Utility.Guid(accessToken.Id.Value))
                .Put("DingTalk-User-Id", userId).Commit();


                if (String.IsNullOrEmpty(mgurl) == false)
                {
                    UMC.Data.WebResource.Instance().Transfer(new Uri(mgurl), open.user_id.Value, 1);
                }

                this.Context.Send("User", true);
            }
            else
            {
                Utility.Debug("login", data);
                this.Prompt("未获得钉钉信息");
            }
        }
Example #2
0
        void Send(WebRequest request, Subject sub, string cid, bool robot)
        {
            if (sub.project_id.HasValue == false)
            {
                return;
            }
            var user    = Security.Identity.Current;
            var project = Utility.CMS.ObjectEntity <Project>().Where.And().Equal(new Project
            {
                Id = sub.project_id.Value
            }).Entities.Single();
            var projectItem = Utility.CMS.ObjectEntity <ProjectItem>().Where.And().Equal(new ProjectItem
            {
                Id = sub.project_item_id.Value
            }).Entities.Single();

            if (robot)
            {
                var pseting = SubjectDingtalkRobotActivity.AccessToken(sub.project_id.Value);
                if (pseting != null)
                {
                    var url = pseting.AccessToken;
                    if (url.StartsWith("https://") == false)
                    {
                        url = "https://oapi.dingtalk.com/robot/send?access_token=" + url;
                    }
                    var sbUrl = new StringBuilder();
                    sbUrl.Append(url);
                    sbUrl.Append("&timestamp=");
                    var timestamp = Utility.TimeSpan() + "000";
                    sbUrl.Append(timestamp);
                    sbUrl.Append("&sign=");
                    sbUrl.Append(CreateToken(timestamp + "\n" + pseting.AppSecret, pseting.AppSecret));

                    var msg = new WebMeta().Put("msgtype", "actionCard").Put("actionCard", new WebMeta().Put("title", sub.Title, "text", string.Format("### {2}的内容发布\n### 标题:{0}\n#### 摘要:\n>{1}...", sub.Title, sub.Description, user.Alias), "singleTitle", "阅读全文", "singleURL", String.Format("https://www.365lu.cn/{0}/{1}/{2}", project.Code, projectItem.Code, sub.Code)));

                    var content = new System.Net.Http.StringContent(UMC.Data.JSON.Serialize(msg), System.Text.UTF8Encoding.UTF8, "application/json");

                    System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient();
                    var str = httpClient.PostAsync(sbUrl.ToString(), content).Result
                              .Content.ReadAsStringAsync().Result;
                    Utility.Log("Robot", sbUrl.ToString(), str, msg);
                }
            }

            if (request.UserAgent.IndexOf("DingTalk") > 0)
            {
                //var strt = UMC.Security.AccessToken.Current.Data["DingTalk-Sub-Id"] as string;

                var userid = UMC.Security.AccessToken.Current.Data["DingTalk-User-Id"] as string;
                if (String.IsNullOrEmpty(cid) == false && String.IsNullOrEmpty(userid) == false)
                {
                    //
                    var Url = String.Format("https://oapi.dingtalk.com/message/send_to_conversation?access_token={0}", SubjectDingtalkActivity.AccessToken(sub.project_id.Value).AccessToken);

                    var hash = new Hashtable();
                    hash["cid"]    = cid;
                    hash["sender"] = userid;

                    hash["msg"] = new WebMeta().Put("msgtype", "action_card").Put("action_card", new WebMeta().Put("title", sub.Title, "markdown", string.Format("### {2}的内容发布\n### 标题:{0}\n#### 摘要:\n>{1}...", sub.Title, sub.Description, user.Alias), "single_title", "阅读全文", "single_url", String.Format("https://www.365lu.cn/{0}/{1}/{2}", project.Code, projectItem.Code, sub.Code)));


                    System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient();
                    var t = httpClient.PostAsync(Url, new System.Net.Http.StringContent(UMC.Data.JSON.Serialize(hash))).Result
                            .Content.ReadAsStringAsync().Result;
                }
            }
        }