q-koa 7.9.0 → 7.9.3

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
  */
@@ -239,6 +332,19 @@ exports.mp_login = async (ctx) => {
239
332
  openid: post.openid,
240
333
  },
241
334
  })
335
+ if (app.attributes.user.mp_openid) {
336
+ app.model.user.update(
337
+ {
338
+ mp_openid: post.openid,
339
+ },
340
+ {
341
+ where: {
342
+ id: openIdUser.user_id,
343
+ mp_openid: '',
344
+ },
345
+ }
346
+ )
347
+ }
242
348
  uid = openIdUser.user_id
243
349
  } else {
244
350
  if (post.user_id) {
@@ -250,12 +356,14 @@ exports.mp_login = async (ctx) => {
250
356
  } else {
251
357
  try {
252
358
  await app.sequelize.transaction(async (transaction) => {
253
- const user = await app.model.user.create(
254
- {},
255
- {
256
- transaction,
257
- }
258
- )
359
+ const target = app.attributes.user.mp_openid
360
+ ? {
361
+ mp_openid: post.openid,
362
+ }
363
+ : {}
364
+ const user = await app.model.user.create(target, {
365
+ transaction,
366
+ })
259
367
  uid = user.id
260
368
  await app.model.mp_user.create(
261
369
  {
@@ -1043,8 +1151,33 @@ exports.checkPay = async (ctx) => {
1043
1151
  order: orderModel,
1044
1152
  order_id: id,
1045
1153
  order_price,
1154
+ transaction_id: payResult.transaction_id,
1046
1155
  })
1047
1156
  }
1048
1157
 
1049
1158
  ctx.SUCCESS({ prefix, out_trade_no, ...payResult })
1050
1159
  }
1160
+
1161
+ exports.init_mp_openid = async (ctx) => {
1162
+ const { app, appName } = getAppByCtx(ctx)
1163
+
1164
+ const list = await app.model.user.findAll({
1165
+ where: {
1166
+ mp_openid: '',
1167
+ },
1168
+ include: app.model.mp_user,
1169
+ })
1170
+
1171
+ const result = list.map((item) => {
1172
+ return {
1173
+ id: item.id,
1174
+ mp_openid: item.mp_user.openid,
1175
+ }
1176
+ })
1177
+
1178
+ await app.model.user.bulkCreate(result, {
1179
+ updateOnDuplicate: ['mp_openid'],
1180
+ })
1181
+
1182
+ ctx.SUCCESS(result.length)
1183
+ }
@@ -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.3",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {