q-koa 12.0.5 → 12.0.7

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.
@@ -1728,3 +1728,77 @@ exports.setMsgJumpPath = async (ctx) => {
1728
1728
 
1729
1729
  ctx.SUCCESS('ok')
1730
1730
  }
1731
+
1732
+ exports.getUserPortrait = async (ctx) => {
1733
+ const { app, appName } = getAppByCtx(ctx)
1734
+ const appConfig = getConfig(app)
1735
+
1736
+ const { config = 'weixin_mp', type = 'day' } = ctx.request.body
1737
+ const { app_id, app_secrect } = await appConfig.getObject(config)
1738
+
1739
+ const weixinMp = new WeixinMp({
1740
+ appid: app_id,
1741
+ secrect: app_secrect,
1742
+ })
1743
+
1744
+ weixinMp.init()
1745
+
1746
+ let obj = {}
1747
+ const diff = moment().hour() > 8 ? -1 : -2
1748
+ if (type === 'day') {
1749
+ obj = {
1750
+ begin_date: moment().add(diff, 'days').format('YYYYMMDD'),
1751
+ end_date: moment().add(diff, 'days').format('YYYYMMDD'),
1752
+ }
1753
+ } else if (type === 'week') {
1754
+ obj = {
1755
+ begin_date: moment()
1756
+ .add(-6 + diff, 'days')
1757
+ .format('YYYYMMDD'),
1758
+ end_date: moment().add(diff, 'days').format('YYYYMMDD'),
1759
+ }
1760
+ } else if (type === 'month') {
1761
+ obj = {
1762
+ begin_date: moment()
1763
+ .add(-29 + diff, 'days')
1764
+ .format('YYYYMMDD'),
1765
+ end_date: moment().add(diff, 'days').format('YYYYMMDD'),
1766
+ }
1767
+ }
1768
+ console.log('obj', obj)
1769
+ const result = await weixinMp.getUserPortrait(obj)
1770
+
1771
+ ctx.SUCCESS(result)
1772
+ }
1773
+
1774
+ exports.getVisitTrend = async (ctx) => {
1775
+ const { app, appName } = getAppByCtx(ctx)
1776
+ const appConfig = getConfig(app)
1777
+
1778
+ const { config = 'weixin_mp', type = 'day', date } = ctx.request.body
1779
+ const { app_id, app_secrect } = await appConfig.getObject(config)
1780
+
1781
+ const weixinMp = new WeixinMp({
1782
+ appid: app_id,
1783
+ secrect: app_secrect,
1784
+ })
1785
+
1786
+ weixinMp.init()
1787
+
1788
+ let obj = {}
1789
+ const diff = moment().hour() > 8 ? -1 : -2
1790
+ if (type === 'day') {
1791
+ obj = {
1792
+ begin_date: moment(date ? date : moment().add(diff, 'days')).format(
1793
+ 'YYYYMMDD'
1794
+ ),
1795
+ end_date: moment(date ? date : moment().add(diff, 'days')).format(
1796
+ 'YYYYMMDD'
1797
+ ),
1798
+ }
1799
+ }
1800
+
1801
+ const result = await weixinMp.getVisitTrend({ ...obj, type })
1802
+
1803
+ ctx.SUCCESS(result)
1804
+ }
@@ -65,6 +65,9 @@ const setMsgJumpPathUrl =
65
65
  const getDeliveryListUrl =
66
66
  'https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/get_delivery_list?access_token=%s'
67
67
 
68
+ const getUserPortraitUrl =
69
+ 'https://api.weixin.qq.com/datacube/getweanalysisappiduserportrait?access_token=%s'
70
+
68
71
  const fsPromise = require('fs/promises')
69
72
  const LRU = require('lru-cache')
70
73
  const request = require('request')
@@ -753,6 +756,63 @@ module.exports = class Singleton {
753
756
  return result
754
757
  }
755
758
 
759
+ async getUserPortrait(payLoad) {
760
+ const access_token = await this.getAccessToken()
761
+ const url = util.format(getUserPortraitUrl, access_token)
762
+
763
+ const result = await axios
764
+ .post(url, {
765
+ ...payLoad,
766
+ })
767
+ .then((res) => res.data)
768
+ if (result.errcode) {
769
+ if (result.errcode === 40001) {
770
+ cache.reset()
771
+ return await this.getUserPortrait(payLoad)
772
+ }
773
+ throw new Error(`${result.errcode};${result.errmsg}`)
774
+ }
775
+ return result
776
+ }
777
+
778
+ async getVisitTrend(payLoad) {
779
+ const access_token = await this.getAccessToken()
780
+ let url = ''
781
+ if (payLoad.type === 'month') {
782
+ url = util.format(
783
+ 'https://api.weixin.qq.com/datacube/getweanalysisappidmonthlyvisittrend?access_token=%s',
784
+ access_token
785
+ )
786
+ } else if (payLoad.type === 'week') {
787
+ url = util.format(
788
+ 'https://api.weixin.qq.com/datacube/getweanalysisappidweeklyvisittrend?access_token=%s',
789
+ access_token
790
+ )
791
+ } else if (payLoad.type === 'day') {
792
+ url = util.format(
793
+ 'https://api.weixin.qq.com/datacube/getweanalysisappiddailyvisittrend?access_token=%s',
794
+ access_token
795
+ )
796
+ }
797
+
798
+ const result = await axios
799
+ .post(url, {
800
+ ...payLoad,
801
+ })
802
+ .then((res) => res.data)
803
+ if (result.errcode) {
804
+ if (result.errcode === 40001) {
805
+ cache.reset()
806
+ return await this.getVisitTrend(payLoad)
807
+ }
808
+ if (result.errcode === 61503) {
809
+ return null
810
+ }
811
+ throw new Error(`${result.errcode};${result.errmsg}`)
812
+ }
813
+ return result
814
+ }
815
+
756
816
  getConfig() {
757
817
  return this.config
758
818
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q-koa",
3
- "version": "12.0.5",
3
+ "version": "12.0.7",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {