Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
5
51business
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
方斌
51business
Commits
aec897e6
Commit
aec897e6
authored
Oct 23, 2019
by
任国军
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add course_wechat
parent
0c63d2ac
Pipeline
#15257
passed with stage
in 36 seconds
Changes
5
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
119 additions
and
42 deletions
+119
-42
wechat.js
app/controller/course/wechat.js
+19
-0
context.js
app/extend/context.js
+9
-0
mini_auth.js
app/middleware/mini_auth.js
+21
-20
course.js
app/router/course.js
+24
-22
wechat.js
app/service/course/wechat.js
+46
-0
No files found.
app/controller/course/wechat.js
0 → 100644
View file @
aec897e6
'use strict'
;
const
Controller
=
require
(
'egg'
).
Controller
;
class
WechatController
extends
Controller
{
async
test
()
{
const
{
ctx
,
service
}
=
this
;
await
service
.
course
.
wechat
.
getAccessToken
();
ctx
.
success
();
}
async
callbackAction
()
{
const
{
ctx
,
service
}
=
this
;
await
service
.
course
.
wechat
.
callbackAction
();
ctx
.
success
();
}
}
module
.
exports
=
WechatController
;
app/extend/context.js
View file @
aec897e6
...
...
@@ -13,6 +13,7 @@ const OLDUSERID = Symbol('Context#oldUserId');
const
USERID
=
Symbol
(
'Context#userId'
);
const
OLDCOOKIE
=
Symbol
(
'Context#oldCookie'
);
const
USERUUID
=
Symbol
(
'Context#userUuid'
);
const
OPENID
=
Symbol
(
'Context#openId'
);
module
.
exports
=
{
failed
(
message
)
{
...
...
@@ -177,6 +178,10 @@ module.exports = {
return
this
[
USERUUID
];
},
get
openId
()
{
return
this
[
OPENID
];
},
setAppUserId
(
app_user_id
)
{
this
[
APPUSERID
]
=
app_user_id
;
},
...
...
@@ -212,4 +217,8 @@ module.exports = {
setUserUuid
(
user_uuid
)
{
this
[
USERUUID
]
=
user_uuid
;
},
setOpenId
(
openId
)
{
this
[
OPENID
]
=
openId
;
},
};
app/middleware/mini_auth.js
View file @
aec897e6
...
...
@@ -2,26 +2,27 @@
module
.
exports
=
(
options
,
app
)
=>
{
return
async
function
(
ctx
,
next
)
{
return
async
function
(
ctx
,
next
)
{
const
uuid
=
ctx
.
headers
.
uuid
;
const
key
=
'course_user_session_'
+
uuid
;
const
auth_info
=
await
ctx
.
app
.
memcache
.
get
(
key
);
if
(
!
auth_info
)
{
// await next();
// return;
ctx
.
failed
(
'login auth error'
);
}
const
openid
=
auth_info
.
openid
;
const
user_uuid
=
auth_info
.
user_uuid
;
const
auth_token
=
ctx
.
headers
.
auth_token
;
if
(
ctx
.
helper
.
md5
(
openid
+
uuid
+
'jbwl'
)
!=
auth_token
)
{
ctx
.
failed
(
'login auth error'
);
}
// console.info('11111111111111111111111111111111');
// console.info(auth_info);
ctx
.
setUserUuid
(
user_uuid
);
const
uuid
=
ctx
.
headers
.
uuid
;
const
key
=
'course_user_session_'
+
uuid
;
const
auth_info
=
await
ctx
.
app
.
memcache
.
get
(
key
);
if
(
!
auth_info
)
{
// await next();
// return;
ctx
.
failed
(
'login auth error'
);
}
const
openid
=
auth_info
.
openid
;
const
user_uuid
=
auth_info
.
user_uuid
;
const
auth_token
=
ctx
.
headers
.
auth_token
;
if
(
ctx
.
helper
.
md5
(
openid
+
uuid
+
'jbwl'
)
!=
auth_token
)
{
ctx
.
failed
(
'login auth error'
);
}
// console.info('11111111111111111111111111111111');
// console.info(auth_info);
ctx
.
setUserUuid
(
user_uuid
);
ctx
.
setOpenId
(
openid
);
await
next
();
};
await
next
();
};
};
app/router/course.js
View file @
aec897e6
'use strict'
;
module
.
exports
=
app
=>
{
const
router
=
app
.
router
.
namespace
(
app
.
config
.
projectRootPath
+
'/course'
);
const
loginAuth
=
app
.
middleware
.
loginAuth
({
type
:
'new'
});
//
登录中间件
const
miniAuth
=
app
.
middleware
.
miniAuth
();
//
因为不跟现有的用户中心系统,所以使用单独的登录中间件
const
router
=
app
.
router
.
namespace
(
app
.
config
.
projectRootPath
+
'/course'
);
const
loginAuth
=
app
.
middleware
.
loginAuth
({
type
:
'new'
});
//
登录中间件
const
miniAuth
=
app
.
middleware
.
miniAuth
();
//
因为不跟现有的用户中心系统,所以使用单独的登录中间件
router
.
get
(
'third'
,
'/options'
,
'course.option.getOptions'
);
//
筛选项
router
.
post
(
'third'
,
'/address'
,
miniAuth
,
'course.location.getAddress'
);
//
根据经纬度或ip获取地理位置信息
router
.
post
(
'third'
,
'/institutions'
,
miniAuth
,
'course.institution.institutionList'
);
//
机构列表
router
.
get
(
'third'
,
'/institutions'
,
miniAuth
,
'course.institution.institutionList'
);
//
机构列表
router
.
get
(
'third'
,
'/institution/:institution_id/:area_id'
,
miniAuth
,
'course.institution.institutionInfo'
);
//
机构详情
router
.
post
(
'third'
,
'/classes'
,
miniAuth
,
'course.institution.classList'
);
//
课程列表
router
.
get
(
'third'
,
'/classes'
,
miniAuth
,
'course.institution.classList'
);
//
课程列表
router
.
get
(
'third'
,
'/class/:class_id'
,
miniAuth
,
'course.institution.classInfo'
);
//
课程详情
router
.
post
(
'third'
,
'/teachers'
,
miniAuth
,
'course.institution.teacherList'
);
//
老师列表
router
.
get
(
'third'
,
'/teachers'
,
miniAuth
,
'course.institution.teacherList'
);
//
老师详情
router
.
get
(
'third'
,
'/teacher/:teacher_id'
,
miniAuth
,
'course.institution.teacherInfo'
);
//
老师详情
router
.
get
(
'third'
,
'/options'
,
'course.option.getOptions'
);
//
筛选项
router
.
post
(
'third'
,
'/address'
,
miniAuth
,
'course.location.getAddress'
);
//
根据经纬度或ip获取地理位置信息
router
.
post
(
'third'
,
'/institutions'
,
miniAuth
,
'course.institution.institutionList'
);
//
机构列表
router
.
get
(
'third'
,
'/institutions'
,
miniAuth
,
'course.institution.institutionList'
);
//
机构列表
router
.
get
(
'third'
,
'/institution/:institution_id/:area_id'
,
miniAuth
,
'course.institution.institutionInfo'
);
//
机构详情
router
.
post
(
'third'
,
'/classes'
,
miniAuth
,
'course.institution.classList'
);
//
课程列表
router
.
get
(
'third'
,
'/classes'
,
miniAuth
,
'course.institution.classList'
);
//
课程列表
router
.
get
(
'third'
,
'/class/:class_id'
,
miniAuth
,
'course.institution.classInfo'
);
//
课程详情
router
.
post
(
'third'
,
'/teachers'
,
miniAuth
,
'course.institution.teacherList'
);
//
老师列表
router
.
get
(
'third'
,
'/teachers'
,
miniAuth
,
'course.institution.teacherList'
);
//
老师详情
router
.
get
(
'third'
,
'/teacher/:teacher_id'
,
miniAuth
,
'course.institution.teacherInfo'
);
//
老师详情
router
.
post
(
'third'
,
'/user/auth'
,
'course.user.auth'
);
//
微信授权登录
router
.
post
(
'third'
,
'/user/register_user'
,
miniAuth
,
'course.user.registerUserInfo'
);
//
授权后注册用户
router
.
post
(
'third'
,
'/user/auth'
,
'course.user.auth'
);
//
微信授权登录
router
.
post
(
'third'
,
'/user/register_user'
,
miniAuth
,
'course.user.registerUserInfo'
);
//
授权后注册用户
router
.
get
(
'third'
,
'/user/baby'
,
miniAuth
,
'course.user.getBabyInfo'
);
//
获取baby信息
router
.
post
(
'third'
,
'/user/baby'
,
miniAuth
,
'course.user.saveBabyInfo'
);
//
保存baby信息
router
.
delete
(
'third'
,
'/user/baby'
,
miniAuth
,
'course.user.delBabyInfo'
);
//
删除baby信息
router
.
get
(
'third'
,
'/user/baby'
,
miniAuth
,
'course.user.getBabyInfo'
);
//
获取baby信息
router
.
post
(
'third'
,
'/user/baby'
,
miniAuth
,
'course.user.saveBabyInfo'
);
//
保存baby信息
router
.
delete
(
'third'
,
'/user/baby'
,
miniAuth
,
'course.user.delBabyInfo'
);
//
删除baby信息
router
.
get
(
'third'
,
'/user/collection/institution'
,
miniAuth
,
'course.user.getCollectInstitutions'
);
//收藏的机构列表
router
.
post
(
'third'
,
'/user/collection/institution'
,
miniAuth
,
'course.user.collectInstitution'
);
//收藏机构
router
.
delete
(
'third'
,
'/user/collection/institution'
,
miniAuth
,
'course.user.delCollectInstitution'
);
//取消收藏机构
router
.
get
(
'third'
,
'/user/collection/institution'
,
miniAuth
,
'course.user.getCollectInstitutions'
);
// 收藏的机构列表
router
.
post
(
'third'
,
'/user/collection/institution'
,
miniAuth
,
'course.user.collectInstitution'
);
// 收藏机构
router
.
delete
(
'third'
,
'/user/collection/institution'
,
miniAuth
,
'course.user.delCollectInstitution'
);
// 取消收藏机构
router
.
get
(
'third'
,
'/wechat/callbackAction'
,
'course.wechat.callbackAction'
);
};
app/service/course/wechat.js
0 → 100644
View file @
aec897e6
'use strict'
;
const
Service
=
require
(
'egg'
).
Service
;
const
APPID
=
'wx4769ebba9b91f8ec'
;
const
SECRET
=
'680440637b4e38c9b66529cfd5dc590e'
;
class
WechatService
extends
Service
{
async
getAccessToken
()
{
const
{
ctx
}
=
this
;
const
url
=
'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='
+
APPID
+
'&secret='
+
SECRET
;
// 先从redis中取
let
token
=
await
this
.
app
.
memcache
.
get
(
'wechat_course_accessToken'
);
if
(
ctx
.
isEmpty
(
token
))
{
const
resp
=
await
ctx
.
helper
.
send_request
(
url
,
{},
{
method
:
'GET'
});
if
(
resp
.
status
===
200
&&
!
ctx
.
isEmpty
(
resp
.
data
)
&&
!
ctx
.
isEmpty
(
resp
.
data
.
access_token
))
{
token
=
resp
.
data
.
access_token
;
await
this
.
app
.
memcache
.
set
(
'wechat_course_accessToken'
,
token
,
resp
.
data
.
expires_in
);
}
}
return
token
;
}
async
callbackAction
()
{
const
{
ctx
}
=
this
;
ctx
.
logger
.
info
(
'course_wechat_receive: '
+
JSON
.
stringify
(
ctx
));
await
this
.
sendMsg
();
}
async
sendMsg
()
{
const
{
ctx
}
=
this
;
const
token
=
await
this
.
getAccessToken
();
const
url
=
'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='
+
token
;
const
params
=
{
access_token
:
token
,
touser
:
ctx
.
openId
,
msgtype
:
'text'
,
text
:
{
content
:
'hello world'
},
};
const
resp
=
await
ctx
.
helper
.
send_request
(
url
,
params
,
{
method
:
'POST'
});
ctx
.
logger
.
info
(
'course_wechat_resp: '
+
JSON
.
stringify
(
resp
));
return
;
}
}
module
.
exports
=
WechatService
;
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment