/// <summary>
        /// 添加粉丝到数据库
        /// </summary>
        /// <param name="mpid"></param>
        /// <param name="openid"></param>
        /// <returns></returns>
        private async Task <MpFanDto> AddFan(int mpid, string openid)
        {
            var account = await _mpAccountAppService.GetCache(mpid);

            if (account == null)
            {
                return(null);
            }
            try
            {
                UserInfoJson wxinfo = null;
                try
                {
                    wxinfo = UserApi.Info(await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret), openid);
                }
                catch
                {
                    Logger.Error(string.Format("获取MpID为{0},openid为{1}的用户信息报错", mpid, openid));
                    wxinfo = UserApi.Info(await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret, true), openid);
                }
                if (wxinfo.errcode != ReturnCode.请求成功)
                {
                    throw new Exception(string.Format("获取MpID为{0},openid为{1}的用户信息报错,错误编号:{2},错误消息:{3}", mpid, openid, wxinfo.errcode, wxinfo.errmsg));
                }

                var entityfans = new MpFanDto();
                entityfans.City               = wxinfo.city;
                entityfans.Country            = wxinfo.country;
                entityfans.HeadImgUrl         = wxinfo.headimgurl;
                entityfans.IsFans             = true;
                entityfans.Language           = wxinfo.language;
                entityfans.MpID               = mpid;
                entityfans.NickName           = wxinfo.nickname;
                entityfans.OpenID             = wxinfo.openid;
                entityfans.Province           = wxinfo.province;
                entityfans.Remark             = wxinfo.remark;
                entityfans.Sex                = wxinfo.sex.ToString();
                entityfans.SubscribeTime      = DateTimeHelper.GetDateTimeFromXml(wxinfo.subscribe_time);
                entityfans.UnionID            = wxinfo.unionid;
                entityfans.WxGroupID          = wxinfo.groupid;
                entityfans.UpdateTime         = DateTime.Now;
                entityfans.FirstSubscribeTime = DateTime.Now;
                entityfans.ChannelID          = 0;
                entityfans.ChannelName        = "公众号";
                return(await _mpFanAppService.Create(entityfans));
            }
            catch (Exception ex)
            {
                Logger.Error(string.Format("粉丝订阅更新数据库失败,原因:{0}", ex.Message));
            }
            return(null);
        }
