-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtcb25.html
954 lines (894 loc) · 224 KB
/
tcb25.html
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
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
<html><head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, minimal-ui">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<link rel="stylesheet" href="https://tcb-1251009918.cos.ap-guangzhou.myqcloud.com/pc/style.css" type="text/css" media="all">
<link rel="icon" href="https://tcb-1251009918.cos.ap-guangzhou.myqcloud.com/pc/icon.jpg">
<script>
window.location.href="https://cloudbase.net/community/guides/handbook/index.html"
</script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script type="text/javascript" async="" src="https://www.google-analytics.com/analytics.js"></script><script src="https://hm.baidu.com/hm.js?43ba6db32083f56ba6b97d4aa8900a0e"></script><script async="" src="https://www.googletagmanager.com/gtag/js?id=UA-42802757-2"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-42802757-2');
</script>
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?43ba6db32083f56ba6b97d4aa8900a0e";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
<link rel="dns-prefetch" href="//fonts.googleapis.com">
<link rel="dns-prefetch" href="//s.w.org">
<link href="https://fonts.gstatic.com" crossorigin="" rel="preconnect">
<link rel="alternate" type="application/rss+xml" title="云开发技术训练营 » Feed" href="https://cloudbasegroup.org/feed/">
<link rel="alternate" type="application/rss+xml" title="云开发技术训练营 » 评论Feed" href="https://cloudbasegroup.org/comments/feed/">
<link rel="alternate" type="application/rss+xml" title="云开发技术训练营 » 云开发与Nodejs评论Feed" href="https://cloudbasegroup.org/handbook/tcb/106/feed/">
<script type="text/javascript">
window._wpemojiSettings = {"baseUrl":"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/72x72\/","ext":".png","svgUrl":"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/","svgExt":".svg","source":{"concatemoji":"https:\/\/cloudbasegroup.org\/wp-includes\/js\/wp-emoji-release.min.js?ver=5.2.2"}};
!function(a,b,c){function d(a,b){var c=String.fromCharCode;l.clearRect(0,0,k.width,k.height),l.fillText(c.apply(this,a),0,0);var d=k.toDataURL();l.clearRect(0,0,k.width,k.height),l.fillText(c.apply(this,b),0,0);var e=k.toDataURL();return d===e}function e(a){var b;if(!l||!l.fillText)return!1;switch(l.textBaseline="top",l.font="600 32px Arial",a){case"flag":return!(b=d([55356,56826,55356,56819],[55356,56826,8203,55356,56819]))&&(b=d([55356,57332,56128,56423,56128,56418,56128,56421,56128,56430,56128,56423,56128,56447],[55356,57332,8203,56128,56423,8203,56128,56418,8203,56128,56421,8203,56128,56430,8203,56128,56423,8203,56128,56447]),!b);case"emoji":return b=d([55357,56424,55356,57342,8205,55358,56605,8205,55357,56424,55356,57340],[55357,56424,55356,57342,8203,55358,56605,8203,55357,56424,55356,57340]),!b}return!1}function f(a){var c=b.createElement("script");c.src=a,c.defer=c.type="text/javascript",b.getElementsByTagName("head")[0].appendChild(c)}var g,h,i,j,k=b.createElement("canvas"),l=k.getContext&&k.getContext("2d");for(j=Array("flag","emoji"),c.supports={everything:!0,everythingExceptFlag:!0},i=0;i<j.length;i++)c.supports[j[i]]=e(j[i]),c.supports.everything=c.supports.everything&&c.supports[j[i]],"flag"!==j[i]&&(c.supports.everythingExceptFlag=c.supports.everythingExceptFlag&&c.supports[j[i]]);c.supports.everythingExceptFlag=c.supports.everythingExceptFlag&&!c.supports.flag,c.DOMReady=!1,c.readyCallback=function(){c.DOMReady=!0},c.supports.everything||(h=function(){c.readyCallback()},b.addEventListener?(b.addEventListener("DOMContentLoaded",h,!1),a.addEventListener("load",h,!1)):(a.attachEvent("onload",h),b.attachEvent("onreadystatechange",function(){"complete"===b.readyState&&c.readyCallback()})),g=c.source||{},g.concatemoji?f(g.concatemoji):g.wpemoji&&g.twemoji&&(f(g.twemoji),f(g.wpemoji)))}(window,document,window._wpemojiSettings);
</script>
<style type="text/css">
img.wp-smiley,
img.emoji {
display: inline !important;
border: none !important;
box-shadow: none !important;
height: 1em !important;
width: 1em !important;
margin: 0 .07em !important;
vertical-align: -0.1em !important;
background: none !important;
padding: 0 !important;
}
</style>
<link rel="stylesheet" id="crayon-theme-monokai-css" href="https://tcb-1251009918.cos.ap-guangzhou.myqcloud.com/pc/cdn/wp-content/plugins/crayon-syntax-highlighter/themes/monokai/monokai.css?ver=_2.7.2_beta" type="text/css" media="all">
<link rel="stylesheet" id="crayon-font-monaco-css" href="https://tcb-1251009918.cos.ap-guangzhou.myqcloud.com/pc/cdn/wp-content/plugins/crayon-syntax-highlighter/fonts/monaco.css?ver=_2.7.2_beta" type="text/css" media="all">
<link rel="stylesheet" id="wp-block-library-css" href="https://tcb-1251009918.cos.ap-guangzhou.myqcloud.com/pc/cdn/wp-includes/css/dist/block-library/style.min.css?ver=5.2.2" type="text/css" media="all">
<link rel="stylesheet" id="dashicons-css" href="https://tcb-1251009918.cos.ap-guangzhou.myqcloud.com/pc/cdn/wp-includes/css/dashicons.min.css?ver=5.2.2" type="text/css" media="all">
<link rel="stylesheet" id="post-views-counter-frontend-css" href="https://tcb-1251009918.cos.ap-guangzhou.myqcloud.com/pc/cdn/wp-content/plugins/post-views-counter/css/frontend.css?ver=1.3.1" type="text/css" media="all">
<link rel="stylesheet" id="twentyfourteen-lato-css" href="https://fonts.googleapis.com/css?family=Lato%3A300%2C400%2C700%2C900%2C300italic%2C400italic%2C700italic&subset=latin%2Clatin-ext" type="text/css" media="all">
<link rel="stylesheet" id="genericons-css" href="https://tcb-1251009918.cos.ap-guangzhou.myqcloud.com/pc/cdn/wp-content/themes/twentyfourteen/genericons/genericons.css?ver=3.0.3" type="text/css" media="all">
<link rel="stylesheet" id="twentyfourteen-style-css" href="https://tcb-1251009918.cos.ap-guangzhou.myqcloud.com/pc/cdn/wp-content/themes/twentyfourteen/style.css?ver=5.2.2" type="text/css" media="all">
<!--[if lt IE 9]>
<link rel='stylesheet' id='twentyfourteen-ie-css' href='https://tcb-1251009918.cos.ap-guangzhou.myqcloud.com/pc/cdn/wp-content/themes/twentyfourteen/css/ie.css?ver=20131205' type='text/css' media='all' />
<![endif]-->
<script type="text/javascript" src="https://tcb-1251009918.cos.ap-guangzhou.myqcloud.com/pc/cdn/wp-includes/js/jquery/jquery.js?ver=1.12.4-wp"></script>
<script type="text/javascript" src="https://tcb-1251009918.cos.ap-guangzhou.myqcloud.com/pc/cdn/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.4.1"></script>
<link rel="https://api.w.org/" href="https://cloudbasegroup.org/wp-json/">
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="https://cloudbasegroup.org/xmlrpc.php?rsd">
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="https://tcb-1251009918.cos.ap-guangzhou.myqcloud.com/pc/cdn/wp-includes/wlwmanifest.xml">
<link rel="prev" title="存储、数组、对象" href="https://cloudbasegroup.org/handbook/tcb/104/">
<meta name="generator" content="WordPress 5.2.2">
<link rel="canonical" href="https://cloudbasegroup.org/handbook/tcb/106/">
<link rel="shortlink" href="https://cloudbasegroup.org/?p=106">
<link rel="alternate" type="application/json+oembed" href="https://cloudbasegroup.org/wp-json/oembed/1.0/embed?url=https%3A%2F%2Fcloudbasegroup.org%2Fhandbook%2Ftcb%2F106%2F">
<link rel="alternate" type="text/xml+oembed" href="https://cloudbasegroup.org/wp-json/oembed/1.0/embed?url=https%3A%2F%2Fcloudbasegroup.org%2Fhandbook%2Ftcb%2F106%2F&format=xml">
<style type="text/css">.recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}</style>
</head>
<body class="post-template-default single single-post postid-106 single-format-standard logged-in masthead-fixed full-width singular">
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<nav id="header-db" class="row navbar navbar-default">
<div class="navbar-header">
<div class="logotop fleft">
<a style="margin-top: 15px;" href="https://tencentcloudbase.github.io/handbook/tcb21.html" target="_blank"><img
src="https://tcb-1251009918.cos.ap-guangzhou.myqcloud.com/pc/icon.svg"></a>
</div>
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<div class="flex nav-items">
<h1 class="flex-item navbar-link">
云开发技术训练营</h1>
<div class="tcb-header__col tcb-header__col-right">
<ul class="tcb-header-nav">
<li class="tcb-header-nav__item ">
<a target="_blank" href="https://cloudbase.net/tools.html">工具</a></li>
<li class="tcb-header-nav__item "><a target="_blank"
href="https://cloudbase.net/sdk.html">SDK</a></li>
<li class="tcb-header-nav__item "><a href="https://docs.cloudbase.net" target="_blank">文档</a></li>
<li class="tcb-header-nav__item "><a target="_blank"
href="https://cloudbase.net/community.html">社区</a></li>
<li class="tcb-header-nav__item "><a target="_blank"
href="https://console.cloud.tencent.com/tcb?from=12341">去控制台</a></li>
</ul>
</div>
</div>
</div>
<div class="collapse navbar-collapse sop-sider" id="bs-example-navbar-collapse-1">
<div class="sop-menu">
<ul>
<li class="page_item no_current_page_item"><a><strong>小程序技术训练营</strong></a></li>
<li class="page_item no_current_page_item"><a href="./tcb01.html">小程序开发入门</a></li>
<li class="page_item no_current_page_item"><a href="./tcb02.html">WXML与WXSS</a></li>
<li class="page_item no_current_page_item"><a href="./tcb03.html">链接与图片</a></li>
<li class="page_item no_current_page_item"><a href="./tcb04.html">WeUI框架</a></li>
<li class="page_item no_current_page_item"><a href="./tcb05.html">渐变与动画</a></li>
<li class="page_item no_current_page_item"><a href="./tcb06.html">数据绑定</a></li>
<li class="page_item no_current_page_item"><a href="./tcb07.html">列表渲染与条件渲染</a></li>
<li class="page_item no_current_page_item"><a href="./tcb08.html">小程序组件</a></li>
<li class="page_item no_current_page_item"><a href="./tcb09.html">优化与部署上线</a></li>
<li class="page_item no_current_page_item"><a><strong>小程序与JavaScript</strong></a></li>
<li class="page_item no_current_page_item"><a href="./tcb11.html">JavaScript入门</a></li>
<li class="page_item no_current_page_item"><a href="./tcb12.html">点击事件</a></li>
<li class="page_item no_current_page_item"><a href="./tcb13.html">页面渲染</a></li>
<li class="page_item no_current_page_item"><a href="./tcb14.html">生命周期</a></li>
<li class="page_item no_current_page_item"><a href="./tcb15.html">数据表单</a></li>
<li class="page_item no_current_page_item"><a href="./tcb16.html">图片、缓存与文件</a></li>
<li class="page_item no_current_page_item"><a href="./tcb17.html">网络API</a></li>
<li class="page_item no_current_page_item"><a><strong>云开发技术训练营</strong></a></li>
<li class="page_item no_current_page_item"><a href="./tcb21.html">云开发快速入门</a></li>
<li class="page_item no_current_page_item"><a href="./tcb22.html">云开发能力</a></li>
<li class="page_item no_current_page_item"><a href="./tcb23.html">云数据库入门</a></li>
<li class="page_item no_current_page_item"><a href="./tcb24.html">存储、数组、对象</a></li>
<li class="page_item current_page_item"><a href="./tcb25.html">云开发与Nodejs</a></li>
</ul>
</div>
</div>
</nav>
<div class="sop-content">
<header class="entry-header">
<h1 class="entry-title">云开发与Nodejs</h1>
</header><!-- .entry-header -->
<div class="entry-content">
<p>云函数的运行环境是 Node.js,我们可以在云函数中使用Nodejs内置模块以及使用 npm 安装第三方依赖来帮助我们更快的开发。借助于一些优秀的开源项目,避免了我们重复造轮子,相比于小程序端,能够大大扩展云函数的使用</p>
<h4>云函数与Nodejs</h4>
<p>由于云函数与Nodejs息息相关,需要我们对云函数与Node的模块以及Nodejs的一些基本知识有一些基本的了解。下面只介绍一些基础的概念,如果你想详细深入了解,建议去翻阅一下Nodejs的官方技术文档:</p>
<p><strong>技术文档:</strong><a href="http://nodejs.cn/api/" target="_blank" rel="noopener noreferrer">Nodejs API 中文技术文档</a></p>
<p><strong>Nodejs的内置模块</strong></p>
<p>在前面我们已经接触过Nodejs的fs模块、path模块,这些我们称之为Nodejs的<strong>内置模块</strong>,内置模块不需要我们使用npm install下载,就可以直接使用require引入:</p>
<div id="crayon-5e40dd515700c792211943" class="crayon-syntax crayon-theme-monokai crayon-font-monaco crayon-os-mac print-yes notranslate crayon-wrapped" data-settings=" minimize scroll-mouseover disable-anim wrap" style="margin-top: 15px; margin-bottom: 15px; font-size: 14px !important; line-height: 20px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 20px !important; z-index: 0; opacity: 0; overflow: hidden;">const fs = require('fs')
const path = require('path')</textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -20px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5e40dd515700c792211943-1" style="height: 20px;">1</div><div class="crayon-num" data-line="crayon-5e40dd515700c792211943-2" style="height: 20px;">2</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5e40dd515700c792211943-1"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">fs</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'fs'</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd515700c792211943-2"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">path</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'path'</span><span class="crayon-sy">)</span></div></div></td>
</tr>
</tbody></table>
</div>
</div><p>Nodejs的常用内置模块以及功能如下所示,这些模块都是可以在云函数里直接使用的:</p>
<ul>
<li><strong><span id="crayon-5defa8f5b719b197864800" class="crayon-syntax crayon-syntax-inline crayon-theme-monokai crayon-theme-monokai-inline crayon-font-monaco"><span class="crayon-pre crayon-code"><span class="crayon-v">fs</span></span></span> 模块</strong>:文件目录的创建、删除、查询以及文件的读取和写入,下面的createReadStream方法类似于读取文件,</li>
<li><strong>path 模块:</strong>提供了一些用于处理文件路径的API</li>
<li><strong>url模块:</strong>用于处理与解析 URL</li>
<li><strong>http模块:</strong>用于创建一个能够处理和响应 http 响应的服务</li>
<li><strong>querystring模块:</strong>解析查询字符串</li>
<li><strong>until模块</strong> :提供用于解析和格式化 URL 查询字符串的实用工具;</li>
<li><strong>net模块:</strong>用于创建基于流的 TCP 或 IPC 的服务器</li>
<li><strong>crypto模块:</strong>提供加密功能,包括对 OpenSSL 的哈希、HMAC、加密、解密、签名、以及验证功能的一整套封装</li>
</ul>
<blockquote><p>在云函数中使用HTTP请求访问第三方服务可以不受域名限制,即不需要像小程序端一样,要将域名添加到request合法域名里;也不受http和https的限制,没有域名只有IP都是可以的,所以云函数可以应用的场景非常多,即能方便的调用第三方服务,也能够充当一个功能复杂的完整应用的后端。不过需要注意的是,云函数是部署在云端,有些局域网等终端通信的业务只能在小程序里进行。</p></blockquote>
<p><b>常用变量</b></p>
<p>module、exports、require</p>
<p>require用于引入模块、
<span id="crayon-5e40dd5157019789142470" class="crayon-syntax crayon-syntax-inline crayon-theme-monokai crayon-theme-monokai-inline crayon-font-monaco" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important;"><span class="crayon-pre crayon-code" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">JSON</span></span></span>、或本地文件。 可以从
<span id="crayon-5e40dd515701e501128169" class="crayon-syntax crayon-syntax-inline crayon-theme-monokai crayon-theme-monokai-inline crayon-font-monaco" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important;"><span class="crayon-pre crayon-code" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">node_modules</span></span></span> 引入模块,可以使用相对路径(例如
<span id="crayon-5e40dd5157020571255931" class="crayon-syntax crayon-syntax-inline crayon-theme-monokai crayon-theme-monokai-inline crayon-font-monaco" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important;"><span class="crayon-pre crayon-code" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-sy">.</span><span class="crayon-o">/</span></span></span>、)引入本地模块或 JSON 文件,路径会根据 <a href="http://nodejs.cn/s/etUQhi" target="_blank" rel="nofollow noopener noreferrer">
<span id="crayon-5e40dd5157021678716918" class="crayon-syntax crayon-syntax-inline crayon-theme-monokai crayon-theme-monokai-inline crayon-font-monaco" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important;"><span class="crayon-pre crayon-code" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">__dirname</span></span></span></a> 定义的目录名或当前工作目录进行处理。</p>
<p>node模块化遵循的是commonjs规范,CommonJs定义的模块分为: 模块标识(module)、模块导出(exports) 、模块引用(require)。</p>
<p>在node中,一个文件即一个模块,使用exports和require来进行处理。</p>
<p>exports表示该模块运行时生成的导出对象。如果按确切的文件名没有找到模块,则 Node.js 会尝试带上
<span id="crayon-5e40dd5157023333919575" class="crayon-syntax crayon-syntax-inline crayon-theme-monokai crayon-theme-monokai-inline crayon-font-monaco" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important;"><span class="crayon-pre crayon-code" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-sy">.</span><span class="crayon-v">js</span></span></span>、
<span id="crayon-5e40dd5157024495720688" class="crayon-syntax crayon-syntax-inline crayon-theme-monokai crayon-theme-monokai-inline crayon-font-monaco" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important;"><span class="crayon-pre crayon-code" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-sy">.</span><span class="crayon-v">json</span></span></span> 或
<span id="crayon-5e40dd5157026520131432" class="crayon-syntax crayon-syntax-inline crayon-theme-monokai crayon-theme-monokai-inline crayon-font-monaco" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important;"><span class="crayon-pre crayon-code" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-sy">.</span><span class="crayon-v">node</span></span></span> 拓展名再加载。
<span id="crayon-5e40dd5157027955425709" class="crayon-syntax crayon-syntax-inline crayon-theme-monokai crayon-theme-monokai-inline crayon-font-monaco" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important;"><span class="crayon-pre crayon-code" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-sy">.</span><span class="crayon-v">js</span></span></span> 文件会被解析为 JavaScript 文本文件,
<span id="crayon-5e40dd515702a039517285" class="crayon-syntax crayon-syntax-inline crayon-theme-monokai crayon-theme-monokai-inline crayon-font-monaco" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important;"><span class="crayon-pre crayon-code" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-sy">.</span><span class="crayon-v">json</span></span></span> 文件会被解析为 JSON 文本文件。
<span id="crayon-5e40dd515702c395475066" class="crayon-syntax crayon-syntax-inline crayon-theme-monokai crayon-theme-monokai-inline crayon-font-monaco" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important;"><span class="crayon-pre crayon-code" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-sy">.</span><span class="crayon-v">node</span></span></span> 文件会被解析为通过
<span id="crayon-5e40dd515702e194791066" class="crayon-syntax crayon-syntax-inline crayon-theme-monokai crayon-theme-monokai-inline crayon-font-monaco" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important;"><span class="crayon-pre crayon-code" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">process</span><span class="crayon-sy">.</span><span class="crayon-e">dlopen</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></span></span> 加载的编译后的插件模块。以
<span id="crayon-5e40dd515702f277301057" class="crayon-syntax crayon-syntax-inline crayon-theme-monokai crayon-theme-monokai-inline crayon-font-monaco" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important;"><span class="crayon-pre crayon-code" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-s">'/'</span></span></span> 为前缀的模块是文件的绝对路径。 例如,
<span id="crayon-5e40dd5157030123844109" class="crayon-syntax crayon-syntax-inline crayon-theme-monokai crayon-theme-monokai-inline crayon-font-monaco" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important;"><span class="crayon-pre crayon-code" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'/home/marco/foo.js'</span><span class="crayon-sy">)</span></span></span> 会加载
<span id="crayon-5e40dd5157032972148120" class="crayon-syntax crayon-syntax-inline crayon-theme-monokai crayon-theme-monokai-inline crayon-font-monaco" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important;"><span class="crayon-pre crayon-code" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-o">/</span><span class="crayon-v">home</span><span class="crayon-o">/</span><span class="crayon-v">marco</span><span class="crayon-o">/</span><span class="crayon-v">foo</span><span class="crayon-sy">.</span><span class="crayon-v">js</span></span></span> 文件。以
<span id="crayon-5e40dd5157033072454631" class="crayon-syntax crayon-syntax-inline crayon-theme-monokai crayon-theme-monokai-inline crayon-font-monaco" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important;"><span class="crayon-pre crayon-code" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-s">'./'</span></span></span> 为前缀的模块是相对于调用
<span id="crayon-5e40dd5157035081107225" class="crayon-syntax crayon-syntax-inline crayon-theme-monokai crayon-theme-monokai-inline crayon-font-monaco" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important;"><span class="crayon-pre crayon-code" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></span></span> 的文件的。 也就是说,
<span id="crayon-5e40dd5157036733645902" class="crayon-syntax crayon-syntax-inline crayon-theme-monokai crayon-theme-monokai-inline crayon-font-monaco" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important;"><span class="crayon-pre crayon-code" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">circle</span><span class="crayon-sy">.</span><span class="crayon-v">js</span></span></span> 必须和
<span id="crayon-5e40dd5157037515314143" class="crayon-syntax crayon-syntax-inline crayon-theme-monokai crayon-theme-monokai-inline crayon-font-monaco" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important;"><span class="crayon-pre crayon-code" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">foo</span><span class="crayon-sy">.</span><span class="crayon-v">js</span></span></span> 在同一目录下以便于
<span id="crayon-5e40dd5157039236518110" class="crayon-syntax crayon-syntax-inline crayon-theme-monokai crayon-theme-monokai-inline crayon-font-monaco" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important;"><span class="crayon-pre crayon-code" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'./circle'</span><span class="crayon-sy">)</span></span></span> 找到它。</p>
<p>
<span id="crayon-5e40dd515703a311866688" class="crayon-syntax crayon-syntax-inline crayon-theme-monokai crayon-theme-monokai-inline crayon-font-monaco" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important;"><span class="crayon-pre crayon-code" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">module</span><span class="crayon-sy">.</span><span class="crayon-v">exports</span></span></span> 用于指定一个模块所导出的内容,即可以通过
<span id="crayon-5e40dd515703c676960246" class="crayon-syntax crayon-syntax-inline crayon-theme-monokai crayon-theme-monokai-inline crayon-font-monaco" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important;"><span class="crayon-pre crayon-code" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></span></span> 访问的内容。</p>
<div id="crayon-5e40dd515703d655118414" class="crayon-syntax crayon-theme-monokai crayon-font-monaco crayon-os-mac print-yes notranslate crayon-wrapped" data-settings=" minimize scroll-mouseover disable-anim wrap" style="margin-top: 15px; margin-bottom: 15px; font-size: 14px !important; line-height: 20px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 20px !important; z-index: 0; opacity: 0; overflow: hidden;">// 引入本地模块:
const myLocalModule = require('./path/myLocalModule');
// 引入 JSON 文件:
const jsonData = require('./path/filename.json');
// 引入 node_modules 模块或 Node.js 内置模块:
const crypto = require('crypto');</textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -20px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5e40dd515703d655118414-1" style="height: 20px;">1</div><div class="crayon-num" data-line="crayon-5e40dd515703d655118414-2" style="height: 20px;">2</div><div class="crayon-num" data-line="crayon-5e40dd515703d655118414-3" style="height: 20px;">3</div><div class="crayon-num" data-line="crayon-5e40dd515703d655118414-4" style="height: 20px;">4</div><div class="crayon-num" data-line="crayon-5e40dd515703d655118414-5" style="height: 20px;">5</div><div class="crayon-num" data-line="crayon-5e40dd515703d655118414-6" style="height: 20px;">6</div><div class="crayon-num" data-line="crayon-5e40dd515703d655118414-7" style="height: 20px;">7</div><div class="crayon-num" data-line="crayon-5e40dd515703d655118414-8" style="height: 20px;">8</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5e40dd515703d655118414-1"><span class="crayon-c">// 引入本地模块:</span></div><div class="crayon-line" id="crayon-5e40dd515703d655118414-2"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">myLocalModule</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'./path/myLocalModule'</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd515703d655118414-3"> </div><div class="crayon-line" id="crayon-5e40dd515703d655118414-4"><span class="crayon-c">// 引入 JSON 文件:</span></div><div class="crayon-line" id="crayon-5e40dd515703d655118414-5"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">jsonData</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'./path/filename.json'</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd515703d655118414-6"> </div><div class="crayon-line" id="crayon-5e40dd515703d655118414-7"><span class="crayon-c">// 引入 node_modules 模块或 Node.js 内置模块:</span></div><div class="crayon-line" id="crayon-5e40dd515703d655118414-8"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">crypto</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'crypto'</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div></div></td>
</tr>
</tbody></table>
</div>
</div><p><strong>wx-server-sdk的模块</strong></p>
<p>tcb-admin-node、protobuf、jstslib</p>
<p><strong>第三方模块</strong></p>
<p>Nodejs有 <a href="https://www.npmjs.com/" target="_blank" rel="noopener noreferrer">npm官网地址</a></p>
<p><strong>Nodejs库推荐:</strong><a href="https://github.com/sindresorhus/awesome-nodejs" target="_blank" rel="noopener noreferrer">awesome Nodejs</a></p>
<p>当没有以
<span id="crayon-5e40dd5157041781170779" class="crayon-syntax crayon-syntax-inline crayon-theme-monokai crayon-theme-monokai-inline crayon-font-monaco" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important;"><span class="crayon-pre crayon-code" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-s">'/'</span></span></span>、
<span id="crayon-5e40dd5157042437783479" class="crayon-syntax crayon-syntax-inline crayon-theme-monokai crayon-theme-monokai-inline crayon-font-monaco" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important;"><span class="crayon-pre crayon-code" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-s">'./'</span></span></span> 或
<span id="crayon-5e40dd5157044142892376" class="crayon-syntax crayon-syntax-inline crayon-theme-monokai crayon-theme-monokai-inline crayon-font-monaco" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important;"><span class="crayon-pre crayon-code" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-s">'../'</span></span></span> 开头来表示文件时,这个模块必须是一个核心模块或加载自
<span id="crayon-5e40dd5157045392467631" class="crayon-syntax crayon-syntax-inline crayon-theme-monokai crayon-theme-monokai-inline crayon-font-monaco" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important;"><span class="crayon-pre crayon-code" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">node_modules</span></span></span> 目录,比如wx-server-sdk就加载自node_modules文件夹:</p>
<div id="crayon-5e40dd5157047737684117" class="crayon-syntax crayon-theme-monokai crayon-font-monaco crayon-os-mac print-yes notranslate crayon-wrapped" data-settings=" minimize scroll-mouseover disable-anim wrap" style="margin-top: 15px; margin-bottom: 15px; font-size: 14px !important; line-height: 20px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 20px !important; z-index: 0; opacity: 0; overflow: hidden;">const cloud = require('wx-server-sdk')</textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -20px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5e40dd5157047737684117-1" style="height: 20px;">1</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5e40dd5157047737684117-1"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">cloud</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'wx-server-sdk'</span><span class="crayon-sy">)</span></div></div></td>
</tr>
</tbody></table>
</div>
</div><p></p>
<h4>Lodash实用工具库</h4>
<p>Lodash是一个一致性、模块化、高性能的 JavaScript <strong>实用工具库</strong>,<span class="rwd-line">通过降低 array、number、objects、string 等数据类型的使用难度从而让 JavaScript 变得更简单。</span><span class="rwd-line">Lodash 的模块化方法非常适用于:</span>遍历 array、object 和 string;对值进行操作和检测;创建符合功能的函数。</p>
<p><strong>技术文档:</strong><a href="https://lodash.com/" target="_blank" rel="noopener noreferrer">Lodash官方文档</a>、<a href="https://www.lodashjs.com/" target="_blank" rel="noopener noreferrer">Lodash中文文档</a></p>
<p>使用开发者工具新建一个云函数,比如lodash,然后在package.json<strong>增加</strong>lodash最新版latest的依赖:</p>
<div id="crayon-5e40dd5157048585657318" class="crayon-syntax crayon-theme-monokai crayon-font-monaco crayon-os-mac print-yes notranslate crayon-wrapped" data-settings=" minimize scroll-mouseover disable-anim wrap" style="margin-top: 15px; margin-bottom: 15px; font-size: 14px !important; line-height: 20px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 20px !important; z-index: 0; opacity: 0; overflow: hidden;"> "dependencies": {
"lodash": "latest"
}</textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -20px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5e40dd5157048585657318-1" style="height: 20px;">1</div><div class="crayon-num" data-line="crayon-5e40dd5157048585657318-2" style="height: 20px;">2</div><div class="crayon-num" data-line="crayon-5e40dd5157048585657318-3" style="height: 20px;">3</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5e40dd5157048585657318-1"><span class="crayon-h"> </span><span class="crayon-s">"dependencies"</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157048585657318-2"><span class="crayon-h"> </span><span class="crayon-s">"lodash"</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-s">"latest"</span></div><div class="crayon-line" id="crayon-5e40dd5157048585657318-3"><span class="crayon-h"> </span><span class="crayon-sy">}</span></div></div></td>
</tr>
</tbody></table>
</div>
</div><p>在index.js里的代码修改为如下,这里使用到了lodash的chunk方法来分割数组:</p>
<div id="crayon-5e40dd515704a592313891" class="crayon-syntax crayon-theme-monokai crayon-font-monaco crayon-os-mac print-yes notranslate crayon-wrapped" data-settings=" minimize scroll-mouseover disable-anim wrap" style="margin-top: 15px; margin-bottom: 15px; font-size: 14px !important; line-height: 20px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 20px !important; z-index: 0; opacity: 0; overflow: hidden;">const cloud = require('wx-server-sdk')
var _ = require('lodash');
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV,
})
exports.main = async (event, context) => {
//将数组拆分为长度为2的数组
const arr= _.chunk(['a', 'b', 'c', 'd'], 2);
return arr
}</textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -28px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5e40dd515704a592313891-1" style="height: 20px;">1</div><div class="crayon-num" data-line="crayon-5e40dd515704a592313891-2" style="height: 20px;">2</div><div class="crayon-num" data-line="crayon-5e40dd515704a592313891-3" style="height: 20px;">3</div><div class="crayon-num" data-line="crayon-5e40dd515704a592313891-4" style="height: 20px;">4</div><div class="crayon-num" data-line="crayon-5e40dd515704a592313891-5" style="height: 20px;">5</div><div class="crayon-num" data-line="crayon-5e40dd515704a592313891-6" style="height: 20px;">6</div><div class="crayon-num" data-line="crayon-5e40dd515704a592313891-7" style="height: 20px;">7</div><div class="crayon-num" data-line="crayon-5e40dd515704a592313891-8" style="height: 20px;">8</div><div class="crayon-num" data-line="crayon-5e40dd515704a592313891-9" style="height: 20px;">9</div><div class="crayon-num" data-line="crayon-5e40dd515704a592313891-10" style="height: 20px;">10</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5e40dd515704a592313891-1"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">cloud</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'wx-server-sdk'</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd515704a592313891-2"><span class="crayon-t">var</span><span class="crayon-h"> </span><span class="crayon-v">_</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'lodash'</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd515704a592313891-3"><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-e">init</span><span class="crayon-sy">(</span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd515704a592313891-4"><span class="crayon-h"> </span><span class="crayon-v">env</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-v">DYNAMIC_CURRENT_ENV</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd515704a592313891-5"><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd515704a592313891-6"><span class="crayon-v">exports</span><span class="crayon-sy">.</span><span class="crayon-v">main</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">async</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-v">event</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">context</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-o">></span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd515704a592313891-7"><span class="crayon-h"> </span><span class="crayon-c">//将数组拆分为长度为2的数组</span></div><div class="crayon-line" id="crayon-5e40dd515704a592313891-8"><span class="crayon-h"> </span><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">arr</span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">_</span><span class="crayon-sy">.</span><span class="crayon-e">chunk</span><span class="crayon-sy">(</span><span class="crayon-sy">[</span><span class="crayon-s">'a'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-s">'b'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-s">'c'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-s">'d'</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">2</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd515704a592313891-9"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-i">arr</span></div><div class="crayon-line" id="crayon-5e40dd515704a592313891-10"><span class="crayon-sy">}</span></div></div></td>
</tr>
</tbody></table>
</div>
</div><p>右键lodash云函数目录,选择“在终端中打开”,npm install 安装模块之后右键部署并上传所有文件。我们就可以通过多种方式来调用它(前面已详细介绍)即可获得结果。Lodash作为工具,非常好用且实用,<strong>它的源码也非常值得学习</strong>,更多相关内容则需要大家去Github和官方技术文档里深入了解。</p>
<blockquote><p>在<a href="https://github.com/sindresorhus/awesome-nodejs" target="_blank" rel="noopener noreferrer">awesome Nodejs</a>页面我们了解到还有Ramba、immutable、Mout等类似工具库,这些都非常推荐。借助于Github的awesome清单,我们就能一手掌握最酷炫好用的开源项目,避免了自己去收集收藏。</p></blockquote>
<h4>moment时间处理</h4>
<p>开发小程序时经常需要格式化时间、处理相对时间、日历时间以及时间的多语言问题,这个时候就可以使用比较流行的momentjs了。</p>
<p><strong>技术文档:</strong><a href="https://momentjs.com/">moment官方文档</a>、<a href="http://momentjs.cn/" target="_blank" rel="noopener noreferrer">moment中文文档</a></p>
<p>使用开发者工具新建一个云函数,比如moment,然后在package.json<strong>增加</strong>moment最新版latest的依赖:</p>
<div id="crayon-5e40dd515704c099715673" class="crayon-syntax crayon-theme-monokai crayon-font-monaco crayon-os-mac print-yes notranslate crayon-wrapped" data-settings=" minimize scroll-mouseover disable-anim wrap" style="margin-top: 15px; margin-bottom: 15px; font-size: 14px !important; line-height: 20px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 20px !important; z-index: 0; opacity: 0; overflow: hidden;"> "dependencies": {
"moment": "latest"
}</textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -20px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5e40dd515704c099715673-1" style="height: 20px;">1</div><div class="crayon-num" data-line="crayon-5e40dd515704c099715673-2" style="height: 20px;">2</div><div class="crayon-num" data-line="crayon-5e40dd515704c099715673-3" style="height: 20px;">3</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5e40dd515704c099715673-1"><span class="crayon-h"> </span><span class="crayon-s">"dependencies"</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd515704c099715673-2"><span class="crayon-h"> </span><span class="crayon-s">"moment"</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-s">"latest"</span></div><div class="crayon-line" id="crayon-5e40dd515704c099715673-3"><span class="crayon-h"> </span><span class="crayon-sy">}</span></div></div></td>
</tr>
</tbody></table>
</div>
</div><p>在index.js里的代码修改为如下,我们将moment区域设置为中国,将时间格式化为
<span id="crayon-5e40dd515704e716536120" class="crayon-syntax crayon-syntax-inline crayon-theme-monokai crayon-theme-monokai-inline crayon-font-monaco" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important;"><span class="crayon-pre crayon-code" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">十二月<span class="crayon-h"> </span><span class="crayon-cn">23</span>日<span class="crayon-h"> </span><span class="crayon-cn">2019</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">4</span><span class="crayon-o">:</span><span class="crayon-cn">13</span><span class="crayon-o">:</span><span class="crayon-cn">29</span><span class="crayon-h"> </span>下午</span></span>的样式以及相对时间多少分钟前:</p>
<div id="crayon-5e40dd515704f478289676" class="crayon-syntax crayon-theme-monokai crayon-font-monaco crayon-os-mac print-yes notranslate crayon-wrapped" data-settings=" minimize scroll-mouseover disable-anim wrap" style="margin-top: 15px; margin-bottom: 15px; font-size: 14px !important; line-height: 20px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 20px !important; z-index: 0; opacity: 0; overflow: hidden;">const cloud = require('wx-server-sdk')
const moment = require("moment");
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV,
})
exports.main = async (event, context) => {
moment.locale('zh-cn');
time1 = moment().format('MMMM Do YYYY, h:mm:ss a');
time2 = moment().startOf('hour').fromNow();
return { time1,time2}
}</textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -28px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5e40dd515704f478289676-1" style="height: 20px;">1</div><div class="crayon-num" data-line="crayon-5e40dd515704f478289676-2" style="height: 20px;">2</div><div class="crayon-num" data-line="crayon-5e40dd515704f478289676-3" style="height: 20px;">3</div><div class="crayon-num" data-line="crayon-5e40dd515704f478289676-4" style="height: 20px;">4</div><div class="crayon-num" data-line="crayon-5e40dd515704f478289676-5" style="height: 20px;">5</div><div class="crayon-num" data-line="crayon-5e40dd515704f478289676-6" style="height: 20px;">6</div><div class="crayon-num" data-line="crayon-5e40dd515704f478289676-7" style="height: 20px;">7</div><div class="crayon-num" data-line="crayon-5e40dd515704f478289676-8" style="height: 20px;">8</div><div class="crayon-num" data-line="crayon-5e40dd515704f478289676-9" style="height: 20px;">9</div><div class="crayon-num" data-line="crayon-5e40dd515704f478289676-10" style="height: 20px;">10</div><div class="crayon-num" data-line="crayon-5e40dd515704f478289676-11" style="height: 20px;">11</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5e40dd515704f478289676-1"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">cloud</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'wx-server-sdk'</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd515704f478289676-2"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">moment</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">"moment"</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd515704f478289676-3"><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-e">init</span><span class="crayon-sy">(</span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd515704f478289676-4"><span class="crayon-h"> </span><span class="crayon-v">env</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-v">DYNAMIC_CURRENT_ENV</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd515704f478289676-5"><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd515704f478289676-6"><span class="crayon-v">exports</span><span class="crayon-sy">.</span><span class="crayon-v">main</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">async</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-v">event</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">context</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-o">></span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd515704f478289676-7"><span class="crayon-h"> </span><span class="crayon-v">moment</span><span class="crayon-sy">.</span><span class="crayon-e">locale</span><span class="crayon-sy">(</span><span class="crayon-s">'zh-cn'</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd515704f478289676-8"><span class="crayon-h"> </span><span class="crayon-v">time1</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">moment</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">.</span><span class="crayon-e">format</span><span class="crayon-sy">(</span><span class="crayon-s">'MMMM Do YYYY, h:mm:ss a'</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd515704f478289676-9"><span class="crayon-h"> </span><span class="crayon-v">time2</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">moment</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">.</span><span class="crayon-e">startOf</span><span class="crayon-sy">(</span><span class="crayon-s">'hour'</span><span class="crayon-sy">)</span><span class="crayon-sy">.</span><span class="crayon-e">fromNow</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd515704f478289676-10"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-sy">{</span><span class="crayon-h"> </span><span class="crayon-v">time1</span><span class="crayon-sy">,</span><span class="crayon-v">time2</span><span class="crayon-sy">}</span></div><div class="crayon-line" id="crayon-5e40dd515704f478289676-11"><span class="crayon-sy">}</span></div></div></td>
</tr>
</tbody></table>
</div>
</div><p>不过云函数中的时区为 UTC+0,不是 UTC+8,格式化得到的时间和在国内的时间是有8个小时的时间差的,我们可以给小时数+8,也可以修改时区。云函数修改时区我们可以使用timezone依赖(和moment是同一个开源作者)。</p>
<p><strong>技术文档:</strong><a href="https://momentjs.com/timezone/" target="_blank" rel="noopener noreferrer">timezone技术文档</a></p>
<p>在package.json<strong>增加</strong>moment-timezone最新版latest的依赖,然后修改上面相应的代码即可,使用起来非常方便:</p>
<div id="crayon-5e40dd5157051943047784" class="crayon-syntax crayon-theme-monokai crayon-font-monaco crayon-os-mac print-yes notranslate crayon-wrapped" data-settings=" minimize scroll-mouseover disable-anim wrap" style="margin-top: 15px; margin-bottom: 15px; font-size: 14px !important; line-height: 20px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 20px !important; z-index: 0; opacity: 0; overflow: hidden;">const moment = require('moment-timezone');
time1 = moment().tz('Asia/Shanghai').format('MMMM Do YYYY, h:mm:ss a');</textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -20px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5e40dd5157051943047784-1" style="height: 20px;">1</div><div class="crayon-num" data-line="crayon-5e40dd5157051943047784-2" style="height: 20px;">2</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5e40dd5157051943047784-1"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">moment</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'moment-timezone'</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd5157051943047784-2"><span class="crayon-v">time1</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">moment</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">.</span><span class="crayon-e">tz</span><span class="crayon-sy">(</span><span class="crayon-s">'Asia/Shanghai'</span><span class="crayon-sy">)</span><span class="crayon-sy">.</span><span class="crayon-e">format</span><span class="crayon-sy">(</span><span class="crayon-s">'MMMM Do YYYY, h:mm:ss a'</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div></div></td>
</tr>
</tbody></table>
</div>
</div><p></p>
<h4>获取公网IP</h4>
<p>有时我们希望能够获取到服务器的公网IP,比如用于IP地址的白名单,或者想根据IP查询到服务器所在的地址,ipify就是一个免费好用的依赖,通过它我们也可以获取到云函数所在服务器的公网IP。</p>
<p><strong>技术文档:</strong><a href="https://github.com/sindresorhus/ipify" target="_blank" rel="noopener noreferrer">ipify Github地址</a></p>
<p>使用开发者工具新建一个getip的云函数,然后输入以下代码,并在package.json的”dependencies”里新增
<span id="crayon-5e40dd5157052916051233" class="crayon-syntax crayon-syntax-inline crayon-theme-monokai crayon-theme-monokai-inline crayon-font-monaco" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important;"><span class="crayon-pre crayon-code" style="font-size: 14px !important; line-height: 20px !important;font-size: 14px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-s">"ipify"</span><span class="crayon-o">:</span><span class="crayon-s">"latest"</span></span></span> ,即最新版的ipify依赖:</p>
<div id="crayon-5e40dd5157054336637165" class="crayon-syntax crayon-theme-monokai crayon-font-monaco crayon-os-mac print-yes notranslate crayon-wrapped" data-settings=" minimize scroll-mouseover disable-anim wrap" style="margin-top: 15px; margin-bottom: 15px; font-size: 14px !important; line-height: 20px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 20px !important; z-index: 0; opacity: 0; overflow: hidden;">const cloud = require('wx-server-sdk')
const ipify = require('ipify');
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV,
})
exports.main = async (event, context) => {
return await ipify({ useIPv6: false })
}</textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -20px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5e40dd5157054336637165-1" style="height: 20px;">1</div><div class="crayon-num" data-line="crayon-5e40dd5157054336637165-2" style="height: 20px;">2</div><div class="crayon-num" data-line="crayon-5e40dd5157054336637165-3" style="height: 20px;">3</div><div class="crayon-num" data-line="crayon-5e40dd5157054336637165-4" style="height: 20px;">4</div><div class="crayon-num" data-line="crayon-5e40dd5157054336637165-5" style="height: 20px;">5</div><div class="crayon-num" data-line="crayon-5e40dd5157054336637165-6" style="height: 20px;">6</div><div class="crayon-num" data-line="crayon-5e40dd5157054336637165-7" style="height: 20px;">7</div><div class="crayon-num" data-line="crayon-5e40dd5157054336637165-8" style="height: 20px;">8</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5e40dd5157054336637165-1"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">cloud</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'wx-server-sdk'</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157054336637165-2"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">ipify</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'ipify'</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd5157054336637165-3"><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-e">init</span><span class="crayon-sy">(</span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157054336637165-4"><span class="crayon-h"> </span><span class="crayon-v">env</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-v">DYNAMIC_CURRENT_ENV</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157054336637165-5"><span class="crayon-sy">}</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157054336637165-6"><span class="crayon-v">exports</span><span class="crayon-sy">.</span><span class="crayon-v">main</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">async</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-v">event</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">context</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-o">></span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157054336637165-7"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-e">await</span><span class="crayon-h"> </span><span class="crayon-e">ipify</span><span class="crayon-sy">(</span><span class="crayon-sy">{</span><span class="crayon-h"> </span><span class="crayon-v">useIPv6</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-t">false</span><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157054336637165-8"><span class="crayon-sy">}</span></div></div></td>
</tr>
</tbody></table>
</div>
</div><p>然后右键getip云函数根目录,选择在终端中打开,输入npm install安装依赖,之后上传并部署所有文件。我们可以在小程序端调用这个云函数,就可以得到云函数服务器的公网IP,这个IP是随机而有限的几个,反复调用getip,就能够穷举所有云函数所在服务器的ip了。</p>
<p>可能你会在使用云函数连接数据库或者用云函数来建微信公众号的后台时需要用到IP白名单,我们可以把这些ip都添加到白名单里面,这样云函数就可以做很多事情啦。</p>
<h4>Buffer文件流</h4>
<p> </p>
<div id="crayon-5e40dd5157055595325887" class="crayon-syntax crayon-theme-monokai crayon-font-monaco crayon-os-mac print-yes notranslate crayon-wrapped" data-settings=" minimize scroll-mouseover disable-anim wrap" style="margin-top: 15px; margin-bottom: 15px; font-size: 14px !important; line-height: 20px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 20px !important; z-index: 0; opacity: 0; overflow: hidden;">const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV,
})
exports.main = async (event, context) => {
const fileID = 'cloud://xly-xrlur.786c-xly-xrlur-1300446086/cloudbase/1576500614167-520.png'
const res = await cloud.downloadFile({
fileID: fileID,
})
const buffer = res.fileContent
return buffer.toString('base64')
}</textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -28px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5e40dd5157055595325887-1" style="height: 20px;">1</div><div class="crayon-num" data-line="crayon-5e40dd5157055595325887-2" style="height: 20px;">2</div><div class="crayon-num" data-line="crayon-5e40dd5157055595325887-3" style="height: 20px;">3</div><div class="crayon-num" data-line="crayon-5e40dd5157055595325887-4" style="height: 20px;">4</div><div class="crayon-num" data-line="crayon-5e40dd5157055595325887-5" style="height: 20px;">5</div><div class="crayon-num" data-line="crayon-5e40dd5157055595325887-6" style="height: 20px;">6</div><div class="crayon-num" data-line="crayon-5e40dd5157055595325887-7" style="height: 20px;">7</div><div class="crayon-num" data-line="crayon-5e40dd5157055595325887-8" style="height: 20px;">8</div><div class="crayon-num" data-line="crayon-5e40dd5157055595325887-9" style="height: 20px;">9</div><div class="crayon-num" data-line="crayon-5e40dd5157055595325887-10" style="height: 20px;">10</div><div class="crayon-num" data-line="crayon-5e40dd5157055595325887-11" style="height: 20px;">11</div><div class="crayon-num" data-line="crayon-5e40dd5157055595325887-12" style="height: 20px;">12</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5e40dd5157055595325887-1"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">cloud</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'wx-server-sdk'</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157055595325887-2"><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-e">init</span><span class="crayon-sy">(</span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157055595325887-3"><span class="crayon-h"> </span><span class="crayon-v">env</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-v">DYNAMIC_CURRENT_ENV</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157055595325887-4"><span class="crayon-sy">}</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157055595325887-5"><span class="crayon-v">exports</span><span class="crayon-sy">.</span><span class="crayon-v">main</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">async</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-v">event</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">context</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-o">></span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157055595325887-6"><span class="crayon-h"> </span><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-e ">fileID</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-s">'cloud://xly-xrlur.786c-xly-xrlur-1300446086/cloudbase/1576500614167-520.png'</span></div><div class="crayon-line" id="crayon-5e40dd5157055595325887-7"><span class="crayon-h"> </span><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">res</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">await </span><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-e">downloadFile</span><span class="crayon-sy">(</span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157055595325887-8"><span class="crayon-h"> </span><span class="crayon-v">fileID</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">fileID</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157055595325887-9"><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157055595325887-10"><span class="crayon-h"> </span><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">buffer</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">res</span><span class="crayon-sy">.</span><span class="crayon-e">fileContent</span></div><div class="crayon-line" id="crayon-5e40dd5157055595325887-11"><span class="crayon-e"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-v">buffer</span><span class="crayon-sy">.</span><span class="crayon-e">toString</span><span class="crayon-sy">(</span><span class="crayon-s">'base64'</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157055595325887-12"><span class="crayon-sy">}</span></div></div></td>
</tr>
</tbody></table>
</div>
</div><p> </p>
<div id="crayon-5e40dd5157057013063134" class="crayon-syntax crayon-theme-monokai crayon-font-monaco crayon-os-mac print-yes notranslate crayon-wrapped" data-settings=" minimize scroll-mouseover disable-anim wrap" style="margin-top: 15px; margin-bottom: 15px; font-size: 14px !important; line-height: 20px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 20px !important; z-index: 0; opacity: 0; overflow: hidden;"> getServerImg(){
wx.cloud.callFunction({
name: 'downloadimg',
success: res => {
console.log("云函数返回的数据",res.result)
this.setData({
img:res.result
})
},
fail: err => {
console.error('云函数调用失败:', err)
}
})
}</textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -28px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5e40dd5157057013063134-1" style="height: 20px;">1</div><div class="crayon-num" data-line="crayon-5e40dd5157057013063134-2" style="height: 20px;">2</div><div class="crayon-num" data-line="crayon-5e40dd5157057013063134-3" style="height: 20px;">3</div><div class="crayon-num" data-line="crayon-5e40dd5157057013063134-4" style="height: 20px;">4</div><div class="crayon-num" data-line="crayon-5e40dd5157057013063134-5" style="height: 20px;">5</div><div class="crayon-num" data-line="crayon-5e40dd5157057013063134-6" style="height: 20px;">6</div><div class="crayon-num" data-line="crayon-5e40dd5157057013063134-7" style="height: 20px;">7</div><div class="crayon-num" data-line="crayon-5e40dd5157057013063134-8" style="height: 20px;">8</div><div class="crayon-num" data-line="crayon-5e40dd5157057013063134-9" style="height: 20px;">9</div><div class="crayon-num" data-line="crayon-5e40dd5157057013063134-10" style="height: 20px;">10</div><div class="crayon-num" data-line="crayon-5e40dd5157057013063134-11" style="height: 20px;">11</div><div class="crayon-num" data-line="crayon-5e40dd5157057013063134-12" style="height: 20px;">12</div><div class="crayon-num" data-line="crayon-5e40dd5157057013063134-13" style="height: 20px;">13</div><div class="crayon-num" data-line="crayon-5e40dd5157057013063134-14" style="height: 20px;">14</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5e40dd5157057013063134-1"><span class="crayon-h"> </span><span class="crayon-e">getServerImg</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157057013063134-2"><span class="crayon-h"> </span><span class="crayon-v">wx</span><span class="crayon-sy">.</span><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-e">callFunction</span><span class="crayon-sy">(</span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157057013063134-3"><span class="crayon-h"> </span><span class="crayon-v">name</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-s">'downloadimg'</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157057013063134-4"><span class="crayon-h"> </span><span class="crayon-v">success</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">res</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-o">></span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157057013063134-5"><span class="crayon-h"> </span><span class="crayon-v">console</span><span class="crayon-sy">.</span><span class="crayon-e">log</span><span class="crayon-sy">(</span><span class="crayon-s">"云函数返回的数据"</span><span class="crayon-sy">,</span><span class="crayon-v">res</span><span class="crayon-sy">.</span><span class="crayon-v">result</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157057013063134-6"><span class="crayon-h"> </span><span class="crayon-r">this</span><span class="crayon-sy">.</span><span class="crayon-e">setData</span><span class="crayon-sy">(</span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157057013063134-7"><span class="crayon-h"> </span><span class="crayon-v">img</span><span class="crayon-o">:</span><span class="crayon-v">res</span><span class="crayon-sy">.</span><span class="crayon-i">result</span></div><div class="crayon-line" id="crayon-5e40dd5157057013063134-8"><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157057013063134-9"><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157057013063134-10"><span class="crayon-h"> </span><span class="crayon-v">fail</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">err</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-o">></span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157057013063134-11"><span class="crayon-h"> </span><span class="crayon-v">console</span><span class="crayon-sy">.</span><span class="crayon-e">error</span><span class="crayon-sy">(</span><span class="crayon-s">'云函数调用失败:'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">err</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157057013063134-12"><span class="crayon-h"> </span><span class="crayon-sy">}</span></div><div class="crayon-line" id="crayon-5e40dd5157057013063134-13"><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157057013063134-14"><span class="crayon-h"> </span><span class="crayon-sy">}</span></div></div></td>
</tr>
</tbody></table>
</div>
</div><p></p>
<div id="crayon-5e40dd5157059794627813" class="crayon-syntax crayon-theme-monokai crayon-font-monaco crayon-os-mac print-yes notranslate crayon-wrapped" data-settings=" minimize scroll-mouseover disable-anim wrap" style="margin-top: 15px; margin-bottom: 15px; font-size: 14px !important; line-height: 20px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 20px !important; z-index: 0; opacity: 0; overflow: hidden;"><image width="400px" height="200px" src="data:image/jpeg;base64,{{img}}"></image></textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -20px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5e40dd5157059794627813-1" style="height: 20px;">1</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5e40dd5157059794627813-1"><span class="crayon-r "><image </span><span class="crayon-e ">width</span><span class="crayon-o">=</span><span class="crayon-s ">"400px"</span><span class="crayon-h"> </span><span class="crayon-e ">height</span><span class="crayon-o">=</span><span class="crayon-s ">"200px"</span><span class="crayon-h"> </span><span class="crayon-e ">src</span><span class="crayon-o">=</span><span class="crayon-s ">"data:image/jpeg;base64,{{img}}"</span><span class="crayon-r ">></span><span class="crayon-r "></image></span></div></div></td>
</tr>
</tbody></table>
</div>
</div><p>Buffer String</p>
<p>Buffer JSON</p>
<h4>图像处理sharp</h4>
<p>sharp是一个高速图像处理库,可以很方便的实现图片编辑操作,如裁剪、格式转换、旋转变换、滤镜添加、图片合成(如添加水印)、图片拼接等,支持JPEG, PNG, WebP, TIFF, GIF 和 SVG格式。在云函数端使用sharp来处理图片,而云存储则可以作为服务端和小程序端来传递图片的桥梁。</p>
<p><strong>技术文档:</strong><a href="https://sharp.pixelplumbing.com/en/stable/" target="_blank" rel="noopener noreferrer">sharp官方技术文档</a></p>
<p>使用开发者工具新建一个</p>
<div id="crayon-5e40dd515705b620045290" class="crayon-syntax crayon-theme-monokai crayon-font-monaco crayon-os-mac print-yes notranslate crayon-wrapped" data-settings=" minimize scroll-mouseover disable-anim wrap" style="margin-top: 15px; margin-bottom: 15px; font-size: 14px !important; line-height: 20px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 20px !important; z-index: 0; opacity: 0; overflow: hidden;">const cloud = require('wx-server-sdk')
const fs = require('fs')
const path = require('path')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV,
})
const sharp = require('sharp');
exports.main = async (event, context) => {
//这里换成自己的fileID,也可以在小程序端上传文件之后,把fileID传进来event.fileID
const fileID = 'cloud://xly-xrlur.786c-xly-xrlur-1300446086/1572315793628-366.png'
//要用云函数处理图片,需要先下载图片,返回的图片类型为Buffer
const res = await cloud.downloadFile({
fileID: fileID,
})
const buffer = res.fileContent
//sharp对图片进行处理之后,保存为output.png,也可以直接保存为Buffer
await sharp(buffer).rotate().resize(200).toFile('output.png')
// 云函数读取模块目录下的图片,并上传到云存储
const fileStream = await fs.createReadStream(path.join(__dirname, 'output.png'))
return await cloud.uploadFile({
cloudPath: 'sharpdemo.jpg',
fileContent: fileStream,
})
}</textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -28px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5e40dd515705b620045290-1" style="height: 20px;">1</div><div class="crayon-num" data-line="crayon-5e40dd515705b620045290-2" style="height: 20px;">2</div><div class="crayon-num" data-line="crayon-5e40dd515705b620045290-3" style="height: 20px;">3</div><div class="crayon-num" data-line="crayon-5e40dd515705b620045290-4" style="height: 20px;">4</div><div class="crayon-num" data-line="crayon-5e40dd515705b620045290-5" style="height: 20px;">5</div><div class="crayon-num" data-line="crayon-5e40dd515705b620045290-6" style="height: 20px;">6</div><div class="crayon-num" data-line="crayon-5e40dd515705b620045290-7" style="height: 20px;">7</div><div class="crayon-num" data-line="crayon-5e40dd515705b620045290-8" style="height: 20px;">8</div><div class="crayon-num" data-line="crayon-5e40dd515705b620045290-9" style="height: 20px;">9</div><div class="crayon-num" data-line="crayon-5e40dd515705b620045290-10" style="height: 20px;">10</div><div class="crayon-num" data-line="crayon-5e40dd515705b620045290-11" style="height: 20px;">11</div><div class="crayon-num" data-line="crayon-5e40dd515705b620045290-12" style="height: 20px;">12</div><div class="crayon-num" data-line="crayon-5e40dd515705b620045290-13" style="height: 20px;">13</div><div class="crayon-num" data-line="crayon-5e40dd515705b620045290-14" style="height: 20px;">14</div><div class="crayon-num" data-line="crayon-5e40dd515705b620045290-15" style="height: 20px;">15</div><div class="crayon-num" data-line="crayon-5e40dd515705b620045290-16" style="height: 20px;">16</div><div class="crayon-num" data-line="crayon-5e40dd515705b620045290-17" style="height: 20px;">17</div><div class="crayon-num" data-line="crayon-5e40dd515705b620045290-18" style="height: 20px;">18</div><div class="crayon-num" data-line="crayon-5e40dd515705b620045290-19" style="height: 20px;">19</div><div class="crayon-num" data-line="crayon-5e40dd515705b620045290-20" style="height: 20px;">20</div><div class="crayon-num" data-line="crayon-5e40dd515705b620045290-21" style="height: 20px;">21</div><div class="crayon-num" data-line="crayon-5e40dd515705b620045290-22" style="height: 20px;">22</div><div class="crayon-num" data-line="crayon-5e40dd515705b620045290-23" style="height: 20px;">23</div><div class="crayon-num" data-line="crayon-5e40dd515705b620045290-24" style="height: 20px;">24</div><div class="crayon-num" data-line="crayon-5e40dd515705b620045290-25" style="height: 20px;">25</div><div class="crayon-num" data-line="crayon-5e40dd515705b620045290-26" style="height: 20px;">26</div><div class="crayon-num" data-line="crayon-5e40dd515705b620045290-27" style="height: 20px;">27</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5e40dd515705b620045290-1"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">cloud</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'wx-server-sdk'</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd515705b620045290-2"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">fs</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'fs'</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd515705b620045290-3"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">path</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'path'</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd515705b620045290-4"><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-e">init</span><span class="crayon-sy">(</span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd515705b620045290-5"><span class="crayon-h"> </span><span class="crayon-v">env</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-v">DYNAMIC_CURRENT_ENV</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd515705b620045290-6"><span class="crayon-sy">}</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd515705b620045290-7"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">sharp</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'sharp'</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd515705b620045290-8"><span class="crayon-v">exports</span><span class="crayon-sy">.</span><span class="crayon-v">main</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">async</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-v">event</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">context</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-o">></span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd515705b620045290-9"><span class="crayon-h"> </span><span class="crayon-c">//这里换成自己的fileID,也可以在小程序端上传文件之后,把fileID传进来event.fileID</span></div><div class="crayon-line" id="crayon-5e40dd515705b620045290-10"><span class="crayon-h"> </span><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-e ">fileID</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-s">'cloud://xly-xrlur.786c-xly-xrlur-1300446086/1572315793628-366.png'</span></div><div class="crayon-line" id="crayon-5e40dd515705b620045290-11"> </div><div class="crayon-line" id="crayon-5e40dd515705b620045290-12"><span class="crayon-h"> </span><span class="crayon-c">//要用云函数处理图片,需要先下载图片,返回的图片类型为Buffer</span></div><div class="crayon-line" id="crayon-5e40dd515705b620045290-13"><span class="crayon-h"> </span><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">res</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">await </span><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-e">downloadFile</span><span class="crayon-sy">(</span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd515705b620045290-14"><span class="crayon-h"> </span><span class="crayon-v">fileID</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">fileID</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd515705b620045290-15"><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd515705b620045290-16"><span class="crayon-h"> </span><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">buffer</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">res</span><span class="crayon-sy">.</span><span class="crayon-v">fileContent</span><span class="crayon-h"> </span></div><div class="crayon-line" id="crayon-5e40dd515705b620045290-17"> </div><div class="crayon-line" id="crayon-5e40dd515705b620045290-18"><span class="crayon-h"> </span><span class="crayon-c">//sharp对图片进行处理之后,保存为output.png,也可以直接保存为Buffer</span></div><div class="crayon-line" id="crayon-5e40dd515705b620045290-19"><span class="crayon-h"> </span><span class="crayon-e">await </span><span class="crayon-e">sharp</span><span class="crayon-sy">(</span><span class="crayon-v">buffer</span><span class="crayon-sy">)</span><span class="crayon-sy">.</span><span class="crayon-e">rotate</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">.</span><span class="crayon-e">resize</span><span class="crayon-sy">(</span><span class="crayon-cn">200</span><span class="crayon-sy">)</span><span class="crayon-sy">.</span><span class="crayon-e">toFile</span><span class="crayon-sy">(</span><span class="crayon-s">'output.png'</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd515705b620045290-20"> </div><div class="crayon-line" id="crayon-5e40dd515705b620045290-21"><span class="crayon-h"> </span><span class="crayon-c">// 云函数读取模块目录下的图片,并上传到云存储</span></div><div class="crayon-line" id="crayon-5e40dd515705b620045290-22"><span class="crayon-h"> </span><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">fileStream</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">await </span><span class="crayon-v">fs</span><span class="crayon-sy">.</span><span class="crayon-e">createReadStream</span><span class="crayon-sy">(</span><span class="crayon-v">path</span><span class="crayon-sy">.</span><span class="crayon-e">join</span><span class="crayon-sy">(</span><span class="crayon-v">__dirname</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-s">'output.png'</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd515705b620045290-23"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-e">await </span><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-e">uploadFile</span><span class="crayon-sy">(</span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd515705b620045290-24"><span class="crayon-h"> </span><span class="crayon-v">cloudPath</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-s">'sharpdemo.jpg'</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd515705b620045290-25"><span class="crayon-h"> </span><span class="crayon-v">fileContent</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">fileStream</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd515705b620045290-26"><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-sy">)</span><span class="crayon-h"> </span></div><div class="crayon-line" id="crayon-5e40dd515705b620045290-27"><span class="crayon-sy">}</span></div></div></td>
</tr>
</tbody></table>
</div>
</div><p>也可以让sharp不需要先toFile转成图片,而是直接转成Buffer,这样就可以直接作为参数传给fileContent上传到云存储,如:</p>
<div id="crayon-5e40dd515705c474203870" class="crayon-syntax crayon-theme-monokai crayon-font-monaco crayon-os-mac print-yes notranslate crayon-wrapped" data-settings=" minimize scroll-mouseover disable-anim wrap" style="margin-top: 15px; margin-bottom: 15px; font-size: 14px !important; line-height: 20px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 20px !important; z-index: 0; opacity: 0; overflow: hidden;"> const buffer2 = await sharp(buffer).rotate().resize(200).toBuffer();
return await cloud.uploadFile({
cloudPath: 'sharpdemo2.jpg',
fileContent: buffer2,
})</textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -20px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5e40dd515705c474203870-1" style="height: 20px;">1</div><div class="crayon-num" data-line="crayon-5e40dd515705c474203870-2" style="height: 20px;">2</div><div class="crayon-num" data-line="crayon-5e40dd515705c474203870-3" style="height: 20px;">3</div><div class="crayon-num" data-line="crayon-5e40dd515705c474203870-4" style="height: 20px;">4</div><div class="crayon-num" data-line="crayon-5e40dd515705c474203870-5" style="height: 20px;">5</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5e40dd515705c474203870-1"><span class="crayon-h"> </span><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">buffer2</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">await </span><span class="crayon-e">sharp</span><span class="crayon-sy">(</span><span class="crayon-v">buffer</span><span class="crayon-sy">)</span><span class="crayon-sy">.</span><span class="crayon-e">rotate</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">.</span><span class="crayon-e">resize</span><span class="crayon-sy">(</span><span class="crayon-cn">200</span><span class="crayon-sy">)</span><span class="crayon-sy">.</span><span class="crayon-e">toBuffer</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd515705c474203870-2"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-e">await </span><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-e">uploadFile</span><span class="crayon-sy">(</span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd515705c474203870-3"><span class="crayon-h"> </span><span class="crayon-v">cloudPath</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-s">'sharpdemo2.jpg'</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd515705c474203870-4"><span class="crayon-h"> </span><span class="crayon-v">fileContent</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">buffer2</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd515705c474203870-5"><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-sy">)</span></div></div></td>
</tr>
</tbody></table>
</div>
</div><p></p>
<h4>连接数据库MySQL</h4>
<p><strong>公网连接数据库MySQL</strong></p>
<p>技术文档:<a href="https://sequelize.org/master/" target="_blank" rel="noopener noreferrer">Sequelize</a></p>
<div id="crayon-5e40dd515705e545541297" class="crayon-syntax crayon-theme-monokai crayon-font-monaco crayon-os-mac print-yes notranslate crayon-wrapped" data-settings=" minimize scroll-mouseover disable-anim wrap" style="margin-top: 15px; margin-bottom: 15px; font-size: 14px !important; line-height: 20px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 20px !important; z-index: 0; opacity: 0; overflow: hidden;">const sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost', //数据库地址,默认本机
port:'3306',
dialect: 'mysql',
pool: { //连接池设置
max: 5, //最大连接数
min: 0, //最小连接数
idle: 10000
},
});</textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -28px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5e40dd515705e545541297-1" style="height: 20px;">1</div><div class="crayon-num" data-line="crayon-5e40dd515705e545541297-2" style="height: 20px;">2</div><div class="crayon-num" data-line="crayon-5e40dd515705e545541297-3" style="height: 20px;">3</div><div class="crayon-num" data-line="crayon-5e40dd515705e545541297-4" style="height: 20px;">4</div><div class="crayon-num" data-line="crayon-5e40dd515705e545541297-5" style="height: 20px;">5</div><div class="crayon-num" data-line="crayon-5e40dd515705e545541297-6" style="height: 20px;">6</div><div class="crayon-num" data-line="crayon-5e40dd515705e545541297-7" style="height: 20px;">7</div><div class="crayon-num" data-line="crayon-5e40dd515705e545541297-8" style="height: 20px;">8</div><div class="crayon-num" data-line="crayon-5e40dd515705e545541297-9" style="height: 20px;">9</div><div class="crayon-num" data-line="crayon-5e40dd515705e545541297-10" style="height: 20px;">10</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5e40dd515705e545541297-1"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">sequelize</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-r">new</span><span class="crayon-h"> </span><span class="crayon-e">Sequelize</span><span class="crayon-sy">(</span><span class="crayon-s">'database'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-s">'username'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-s">'password'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd515705e545541297-2"><span class="crayon-h"> </span><span class="crayon-v">host</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-s">'localhost'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-c">//数据库地址,默认本机</span></div><div class="crayon-line" id="crayon-5e40dd515705e545541297-3"><span class="crayon-h"> </span><span class="crayon-v">port</span><span class="crayon-o">:</span><span class="crayon-s">'3306'</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd515705e545541297-4"><span class="crayon-h"> </span><span class="crayon-v">dialect</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-s">'mysql'</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd515705e545541297-5"><span class="crayon-h"> </span><span class="crayon-v">pool</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-sy">{</span><span class="crayon-h"> </span><span class="crayon-c">//连接池设置</span></div><div class="crayon-line" id="crayon-5e40dd515705e545541297-6"><span class="crayon-h"> </span><span class="crayon-v">max</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-cn">5</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-c">//最大连接数</span></div><div class="crayon-line" id="crayon-5e40dd515705e545541297-7"><span class="crayon-h"> </span><span class="crayon-v">min</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-cn">0</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-c">//最小连接数</span></div><div class="crayon-line" id="crayon-5e40dd515705e545541297-8"><span class="crayon-h"> </span><span class="crayon-v">idle</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-cn">10000</span></div><div class="crayon-line" id="crayon-5e40dd515705e545541297-9"><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd515705e545541297-10"><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div></div></td>
</tr>
</tbody></table>
</div>
</div><p></p>
<blockquote><p>无论是MySQL,还是PostgreSQL、Redis、MongoDB等其他数据库,只要我们在</p></blockquote>
<p><strong>私有网络连接MySQL</strong></p>
<p>默认情况下,云开发的函数部署在<strong>公共网络</strong>中,只可以访问公网。如果开发者需要访问腾讯云的 <strong>Redis、TencentDB、CVM、Kafka</strong> 等资源,需要建立<strong>私有网络</strong>来确保<strong>数据安全及连接安全</strong>。</p>
<p><span style="font-size: 20px; font-weight: bold; color: #2b2b2b;">连接数据库Redis</span></p>
<div id="crayon-5e40dd5157060418031812" class="crayon-syntax crayon-theme-monokai crayon-font-monaco crayon-os-mac print-yes notranslate crayon-wrapped" data-settings=" minimize scroll-mouseover disable-anim wrap" style="margin-top: 15px; margin-bottom: 15px; font-size: 14px !important; line-height: 20px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 20px !important; z-index: 0; opacity: 0; overflow: hidden;">const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV,
})
const Redis = require('ioredis')
const redis = new Redis({
port: 6379,
host: '10.168.0.15',
family: 4,
password: 'CloudBase2018',
db: 0,
})
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
const cacheKey = wxContext.OPENID
const cache = await redis.get(cacheKey)
if (!cache) {
const result = await new Promise((resolve, reject) => {
setTimeout(() => resolve(Math.random()), 2000)
})
redis.set(cacheKey, result, 'EX', 3600)
return result
} else {
return cache
}
}</textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -28px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5e40dd5157060418031812-1" style="height: 20px;">1</div><div class="crayon-num" data-line="crayon-5e40dd5157060418031812-2" style="height: 20px;">2</div><div class="crayon-num" data-line="crayon-5e40dd5157060418031812-3" style="height: 20px;">3</div><div class="crayon-num" data-line="crayon-5e40dd5157060418031812-4" style="height: 20px;">4</div><div class="crayon-num" data-line="crayon-5e40dd5157060418031812-5" style="height: 20px;">5</div><div class="crayon-num" data-line="crayon-5e40dd5157060418031812-6" style="height: 20px;">6</div><div class="crayon-num" data-line="crayon-5e40dd5157060418031812-7" style="height: 20px;">7</div><div class="crayon-num" data-line="crayon-5e40dd5157060418031812-8" style="height: 20px;">8</div><div class="crayon-num" data-line="crayon-5e40dd5157060418031812-9" style="height: 20px;">9</div><div class="crayon-num" data-line="crayon-5e40dd5157060418031812-10" style="height: 20px;">10</div><div class="crayon-num" data-line="crayon-5e40dd5157060418031812-11" style="height: 20px;">11</div><div class="crayon-num" data-line="crayon-5e40dd5157060418031812-12" style="height: 20px;">12</div><div class="crayon-num" data-line="crayon-5e40dd5157060418031812-13" style="height: 20px;">13</div><div class="crayon-num" data-line="crayon-5e40dd5157060418031812-14" style="height: 20px;">14</div><div class="crayon-num" data-line="crayon-5e40dd5157060418031812-15" style="height: 20px;">15</div><div class="crayon-num" data-line="crayon-5e40dd5157060418031812-16" style="height: 20px;">16</div><div class="crayon-num" data-line="crayon-5e40dd5157060418031812-17" style="height: 20px;">17</div><div class="crayon-num" data-line="crayon-5e40dd5157060418031812-18" style="height: 20px;">18</div><div class="crayon-num" data-line="crayon-5e40dd5157060418031812-19" style="height: 20px;">19</div><div class="crayon-num" data-line="crayon-5e40dd5157060418031812-20" style="height: 20px;">20</div><div class="crayon-num" data-line="crayon-5e40dd5157060418031812-21" style="height: 20px;">21</div><div class="crayon-num" data-line="crayon-5e40dd5157060418031812-22" style="height: 20px;">22</div><div class="crayon-num" data-line="crayon-5e40dd5157060418031812-23" style="height: 20px;">23</div><div class="crayon-num" data-line="crayon-5e40dd5157060418031812-24" style="height: 20px;">24</div><div class="crayon-num" data-line="crayon-5e40dd5157060418031812-25" style="height: 20px;">25</div><div class="crayon-num" data-line="crayon-5e40dd5157060418031812-26" style="height: 20px;">26</div><div class="crayon-num" data-line="crayon-5e40dd5157060418031812-27" style="height: 20px;">27</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5e40dd5157060418031812-1"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">cloud</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'wx-server-sdk'</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157060418031812-2"><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-e">init</span><span class="crayon-sy">(</span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157060418031812-3"><span class="crayon-h"> </span><span class="crayon-v">env</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-v">DYNAMIC_CURRENT_ENV</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157060418031812-4"><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157060418031812-5"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">Redis</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'ioredis'</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157060418031812-6"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">redis</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-r">new</span><span class="crayon-h"> </span><span class="crayon-e">Redis</span><span class="crayon-sy">(</span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157060418031812-7"><span class="crayon-h"> </span><span class="crayon-v">port</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-cn">6379</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157060418031812-8"><span class="crayon-h"> </span><span class="crayon-v">host</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-s">'10.168.0.15'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span></div><div class="crayon-line" id="crayon-5e40dd5157060418031812-9"><span class="crayon-h"> </span><span class="crayon-v">family</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-cn">4</span><span class="crayon-sy">,</span><span class="crayon-h"> </span></div><div class="crayon-line" id="crayon-5e40dd5157060418031812-10"><span class="crayon-h"> </span><span class="crayon-v">password</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-s">'CloudBase2018'</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157060418031812-11"><span class="crayon-h"> </span><span class="crayon-v">db</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-cn">0</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157060418031812-12"><span class="crayon-sy">}</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157060418031812-13"> </div><div class="crayon-line" id="crayon-5e40dd5157060418031812-14"><span class="crayon-v">exports</span><span class="crayon-sy">.</span><span class="crayon-v">main</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">async</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-v">event</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">context</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-o">></span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157060418031812-15"><span class="crayon-h"> </span><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">wxContext</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-e">getWXContext</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157060418031812-16"><span class="crayon-h"> </span><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">cacheKey</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">wxContext</span><span class="crayon-sy">.</span><span class="crayon-e">OPENID</span></div><div class="crayon-line" id="crayon-5e40dd5157060418031812-17"><span class="crayon-e"> </span><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">cache</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">await </span><span class="crayon-v">redis</span><span class="crayon-sy">.</span><span class="crayon-e">get</span><span class="crayon-sy">(</span><span class="crayon-v">cacheKey</span><span class="crayon-sy">)</span><span class="crayon-h"> </span></div><div class="crayon-line" id="crayon-5e40dd5157060418031812-18"><span class="crayon-h"> </span><span class="crayon-st">if</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-o">!</span><span class="crayon-v">cache</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157060418031812-19"><span class="crayon-h"> </span><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">result</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">await </span><span class="crayon-r">new</span><span class="crayon-h"> </span><span class="crayon-e">Promise</span><span class="crayon-sy">(</span><span class="crayon-sy">(</span><span class="crayon-v">resolve</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">reject</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-o">></span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157060418031812-20"><span class="crayon-h"> </span><span class="crayon-r">setTimeout</span><span class="crayon-sy">(</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-o">></span><span class="crayon-h"> </span><span class="crayon-e">resolve</span><span class="crayon-sy">(</span><span class="crayon-v">Math</span><span class="crayon-sy">.</span><span class="crayon-e">random</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">2000</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157060418031812-21"><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157060418031812-22"><span class="crayon-h"> </span><span class="crayon-v">redis</span><span class="crayon-sy">.</span><span class="crayon-e">set</span><span class="crayon-sy">(</span><span class="crayon-v">cacheKey</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">result</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-s">'EX'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">3600</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157060418031812-23"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-i">result</span></div><div class="crayon-line" id="crayon-5e40dd5157060418031812-24"><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-h"> </span><span class="crayon-st">else</span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157060418031812-25"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-i">cache</span></div><div class="crayon-line" id="crayon-5e40dd5157060418031812-26"><span class="crayon-h"> </span><span class="crayon-sy">}</span></div><div class="crayon-line" id="crayon-5e40dd5157060418031812-27"><span class="crayon-h"> </span><span class="crayon-sy">}</span></div></div></td>
</tr>
</tbody></table>
</div>
</div><p></p>
<h4>二维码qrcode</h4>
<p><strong>技术文档:</strong><a href="https://github.com/soldair/node-qrcode" target="_blank" rel="noopener noreferrer">node-qrcode Github地址</a></p>
<h4>邮件处理</h4>
<p>技术文档:<a href="https://github.com/nodemailer/nodemailer" target="_blank" rel="noopener noreferrer">Nodemailer Github地址</a>、<a href="https://nodemailer.com/about/" target="_blank" rel="noopener noreferrer">Nodemailer官方文档</a></p>
<p>使用开发者工具创建一个云函数,比如nodemail,然后在package.json增加nodemailer最新版latest的依赖:</p>
<div id="crayon-5e40dd5157062202253930" class="crayon-syntax crayon-theme-monokai crayon-font-monaco crayon-os-mac print-yes notranslate crayon-wrapped" data-settings=" minimize scroll-mouseover disable-anim wrap" style="margin-top: 15px; margin-bottom: 15px; font-size: 14px !important; line-height: 20px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 20px !important; z-index: 0; opacity: 0; overflow: hidden;"> "dependencies": {
"nodemailer": "latest"
}</textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -20px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5e40dd5157062202253930-1" style="height: 20px;">1</div><div class="crayon-num" data-line="crayon-5e40dd5157062202253930-2" style="height: 20px;">2</div><div class="crayon-num" data-line="crayon-5e40dd5157062202253930-3" style="height: 20px;">3</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5e40dd5157062202253930-1"><span class="crayon-h"> </span><span class="crayon-s">"dependencies"</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157062202253930-2"><span class="crayon-h"> </span><span class="crayon-s">"nodemailer"</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-s">"latest"</span></div><div class="crayon-line" id="crayon-5e40dd5157062202253930-3"><span class="crayon-h"> </span><span class="crayon-sy">}</span></div></div></td>
</tr>
</tbody></table>
</div>
</div><p>发送邮件服务器:smtp.qq.com,使用SSL,端口号465或587</p>
<div id="crayon-5e40dd5157063903937231" class="crayon-syntax crayon-theme-monokai crayon-font-monaco crayon-os-mac print-yes notranslate crayon-wrapped" data-settings=" minimize scroll-mouseover disable-anim wrap" style="margin-top: 15px; margin-bottom: 15px; font-size: 14px !important; line-height: 20px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 20px !important; z-index: 0; opacity: 0; overflow: hidden;">const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV,
})
exports.main = async (event, context) => {
const nodemailer = require("nodemailer");
let transporter = nodemailer.createTransport({
host: "smtp.qq.com", //SMTP服务器地址
port: 465, //端口号,通常为465,587,25,不同的邮件客户端端口号可能不一样
secure: true, //如果端口是465,就为true;如果是587、25,就填false
auth: {
user: "344169902@qq.com", //你的邮箱账号
pass: "你的QQ邮箱授权码" //邮箱密码,QQ的需要是独立授权码
}
});
let message = {
from: '来自李东bbsky <344169902@qq.com>', //你的发件邮箱
to: '你要发送给谁', //你要发给谁
// cc:'', 支持cc 抄送
// bcc: '', 支持bcc 密送
subject: '欢迎大家参与云开发技术训练营活动',
//支持text纯文字,html代码
text: '欢迎大家',
html:
'<p><b>你好:</b><img src="https://hackwork-1251009918.cos.ap-shanghai.myqcloud.com/handbook/html5/weapp.jpg"/></p>' +
'<p>欢迎欢迎<br/></p>',
attachments: [ //支持多种附件形式,可以是String, Buffer或Stream
{
filename: 'image.png',
content: Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD/' +
'//+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4U' +
'g9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC',
'base64'
),
},
]
};
let res = await transporter.sendMail(message);
return res;
}</textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -28px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-1" style="height: 20px;">1</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-2" style="height: 20px;">2</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-3" style="height: 20px;">3</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-4" style="height: 20px;">4</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-5" style="height: 20px;">5</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-6" style="height: 20px;">6</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-7" style="height: 20px;">7</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-8" style="height: 20px;">8</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-9" style="height: 20px;">9</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-10" style="height: 20px;">10</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-11" style="height: 20px;">11</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-12" style="height: 20px;">12</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-13" style="height: 20px;">13</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-14" style="height: 20px;">14</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-15" style="height: 20px;">15</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-16" style="height: 20px;">16</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-17" style="height: 20px;">17</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-18" style="height: 20px;">18</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-19" style="height: 20px;">19</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-20" style="height: 20px;">20</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-21" style="height: 20px;">21</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-22" style="height: 20px;">22</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-23" style="height: 20px;">23</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-24" style="height: 20px;">24</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-25" style="height: 20px;">25</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-26" style="height: 20px;">26</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-27" style="height: 40px;">27</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-28" style="height: 20px;">28</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-29" style="height: 20px;">29</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-30" style="height: 20px;">30</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-31" style="height: 20px;">31</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-32" style="height: 20px;">32</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-33" style="height: 20px;">33</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-34" style="height: 20px;">34</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-35" style="height: 20px;">35</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-36" style="height: 20px;">36</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-37" style="height: 20px;">37</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-38" style="height: 20px;">38</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-39" style="height: 20px;">39</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-40" style="height: 20px;">40</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-41" style="height: 20px;">41</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-42" style="height: 20px;">42</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-43" style="height: 20px;">43</div><div class="crayon-num" data-line="crayon-5e40dd5157063903937231-44" style="height: 20px;">44</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5e40dd5157063903937231-1"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">cloud</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'wx-server-sdk'</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-2"><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-e">init</span><span class="crayon-sy">(</span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-3"><span class="crayon-h"> </span><span class="crayon-v">env</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-v">DYNAMIC_CURRENT_ENV</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-4"><span class="crayon-sy">}</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-5"><span class="crayon-v">exports</span><span class="crayon-sy">.</span><span class="crayon-v">main</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">async</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-v">event</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">context</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-o">></span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-6"><span class="crayon-h"> </span><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">nodemailer</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">"nodemailer"</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-7"><span class="crayon-h"> </span><span class="crayon-e">let </span><span class="crayon-v">transporter</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">nodemailer</span><span class="crayon-sy">.</span><span class="crayon-e">createTransport</span><span class="crayon-sy">(</span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-8"><span class="crayon-h"> </span><span class="crayon-v">host</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-s">"smtp.qq.com"</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-c">//SMTP服务器地址</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-9"><span class="crayon-h"> </span><span class="crayon-v">port</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-cn">465</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-c">//端口号,通常为465,587,25,不同的邮件客户端端口号可能不一样</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-10"><span class="crayon-h"> </span><span class="crayon-v">secure</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-t">true</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-c">//如果端口是465,就为true;如果是587、25,就填false</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-11"><span class="crayon-h"> </span><span class="crayon-v">auth</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-12"><span class="crayon-h"> </span><span class="crayon-v">user</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-s">"344169902@qq.com"</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-c">//你的邮箱账号</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-13"><span class="crayon-h"> </span><span class="crayon-v">pass</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-s">"你的QQ邮箱授权码"</span><span class="crayon-h"> </span><span class="crayon-c">//邮箱密码,QQ的需要是独立授权码</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-14"><span class="crayon-h"> </span><span class="crayon-sy">}</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-15"><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-16"> </div><div class="crayon-line" id="crayon-5e40dd5157063903937231-17"><span class="crayon-h"> </span><span class="crayon-e">let </span><span class="crayon-v">message</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-18"><span class="crayon-h"> </span><span class="crayon-v">from</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-s">'来自李东bbsky <344169902@qq.com>'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-c">//你的发件邮箱</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-19"><span class="crayon-h"> </span><span class="crayon-st">to</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-s">'你要发送给谁'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-c">//你要发给谁</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-20"><span class="crayon-h"> </span><span class="crayon-c">// cc:'', 支持cc 抄送</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-21"><span class="crayon-h"> </span><span class="crayon-c">// bcc: '', 支持bcc 密送</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-22"><span class="crayon-h"> </span><span class="crayon-v">subject</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-s">'欢迎大家参与云开发技术训练营活动'</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-23"> </div><div class="crayon-line" id="crayon-5e40dd5157063903937231-24"><span class="crayon-h"> </span><span class="crayon-c">//支持text纯文字,html代码</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-25"><span class="crayon-h"> </span><span class="crayon-v">text</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-s">'欢迎大家'</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-26"><span class="crayon-h"> </span><span class="crayon-v">html</span><span class="crayon-o">:</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-27"><span class="crayon-h"> </span><span class="crayon-s">'<p><b>你好:</b><img src="https://hackwork-1251009918.cos.ap-shanghai.myqcloud.com/handbook/html5/weapp.jpg"/></p>'</span><span class="crayon-h"> </span><span class="crayon-o">+</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-28"><span class="crayon-h"> </span><span class="crayon-s">'<p>欢迎欢迎<br/></p>'</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-29"><span class="crayon-h"> </span><span class="crayon-v">attachments</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-sy">[</span><span class="crayon-h"> </span><span class="crayon-c">//支持多种附件形式,可以是String, Buffer或Stream</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-30"><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-31"><span class="crayon-h"> </span><span class="crayon-v">filename</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-s">'image.png'</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-32"><span class="crayon-h"> </span><span class="crayon-v">content</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">Buffer</span><span class="crayon-sy">.</span><span class="crayon-e">from</span><span class="crayon-sy">(</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-33"><span class="crayon-h"> </span><span class="crayon-s">'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD/'</span><span class="crayon-h"> </span><span class="crayon-o">+</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-34"><span class="crayon-h"> </span><span class="crayon-s">'//+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4U'</span><span class="crayon-h"> </span><span class="crayon-o">+</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-35"><span class="crayon-h"> </span><span class="crayon-s">'g9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC'</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-36"><span class="crayon-h"> </span><span class="crayon-s">'base64'</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-37"><span class="crayon-h"> </span><span class="crayon-sy">)</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-38"><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-39"><span class="crayon-h"> </span><span class="crayon-sy">]</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-40"><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-41"> </div><div class="crayon-line" id="crayon-5e40dd5157063903937231-42"><span class="crayon-h"> </span><span class="crayon-e">let </span><span class="crayon-v">res</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">await </span><span class="crayon-v">transporter</span><span class="crayon-sy">.</span><span class="crayon-e">sendMail</span><span class="crayon-sy">(</span><span class="crayon-v">message</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-43"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-v">res</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd5157063903937231-44"><span class="crayon-sy">}</span></div></div></td>
</tr>
</tbody></table>
</div>
</div><p></p>
<h4>Excel文档处理</h4>
<p>Excel是存储数据比较常见的格式,那如何让云函数拥有读写Excel文件的能力呢?我们可以在Github上搜索关键词“Node Excel”,去筛选Star比较多,条件比较契合的。</p>
<p>Github地址:<a href="https://github.com/mgcrea/node-xlsx" target="_blank" rel="noopener noreferrer">node-xlsx</a></p>
<p>使用开发者工具新建一个云函数,在package.json里添加latest最新版的node-xlsx:</p>
<div id="crayon-5e40dd5157065641525490" class="crayon-syntax crayon-theme-monokai crayon-font-monaco crayon-os-mac print-yes notranslate crayon-wrapped" data-settings=" minimize scroll-mouseover disable-anim wrap" style="margin-top: 15px; margin-bottom: 15px; font-size: 14px !important; line-height: 20px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 20px !important; z-index: 0; opacity: 0; overflow: hidden;">"dependencies": {
"wx-server-sdk": "latest",
"node-xlsx": "latest"
}</textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -20px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5e40dd5157065641525490-1" style="height: 20px;">1</div><div class="crayon-num" data-line="crayon-5e40dd5157065641525490-2" style="height: 20px;">2</div><div class="crayon-num" data-line="crayon-5e40dd5157065641525490-3" style="height: 20px;">3</div><div class="crayon-num" data-line="crayon-5e40dd5157065641525490-4" style="height: 20px;">4</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5e40dd5157065641525490-1"><span class="crayon-s">"dependencies"</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157065641525490-2"><span class="crayon-h"> </span><span class="crayon-s">"wx-server-sdk"</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-s">"latest"</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157065641525490-3"><span class="crayon-h"> </span><span class="crayon-s">"node-xlsx"</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-s">"latest"</span></div><div class="crayon-line" id="crayon-5e40dd5157065641525490-4"><span class="crayon-sy">}</span></div></div></td>
</tr>
</tbody></table>
</div>
</div><p><strong>读取云存储的Excel文件</strong></p>
<div id="crayon-5e40dd5157067523822703" class="crayon-syntax crayon-theme-monokai crayon-font-monaco crayon-os-mac print-yes notranslate crayon-wrapped" data-settings=" minimize scroll-mouseover disable-anim wrap" style="margin-top: 15px; margin-bottom: 15px; font-size: 14px !important; line-height: 20px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 20px !important; z-index: 0; opacity: 0; overflow: hidden;">const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV,
})
const xlsx = require('node-xlsx');
const db = cloud.database()
exports.main = async (event, context) => {
const fileID = 'cloud://xly-xrlur.786c-xly-xrlur-1300446086/china.csv'
const res = await cloud.downloadFile({
fileID: fileID,
})
const buffer = res.fileContent
const tasks = []
var sheets = xlsx.parse(buffer);
sheets.forEach(function (sheet) {
for (var rowId in sheet['data']) {
console.log(rowId);
var row = sheet['data'][rowId];
if (rowId > 0 && row) {
const promise = db.collection('chinaexcel')
.add({
data: {
city: row[0],
province: row[1],
city_area: row[2],
builtup_area: row[3],
reg_pop: row[4],
resident_pop: row[5],
gdp: row[6]
}
})
tasks.push(promise)
}
}
});
let result = await Promise.all(tasks).then(res => {
return res
}).catch(function (err) {
return err
})
return result
}</textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -28px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-1" style="height: 20px;">1</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-2" style="height: 20px;">2</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-3" style="height: 20px;">3</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-4" style="height: 20px;">4</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-5" style="height: 20px;">5</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-6" style="height: 20px;">6</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-7" style="height: 20px;">7</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-8" style="height: 20px;">8</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-9" style="height: 20px;">9</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-10" style="height: 20px;">10</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-11" style="height: 20px;">11</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-12" style="height: 20px;">12</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-13" style="height: 20px;">13</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-14" style="height: 20px;">14</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-15" style="height: 20px;">15</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-16" style="height: 20px;">16</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-17" style="height: 20px;">17</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-18" style="height: 20px;">18</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-19" style="height: 20px;">19</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-20" style="height: 20px;">20</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-21" style="height: 20px;">21</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-22" style="height: 20px;">22</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-23" style="height: 20px;">23</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-24" style="height: 20px;">24</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-25" style="height: 20px;">25</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-26" style="height: 20px;">26</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-27" style="height: 20px;">27</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-28" style="height: 20px;">28</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-29" style="height: 20px;">29</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-30" style="height: 20px;">30</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-31" style="height: 20px;">31</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-32" style="height: 20px;">32</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-33" style="height: 20px;">33</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-34" style="height: 20px;">34</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-35" style="height: 20px;">35</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-36" style="height: 20px;">36</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-37" style="height: 20px;">37</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-38" style="height: 20px;">38</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-39" style="height: 20px;">39</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-40" style="height: 20px;">40</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-41" style="height: 20px;">41</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-42" style="height: 20px;">42</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-43" style="height: 20px;">43</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-44" style="height: 20px;">44</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-45" style="height: 20px;">45</div><div class="crayon-num" data-line="crayon-5e40dd5157067523822703-46" style="height: 20px;">46</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5e40dd5157067523822703-1"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">cloud</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'wx-server-sdk'</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-2"><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-e">init</span><span class="crayon-sy">(</span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-3"><span class="crayon-h"> </span><span class="crayon-v">env</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-v">DYNAMIC_CURRENT_ENV</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-4"><span class="crayon-sy">}</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-5"> </div><div class="crayon-line" id="crayon-5e40dd5157067523822703-6"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">xlsx</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'node-xlsx'</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-7"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">db</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-e">database</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-8"> </div><div class="crayon-line" id="crayon-5e40dd5157067523822703-9"><span class="crayon-v">exports</span><span class="crayon-sy">.</span><span class="crayon-v">main</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">async</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-v">event</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">context</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-o">></span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-10"><span class="crayon-h"> </span><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-e ">fileID</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-s">'cloud://xly-xrlur.786c-xly-xrlur-1300446086/china.csv'</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-11"><span class="crayon-h"> </span><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">res</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">await </span><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-e">downloadFile</span><span class="crayon-sy">(</span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-12"><span class="crayon-h"> </span><span class="crayon-v">fileID</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">fileID</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-13"><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-14"><span class="crayon-h"> </span><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">buffer</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">res</span><span class="crayon-sy">.</span><span class="crayon-e">fileContent</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-15"> </div><div class="crayon-line" id="crayon-5e40dd5157067523822703-16"><span class="crayon-e"> </span><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">tasks</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-sy">[</span><span class="crayon-sy">]</span><span class="crayon-h"> </span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-17"><span class="crayon-h"> </span><span class="crayon-t">var</span><span class="crayon-h"> </span><span class="crayon-v">sheets</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">xlsx</span><span class="crayon-sy">.</span><span class="crayon-e">parse</span><span class="crayon-sy">(</span><span class="crayon-v">buffer</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span><span class="crayon-h"> </span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-18"><span class="crayon-h"> </span><span class="crayon-v">sheets</span><span class="crayon-sy">.</span><span class="crayon-st">forEach</span><span class="crayon-sy">(</span><span class="crayon-t">function</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-v">sheet</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-19"><span class="crayon-h"> </span><span class="crayon-st">for</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-t">var</span><span class="crayon-h"> </span><span class="crayon-e">rowId </span><span class="crayon-st">in</span><span class="crayon-h"> </span><span class="crayon-v">sheet</span><span class="crayon-sy">[</span><span class="crayon-s">'data'</span><span class="crayon-sy">]</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-20"><span class="crayon-h"> </span><span class="crayon-v">console</span><span class="crayon-sy">.</span><span class="crayon-e">log</span><span class="crayon-sy">(</span><span class="crayon-v">rowId</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-21"><span class="crayon-h"> </span><span class="crayon-t">var</span><span class="crayon-h"> </span><span class="crayon-v">row</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">sheet</span><span class="crayon-sy">[</span><span class="crayon-s">'data'</span><span class="crayon-sy">]</span><span class="crayon-sy">[</span><span class="crayon-v">rowId</span><span class="crayon-sy">]</span><span class="crayon-sy">;</span><span class="crayon-h"> </span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-22"><span class="crayon-h"> </span><span class="crayon-st">if</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-v">rowId</span><span class="crayon-h"> </span><span class="crayon-o">></span><span class="crayon-h"> </span><span class="crayon-cn">0</span><span class="crayon-h"> </span><span class="crayon-o">&&</span><span class="crayon-h"> </span><span class="crayon-v">row</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-sy">{</span><span class="crayon-h"> </span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-23"><span class="crayon-h"> </span><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">promise</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">db</span><span class="crayon-sy">.</span><span class="crayon-e">collection</span><span class="crayon-sy">(</span><span class="crayon-s">'chinaexcel'</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-24"><span class="crayon-h"> </span><span class="crayon-sy">.</span><span class="crayon-e">add</span><span class="crayon-sy">(</span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-25"><span class="crayon-h"> </span><span class="crayon-v">data</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-26"><span class="crayon-h"> </span><span class="crayon-v">city</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">row</span><span class="crayon-sy">[</span><span class="crayon-cn">0</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span><span class="crayon-h"> </span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-27"><span class="crayon-h"> </span><span class="crayon-v">province</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">row</span><span class="crayon-sy">[</span><span class="crayon-cn">1</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span><span class="crayon-h"> </span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-28"><span class="crayon-h"> </span><span class="crayon-v">city_area</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">row</span><span class="crayon-sy">[</span><span class="crayon-cn">2</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span><span class="crayon-h"> </span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-29"><span class="crayon-h"> </span><span class="crayon-v">builtup_area</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">row</span><span class="crayon-sy">[</span><span class="crayon-cn">3</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-30"><span class="crayon-h"> </span><span class="crayon-v">reg_pop</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">row</span><span class="crayon-sy">[</span><span class="crayon-cn">4</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-31"><span class="crayon-h"> </span><span class="crayon-v">resident_pop</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">row</span><span class="crayon-sy">[</span><span class="crayon-cn">5</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-32"><span class="crayon-h"> </span><span class="crayon-v">gdp</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">row</span><span class="crayon-sy">[</span><span class="crayon-cn">6</span><span class="crayon-sy">]</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-33"><span class="crayon-h"> </span><span class="crayon-sy">}</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-34"><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-35"><span class="crayon-h"> </span><span class="crayon-v">tasks</span><span class="crayon-sy">.</span><span class="crayon-e">push</span><span class="crayon-sy">(</span><span class="crayon-v">promise</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-36"><span class="crayon-h"> </span><span class="crayon-sy">}</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-37"><span class="crayon-h"> </span><span class="crayon-sy">}</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-38"><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-39"> </div><div class="crayon-line" id="crayon-5e40dd5157067523822703-40"><span class="crayon-h"> </span><span class="crayon-e">let </span><span class="crayon-v">result</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">await </span><span class="crayon-v">Promise</span><span class="crayon-sy">.</span><span class="crayon-e">all</span><span class="crayon-sy">(</span><span class="crayon-v">tasks</span><span class="crayon-sy">)</span><span class="crayon-sy">.</span><span class="crayon-st">then</span><span class="crayon-sy">(</span><span class="crayon-v">res</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-o">></span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-41"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-i">res</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-42"><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-sy">)</span><span class="crayon-sy">.</span><span class="crayon-st">catch</span><span class="crayon-sy">(</span><span class="crayon-t">function</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-v">err</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-43"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-i">err</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-44"><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-45"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-i">result</span></div><div class="crayon-line" id="crayon-5e40dd5157067523822703-46"><span class="crayon-sy">}</span></div></div></td>
</tr>
</tbody></table>
</div>
</div><p>将数据库里的数据保存为CSV</p>
<p><strong>技术文档:</strong><a href="https://github.com/zemirco/json2csv" target="_blank" rel="noopener noreferrer">json2CSV</a></p>
<p> </p>
<h4>HTTP处理</h4>
<p>got、superagent、request、axios、request-promise</p>
<blockquote><p>尽管云函数的Nodejs版本比较低(目前为8.9),但绝大多数模块我们都可以使用Nodejs 12或13的环境来测试,不过有时候也要留意有些模块不支持8.9,比如got 10.0.1以上的版本。</p></blockquote>
<p> </p>
<p>node中,http模块也可作为客户端使用(发送请求),第三方模块request对其使用方法进行了封装,操作更方便!所以来介绍一下request模块</p>
<p><strong>get请求</strong></p>
<div id="crayon-5e40dd5157069590912963" class="crayon-syntax crayon-theme-monokai crayon-font-monaco crayon-os-mac print-yes notranslate crayon-wrapped" data-settings=" minimize scroll-mouseover disable-anim wrap" style="margin-top: 15px; margin-bottom: 15px; font-size: 14px !important; line-height: 20px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 20px !important; z-index: 0; opacity: 0; overflow: hidden;">const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV,
})
const rp = require('request-promise')
exports.main = async (event, context) => {
const options = {
url: 'https://news-at.zhihu.com/api/4/news/latest',
json: true,
method: 'GET',
};
return await rp(options)
}</textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -28px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5e40dd5157069590912963-1" style="height: 20px;">1</div><div class="crayon-num" data-line="crayon-5e40dd5157069590912963-2" style="height: 20px;">2</div><div class="crayon-num" data-line="crayon-5e40dd5157069590912963-3" style="height: 20px;">3</div><div class="crayon-num" data-line="crayon-5e40dd5157069590912963-4" style="height: 20px;">4</div><div class="crayon-num" data-line="crayon-5e40dd5157069590912963-5" style="height: 20px;">5</div><div class="crayon-num" data-line="crayon-5e40dd5157069590912963-6" style="height: 20px;">6</div><div class="crayon-num" data-line="crayon-5e40dd5157069590912963-7" style="height: 20px;">7</div><div class="crayon-num" data-line="crayon-5e40dd5157069590912963-8" style="height: 20px;">8</div><div class="crayon-num" data-line="crayon-5e40dd5157069590912963-9" style="height: 20px;">9</div><div class="crayon-num" data-line="crayon-5e40dd5157069590912963-10" style="height: 20px;">10</div><div class="crayon-num" data-line="crayon-5e40dd5157069590912963-11" style="height: 20px;">11</div><div class="crayon-num" data-line="crayon-5e40dd5157069590912963-12" style="height: 20px;">12</div><div class="crayon-num" data-line="crayon-5e40dd5157069590912963-13" style="height: 20px;">13</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5e40dd5157069590912963-1"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">cloud</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'wx-server-sdk'</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157069590912963-2"><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-e">init</span><span class="crayon-sy">(</span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157069590912963-3"><span class="crayon-h"> </span><span class="crayon-v">env</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-v">DYNAMIC_CURRENT_ENV</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157069590912963-4"><span class="crayon-sy">}</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157069590912963-5"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">rp</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'request-promise'</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157069590912963-6"><span class="crayon-v">exports</span><span class="crayon-sy">.</span><span class="crayon-v">main</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">async</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-v">event</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">context</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-o">></span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157069590912963-7"><span class="crayon-h"> </span><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">options</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157069590912963-8"><span class="crayon-h"> </span><span class="crayon-v">url</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-s">'https://news-at.zhihu.com/api/4/news/latest'</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157069590912963-9"><span class="crayon-h"> </span><span class="crayon-v">json</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-t">true</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157069590912963-10"><span class="crayon-h"> </span><span class="crayon-v">method</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-s">'GET'</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157069590912963-11"><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd5157069590912963-12"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-e">await </span><span class="crayon-e">rp</span><span class="crayon-sy">(</span><span class="crayon-v">options</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157069590912963-13"><span class="crayon-sy">}</span></div></div></td>
</tr>
</tbody></table>
</div>
</div><p><strong>post请求</strong></p>
<p> </p>
<p><strong>结合文件流</strong></p>
<div id="crayon-5e40dd515706a386232891" class="crayon-syntax crayon-theme-monokai crayon-font-monaco crayon-os-mac print-yes notranslate crayon-wrapped" data-settings=" minimize scroll-mouseover disable-anim wrap" style="margin-top: 15px; margin-bottom: 15px; font-size: 14px !important; line-height: 20px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 20px !important; z-index: 0; opacity: 0; overflow: hidden;">request('https://www.jmjc.tech/public/home/img/flower.png').pipe(fs.createWriteStream('./flower.png')) // 下载文件到本地</textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -20px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5e40dd515706a386232891-1" style="height: 20px;">1</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5e40dd515706a386232891-1"><span class="crayon-e">request</span><span class="crayon-sy">(</span><span class="crayon-s">'https://www.jmjc.tech/public/home/img/flower.png'</span><span class="crayon-sy">)</span><span class="crayon-sy">.</span><span class="crayon-e">pipe</span><span class="crayon-sy">(</span><span class="crayon-v">fs</span><span class="crayon-sy">.</span><span class="crayon-e">createWriteStream</span><span class="crayon-sy">(</span><span class="crayon-s">'./flower.png'</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-c">// 下载文件到本地</span></div></div></td>
</tr>
</tbody></table>
</div>
</div><p></p>
<h4>加解密Crypto</h4>
<p>crypto模块是nodejs的核心模块之一,它提供了安全相关的功能,包含对 OpenSSL 的哈希、HMAC、加密、解密、签名、以及验证功能的一整套封装。由于crypto模块是内置模块,我们引入它是无需下载,就可以直接引入。</p>
<p>使用开发者工具新建一个云函数,比如crypto,在index.js里输入以下代码,我们来了解一下crypto支持哪些加密算法,并以MD5加密为例:</p>
<div id="crayon-5e40dd515706c171525188" class="crayon-syntax crayon-theme-monokai crayon-font-monaco crayon-os-mac print-yes notranslate crayon-wrapped" data-settings=" minimize scroll-mouseover disable-anim wrap" style="margin-top: 15px; margin-bottom: 15px; font-size: 14px !important; line-height: 20px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 20px !important; z-index: 0; opacity: 0; overflow: hidden;">const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV,
})
const crypto = require('crypto');
exports.main = async (event, context) => {
const hashes = crypto.getHashes(); //获取crypto支持的加密算法种类列表
//md5 加密 CloudBase2020 返回十六进制
var md5 = crypto.createHash('md5');
var message = 'CloudBase2020';
var digest = md5.update(message, 'utf8').digest('hex');
return {
"crypto支持的加密算法种类":hashes,
"md5加密返回的十六进制":digest
};
}</textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -28px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5e40dd515706c171525188-1" style="height: 20px;">1</div><div class="crayon-num" data-line="crayon-5e40dd515706c171525188-2" style="height: 20px;">2</div><div class="crayon-num" data-line="crayon-5e40dd515706c171525188-3" style="height: 20px;">3</div><div class="crayon-num" data-line="crayon-5e40dd515706c171525188-4" style="height: 20px;">4</div><div class="crayon-num" data-line="crayon-5e40dd515706c171525188-5" style="height: 20px;">5</div><div class="crayon-num" data-line="crayon-5e40dd515706c171525188-6" style="height: 20px;">6</div><div class="crayon-num" data-line="crayon-5e40dd515706c171525188-7" style="height: 20px;">7</div><div class="crayon-num" data-line="crayon-5e40dd515706c171525188-8" style="height: 20px;">8</div><div class="crayon-num" data-line="crayon-5e40dd515706c171525188-9" style="height: 20px;">9</div><div class="crayon-num" data-line="crayon-5e40dd515706c171525188-10" style="height: 20px;">10</div><div class="crayon-num" data-line="crayon-5e40dd515706c171525188-11" style="height: 20px;">11</div><div class="crayon-num" data-line="crayon-5e40dd515706c171525188-12" style="height: 20px;">12</div><div class="crayon-num" data-line="crayon-5e40dd515706c171525188-13" style="height: 20px;">13</div><div class="crayon-num" data-line="crayon-5e40dd515706c171525188-14" style="height: 20px;">14</div><div class="crayon-num" data-line="crayon-5e40dd515706c171525188-15" style="height: 20px;">15</div><div class="crayon-num" data-line="crayon-5e40dd515706c171525188-16" style="height: 20px;">16</div><div class="crayon-num" data-line="crayon-5e40dd515706c171525188-17" style="height: 20px;">17</div><div class="crayon-num" data-line="crayon-5e40dd515706c171525188-18" style="height: 20px;">18</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5e40dd515706c171525188-1"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">cloud</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'wx-server-sdk'</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd515706c171525188-2"><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-e">init</span><span class="crayon-sy">(</span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd515706c171525188-3"><span class="crayon-h"> </span><span class="crayon-v">env</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-v">DYNAMIC_CURRENT_ENV</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd515706c171525188-4"><span class="crayon-sy">}</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd515706c171525188-5"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">crypto</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'crypto'</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd515706c171525188-6"><span class="crayon-v">exports</span><span class="crayon-sy">.</span><span class="crayon-v">main</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">async</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-v">event</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">context</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-o">></span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd515706c171525188-7"><span class="crayon-h"> </span><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">hashes</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">crypto</span><span class="crayon-sy">.</span><span class="crayon-e">getHashes</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span><span class="crayon-h"> </span><span class="crayon-c">//获取crypto支持的加密算法种类列表</span></div><div class="crayon-line" id="crayon-5e40dd515706c171525188-8"> </div><div class="crayon-line" id="crayon-5e40dd515706c171525188-9"><span class="crayon-h"> </span><span class="crayon-c">//md5 加密 CloudBase2020 返回十六进制</span></div><div class="crayon-line" id="crayon-5e40dd515706c171525188-10"><span class="crayon-h"> </span><span class="crayon-t">var</span><span class="crayon-h"> </span><span class="crayon-v">md5</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">crypto</span><span class="crayon-sy">.</span><span class="crayon-e">createHash</span><span class="crayon-sy">(</span><span class="crayon-s">'md5'</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd515706c171525188-11"><span class="crayon-h"> </span><span class="crayon-t">var</span><span class="crayon-h"> </span><span class="crayon-e ">message</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-s">'CloudBase2020'</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd515706c171525188-12"><span class="crayon-h"> </span><span class="crayon-t">var</span><span class="crayon-h"> </span><span class="crayon-v">digest</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">md5</span><span class="crayon-sy">.</span><span class="crayon-e">update</span><span class="crayon-sy">(</span><span class="crayon-v">message</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-s">'utf8'</span><span class="crayon-sy">)</span><span class="crayon-sy">.</span><span class="crayon-e">digest</span><span class="crayon-sy">(</span><span class="crayon-s">'hex'</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span><span class="crayon-h"> </span></div><div class="crayon-line" id="crayon-5e40dd515706c171525188-13"> </div><div class="crayon-line" id="crayon-5e40dd515706c171525188-14"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd515706c171525188-15"><span class="crayon-h"> </span><span class="crayon-s">"crypto支持的加密算法种类"</span><span class="crayon-o">:</span><span class="crayon-v">hashes</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd515706c171525188-16"><span class="crayon-h"> </span><span class="crayon-s">"md5加密返回的十六进制"</span><span class="crayon-o">:</span><span class="crayon-i">digest</span></div><div class="crayon-line" id="crayon-5e40dd515706c171525188-17"><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd515706c171525188-18"><span class="crayon-sy">}</span></div></div></td>
</tr>
</tbody></table>
</div>
</div><p>将云函数部署之后调用从返回的结果我们可以了解到,云函数crypto模块支持46种加密算法。</p>
<h4>发短信</h4>
<div>
<div>“qcloudsms_js”: “^0.1.1”</div>
</div>
<p></p>
<div id="crayon-5e40dd515706e568558567" class="crayon-syntax crayon-theme-monokai crayon-font-monaco crayon-os-mac print-yes notranslate crayon-wrapped" data-settings=" minimize scroll-mouseover disable-anim wrap" style="margin-top: 15px; margin-bottom: 15px; font-size: 14px !important; line-height: 20px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 20px !important; z-index: 0; opacity: 0; overflow: hidden;">const cloud = require('wx-server-sdk')
const QcloudSms = require("qcloudsms_js")
const appid = 1400284950 // 替换成您申请的云短信 AppID 以及 AppKey
const appkey = "a33b602345f3bb866f040303ac6f98ca"
const templateId = 472078 // 替换成您所申请模板 ID
const smsSign = "统计小助理" // 替换成您所申请的签名
cloud.init()
// 云函数入口函数
exports.main = async (event, context) => new Promise((resolve, reject) => {
/*单发短信示例为完整示例,更多功能请直接替换以下代码*/
var qcloudsms = QcloudSms(appid, appkey);
var ssender = qcloudsms.SmsSingleSender();
var params = ["1234", "15"];
// 获取发送短信的手机号码
var mobile = event.mobile
// 获取手机号国家/地区码
var nationcode = event.nationcode
ssender.sendWithParam(nationcode, mobile, templateId, params, smsSign, "", "", (err, res, resData) => {
/*设置请求回调处理, 这里只是演示,您需要自定义相应处理逻辑*/
if (err) {
console.log("err: ", err);
reject({ err })
} else {
resolve({ res: res.req, resData })
}
}
);
})</textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -28px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-1" style="height: 20px;">1</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-2" style="height: 20px;">2</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-3" style="height: 20px;">3</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-4" style="height: 20px;">4</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-5" style="height: 20px;">5</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-6" style="height: 20px;">6</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-7" style="height: 20px;">7</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-8" style="height: 20px;">8</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-9" style="height: 20px;">9</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-10" style="height: 20px;">10</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-11" style="height: 20px;">11</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-12" style="height: 20px;">12</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-13" style="height: 20px;">13</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-14" style="height: 20px;">14</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-15" style="height: 20px;">15</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-16" style="height: 20px;">16</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-17" style="height: 20px;">17</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-18" style="height: 20px;">18</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-19" style="height: 20px;">19</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-20" style="height: 20px;">20</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-21" style="height: 20px;">21</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-22" style="height: 20px;">22</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-23" style="height: 20px;">23</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-24" style="height: 20px;">24</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-25" style="height: 20px;">25</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-26" style="height: 20px;">26</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-27" style="height: 20px;">27</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-28" style="height: 20px;">28</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-29" style="height: 20px;">29</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-30" style="height: 20px;">30</div><div class="crayon-num" data-line="crayon-5e40dd515706e568558567-31" style="height: 20px;">31</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5e40dd515706e568558567-1"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">cloud</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">'wx-server-sdk'</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd515706e568558567-2"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">QcloudSms</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">require</span><span class="crayon-sy">(</span><span class="crayon-s">"qcloudsms_js"</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd515706e568558567-3"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">appid</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-cn">1400284950</span><span class="crayon-h"> </span><span class="crayon-c">// 替换成您申请的云短信 AppID 以及 AppKey</span></div><div class="crayon-line" id="crayon-5e40dd515706e568558567-4"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-e ">appkey</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-s">"a33b602345f3bb866f040303ac6f98ca"</span></div><div class="crayon-line" id="crayon-5e40dd515706e568558567-5"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-v">templateId</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-cn">472078</span><span class="crayon-h"> </span><span class="crayon-c">// 替换成您所申请模板 ID</span></div><div class="crayon-line" id="crayon-5e40dd515706e568558567-6"><span class="crayon-m">const</span><span class="crayon-h"> </span><span class="crayon-e ">smsSign</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-s">"统计小助理"</span><span class="crayon-h"> </span><span class="crayon-c">// 替换成您所申请的签名</span></div><div class="crayon-line" id="crayon-5e40dd515706e568558567-7"> </div><div class="crayon-line" id="crayon-5e40dd515706e568558567-8"><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-e">init</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd515706e568558567-9"> </div><div class="crayon-line" id="crayon-5e40dd515706e568558567-10"><span class="crayon-c">// 云函数入口函数</span></div><div class="crayon-line" id="crayon-5e40dd515706e568558567-11"><span class="crayon-v">exports</span><span class="crayon-sy">.</span><span class="crayon-v">main</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">async</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-v">event</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">context</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-o">></span><span class="crayon-h"> </span><span class="crayon-r">new</span><span class="crayon-h"> </span><span class="crayon-e">Promise</span><span class="crayon-sy">(</span><span class="crayon-sy">(</span><span class="crayon-v">resolve</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">reject</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-o">></span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd515706e568558567-12"><span class="crayon-h"> </span><span class="crayon-c">/*单发短信示例为完整示例,更多功能请直接替换以下代码*/</span></div><div class="crayon-line" id="crayon-5e40dd515706e568558567-13"><span class="crayon-h"> </span><span class="crayon-t">var</span><span class="crayon-h"> </span><span class="crayon-v">qcloudsms</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">QcloudSms</span><span class="crayon-sy">(</span><span class="crayon-v">appid</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">appkey</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd515706e568558567-14"><span class="crayon-h"> </span><span class="crayon-t">var</span><span class="crayon-h"> </span><span class="crayon-v">ssender</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">qcloudsms</span><span class="crayon-sy">.</span><span class="crayon-e">SmsSingleSender</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd515706e568558567-15"><span class="crayon-h"> </span><span class="crayon-t">var</span><span class="crayon-h"> </span><span class="crayon-v">params</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-sy">[</span><span class="crayon-s">"1234"</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-s">"15"</span><span class="crayon-sy">]</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd515706e568558567-16"><span class="crayon-h"> </span><span class="crayon-c">// 获取发送短信的手机号码</span></div><div class="crayon-line" id="crayon-5e40dd515706e568558567-17"><span class="crayon-h"> </span><span class="crayon-t">var</span><span class="crayon-h"> </span><span class="crayon-v">mobile</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">event</span><span class="crayon-sy">.</span><span class="crayon-v">mobile</span></div><div class="crayon-line" id="crayon-5e40dd515706e568558567-18"><span class="crayon-h"> </span><span class="crayon-c">// 获取手机号国家/地区码</span></div><div class="crayon-line" id="crayon-5e40dd515706e568558567-19"><span class="crayon-h"> </span><span class="crayon-t">var</span><span class="crayon-h"> </span><span class="crayon-v">nationcode</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">event</span><span class="crayon-sy">.</span><span class="crayon-e">nationcode</span></div><div class="crayon-line" id="crayon-5e40dd515706e568558567-20"><span class="crayon-e"> </span><span class="crayon-v">ssender</span><span class="crayon-sy">.</span><span class="crayon-e">sendWithParam</span><span class="crayon-sy">(</span><span class="crayon-v">nationcode</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">mobile</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">templateId</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">params</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">smsSign</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-s">""</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-s">""</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-v">err</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">res</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">resData</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-o">></span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd515706e568558567-21"><span class="crayon-h"> </span><span class="crayon-c">/*设置请求回调处理, 这里只是演示,您需要自定义相应处理逻辑*/</span></div><div class="crayon-line" id="crayon-5e40dd515706e568558567-22"><span class="crayon-h"> </span><span class="crayon-st">if</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-v">err</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd515706e568558567-23"><span class="crayon-h"> </span><span class="crayon-v">console</span><span class="crayon-sy">.</span><span class="crayon-e">log</span><span class="crayon-sy">(</span><span class="crayon-s">"err: "</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">err</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd515706e568558567-24"><span class="crayon-h"> </span><span class="crayon-e">reject</span><span class="crayon-sy">(</span><span class="crayon-sy">{</span><span class="crayon-h"> </span><span class="crayon-i">err</span><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd515706e568558567-25"><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-h"> </span><span class="crayon-st">else</span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd515706e568558567-26"><span class="crayon-h"> </span><span class="crayon-e">resolve</span><span class="crayon-sy">(</span><span class="crayon-sy">{</span><span class="crayon-h"> </span><span class="crayon-v">res</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">res</span><span class="crayon-sy">.</span><span class="crayon-v">req</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-i">resData</span><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd515706e568558567-27"><span class="crayon-h"> </span><span class="crayon-sy">}</span></div><div class="crayon-line" id="crayon-5e40dd515706e568558567-28"><span class="crayon-h"> </span><span class="crayon-sy">}</span></div><div class="crayon-line" id="crayon-5e40dd515706e568558567-29"><span class="crayon-h"> </span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5e40dd515706e568558567-30"> </div><div class="crayon-line" id="crayon-5e40dd515706e568558567-31"><span class="crayon-sy">}</span><span class="crayon-sy">)</span></div></div></td>
</tr>
</tbody></table>
</div>
</div><p>使用开发者工具</p>
<div id="crayon-5e40dd5157070984296937" class="crayon-syntax crayon-theme-monokai crayon-font-monaco crayon-os-mac print-yes notranslate crayon-wrapped" data-settings=" minimize scroll-mouseover disable-anim wrap" style="margin-top: 15px; margin-bottom: 15px; font-size: 14px !important; line-height: 20px !important; height: auto;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 20px !important; z-index: 0; opacity: 0; overflow: hidden;">wx.cloud.callFunction({
name: 'sendphone',
data: {
// mobile: '13217922526',
mobile: '18565678773',
nationcode: '86'
},
success: res => {
console.log('[云函数] [sendsms] 调用成功')
console.log(res)
},
fail: err => {
console.error('[云函数] [sendsms] 调用失败', err)
}
})</textarea></div>
<div class="crayon-main" style="position: relative; z-index: 1; overflow: hidden;">
<table class="crayon-table" style="margin-left: -28px;">
<tbody><tr class="crayon-row">
<td class="crayon-nums " data-settings="hide">
<div class="crayon-nums-content" style="font-size: 14px !important; line-height: 20px !important;"><div class="crayon-num" data-line="crayon-5e40dd5157070984296937-1" style="height: 20px;">1</div><div class="crayon-num" data-line="crayon-5e40dd5157070984296937-2" style="height: 20px;">2</div><div class="crayon-num" data-line="crayon-5e40dd5157070984296937-3" style="height: 20px;">3</div><div class="crayon-num" data-line="crayon-5e40dd5157070984296937-4" style="height: 20px;">4</div><div class="crayon-num" data-line="crayon-5e40dd5157070984296937-5" style="height: 20px;">5</div><div class="crayon-num" data-line="crayon-5e40dd5157070984296937-6" style="height: 20px;">6</div><div class="crayon-num" data-line="crayon-5e40dd5157070984296937-7" style="height: 20px;">7</div><div class="crayon-num" data-line="crayon-5e40dd5157070984296937-8" style="height: 20px;">8</div><div class="crayon-num" data-line="crayon-5e40dd5157070984296937-9" style="height: 20px;">9</div><div class="crayon-num" data-line="crayon-5e40dd5157070984296937-10" style="height: 20px;">10</div><div class="crayon-num" data-line="crayon-5e40dd5157070984296937-11" style="height: 20px;">11</div><div class="crayon-num" data-line="crayon-5e40dd5157070984296937-12" style="height: 20px;">12</div><div class="crayon-num" data-line="crayon-5e40dd5157070984296937-13" style="height: 20px;">13</div><div class="crayon-num" data-line="crayon-5e40dd5157070984296937-14" style="height: 20px;">14</div><div class="crayon-num" data-line="crayon-5e40dd5157070984296937-15" style="height: 20px;">15</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 14px !important; line-height: 20px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5e40dd5157070984296937-1"><span class="crayon-v">wx</span><span class="crayon-sy">.</span><span class="crayon-v">cloud</span><span class="crayon-sy">.</span><span class="crayon-e">callFunction</span><span class="crayon-sy">(</span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157070984296937-2"><span class="crayon-h"> </span><span class="crayon-v">name</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-s">'sendphone'</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157070984296937-3"><span class="crayon-h"> </span><span class="crayon-v">data</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157070984296937-4"><span class="crayon-h"> </span><span class="crayon-c">// mobile: '13217922526',</span></div><div class="crayon-line" id="crayon-5e40dd5157070984296937-5"><span class="crayon-h"> </span><span class="crayon-v">mobile</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-s">'18565678773'</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157070984296937-6"><span class="crayon-h"> </span><span class="crayon-v">nationcode</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-s">'86'</span></div><div class="crayon-line" id="crayon-5e40dd5157070984296937-7"><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157070984296937-8"><span class="crayon-h"> </span><span class="crayon-v">success</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">res</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-o">></span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157070984296937-9"><span class="crayon-h"> </span><span class="crayon-v">console</span><span class="crayon-sy">.</span><span class="crayon-e">log</span><span class="crayon-sy">(</span><span class="crayon-s">'[云函数] [sendsms] 调用成功'</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157070984296937-10"><span class="crayon-h"> </span><span class="crayon-v">console</span><span class="crayon-sy">.</span><span class="crayon-e">log</span><span class="crayon-sy">(</span><span class="crayon-v">res</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157070984296937-11"><span class="crayon-h"> </span><span class="crayon-sy">}</span><span class="crayon-sy">,</span></div><div class="crayon-line" id="crayon-5e40dd5157070984296937-12"><span class="crayon-h"> </span><span class="crayon-v">fail</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">err</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-o">></span><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5e40dd5157070984296937-13"><span class="crayon-h"> </span><span class="crayon-v">console</span><span class="crayon-sy">.</span><span class="crayon-e">error</span><span class="crayon-sy">(</span><span class="crayon-s">'[云函数] [sendsms] 调用失败'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">err</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5e40dd5157070984296937-14"><span class="crayon-h"> </span><span class="crayon-sy">}</span></div><div class="crayon-line" id="crayon-5e40dd5157070984296937-15"><span class="crayon-sy">}</span><span class="crayon-sy">)</span></div></div></td>
</tr>
</tbody></table>
</div>
</div><p></p>
<div class="sop-footer">
<hr><p><a href="https://www.zhihu.com/people/lidongyx" target="_blank">李东bbsky</a>:致力于互联网技术技能的普及。Tell me and I forget . Teach me and I remember. Involve me and I learn. </p>
</div>
</div><!-- #post-## -->
<!-- <div class="sop-bottom">
<span class="sop-bottom-tip">想要更系统的指导和一起学习的伙伴,欢迎加入:</span>
<a target="_blank" class="sop-more" href="https://h5.youzan.com/v2/goods/361m5sscp7awc">线上学习社区</a>
<span class="sop-bottom-tip">进群加微信:jishuzu360</span>
</div> -->
</div><!-- #content -->
</div><!-- #primary -->
<link rel="stylesheet" id="crayon-css" href="https://tcb-1251009918.cos.ap-guangzhou.myqcloud.com/pc/cdn/wp-content/plugins/crayon-syntax-highlighter/css/min/crayon.min.css?ver=_2.7.2_beta" type="text/css" media="all">
<script type="text/javascript" src="https://tcb-1251009918.cos.ap-guangzhou.myqcloud.com/pc/cdn/wp-includes/js/comment-reply.min.js?ver=5.2.2"></script>
<script type="text/javascript" src="https://tcb-1251009918.cos.ap-guangzhou.myqcloud.com/pc/cdn/wp-content/themes/twentyfourteen/js/functions.js?ver=20150315"></script>
<script type="text/javascript" src="https://tcb-1251009918.cos.ap-guangzhou.myqcloud.com/pc/cdn/wp-includes/js/wp-embed.min.js?ver=5.2.2"></script>
<script type="text/javascript">
/* <![CDATA[ */
var CrayonSyntaxSettings = {"version":"_2.7.2_beta","is_admin":"0","ajaxurl":"https:\/\/cloudbasegroup.org\/wp-admin\/admin-ajax.php","prefix":"crayon-","setting":"crayon-setting","selected":"crayon-setting-selected","changed":"crayon-setting-changed","special":"crayon-setting-special","orig_value":"data-orig-value","debug":""};
var CrayonSyntaxStrings = {"copy":"Press %s to Copy, %s to Paste","minimize":"Click To Expand Code"};
/* ]]> */
</script>
<script type="text/javascript" src="https://tcb-1251009918.cos.ap-guangzhou.myqcloud.com/pc/cdn/wp-content/plugins/crayon-syntax-highlighter/js/min/crayon.min.js?ver=_2.7.2_beta"></script>
<script src="https://hackweek.oss-cn-shanghai.aliyuncs.com/hw18/component/jquery.min.js"></script>
<script src="https://hackweek.oss-cn-shanghai.aliyuncs.com/hw18/component/bootstrap.min.js"></script>
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?8efc2c9aaaffb2f7a4cf6a7ff9c65cbb";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
</body></html>