Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/1request/weixin into feat…
Browse files Browse the repository at this point in the history
…ure/connect_rails

Conflicts:
	both/controllers/staffs.js
	client/components/header/header.html
	client/pages/staffs/chat.coffee

resolved
  • Loading branch information
zuohaisu committed Jun 27, 2014
2 parents 254a0b5 + 6c8b3f8 commit b74d5e0
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .meteor/release
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.8.1.3
0.8.2
1 change: 1 addition & 0 deletions both/collections/accounts.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
db.accounts = new Meteor.Collection 'accounts'
1 change: 1 addition & 0 deletions both/controllers/staffs.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Staffs.chat = AppController.extend({
Meteor.subscribe('staffs'),
Meteor.subscribe('customers', this.customersOption()),
Meteor.subscribe('messages', Session.get('customerSelected'), this.messagesOption())
Meteor.subscribe('accounts'),
];
}
});
Expand Down
63 changes: 63 additions & 0 deletions client/components/header/header.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
############################
# Local Functions
############################
@validateAccountInfo = ->
if (!document.getElementById('gh_id').value)
throw new Meteor.Error(422, 'Please fill in GH ID')
if (!Session.get('accountSelected')?)
account = db.accounts.findOne({gh_id: document.getElementById('gh_id').value})
throw new Meteor.Error(422, 'GH ID is already in use') if account?
if (!document.getElementById('weixin_id').value)
throw new Meteor.Error(422, 'Please fill in WeChat ID')
if (!document.getElementById('account_name').value)
throw new Meteor.Error(422, 'Please fill in Account name')
if (!document.getElementById('app_id').value)
throw new Meteor.Error(422, 'Please fill in App ID')
if (!document.getElementById('app_secret').value)
throw new Meteor.Error(422, 'Please fill in App Secret')

############################
# Template: accountList
############################
Template.accountList.helpers
accounts: ->
db.accounts.find()

Template.accountList.events
'click li': (e) ->
Session.set('accountSelected', @gh_id)

############################
# Template: accountModal
############################
Template.accountModal.helpers
account: ->
db.accounts.findOne({gh_id: Session.get('accountSelected')})
accountSelected: ->
Session.get('accountSelected')?

Template.accountModal.events
'submit .form': (e) ->
e.preventDefault()

try
validateAccountInfo()

HTTP.post('http://api.xin.io/accounts',
params:
gh_id: document.getElementById('gh_id').value
weixin_id: document.getElementById('weixin_id').value
name: document.getElementById('account_name').value
app_id: document.getElementById('app_id').value
app_secret: document.getElementById('app_secret').value
headers:
'Content-Type': 'application/x-www-form-urlencoded'
(error, result) -> console.log result
)

Session.set('accountSelected', document.getElementById('gh_id').value)

$('.form')[0].reset()
$('#accountModal').modal('toggle')
catch error
alert(error.reason)
88 changes: 88 additions & 0 deletions client/components/header/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,101 @@
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">

<li><a href="{{pathFor 'weixin'}}">首页</a></li>
<li><a href="{{pathFor 'staffChat'}}">客服中心</a></li>
{{#if currentUser}}
<li>{{> accountList}}</li>
{{/if}}
</ul>
<ul class="nav navbar-nav navbar-right">
{{> loginButtons}}
</ul>
</div>
</header>

{{> accountModal}}
</template>

<template name="accountList">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
Account
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li role="dropdown-menu-title">


</li>
{{#each accounts}}
{{> accountDropdown}}
{{/each}}
<li class="divider"></li>
<li><a id="addAccount" data-toggle="modal" data-target="#accountModal">Add WeChat Account</a></li>
</ul>
</template>

<template name="accountDropdown">
<li><a href="#">{{name}} ({{weixin_id}})</a></li>
</template>

<template name="accountModal">
<!-- Modal -->
<div class="modal fade" id="accountModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title">WeChat Account</h4>
</div>
<form class="form">
<div class="modal-body">
<div class="form-group">
<label class="control-label" for="account_name">名称</label>
{{#if accountSelected}}
<input type="text" class="form-control disabled" id="account_name" value="{{account.name}}" disabled="">
{{else}}
<input type="text" class="form-control" id="account_name" value="{{account.name}}">
{{/if}}
</div>
<div class="form-group">
<label class="control-label" for="gh_id">原始ID</label>
{{#if accountSelected}}
<input type="text" class="form-control disabled" id="gh_id" value="{{account.gh_id}}" disabled="">
{{else}}
<input type="text" class="form-control" id="gh_id" value="{{account.gh_id}}">
<p class="help-block">e.g. gh_xxxxxxxx</p>
{{/if}}
</div>
<div class="form-group">
<label class="control-label" for="weixin_id">微信号</label>
{{#if accountSelected}}
<input type="text" class="form-control disabled" id="weixin_id" value="{{account.weixin_id}}" disabled="">
{{else}}
<input type="text" class="form-control" id="weixin_id" value="{{account.weixin_id}}">
{{/if}}
</div>
<div class="form-group">
<label class="control-label" for="app_id">App ID</label>
<input type="text" class="form-control" id="app_id" value="{{account.app_id}}">
</div>
<div class="form-group">
<label class="control-label" for="app_secret">App Secret</label>
<input type="text" class="form-control" id="app_secret" value="{{account.app_secret}}">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button tyoe="button" class="btn btn-primary">
{{#if accountSelected}}
Save
{{else}}
Create
{{/if}}</button>
</div>
</form>
</div>
</div>
</div>
</template>

20 changes: 19 additions & 1 deletion client/pages/staffs/chat.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ layoutDone = ->
offset = height - 17
$(elem).find('.message-arrow').css({'height': height + 'px', 'background-position-y': offset + 'px'})

############################
# Template: accountInfo
############################
Template.accountInfo.helpers
accountSelected: ->
Session.get('accountSelected')
account: ->
db.accounts.findOne({gh_id: Session.get('accountSelected')})
account_url: ->
account = db.accounts.findOne({gh_id: Session.get('accountSelected')})
if account
'http://api.xin.io/weixin/' + account.weixin_secret_key

############################
# Template: chat
############################
Expand All @@ -22,7 +35,8 @@ Template.chat.helpers
Session.get('lastUpdateTime')

messages: ->
db.messages.find(customer_id: Session.get('customerSelected'), {sort: created_at: 1})
db.messages.find({}, {sort: {weixin_msg_id: 1}})


showDefault: ->
'default' if Session.get('customerSelected') == ''
Expand Down Expand Up @@ -52,10 +66,12 @@ Template.chat.helpers
Template.chat.events
'submit .form': (e) ->
e.preventDefault()
account = db.accounts.findOne({gh_id: Session.get('accountSelected')})

message = $('.write-message')
data =
message: message.val()
account_id: account._id
customer_id: Session.get('customerSelected')
user_id: Meteor.userId()
message_type: 'staff'
Expand All @@ -70,6 +86,7 @@ Template.chat.events
customer = db.customers.findOne({_id: Session.get('customerSelected')})
HTTP.post('http://api.xin.io/kf',
params:
gh_id: Session.get('accountSelected')
weixin_id: customer.fromUser
q: message.val()
headers:
Expand Down Expand Up @@ -149,6 +166,7 @@ Template.messageItem.helpers
@content_type is type
railsUrl: ->
'http://api.xin.io'

Template.messageItem.rendered = ->
layoutDone()

Expand Down
50 changes: 50 additions & 0 deletions client/pages/staffs/chat.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<div class='staff-chat box'>
{{#if currentUser}}
{{> accountInfo}}
{{> chat}}
{{else}}
<h1>Please login</h1>
Expand All @@ -10,6 +11,55 @@ <h1>Please login</h1>

</template>

<template name="accountInfo">
<div class="box-header">
<h2><i class="fa fa-user"></i>Account Info</h2>
<div class="box-icon">
<i class="fa fa-wrench" data-toggle="modal" data-target="#accountModal"></i>
<span class="arrow-toggle" data-toggle="collapse" data-parent="#accordion" href="#accountInfo">
<i class="fa fa-chevron-up"></i>
<i class="fa fa-chevron-down"></i>
</span>
</div>
</div>
<div class="box-content panel-collapse collapse in" id="accountInfo">
<table class="table">
<tbody>
<tr>
<td>名称</td>
<td>{{account.name}}</td>
</tr>
<tr>
<td>原始ID</td>
<td>{{account.gh_id}}</td>
</tr>
<tr>
<td>微信号</td>
<td>{{account.weixin_id}}</td>
</tr>
<tr>
<td>App ID</td>
<td>{{account.app_id}}</td>
</tr>
<tr>
<td>App Secret</td>
<td>{{account.app_secret}}</td>
</tr>
<tr>
<td>URL</td>
<td>
{{account_url}}
</td>
</tr>
<tr>
<td>Token</td>
<td>{{account.weixin_token}}</td>
</tr>
</tbody>
</table>
</div>
</template>

<template name="chat">
<div id="xinplus-chat" class="box chat chat-full noOverflow">

Expand Down
10 changes: 10 additions & 0 deletions client/pages/staffs/chat.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ html input[disabled] {
cursor: not-allowed;
}

.arrow-toggle .fa.fa-chevron-up,
.arrow-toggle.collapsed .fa.fa-chevron-down {
display: inline-block !important;
}

.arrow-toggle .fa.fa-chevron-down,
.arrow-toggle.collapsed .fa.fa-chevron-up {
display: none !important;
}

.staff-chat.box {
width: 980px;
margin: 10px auto 0;
Expand Down
6 changes: 6 additions & 0 deletions server/fixture.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# customer = db.customers.find()
# customer.forEach (eachCustomer) ->
# msg = db.messages.find(customer_id: eachCustomer._id)
# msg.forEach (message) ->
# if !message.created_at
# message.created_at = new Date()
2 changes: 2 additions & 0 deletions server/publications/accounts.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Meteor.publish 'accounts', ->
db.accounts.find()

0 comments on commit b74d5e0

Please sign in to comment.