From a14671b70dbd3dcef87f10db4ce3fe1a208dfd41 Mon Sep 17 00:00:00 2001 From: Hai Liang Wang Date: Mon, 25 Sep 2023 10:41:48 +0800 Subject: [PATCH] https://gitee.com/cskefu/cskefu/issues/I836RO enable contact as billing resource Signed-off-by: Hai Liang Wang --- .../com/cskefu/cc/aspect/ContactsAspect.java | 50 +++++++++++++++++++ .../controller/apps/ContactsController.java | 30 ++++++----- .../com/cskefu/cc/proxy/LicenseProxy.java | 49 +++++++++++++----- .../resources/templates/apps/contacts/add.pug | 5 -- .../templates/apps/contacts/index.pug | 10 +--- .../templates/apps/include/layout.pug | 1 + 6 files changed, 108 insertions(+), 37 deletions(-) create mode 100644 contact-center/app/src/main/java/com/cskefu/cc/aspect/ContactsAspect.java diff --git a/contact-center/app/src/main/java/com/cskefu/cc/aspect/ContactsAspect.java b/contact-center/app/src/main/java/com/cskefu/cc/aspect/ContactsAspect.java new file mode 100644 index 00000000..8c7f3e6b --- /dev/null +++ b/contact-center/app/src/main/java/com/cskefu/cc/aspect/ContactsAspect.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2023 Beijing Huaxia Chunsong Technology Co., Ltd. + * , Licensed under the Chunsong Public + * License, Version 1.0 (the "License"), https://docs.cskefu.com/licenses/v1.html + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.cskefu.cc.aspect; + +import com.cskefu.cc.basic.MainContext; +import com.cskefu.cc.basic.MainUtils; +import com.cskefu.cc.exception.BillingQuotaException; +import com.cskefu.cc.exception.BillingResourceException; +import com.cskefu.cc.model.Contacts; +import com.cskefu.cc.model.User; +import com.cskefu.cc.proxy.LicenseProxy; +import org.apache.commons.lang3.StringUtils; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Aspect +@Component +public class ContactsAspect { + + private final static Logger logger = LoggerFactory.getLogger(ContactsAspect.class); + + @Autowired + private LicenseProxy licenseProxy; + + @Before("execution(* com.cskefu.cc.persistence.repository.ContactsRepository.save(..))") + public void beforeSave(final JoinPoint joinPoint) throws BillingResourceException, BillingQuotaException { + final Contacts contacts = (Contacts) joinPoint.getArgs()[0]; + logger.info("[save] before contacts id {}", contacts.getId()); + if (StringUtils.isBlank(contacts.getId())) { + // 执行配额扣除 + licenseProxy.writeDownResourceUsageInStore(MainContext.BillingResource.CONTACT, 1); + contacts.setId(MainUtils.getUUID()); + } else { + // update existed user + } + } +} diff --git a/contact-center/app/src/main/java/com/cskefu/cc/controller/apps/ContactsController.java b/contact-center/app/src/main/java/com/cskefu/cc/controller/apps/ContactsController.java index ad9f4401..4b1c761d 100644 --- a/contact-center/app/src/main/java/com/cskefu/cc/controller/apps/ContactsController.java +++ b/contact-center/app/src/main/java/com/cskefu/cc/controller/apps/ContactsController.java @@ -1,21 +1,22 @@ /* - * Copyright (C) 2023 Beijing Huaxia Chunsong Technology Co., Ltd. - * , Licensed under the Chunsong Public + * Copyright (C) 2023 Beijing Huaxia Chunsong Technology Co., Ltd. + * , Licensed under the Chunsong Public * License, Version 1.0 (the "License"), https://docs.cskefu.com/licenses/v1.html * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * Copyright (C) 2018- Jun. 2023 Chatopera Inc, , Licensed under the Apache License, Version 2.0, + * Copyright (C) 2018- Jun. 2023 Chatopera Inc, , Licensed under the Apache License, Version 2.0, * http://www.apache.org/licenses/LICENSE-2.0 - * Copyright (C) 2017 优客服-多渠道客服系统, Licensed under the Apache License, Version 2.0, + * Copyright (C) 2017 优客服-多渠道客服系统, Licensed under the Apache License, Version 2.0, * http://www.apache.org/licenses/LICENSE-2.0 */ package com.cskefu.cc.controller.apps; import com.cskefu.cc.basic.MainUtils; import com.cskefu.cc.controller.Handler; +import com.cskefu.cc.exception.BillingQuotaException; import com.cskefu.cc.exception.CSKefuException; import com.cskefu.cc.model.*; import com.cskefu.cc.persistence.repository.*; @@ -47,8 +48,10 @@ import org.springframework.web.servlet.ModelAndView; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import jakarta.validation.Valid; + import java.io.File; import java.io.IOException; +import java.lang.reflect.UndeclaredThrowableException; import java.text.SimpleDateFormat; import java.util.*; @@ -255,13 +258,11 @@ public class ContactsController extends Handler { @RequestParam(name = "idselflocation", required = false) String selflocation) { final User logined = super.getUser(request); Organ currentOrgan = super.getOrgan(request); - String skypeIDReplace = contactsProxy.sanitizeSkypeId(contacts.getSkypeid()); String msg = ""; - Contacts contact = contactsRes.findByskypeidAndDatastatus(skypeIDReplace, false); // 添加数据 - if (contacts.getSkypeid() != null && contact == null) { - logger.info("[save] 数据库没有相同skypeid"); + try { + contacts.setId(null); contacts.setCreater(logined.getId()); if (currentOrgan != null && StringUtils.isBlank(contacts.getOrgan())) { @@ -274,11 +275,16 @@ public class ContactsController extends Handler { } contactsRes.save(contacts); msg = "new_contacts_success"; - - return request(super.createView( - "redirect:/apps/contacts/index.html?ckind=" + contacts.getCkind() + "&msg=" + msg)); + } catch (Exception e) { + if (e instanceof UndeclaredThrowableException) { + logger.error("[save] BillingQuotaException", e); + if (StringUtils.startsWith(e.getCause().getMessage(), BillingQuotaException.SUFFIX)) { + msg = e.getCause().getMessage(); + } + } else { + logger.error("[save] err", e); + } } - msg = "new_contacts_fail"; return request(super.createView( "redirect:/apps/contacts/index.html?ckind=" + contacts.getCkind() + "&msg=" + msg)); } diff --git a/contact-center/app/src/main/java/com/cskefu/cc/proxy/LicenseProxy.java b/contact-center/app/src/main/java/com/cskefu/cc/proxy/LicenseProxy.java index cfc722a8..6241c442 100644 --- a/contact-center/app/src/main/java/com/cskefu/cc/proxy/LicenseProxy.java +++ b/contact-center/app/src/main/java/com/cskefu/cc/proxy/LicenseProxy.java @@ -216,6 +216,7 @@ public class LicenseProxy { } } + /** * 生成随机字符串,作为服务名称 * @@ -407,6 +408,26 @@ public class LicenseProxy { } } + /** + * 获取在 MetaKv 中资源的已经使用的计数 + * + * @param billingResource + * @return + */ + private int getResourceUsageInMetaKv(final MainContext.BillingResource billingResource) { + final String key = getResourceUsageKey(billingResource.toString()); + try { + MetaKv kv = retrieveMetaKv(key); + int pre = Integer.parseInt(kv.getMetavalue()); + return pre; + } catch (MetaKvNotExistException e) { + createMetaKv(key, Integer.toString(0), Constants.METAKV_DATATYPE_INT); + return 0; + } catch (MetaKvInvalidKeyException e) { + return 0; + } + } + /** * 获得春松客服 cskefu001 产品的证书标识 ID * 春松客服证书基本类型 @@ -429,11 +450,20 @@ public class LicenseProxy { * 执行配额变更操作 * * @param billingResource - * @param consume + * @param unitNum * @return */ - public ExecuteResult writeDownResourceUsageInStore(final MainContext.BillingResource billingResource, int consume) throws BillingQuotaException, BillingResourceException { - ExecuteResult er = new ExecuteResult(); + public void writeDownResourceUsageInStore(final MainContext.BillingResource billingResource, + int unitNum) throws BillingQuotaException, BillingResourceException { + + // 检查是否还在体验阶段 + if (billingResource == MainContext.BillingResource.CONTACT) { + int alreadyUsed = getResourceUsageInMetaKv(billingResource); + if (alreadyUsed <= 1) { + // 可以免费创建 1 个联系人 + return; + } + } // 请求操作配额 String licenseId = getLicenseIdAsCskefu001InMetaKv(); @@ -442,13 +472,13 @@ public class LicenseProxy { try { Response resp = quotaWdClient.write(licenseId, - serverinstId, servicename, consume * BILLING_RES_QUOTA_MAPPINGS.get(billingResource)); + serverinstId, servicename, unitNum * BILLING_RES_QUOTA_MAPPINGS.get(billingResource)); // 识别操作是否完成,并处理 if (resp.getRc() == 0) { final JSONObject data = (JSONObject) resp.getData(); // 配额操作成功,执行计数 - increResourceUsageInMetaKv(billingResource, consume); + increResourceUsageInMetaKv(billingResource, unitNum); } else if (resp.getRc() == 1 || resp.getRc() == 2) { throw new BillingQuotaException(BillingQuotaException.INVALID_REQUEST_BODY); } else if (resp.getRc() == 3) { @@ -479,12 +509,7 @@ public class LicenseProxy { logger.error("[writeDownResourceUsageInStore] error ", e); throw new BillingQuotaException(BillingQuotaException.RESPONSE_UNEXPECTED); } - -// er.setRc(ExecuteResult.RC_ERR1); -// if (er.getRc() != ExecuteResult.RC_SUCC) { -// throw new BillingQuotaException(BillingQuotaException.NO_LICENSE_FOUND); -// } - - return er; } + + } diff --git a/contact-center/app/src/main/resources/templates/apps/contacts/add.pug b/contact-center/app/src/main/resources/templates/apps/contacts/add.pug index b4357527..51afbf63 100644 --- a/contact-center/app/src/main/resources/templates/apps/contacts/add.pug +++ b/contact-center/app/src/main/resources/templates/apps/contacts/add.pug @@ -79,11 +79,6 @@ include /mixins/dic.mixin.pug label.layui-form-label 电子邮件: .layui-input-inline(style='margin-left:5px;') input.layui-input(type='text', name='email', lay-verify='entemail', autocomplete='off') - .layui-form-item - .layui-inline - label.layui-form-label#contacts_skypeid(style='widht:80px;') Skype ID: - .layui-input-inline - input#skypeid.layui-input(type='text', name='skypeid', lay-verify='skypeid', autocomplete='off') .layui-form-item .layui-inline label.layui-form-label 联系人地址: diff --git a/contact-center/app/src/main/resources/templates/apps/contacts/index.pug b/contact-center/app/src/main/resources/templates/apps/contacts/index.pug index 827a4365..5c74889e 100644 --- a/contact-center/app/src/main/resources/templates/apps/contacts/index.pug +++ b/contact-center/app/src/main/resources/templates/apps/contacts/index.pug @@ -103,14 +103,6 @@ block content td #{contacts.ckind && uKeFuDic[contacts.ckind] ? uKeFuDic[contacts.ckind].name : ""} td #{contacts.user ? contacts.user.username : ""} td - if approachable.contains(contacts.id) - a(href="#", onclick="openDialogWinByContactid('" + contacts.id + "')") - i.layui-icon  - | 聊天 - else - a.disabled(href="#", onclick="unreachableDialogWinByContactid('" + contacts.id + "')") - i.layui-icon  - | 聊天 a(href="/apps/contacts/detail.pug?id=" + (contacts.id ? contacts.id : ""), style="margin-left:10px;") i.layui-icon  | 详情 @@ -186,6 +178,8 @@ block content layer.msg('联系人编辑成功', {icon: 1, time: 1500}) else if (msg && msg == 'edit_contacts_fail') layer.msg('联系人编辑失败,因为存在相同Skype ID', {icon: 2, time: 1500}) + else + handleGeneralCodeInQueryPathOrApiResp(msg); }); }); diff --git a/contact-center/app/src/main/resources/templates/apps/include/layout.pug b/contact-center/app/src/main/resources/templates/apps/include/layout.pug index a051e3fc..e22e5ec2 100644 --- a/contact-center/app/src/main/resources/templates/apps/include/layout.pug +++ b/contact-center/app/src/main/resources/templates/apps/include/layout.pug @@ -36,6 +36,7 @@ html(xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xm script(src="/js/select/js/i18n/zh-CN.js") script(src="/layui.js") script(src="/js/cskefu.js") + script(src="/js/CSKeFu_Admin.v1.js") script(type="text/javascript" src="/js/kindeditor/kindeditor.js") script(type="text/javascript" src="/js/kindeditor/lang/zh-CN.js") script(type="text/javascript" src="/js/kindeditor-suggest.js")