q-koa 7.9.0 → 7.9.1

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.
@@ -171,6 +171,99 @@ exports.getPhone = async (ctx) => {
171
171
  return ctx.SUCCESS({ phoneNumber })
172
172
  }
173
173
 
174
+ exports.mp_getPhoneNew = async (ctx) => {
175
+ const { app, appName } = getAppByCtx(ctx)
176
+
177
+ const { code } = ctx.request.body
178
+
179
+ const user_id = ctx.request[appName + '-user'].id
180
+ const appConfig = getConfig(app)
181
+ const { app_id, app_secrect } = await appConfig.getObject('weixin_mp')
182
+
183
+ const weixinMp = new WeixinMp({
184
+ appid: app_id,
185
+ secrect: app_secrect,
186
+ })
187
+ weixinMp.init()
188
+
189
+ const { phoneNumber } = await weixinMp.getPhoneNumber(code)
190
+
191
+ if (!phoneNumber) throw new Error('微信获取手机失败,请重试')
192
+
193
+ const exist = await app.model.user.findOne({
194
+ where: {
195
+ mobile: phoneNumber,
196
+ },
197
+ include: [
198
+ {
199
+ model: app.model.mp_user,
200
+ },
201
+ ],
202
+ })
203
+
204
+ const mobileExist =
205
+ exist &&
206
+ exist.mp_user &&
207
+ (!exist.mp_user.appid || exist.mp_user.appid === app_id)
208
+
209
+ if (mobileExist) {
210
+ throw new Error('系统已存在该手机号')
211
+ }
212
+
213
+ await app.model.user.upsert({
214
+ id: user_id,
215
+ mobile: phoneNumber,
216
+ })
217
+
218
+ const result = await app.model.user.findOne({
219
+ where: {
220
+ id: user_id,
221
+ },
222
+ include: [
223
+ {
224
+ model: app.model.h5_user,
225
+ },
226
+ {
227
+ model: app.model.mp_user,
228
+ },
229
+ {
230
+ model: app.model.toutiao_user,
231
+ },
232
+ ...app.include.user,
233
+ ].filter((item) => item.model),
234
+ })
235
+
236
+ const tokenResult = {
237
+ id: result.id,
238
+ name: result.name,
239
+ mobile: result.mobile,
240
+ h5_user: result.h5_user
241
+ ? {
242
+ openid: result.h5_user && result.h5_user.openid,
243
+ }
244
+ : null,
245
+ mp_user: result.mp_user
246
+ ? {
247
+ openid: result.mp_user && result.mp_user.openid,
248
+ }
249
+ : null,
250
+ toutiao_user: result.toutiao_user
251
+ ? {
252
+ openid: result.toutiao_user && result.toutiao_user.openid,
253
+ }
254
+ : null,
255
+ }
256
+
257
+ const token = await app.sign({
258
+ user: tokenResult,
259
+ })
260
+
261
+ return ctx.SUCCESS({
262
+ token,
263
+ user: result,
264
+ })
265
+ }
266
+
174
267
  /**
175
268
  * 微信小程序登录
176
269
  */
@@ -1043,6 +1136,7 @@ exports.checkPay = async (ctx) => {
1043
1136
  order: orderModel,
1044
1137
  order_id: id,
1045
1138
  order_price,
1139
+ transaction_id: payResult.transaction_id,
1046
1140
  })
1047
1141
  }
1048
1142
 
@@ -29,7 +29,7 @@ module.exports = class Singleton {
29
29
  },
30
30
  }).then((res) => res.data)
31
31
 
32
- if (result.msg === 'ok') {
32
+ if (result.msg === 'ok' || result.status === '205') {
33
33
  return result.result
34
34
  } else {
35
35
  throw new Error(result.msg)
@@ -4,6 +4,8 @@ const axios = require('axios')
4
4
  const { lodash } = require('q-koa')
5
5
  const getAccessTokenUrl =
6
6
  'https://api.weixin.qq.com/cgi-bin/token?grant_type=%s&appid=%s&secret=%s'
7
+ const getPhoneNumberUrl =
8
+ 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s'
7
9
  const createQRCodeUrl =
8
10
  'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s'
9
11
  const sendMessageUrl =
@@ -273,16 +275,27 @@ module.exports = class Singleton {
273
275
  if (result.errcode) {
274
276
  if (result.errcode === 40001) {
275
277
  cache.reset()
276
- return await this.getLive({
277
- start,
278
- limit,
279
- })
278
+ return await this.getLive(start, limit)
280
279
  }
281
280
  throw new Error(`${result.errcode};${result.errmsg}`)
282
281
  }
283
282
  return result.room_info
284
283
  }
285
284
 
285
+ async getPhoneNumber(code) {
286
+ const access_token = await this.getAccessToken()
287
+ const url = util.format(getPhoneNumberUrl, access_token)
288
+ const result = await axios.post(url, { code }).then((res) => res.data)
289
+ if (result.errcode) {
290
+ if (result.errcode === 40001) {
291
+ cache.reset()
292
+ return await this.getPhoneNumber(code)
293
+ }
294
+ throw new Error(`${result.errcode};${result.errmsg}`)
295
+ }
296
+ return result.phone_info
297
+ }
298
+
286
299
  async createQR(options) {
287
300
  if (!this.config.targetPath) throw new Error('没有配置文件夹')
288
301
  const access_token = await this.getAccessToken()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q-koa",
3
- "version": "7.9.0",
3
+ "version": "7.9.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {