请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

Cannot read property 'then' of undefined

在执行deleteMenu()的时候,deleteMenu()里面的then错误,这是为什么?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Wechat.prototype.deleteMenu = function(){
    var that = this
 
    return new Promise(function(resolve, reject){
        that
            .fetchAccessToken()
            .then(function(data){
                var url = api.menu.del + 'access_token=' + data.access_token
 
                request({method: 'GET', url: url, json: true}).then(function(response){
                    var _data = response.body
                    if (_data) {
                        resolve(_data)
                    }
                    else{
                        throw new Error('Get menu fails')
                    }
                })
                .catch(function(err){
                    reject(err)
                })
            })
    })
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
'use strict'
 
var config = require('../config')
var Wechat = require('../wechat/wechat')
var menu = require('./menu')
var wechatApi = new Wechat(config.wechat)
var path = require('path')
 
wechatApi.deleteMenu().then(function(){
    return wechatApi.createMenu(menu)
})
.then(function(msg){
    console.log(msg)
})
exports.reply = function* (next){
    var message = this.weixin
 
    if (message.MsgType === 'event') {
        if (message.Event === 'subscribe') {
            if (message.EventKey) {
                console.log('扫描二维码进来的:' + message.EventKey + '' + message.ticket)
            }
            this.body = '哈哈,你订阅了微信号'
        }
        else if (message.Event === 'unsubscribe') {
            console.log('无情取关')
            this.body = ''
        }
        else if (message.Event === 'LOCATION') {
            this.body = '您上报的位置是:' + message.Latitude + '/' + message.Longitude + '-' + message.Precision
        }
        else if (message.Event === 'CLICK') {
            this.body = '您点击了菜单:' + message.EventKey
        }
        else if (message.Event === 'SCAN') {
            console.log('关注后扫二维码' + message.EventKey + ' ' + message.ticket)
            this.body = '看到你扫了一下哦'
        }
        else if (message.Event === 'VIEW') {
            this.body = '您点击了菜单中的链接:' + message.EventKey
        }
        else if (message.Event === 'scancode_push') {
            console.log(message.ScanCodeInfo.ScanType)
            console.log(message.ScanCodeInfo.ScanResult)
            this.body = '您点击了菜单中的链接:' + message.EventKey
        }
        else if (message.Event === 'scancode_waitmsg') {
            console.log(message.ScanCodeInfo.ScanType)
            console.log(message.ScanCodeInfo.ScanResult)
            this.body = '您点击了菜单中的链接:' + message.EventKey
        }
        else if (message.Event === 'pic_sysphoto') {
            console.log(message.SendPicsInfo.PicList)
            console.log(message.SendPicsInfo.Count)
            this.body = '您点击了菜单中的链接:' + message.EventKey
        }
        else if (message.Event === 'pic_photo_or_album') {
            console.log(message.SendPicsInfo.PicList)
            console.log(message.SendPicsInfo.Count)
            this.body = '您点击了菜单中的链接:' + message.EventKey
        }
        else if (message.Event === 'pic_weixin') {
            console.log(message.SendPicsInfo.PicList)
            console.log(message.SendPicsInfo.Count)
            this.body = '您点击了菜单中的链接:' + message.EventKey
        }
        else if (message.Event === 'location_select') {
            console.log(message.SendLocationInfo.Location_X)
            console.log(message.SendLocationInfo.Location_Y)
            console.log(message.SendLocationInfo.Scale)
            console.log(message.SendLocationInfo.Label)
            console.log(message.SendLocationInfo.Poiname)
            console.log(message.SendPicsInfo.Count)
            this.body = '您点击了菜单中的链接:' + message.EventKey
        }
    }
    else if (message.MsgType === 'text') {
        var content = message.Content
        var reply = '额,你说的“' + message.Content + '”太复杂了,我不懂。'
 
        if (content === '1') {
            reply = '天下第一吃大米'
            console.log(message)
        }
        else if (content === '2') {
            reply = '天下第一吃豆腐'
        }
        else if (content === '3') {
            reply = '天下第一吃仙丹'
        }
        else if (content === '张杨海是谁') {
            reply = '张'
        }
        else if (content === '4') {
            reply = [{
                title: '技术改变世界',
                description: '只是个描述而已',
                picUrl: 'http://tu.dytt.com/20160426054059859.jpg',
                url:'http://virjay.com'
            }]
        }
        else if (content === '5') {
            var data = yield wechatApi.uploadMaterial('image', path.join(__dirname, '../2.jpg'))
 
            reply = {
                type: 'image',
                mediaId: data.media_id
            }
        }
        else if (content === '6') {
            var data = yield wechatApi.uploadMaterial('video', path.join(__dirname, '../6.mp4'))
            reply = {
                type: 'video',
                title: '蝙蝠侠:黑暗骑士',
                description: '蝙蝠侠与小丑的故事...',
                mediaId: data.media_id
            }
        }
        else if (content === '7') {
            var data = yield wechatApi.uploadMaterial('image', path.join(__dirname, '../2.jpg'))
            reply = {
                type: 'music',
                title: '音悦台',
                description: '放松一下...',
                musicUrl: 'http://virjay.com/7.mp3',
                thumbMediaId: data.media_id
            }
        }
        else if (content === '8') {
            var data = yield wechatApi.uploadMaterial('image', path.join(__dirname, '../2.jpg'),{type: 'image'})
 
            reply = {
                type: 'image',
                mediaId: data.media_id
            }
        }
        else if (content === '9') {
            var data = yield wechatApi.uploadMaterial('image', path.join(__dirname, '../6.mp4'),{type: 'video', description: '{"title":"batman", "introduction": "batman..."}'})
 
            reply = {
                type: 'video',
                title: '蝙蝠侠:黑暗骑士',
                description: '蝙蝠侠与小丑的故事...',
                mediaId: data.media_id
            }
        }
        else if (content === '10') {
            var picData = yield wechatApi.uploadMaterial('image', path.join(__dirname, '../2.jpg'),{})
 
            var media = {
                articles: [{
                    title: 'tututu',
                    thumb_media_id: picData.media_id,
                    author: 'virjay',
                    digest: '没有摘要',
                    show_cover_pic: 1,
                    content: '没有内容',
                    content_source_url: 'http://www.virjay.com'
                },{
                    title: 'tututu2',
                    thumb_media_id: picData.media_id,
                    author: 'virjay',
                    digest: '没有摘要',
                    show_cover_pic: 1,
                    content: '没有内容',
                    content_source_url: 'http://www.virjay.com'
                }]
            }
 
            data = yield wechatApi.uploadMaterial('news', media, {})
            data = yield wechatApi.fetchMaterial(data.media_id, 'news', {})
 
            console.log(data)
 
            var items = data.news_item
            var news = []
 
            items.forEach(function(items){
                news.push({
                    title: items.title,
                    description: items.digest,
                    picUrl: picData.url,
                    url: items.url
                })
            })
 
            reply = news
        }
        else if (content === '11') {
            var counts = yield wechatApi.countMaterial()
 
            console.log(JSON.stringify(counts))
 
            var results = yield [
                wechatApi.batchMaterial({
                    type: 'image',
                    offset: 0,
                    count: 10
                }),
                wechatApi.batchMaterial({
                    type: 'video',
                    offset: 0,
                    count: 10
                }),
                wechatApi.batchMaterial({
                    type: 'voice',
                    offset: 0,
                    count: 10
                }),
                wechatApi.batchMaterial({
                    type: 'news',
                    offset: 0,
                    count: 10
                })
            ]
             
            console.log(JSON.stringify(results))
 
            reply = '1'
        }
        else if (content === '12') {
            // var group = yield wechatApi.createGroup('wechat')
            // console.log('新分组 wechat:')
            // console.log(group)
 
            // var groups = yield wechatApi.fetchGroups()
            // console.log('加了 wechat 后的分组列表:')
            // console.log(groups)
 
            var group2 = yield wechatApi.checkGroup(message.FromUserName)
            console.log('查看自己的分组')
            console.log(group2)
 
            // var result = yield wechatApi.moveGroup(message.FromUserName,100)
            // console.log('移动到分组100')
            // console.log(result)
 
            // var result2 = yield wechatApi.moveGroup([message.FromUserName],101)
            // console.log('批量移动到分组101')
            // console.log(result2)
 
 
            var result3 = yield wechatApi.updateGroup(101, 'Custom')
            console.log('重命名结果:')
            console.log(result3)
 
            var groups3 = yield wechatApi.fetchGroups()
            console.log('更新后的分组列表:')
            console.log(groups3)
 
            // var result4 = yield wechatApi.deleteGroup(100)
            // console.log('删除100分组:')
            // console.log(result4)
 
            // var groups4 = yield wechatApi.fetchGroups()
            // console.log('更新后的分组列表:')
            // console.log(groups4)
 
            reply = 'Group done!'
        }
        else if (content === '13') {
            var user = yield wechatApi.fetchUsers(message.FromUserName, 'zh_CN')
 
            console.log(user)
 
            var openIds = [
                {
                    openid: message.FromUserName,
                    lang: 'zh_CN'
                }
            ]
 
            var users = yield wechatApi.fetchUsers(openIds)
            console.log(users)
 
            reply = JSON.stringify(user)
        }
        else if (content === '14') {
            var userlist = yield wechatApi.listUsers()
            console.log(userlist)
 
            reply = userlist.total
        }
        else if (content === '15') {
            var mpnews = {
                media_id: 'eSjpAvdDK68FjK2nRbbq5zXB_hzzICx8TJ8wn3-Io7s'
            }
            var test = {
                'content' 'test'
            }
            var msgData = yield wechatApi.sendByGroup('image', mpnews, 101)
 
            console.log(msgData)
 
            reply = 'Yeah!'
        }
        else if (content === '16') {
            var mpnews = {
                media_id: 'eSjpAvdDK68FjK2nRbbq5zXB_hzzICx8TJ8wn3-Io7s'
            }
            // var test = {
            //     'content' : 'test'
            // }
            var msgData = yield wechatApi.previewMass('image', mpnews, 'oO2E0wZFvj4hcDAcwTc5NE79Ib04')
 
            console.log(msgData)
 
            reply = 'Yeah!'
        }
        else if (content === '17') {
            var msgData = yield wechatApi.checkMass('6297401665371273792')
 
            console.log(msgData)
 
            reply = 'Yeah!'
        }
        this.body = reply
    }
    yield next
}


正在回答 回答被采纳积分+3

插入代码

5回答

Scott 2016-06-22 09:43:14

把 response.body 换成 response[1] 试试,看看是不是这里的版本导致的问题。

我用你的代码模拟,


1
2
Unhandled rejection Error: Get menu fails
    at /Users/black/scott/wechat-movie-for-test/wechat/wechat.js:861:7


代码是在这里出了错误:


1
2
var url = api.menu.del + 'access_token=' + data.access_token
request({url: url, json: true}).then(function(response){


我把 response.body 换成 response[1] 后就 ok 了,再检查下 app.js 里面引入的 reply 中间件是不是这一份文件。

0 回复 有任何疑惑可以回复我~
  • 提问者 杰克大叔 #1
    对了,这个问题搞定了,现在 'type'无法读取,和慕粉3278383遇到的情况一样
    回复 有任何疑惑可以回复我~ 2016-06-22 10:06:38
Scott 2016-06-21 22:17:23

把 wechat.js 这个构造函数的代码全部贴出来看一下,看是不是哪里少了 return

0 回复 有任何疑惑可以回复我~
  • 提问者 杰克大叔 #1
    更新了,在底下,提问内容太长了不能保存,老师帮忙看一下
    回复 有任何疑惑可以回复我~ 2016-06-21 22:40:20
小七努力变大牛 2016-08-27 00:56:23

我也是这边出问题了.... 找了好久 也木有看到... 麻烦能告诉一下   到底是哪里出问题了么?

0 回复 有任何疑惑可以回复我~
提问者 杰克大叔 2016-06-21 22:39:08
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
'user strict'
 
var Promise = require('bluebird')
var _ = require('lodash')
var request = Promise.promisify(require('request'))
var util = require('./util')
var fs = require('fs')
var prefix = 'https://api.weixin.qq.com/cgi-bin/'
var api = {
accessToken: prefix + 'token?grant_type=client_credential',
temporary: {
upload: prefix + 'media/upload?',
fetch: prefix + 'media/get?'
},
permanent: {
upload: prefix + 'material/add_material?',
fetch : prefix + 'material/get_material?',
uploadNews: prefix + 'material/add_news?',
uploadNewsPic: prefix + 'media/uploadimg?',
del: prefix + 'material/del_material?',
update: prefix + 'material/update_news?',
count: prefix + 'material/get_materialcount?',
batch: prefix + 'material/batchget_material?'
},
group: {
create: prefix + 'groups/create?',
fetch: prefix + 'groups/get?',
check: prefix + 'groups/getid?',
update: prefix + 'groups/update?',
move: prefix + 'groups/members/update?',
batchupdate: prefix + 'groups/members/batchupdate?',//移动分组
del: prefix + 'groups/delete?'
},
user: {
remark: prefix + 'user/info/updateremark?',
fetch: prefix + 'user/info?',
batchFetch: prefix + 'user/info/batchget?',
list: prefix + 'user/get?'
},
mass: {
group: prefix + 'message/mass/sendall?',
openId: prefix + 'message/mass/send?',
del : prefix + 'message/mass/delete?',
preview: prefix + 'message/mass/preview?',
check: prefix + 'message/mass/get?'
},
menu: {
create: prefix + 'menu/create?',
get: prefix + 'menu/get?',
del: prefix + 'menu/delete?',
current: prefix + 'get_current_selfmenu_info?'
}
}
 
function Wechat(opts){
var that = this
this.appID = opts.appID
this.appSecret = opts.appSecret
this.getAccessToken = opts.getAccessToken
this.saveAccessToken = opts.saveAccessToken
 
this.fetchAccessToken()
}
 
Wechat.prototype.fetchAccessToken = function(data){
var that = this
 
if (this.access_token && this.expires_in) {
if (this.isValidAccessToken(this)) {
return Promise.resolve(this)
}
}
 
return this.getAccessToken()
.then(function(data){
try{
data = JSON.parse(data)
}
catch(e){
return that.updateAccessToken(data)
}
 
if (that.isValidAccessToken(data)) {
return Promise.resolve(data)
}
else{
return that.updateAccessToken()
}
})
.then(function(data){
that.access_token = data.access_token
that.expires_in = data.expires_in
 
that.saveAccessToken(data)
 
return Promise.resolve(data)
})
}
 
Wechat.prototype.isValidAccessToken = function(data){
if (!data || !data.access_token || !data.expires_in) {
return false
}
 
var access_token = data.access_token
var expires_in = data.expires_in
var now = (new Date().getTime())
 
if (now < expires_in) {
return true
}
else{
return false
}
}
 
Wechat.prototype.updateAccessToken = function(data){
var appID = this.appID
var appSecret = this.appSecret
var url = api.accessToken + '&appid=' + appID + '&secret=' + appSecret
 
return new Promise(function(resolve, reject){
request({url: url, json: true}).then(function(response){
var data = response.body
var now = (new Date().getTime())
var expires_in = now + (data.expires_in - 20) * 1000
 
data.expires_in = expires_in
 
resolve(data)
})
})
}
 
Wechat.prototype.uploadMaterial = function(type, material, permanent){
var that = this
var form = {}
var uploadUrl = api.temporary.upload
if (permanent) {
uploadUrl = api.permanent.upload
 
_.extend(form, permanent)
}
 
if (type === 'pic') {
uploadUrl = api.permanent.uploadNewsPic
}
 
if (type === 'news') {
uploadUrl = api.permanent.uploadNews
form = material
}
else{
form.media = fs.createReadStream(material)
}
 
return new Promise(function(resolve, reject){
that
.fetchAccessToken()
.then(function(data){
var url = uploadUrl + 'access_token=' + data.access_token
if (!permanent) {
url += '&type=' + type
}
else {
form.access_token = data.access_token
}
var options = {
method : 'POST',
url: url,
json: true
}
 
if (type === 'news') {
options.body = form
}
else{
options.formData = form
}
request(options).then(function(response){
var _data = response.body
if (_data) {
resolve(_data)
}
else{
throw new Error('Upload material fails')
}
})
.catch(function(err){
reject(err)
})
})
 
 
})
}
 
Wechat.prototype.fetchMaterial = function(mediaId, type, permanent){
var that = this
var fetchUrl = api.temporary.fetch
 
if (permanent) {
fetchUrl = api.permanent.fetch
}
 
return new Promise(function(resolve, reject){
that
.fetchAccessToken()
.then(function(data){
var url = fetchUrl + 'access_token=' + data.access_token + '&media_id=' + mediaId
var form = {}
var options = {method: 'POST', url: url, json: true}
 
 
if (permanent) {
form.media_id = mediaId
form.access_token = data.access_token
options.body = form
}
else{
if (type === 'video') {
url = url.replace('https','http')
}
url += '&media_id' + mediaId
}
 
if (type === 'news' || type === 'video') {
request(options).then(function(response){
var _data = response.body
if (_data) {
resolve(_data)
}
else{
throw new Error('fetch material fails')
}
})
.catch(function(err){
reject(err)
})
}
else{
resolve(url)
}
})
})
}
 
Wechat.prototype.deleteMaterial = function(mediaId){
var that = this
var form = {
media_id: mediaId
}
 
return new Promise(function(resolve, reject){
that
.fetchAccessToken()
.then(function(data){
var url = api.permanent.del + 'access_token=' + data.access_token + '&media_id=' + mediaId
request({method: 'POST', url: url,body: form, json: true}).then(function(response){
var _data = response.body
if (_data) {
resolve(_data)
}
else{
throw new Error('Delete material fails')
}
})
.catch(function(err){
reject(err)
})
})
})
}
 
Wechat.prototype.updateMaterial = function(mediaId, news){
var that = this
var form = {
media_id: mediaId
}
 
_.extend(form, news)
 
return new Promise(function(resolve, reject){
that
.fetchAccessToken()
.then(function(data){
var url = api.permanent.update + 'access_token=' + data.access_token + '&media_id=' + mediaId
request({method: 'POST', url: url,body: form, json: true}).then(function(response){
var _data = response.body
if (_data) {
resolve(_data)
}
else{
throw new Error('Update material fails')
}
})
.catch(function(err){
reject(err)
})
})
})
}
 
Wechat.prototype.countMaterial = function(){
var that = this
 
return new Promise(function(resolve, reject){
that
.fetchAccessToken()
.then(function(data){
var url = api.permanent.count + 'access_token=' + data.access_token
request({method: 'GET', url: url,json: true}).then(function(response){
var _data = response.body
if (_data) {
resolve(_data)
}
else{
throw new Error('Count material fails')
}
})
.catch(function(err){
reject(err)
})
})
})
}
 
Wechat.prototype.batchMaterial = function(options){
var that = this
 
options.type = options.type || 'image'
options.offset = options.offset || 0
options.count = options.count || 1
 
return new Promise(function(resolve, reject){
that
.fetchAccessToken()
.then(function(data){
var url = api.permanent.batch + 'access_token=' + data.access_token
request({method: 'POST', url: url, body: options, json: true}).then(function(response){
var _data = response.body
if (_data) {
resolve(_data)
}
else{
throw new Error('Batch material fails')
}
})
.catch(function(err){
reject(err)
})
})
})
}
 
Wechat.prototype.createGroup = function(name){
var that = this
 
return new Promise(function(resolve, reject){
that
.fetchAccessToken()
.then(function(data){
var url = api.group.create + 'access_token=' + data.access_token
var form = {
group: {
name: name
}
}
request({method: 'POST', url: url, body: form, json: true}).then(function(response){
var _data = response.body
if (_data) {
resolve(_data)
}
else{
throw new Error('CreateGroup material fails')
}
})
.catch(function(err){
reject(err)
})
})
})
}
 
Wechat.prototype.fetchGroups = function(name){
var that = this
 
return new Promise(function(resolve, reject){
that
.fetchAccessToken()
.then(function(data){
var url = api.group.fetch + 'access_token=' + data.access_token
 
request({url: url, json: true}).then(function(response){
var _data = response.body
if (_data) {
resolve(_data)
}
else{
throw new Error('fetchGroup material fails')
}
})
.catch(function(err){
reject(err)
})
})
})
}
 
Wechat.prototype.checkGroup = function(openId){
var that = this
 
return new Promise(function(resolve, reject){
that
.fetchAccessToken()
.then(function(data){
var url = api.group.check + 'access_token=' + data.access_token
var form = {
openid: openId
}
console.log(form)
request({method: 'POST', url: url, body: form, json: true}).then(function(response){
var _data = response.body
if (_data) {
resolve(_data)
}
else{
throw new Error('checkGroup material fails')
}
})
.catch(function(err){
reject(err)
})
})
})
}
 
Wechat.prototype.updateGroup = function(id, name){
var that = this
 
return new Promise(function(resolve, reject){
that
.fetchAccessToken()
.then(function(data){
var url = api.group.update + 'access_token=' + data.access_token
var form = {
group: {
id: id,
name: name
}
}
console.log(url)
console.log(form)
request({method: 'POST', url: url, body: form, json: true}).then(function(response){
var _data = response.body
if (_data) {
resolve(_data)
}
else{
throw new Error('updateGroup material fails')
}
})
.catch(function(err){
reject(err)
})
})
})
}
 
Wechat.prototype.moveGroup = function(openIds, to){
var that = this
 
return new Promise(function(resolve, reject){
that
.fetchAccessToken()
.then(function(data){
var url
var form ={
to_groupid: to
}
 
if (_.isArray(openIds)) {
url = api.group.batchupdate + 'access_token=' + data.access_token
form.openid_list = openIds
}
else{
url = api.group.move + 'access_token=' + data.access_token
form.openid = openIds
}
request({method: 'POST', url: url, body: form, json: true}).then(function(response){
var _data = response.body
if (_data) {
resolve(_data)
}
else{
throw new Error('Move Group material fails')
}
})
.catch(function(err){
reject(err)
})
})
})
}
 
Wechat.prototype.deleteGroup = function(id){
var that = this
 
 
return new Promise(function(resolve, reject){
that
.fetchAccessToken()
.then(function(data){
url = api.group.del + 'access_token=' + data.access_token
var form ={
group: {
id: id
}
}
request({method: 'POST', url: url, body: form, json: true}).then(function(response){
var _data = response.body
if (_data) {
resolve(_data)
}
else{
throw new Error('Delete group material fails')
}
})
.catch(function(err){
reject(err)
})
})
})
}
 
Wechat.prototype.remarkUser = function(openId, remark){
var that = this
 
return new Promise(function(resolve, reject){
that
.fetchAccessToken()
.then(function(data){
url = api.user.remark + 'access_token=' + data.access_token
var form ={
openid: openId,
remark: remark
}
request({method: 'POST', url: url, body: form, json: true}).then(function(response){
var _data = response.body
if (_data) {
resolve(_data)
}
else{
throw new Error('Remark group material fails')
}
})
.catch(function(err){
reject(err)
})
})
})
}
 
Wechat.prototype.fetchUsers = function(openIds, lang){
var that = this
var lang =  lang || 'zh_CN'
 
return new Promise(function(resolve, reject){
that
.fetchAccessToken()
.then(function(data){
var options = {
json: true
 
}
if (_.isArray(openIds)) {
options.url = api.user.batchFetch + 'access_token=' + data.access_token
options.body ={
user_list: openIds
}
options.method = 'POST'
}
else{
options.url = api.user.fetch + 'access_token=' + data.access_token + '&openid=' + openIds + '&lang=' + lang
}
 
 
request(options).then(function(response){
var _data = response.body
if (_data) {
resolve(_data)
}
else{
throw new Error('batchFetchUsers material fails')
}
})
.catch(function(err){
reject(err)
})
})
})
}
 
Wechat.prototype.listUsers = function(openId){
var that = this
var lang =  lang || 'zh_CN'
 
return new Promise(function(resolve, reject){
that
.fetchAccessToken()
.then(function(data){
var url = api.user.list + 'access_token=' + data.access_token
if (openId) {
url +=  '&next_openid=' + openId
}
request({url: url, json: true}).then(function(response){
var _data = response.body
if (_data) {
resolve(_data)
}
else{
throw new Error('ListUsers fails')
}
})
.catch(function(err){
reject(err)
})
})
})
}
 
Wechat.prototype.sendByGroup = function(type, message, groupId){
var that = this
var msg = {
filter: {},
msgtype: type
}
 
msg[type] = message
 
if (!groupId) {
msg.filter.is_to_all = true
}
else{
msg.filter.is_to_all = false,
msg.filter = {
is_to_all : false,
group_id : groupId
}
}
 
return new Promise(function(resolve, reject){
that
.fetchAccessToken()
.then(function(data){
var url = api.mass.group + 'access_token=' + data.access_token
 
request({method: 'POST', url: url, body: msg, json: true}).then(function(response){
var _data = response.body
if (_data) {
resolve(_data)
}
else{
throw new Error('Send to group fails')
}
})
.catch(function(err){
reject(err)
})
})
})
}
 
Wechat.prototype.sendByOpenId = function(type, message, openIds){
var that = this
var msg = {
msgtype: type,
touser: openIds
}
 
msg[type] = message
 
return new Promise(function(resolve, reject){
that
.fetchAccessToken()
.then(function(data){
var url = api.mass.openId + 'access_token=' + data.access_token
 
request({method: 'POST', url: url, body: msg, json: true}).then(function(response){
var _data = response.body
if (_data) {
resolve(_data)
}
else{
throw new Error('Send ID to group fails')
}
})
.catch(function(err){
reject(err)
})
})
})
}
 
Wechat.prototype.deleteMass = function(msgId){
 
return new Promise(function(resolve, reject){
that
.fetchAccessToken()
.then(function(data){
var url = api.mass.del + 'access_token=' + data.access_token
 
var form = {
msg_id: msgId
}
 
request({method: 'POST', url: url, body: form, json: true}).then(function(response){
var _data = response.body
if (_data) {
resolve(_data)
}
else{
throw new Error('Delete ID to group fails')
}
})
.catch(function(err){
reject(err)
})
})
})
}
 
Wechat.prototype.previewMass = function(type, message, openId){
var that = this
 
var msg = {
msgtype: type,
touser: openId
}
 
msg[type] = message
 
return new Promise(function(resolve, reject){
that
.fetchAccessToken()
.then(function(data){
var url = api.mass.preview + 'access_token=' + data.access_token
 
request({method: 'POST', url: url, body: msg, json: true}).then(function(response){
var _data = response.body
if (_data) {
resolve(_data)
}
else{
throw new Error('Delete ID to group fails')
}
})
.catch(function(err){
reject(err)
})
})
})
}
 
Wechat.prototype.checkMass = function(msgId){
var that = this
 
return new Promise(function(resolve, reject){
that
.fetchAccessToken()
.then(function(data){
var url = api.mass.check + 'access_token=' + data.access_token
var form = {
msg_id: msgId
}
 
request({method: 'POST', url: url, body: form, json: true}).then(function(response){
var _data = response.body
if (_data) {
resolve(_data)
}
else{
throw new Error('checkMass ID to group fails')
}
})
.catch(function(err){
reject(err)
})
})
})
}
 
Wechat.prototype.createMenu = function(menu){
var that = this
console.log(menu)
return new Promise(function(resolve, reject){
that
.fetchAccessToken()
.then(function(data){
var url = api.menu.create + 'access_token=' + data.access_token
 
request({method: 'POST', url: url, body: menu, json: true}).then(function(response){
var _data = response.body
if (_data) {
resolve(_data)
}
else{
throw new Error('Create menu fails')
}
})
.catch(function(err){
reject(err)
})
})
})
}
 
Wechat.prototype.getMenu = function(){
var that = this
 
return new Promise(function(resolve, reject){
that
.fetchAccessToken()
.then(function(data){
var url = api.menu.get + 'access_token=' + data.access_token
 
request({url: url, json: true}).then(function(response){
var _data = response.body
if (_data) {
resolve(_data)
}
else{
throw new Error('Get menu fails')
}
})
.catch(function(err){
reject(err)
})
})
})
}
 
Wechat.prototype.deleteMenu = function(){
var that = this
 
return new Promise(function(resolve, reject){
that
.fetchAccessToken()
.then(function(data){
var url = api.menu.del + 'access_token=' + data.access_token
 
request({url: url, json: true}).then(function(response){
var _data = response.body
if (_data) {
resolve(_data)
}
else{
throw new Error('Get menu fails')
}
})
.catch(function(err){
reject(err)
})
})
})
}
 
Wechat.prototype.getCurrentMenu = function(){
var that = this
 
return new Promise(function(resolve, reject){
that
.fetchAccessToken()
.then(function(data){
var url = api.menu.current + 'access_token=' + data.access_token
 
request({url: url, json: true}).then(function(response){
var _data = response.body
if (_data) {
resolve(_data)
}
else{
throw new Error('Get current menu fails')
}
})
.catch(function(err){
reject(err)
})
})
})
}
 
Wechat.prototype.reply = function(){
var content = this.body
var message = this.weixin
 
var xml = util.tpl(content, message)
 
this.status = 200
this.type = 'application/xml'
this.body = xml
}
 
module.exports = Wechat


0 回复 有任何疑惑可以回复我~
yql 2016-06-20 16:02:56

仔细看视频,有个地方写漏掉了,我也遇到过,看能不能找到哪里漏了

0 回复 有任何疑惑可以回复我~
  • 提问者 杰克大叔 #1
    谢谢,我回去看看
    回复 有任何疑惑可以回复我~ 2016-06-20 16:59:54
  • 提问者 杰克大叔 #2
    找了一下,没有漏掉,还是不明白为什么会.then错误
    回复 有任何疑惑可以回复我~ 2016-06-21 10:05:13
  • 哪里漏掉了
    回复 有任何疑惑可以回复我~ 2016-08-07 18:34:04
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号