/// <summary> /// 保存公众账号 /// </summary> /// <param name="user"></param> /// <param name="account"></param> /// <param name="account_ent"></param> /// <param name="merchant"></param> /// <param name="imgmsg"></param> /// <returns></returns> public JsonResult SaveAccount(SysUser user, WeChatAccount account, WeChatAccountEnt account_ent, WeChatMerchant merchant, string imgmsg) { //微信公众号类型验证 if (account.type != WXAccountType.Subscription) { AccessToken token = WeChatBaseHelper.GetAccessToken(account.appid, account.app_secret); if (token.access_token == null) { return(Json(GetResult(StateCode.State_11001, token.errcode.ToString()))); } } StateCode state = ServiceIoc.Get <WeChatAccountService>().Save(user.id, account, account_ent, merchant, imgmsg); return(Json(GetResult(state))); }
/// <summary> /// 保存微信账号 /// </summary> /// <param name="user_id"></param> /// <param name="account"></param> /// <param name="account_ent"></param> /// <param name="merchant"></param> /// <param name="imgmsg"></param> /// <returns></returns> public StateCode Save(long user_id, WeChatAccount account, WeChatAccountEnt account_ent, WeChatMerchant merchant, string imgmsg) { using (ISession s = SessionFactory.Instance.CreateSession()) { try { s.StartTransaction(); WeChatAccount wx_account = s.Get <WeChatAccount>("where id != @0", 0); if (wx_account != null) { account.id = wx_account.id; account.updated_user_id = user_id; account.updated_date = DateTime.Now; s.Update <WeChatAccount>(account); } else { int rand; char code; string randomcode = String.Empty; //生成一定长度的ToKen System.Random random = new Random(); for (int i = 0; i < 8; i++) { rand = random.Next(); if (rand % 3 == 0) { code = (char)('A' + (char)(rand % 26)); } else { code = (char)('0' + (char)(rand % 10)); } randomcode += code.ToString(); } //设置账号ToKen account.token = randomcode; account.created_user_id = user_id; account.created_date = DateTime.Now; s.Insert <WeChatAccount>(account); } //企业号信息 WeChatAccountEnt wx_account_ent = s.Get <WeChatAccountEnt>("where id > 0"); if (wx_account_ent == null) { account_ent.created_user_id = user_id; account_ent.created_date = DateTime.Now; s.Insert(account_ent); } else { account_ent.id = wx_account_ent.id; s.Update(account_ent); } WeChatMerchant mh = s.Get <WeChatMerchant>("where id > 0"); if (mh == null) { s.Insert <WeChatMerchant>(merchant); } else { merchant.id = mh.id; s.Update <WeChatMerchant>(merchant); } //判断是否存在图片信息 if (!string.IsNullOrEmpty(imgmsg) && imgmsg.IndexOf("#") != -1) { //图片名称 string filename = imgmsg.Split('#')[0]; //图片类型 string biztype = imgmsg.Split('#')[1]; //去除重复图片 s.ExcuteUpdate("update tb_img set biz_id = 0 where biz_type = @0 and biz_type = @1 ", biztype, wx_account.id); Img img = s.Get <Img>("where file_name = @0 and biz_type = @1 ", filename, biztype); if (img != null) { img.biz_id = wx_account.id; s.Update <Img>(img); } } s.Commit(); return(StateCode.State_200); } catch (Exception ex) { s.RollBack(); return(StateCode.State_500); } } }