q-koa 7.9.9 → 8.0.2

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
@@ -236,7 +236,7 @@ class APP {
236
236
  ) {
237
237
  this.app[appName].model.log.upsert(errorData)
238
238
  }
239
- throw new Error(err)
239
+ throw new Error(err.message)
240
240
  })
241
241
 
242
242
  // this.app.on('message', async (content) => {
@@ -381,7 +381,7 @@ class APP {
381
381
  } catch (e) {
382
382
  console.log(`${appName}数据库连接失败`)
383
383
  console.log(e)
384
- throw new Error(e)
384
+ throw new Error(e.message)
385
385
  }
386
386
  if (
387
387
  this.app[appName] &&
@@ -1764,7 +1764,14 @@ APP.getAppByCtx = (ctx) => {
1764
1764
  controller,
1765
1765
  }
1766
1766
  }
1767
-
1767
+ APP.getUserByCtx = (ctx) => {
1768
+ try {
1769
+ const appName = ctx.router.opts.prefix.substring(1)
1770
+ return ctx.request[`${appName}-user`].id
1771
+ } catch (e) {
1772
+ return 0
1773
+ }
1774
+ }
1768
1775
  APP.getClientTypeByCtx = (ctx) => {
1769
1776
  const typeEnum = {
1770
1777
  1: 'admin',
@@ -80,7 +80,7 @@ exports.mp_getPhone = async (ctx) => {
80
80
  (!exist.mp_user.appid || exist.mp_user.appid === app_id)
81
81
 
82
82
  if (mobileExist) {
83
- throw new Error('系统已存在该手机号')
83
+ return ctx.ERROR('系统已存在该手机号')
84
84
  }
85
85
 
86
86
  const userResult = await app.model.mp_user.findOne({
@@ -90,7 +90,7 @@ exports.mp_getPhone = async (ctx) => {
90
90
  })
91
91
 
92
92
  if (!userResult || !userResult.user_id)
93
- throw new Error('微信获取手机失败,请重试')
93
+ return ctx.ERROR('微信获取手机失败,请重试')
94
94
 
95
95
  await app.model.user.upsert({
96
96
  id: userResult.user_id,
@@ -207,7 +207,7 @@ exports.mp_getPhoneNew = async (ctx) => {
207
207
  (!exist.mp_user.appid || exist.mp_user.appid === app_id)
208
208
 
209
209
  if (mobileExist) {
210
- throw new Error('系统已存在该手机号')
210
+ return ctx.ERROR('系统已存在该手机号')
211
211
  }
212
212
 
213
213
  await app.model.user.upsert({
@@ -1,37 +1,40 @@
1
1
  const Core = require('@alicloud/pop-core')
2
2
  const { lodash } = require('q-koa')
3
- module.exports = class Sms {
4
- constructor(config) {
5
- this.config = {
6
- accessKeyId: config.accessKeyId, // 开发者账号id
7
- accessKeySecret: config.accessKeySecret, // 开发者token
8
- SignName: config.SignName, // 应用id
9
- param: Array.from(
10
- {
11
- length: config.number || 4,
12
- },
13
- () => lodash.random(0, 9)
14
- ).join(''),
3
+ module.exports = class Singleton {
4
+ constructor(key) {
5
+ this.key = key
6
+
7
+ /**
8
+ * 单例模式
9
+ */
10
+ if (!Singleton.instance) {
11
+ Singleton.instance = this
12
+ }
13
+ const previous = Singleton.instance.getConfig()
14
+ if (!lodash.isEqual(previous, key)) {
15
+ Singleton.instance = this
15
16
  }
17
+ return Singleton.instance
18
+ }
19
+
20
+ init() {
16
21
  this.client = new Core({
17
- accessKeyId: this.config.accessKeyId,
18
- accessKeySecret: this.config.accessKeySecret,
22
+ accessKeyId: this.key.accessKeyId,
23
+ accessKeySecret: this.key.accessKeySecret,
19
24
  endpoint: 'https://dysmsapi.aliyuncs.com',
20
25
  apiVersion: '2017-05-25',
21
26
  })
22
27
  }
23
28
 
24
29
  async send({ mobile, TemplateCode, TemplateParam }) {
25
- // if(!mobile) throw new Error('请输入手机号码')
30
+ console.log('JSON.stringify(TemplateParam)')
26
31
  const result = await this.client.request(
27
32
  'SendSms',
28
33
  {
29
34
  PhoneNumbers: mobile,
30
- SignName: this.config.SignName,
35
+ SignName: this.key.SignName,
31
36
  TemplateCode,
32
- TemplateParam: JSON.stringify({
33
- code: this.config.param,
34
- }),
37
+ TemplateParam: JSON.stringify(TemplateParam),
35
38
  },
36
39
  {
37
40
  method: 'POST',
@@ -40,7 +43,7 @@ module.exports = class Sms {
40
43
  return result
41
44
  }
42
45
 
43
- getCode() {
44
- return this.config.param
46
+ getConfig() {
47
+ return this.key
45
48
  }
46
49
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q-koa",
3
- "version": "7.9.9",
3
+ "version": "8.0.2",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {