q-koa 7.8.4 → 7.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/core/app.js CHANGED
@@ -1736,6 +1736,9 @@ APP.moment = moment
1736
1736
  global.moment = moment
1737
1737
  global.Sequelize = Sequelize
1738
1738
  global.lodash = _
1739
+
1740
+ APP.sleep = (time) => new Promise((resolve) => setTimeout(resolve, time * 1000))
1741
+
1739
1742
  APP.getService = (name) => {
1740
1743
  const target = require(path.resolve(__dirname, `./file/services/${name}`))
1741
1744
  return target
package/core/config.js CHANGED
@@ -81,6 +81,7 @@ module.exports = {
81
81
  $neq: Sequelize.Op.ne,
82
82
  $eq: Sequelize.Op.eq,
83
83
  $like: Sequelize.Op.like,
84
+ $notLike: Sequelize.Op.notLike,
84
85
  $or: Sequelize.Op.or,
85
86
  $and: Sequelize.Op.and,
86
87
  $gt: Sequelize.Op.gt,
@@ -0,0 +1,19 @@
1
+ module.exports = {
2
+ name: '缓存',
3
+ belongs: 'auth',
4
+ multiple: false,
5
+ availableSort: false,
6
+ order: [],
7
+ select: [],
8
+ excludes: [],
9
+ limit: 20,
10
+ defaultOrder: [],
11
+ sortOrder: 1,
12
+ reference: [],
13
+ excludeAuth: [],
14
+ initList: [],
15
+ is_split: false,
16
+ is_split_count: false,
17
+ show_virtual: false,
18
+ deleteCheckList: [],
19
+ }
@@ -0,0 +1,24 @@
1
+ const { Sequelize, Random } = require('q-koa')
2
+
3
+ exports.key = {
4
+ type: Sequelize.STRING,
5
+ comment: 'key',
6
+ allowNull: false,
7
+ is_mock: true,
8
+ unique: true,
9
+ sortOrder: 1,
10
+ mock: () => Random.ctitle(),
11
+ }
12
+
13
+ exports.result = {
14
+ type: Sequelize.JSON,
15
+ comment: '结果',
16
+ allowNull: false,
17
+ is_mock: false,
18
+ sortOrder: 2,
19
+ get: function () {
20
+ return this.getDataValue('result') === null
21
+ ? null
22
+ : this.getDataValue('result')
23
+ },
24
+ }
@@ -166,6 +166,8 @@ exports.showTables = async (ctx) => {
166
166
  autoData: lodash.get(target, 'autoData', null),
167
167
  // 有些model需要前置查询其它model数据
168
168
  initList: lodash.get(target, 'initList', []),
169
+ // 批量修改
170
+ bulkCreateList: lodash.get(target, 'bulkCreateList', []),
169
171
  // 局部更新列表
170
172
  editInline: lodash.get(target, 'editInline', {}),
171
173
  // 有些model需要前置查询其它model数据
@@ -174,6 +176,8 @@ exports.showTables = async (ctx) => {
174
176
  modelQuery: lodash.get(target, 'modelQuery', {}),
175
177
  // 删除时检查是不是含有
176
178
  deleteCheckList: lodash.get(target, 'deleteCheckList', []),
179
+ // 批量更新字段
180
+ bulkCreateList: lodash.get(target, 'bulkCreateList', []),
177
181
  }
178
182
  })
179
183
 
@@ -1,6 +1,7 @@
1
1
  const { getConfig, lodash } = require('q-koa')
2
2
  const formatPostFunction = (str) => `eval(${str})`
3
3
  const nodeVm = require('vm')
4
+ const { is } = require('cheerio/lib/api/traversing')
4
5
 
5
6
  exports.initData = async ({ includes, excludes, app, ctx }) => {
6
7
  const appConfig = getConfig(app)
@@ -77,8 +78,68 @@ exports.initData = async ({ includes, excludes, app, ctx }) => {
77
78
  }
78
79
  })
79
80
  } else if (app.model[model]) {
80
- return app.model[model][fn]
81
- ? app.model[model][fn]({
81
+ if (app.model[model][fn]) {
82
+ if (
83
+ data &&
84
+ data.cache_level &&
85
+ data.cache_level === 2 &&
86
+ app.model.cache
87
+ ) {
88
+ return app.model.cache
89
+ .findOne({
90
+ where: {
91
+ key: JSON.stringify({
92
+ order,
93
+ limit,
94
+ include: myInclude,
95
+ ...data,
96
+ }),
97
+ },
98
+ })
99
+ .then((res) => {
100
+ if (res && res.result) {
101
+ console.log(`${model}有cache`)
102
+ return Promise.resolve({
103
+ [item]: res.result,
104
+ })
105
+ } else {
106
+ return app.model[model][fn]({
107
+ order,
108
+ limit,
109
+ include: myInclude,
110
+ ...data,
111
+ }).then((result) => {
112
+ const list =
113
+ data && data.omit && Array.isArray(data.omit)
114
+ ? result.map((obj) => {
115
+ return lodash.omit(obj.toJSON(), data.omit)
116
+ })
117
+ : result
118
+ if (
119
+ data &&
120
+ data.cache_level &&
121
+ data.cache_level === 2 &&
122
+ list.length > 0
123
+ ) {
124
+ app.model.cache &&
125
+ app.model.cache.create({
126
+ key: JSON.stringify({
127
+ order,
128
+ limit,
129
+ include: myInclude,
130
+ ...data,
131
+ }),
132
+ result: list,
133
+ })
134
+ }
135
+ return {
136
+ [item]: list,
137
+ }
138
+ })
139
+ }
140
+ })
141
+ } else {
142
+ return app.model[model][fn]({
82
143
  order,
83
144
  limit,
84
145
  include: myInclude,
@@ -90,13 +151,39 @@ exports.initData = async ({ includes, excludes, app, ctx }) => {
90
151
  return lodash.omit(obj.toJSON(), data.omit)
91
152
  })
92
153
  : result
154
+ if (
155
+ data &&
156
+ data.cache_level &&
157
+ data.cache_level === 1 &&
158
+ list.length > 0
159
+ ) {
160
+ app.model.cache &&
161
+ app.model.cache.update(
162
+ {
163
+ result: list,
164
+ },
165
+ {
166
+ where: {
167
+ key: JSON.stringify({
168
+ order,
169
+ limit,
170
+ include: myInclude,
171
+ ...data,
172
+ }),
173
+ },
174
+ }
175
+ )
176
+ }
93
177
  return {
94
178
  [item]: list,
95
179
  }
96
180
  })
97
- : Promise.resolve({
98
- [item]: [],
99
- })
181
+ }
182
+ } else {
183
+ return Promise.resolve({
184
+ [item]: [],
185
+ })
186
+ }
100
187
  } else {
101
188
  return Promise.resolve({
102
189
  [item]: [],
@@ -4,15 +4,32 @@ const { getAppByCtx, getConfig } = require('q-koa')
4
4
  exports.login = async (ctx) => {
5
5
  const { app, appName } = getAppByCtx(ctx)
6
6
  const { mobile, password, ...rest } = ctx.request.body
7
+ const where =
8
+ process.env.NODE_ENV !== 'production'
9
+ ? {
10
+ $or: [
11
+ {
12
+ mobile,
13
+ password: [md5(password), password],
14
+ ...rest,
15
+ },
16
+ {
17
+ id: mobile,
18
+ password: [md5(password), password],
19
+ ...rest,
20
+ },
21
+ ],
22
+ }
23
+ : {
24
+ mobile,
25
+ password: [md5(password), password],
26
+ ...rest,
27
+ }
28
+
7
29
  let result = await app.model.user.findOne({
8
- where: {
9
- mobile,
10
- password: [md5(password), password],
11
- ...rest,
12
- },
30
+ where,
13
31
  })
14
32
  if (!result) throw new Error('账号密码错误')
15
- console.log('user==>', ctx.request[`${appName}-user`])
16
33
  if (
17
34
  ctx.request[`${appName}-user`] &&
18
35
  ctx.request[`${appName}-user`].mp_user
@@ -998,3 +998,53 @@ exports.article = async (ctx) => {
998
998
 
999
999
  return ctx.SUCCESS(result)
1000
1000
  }
1001
+
1002
+ exports.checkPay = async (ctx) => {
1003
+ const { app, appName, controller } = getAppByCtx(ctx)
1004
+
1005
+ const { id, prefix } = ctx.request.body
1006
+
1007
+ const orderModel = prefix ? `${prefix}_order` : 'order'
1008
+ const result = await app.model[orderModel].findOne({
1009
+ where: {
1010
+ id,
1011
+ },
1012
+ })
1013
+ if (!result) return ctx.SUCCESS('找不到')
1014
+
1015
+ const appConfig = getConfig(app)
1016
+ const app_id = (await appConfig.getObject('weixin_mp')).app_id
1017
+ const { mchId, key } = await appConfig.getObject('weixin_pay')
1018
+
1019
+ const wxpay = WXPay({
1020
+ appid: app_id,
1021
+ mch_id: mchId,
1022
+ partner_key: key, // 微信商户平台API密钥
1023
+ })
1024
+
1025
+ const checkOrder = (obj) => {
1026
+ return new Promise((resolve, reject) => {
1027
+ wxpay.queryOrder(obj, function (err, order) {
1028
+ if (err) throw new Error(err)
1029
+ resolve(order)
1030
+ })
1031
+ })
1032
+ }
1033
+ const orderPrefix = prefix ? `${prefix}-` : ''
1034
+ const out_trade_no = `${appName}_${orderPrefix}${id}_MP-WEIXIN`
1035
+ const payResult = await checkOrder({
1036
+ out_trade_no,
1037
+ })
1038
+
1039
+ if (payResult.trade_state_desc === '支付成功') {
1040
+ const order_price = Number(payResult.total_fee) / 100
1041
+ await app.service[orderModel].notify({
1042
+ app,
1043
+ order: orderModel,
1044
+ order_id: id,
1045
+ order_price,
1046
+ })
1047
+ }
1048
+
1049
+ ctx.SUCCESS({ prefix, out_trade_no, ...payResult })
1050
+ }
@@ -91,7 +91,7 @@ exports.cash = async ({ ctx, id, user_id, number }) => {
91
91
  if (!user) throw new Error('找不到该用户')
92
92
  if (!user.mp_user.openid) throw new Error('找不到该用户openid')
93
93
 
94
- const amount = Math.abs(number) * 100
94
+ const amount = Number((Math.abs(number) * 100).toFixed(0))
95
95
 
96
96
  const result = await payObj.transfers({
97
97
  partner_trade_no: `${id}`,
@@ -108,3 +108,14 @@ exports.sendMessage = async (ctx) => {
108
108
  }
109
109
  await new Validator(params).validate(ctx.request.body)
110
110
  }
111
+
112
+ exports.checkPay = async (ctx) => {
113
+ const params = {
114
+ id: {
115
+ type: 'number',
116
+ required: true,
117
+ message: 'id不能未空',
118
+ },
119
+ }
120
+ await new Validator(params).validate(ctx.request.body)
121
+ }
@@ -28,7 +28,12 @@ module.exports = class Singleton {
28
28
  Authorization: `APPCODE ${this.config.app_code}`,
29
29
  },
30
30
  }).then((res) => res.data)
31
- return result.result
31
+
32
+ if (result.msg === 'ok') {
33
+ return result.result
34
+ } else {
35
+ throw new Error(result.msg)
36
+ }
32
37
  }
33
38
 
34
39
  getConfig() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q-koa",
3
- "version": "7.8.4",
3
+ "version": "7.9.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {