customer_service_im 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/customer_service_im/application_controller.rb +7 -2
- data/app/controllers/customer_service_im/conversations_controller.rb +13 -0
- data/app/controllers/customer_service_im/messages_controller.rb +1 -0
- data/app/controllers/customer_service_im/staffs_controller.rb +13 -0
- data/app/controllers/customer_service_im/users_controller.rb +34 -0
- data/app/models/customer_service_im/staff.rb +2 -0
- data/app/views/customer_service_im/conversations/show.json.jbuilder +2 -0
- data/config/initializers/mini_program.rb +5 -0
- data/config/routes.rb +12 -0
- data/db/migrate/20211026062727_add_password_digest_to_customer_service_im_staffs.rb +5 -0
- data/db/migrate/20211026062951_add_email_to_customer_service_im_staffs.rb +5 -0
- data/lib/customer_service_im/version.rb +1 -1
- data/lib/customer_service_im.rb +2 -2
- metadata +25 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b1159035b88fea5b356d2331f6a8ea28f703599bd2018e11f9daccde396095ac
|
4
|
+
data.tar.gz: a8cdc0d5b2bbc65d595b882ecfde3321b6d862179ab487b5bde983ee74d376ee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c4993873b7ec3882da885ecfa2473853f5a81066130123de8596180f1141365e13441adb5780a8c1b484c8a674dddd2e5cd4fded54b3bea1483465bdb828540
|
7
|
+
data.tar.gz: 02e4ed7d1ecaef3e70abaf09571f45bb1a501200bb0eddcf7c897327aceff83738364ab625c7d474bc797bd4865dd247a092f2aca9cd70ceecefd37c5d4f7256
|
@@ -10,6 +10,10 @@ module CustomerServiceIm
|
|
10
10
|
render json: { errors: errors }, status: :unprocessable_entity
|
11
11
|
end
|
12
12
|
|
13
|
+
def render_401(errors: nil)
|
14
|
+
render json: { errors: errors }, status: :unauthorized
|
15
|
+
end
|
16
|
+
|
13
17
|
def render_200(data)
|
14
18
|
render json: data
|
15
19
|
end
|
@@ -17,7 +21,7 @@ module CustomerServiceIm
|
|
17
21
|
private
|
18
22
|
|
19
23
|
def token_user(user)
|
20
|
-
payload = { user_type: user.class.name, user_id: user.id }
|
24
|
+
payload = { data: { user_type: user.class.name, user_id: user.id } }
|
21
25
|
JWT.encode(payload, hmac_secret, 'HS256')
|
22
26
|
end
|
23
27
|
|
@@ -34,9 +38,10 @@ module CustomerServiceIm
|
|
34
38
|
token = request.headers[:token]
|
35
39
|
payload, = JWT.decode(token, hmac_secret, true, { algorithm: 'HS256' })
|
36
40
|
rescue JWT::DecodeError => e
|
41
|
+
# binding.pry
|
37
42
|
logger = Logger.new Rails.root.join("log/jwt.log")
|
38
43
|
logger.error("JWT Decode Error: #{e}")
|
39
|
-
|
44
|
+
return false
|
40
45
|
end
|
41
46
|
|
42
47
|
payload["data"]["user_type"].classify.constantize.find(payload["data"]["user_id"])
|
@@ -1,6 +1,7 @@
|
|
1
1
|
|
2
2
|
module CustomerServiceIm
|
3
3
|
class ConversationsController < ApplicationController
|
4
|
+
before_action :authenticate_user!
|
4
5
|
before_action :set_conversation, only: [:messages, :all_messages_read]
|
5
6
|
|
6
7
|
def index
|
@@ -8,6 +9,18 @@ module CustomerServiceIm
|
|
8
9
|
@total_count = @conversations.size
|
9
10
|
end
|
10
11
|
|
12
|
+
def show; end
|
13
|
+
|
14
|
+
def create
|
15
|
+
@conversation = Conversation.new(user: logged_user)
|
16
|
+
|
17
|
+
if @conversation.save
|
18
|
+
render :show
|
19
|
+
else
|
20
|
+
render_500 errors: @conversation.errors
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
11
24
|
def messages
|
12
25
|
@messages = @conversation.messages
|
13
26
|
.order('created_at desc')
|
@@ -0,0 +1,13 @@
|
|
1
|
+
|
2
|
+
module CustomerServiceIm
|
3
|
+
class StaffsController < CustomerServiceIm::ApplicationController
|
4
|
+
def login
|
5
|
+
staff = Staff.find_by!(email: params[:email])
|
6
|
+
if staff.authenticate(params[:password])
|
7
|
+
render_200 staff.as_json.merge({ token: token_user(staff) })
|
8
|
+
else
|
9
|
+
render_401(errors: "邮箱或密码错误")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
|
2
|
+
module CustomerServiceIm
|
3
|
+
class UsersController < ApplicationController
|
4
|
+
def login
|
5
|
+
mp = MiniProgram::Client.new(appid: CustomerServiceIm.appid, app_secret: CustomerServiceIm.app_secret )
|
6
|
+
result = mp.login(params.require(:code))
|
7
|
+
|
8
|
+
if result.failure?
|
9
|
+
render_500 errors: result.errors
|
10
|
+
end
|
11
|
+
|
12
|
+
open_id = result.data[:openid]
|
13
|
+
|
14
|
+
user = User.find_by!(open_id: open_id)
|
15
|
+
|
16
|
+
render_200 user.as_json.merge({ token: token_user(user), conversation_id: user.conversation_ids.first })
|
17
|
+
end
|
18
|
+
|
19
|
+
def update
|
20
|
+
user = logged_user
|
21
|
+
if user.update(user_params)
|
22
|
+
render_200 result: :ok
|
23
|
+
else
|
24
|
+
render_500 errors: user.errors
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def user_params
|
31
|
+
params.require(:user).permit(:nickname, :avatar)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/config/routes.rb
CHANGED
@@ -1,6 +1,12 @@
|
|
1
1
|
CustomerServiceIm::Engine.routes.draw do
|
2
2
|
mount ActionCable.server => "/cable"
|
3
3
|
|
4
|
+
resources :users, defaults: { format: :json } do
|
5
|
+
collection do
|
6
|
+
post :login
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
4
10
|
resources :conversations, defaults: { format: :json } do
|
5
11
|
member do
|
6
12
|
get :messages
|
@@ -20,4 +26,10 @@ CustomerServiceIm::Engine.routes.draw do
|
|
20
26
|
post :reply
|
21
27
|
end
|
22
28
|
end
|
29
|
+
|
30
|
+
resources :staffs do
|
31
|
+
collection do
|
32
|
+
post :login
|
33
|
+
end
|
34
|
+
end
|
23
35
|
end
|
data/lib/customer_service_im.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
require "customer_service_im/version"
|
2
2
|
require "customer_service_im/engine"
|
3
3
|
require "customer_service_im/core_ext"
|
4
|
-
require 'shoulda'
|
4
|
+
# require 'shoulda'
|
5
5
|
require "jwt"
|
6
6
|
require 'jbuilder'
|
7
7
|
require "tx_asr"
|
8
8
|
require "redis"
|
9
|
-
|
9
|
+
require 'mini_program'
|
10
10
|
|
11
11
|
module CustomerServiceIm
|
12
12
|
# Your code goes here...
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: customer_service_im
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ian
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-10-
|
11
|
+
date: 2021-10-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -31,19 +31,19 @@ dependencies:
|
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: 6.1.4.1
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
|
-
name: shoulda
|
34
|
+
name: shoulda-matchers
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
37
|
- - "~>"
|
38
38
|
- !ruby/object:Gem::Version
|
39
|
-
version:
|
39
|
+
version: 4.5.1
|
40
40
|
type: :runtime
|
41
41
|
prerelease: false
|
42
42
|
version_requirements: !ruby/object:Gem::Requirement
|
43
43
|
requirements:
|
44
44
|
- - "~>"
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version:
|
46
|
+
version: 4.5.1
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: jwt
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
@@ -114,6 +114,20 @@ dependencies:
|
|
114
114
|
- - ">="
|
115
115
|
- !ruby/object:Gem::Version
|
116
116
|
version: '0'
|
117
|
+
- !ruby/object:Gem::Dependency
|
118
|
+
name: mini_program
|
119
|
+
requirement: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
type: :runtime
|
125
|
+
prerelease: false
|
126
|
+
version_requirements: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
117
131
|
description: Description of CustomerServiceIm.
|
118
132
|
email:
|
119
133
|
- ianlynxk@gmail.com
|
@@ -132,6 +146,8 @@ files:
|
|
132
146
|
- app/controllers/customer_service_im/application_controller.rb
|
133
147
|
- app/controllers/customer_service_im/conversations_controller.rb
|
134
148
|
- app/controllers/customer_service_im/messages_controller.rb
|
149
|
+
- app/controllers/customer_service_im/staffs_controller.rb
|
150
|
+
- app/controllers/customer_service_im/users_controller.rb
|
135
151
|
- app/jobs/customer_service_im/application_job.rb
|
136
152
|
- app/mailers/customer_service_im/application_mailer.rb
|
137
153
|
- app/models/customer_service_im/application_record.rb
|
@@ -143,9 +159,11 @@ files:
|
|
143
159
|
- app/views/customer_service_im/conversations/_message.json.jbuilder
|
144
160
|
- app/views/customer_service_im/conversations/index.json.jbuilder
|
145
161
|
- app/views/customer_service_im/conversations/messages.json.jbuilder
|
162
|
+
- app/views/customer_service_im/conversations/show.json.jbuilder
|
146
163
|
- app/views/customer_service_im/messages/_message.json.jbuilder
|
147
164
|
- app/views/customer_service_im/messages/index.json.jbuilder
|
148
165
|
- config/cable.yml
|
166
|
+
- config/initializers/mini_program.rb
|
149
167
|
- config/initializers/tx_asr.rb
|
150
168
|
- config/routes.rb
|
151
169
|
- db/migrate/20210927065745_create_customer_service_im_staffs.rb
|
@@ -154,6 +172,8 @@ files:
|
|
154
172
|
- db/migrate/20210928084420_add_state_to_customer_service_im_messages.rb
|
155
173
|
- db/migrate/20210928085514_add_last_messaged_on_to_customer_service_im_conversations.rb
|
156
174
|
- db/migrate/20210930025215_add_unread_message_count_to_customer_service_im_conversations.rb
|
175
|
+
- db/migrate/20211026062727_add_password_digest_to_customer_service_im_staffs.rb
|
176
|
+
- db/migrate/20211026062951_add_email_to_customer_service_im_staffs.rb
|
157
177
|
- lib/customer_service_im.rb
|
158
178
|
- lib/customer_service_im/core_ext.rb
|
159
179
|
- lib/customer_service_im/engine.rb
|