Skip to content
This repository has been archived by the owner on Mar 12, 2022. It is now read-only.

Commit

Permalink
feat: #79 项目结构升级; 扫码登录后台逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
klren0312 committed Jul 27, 2020
1 parent 9cb8534 commit 99699b2
Show file tree
Hide file tree
Showing 138 changed files with 5,599 additions and 758 deletions.
8 changes: 4 additions & 4 deletions ApiServer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

- NodeJS
- MySQL
- Redis
- Redis (12 - 订单, 4 - wechat, 5 - toutiao, 6 - qrcode login)

### 1.NodeJS
JS服务端运行环境
Expand Down Expand Up @@ -222,7 +222,7 @@ async findPage({
async getUserLog() {
const { ctx, app } = this
let { page } = ctx
const { common } = ctx.service
const { common } = ctx.service.v1
console.log(page)
page = {
...page,
Expand Down Expand Up @@ -287,7 +287,7 @@ https://www.jianshu.com/p/8df2213f6f9d
```javascript
async getWechatToken() {
const {ctx, app} = this
let result = await ctx.helper.request({ctx, url: `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${app.config.weapp.appId}&secret=${app.config.weapp.secret}`})
let result = await ctx.helper.request({ctx, url: `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${app.config.wechat.tlgc.appId}&secret=${app.config.wechat.tlgc.secret}`})
app.redis.set('weToken', result.data.access_token, 'EX', result.data.expires_in)
return result.data.access_token
}
Expand All @@ -312,7 +312,7 @@ async getWechatUser() {
})
return result
} else {
token = await ctx.service.data.getWechatToken()
token = await ctx.service.v1.data.getWechatToken()
const result = await ctx.helper.request({
ctx,
url: `https://api.weixin.qq.com/datacube/getweanalysisappiduserportrait?access_token=${token}`,
Expand Down
149 changes: 149 additions & 0 deletions ApiServer/api.rest
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# 使用的是 REST Client 插件

### login
POST http://localhost:7001/api/v1/user/login HTTP/1.1
content-type: application/json

{
"username": "tlgc",
"password": "qwer1234"
}

### login
POST http://localhost:7001/api/v1/user/login HTTP/1.1
content-type: application/json

{
"username": "tour",
"password": "tour520"
}

### get weixin user
GET http://localhost:7001/api/v1/wuser?pageSize=10&pageField=id HTTP/1.1
User-Agent: rest-client
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NCwidXNlcm5hbWUiOiJ0bGdjIiwicm9sZSI6InN1cGVyQWRtaW4iLCJpYXQiOjE1NjQyMTU0OTUsImV4cCI6MTU2NDM4ODI5NX0.amsnvOa-aAxCc0IkD_5VCpyM3g2f_9JhPrAUlqWxTwo
content-type: application/json

### get weixin user
GET http://localhost:7001/api/v1/wuser?pageSize=10&pageField=count HTTP/1.1
User-Agent: rest-client
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NCwidXNlcm5hbWUiOiJ0bGdjIiwicm9sZSI6InN1cGVyQWRtaW4iLCJpYXQiOjE1NjQyMTU0OTUsImV4cCI6MTU2NDM4ODI5NX0.amsnvOa-aAxCc0IkD_5VCpyM3g2f_9JhPrAUlqWxTwo
content-type: application/json

### get system log
GET http://localhost:7001/api/v1/log HTTP/1.1
User-Agent: rest-client
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NCwidXNlcm5hbWUiOiJ0bGdjIiwicm9sZSI6InN1cGVyQWRtaW4iLCJpYXQiOjE1NjQyMTU0OTUsImV4cCI6MTU2NDM4ODI5NX0.amsnvOa-aAxCc0IkD_5VCpyM3g2f_9JhPrAUlqWxTwo
content-type: application/json




######################################################
######################################################
### get system log
GET http://localhost:7001/api/v1/log HTTP/1.1
User-Agent: rest-client
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NCwidXNlcm5hbWUiOiJ0bGdjIiwicm9sZSI6InN1cGVyQWRtaW4iLCJpYXQiOjE1NjM0MzIwODAsImV4cCI6MTU2MzYwNDg4MH0.F00GVxtJXLVE2VOCjp2SG3blB9BFNQjh8o2XIJTDkYM
content-type: application/json

###
POST http://localhost:7001/api/v1/user/login HTTP/1.1
content-type: application/json

{
"username": "tlgc",
"password": "qwer1234"
}

###
POST http://localhost:7001/api/v1/user/login HTTP/1.1
content-type: application/json

{
"username": "tour",
"password": "tour520"
}

###
GET http://localhost:7001/api/v1/article HTTP/1.1
content-type: application/json
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NCwidXNlcm5hbWUiOiJ0bGdjIiwicm9sZSI6InN1cGVyQWRtaW4iLCJpYXQiOjE1ODQ2MjkxMTMsImV4cCI6MTU4NDgwMTkxM30.uRrAdmi0Kpn0DhMbJoIBLFrGbqhNoabm7eKm1ElsQYU

###
GET http://localhost:7001/api/v1/wechat/logout HTTP/1.1
content-type: application/json
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NCwidXNlcm5hbWUiOiJ0bGdjIiwicm9sZSI6InN1cGVyQWRtaW4iLCJpYXQiOjE1NjQyMjAxNDQsImV4cCI6MTU2NDM5Mjk0NH0.HXHFIx_tRTOOZOC9e5DWjb6zyC-NDEYxCqWd93LRl0M

###
GET http://localhost:7001 HTTP/1.1
content-type: application/json

###
GET http://localhost:7001/api/v1/test HTTP/1.1
content-type: application/json

###
GET http://localhost:7001/api/v1/captcha HTTP/1.1
content-type: application/json

###
GET http://ip.taobao.com/service/getIpInfo.php?ip=116.231.196.113

###
GET http://localhost:7001/api/v1/cars/all HTTP/1.1
Content-Type: application/json

###
POST http://192.168.99.186:7001/api/v1/location HTTP/1.1
Content-Type: application/json

{
"latitude": 1,
"longitude": 1
}

###
GET http://192.168.99.186:7001/api/v1/location/current HTTP/1.1
Content-Type: application/json

###
POST http://localhost/hdfq/uvCount HTTP/1.1
Content-Type: application/json

###
POST http://localhost:7001/api/v1/pay HTTP/1.1
Content-Type: application/json
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NCwidXNlcm5hbWUiOiJ0bGdjIiwicm9sZSI6InN1cGVyQWRtaW4iLCJpYXQiOjE1ODUxMjg3MzQsImV4cCI6MTU4NTMwMTUzNH0.qTYj2_NxOJsiiqORYVSg7XDwRCFmMZ3B-aesOy0ME-c

{
"outTradeNo": "1231424142313",
"totalAmount": 0.01,
"subject": "ceui",
"body": "ceui",
"address": "长江",
"mobile": 15755555555,
"number": 1
}


###
GET http://localhost:7001/api/v1/pay/order/fewfewafewwefaaaaaaaaaaaaaa HTTP/1.1
Content-Type: application/json
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NCwidXNlcm5hbWUiOiJ0bGdjIiwicm9sZSI6InN1cGVyQWRtaW4iLCJpYXQiOjE1ODUxMjg3MzQsImV4cCI6MTU4NTMwMTUzNH0.qTYj2_NxOJsiiqORYVSg7XDwRCFmMZ3B-aesOy0ME-c

#Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NCwidXNlcm5hbWUiOiJ0bGdjIiwicm9sZSI6InN1cGVyQWRtaW4iLCJpYXQiOjE1ODUxMjg3MzQsImV4cCI6MTU4NTMwMTUzNH0.qTYj2_NxOJsiiqORYVSg7XDwRCFmMZ3B-aesOy0ME-c
###
GET http://localhost:7001/api/v1/pay/order HTTP/1.1
Content-Type: application/json
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NCwidXNlcm5hbWUiOiJ0bGdjIiwicm9sZSI6InN1cGVyQWRtaW4iLCJpYXQiOjE1ODUxMjg3MzQsImV4cCI6MTU4NTMwMTUzNH0.qTYj2_NxOJsiiqORYVSg7XDwRCFmMZ3B-aesOy0ME-c


###
GET http://localhost:7001/api/v1/pay/status/22222222222222 HTTP/1.1
Content-Type: application/json
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NCwidXNlcm5hbWUiOiJ0bGdjIiwicm9sZSI6InN1cGVyQWRtaW4iLCJpYXQiOjE1ODUxMjg3MzQsImV4cCI6MTU4NTMwMTUzNH0.qTYj2_NxOJsiiqORYVSg7XDwRCFmMZ3B-aesOy0ME-c

###
GET http://localhost:7001/api/v2/toutiao/text/蛤蟆 HTTP/1.1
Content-Type: application/json
6 changes: 2 additions & 4 deletions ApiServer/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ module.exports = app => {
sub.subscribe(expired_subKey, function () {
sub.on('message', async (info, msg) => {
const ctx = app.createAnonymousContext()
const order = await ctx.service.alipay.query(msg)
const order = await ctx.service.v1.alipay.query(msg)
if (order.subCode === 'ACQ.TRADE_NOT_EXIST') { // 用户没有登录订单, 订单为没创建
console.log(msg)
app.redis.set(msg, 'order', 'EX', 60 * 60)
} else if (order.trade_status !== 'TRADE_SUCCESS') { // 订单创建, 没支付, 就关闭订单
console.log('close')
ctx.service.alipay.close(msg)
ctx.service.v1.alipay.close(msg)
}
})
})
Expand Down
134 changes: 134 additions & 0 deletions ApiServer/app/controller/api/v1/alipay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
'use strict'

const Controller = require('egg').Controller

class AlipayController extends Controller {
/**
* PC网页支付
*/
async pay() {
const { ctx } = this
const { alipay } = ctx.service.v1
const data = ctx.request.body
ctx.validate({
outTradeNo: {type: 'string', require: true},
totalAmount: {type: 'number', max:100000000, min:0.01, require: true},
subject: {type: 'string', require: true}
}, data)
const url = await alipay.pay(data)
if (url) {
ctx.status = 201
ctx.helper.success({ctx, res: url})
} else {
ctx.helper.fail({ctx, res: '付款失败'})
}
}

/**
* 手机端网页支付
*/
async mobilePay() {
const { ctx } = this
const { alipay } = ctx.service.v1
const data = ctx.request.body
ctx.validate({
outTradeNo: {type: 'string', require: true},
totalAmount: {type: 'number', max:100000000, min:0.01, require: true},
subject: {type: 'string', require: true}
}, data)
const form = await alipay.mobilePay(data)
if (form) {
ctx.status = 201
ctx.helper.success({ctx, res: form})
} else {
ctx.helper.fail({ctx, res: '付款失败'})
}
}

/**
* 支付回调
*/
async payCallback() {
const { ctx } = this
const { alipay } = ctx.service.v1
const data = ctx.request.body
const result = await alipay.checkSign(data)
ctx.body = {
result
}
}

/**
* 查询支付
*/
async query() {
const {ctx} = this
const { alipay } = ctx.service.v1
const outTradeNo = ctx.params.outTradeNo
ctx.validate({
outTradeNo: {type: 'string', require: true}
}, ctx.params)
const result = await alipay.query(outTradeNo)
ctx.helper.success({ctx, res: result})
}

/**
* 用户获取自己的订单
*/
async getOrderByUser () {
const { ctx, app } = this
// 获取用户id
const userInfo = app.verifyToken(ctx)
const { id: userId, role } = userInfo
let { page } = ctx
const { common } = ctx.service.v1
const search = ctx.request.query
let where = {}
where = {
user_id: userId
}
if (role === 'superAdmin') {
if (search.hasOwnProperty('userId') && search.userId !== '') {
where = {
user_id: search.userId
}
} else {
where = {}
}
}
page = {
...page,
pageField: 'id',
pageSort: 'DESC'
}
const include = []
const [total, items] = await Promise.all([
common.findCount({ modelName: 'Order' }),
common.findPage({
modelName: 'Order',
page,
include,
where
})
])
const res = { total, items, pageSize: page.pageSize, pageIndex: page.pageIndex }
ctx.helper.success({ ctx, res: res })
}

/**
* 用户查询订单支付状态
*/
async checkPay () {
const { ctx } = this
const { alipay } = ctx.service.v1
const outTradeNo = ctx.params.outTradeNo
const result = await alipay.checkPay(outTradeNo)
if (result) {
ctx.helper.success({ctx, res: {status: true}, msg: '支付成功'})
} else {
ctx.helper.success({ctx, res: {status: false}, msg: '未支付'})
}
}
}

module.exports = AlipayController
Loading

0 comments on commit 99699b2

Please sign in to comment.