Esempio n. 2
0
 public CreateOrEditMpFanViewModel(MpFanDto output)
 {
     output.MapTo(this);
 }
        public async Task SanCodePushMessageByActivity(int source, MpFanDto fan, MpUserMemberDto userMember, string openId, bool isSubscribe = true)
        {
            try
            {
                if (source > 110000)//网格化活动扫码签到(妈妈班/嘉年华)
                {
                    _logger.Info("进入网格化推送,sourceID:" + source);
                    string activityName = "本次";
                    int    activityID   = source - 110000;
                    _logger.Info("进入活动接口判定=================================================");
                    ActivityInfo activity = await GetActivity(activityID);

                    if (activity != null)
                    {
                        _logger.Info("活动接口判定结果为:" + JsonConvert.SerializeObject(activity));
                        if (fan != null && fan.MemberID > 0)//已绑定用户
                        {
                            _logger.Info("开始获取会员authkey");
                            string authKey = await GetUserMemberAuthKey(userMember.MemeberUserName, userMember.MemberPassword);//获取会员AuthKey

                            _logger.Info("获取会员authkey成功:" + authKey);
                            _logger.Info("开始获取会员信息,调用美驰接口");
                            MemberTemp serviceMember = await GetServiceMember(authKey);

                            _logger.Info("被动接口-获取会员信息为:" + JsonConvert.SerializeObject(serviceMember));
                            int _crmid = Convert.ToInt32(userMember.CRMID);
                            if (activity.Classify == 1)//妈妈班  1
                            {
                                _logger.Info("妈妈班活动");
                                _logger.Info($"妈妈班活动会员信息:{JsonConvert.SerializeObject(serviceMember)}");
                                if (serviceMember != null && !string.IsNullOrWhiteSpace(serviceMember.ServiceCMClientCode))//已绑定门店
                                {
                                    _logger.Info("已绑定门店");
                                    if (serviceMember.InfoCollectActivity == 0)
                                    {
                                        _logger.Info("开始更新会员信息,调用美驰接口:活动id:" + activityID);
                                        serviceMember.InfoCollectActivity = activityID;
                                        await UpdateMemberInfoJson(authKey, serviceMember);

                                        _logger.Info("更新成功");
                                    }
                                    _logger.Info("开始活动签到");
                                    int ret = await JoinActivitySign(_crmid, activityID, 51, userMember);

                                    _logger.Info($",签到活动:{activityID},签到返回数据:{ret}");
                                    string msg = "恭喜您签到成功!";
                                    if (ret > 0)
                                    {
                                        msg = "恭喜您签到成功!";
                                    }
                                    else if (ret == 0)
                                    {
                                        msg = "您已经签到过本次活动了哦!";
                                    }
                                    _logger.Info(msg);

                                    if (!string.IsNullOrEmpty(serviceMember.ServicePromotorName))
                                    {
                                        //恭喜您签到成功!您的专属营养顾问是张三(华润万达广场店)联系电话:13312345678。
                                        msg += string.Format("您的专属营养顾问是{0}({1})联系电话:{2}。", serviceMember.ServicePromotorName, serviceMember.ServiceCMClientName, serviceMember.ServicePromotorMobile);
                                    }
                                    if (isSubscribe)
                                    {
                                        _logger.Info("开始推送消息,美驰接口推送");
                                        await SendMessage(openId, msg, false);
                                    }
                                    else
                                    {
                                        await SendCustomerMsg(openId, msg);
                                    }

                                    //孕晚期推送
                                    DateTime?babyBirthday = userMember.BabyBirthday;
                                    if (babyBirthday != null)
                                    {
                                        if (babyBirthday > DateTime.Now && babyBirthday < DateTime.Now.AddDays(105))
                                        {
                                            _logger.Info("开始固定推送");
                                            await SendCustomerImage(openId, MamaBanToYun2OneMediaID);

                                            //推送固定
                                        }
                                    }
                                }
                                else
                                {
                                    _logger.Info("未绑定门店");
                                    activityName = activity.Topic;
                                    _logger.Info("活动标题:" + activityName);
                                    //string _str = "欢迎参加" + activityName + "活动,完成签到后将有专属营养顾问为您服务,并有礼品相赠哦~~\n";
                                    string _str = "欢迎参加" + activityName + "活动,完成签到后将有专属营养顾问为您服务,并有礼品相赠哦~~请点此进行签到!";

                                    ///////////////////////////有疑问,需要更换地址
                                    var    reurl = Base64Helper.EncodeBase64($"{wxOnlineUrl}?qd=inmm");
                                    string _url  = $"{auth2Url}?token={_account.TaskAccessToken}&needRegister=1&reurl={reurl}&wghhd=inmm&ActivityId={activityID}&Classify={activity.Classify}&DefaultClient={activity.DefaultClient}&DefaultClientCode={activity.DefaultClientCode}&DefaultClientFullName={activity.DefaultClientFullName}&OfficialCity={activity.OfficialCity}&response_type=code&scope=snsapi_base&state=state#wechat_redirect";

                                    await SendLinkMessage(openId, _url, _str, false);
                                }
                            }
                            else //嘉年华  2
                            {
                                _logger.Info("嘉年华活动");

                                if (serviceMember != null && serviceMember.InfoCollectActivity == 0)
                                {
                                    _logger.Info("开始更新会员信息,调用美驰接口:活动id:" + activityID);
                                    serviceMember.InfoCollectActivity = activityID;
                                    await UpdateMemberInfoJson(authKey, serviceMember);

                                    _logger.Info("更新成功");
                                }
                                _logger.Info("开始活动签到");
                                int ret = await JoinActivitySign(_crmid, activityID, 51, userMember);

                                string msg = "恭喜您签到成功!";
                                if (ret > 0)
                                {
                                    msg = "恭喜您签到成功!";
                                }
                                else if (ret == 0)
                                {
                                    msg = "您已经签到过本次活动了哦!";
                                }
                                else if (ret == -11)
                                {
                                    msg = "您当前参与的活动尚未结束,无法参加其他嘉年华活动哦!";
                                }
                                else if (ret == -12)
                                {
                                    msg = "您本月参与嘉年华活动次数已达上限了哦!";
                                }
                                _logger.Info(msg);
                                if (isSubscribe)
                                {
                                    _logger.Info("开始推送消息,美驰接口推送");
                                    await SendMessage(openId, msg, false);
                                }
                                else
                                {
                                    await SendCustomerMsg(openId, msg);

                                    //wt.SimpleTextSender(requestMsg.FromUserName, msg);
                                }

                                if (ret > 0)//仅这对成功签到
                                {
                                    await SendCustomerImage(openId, JNHToLongLineMediaID);

                                    //wt.SendImageMsg(requestMsg.FromUserName, JNHToLongLineMediaID);
                                }
                            }
                        }
                        else//未绑定用户
                        {
                            _logger.Info("MCServiceHander活动接口--非会员入口");
                            _logger.Info("=====================================================================================================================================================================================================");
                            string _url = "";
                            _logger.Info($"活动类型:{activity.Classify}");

                            string _str = "请点此注册签到!";
                            if (activity.Classify == 1) //妈妈班  1
                            {
                                var reurl = Base64Helper.EncodeBase64($"{wxOnlineUrl}?qd=unmm");
                                _url = $"{auth2Url}?token={_account.TaskAccessToken}&needRegister=1&reurl={reurl}&wghhd=unmm&ActivityId={activityID}&Classify={activity.Classify}&DefaultClient={activity.DefaultClient}&DefaultClientCode={activity.DefaultClientCode}&DefaultClientFullName={activity.DefaultClientFullName}&OfficialCity={activity.OfficialCity}&response_type=code&scope=snsapi_base&state=state#wechat_redirect";
                                //欢迎参加朝阳妇幼保健院妈妈班(emp中录入的活动名称)活动,完成签到后将有专属营养顾问为您服务,并有礼品相赠哦~~请点此进行签到!
                                activityName = activity.Topic;
                                //_str = "欢迎参加" + activityName + "活动,完成签到后将有专属营养顾问为您服务,并有礼品相赠哦~~\n";
                                //_str += "<a href='" + _url + "'>请点此进行签到!</a>";
                                _str = "欢迎参加" + activityName + "活动,完成签到后将有专属营养顾问为您服务,并有礼品相赠哦~~请点此进行签到!";
                            }
                            else //嘉年华  2
                            {
                                var reurl = Base64Helper.EncodeBase64($"{wxOnlineUrl}?qd=injnh");
                                _url = $"{auth2Url}?token={_account.TaskAccessToken}&needRegister=1&reurl={reurl}&wghhd=injnh&ActivityId={activityID}&Classify={activity.Classify}&DefaultClient={activity.DefaultClient}&DefaultClientCode={activity.DefaultClientCode}&DefaultClientFullName={activity.DefaultClientFullName}&OfficialCity={activity.OfficialCity}&response_type=code&scope=snsapi_base&state=state#wechat_redirect";
                                //欢迎参加大润发金领冠嘉年华(emp中录入的活动名称)活动,完成签到后有机会获得礼品哦~~请点此进行签到!
                                activityName = activity.Topic;
                                //_str = "欢迎参加" + activityName + "活动,完成签到后有机会获得礼品哦~~\n";
                                //_str += "<a href='" + _url + "'>请点此进行签到!</a>";
                                _str = "欢迎参加" + activityName + "活动,完成签到后有机会获得礼品哦~~请点此进行签到!";

                                _logger.Info($"反推地址:{_url}");
                            }

                            await SendLinkMessage(openId, _url, _str, false);
                        }
                    }
                    else
                    {
                        _logger.Info("活动接口判定结果为空值");
                    }
                }
                else if (source > 100) //营养顾问帮注册扫码
                {
                    _logger.Info("进入营销顾问推送");
                    if (fan != null && fan.MemberID > 0)
                    {
                        _logger.Info("会员推送");
                        await SendCustomerMsg(openId, "亲爱的" + fan.NickName + ",您已经绑定会员啦,不要重复注册哦~");
                    }
                    else
                    {
                        try
                        {
                            _logger.Info("非会员推送,开始生成二维码");
                            string result = await client.GetPromotorInfoAsync(source);//根据顾问ID生成的二维码

                            _logger.Info("二维码内容:" + result);
                            if (!string.IsNullOrEmpty(result))
                            {
                                _logger.Info("有结果,结果为:" + result);
                                string[] _retlist    = result.Split(new char[] { '|' });
                                string   _assigntype = _retlist[2];
                                _logger.Info("_assigntype内容:" + _assigntype);
                                if (_assigntype == "2")
                                {
                                    _logger.Info("2的推送");
                                    string _str  = "亲爱的会员," + _retlist[1] + "营养顾问正在帮您注册/绑定会员,请继续扫描营养顾问APP“绑定码”或点此完善您的信息,以便获得六大会员专属服务!";
                                    var    reurl = Base64Helper.EncodeBase64($"{wxOnlineUrl}?qd=app");
                                    string _url  = $"{auth2Url}?token={_account.TaskAccessToken}&needRegister=1&reurl={reurl}&response_type=code&scope=snsapi_base&state=state#wechat_redirect";

                                    await SendLinkMessage(openId, _url, _str, false);
                                }
                                else if (_assigntype == "3")
                                {
                                    _logger.Info("3的推送");
                                    string _str  = "亲爱的会员,营养代表(" + _retlist[1] + ")正在帮您注册/绑定会员,点此完善您的信息,以便获得六大会员专属服务!";
                                    var    reurl = Base64Helper.EncodeBase64($"{wxOnlineUrl}?qd=app");
                                    string _url  = $"{auth2Url}?token={_account.TaskAccessToken}&needRegister=1&reurl={reurl}&response_type=code&scope=snsapi_base&state=state#wechat_redirect";
                                    await SendLinkMessage(openId, _url, _str, false);
                                }
                            }
                            _logger.Info("取结果结束。");
                        }
                        catch (Exception eex)
                        {
                            _logger.Error($"错误原因:{eex.Message},错误堆栈:{eex.StackTrace}");
                            throw eex;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error($"活动扫码/关注处理异常:错误信息:{ex.Message},错误堆栈:{ex.StackTrace}");
            }
        }