Ejemplo n.º 1
0
        public async Task <IActionResult> SaveAndUpdate(CustomerServiceOnlineDto input)
        {
            CustomerServiceOnlineDto result = null;

            input.LastModificationTime = DateTime.Now;
            input.KfAccount            = input.PreKfAccount + "@" + input.PublicNumberAccount;
            string fileUrl = string.Empty;

            if (Request.Form.Files.Count > 0)
            {
                var    profilePictureFile = Request.Form.Files.First();
                byte[] fileBytes;
                using (var stream = profilePictureFile.OpenReadStream())
                {
                    fileBytes = stream.GetAllBytes();
                }
                var fileInfo = new FileInfo(profilePictureFile.FileName);
                var extra    = fileInfo.Extension.Substring(fileInfo.Extension.IndexOf(".") + 1);
                fileUrl = await _fileServer.UploadFile(fileBytes, extra, "CustomerServiceOnline");

                input.LocalHeadingUrl = fileUrl;
                input.KfHeadingUrl    = fileUrl;
                var filename = fileUrl.Substring(fileUrl.LastIndexOf("/") + 1);
            }

            Logger.Info($"客服信息上传开始:客服账号:{input.KfAccount},客服昵称:{input.KfNick},密码:{input.KfPassWord},id={input.Id}");
            if (input.KfType == KFType.WX.ToString())
            {
                //上传客服信息
                if (input.Id == 0)
                {
                    await _wxMediaAppService.AddCustom(input.MpID, input.KfAccount, input.KfNick, input.KfPassWord, input.KfWx);
                }
                else
                {
                    await _wxMediaAppService.UpdateCustom(input.MpID, input.KfAccount, input.KfNick, input.KfPassWord);
                }

                if (Request.Form.Files.Count > 0)
                {
                    Logger.Info($"客服信息上传结束,开始上传客服头像,头像url:{fileUrl}");
                    //上传客服头像
                    await _wxMediaUpload.UploadKfHeading(input.MpID, fileUrl, input.KfAccount);
                }
            }
            else
            {
                input.KfType = KFType.YL.ToString();
            }

            if (input.Id == 0)
            {
                await _CustomerServiceOnlineAppService.Create(input);
            }
            else
            {
                await _CustomerServiceOnlineAppService.Update(input);
            }
            return(Json(""));
        }
 public CreateOrEditCustomerServiceOnlineViewModel(CustomerServiceOnlineDto output)
 {
     output.MapTo(this);
 }
        public async Task DoExecute(FansMessageDto args)
        {
            var faninfo = await GetFan(args.MpID, args.OpenID);

            if (faninfo != null)
            {
                Logger.Info($"开始调用客服接入机制");
                CustomerServiceOnlineDto customer = null;
                if (faninfo.LastCustomerServiceId != 0)
                {
                    customer = await _customerServiceOnlineAppService.GetLastCustomerService(faninfo.LastCustomerServiceId);//获取末次接待客服
                }
                if (customer == null)
                {
                    customer = await _customerServiceOnlineAppService.GetAutoJoinCustomer(args.MpID); //获取自动分配客服
                }
                if (customer == null)                                                                 //微客服坐席已满,进入等待队列
                {
                    Logger.Info($"进入等待队列,开始创建等待会话");
                    var conversation = await _customerServiceConversationAppService.Create(new CustomerServiceConversationDto()
                    {
                        FanOpenId  = args.OpenID,
                        FanId      = faninfo.Id,
                        MpID       = args.MpID,
                        State      = (int)CustomerServiceConversationState.Wait,
                        HeadImgUrl = faninfo.HeadImgUrl,
                        NickName   = faninfo.NickName
                    });

                    Logger.Info($"进入等待队列,创建等待会话成功,开始记录缓存");
                    await _cacheManager.GetCache(AppConsts.Cache_FanOpenId2Conversation).SetAsync(args.OpenID, conversation);

                    //await _cacheManager.GetCache(AppConsts.Cache_Kf_FanOpenId2Conversation).SetAsync(args.OpenID, JsonConvert.SerializeObject(conversation));
                    await _cacheManager.GetCache(AppConsts.Cache_Kf_FanOpenId2Conversation).RemoveAsync(args.OpenID);

                    #region 推送消息给客服
                    Logger.Info($"进入等待队列,创建等待会话成功,记录缓存成功,推送等待人数");
                    Logger.Info($"推送url为{_signalrCustomerService.SetUnConnectNoticeUrl}");
                    WebClient wc = new WebClient();
                    await wc.UploadValuesTaskAsync(_signalrCustomerService.SetUnConnectNoticeUrl, new System.Collections.Specialized.NameValueCollection());

                    #endregion
                }
                else
                {
                    Logger.Info($"分配到自动接入客服:{JsonConvert.SerializeObject(customer)}");
                    #region 创建客服会话
                    var conversation = await _customerServiceConversationAppService.Create(new CustomerServiceConversationDto()
                    {
                        FanOpenId            = args.OpenID,
                        FanId                = faninfo.Id,
                        MpID                 = args.MpID,
                        State                = (int)CustomerServiceConversationState.Asking,
                        HeadImgUrl           = faninfo.HeadImgUrl,
                        NickName             = faninfo.NickName,
                        CustomerId           = customer.Id,
                        CustomerOpenId       = customer.OpenID,
                        StartTalkTime        = DateTime.Now,
                        LastModificationTime = DateTime.Now,
                        ConversationScore    = -1
                    });

                    faninfo.LastCustomerServiceId     = customer.Id;
                    faninfo.LastCustomerServiceOpenId = customer.OpenID;
                    await _mpFanAppService.Update(faninfo);

                    await _cacheManager.GetCache(AppConsts.Cache_FanOpenId2Conversation).SetAsync(args.OpenID, conversation);

                    //await _cacheManager.GetCache(AppConsts.Cache_Kf_FanOpenId2Conversation).SetAsync(args.OpenID, JsonConvert.SerializeObject(conversation));
                    await _cacheManager.GetCache(AppConsts.Cache_Kf_FanOpenId2Conversation).RemoveAsync(args.OpenID);

                    WebClient wc     = new WebClient();
                    var       values = new NameValueCollection();
                    values.Add("messageToken", customer.MessageToken);                          //消息令牌
                    values.Add("fanOpenId", conversation.FanOpenId);                            //粉丝openid
                    values.Add("fanNickName", faninfo.NickName);                                //粉丝昵称
                    values.Add("fanHeadImgUrl", faninfo.HeadImgUrl);                            //粉丝头像
                    values.Add("replyType", ((int)CustomerServiceReplyType.AutoIn).ToString()); //回复类型
                    values.Add("msgType", MpMessageType.text.ToString());                       //消息类型
                    values.Add("msgContent", "");                                               //文本消息
                    var result = await wc.UploadValuesTaskAsync(_signalrCustomerService.CustomerAskUrl, values);

                    #endregion
                }
            }
        }