using BP.Cloud.WeXinAPI; using BP.DA; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace CCFlow.App.Portal { public partial class DirectWeiXin : System.Web.UI.Page { public string GetVal(string key) { string val = this.Request.QueryString[key]; return BP.Tools.DealString.DealStr(val); } //httppost请求 BP.WF.HttpWebResponseUtility httpWebResponseUtility = new BP.WF.HttpWebResponseUtility(); protected void Page_Load(object sender, EventArgs e) { string code = HttpUtility.UrlDecode(HttpContext.Current.Request["code"] ?? string.Empty); string state = HttpUtility.UrlDecode(HttpContext.Current.Request["state"] ?? string.Empty); //获取第三方应用凭证 string suitAccessToken = BP.Cloud.WeXinAPI.Glo.getSuitAccessToken(); //20220615suitAccessToken为空时,重新获取 if (string.IsNullOrEmpty(suitAccessToken)) suitAccessToken = BP.Cloud.WeXinAPI.Glo.getSuitAccessToken(); string getUserUrl = "https://qyapi.weixin.qq.com/cgi-bin/service/getuserinfo3rd?suite_access_token=" + suitAccessToken + "&code=" + code; string res = BP.DA.DataType.ReadURLContext(getUserUrl); // httpWebResponseUtility.HttpResponseGet(getUserUrl); //解析返回的json串 JObject jobj = JObject.Parse(res); JToken resJtoken = jobj as JToken; //返回码 string errcode = Glo.JSON_SeleteNode(resJtoken, "errcode"); if (string.IsNullOrEmpty(errcode) == false && errcode.Equals("0") == false) { this.Response.Write("err@获取员工信息错误,错误码为:" + errcode + " URL=" + getUserUrl); // BP.DA.Log.DebugWriteError("err@获取员工信息错误,错误码为:" + errcode); return; } //用户所属企业的corpid string corpId = Glo.JSON_SeleteNode(resJtoken, "CorpId"); BP.Cloud.Org org = new BP.Cloud.Org(); org.Retrieve(BP.Cloud.OrgAttr.CorpID, corpId); //用户在企业内的UserID,如果该企业与第三方应用有授权关系时,返回明文UserId,否则返回密文UserId string userID = Glo.JSON_SeleteNode(resJtoken, "UserId"); //手机设备号(由企业微信在安装时随机生成,删除重装会改变,升级不受影响) string DeviceId = Glo.JSON_SeleteNode(resJtoken, "DeviceId"); //成员票据,最大为512字节。 scope为snsapi_userinfo或snsapi_privateinfo,且用户在应用可见范围之内时返回此参数。后续利用该参数可以获取用户信息或敏感信息,参见“第三方使用user_ticket获取成员详情”。 string user_ticket = Glo.JSON_SeleteNode(resJtoken, "user_ticket"); //user_ticket的有效时间(秒),随user_ticket一起返回 string expires_in = Glo.JSON_SeleteNode(resJtoken, "expires_in"); //全局唯一。对于同一个服务商,不同应用获取到企业内同一个成员的open_userid是相同的,最多64个字节。仅第三方应用可获取 string open_userid = Glo.JSON_SeleteNode(resJtoken, "open_userid"); BP.Web.WebUser.OrgNo = org.No; BP.WF.Dev2Interface.Port_Login(userID,org.No); //真实的ID. BP.Web.WebUser.No = userID; BP.Web.WebUser.OrgNo = org.No; BP.Web.WebUser.OrgName = org.Name; string doType = this.GetVal("DoType"); if (doType == null) doType = ""; string workid = this.GetVal("WorkID"); string flowNo = this.GetVal("FK_Flow"); //如果是手机端. if (this.IsMobile() == true) { if (doType.Equals("OpenWork") == true) { this.Response.Redirect("/CCMobile/MyFlow.htm?WorkID=" + workid + "&FK_Flow=" + flowNo + "&OrgNo=" + org.No + "&UserNo=" + userID, true); return; } this.Response.Redirect("/CCMobilePortal/Home.htm?OrgNo=" + org.No + "&UserNo=" + userID, true); return; } if (doType.Equals("OpenWork") == true) { this.Response.Redirect("/WF/MyFlow.htm?WorkID=" + workid + "&FK_Flow=" + flowNo + "&OrgNo=" + org.No + "&UserNo=" + userID, true); return; } string url = ""; url = "/App/Portal/Home.htm?UserID=" + BP.Web.WebUser.No + "&OrgNo=" + org.No; this.Response.Redirect(url, true); return; } private bool IsMobile() { HttpContext context = HttpContext.Current; if (context != null) { HttpRequest request = context.Request; return request.Browser.IsMobileDevice; } return false; } public void DoOF() { if (this.GetVal("DoType").Equals("OF")) { string sid = this.GetVal("Token"); //sid 格式为: guid+"_"+workid+"_"+empNo+"_"+nodeID; string[] strs = sid.Split('_'); string workID = strs[1]; string empNo = strs[2]; BP.WF.GenerWorkerList wl = new BP.WF.GenerWorkerList(); int i = wl.Retrieve(BP.WF.GenerWorkerListAttr.FK_Emp, empNo, BP.WF.GenerWorkerListAttr.WorkID, workID, BP.WF.GenerWorkerListAttr.IsPass, 0); if (i == 0) { this.Response.Write("info@此作已经被别人处理或者此流程已删除"); return; } BP.WF.GenerWorkFlow gwf = new BP.WF.GenerWorkFlow(wl.WorkID); //设置他的组织. BP.Web.WebUser.OrgNo = gwf.OrgNo; BP.Port.Emp empOF = new BP.Port.Emp(wl.FK_Emp); BP.Web.WebUser.SignInOfGener(empOF); string myurl = "/WF/MyFlow.htm?FK_Flow=" + wl.FK_Flow + "&WorkID=" + wl.WorkID + "&FK_Node=" + wl.FK_Node + "&FID=" + wl.FID + "&PWorkID=" + gwf.PWorkID; this.Response.Redirect(myurl, true); return; } } } }