/// <summary> /// 组织发送模板消息内容 /// </summary> /// <param name="weixinApplet">小程序信息</param> /// <param name="templateCode">模板编码</param> /// <param name="openid">用户openid</param> /// <param name="form_id">表单id或支付prepay_id</param> /// <param name="data">发送数据,不同内容用;分隔</param> /// <param name="path">跳转路径</param> /// <returns></returns> public static string sendWeixinTemplate(weixin_applet weixinApplet, string templateCode, string openid, string form_id, string data, string path) { BaseBLL <weixin_applet_template> templateBll = new BaseBLL <weixin_applet_template>(); weixin_applet_template appletTemplate = templateBll.Find(x => x.template_code == templateCode && x.weixin_applet_id == weixinApplet.id); //组织发送的数据 string[] valueArray = data.Split(';'); JObject jobject = JObject.Parse("{}"); for (int i = 0; i < valueArray.Length; i++) { JObject subObject = new JObject( new JProperty("value", valueArray[i]), new JProperty("color", "#333") ); jobject.Add("keyword" + (i + 1), subObject); } WeixinXAPI weixinXApi = new WeixinXAPI(weixinApplet.appid, weixinApplet.secret, weixinApplet.access_token, weixinApplet.access_token_time.ToString(), weixinApplet.id); JObject postData = JObject.Parse("{}"); postData.Add("touser", openid); postData.Add("template_id", appletTemplate.template_id); postData.Add("page", path); postData.Add("form_id", form_id); postData.Add("data", jobject); return(weixinXApi.sendTemplate(postData.ToString())); }
public ApiResult WeixinLogin(dynamic data) { ApiResult apiResult = new ApiResult(); var checkResult = Util.CheckParameters( new Parameter { Value = data?.code, Msg = "code不能为空" }, new Parameter { Value = data?.userinfo.ToString(), Msg = "userInfo不能为空" }, new Parameter { Value = data?.appcode, Msg = "appcode不能为空!" } ); if (!checkResult.OK) { apiResult.success = false; apiResult.status = ApiStatusCode.InvalidParam; apiResult.message = checkResult.Msg; return(apiResult); } try { //根据code查找APPID与Secret,获取微信session、openid和unionid string appcode = data.appcode.ToString(); BaseBLL <weixin_applet> weixinAppletBll = new BaseBLL <weixin_applet>(); weixin_applet weixinApplet = weixinAppletBll.Find(x => x.appcode == appcode); WeixinXAPI weixinxapi = new WeixinXAPI(weixinApplet.appid, weixinApplet.secret); string str = weixinxapi.codeToSession(data.code.ToString()); JObject session_json = JObject.Parse(str); if (session_json["errcode"].To <string>().IsNotNullAndEmpty()) { apiResult.success = false; apiResult.status = ApiStatusCode.NotFound; apiResult.message = str; return(apiResult); } string openid = session_json["openid"].ToString(); string session_key = session_json["session_key"].ToString(); string unionid = session_json["unionid"].To <string>(); if (StringHelper.IsNullOrEmpty(unionid) && !StringHelper.IsNullOrEmpty(data?.encryptedData) && !StringHelper.IsNullOrEmpty(data?.iv)) { string info = DEncrypt.XCXDecrypt(data?.encryptedData.ToString(), session_key, data?.iv.ToString()); JObject userInfoJson = JObject.Parse(info); unionid = userInfoJson["unionId"].To <string>(); } weixin_user userInfo = new weixin_user { openid = openid, unionid = unionid, nickname = data.userinfo["nickName"], sex = data.userinfo["gender"], language = data.userinfo["language"], city = data.userinfo["city"], province = data.userinfo["province"], country = data.userinfo["country"], headimgurl = data.userinfo["avatarUrl"], source_code = appcode, weixin_applet_id = weixinApplet.id }; if (Util.isNotNull(userInfo.unionid)) { //查询当前openid的用户是否存在 //如果不存在则要创建,创建时,先创建 iuser ,再创建 weixin_user BaseBLL <weixin_user> weixinUserBll = new BaseBLL <weixin_user>(); var weixinUser = weixinUserBll.Find(x => x.unionid == unionid); bool first_login = false; //可能是第一次登陆,在网页端登陆 if (weixinUser == null) { //微信开发平台的openid与小程序的openid不一致 first_login = true; //先存iuser var iuser = new iuser(); BaseBLL <iuser> iuserBll = new BaseBLL <iuser>(); iuser.random = sys.getRandomStr(); iuser.createtime = DateTime.Now; iuser.updatetime = DateTime.Now; iuser = iuserBll.Add(iuser); //再存weixin_user userInfo.uid = iuser.id; userInfo.sub_time = DateTime.Now; userInfo.first_sub_time = DateTime.Now; LogHelper.Info("first_login:"******",userInfo:" + Newtonsoft.Json.JsonConvert.SerializeObject(userInfo)); weixinUser = weixinUserBll.Add(userInfo); } else { weixinUser.nickname = userInfo.nickname; weixinUser.headimgurl = userInfo.headimgurl; LogHelper.Info("first_login:"******",userInfo:" + Newtonsoft.Json.JsonConvert.SerializeObject(userInfo)); weixinUserBll.Update(weixinUser); } apiResult.success = true; apiResult.data = new { first_login = first_login, weixinUser = weixinUser }; apiResult.status = ApiStatusCode.OK; } else { return(new ApiResult() { success = false, message = "unionid不能为空,小程序必须绑定开放平台" }); } } catch (Exception ex) { LogHelper.Error(ex.Message, ex); apiResult.success = false; apiResult.status = ApiStatusCode.Error; } return(apiResult); }
/// <summary> /// 生成小程序码(圆形) /// </summary> /// <param name="path">地址</param> /// <param name="appcode">小程序标识</param> /// <returns></returns> public static string CreateCircleWxaCodeByPath(string path, string appcode) { string img_path = string.Empty; BaseBLL <weixin_applet> appBLL = new BaseBLL <weixin_applet>(); weixin_applet app = appBLL.Find(x => x.appcode == appcode); string code_name = app.applet_name; string code_desc = $"{app.applet_name}小程序码"; //1、进入首先记录调用情况 //2、根据path去搜索,如果找到则返回没找到继续执行 //3、生成小程序图片上传到服务器 //4、存入数据库 #region 记录调用情况 BaseBLL <weixin_applet_log> logBLL = new BaseBLL <weixin_applet_log>(); weixin_applet_log log = new weixin_applet_log(); log.appcode = appcode; log.title = "后台生成小程序码WeiXinHelper.CreateCircleWxaCodeByPath"; log.ip = Util.GetUserIp(); log.createtime = DateTime.Now; log.request_url = System.Web.HttpContext.Current.Request.RawUrl; log.info = $"接口参数为:path:{path},appcode:{appcode}"; logBLL.Add(log); #endregion //2、根据path去搜索,如果找到则返回没找到继续执行 BaseBLL <weixin_applet_code> appCodeBLL = new BaseBLL <weixin_applet_code>(); weixin_applet_code appletCode = appCodeBLL.Find(x => x.xcx_url == path && x.weixin_applet_id == app.id && x.code_type == 2); if (appletCode != null) { img_path = appletCode.img_path; } else { //3、生成小程序图片上传到服务器 WeixinXAPI weixinApi = new WeixinXAPI(app.appid, app.secret, app.access_token, app.access_token_time?.ToString() ?? "", app.id); if (weixinApi.rootSuccess) { string imgpath = weixinApi.CreateWxaCode(path); if (!string.IsNullOrEmpty(imgpath)) { //4、存入数据库 #region 存入数据库 var newAppCode = new weixin_applet_code { code_name = code_name, code_desc = code_desc, weixin_applet_id = app.id, create_time = DateTime.Now, random = sys.getRandomStr(), img_path = imgpath, is_delete = 0, xcx_url = path, code_type = 2 }; appCodeBLL.Add(newAppCode); #endregion } img_path = imgpath; } } return(img_path); }