Skip to content

Commit

Permalink
添加 html loader,适配 Tpl.html 后缀
Browse files Browse the repository at this point in the history
添加 alias 作为 require 根路径
新增 getArticleJson 接口
新增 popupList 组件
utils.sameProtocol 兼容只有根路径的情况
css 兼容新旧两版 tenantInfo
新版 tenantInfo 独立目录
  • Loading branch information
dcw1123 committed Apr 25, 2018
1 parent ec575fb commit 529c609
Show file tree
Hide file tree
Showing 11 changed files with 410 additions and 29 deletions.
43 changes: 29 additions & 14 deletions src/js/app/modules/apiHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,6 @@ function getConfig(configId){
}, function(err){
reject(err);
});


});
}
function getProjectId(){
Expand Down Expand Up @@ -1112,12 +1110,12 @@ function createWechatImUser(openId){
emajax({
url: "/v1/webimplugin/visitors/wechat/"
+ [
config.tenantId,
config.orgName,
config.appName,
config.toUser,
openId,
].join("_")
config.tenantId,
config.orgName,
config.appName,
config.toUser,
openId,
].join("_")
+ "?tenantId=" + config.tenantId,
data: {
orgName: config.orgName,
Expand Down Expand Up @@ -1204,12 +1202,12 @@ function updateCustomerInfo(data){
]).then(function(result){
var visitorId = result[0];
var token = result[1];
data["visitorId"] = visitorId;
data["tenantId"] = config.tenantId;
data["orgName"] = config.orgName;
data["appName"] = config.appName;
data["userName"] = config.user.username;
data["token"] = token;
data.visitorId = visitorId;
data.tenantId = config.tenantId;
data.orgName = config.orgName;
data.appName = config.appName;
data.userName = config.user.username;
data.token = token;
api("updateCustomerInfo", data, function(msg){
// resolve(msg.data);
}, function(err){
Expand All @@ -1219,6 +1217,22 @@ function updateCustomerInfo(data){
});
}

function getArticleJson(data){
return new Promise(function(resolve, reject){
api("getArticleJson", {
media_id: data.media_id,
tenantId: config.tenantId,
userId: config.user.userName,
orgName: config.orgName,
appName: config.appName,
token: 0,
}, function(ret){
var articles = utils.getDataByPath(ret, "data.entity.articles");
resolve(articles);
});
});
}

module.exports = {
getCurrentServiceSession: getCurrentServiceSession,
getToken: getToken,
Expand Down Expand Up @@ -1267,6 +1281,7 @@ module.exports = {
getCustomEmojiFiles: getCustomEmojiFiles,
getSatisfactionTipWord: getSatisfactionTipWord,
updateCustomerInfo: updateCustomerInfo,
getArticleJson: getArticleJson,

initApiTransfer: initApiTransfer,
api: api,
Expand Down
31 changes: 22 additions & 9 deletions src/js/app/modules/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ var initGetGreetings = require("./chat/initGetGreetings");
var initAgentNicknameUpdate = require("./chat/initAgentNicknameUpdate");
var emojiPanel = require("./chat/emojiPanel");
var extendMessageSender = require("./chat/extendMessageSender");
var TenantInfo = require("@/app/modules/tenantInfo/index");
var tenantInfo;

var isMessageChannelReady;
var config;
Expand Down Expand Up @@ -165,21 +167,28 @@ function _setLogo(){
function _setNotice(){
var noticeContent = document.querySelector(".em-widget-tip .content");
var noticeCloseBtn = document.querySelector(".em-widget-tip .tip-close");

apiHelper.getNotice().then(function(notice){
if(!notice.enabled) return;
var slogan = notice.content;
if(!notice.enabled) return;

// 设置信息栏内容
noticeContent.innerHTML = WebIM.utils.parseLink(slogan);
// 显示信息栏
utils.addClass(doms.imChat, "has-tip");

// 隐藏信息栏按钮
utils.on(noticeCloseBtn, utils.click, function(){
// 隐藏信息栏
utils.removeClass(doms.imChat, "has-tip");
});
// 新配置就走新 tenantInfo
if(config.isWebChannelConfig){
tenantInfo = new TenantInfo();
}
else{
(function(){
// 设置信息栏内容
noticeContent.innerHTML = WebIM.utils.parseLink(slogan);
// 隐藏信息栏按钮
utils.on(noticeCloseBtn, utils.click, function(){
// 隐藏信息栏
utils.removeClass(doms.imChat, "has-tip");
});
})();
}
});
}

Expand Down Expand Up @@ -423,6 +432,7 @@ function _bindEvents(){
var iframe = articleContainer.querySelector("iframe");
iframe && utils.removeDom(iframe);
articleContainer.style.display = "none";
tenantInfo && tenantInfo.show();
});

utils.live(".article-link", "click", function(e){
Expand All @@ -444,6 +454,9 @@ function _bindEvents(){
else{
window.open(url);
}

// 隐藏整个 tenantInfo
tenantInfo && tenantInfo.hide();
});

var messagePredict = _.throttle(function(msg){
Expand Down
195 changes: 195 additions & 0 deletions src/js/app/modules/tenantInfo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
var apiHelper = require("@/app/modules/apiHelper");
var PopupList = require("@/app/modules/uikit/popupList");
var utils = require("@/common/utils");
var channel = require("@/app/modules/channel");
var tpl = require("./template/indexTpl.html");

module.exports = function(){
// 二级菜单实例
var menuInsArr = [];
var menuArr = [];
var container = document.querySelector(".em-widget-tip");
container.innerHTML = "";
utils.addClass(container, "new");

apiHelper.getNotice()
.then(function(notice){
if(!notice.enabled) return;



// test
// notice.content = [
// {
// name: "菜单",
// sub_button: [
// {
// type: "view",
// name: "搜索2666",
// url: "http://www.soso.com/"
// }
// ]
// },
// {
// name: "222",
// sub_button: [
// {
// type: "media_id",
// name: "111",
// media_id: "75cffa4b-e462-40e8-a517-0ff807db29a6"
// },
// {
// type: "media_id",
// name: "香格里拉",
// media_id: "4150c891-9917-4482-909c-ab7c9954110a"
// }
// ]
// },
// {
// type: "media_id",
// name: "333",
// media_id: "75cffa4b-e462-40e8-a517-0ff807db29a6"
// }
// ];




menuArr = notice.content;
container.innerHTML = _.template(tpl)({
menu: menuArr,
});
createMenu();
utils.live(".tip-btn", "click", onMenuClick, container);
});

// 菜单点击
function onMenuClick(e){
var menuId;
var menuDat;
var target = e.srcElement || e.target;
var targetBounding;
if(utils.hasClass(target.parentNode, "tip-btn")){
target = target.parentNode;
}
menuId = target.getAttribute("menuId");
menuDat = menuArr[menuId];

targetBounding = target.getBoundingClientRect();
handleMenuClick(menuDat, menuId, {
top: targetBounding.top + 48,
left: targetBounding.left + (target.clientWidth * 0.1),
width: target.clientWidth * 0.8,
});
// ie8
// 使得 popupList 可以自动关闭
e.stopPropagation();
return false;
}

function handleMenuClick(menuDat, menuId, pos){
// 有子菜单
if(menuDat.sub_button){
popupList(menuId, pos || {
left: 0,
top: 0,
width: 0,
});
}
// 无子菜单
else{
hideAllSubMenu();
fireMsg(menuDat);
}
}

function fireMsg(dat){
switch(dat.type){
// 发送 article
case "media_id":
fireArticle(dat);
break;
// 发送 url
case "view":
fireUrl(dat);
break;
// 发送 text
case "txt":
fireText(dat);
break;
default:
break;
}
}

function fireArticle(menuDat){
apiHelper.getArticleJson({
media_id: menuDat.media_id
}).then(function(articles){
articles = _.map(articles, function(article){
return {
title: article.title,
digest: article.digest,
description: article.digest,
// url: "/v1/webimplugin/tenants/" + article.tenantId + "/robot/article/html/" + article.articleId,
url: "/v1/Tenants/" + article.tenantId + "/robot/article/html/" + article.articleId,
thumbUrl: "/v1/Tenant/" + article.tenantId + "/MediaFiles/" + article.thumb_media_id,
picurl: "/v1/Tenant/" + article.tenantId + "/MediaFiles/" + article.thumb_media_id,
prop: article.prop,
};
});
channel.handleMessage(
{
ext: {
msgtype: {
articles: articles,
}
},
},
{
type: "article",
noPrompt: true
}
);
});
}

function fireText(menuDat){

}

function fireUrl(menuDat){

}

// 每个 btn 创建一个菜单
function createMenu(){
_.each(menuArr, function(menuDat, menuId){
menuInsArr[menuId] = new PopupList({
items: menuDat.sub_button,
reportClick: handleMenuClick,
});
});
}

// 弹出菜单
function popupList(menuId, pos){
hideAllSubMenu();
menuInsArr[menuId].show(pos);
}

function hideAllSubMenu(){
_.each(menuInsArr, function(menuIns){
menuIns.hide();
});
}

return {
show: function(){
container.style.display = "block";
},
hide: function(){
container.style.display = "none";
}
};
};
6 changes: 6 additions & 0 deletions src/js/app/modules/tenantInfo/template/indexTpl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<% _.each(menu, function(itm, menuId){ %>
<div class="tip-btn" menuId="<%= menuId %>">
<i class="icon-list"></i>
<span><%= itm.name %></span>
</div>
<% }) %>
50 changes: 50 additions & 0 deletions src/js/app/modules/uikit/popupList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var utils = require("@/common/utils");
var tpl = require("./template/popupListTpl.html");

module.exports = function(opt){
var items = opt.items;
var reportClick = opt.reportClick || utils.noop;

var dom = utils.createElementFromHTML(_.template(tpl)({
items: items
}));
document.body.appendChild(dom);
utils.on(window, "resize", function(){
hide();
});
utils.on(document, "click", function(e){
var target = e.srcElement || e.target;
if(!dom.contains(target)){
hide();
}
});

// 菜单点击
utils.live("li", "click", function(e){
var menuId;
var target = e.srcElement || e.target;
if(utils.hasClass(target.parentNode, "popup-item")){
target = target.parentNode;
}
menuId = target.getAttribute("menuId");
// 上报点击项
reportClick(items[menuId], menuId);
hide();
}, dom);

function show(pos){
utils.removeClass(dom, "hide");
dom.style.left = pos.left + "px";
dom.style.top = pos.top + "px";
pos.width && (dom.style.width = pos.width + "px");
}

function hide(){
utils.addClass(dom, "hide");
}

return {
show: show,
hide: hide,
};
};
Loading

0 comments on commit 529c609

Please sign in to comment.