/// <summary> /// 根据用户openId 创建或者更新新的二维码 /// </summary> /// <param name="openId"></param> /// <param name="appid"></param> /// <param name="activityName"></param> /// <param name="activityid"></param> /// <returns></returns> public ActionResult CreateOrUpdateQrCodeByOpenId(string openId, string wechatappid, string activityName, string activityid, string isUpdate) { if (!VerifyParam("openId,wechatappid,activityName,activityid,isUpdate")) { return(ErrMsg()); } int newAppid = System.Convert.ToInt32(wechatappid); int newactivityId = System.Convert.ToInt32(activityid); try { var config = WeChatCommonService.GetWeChatConfigByID(newAppid); var user = _WechatMPUserService.Repository.Entities.Where(u => u.OpenId.Equals(openId, StringComparison.CurrentCultureIgnoreCase) && u.AccountManageId == config.AccountManageId && u.IsCanceled == false).FirstOrDefault(); var qrcode = _QrCodeService.Repository.Entities.Where(q => q.RelatedUserId == user.Id); QrCodeMPItem newqr = new QrCodeMPItem(); if (qrcode.Any()) { newqr = qrcode.FirstOrDefault(); UpdateQRCode(newqr, newAppid, isUpdate); } else { newqr.AppId = newAppid; newqr.Description = activityName; newqr.CreatedDate = DateTime.Now; newqr.Deleted = false; newqr.CreatedUserID = User.Identity.Name; newqr.UpdatedDate = DateTime.Now; newqr.UpdatedUserID = User.Identity.Name; newqr.RelatedUserId = user.Id; _QrCodeService.Repository.Insert(newqr); ///添加临时二维码时 先插入数据获取当前记录id ///目前商定 所有活动用二维码 由10位数字组成 前三位代表活动id,后7位代表活动用二维码id。 /// newqr.SceneId = newactivityId * 10000000 + newqr.Id; //var config = WeChatCommonService.GetWeChatConfigByID(newAppid); ///临时用二维码 过期时间为2592000秒 30天 ///x向腾讯申请二维码 并获取二维码url var result = QrCodeApi.Create(config.WeixinAppId, config.WeixinCorpSecret, 2592000, (int)newqr.SceneId); if (result.errcode == Weixin.ReturnCode.请求成功) { var qrurl = QrCodeApi.GetShowQrCodeUrl(result.ticket); var userinfo = Innocellence.Weixin.MP.AdvancedAPIs.UserApi.Info(config.WeixinCorpId, config.WeixinCorpSecret, openId); string userHeadUrl = userinfo.headimgurl; newqr.Url = CombineQrCodeAndHeadImg(userHeadUrl, qrurl); } ///重新更新当前model _QrCodeService.Repository.Update(newqr); } return(Json(new { QrcodeUrl = newqr.Url, SceneId = newqr.SceneId, Status = 200, UpdatedDate = newqr.UpdatedDate }, JsonRequestBehavior.AllowGet)); } catch (Exception e) { _Logger.Error(e); } return(Json(new { Status = 400 }, JsonRequestBehavior.AllowGet)); }
public void UpdateQRCode(QrCodeMPItem thisqr, int newappid, string isUpdate) { if (thisqr.UpdatedDate.Value.AddMonths(1) < DateTime.Now || isUpdate.Equals("true")) { var config = WeChatCommonService.GetWeChatConfigByID(newappid); var result = QrCodeApi.Create(config.WeixinAppId, config.WeixinCorpSecret, 2592000, (int)thisqr.SceneId); if (result.errcode == Weixin.ReturnCode.请求成功) { var user = _WechatMPUserService.Repository.Entities.Where(u => u.Id == thisqr.RelatedUserId); if (user.Any()) { var userinfo = Innocellence.Weixin.MP.AdvancedAPIs.UserApi.Info(config.WeixinCorpId, config.WeixinCorpSecret, user.FirstOrDefault().OpenId); string userHeadUrl = userinfo.headimgurl; var qrurl = QrCodeApi.GetShowQrCodeUrl(result.ticket); thisqr.Url = CombineQrCodeAndHeadImg(userHeadUrl, qrurl); thisqr.UpdatedDate = DateTime.Now; } } ///重新更新当前model _QrCodeService.Repository.Update(thisqr); } }