index.html 370 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width,initial-scale=1">
  6. <title>小程序笔记 | 彪哥博客</title>
  7. <meta name="generator" content="VuePress 1.9.5">
  8. <link rel="icon" href="/blog/img/favicon.ico">
  9. <meta name="description" content="web前端技术博客,专注web前端学习与总结。JavaScript,js,ES6,TypeScript,vue,React,python,css3,html5,Node,git,github等技术文章。">
  10. <meta name="keywords" content="前端博客,个人技术博客,前端,前端开发,前端框架,web前端,前端面试题,技术文档,学习,面试,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,markdown">
  11. <meta name="theme-color" content="#11a8cd">
  12. <link rel="preload" href="/blog/assets/css/0.styles.cf8cd190.css" as="style"><link rel="preload" href="/blog/assets/js/app.28edfb28.js" as="script"><link rel="preload" href="/blog/assets/js/2.395c0d18.js" as="script"><link rel="preload" href="/blog/assets/js/3.6748bd5c.js" as="script"><link rel="preload" href="/blog/assets/js/32.66c0e717.js" as="script"><link rel="prefetch" href="/blog/assets/js/10.cad3aa70.js"><link rel="prefetch" href="/blog/assets/js/100.08a8b2d8.js"><link rel="prefetch" href="/blog/assets/js/101.2aabb12c.js"><link rel="prefetch" href="/blog/assets/js/102.13f3cc4d.js"><link rel="prefetch" href="/blog/assets/js/103.c19aee03.js"><link rel="prefetch" href="/blog/assets/js/104.91a6aec1.js"><link rel="prefetch" href="/blog/assets/js/105.71de1aa4.js"><link rel="prefetch" href="/blog/assets/js/106.207422de.js"><link rel="prefetch" href="/blog/assets/js/107.bf754f60.js"><link rel="prefetch" href="/blog/assets/js/108.a43622c2.js"><link rel="prefetch" href="/blog/assets/js/109.3219d87b.js"><link rel="prefetch" href="/blog/assets/js/11.f2e9eca8.js"><link rel="prefetch" href="/blog/assets/js/110.f41596b0.js"><link rel="prefetch" href="/blog/assets/js/111.da99a105.js"><link rel="prefetch" href="/blog/assets/js/112.9dd75c6f.js"><link rel="prefetch" href="/blog/assets/js/113.9322f157.js"><link rel="prefetch" href="/blog/assets/js/114.c5150cc6.js"><link rel="prefetch" href="/blog/assets/js/115.f58b49f8.js"><link rel="prefetch" href="/blog/assets/js/116.8f7d9c6f.js"><link rel="prefetch" href="/blog/assets/js/117.3c970f48.js"><link rel="prefetch" href="/blog/assets/js/118.921e1d54.js"><link rel="prefetch" href="/blog/assets/js/119.0141defb.js"><link rel="prefetch" href="/blog/assets/js/12.98512c60.js"><link rel="prefetch" href="/blog/assets/js/120.de47a761.js"><link rel="prefetch" href="/blog/assets/js/121.a0b3693a.js"><link rel="prefetch" href="/blog/assets/js/122.6c7dd225.js"><link rel="prefetch" href="/blog/assets/js/123.dbff103c.js"><link rel="prefetch" href="/blog/assets/js/124.493776ef.js"><link rel="prefetch" href="/blog/assets/js/125.554c9fbf.js"><link rel="prefetch" href="/blog/assets/js/126.9d3b75dc.js"><link rel="prefetch" href="/blog/assets/js/127.aad20a7e.js"><link rel="prefetch" href="/blog/assets/js/128.6543adba.js"><link rel="prefetch" href="/blog/assets/js/129.d7c56b92.js"><link rel="prefetch" href="/blog/assets/js/13.a79fa0c7.js"><link rel="prefetch" href="/blog/assets/js/130.593d21f0.js"><link rel="prefetch" href="/blog/assets/js/131.4c90d8b8.js"><link rel="prefetch" href="/blog/assets/js/132.4ad12bdc.js"><link rel="prefetch" href="/blog/assets/js/133.6df121d8.js"><link rel="prefetch" href="/blog/assets/js/134.65e904d2.js"><link rel="prefetch" href="/blog/assets/js/135.47498729.js"><link rel="prefetch" href="/blog/assets/js/136.d99350df.js"><link rel="prefetch" href="/blog/assets/js/137.71e8757e.js"><link rel="prefetch" href="/blog/assets/js/138.75e29c6f.js"><link rel="prefetch" href="/blog/assets/js/139.766f20b7.js"><link rel="prefetch" href="/blog/assets/js/14.0fdf0c78.js"><link rel="prefetch" href="/blog/assets/js/140.0d9b8fbc.js"><link rel="prefetch" href="/blog/assets/js/141.128b6e26.js"><link rel="prefetch" href="/blog/assets/js/142.e004a584.js"><link rel="prefetch" href="/blog/assets/js/143.5a19a0b2.js"><link rel="prefetch" href="/blog/assets/js/144.5f397211.js"><link rel="prefetch" href="/blog/assets/js/145.3b994de3.js"><link rel="prefetch" href="/blog/assets/js/146.24441c89.js"><link rel="prefetch" href="/blog/assets/js/147.f7f1952e.js"><link rel="prefetch" href="/blog/assets/js/148.25581f95.js"><link rel="prefetch" href="/blog/assets/js/149.7a6ca4f7.js"><link rel="prefetch" href="/blog/assets/js/15.fce722b2.js"><link rel="prefetch" href="/blog/assets/js/150.3a1675b7.js"><link rel="prefetch" href="/blog/assets/js/151.250a4894.js"><link rel="prefetch" href="/blog/assets/js/152.fa700729.js"><link rel="prefetch" href="/blog/assets/js/153.cc421f44.js"><link rel="prefetch" href="/blog/assets/js/154.a04c0164.js"><link rel="prefetch" href="/blog/assets/js/155.b5c0abcd.js"><link rel="prefetch" href="/blog/assets/js/156.2ba750fc.js"><link rel="prefetch" href="/blog/assets/js/157.99ee745c.js"><link rel="prefetch" href="/blog/assets/js/158.09b15f2b.js"><link rel="prefetch" href="/blog/assets/js/159.3fd855e1.js"><link rel="prefetch" href="/blog/assets/js/16.7cf1b239.js"><link rel="prefetch" href="/blog/assets/js/160.307b6a2e.js"><link rel="prefetch" href="/blog/assets/js/161.97acae68.js"><link rel="prefetch" href="/blog/assets/js/162.86431056.js"><link rel="prefetch" href="/blog/assets/js/163.f1bfa2fe.js"><link rel="prefetch" href="/blog/assets/js/164.105b48fa.js"><link rel="prefetch" href="/blog/assets/js/165.700f83da.js"><link rel="prefetch" href="/blog/assets/js/166.8de21024.js"><link rel="prefetch" href="/blog/assets/js/167.2b670c3d.js"><link rel="prefetch" href="/blog/assets/js/168.330ac31c.js"><link rel="prefetch" href="/blog/assets/js/169.f8233c38.js"><link rel="prefetch" href="/blog/assets/js/17.93f492a1.js"><link rel="prefetch" href="/blog/assets/js/170.7c8b0366.js"><link rel="prefetch" href="/blog/assets/js/171.c3155533.js"><link rel="prefetch" href="/blog/assets/js/172.b659d767.js"><link rel="prefetch" href="/blog/assets/js/173.62c681db.js"><link rel="prefetch" href="/blog/assets/js/174.5c66f092.js"><link rel="prefetch" href="/blog/assets/js/175.d41dd28b.js"><link rel="prefetch" href="/blog/assets/js/176.e60d7f0a.js"><link rel="prefetch" href="/blog/assets/js/177.10de95b1.js"><link rel="prefetch" href="/blog/assets/js/178.f301674d.js"><link rel="prefetch" href="/blog/assets/js/179.4d54a4ff.js"><link rel="prefetch" href="/blog/assets/js/18.f17de23a.js"><link rel="prefetch" href="/blog/assets/js/180.955aa8ec.js"><link rel="prefetch" href="/blog/assets/js/181.a53e32e0.js"><link rel="prefetch" href="/blog/assets/js/182.38687994.js"><link rel="prefetch" href="/blog/assets/js/183.544fef00.js"><link rel="prefetch" href="/blog/assets/js/184.711e54a6.js"><link rel="prefetch" href="/blog/assets/js/185.20075148.js"><link rel="prefetch" href="/blog/assets/js/186.08c67f20.js"><link rel="prefetch" href="/blog/assets/js/187.7ca2d0c6.js"><link rel="prefetch" href="/blog/assets/js/188.cd167879.js"><link rel="prefetch" href="/blog/assets/js/189.e8e2eb21.js"><link rel="prefetch" href="/blog/assets/js/19.6b963460.js"><link rel="prefetch" href="/blog/assets/js/190.8b557318.js"><link rel="prefetch" href="/blog/assets/js/191.3e6fdc4f.js"><link rel="prefetch" href="/blog/assets/js/192.99d6e1d8.js"><link rel="prefetch" href="/blog/assets/js/193.4ced091e.js"><link rel="prefetch" href="/blog/assets/js/194.4b375e2e.js"><link rel="prefetch" href="/blog/assets/js/195.003e3d67.js"><link rel="prefetch" href="/blog/assets/js/196.7a3f55e5.js"><link rel="prefetch" href="/blog/assets/js/197.30d4c5b4.js"><link rel="prefetch" href="/blog/assets/js/198.628c2c1a.js"><link rel="prefetch" href="/blog/assets/js/199.89c4e586.js"><link rel="prefetch" href="/blog/assets/js/20.839dae41.js"><link rel="prefetch" href="/blog/assets/js/200.1d9f2ef9.js"><link rel="prefetch" href="/blog/assets/js/201.5ba078d9.js"><link rel="prefetch" href="/blog/assets/js/202.b49b23f4.js"><link rel="prefetch" href="/blog/assets/js/203.d88a03d9.js"><link rel="prefetch" href="/blog/assets/js/204.fb928277.js"><link rel="prefetch" href="/blog/assets/js/205.432c3d8d.js"><link rel="prefetch" href="/blog/assets/js/206.ed726599.js"><link rel="prefetch" href="/blog/assets/js/207.9ac30d7b.js"><link rel="prefetch" href="/blog/assets/js/208.c5e77adc.js"><link rel="prefetch" href="/blog/assets/js/209.d396aad5.js"><link rel="prefetch" href="/blog/assets/js/21.bd21bd29.js"><link rel="prefetch" href="/blog/assets/js/210.02e4ee2f.js"><link rel="prefetch" href="/blog/assets/js/211.0f8a9304.js"><link rel="prefetch" href="/blog/assets/js/212.9220dd3d.js"><link rel="prefetch" href="/blog/assets/js/213.89521ebd.js"><link rel="prefetch" href="/blog/assets/js/214.cd5ec468.js"><link rel="prefetch" href="/blog/assets/js/215.0084d772.js"><link rel="prefetch" href="/blog/assets/js/216.a72becb1.js"><link rel="prefetch" href="/blog/assets/js/217.71a7f3aa.js"><link rel="prefetch" href="/blog/assets/js/218.9793e19a.js"><link rel="prefetch" href="/blog/assets/js/219.c89175e9.js"><link rel="prefetch" href="/blog/assets/js/22.e6bcf65d.js"><link rel="prefetch" href="/blog/assets/js/220.d496e411.js"><link rel="prefetch" href="/blog/assets/js/221.07a9338a.js"><link rel="prefetch" href="/blog/assets/js/222.22bf261f.js"><link rel="prefetch" href="/blog/assets/js/223.b9c6a055.js"><link rel="prefetch" href="/blog/assets/js/224.364b4b11.js"><link rel="prefetch" href="/blog/assets/js/225.adec6660.js"><link rel="prefetch" href="/blog/assets/js/226.78192713.js"><link rel="prefetch" href="/blog/assets/js/227.0c1ec0e5.js"><link rel="prefetch" href="/blog/assets/js/228.a94ad835.js"><link rel="prefetch" href="/blog/assets/js/229.51820867.js"><link rel="prefetch" href="/blog/assets/js/23.7bb82fc7.js"><link rel="prefetch" href="/blog/assets/js/230.6eb1d9bf.js"><link rel="prefetch" href="/blog/assets/js/231.55d62061.js"><link rel="prefetch" href="/blog/assets/js/232.f938a3a8.js"><link rel="prefetch" href="/blog/assets/js/233.2c81f1b3.js"><link rel="prefetch" href="/blog/assets/js/234.4dbf5d59.js"><link rel="prefetch" href="/blog/assets/js/235.f78274d9.js"><link rel="prefetch" href="/blog/assets/js/236.a2b61bec.js"><link rel="prefetch" href="/blog/assets/js/237.6ae31c88.js"><link rel="prefetch" href="/blog/assets/js/238.2f5c56ae.js"><link rel="prefetch" href="/blog/assets/js/239.506a4e9f.js"><link rel="prefetch" href="/blog/assets/js/24.2c9c4ee6.js"><link rel="prefetch" href="/blog/assets/js/25.b4de33d1.js"><link rel="prefetch" href="/blog/assets/js/26.0bb98ba9.js"><link rel="prefetch" href="/blog/assets/js/27.df98327e.js"><link rel="prefetch" href="/blog/assets/js/28.31289bac.js"><link rel="prefetch" href="/blog/assets/js/29.45af5621.js"><link rel="prefetch" href="/blog/assets/js/30.d5c08e66.js"><link rel="prefetch" href="/blog/assets/js/31.78e43a68.js"><link rel="prefetch" href="/blog/assets/js/33.0efe6b11.js"><link rel="prefetch" href="/blog/assets/js/34.cb1866c1.js"><link rel="prefetch" href="/blog/assets/js/35.dca9b927.js"><link rel="prefetch" href="/blog/assets/js/36.fb4476c3.js"><link rel="prefetch" href="/blog/assets/js/37.09dfc1c5.js"><link rel="prefetch" href="/blog/assets/js/38.6624bf02.js"><link rel="prefetch" href="/blog/assets/js/39.df6c26ac.js"><link rel="prefetch" href="/blog/assets/js/4.44654b1a.js"><link rel="prefetch" href="/blog/assets/js/40.80101c19.js"><link rel="prefetch" href="/blog/assets/js/41.2b5e8c27.js"><link rel="prefetch" href="/blog/assets/js/42.c6ded3fe.js"><link rel="prefetch" href="/blog/assets/js/43.6d9424d6.js"><link rel="prefetch" href="/blog/assets/js/44.835e4b5c.js"><link rel="prefetch" href="/blog/assets/js/45.e7bb6f6a.js"><link rel="prefetch" href="/blog/assets/js/46.1b9286bf.js"><link rel="prefetch" href="/blog/assets/js/47.8d66ca97.js"><link rel="prefetch" href="/blog/assets/js/48.3c1102e1.js"><link rel="prefetch" href="/blog/assets/js/49.e17a3436.js"><link rel="prefetch" href="/blog/assets/js/5.88de390f.js"><link rel="prefetch" href="/blog/assets/js/50.6750f186.js"><link rel="prefetch" href="/blog/assets/js/51.9f93af9f.js"><link rel="prefetch" href="/blog/assets/js/52.f3ef3b5e.js"><link rel="prefetch" href="/blog/assets/js/53.a6bacd25.js"><link rel="prefetch" href="/blog/assets/js/54.dbb7c9ab.js"><link rel="prefetch" href="/blog/assets/js/55.2562d0c8.js"><link rel="prefetch" href="/blog/assets/js/56.14ea4931.js"><link rel="prefetch" href="/blog/assets/js/57.a2fad780.js"><link rel="prefetch" href="/blog/assets/js/58.8165b971.js"><link rel="prefetch" href="/blog/assets/js/59.556cab0d.js"><link rel="prefetch" href="/blog/assets/js/6.277038ca.js"><link rel="prefetch" href="/blog/assets/js/60.f048aa7c.js"><link rel="prefetch" href="/blog/assets/js/61.bdb307a8.js"><link rel="prefetch" href="/blog/assets/js/62.37a94f10.js"><link rel="prefetch" href="/blog/assets/js/63.74811780.js"><link rel="prefetch" href="/blog/assets/js/64.d3429318.js"><link rel="prefetch" href="/blog/assets/js/65.acfdd371.js"><link rel="prefetch" href="/blog/assets/js/66.cb805d9b.js"><link rel="prefetch" href="/blog/assets/js/67.39f85baa.js"><link rel="prefetch" href="/blog/assets/js/68.a1902a2a.js"><link rel="prefetch" href="/blog/assets/js/69.923fdd78.js"><link rel="prefetch" href="/blog/assets/js/7.474f8f8d.js"><link rel="prefetch" href="/blog/assets/js/70.1f3e978d.js"><link rel="prefetch" href="/blog/assets/js/71.13cd9358.js"><link rel="prefetch" href="/blog/assets/js/72.739b22a8.js"><link rel="prefetch" href="/blog/assets/js/73.95f69ae2.js"><link rel="prefetch" href="/blog/assets/js/74.b6624f6a.js"><link rel="prefetch" href="/blog/assets/js/75.31b56e72.js"><link rel="prefetch" href="/blog/assets/js/76.b9c0ce40.js"><link rel="prefetch" href="/blog/assets/js/77.46f6e413.js"><link rel="prefetch" href="/blog/assets/js/78.aebd00ee.js"><link rel="prefetch" href="/blog/assets/js/79.1b784d15.js"><link rel="prefetch" href="/blog/assets/js/8.9428e7ee.js"><link rel="prefetch" href="/blog/assets/js/80.1f550d53.js"><link rel="prefetch" href="/blog/assets/js/81.101cc131.js"><link rel="prefetch" href="/blog/assets/js/82.077c8298.js"><link rel="prefetch" href="/blog/assets/js/83.2e375d11.js"><link rel="prefetch" href="/blog/assets/js/84.38102a34.js"><link rel="prefetch" href="/blog/assets/js/85.24532d6a.js"><link rel="prefetch" href="/blog/assets/js/86.1dabbf00.js"><link rel="prefetch" href="/blog/assets/js/87.763da0f2.js"><link rel="prefetch" href="/blog/assets/js/88.ff6e5f7c.js"><link rel="prefetch" href="/blog/assets/js/89.187e5e16.js"><link rel="prefetch" href="/blog/assets/js/9.da143545.js"><link rel="prefetch" href="/blog/assets/js/90.3c8cff94.js"><link rel="prefetch" href="/blog/assets/js/91.a50bd44d.js"><link rel="prefetch" href="/blog/assets/js/92.5484868f.js"><link rel="prefetch" href="/blog/assets/js/93.c8ee75e3.js"><link rel="prefetch" href="/blog/assets/js/94.b18a3e9b.js"><link rel="prefetch" href="/blog/assets/js/95.cddef6ae.js"><link rel="prefetch" href="/blog/assets/js/96.80e5a938.js"><link rel="prefetch" href="/blog/assets/js/97.1f5e5197.js"><link rel="prefetch" href="/blog/assets/js/98.e3a275c8.js"><link rel="prefetch" href="/blog/assets/js/99.d33bf89e.js">
  13. <link rel="stylesheet" href="/blog/assets/css/0.styles.cf8cd190.css">
  14. </head>
  15. <body class="theme-mode-light">
  16. <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/blog/" class="home-link router-link-active"><img src="/blog/img/logo.png" alt="彪哥博客" class="logo"> <span class="site-name can-hide">彪哥博客</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/" class="nav-link">首页</a></div><div class="nav-item"><a href="http://fseller.com" target="_blank" rel="noopener noreferrer" class="nav-link external">
  17. 个人游戏网站
  18. <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端" class="dropdown-title"><a href="/blog/web/" class="link-title">前端</a> <span class="title" style="display:none;">前端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>前端文章</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/blog/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>学习笔记</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/blog/note/javascript/" class="nav-link">《JavaScript教程》</a></li><li class="dropdown-subitem"><a href="/blog/note/js/" class="nav-link">《JavaScript高级程序设计》</a></li><li class="dropdown-subitem"><a href="/blog/note/es6/" class="nav-link">《ES6 教程》</a></li><li class="dropdown-subitem"><a href="/blog/note/vue/" class="nav-link">《Vue》</a></li><li class="dropdown-subitem"><a href="/blog/note/react/" class="nav-link">《React》</a></li><li class="dropdown-subitem"><a href="/blog/note/typescript-axios/" class="nav-link">《TypeScript 从零实现 axios》</a></li><li class="dropdown-subitem"><a href="/blog/note/git/" class="nav-link">《Git》</a></li><li class="dropdown-subitem"><a href="/blog/pages/51afd6/" class="nav-link">TypeScript</a></li><li class="dropdown-subitem"><a href="/blog/pages/4643cd/" class="nav-link">JS设计模式总结</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="页面" class="dropdown-title"><a href="/blog/ui/" class="link-title">页面</a> <span class="title" style="display:none;">页面</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/8309a5b876fc95e3/" class="nav-link">HTML</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/0a83b083bdf257cb/" class="nav-link">CSS</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术" class="dropdown-title"><a href="/blog/technology/" class="link-title">技术</a> <span class="title" style="display:none;">技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/9a7ee40fc232253e/" class="nav-link">技术文档</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/4c778760be26d8b3/" class="nav-link">GitHub技巧</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/117708e0af7f0bd9/" class="nav-link">Nodejs</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/41f87d890d0a02af/" class="nav-link">博客搭建</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><a href="/blog/more/" class="link-title">更多</a> <span class="title" style="display:none;">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/f2a556/" class="nav-link">学习</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/aea6571b7a8bae86/" class="nav-link">面试</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/2d615df9a36a98ed/" class="nav-link">心情杂货</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/baaa02/" class="nav-link">实用技巧</a></li><li class="dropdown-item"><!----> <a href="/blog/friends/" class="nav-link">友情链接</a></li></ul></div></div><div class="nav-item"><a href="/blog/about/" class="nav-link">关于</a></div><div class="nav-item"><a href="/blog/pages/beb6c0bd8a66cea6/" class="nav-link">收藏</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/blog/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/blog/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/blog/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com/heBody/blog" target="_blank" rel="noopener noreferrer" class="repo-link">
  19. GitHub
  20. <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="https://fastly.jsdelivr.net/gh/xugaoyi/image_store/blog/20200103123203.jpg"> <div class="blogger-info"><h3>彪哥</h3> <span>爱好前端</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/blog/" class="nav-link">首页</a></div><div class="nav-item"><a href="http://fseller.com" target="_blank" rel="noopener noreferrer" class="nav-link external">
  21. 个人游戏网站
  22. <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端" class="dropdown-title"><a href="/blog/web/" class="link-title">前端</a> <span class="title" style="display:none;">前端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>前端文章</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/blog/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>学习笔记</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/blog/note/javascript/" class="nav-link">《JavaScript教程》</a></li><li class="dropdown-subitem"><a href="/blog/note/js/" class="nav-link">《JavaScript高级程序设计》</a></li><li class="dropdown-subitem"><a href="/blog/note/es6/" class="nav-link">《ES6 教程》</a></li><li class="dropdown-subitem"><a href="/blog/note/vue/" class="nav-link">《Vue》</a></li><li class="dropdown-subitem"><a href="/blog/note/react/" class="nav-link">《React》</a></li><li class="dropdown-subitem"><a href="/blog/note/typescript-axios/" class="nav-link">《TypeScript 从零实现 axios》</a></li><li class="dropdown-subitem"><a href="/blog/note/git/" class="nav-link">《Git》</a></li><li class="dropdown-subitem"><a href="/blog/pages/51afd6/" class="nav-link">TypeScript</a></li><li class="dropdown-subitem"><a href="/blog/pages/4643cd/" class="nav-link">JS设计模式总结</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="页面" class="dropdown-title"><a href="/blog/ui/" class="link-title">页面</a> <span class="title" style="display:none;">页面</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/8309a5b876fc95e3/" class="nav-link">HTML</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/0a83b083bdf257cb/" class="nav-link">CSS</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术" class="dropdown-title"><a href="/blog/technology/" class="link-title">技术</a> <span class="title" style="display:none;">技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/9a7ee40fc232253e/" class="nav-link">技术文档</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/4c778760be26d8b3/" class="nav-link">GitHub技巧</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/117708e0af7f0bd9/" class="nav-link">Nodejs</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/41f87d890d0a02af/" class="nav-link">博客搭建</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><a href="/blog/more/" class="link-title">更多</a> <span class="title" style="display:none;">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/f2a556/" class="nav-link">学习</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/aea6571b7a8bae86/" class="nav-link">面试</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/2d615df9a36a98ed/" class="nav-link">心情杂货</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/baaa02/" class="nav-link">实用技巧</a></li><li class="dropdown-item"><!----> <a href="/blog/friends/" class="nav-link">友情链接</a></li></ul></div></div><div class="nav-item"><a href="/blog/about/" class="nav-link">关于</a></div><div class="nav-item"><a href="/blog/pages/beb6c0bd8a66cea6/" class="nav-link">收藏</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/blog/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/blog/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/blog/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com/heBody/blog" target="_blank" rel="noopener noreferrer" class="repo-link">
  23. GitHub
  24. <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav> <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>JavaScript文章</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>学习笔记</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/blog/note/javascript/" class="sidebar-link">《JavaScript教程》笔记</a></li><li><a href="/blog/note/js/" class="sidebar-link">《JavaScript高级程序设计》笔记</a></li><li><a href="/blog/note/es6/" class="sidebar-link">《ES6 教程》笔记</a></li><li><a href="/blog/note/vue/" class="sidebar-link">《Vue》笔记</a></li><li><a href="/blog/note/react/" class="sidebar-link">《React》笔记</a></li><li><a href="/blog/note/typescript-axios/" class="sidebar-link">《TypeScript 从零实现 axios》</a></li><li><a href="/blog/note/git/" class="sidebar-link">《Git》学习笔记</a></li><li><a href="/blog/pages/51afd6/" class="sidebar-link">TypeScript笔记</a></li><li><a href="/blog/note/wx-miniprogram/" aria-current="page" class="active sidebar-link">小程序笔记</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/blog/note/wx-miniprogram/#基础课程部分" class="sidebar-link">基础课程部分</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#微信公众平台" class="sidebar-link">微信公众平台</a></li><li class="sidebar-sub-header level5"><a href="/blog/note/wx-miniprogram/#小程序文档api" class="sidebar-link">小程序文档API</a></li><li class="sidebar-sub-header level5"><a href="/blog/note/wx-miniprogram/#微信开放社区" class="sidebar-link">微信开放社区</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#目录说明" class="sidebar-link">目录说明</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#文件说明" class="sidebar-link">文件说明</a></li><li class="sidebar-sub-header level5"><a href="/blog/note/wx-miniprogram/#阻止事件冒泡" class="sidebar-link">阻止事件冒泡</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#第三方库" class="sidebar-link">第三方库</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#云开发" class="sidebar-link">云开发</a></li><li class="sidebar-sub-header level5"><a href="/blog/note/wx-miniprogram/#云开发三大基础能力" class="sidebar-link">云开发三大基础能力</a></li><li class="sidebar-sub-header level5"><a href="/blog/note/wx-miniprogram/#云函数" class="sidebar-link">云函数</a></li><li class="sidebar-sub-header level5"><a href="/blog/note/wx-miniprogram/#云数据库" class="sidebar-link">云数据库</a></li><li class="sidebar-sub-header level5"><a href="/blog/note/wx-miniprogram/#云存储" class="sidebar-link">云存储</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#云数据库能力" class="sidebar-link">云数据库能力</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#数据类型" class="sidebar-link">数据类型</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#操作云数据库" class="sidebar-link">操作云数据库</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#云数据库权限管理" class="sidebar-link">云数据库权限管理</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#操作云数据库-2" class="sidebar-link">操作云数据库</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/note/wx-miniprogram/#实战课程部分" class="sidebar-link">实战课程部分</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#serverless-无服务" class="sidebar-link">serverless(无服务)</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#云开发优势" class="sidebar-link">云开发优势</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#云开发提供能力" class="sidebar-link">云开发提供能力</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#appid" class="sidebar-link">appID</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#云开发项目默认目录结构" class="sidebar-link">云开发项目默认目录结构</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#云开发环境" class="sidebar-link">云开发环境</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#开发前的准备" class="sidebar-link">开发前的准备</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#project-config-json-文件说明" class="sidebar-link">project.config.json 文件说明</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#app-json" class="sidebar-link">app.json</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#代码规范" class="sidebar-link">代码规范</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#《音乐》页面开发" class="sidebar-link">《音乐》页面开发</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#自定义组件" class="sidebar-link">自定义组件</a></li><li class="sidebar-sub-header level5"><a href="/blog/note/wx-miniprogram/#引入组件" class="sidebar-link">引入组件</a></li><li class="sidebar-sub-header level5"><a href="/blog/note/wx-miniprogram/#组件传值" class="sidebar-link">组件传值</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#wx-key-的使用" class="sidebar-link">wx:key 的使用</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#async-await-语法" class="sidebar-link">async/await 语法</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#云函数的使用" class="sidebar-link">云函数的使用</a></li><li class="sidebar-sub-header level5"><a href="/blog/note/wx-miniprogram/#数据库操作" class="sidebar-link">数据库操作</a></li><li class="sidebar-sub-header level5"><a href="/blog/note/wx-miniprogram/#查询数据库" class="sidebar-link">查询数据库</a></li><li class="sidebar-sub-header level5"><a href="/blog/note/wx-miniprogram/#云函数调试" class="sidebar-link">云函数调试</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#定时触发云函数" class="sidebar-link">定时触发云函数</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#配置云函数超时时间" class="sidebar-link">配置云函数超时时间</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#上拉加载与下拉刷新" class="sidebar-link">上拉加载与下拉刷新</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#云函数路由优化tcb-router" class="sidebar-link">云函数路由优化tcb-router</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#本地存储-缓存" class="sidebar-link">本地存储(缓存)</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#api设置title" class="sidebar-link">api设置title</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#背景播放音" class="sidebar-link">背景播放音</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#createselectorquery查询节点信息" class="sidebar-link">createSelectorQuery查询节点信息</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#组件内的方法" class="sidebar-link">组件内的方法</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#组件生命周期" class="sidebar-link">组件生命周期</a></li><li class="sidebar-sub-header level5"><a href="/blog/note/wx-miniprogram/#组件所在页面的生命周期" class="sidebar-link">组件所在页面的生命周期</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#组件对数据的监听" class="sidebar-link">组件对数据的监听</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#子组件自定义事件传递给父组件" class="sidebar-link">子组件自定义事件传递给父组件</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#父组件自定义事件传递给子组件" class="sidebar-link">父组件自定义事件传递给子组件</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#兄弟组件间传递事件和传值" class="sidebar-link">兄弟组件间传递事件和传值</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#获取手机信息" class="sidebar-link">获取手机信息</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#滚动组件" class="sidebar-link">滚动组件</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#全局属性、方法-类似vuex" class="sidebar-link">全局属性、方法(类似vuex)</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#消息提示框" class="sidebar-link">消息提示框</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/note/wx-miniprogram/#《发现》页面" class="sidebar-link">《发现》页面</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#调用组件外部的样式" class="sidebar-link">调用组件外部的样式</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#组件插槽slot" class="sidebar-link">组件插槽slot</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#判断用户授权" class="sidebar-link">判断用户授权</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#button的开发能力-获取用户信息-1" class="sidebar-link">button的开发能力(获取用户信息)1</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#原生组件" class="sidebar-link">原生组件</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#选择上传图片" class="sidebar-link">选择上传图片</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#图片裁剪" class="sidebar-link">图片裁剪</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#获取标签自定义属性data-删除图片的实现" class="sidebar-link">获取标签自定义属性data-* (删除图片的实现)</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#全屏预览图片-点击图片放大预览" class="sidebar-link">全屏预览图片(点击图片放大预览)</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#文件上传云存储-发布博客例子" class="sidebar-link">文件上传云存储(发布博客例子)</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#js模块化-时间格式化" class="sidebar-link">js模块化 (时间格式化)</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#阻止事件冒泡-2" class="sidebar-link">阻止事件冒泡</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#返回上一个页面并执行方法" class="sidebar-link">返回上一个页面并执行方法</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#图片懒加载" class="sidebar-link">图片懒加载</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#模糊查询" class="sidebar-link">模糊查询</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#提升模糊查询的效率-添加索引-对数据量大的查询效果明显" class="sidebar-link">提升模糊查询的效率 (添加索引,对数据量大的查询效果明显)</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#小程序端调用云数据库" class="sidebar-link">小程序端调用云数据库</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#云数据库权限管理-2" class="sidebar-link">云数据库权限管理</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#数据库中1对n关系的三种设计方式" class="sidebar-link">数据库中1对N关系的三种设计方式</a></li><li class="sidebar-sub-header level5"><a href="/blog/note/wx-miniprogram/#第一种-n的数量较少-几十个以内" class="sidebar-link">第一种:N的数量较少 几十个以内</a></li><li class="sidebar-sub-header level5"><a href="/blog/note/wx-miniprogram/#第二种-n的数量较多-几十到几百个" class="sidebar-link">第二种:N的数量较多 几十到几百个</a></li><li class="sidebar-sub-header level5"><a href="/blog/note/wx-miniprogram/#第三种-n的数量巨大-几百成千上万个" class="sidebar-link">第三种:N的数量巨大 几百成千上万个</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#云调用" class="sidebar-link">云调用</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#模板消息推送" class="sidebar-link">模板消息推送</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#云函数多集合查询数据库" class="sidebar-link">云函数多集合查询数据库</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#分享功能" class="sidebar-link">分享功能</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#不同场景获取用户信息的方式" class="sidebar-link">不同场景获取用户信息的方式</a></li><li class="sidebar-sub-header level5"><a href="/blog/note/wx-miniprogram/#场景一-只想在界面上显示自己的昵称和头像" class="sidebar-link">场景一:只想在界面上显示自己的昵称和头像</a></li><li class="sidebar-sub-header level5"><a href="/blog/note/wx-miniprogram/#场景二-在js中获取用户信息" class="sidebar-link">场景二:在JS中获取用户信息</a></li><li class="sidebar-sub-header level5"><a href="/blog/note/wx-miniprogram/#场景三-获取openid" class="sidebar-link">场景三:获取openId</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/note/wx-miniprogram/#《我的》页面" class="sidebar-link">《我的》页面</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#导航页面链接跳转" class="sidebar-link">导航页面链接跳转</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#背景图片" class="sidebar-link">背景图片</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#每个页面都有的page标签" class="sidebar-link">每个页面都有的page标签</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#播放历史与本地存储" class="sidebar-link">播放历史与本地存储</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#我的发现" class="sidebar-link">我的发现</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#小程序码" class="sidebar-link">小程序码</a></li><li class="sidebar-sub-header level5"><a href="/blog/note/wx-miniprogram/#判断是从扫码小程序码进入-以及参数获取" class="sidebar-link">判断是从扫码小程序码进入,以及参数获取</a></li><li class="sidebar-sub-header level3"><a href="/blog/note/wx-miniprogram/#版本更新检测" class="sidebar-link">版本更新检测</a></li><li class="sidebar-sub-header level3"><a href="/blog/note/wx-miniprogram/#性能优化" class="sidebar-link">性能优化</a></li><li class="sidebar-sub-header level3"><a href="/blog/note/wx-miniprogram/#场景值scene的作用与应用场景" class="sidebar-link">场景值scene的作用与应用场景</a></li><li class="sidebar-sub-header level3"><a href="/blog/note/wx-miniprogram/#小程序的-seo-页面收录sitemap" class="sidebar-link">小程序的&quot;SEO&quot;---页面收录sitemap</a></li><li class="sidebar-sub-header level3"><a href="/blog/note/wx-miniprogram/#小程序上线审核流程" class="sidebar-link">小程序上线审核流程</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/note/wx-miniprogram/#后台管理系统" class="sidebar-link">后台管理系统</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#架构示意图" class="sidebar-link">架构示意图</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#vue-admin-template构建管理系统前端" class="sidebar-link">vue-admin-template构建管理系统前端</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#koa2构建管理系统后端" class="sidebar-link">Koa2构建管理系统后端</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#接口调用凭证-access-token-的缓存与更新" class="sidebar-link">接口调用凭证 access_token 的缓存与更新</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#后端代码通过http-api-触发云函数获取数据" class="sidebar-link">后端代码通过HTTP API 触发云函数获取数据</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#产生跨域和后端解决跨域问题" class="sidebar-link">产生跨域和后端解决跨域问题</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#云数据库的增删改查接口" class="sidebar-link">云数据库的增删改查接口</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#后端获取前端post请求传来的数据" class="sidebar-link">后端获取前端post请求传来的数据</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#后端获取云存储图片" class="sidebar-link">后端获取云存储图片</a></li><li class="sidebar-sub-header level4"><a href="/blog/note/wx-miniprogram/#后端上传图片到云存储" class="sidebar-link">后端上传图片到云存储</a></li></ul></li></ul></li><li><a href="/blog/pages/4643cd/" class="sidebar-link">JS设计模式总结笔记</a></li></ul></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06970110><div class="articleInfo" data-v-06970110><ul class="breadcrumbs" data-v-06970110><li data-v-06970110><a href="/blog/" title="首页" class="iconfont icon-home router-link-active" data-v-06970110></a></li> <li data-v-06970110><a href="/blog/web/#前端" data-v-06970110>前端</a></li><li data-v-06970110><a href="/blog/web/#学习笔记" data-v-06970110>学习笔记</a></li></ul> <div class="info" data-v-06970110><div title="作者" class="author iconfont icon-touxiang" data-v-06970110><a href="https://github.com/heBody" target="_blank" title="作者" class="beLink" data-v-06970110>heBody</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06970110><a href="javascript:;" data-v-06970110>2019-12-25</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB4AAAAeCAYAAAA7MK6iAAAAAXNSR0IArs4c6QAABGpJREFUSA3tVVtoXFUU3fvOI53UlmCaKIFmwEhsE7QK0ipFEdHEKpXaZGrp15SINsXUWvBDpBgQRKi0+KKoFeJHfZA+ED9KKoIU2gYD9UejTW4rVIzm0VSTziPzuNu1z507dibTTjL4U/DAzLn3nL3X2o91ziX6f9wMFdh6Jvbm9nNSV0msViVO6tN1Rm7NMu2OpeJ9lWBUTDxrJbYTS0hInuwciu9eLHlFxCLCZEk3MegsJmZ5K/JD6t7FkFdEvGUo1g7qJoG3MHImqRIn8/nzY1K9UPKKiJmtnUqHVE3Gbuay6vJE/N2FEmuxFjW2nUuE0yQXRRxLiTUAzs36zhZvOXJPdX850EVnnLZkB8prodQoM5JGj7Xk2mvC7JB8tG04Ef5PiXtG0UtxupRQSfTnBoCy554x18yJHI6I+G5Eru4LHmPJZEQsrvPUbMiA8G/WgMK7w7I+ez7++o2ANfbrjvaOl1tFMs+htG3IrZH9/hDX1Pr8Tc0UvH8tcX29KzAgIGcEkINyW5BF9x891hw6VYqgJHEk0huccS7vh3C6gTiODL+26huuBtbct8eZnqLML8PkxGYpuPZBqtqwkSjgc4mB5gbgig5i+y0UDK35LMxXisn9xQtK+nd26gTIHsHe/oblK/b29fUmN/8Y+9jAQrnBp56m1LcDlDp9irKTExSKduXJVWSqdBMA08pEJnEIOB3FPPMybu/oeV8zFeYN3xx576Q6RH+VmplE4ncQV5v+5rzSoyOU7PuEAg8g803PwBJ0CExno/jcMbN8tONYeOmHiuUNryvm3fRUy4tMPVLdAGkUhNWuggGrJcXPv+ouCjz0MKUHz1J2/E8IC9nqTabcxgaBYM0hPhD5Y65FsbxRQKxCQrDjDctW7PUM3HuZunFyifSAqEfuzCp48Il24luWUWZoyJCaPR82jE0+kFA643wRFVni4RYSq3ohJO2pZ7B5dO4xkDWbEpossJPLSrPjYID8rS2UHTlvyNxqIGsg674XJJ7vnh5L7PNwC4hh2sjCI96mzszOTpxLF0T7l88Yz7lAuK6OnL8gXLOnTvpzSb22YG8W7us3jSebFHeeqnXRG1vt+MoUM84LQIBmMsCTAcOauTh0T0l0neQK7m2bLMt2mGxU3HYssS0J2cdv5wljlPsrIuZLAG/2DOZIXgCYT8uMGZN+e2kSirfxZOPCsC0f24nTZzspnVn9VePS1Z5vubmAGGXG8ZFno9Hel0yfA5ZPhF7Dh972BQJ2qCpgH67lmWtBYbvk6sz02wjky2vXyz0XErP/kFB619js1BtwfOV4OPRqOQBjy3Qbk18vigUPPSD5ceHnwck7W9bhAqZdd7SuG7w4/P2F/GaJh8c7e9qgow+Q7cGBo+98WsLkuktFqiZabtXuQTu/Y5ETbR0v7tNSFnvrmu6pjdoan2KjMu8q/Hmj1EfCO2ZGfEIbIXKUlw8qaX9/b2oeSJmFksSeT/Fn0V3nSypChh4Gjh74ybO9aeZ/AN2dwciu2/MhAAAAAElFTkSuQmCC">小程序笔记<!----></h1> <div class="theme-vdoing-content content__default"><h1 id="小程序笔记"><a href="#小程序笔记" class="header-anchor">#</a> 小程序笔记</h1> <h2 id="基础课程部分"><a href="#基础课程部分" class="header-anchor">#</a> 基础课程部分</h2> <h4 id="微信公众平台"><a href="#微信公众平台" class="header-anchor">#</a> 微信公众平台</h4> <p><a href="https://mp.weixin.qq.com/" target="_blank" rel="noopener noreferrer">https://mp.weixin.qq.com/<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <p>注册时可选择类型:订阅号、服务号、小程序、企业微信</p> <p><strong>每个邮箱仅能注册一个小程序。</strong></p> <p>个人类型小程序:无法使用微信支付、无法使用卡包功能</p> <h5 id="小程序文档api"><a href="#小程序文档api" class="header-anchor">#</a> 小程序文档API</h5> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/framework/quickstart/" target="_blank" rel="noopener noreferrer">小程序开发文档<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h5 id="微信开放社区"><a href="#微信开放社区" class="header-anchor">#</a> 微信开放社区</h5> <p><a href="https://developers.weixin.qq.com/community/develop/question" target="_blank" rel="noopener noreferrer">微信开发社区<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h4 id="目录说明"><a href="#目录说明" class="header-anchor">#</a> 目录说明</h4> <p><strong>默认目录</strong></p> <p>pages-----------------------页面相关</p> <p>​ index ----------------- 首页文件夹</p> <p>​ index.js ------------首页js</p> <p>​ index.json---------首页配置</p> <p>​ index.wxml-------首页html</p> <p>​ index.wxss--------首页css</p> <p>utils------------------------工具相关</p> <p>app.js ----------------------项目总js</p> <p>app.json-------------------全局配置( 页面路由以及头部、底部导航的配置等)</p> <p>app.wxss -----------------项目总样式css</p> <p>project.config.json ----项目配置</p> <p><strong>代码构成</strong></p> <p>.json :配置文件,以json格式存储配置</p> <p>​ 项目中有三种配置:项目配置(project.config.json)、全局配置(app.json)、页面配置(index.json)</p> <p>.wxml: 相当于html文件</p> <p>.wxss: 相当于css</p> <p>.js : 就是js</p> <h4 id="文件说明"><a href="#文件说明" class="header-anchor">#</a> 文件说明</h4> <p><strong>project.config.json项目配置 部分代码说明</strong></p> <p>setting:{</p> <p>urlCheck 是否检测安全的域名</p> <p>es6 是否把es6转es5</p> <p>postcss 是否把css样式自动补全</p> <p>minified 是否压缩</p> <p>}</p> <p><strong>app.json 全局配置</strong></p> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/reference/configuration/app.html" target="_blank" rel="noopener noreferrer">全局配置API<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <p><strong>wxml 相关介绍</strong></p> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/framework/view/wxml/" target="_blank" rel="noopener noreferrer">wxmlAPI<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>view</span><span class="token punctuation">&gt;</span></span>{{motto}}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>view</span><span class="token punctuation">&gt;</span></span>
  25. 循环渲染
  26. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>view</span> <span class="token attr-name"><span class="token namespace">wx:</span>for</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{{list}}<span class="token punctuation">&quot;</span></span> <span class="token attr-name"><span class="token namespace">wx:</span>key</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{{index}}<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
  27. {{index}} {{item}}
  28. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>view</span><span class="token punctuation">&gt;</span></span>
  29. 改变for循环item和index的名称
  30. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>block</span> <span class="token attr-name"><span class="token namespace">wx:</span>for</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{{list}}<span class="token punctuation">&quot;</span></span> <span class="token attr-name"><span class="token namespace">wx:</span>for-item</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>data<span class="token punctuation">&quot;</span></span> <span class="token attr-name"><span class="token namespace">wx:</span>for-index</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>inx<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
  31. {{inx}} {{data}}
  32. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>block</span><span class="token punctuation">&gt;</span></span>
  33. 条件渲染 (类似vue的v-if、v-else)
  34. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>view</span> <span class="token attr-name"><span class="token namespace">wx:</span>if</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{{isLogin}}<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>已登录<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>view</span><span class="token punctuation">&gt;</span></span>
  35. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>view</span> <span class="token attr-name"><span class="token namespace">wx:</span>else</span><span class="token punctuation">&gt;</span></span>请登录<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>view</span><span class="token punctuation">&gt;</span></span>
  36. 条件显示(类似vue的v-show)
  37. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>view</span> <span class="token attr-name">hidden</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{{isLogin}}<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>显示内容<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>view</span><span class="token punctuation">&gt;</span></span>
  38. 绑定点击事件
  39. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">bindtap</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span>“tapName”</span><span class="token punctuation">&gt;</span></span>按钮<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span>
  40. Page({
  41. tapName: function(event) {
  42. console.log(event)
  43. }
  44. })
  45. ...
  46. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br></div></div><p><strong>wxss 相关介绍</strong></p> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/framework/view/wxss.html" target="_blank" rel="noopener noreferrer">wxssAPI<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <p>尺寸单位:rpx,根据屏幕宽度自适应。</p> <p>引入外部wxss:@import ’...‘</p> <p><strong>js相关介绍</strong></p> <p>WXS(WeiXin Script)是小程序的一套脚本语言</p> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/framework/view/wxs/" target="_blank" rel="noopener noreferrer">wxsAPI<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <p>绑定点击事件</p> <div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">bindtap</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span>“onTapHandler”</span><span class="token punctuation">&gt;</span></span>点我+1<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span>
  47. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>view</span><span class="token punctuation">&gt;</span></span>{{count}}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>view</span><span class="token punctuation">&gt;</span></span>
  48. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">Page</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  49. <span class="token literal-property property">data</span><span class="token operator">:</span> <span class="token punctuation">{</span>
  50. <span class="token literal-property property">count</span><span class="token operator">:</span> <span class="token number">0</span>
  51. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  52. <span class="token function-variable function">onTapHandler</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  53. <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setData</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  54. <span class="token literal-property property">count</span><span class="token operator">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>data<span class="token punctuation">.</span>count<span class="token operator">++</span>
  55. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  56. <span class="token punctuation">}</span>
  57. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  58. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h5 id="阻止事件冒泡"><a href="#阻止事件冒泡" class="header-anchor">#</a> 阻止事件冒泡</h5> <p>把绑定方式 bindtap 换成 catchtap 即可。</p> <h4 id="第三方库"><a href="#第三方库" class="header-anchor">#</a> 第三方库</h4> <p><strong>WeUI</strong></p> <p>weUI是一套同微信原生视觉体验一致的基础样式库</p> <p><strong>iView Weapp</strong></p> <p>一套高质量的微信小程序UI组件库</p> <p><strong>Vant Weapp</strong></p> <p>轻量、可靠的小程序UI组件库</p> <h4 id="云开发"><a href="#云开发" class="header-anchor">#</a> 云开发</h4> <p><strong>小程序传统开发模式</strong></p> <p>客户端 -----&gt; 服务端(后端代码、数据库)------&gt; 运维(DB维护、文件存储、内容加速、网络防护、容器服务、负载均衡、安全加固等...)</p> <p><strong>小程序云开发模式</strong></p> <p>客户端 -----&gt; 云开发(云函数、云数据库、云存储)</p> <p><strong>传统开发 VS 云开发</strong></p> <p>开发效率低 Serverless(无服务)</p> <p>运维成本高 开发者更关注业务逻辑</p> <p><strong>无服务(Serverless)开发是未来的发展趋势</strong></p> <h5 id="云开发三大基础能力"><a href="#云开发三大基础能力" class="header-anchor">#</a> <strong>云开发三大基础能力</strong></h5> <h5 id="云函数"><a href="#云函数" class="header-anchor">#</a> 云函数</h5> <p>(相当于传统开发中的后台接口)</p> <p>获取appid、获取openid、生成分享图、调用腾讯云SDK ...</p> <h5 id="云数据库"><a href="#云数据库" class="header-anchor">#</a> 云数据库</h5> <p>数据的增、删、改、查 ...</p> <h5 id="云存储"><a href="#云存储" class="header-anchor">#</a> 云存储</h5> <p>管理文件、上传文件、下载文件、分享文件 ...</p> <p><strong>每个小程序账号可免费创建两个环境,建议:开发环境、生成环境</strong></p> <h4 id="云数据库能力"><a href="#云数据库能力" class="header-anchor">#</a> 云数据库能力</h4> <p>云开发提供了一个json数据库,提供2GB免费存储空间。</p> <h4 id="数据类型"><a href="#数据类型" class="header-anchor">#</a> 数据类型</h4> <p>String 字符串</p> <p>Number 数字</p> <p>Object 对象</p> <p>Array 数组</p> <p>Boolean 布尔值</p> <p>GeoPoint 地理位置点</p> <p>Date 时间 (精确到毫秒ms,客户端时间)</p> <p>Null 空</p> <h4 id="操作云数据库"><a href="#操作云数据库" class="header-anchor">#</a> 操作云数据库</h4> <p>小程序控制(读写数据库受权限限制)</p> <p>云函数控制(拥有所有读写数据库的权限)</p> <p>控制台控制(拥有所有读写数据库的权限)</p> <h4 id="云数据库权限管理"><a href="#云数据库权限管理" class="header-anchor">#</a> 云数据库权限管理</h4> <p>仅创建者可写,所有人可读 (适合于文章)</p> <p>仅创建者可读写 (适用于私密内容)</p> <p>仅管理端可写,所有人可读(适用于商品信息)</p> <p>仅管理端可读写(适用于后台敏感数据)</p> <h4 id="操作云数据库-2"><a href="#操作云数据库-2" class="header-anchor">#</a> 操作云数据库</h4> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//初始化数据库</span>
  59. <span class="token keyword">const</span> db <span class="token operator">=</span> wx<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span><span class="token function">database</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 小程序端初始化数据库,如果在云函数端不需要加wx</span>
  60. <span class="token comment">//切换环境(开发环境/生产环境)</span>
  61. <span class="token keyword">const</span> testDB <span class="token operator">=</span> wx<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span><span class="token function">database</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  62. <span class="token literal-property property">env</span><span class="token operator">:</span> <span class="token string">'test'</span>
  63. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  64. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h2 id="实战课程部分"><a href="#实战课程部分" class="header-anchor">#</a> 实战课程部分</h2> <h4 id="serverless-无服务"><a href="#serverless-无服务" class="header-anchor">#</a> serverless(无服务)</h4> <p>概念:函数即服务,当需要后端服务的时候,不需要关心后端的IP地址、域名,只需要像调用普通函数一样既可以实现调用。</p> <h4 id="云开发优势"><a href="#云开发优势" class="header-anchor">#</a> 云开发优势</h4> <p>快速上线、专注核心业务、独立开发一个完整的微信小程序、不需要学习新的语言,只需要会javascript、无需运维, 节约成本、数据安全、</p> <h4 id="云开发提供能力"><a href="#云开发提供能力" class="header-anchor">#</a> 云开发提供能力</h4> <p>云函数:在云端运行的代码,微信私有协议天然鉴权 (理解:相当于后端部分)</p> <p>云数据库:一个既可以在小程序端操作又可以在云函数中操作的JSON数据库</p> <p>云存储:在云端存储文件,可以在云端控制台可视化管理</p> <p>云调用:基于云函数免鉴权使用小程序开放接口的能力(比如说给用户推送消息等)</p> <p>HTTP API:使用HTTP API开发者可在已有服务器上访问云资源,实现与云开发的互通(作用:对原有传统模式下开发的小程序,可以与云开发进行互通)</p> <h4 id="appid"><a href="#appid" class="header-anchor">#</a> appID</h4> <p>每个小程序唯一的id</p> <h4 id="云开发项目默认目录结构"><a href="#云开发项目默认目录结构" class="header-anchor">#</a> 云开发项目默认目录结构</h4> <p>cloudfunctions ----------------------------云函数</p> <p>​ callback ---------------------------------- 回调函数</p> <p>​ config.json ---------------------------</p> <p>​ index.js --------------------------------</p> <p>​ package.json ------------------------</p> <p>​ echo ----------------------------------------</p> <p>​ login ----------------------------------------</p> <p>​ openapi -----------------------------------</p> <p>miniprogram ------------------------------- 小程序</p> <p>​ images ------------------------------------- 图片</p> <p>​ pages --------------------------------------- 页面</p> <p>​ style ----------------------------------------- 样式</p> <p>​ app.js --------------------------------------- 项目js</p> <p>​ app.json ----------------------------------- 全局配置</p> <p>​ app.wxss ---------------------------------- 项目样式</p> <p>​ sitemap.json ----------------------------- (小程序SEO相关)</p> <p>project.config.json ----------------------- 项目配置</p> <h4 id="云开发环境"><a href="#云开发环境" class="header-anchor">#</a> 云开发环境</h4> <p>云开发可创建两个环境,建议一个为开发环境,一个为生产环境</p> <h4 id="开发前的准备"><a href="#开发前的准备" class="header-anchor">#</a> 开发前的准备</h4> <p>开发工具 &gt; 右上角详情 &gt; 本地设置 &gt; 调试基础库 设置为最新版本</p> <p>app.js &gt; wx.cloud.init &gt; env 设置环境ID</p> <h4 id="project-config-json-文件说明"><a href="#project-config-json-文件说明" class="header-anchor">#</a> project.config.json 文件说明</h4> <p>miniprogramRoot 小程序前端代码目录</p> <p>cloudfunctionRoot 云函数代码目录</p> <h4 id="app-json"><a href="#app-json" class="header-anchor">#</a> app.json</h4> <p>pages 设置页面 ,设置后会自动在pages目录下生成相应的目录和文件</p> <p>设置底部导航按钮:</p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token property">&quot;tabBar&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
  65. <span class="token property">&quot;color&quot;</span><span class="token operator">:</span> <span class="token string">&quot;#474747&quot;</span><span class="token punctuation">,</span> <span class="token comment">// 文字颜色</span>
  66. <span class="token property">&quot;selectedColor&quot;</span><span class="token operator">:</span> <span class="token string">&quot;#d43c43&quot;</span><span class="token punctuation">,</span> <span class="token comment">// 文字选中颜色</span>
  67. <span class="token property">&quot;list&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span> <span class="token comment">// 按钮列表,2-5项</span>
  68. <span class="token property">&quot;pagePath&quot;</span><span class="token operator">:</span> <span class="token string">&quot;pages/playlist/playlist&quot;</span><span class="token punctuation">,</span> <span class="token comment">// 按钮对应页面</span>
  69. <span class="token property">&quot;text&quot;</span><span class="token operator">:</span> <span class="token string">&quot;音乐&quot;</span><span class="token punctuation">,</span> <span class="token comment">// 文字</span>
  70. <span class="token property">&quot;iconPath&quot;</span><span class="token operator">:</span> <span class="token string">&quot;images/music.png&quot;</span><span class="token punctuation">,</span> <span class="token comment">// 图标路径</span>
  71. <span class="token property">&quot;selectedIconPath&quot;</span><span class="token operator">:</span> <span class="token string">&quot;images/music-actived.png&quot;</span> <span class="token comment">// 选中图标的路径</span>
  72. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  73. <span class="token punctuation">{</span>
  74. <span class="token property">&quot;pagePath&quot;</span><span class="token operator">:</span> <span class="token string">&quot;pages/blog/blog&quot;</span><span class="token punctuation">,</span>
  75. <span class="token property">&quot;text&quot;</span><span class="token operator">:</span> <span class="token string">&quot;发现&quot;</span><span class="token punctuation">,</span>
  76. <span class="token property">&quot;iconPath&quot;</span><span class="token operator">:</span> <span class="token string">&quot;images/blog.png&quot;</span><span class="token punctuation">,</span>
  77. <span class="token property">&quot;selectedIconPath&quot;</span><span class="token operator">:</span> <span class="token string">&quot;images/blog-actived.png&quot;</span>
  78. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  79. <span class="token punctuation">{</span>
  80. <span class="token property">&quot;pagePath&quot;</span><span class="token operator">:</span> <span class="token string">&quot;pages/profile/profile&quot;</span><span class="token punctuation">,</span>
  81. <span class="token property">&quot;text&quot;</span><span class="token operator">:</span> <span class="token string">&quot;我的&quot;</span><span class="token punctuation">,</span>
  82. <span class="token property">&quot;iconPath&quot;</span><span class="token operator">:</span> <span class="token string">&quot;images/profile.png&quot;</span><span class="token punctuation">,</span>
  83. <span class="token property">&quot;selectedIconPath&quot;</span><span class="token operator">:</span> <span class="token string">&quot;images/profile-actived.png&quot;</span>
  84. <span class="token punctuation">}</span><span class="token punctuation">]</span>
  85. <span class="token punctuation">}</span>
  86. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><blockquote><p>图标来自于 <a href="https://www.iconfont.cn" target="_blank" rel="noopener noreferrer">https://www.iconfont.cn<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <p>阿里巴巴图标库,包含矢量图标、字体图标、字体等</p></blockquote> <h4 id="代码规范"><a href="#代码规范" class="header-anchor">#</a> 代码规范</h4> <p>很多公司借鉴的代码规范:<a href="https://github.com/airbnb/javascript" target="_blank" rel="noopener noreferrer">https://github.com/airbnb/javascript<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h4 id="《音乐》页面开发"><a href="#《音乐》页面开发" class="header-anchor">#</a> 《音乐》页面开发</h4> <div class="language-html line-numbers-mode"><pre class="language-html"><code> <span class="token comment">&lt;!-- 轮播图组件 参数:indicator-dots 小圆点,autoplay 自动播放, interval 间隔时间,duration 动画时长 --&gt;</span>
  87. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>swiper</span> <span class="token attr-name">indicator-dots</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>true<span class="token punctuation">&quot;</span></span> <span class="token attr-name">circular</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>true<span class="token punctuation">&quot;</span></span> <span class="token attr-name">interval</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>3000<span class="token punctuation">&quot;</span></span> <span class="token attr-name">duration</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>500<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
  88. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>block</span> <span class="token attr-name"><span class="token namespace">wx:</span>for</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{{swiperImgUrls}}<span class="token punctuation">&quot;</span></span> <span class="token attr-name"><span class="token namespace">wx:</span>key</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{{index}}<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span> <span class="token comment">&lt;!-- 空节点 --&gt;</span>
  89. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>swiper-item</span><span class="token punctuation">&gt;</span></span>
  90. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>image</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{{item.url}}<span class="token punctuation">&quot;</span></span> <span class="token attr-name">mode</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>widthFix<span class="token punctuation">&quot;</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>img<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>image</span><span class="token punctuation">&gt;</span></span>
  91. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>swiper-item</span><span class="token punctuation">&gt;</span></span>
  92. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>block</span><span class="token punctuation">&gt;</span></span>
  93. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>swiper</span><span class="token punctuation">&gt;</span></span>
  94. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h4 id="自定义组件"><a href="#自定义组件" class="header-anchor">#</a> 自定义组件</h4> <p><strong>创建组件</strong></p> <p>创建目录 components &gt; 组件目录名称 &gt; 右键 <code>新建Component</code></p> <h5 id="引入组件"><a href="#引入组件" class="header-anchor">#</a> <strong>引入组件</strong></h5> <p>在page的json文件中:</p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
  95. <span class="token property">&quot;usingComponents&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
  96. <span class="token property">&quot;x-playlist&quot;</span><span class="token operator">:</span><span class="token string">&quot;/components/playlist/playlist&quot;</span>
  97. <span class="token punctuation">}</span>
  98. <span class="token punctuation">}</span>
  99. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>在page的wxml中:</p> <div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>x-playlist</span><span class="token punctuation">&gt;</span></span> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>x-playlist</span><span class="token punctuation">&gt;</span></span>
  100. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>页面引入组件以及组件内部在引用子组件的方法是一样的,同样需要设置json文件。</strong></p> <h5 id="组件传值"><a href="#组件传值" class="header-anchor">#</a> <strong>组件传值</strong></h5> <p>父组件中:在引入组件的时候自定义属性名称,并把数据传入子组件</p> <div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token comment">&lt;!-- 参数:playlist 自定义名称,传入组件的数据 --&gt;</span>
  101. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>x-playlist</span> <span class="token attr-name">playlist</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{{传入的数据}}<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>x-playlist</span><span class="token punctuation">&gt;</span></span>
  102. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>子组件中:
  103. 子组件的js文件:</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code> <span class="token comment">/**
  104. * 组件的属性列表
  105. */</span>
  106. <span class="token literal-property property">properties</span><span class="token operator">:</span> <span class="token punctuation">{</span>
  107. <span class="token literal-property property">playlist</span><span class="token operator">:</span><span class="token punctuation">{</span> <span class="token comment">// 接收父组件传输的数据</span>
  108. <span class="token literal-property property">type</span><span class="token operator">:</span> Object <span class="token comment">// 数据类型</span>
  109. <span class="token punctuation">}</span>
  110. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  111. <span class="token comment">//子组件的wxml文件可直接引入数据{{playlist}}</span>
  112. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h4 id="wx-key-的使用"><a href="#wx-key-的使用" class="header-anchor">#</a> wx:key 的使用</h4> <p>key的值不建议使用index,因为当数据发生变化会dom结构产生变化时,使用index的地方不会随之变化。</p> <p>可以使用数据内部每项不一样的一个数值,如id</p> <div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>block</span> <span class="token attr-name"><span class="token namespace">wx:</span>for</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{{swiperImgUrls}}<span class="token punctuation">&quot;</span></span> <span class="token attr-name"><span class="token namespace">wx:</span>key</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>url<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span> 这里url不需要双大括号,如使用index则需要{{}}
  113. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>view</span><span class="token punctuation">&gt;</span></span>
  114. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>image</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{{item.url}}<span class="token punctuation">&quot;</span></span> <span class="token attr-name">mode</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>widthFix<span class="token punctuation">&quot;</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>img<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>image</span><span class="token punctuation">&gt;</span></span>
  115. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>view</span><span class="token punctuation">&gt;</span></span>
  116. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>block</span><span class="token punctuation">&gt;</span></span>
  117. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>view</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>playlist-container<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
  118. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>block</span> <span class="token attr-name"><span class="token namespace">wx:</span>for</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{{playlist}}<span class="token punctuation">&quot;</span></span> <span class="token attr-name"><span class="token namespace">wx:</span>key</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>_id<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
  119. <span class="token comment">&lt;!-- 参数:playlist 自定义名称,传入组件的数据 --&gt;</span>
  120. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>x-playlist</span> <span class="token attr-name">playlist</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{{item}}<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>x-playlist</span><span class="token punctuation">&gt;</span></span>
  121. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>block</span><span class="token punctuation">&gt;</span></span>
  122. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>view</span><span class="token punctuation">&gt;</span></span>
  123. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><h4 id="async-await-语法"><a href="#async-await-语法" class="header-anchor">#</a> async/await 语法</h4> <p>目前,在云函数里,由于 Node 版本最低是 8.9,因此是天然支持 async/await 语法的。而在小程序端则不然。在微信开发者工具里,以及 Android 端手机(浏览器内核是 QQ浏览器的 X5),async/await是天然支持的,但 iOS 端手机在较低版本则不支持,因此需要引入额外的 文件。</p> <p>可把这个 <a href="https://github.com/xiecheng328/miniprogram/blob/master/regenerator/runtime.js" target="_blank" rel="noopener noreferrer">runtime.js<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 文件引用到有使用 async/await 的文件当中。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 注意,必须命名为 regeneratorRuntime</span>
  124. <span class="token keyword">import</span> regeneratorRuntime <span class="token keyword">from</span> <span class="token string">'../../utils/runtime.js'</span>
  125. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="云函数的使用"><a href="#云函数的使用" class="header-anchor">#</a> 云函数的使用</h4> <p>cloudfunctions目录 右键 <code>新建 Node.js 云函数</code> &gt; 输入目录名 <code>getPlaylist</code></p> <p>在云函数中向第三方服务器发送请求要依赖第三方库</p> <p><strong>安装依赖包</strong></p> <p>云函数目录 <code>getPlaylist</code> 右键 <code>在终端打开</code> 打开命令行 输入命令:</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">npm</span> <span class="token function">install</span> <span class="token parameter variable">--save</span> request
  126. <span class="token function">npm</span> <span class="token function">install</span> <span class="token parameter variable">--save</span> request-promise
  127. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>github request-promise:<a href="https://github.com/request/request-promise" target="_blank" rel="noopener noreferrer">https://github.com/request/request-promise<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <p>然后写相应代码</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 云函数入口文件</span>
  128. <span class="token keyword">const</span> cloud <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'wx-server-sdk'</span><span class="token punctuation">)</span>
  129. cloud<span class="token punctuation">.</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  130. <span class="token keyword">const</span> rp <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'request-promise'</span><span class="token punctuation">)</span> <span class="token comment">// 需安装依赖包</span>
  131. <span class="token keyword">const</span> <span class="token constant">URL</span> <span class="token operator">=</span> <span class="token string">'http://musicapi.xiecheng.live/personalized'</span>
  132. <span class="token comment">// 云函数入口函数</span>
  133. exports<span class="token punctuation">.</span><span class="token function-variable function">main</span> <span class="token operator">=</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token parameter">event<span class="token punctuation">,</span> context</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  134. <span class="token keyword">const</span> playlist <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token function">rp</span><span class="token punctuation">(</span><span class="token constant">URL</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  135. <span class="token keyword">return</span> <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span><span class="token punctuation">.</span>result
  136. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  137. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>playlist<span class="token punctuation">)</span>
  138. <span class="token punctuation">}</span>
  139. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>写完代码,云函数目录 <code>getPlaylist</code> 右键 <code>上传并部署:云端安装依赖(不上传node_modules)</code> 进行上传部署代码到云端,等待上传成功,打开云开发控制台即可看到已经上传的云函数,并可对云函数进行测试。</p> <h5 id="数据库操作"><a href="#数据库操作" class="header-anchor">#</a> 数据库操作</h5> <p>数据库&gt; 创建集合 &gt; playlist</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 云函数入口文件</span>
  140. <span class="token keyword">const</span> cloud <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'wx-server-sdk'</span><span class="token punctuation">)</span>
  141. cloud<span class="token punctuation">.</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  142. <span class="token keyword">const</span> db <span class="token operator">=</span> cloud<span class="token punctuation">.</span><span class="token function">database</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 初始化数据库,如果在小程序端初始化数据库需要在前面加wx.</span>
  143. <span class="token keyword">const</span> rp <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'request-promise'</span><span class="token punctuation">)</span> <span class="token comment">// 需安装依赖包</span>
  144. <span class="token keyword">const</span> <span class="token constant">URL</span> <span class="token operator">=</span> <span class="token string">'http://musicapi.xiecheng.live/personalized'</span> <span class="token comment">// 第三方服务器地址(老师从网易云获取的数据部署在其服务器,每天的数据会更新)</span>
  145. <span class="token keyword">const</span> playlistCollection <span class="token operator">=</span> db<span class="token punctuation">.</span><span class="token function">collection</span><span class="token punctuation">(</span><span class="token string">'playlist'</span><span class="token punctuation">)</span> <span class="token comment">// 获取到数据库playlist集合</span>
  146. <span class="token keyword">const</span> <span class="token constant">MAX_LIMIT</span> <span class="token operator">=</span> <span class="token number">10</span> <span class="token comment">// 定义常量,获取数据库条数最大的限制</span>
  147. <span class="token comment">// 云函数入口函数</span>
  148. exports<span class="token punctuation">.</span><span class="token function-variable function">main</span> <span class="token operator">=</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token parameter">event<span class="token punctuation">,</span> context</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  149. <span class="token comment">/**
  150. * 注:
  151. * - 关于数据库的操作都是异步操作,都需添加await关键字
  152. * - console.log 打印在云开发控制台 云函数测试内查看
  153. * - 单次获取数据库数据有条数限制,云函数端最多获取100条,小程序端最多获取20条
  154. */</span>
  155. <span class="token comment">// const list = await playlistCollection.get() // 获取数据库集合的数据 (因为有条数限制,不直接用此方法)</span>
  156. <span class="token comment">// 突破条数限制 (为了读取到全部数据然后与第三方服务器获取的数据进行对比去重)</span>
  157. <span class="token keyword">const</span> countResult <span class="token operator">=</span> <span class="token keyword">await</span> playlistCollection<span class="token punctuation">.</span><span class="token function">count</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 获取数据总条数 返回为对象</span>
  158. <span class="token keyword">const</span> total <span class="token operator">=</span> countResult<span class="token punctuation">.</span>total <span class="token comment">// 取得总条数</span>
  159. <span class="token keyword">const</span> batchTimes <span class="token operator">=</span> Math<span class="token punctuation">.</span><span class="token function">ceil</span><span class="token punctuation">(</span>total <span class="token operator">/</span> <span class="token constant">MAX_LIMIT</span><span class="token punctuation">)</span>
  160. <span class="token keyword">const</span> tasks <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  161. <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> batchTimes<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  162. <span class="token keyword">let</span> promise <span class="token operator">=</span> playlistCollection<span class="token punctuation">.</span><span class="token function">skip</span><span class="token punctuation">(</span>i <span class="token operator">*</span> <span class="token constant">MAX_LIMIT</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token constant">MAX_LIMIT</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 从第 skip 条开始取,最多取 limit 条数据</span>
  163. tasks<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>promise<span class="token punctuation">)</span>
  164. <span class="token punctuation">}</span>
  165. <span class="token keyword">let</span> list <span class="token operator">=</span> <span class="token punctuation">{</span>
  166. <span class="token literal-property property">data</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  167. <span class="token punctuation">}</span>
  168. <span class="token keyword">if</span> <span class="token punctuation">(</span>tasks<span class="token punctuation">.</span>length <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  169. list <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">await</span> Promise<span class="token punctuation">.</span><span class="token function">all</span><span class="token punctuation">(</span>tasks<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">acc<span class="token punctuation">,</span> cur</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span> <span class="token comment">// reduce数组方法 累积拼接</span>
  170. <span class="token keyword">return</span> <span class="token punctuation">{</span>
  171. <span class="token literal-property property">data</span><span class="token operator">:</span> acc<span class="token punctuation">.</span>data<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>cur<span class="token punctuation">.</span>data<span class="token punctuation">)</span>
  172. <span class="token punctuation">}</span>
  173. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  174. <span class="token punctuation">}</span>
  175. <span class="token comment">// 获取第三方服务器端数据</span>
  176. <span class="token keyword">const</span> playlist <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token function">rp</span><span class="token punctuation">(</span><span class="token constant">URL</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  177. <span class="token keyword">return</span> <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span><span class="token punctuation">.</span>result
  178. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  179. <span class="token comment">// 数据库与服务器数据对比去重(数据已存在数据库的无需再重复添加)</span>
  180. <span class="token keyword">const</span> newData <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  181. <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> len1 <span class="token operator">=</span> playlist<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i <span class="token operator">&lt;</span> len1<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  182. <span class="token keyword">let</span> flag <span class="token operator">=</span> <span class="token boolean">true</span>
  183. <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">let</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> len2 <span class="token operator">=</span> list<span class="token punctuation">.</span>data<span class="token punctuation">.</span>length<span class="token punctuation">;</span> j <span class="token operator">&lt;</span> len2<span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  184. <span class="token keyword">if</span><span class="token punctuation">(</span>playlist<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>id <span class="token operator">===</span> list<span class="token punctuation">.</span>data<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">.</span>id<span class="token punctuation">)</span><span class="token punctuation">{</span>
  185. flag <span class="token operator">=</span> <span class="token boolean">false</span>
  186. <span class="token keyword">break</span>
  187. <span class="token punctuation">}</span>
  188. <span class="token punctuation">}</span>
  189. <span class="token keyword">if</span><span class="token punctuation">(</span>flag<span class="token punctuation">)</span><span class="token punctuation">{</span>
  190. newData<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>playlist<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
  191. <span class="token punctuation">}</span>
  192. <span class="token punctuation">}</span>
  193. <span class="token comment">// 把数据插入数据库,需要单条插入</span>
  194. <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> len <span class="token operator">=</span> newData<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i <span class="token operator">&lt;</span> len<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  195. <span class="token keyword">await</span> playlistCollection<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token comment">// 给数据库集合添加数据</span>
  196. <span class="token literal-property property">data</span><span class="token operator">:</span> <span class="token punctuation">{</span>
  197. <span class="token operator">...</span>newData<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span>
  198. <span class="token literal-property property">createTime</span><span class="token operator">:</span> db<span class="token punctuation">.</span><span class="token function">serverDate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">// db.serverDate() 获取服务器时间</span>
  199. <span class="token punctuation">}</span>
  200. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span> <span class="token comment">// 数据添加成功</span>
  201. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'数据添加成功'</span><span class="token punctuation">)</span>
  202. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">catch</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">err</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span> <span class="token comment">// 失败</span>
  203. console<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
  204. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  205. <span class="token punctuation">}</span>
  206. <span class="token keyword">return</span> newData<span class="token punctuation">.</span>length <span class="token comment">// 插入多少条数据</span>
  207. <span class="token punctuation">}</span>
  208. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br><span class="line-number">71</span><br><span class="line-number">72</span><br><span class="line-number">73</span><br><span class="line-number">74</span><br><span class="line-number">75</span><br><span class="line-number">76</span><br><span class="line-number">77</span><br><span class="line-number">78</span><br><span class="line-number">79</span><br><span class="line-number">80</span><br><span class="line-number">81</span><br></div></div><h5 id="查询数据库"><a href="#查询数据库" class="header-anchor">#</a> 查询数据库</h5> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//云函数中查询数据库的例子:</span>
  209. <span class="token comment">// 云函数入口文件</span>
  210. <span class="token keyword">const</span> cloud <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'wx-server-sdk'</span><span class="token punctuation">)</span>
  211. cloud<span class="token punctuation">.</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  212. <span class="token keyword">const</span> TcbRouter <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'tcb-router'</span><span class="token punctuation">)</span>
  213. <span class="token keyword">const</span> db <span class="token operator">=</span> cloud<span class="token punctuation">.</span><span class="token function">database</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 初始化数据库</span>
  214. <span class="token keyword">const</span> blogCollection <span class="token operator">=</span> db<span class="token punctuation">.</span><span class="token function">collection</span><span class="token punctuation">(</span><span class="token string">'blog'</span><span class="token punctuation">)</span> <span class="token comment">// 博客的数据库集合</span>
  215. <span class="token comment">// 云函数入口函数</span>
  216. exports<span class="token punctuation">.</span><span class="token function-variable function">main</span> <span class="token operator">=</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token parameter">event<span class="token punctuation">,</span> context</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  217. <span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TcbRouter</span><span class="token punctuation">(</span><span class="token punctuation">{</span> event <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">// 初始化TcbRouter</span>
  218. app<span class="token punctuation">.</span><span class="token function">router</span><span class="token punctuation">(</span><span class="token string">'list'</span><span class="token punctuation">,</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token parameter">ctx<span class="token punctuation">,</span> next</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  219. <span class="token comment">// skip 从第几条开始查,limit 查几条数据,orderBy(排序字段,排序方式) 排序,排序方式desc降序/asc升序</span>
  220. ctx<span class="token punctuation">.</span>body <span class="token operator">=</span> <span class="token keyword">await</span> blogCollection<span class="token punctuation">.</span><span class="token function">skip</span><span class="token punctuation">(</span>event<span class="token punctuation">.</span>start<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">limit</span><span class="token punctuation">(</span>event<span class="token punctuation">.</span>count<span class="token punctuation">)</span>
  221. <span class="token punctuation">.</span><span class="token function">orderBy</span><span class="token punctuation">(</span><span class="token string">'createTime'</span><span class="token punctuation">,</span> <span class="token string">'desc'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  222. <span class="token keyword">return</span> res<span class="token punctuation">.</span>data
  223. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  224. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  225. <span class="token keyword">return</span> app<span class="token punctuation">.</span><span class="token function">serve</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 必需返回</span>
  226. <span class="token punctuation">}</span>
  227. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br></div></div><h5 id="云函数调试"><a href="#云函数调试" class="header-anchor">#</a> 云函数调试</h5> <p>云控制台中可会云函数进行云端测试</p> <p>在小程序调用云函数后,可查看云函数<strong>日志</strong></p> <h4 id="定时触发云函数"><a href="#定时触发云函数" class="header-anchor">#</a> 定时触发云函数</h4> <p>如果云函数需要定时 / 定期执行,也就是定时触发,我们可以使用云函数定时触发器。配置了定时触发器的云函数,会在相应时间点被自动触发,函数的返回结果不会返回给调用方</p> <p>云函数目录下新建 config.json</p> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/functions/triggers.html" target="_blank" rel="noopener noreferrer">API<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
  228. <span class="token property">&quot;triggers&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
  229. <span class="token punctuation">{</span>
  230. <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;myTriggers&quot;</span><span class="token punctuation">,</span>
  231. <span class="token property">&quot;type&quot;</span><span class="token operator">:</span> <span class="token string">&quot;timer&quot;</span><span class="token punctuation">,</span>
  232. <span class="token property">&quot;config&quot;</span><span class="token operator">:</span><span class="token string">&quot;0 0 10,14,16,20 * * * *&quot;</span> <span class="token comment">//表示每天的10点、14点、16点、20点触发一次</span>
  233. <span class="token punctuation">}</span>
  234. <span class="token punctuation">]</span>
  235. <span class="token punctuation">}</span>
  236. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>编辑好触发器之后,要在云函数目录 &gt; 右键 &gt; <code>上传触发器</code></p> <h4 id="配置云函数超时时间"><a href="#配置云函数超时时间" class="header-anchor">#</a> 配置云函数超时时间</h4> <p>当云函数比较复杂的时候,默认的超时时间3秒可能不能够满足需求,可以适当的设置更为合理的时间</p> <p>云开发控制台 &gt; 云函数 &gt; 配置 &gt; 超时时间</p> <h4 id="上拉加载与下拉刷新"><a href="#上拉加载与下拉刷新" class="header-anchor">#</a> 上拉加载与下拉刷新</h4> <div class="language-js line-numbers-mode"><pre class="language-js"><code>page页面json中:
  237. <span class="token string-property property">&quot;enablePullDownRefresh&quot;</span><span class="token operator">:</span> <span class="token boolean">true</span>
  238. page页面js中有这两个函数:
  239. <span class="token comment">/**
  240. * 页面相关事件处理函数--监听用户下拉动作
  241. */</span>
  242. <span class="token function-variable function">onPullDownRefresh</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  243. <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setData</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  244. <span class="token literal-property property">playlist</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  245. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  246. <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">_getPlaylist</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  247. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  248. <span class="token comment">/**
  249. * 页面上拉触底事件的处理函数
  250. */</span>
  251. <span class="token function-variable function">onReachBottom</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  252. <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">_getPlaylist</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  253. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  254. 下拉刷新请求完数据后
  255. wx<span class="token punctuation">.</span><span class="token function">stopPullDownRefresh</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 停止下拉刷新动画</span>
  256. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br></div></div><h4 id="云函数路由优化tcb-router"><a href="#云函数路由优化tcb-router" class="header-anchor">#</a> 云函数路由优化tcb-router</h4> <p>一个用户在一个云环境只能创建50个云函数</p> <p>假如小程序非常复杂,50个云函数不能够满足业务需求怎么办?</p> <p>相似的请求归类到同一个云函数处理</p> <p>tcb-router是一个koa风格的云函数路由库</p> <p>通俗理解就是可以把很多个接口归类到同一个云函数内。</p> <p>github-tcb-router: <a href="https://github.com/TencentCloudBase/tcb-router" target="_blank" rel="noopener noreferrer">https://github.com/TencentCloudBase/tcb-router<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <p>koa洋葱模型...</p> <p>安装:</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>在使用到tcb-router的云函数目录下打开命令行,输入命令进行安装
  257. <span class="token function">npm</span> <span class="token function">install</span> <span class="token parameter variable">--save</span> tcb-router
  258. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 云函数的 index.js</span>
  259. <span class="token keyword">const</span> TcbRouter <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'router'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 必需</span>
  260. exports<span class="token punctuation">.</span><span class="token function-variable function">main</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">event<span class="token punctuation">,</span> context</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  261. <span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TcbRouter</span><span class="token punctuation">(</span><span class="token punctuation">{</span> event <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 必需</span>
  262. <span class="token comment">// app.use 表示该中间件会适用于所有的路由(全局中间件) 非必需</span>
  263. app<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span><span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token parameter">ctx<span class="token punctuation">,</span> next</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span> <span class="token comment">// 这个中间件表示所有路由都会调用到,而路由中间件为单独调用</span>
  264. ctx<span class="token punctuation">.</span>data <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token comment">// 获取要传给小程序端的数据</span>
  265. ctx<span class="token punctuation">.</span>data<span class="token punctuation">.</span>openId <span class="token operator">=</span> event<span class="token punctuation">.</span>userInfo<span class="token punctuation">.</span>openId <span class="token comment">// 这里获取到的openId将分布到所有路由</span>
  266. <span class="token keyword">await</span> <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 执行下一中间件</span>
  267. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  268. <span class="token comment">// 路由为数组表示,该中间件适用于 user 和 timer 两个路由</span>
  269. app<span class="token punctuation">.</span><span class="token function">router</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'user'</span><span class="token punctuation">,</span> <span class="token string">'timer'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token parameter">ctx<span class="token punctuation">,</span> next</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  270. ctx<span class="token punctuation">.</span>data<span class="token punctuation">.</span>company <span class="token operator">=</span> <span class="token string">'Tencent'</span><span class="token punctuation">;</span> <span class="token comment">// 这里获取到的数据将分布到 user 和 timer 两个路由</span>
  271. <span class="token keyword">await</span> <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 执行下一中间件</span>
  272. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  273. <span class="token comment">// 路由为字符串,该中间件只适用于 user 路由</span>
  274. app<span class="token punctuation">.</span><span class="token function">router</span><span class="token punctuation">(</span><span class="token string">'user'</span><span class="token punctuation">,</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token parameter">ctx<span class="token punctuation">,</span> next</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  275. ctx<span class="token punctuation">.</span>data<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">'heyli'</span><span class="token punctuation">;</span> <span class="token comment">// 获取要传给小程序端的数据</span>
  276. <span class="token keyword">await</span> <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 执行下一中间件</span>
  277. <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token parameter">ctx<span class="token punctuation">,</span> next</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  278. ctx<span class="token punctuation">.</span>data<span class="token punctuation">.</span>sex <span class="token operator">=</span> <span class="token string">'male'</span><span class="token punctuation">;</span> <span class="token comment">// 获取要传给小程序端的数据</span>
  279. <span class="token keyword">await</span> <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 执行下一中间件</span>
  280. <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token parameter">ctx</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  281. ctx<span class="token punctuation">.</span>data<span class="token punctuation">.</span>city <span class="token operator">=</span> <span class="token string">'Foshan'</span><span class="token punctuation">;</span> <span class="token comment">// 获取要传给小程序端的数据</span>
  282. <span class="token comment">// ctx.body 返回数据到小程序端</span>
  283. ctx<span class="token punctuation">.</span>body <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token literal-property property">code</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token literal-property property">data</span><span class="token operator">:</span> ctx<span class="token punctuation">.</span>data<span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token comment">// 要传给小程序端的数据</span>
  284. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  285. <span class="token comment">// 路由为字符串,该中间件只适用于 timer 路由</span>
  286. app<span class="token punctuation">.</span><span class="token function">router</span><span class="token punctuation">(</span><span class="token string">'timer'</span><span class="token punctuation">,</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token parameter">ctx<span class="token punctuation">,</span> next</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  287. ctx<span class="token punctuation">.</span>data<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">'flytam'</span><span class="token punctuation">;</span>
  288. <span class="token keyword">await</span> <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 执行下一中间件</span>
  289. <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token parameter">ctx<span class="token punctuation">,</span> next</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  290. ctx<span class="token punctuation">.</span>data<span class="token punctuation">.</span>sex <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token keyword">new</span> <span class="token class-name">Promise</span><span class="token punctuation">(</span><span class="token parameter">resolve</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  291. <span class="token comment">// 等待500ms,再执行下一中间件</span>
  292. <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  293. <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string">'male'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  294. <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  295. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  296. <span class="token keyword">await</span> <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 执行下一中间件</span>
  297. <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token parameter">ctx</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  298. ctx<span class="token punctuation">.</span>data<span class="token punctuation">.</span>city <span class="token operator">=</span> <span class="token string">'Taishan'</span><span class="token punctuation">;</span>
  299. <span class="token comment">// ctx.body 返回数据到小程序端</span>
  300. ctx<span class="token punctuation">.</span>body <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token literal-property property">code</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token literal-property property">data</span><span class="token operator">:</span> ctx<span class="token punctuation">.</span>data <span class="token punctuation">}</span><span class="token punctuation">;</span>
  301. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  302. <span class="token keyword">return</span> app<span class="token punctuation">.</span><span class="token function">serve</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 必需</span>
  303. <span class="token punctuation">}</span>
  304. 小程序端:
  305. <span class="token comment">// 调用名为 router 的云函数,路由名为 user</span>
  306. wx<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span><span class="token function">callFunction</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  307. <span class="token comment">// 要调用的云函数名称</span>
  308. <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">&quot;router&quot;</span><span class="token punctuation">,</span>
  309. <span class="token comment">// 传递给云函数的参数</span>
  310. <span class="token literal-property property">data</span><span class="token operator">:</span> <span class="token punctuation">{</span>
  311. <span class="token literal-property property">$url</span><span class="token operator">:</span> <span class="token string">&quot;user&quot;</span><span class="token punctuation">,</span> <span class="token comment">// 要调用的路由的路径,传入准确路径或者通配符*</span>
  312. <span class="token literal-property property">other</span><span class="token operator">:</span> <span class="token string">&quot;xxx&quot;</span>
  313. <span class="token punctuation">}</span>
  314. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  315. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span>
  316. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  317. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br></div></div><p>上面tcb-router代码会按照洋葱模型执行,即先从上往下逐个进入中间件,再从下往上逐个退出中间件。</p> <h4 id="本地存储-缓存"><a href="#本地存储-缓存" class="header-anchor">#</a> 本地存储(缓存)</h4> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 存储:</span>
  318. wx<span class="token punctuation">.</span><span class="token function">setStorageSync</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> data<span class="token punctuation">)</span> <span class="token comment">// 同步存储(存储成功再继续下一步操作)</span>
  319. wx<span class="token punctuation">.</span><span class="token function">setStorage</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> data<span class="token punctuation">)</span> <span class="token comment">// 异步存储(即使存储没成功也会执行下一步代码)、</span>
  320. <span class="token comment">// 读取:</span>
  321. wx<span class="token punctuation">.</span><span class="token function">getStorageSync</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span> <span class="token comment">// 同步 (读取到数据在进行下一步操作)</span>
  322. wx<span class="token punctuation">.</span><span class="token function">setStorage</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span> <span class="token comment">// 异步</span>
  323. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h4 id="api设置title"><a href="#api设置title" class="header-anchor">#</a> api设置title</h4> <div class="language-js line-numbers-mode"><pre class="language-js"><code>wx<span class="token punctuation">.</span><span class="token function">setNavigationBarTitle</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  324. <span class="token literal-property property">title</span><span class="token operator">:</span> <span class="token string">''</span><span class="token punctuation">,</span>
  325. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  326. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h4 id="背景播放音"><a href="#背景播放音" class="header-anchor">#</a> 背景播放音</h4> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/api/media/background-audio/BackgroundAudioManager.html" target="_blank" rel="noopener noreferrer">BackgroundAudioManager<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 全局唯一的背景音频管理器</p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token comment">// 需要在app.json配置,才能使用后台音乐播放的能力</span>
  327. <span class="token property">&quot;requiredBackgroundModes&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">&quot;audio&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;location&quot;</span><span class="token punctuation">]</span>
  328. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 获取全局唯一的背景音频管理器</span>
  329. <span class="token keyword">const</span> backgroundAudioManager <span class="token operator">=</span> wx<span class="token punctuation">.</span><span class="token function">getBackgroundAudioManager</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  330. backgroundAudioManager<span class="token punctuation">.</span>src <span class="token operator">=</span> 音频链接
  331. backgroundAudioManager<span class="token punctuation">.</span>title <span class="token operator">=</span> 音频标题
  332. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h4 id="createselectorquery查询节点信息"><a href="#createselectorquery查询节点信息" class="header-anchor">#</a> createSelectorQuery查询节点信息</h4> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/api/wxml/wx.createSelectorQuery.html" target="_blank" rel="noopener noreferrer">createSelectorQuery<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 小程序的方法,用于查询节点等操作</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> query <span class="token operator">=</span> wx<span class="token punctuation">.</span><span class="token function">createSelectorQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  333. query<span class="token punctuation">.</span><span class="token function">select</span><span class="token punctuation">(</span><span class="token string">'#the-id'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">boundingClientRect</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 节点的布局信息</span>
  334. query<span class="token punctuation">.</span><span class="token function">selectViewport</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">scrollOffset</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  335. query<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  336. res<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>top <span class="token comment">// #the-id节点的上边界坐标</span>
  337. res<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>scrollTop <span class="token comment">// 显示区域的竖直滚动位置</span>
  338. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  339. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h4 id="组件内的方法"><a href="#组件内的方法" class="header-anchor">#</a> 组件内的方法</h4> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/reference/api/Component.html" target="_blank" rel="noopener noreferrer">Component(Object object)<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h4 id="组件生命周期"><a href="#组件生命周期" class="header-anchor">#</a> 组件生命周期</h4> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/lifetimes.html" target="_blank" rel="noopener noreferrer">lifetimes<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 生命周期</span>
  340. <span class="token literal-property property">lifetimes</span><span class="token operator">:</span> <span class="token punctuation">{</span>
  341. <span class="token function">ready</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 在组件在视图层布局完成后执行</span>
  342. <span class="token operator">...</span>
  343. <span class="token punctuation">}</span>
  344. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  345. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h5 id="组件所在页面的生命周期"><a href="#组件所在页面的生命周期" class="header-anchor">#</a> 组件所在页面的生命周期</h5> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">Component</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  346. <span class="token literal-property property">pageLifetimes</span><span class="token operator">:</span> <span class="token punctuation">{</span>
  347. <span class="token function-variable function">show</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  348. <span class="token comment">// 页面被展示</span>
  349. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  350. <span class="token function-variable function">hide</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  351. <span class="token comment">// 页面被隐藏</span>
  352. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  353. <span class="token function-variable function">resize</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">size</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  354. <span class="token comment">// 页面尺寸变化</span>
  355. <span class="token punctuation">}</span>
  356. <span class="token punctuation">}</span>
  357. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  358. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><h4 id="组件对数据的监听"><a href="#组件对数据的监听" class="header-anchor">#</a> 组件对数据的监听</h4> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/observer.html" target="_blank" rel="noopener noreferrer">observers<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token literal-property property">observers</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token comment">// 对数据的监听(数据初次加载完成也会执行)</span>
  359. <span class="token function">监听的数据对象</span><span class="token punctuation">(</span><span class="token parameter">newData</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  360. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>newData<span class="token punctuation">)</span>
  361. <span class="token punctuation">}</span>
  362. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  363. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h4 id="子组件自定义事件传递给父组件"><a href="#子组件自定义事件传递给父组件" class="header-anchor">#</a> 子组件自定义事件传递给父组件</h4> <div class="language- line-numbers-mode"><pre class="language-text"><code>子组件js:
  364. // 触发自定义事件 向父组件传值, 参数x(可选,传递给父组件的参数,可以是对象或其他)
  365. this.triggerEvent('自定义事件名', 参数x)
  366. 父组件wxml:
  367. &lt;子组件标签 bind:自定义事件名=&quot;执行的事件&quot; /&gt;
  368. 父组件js:
  369. 执行的事件(event) {
  370. console.log(event.detil.参数)
  371. }
  372. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h4 id="父组件自定义事件传递给子组件"><a href="#父组件自定义事件传递给子组件" class="header-anchor">#</a> 父组件自定义事件传递给子组件</h4> <div class="language- line-numbers-mode"><pre class="language-text"><code>父组件wxml:
  373. &lt;子组件标签 class=&quot;子组件类名&quot;&gt;
  374. 父组件JS:
  375. // 选择组件,并传入事件和参数
  376. this.selectComponent('.子组件类名').自定义事件名(传入参数)
  377. 子组件js:
  378. methods: {
  379. 自定义事件名(参数x){
  380. console.log(参数x)
  381. }
  382. }
  383. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h4 id="兄弟组件间传递事件和传值"><a href="#兄弟组件间传递事件和传值" class="header-anchor">#</a> 兄弟组件间传递事件和传值</h4> <div class="language- line-numbers-mode"><pre class="language-text"><code>例子:子组件1向子组件2传递参数
  384. 父组件wxml中:
  385. &lt;子组件标签1 bind:自定义事件名1=&quot;执行的事件&quot;&gt;
  386. &lt;子组件标签2 class=&quot;子组件2类名&quot;&gt;
  387. 父组件js:
  388. 执行的事件(event) {
  389. this.selectComponent('.子组件2类名').自定义事件名2(event.detil.参数x) // 向子组件2传值
  390. }
  391. 子组件1js:
  392. // 触发自定义事件 向父组件传值, 参数x(可选,传递给父组件的参数,可以是对象或其他)
  393. this.triggerEvent('自定义事件名1', 参数x)
  394. 子组件2js:
  395. methods: {
  396. 自定义事件名2(参数x){
  397. console.log(参数x) // 接收父组件传入的值
  398. }
  399. }
  400. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><h4 id="获取手机信息"><a href="#获取手机信息" class="header-anchor">#</a> 获取手机信息</h4> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/api/base/system/system-info/wx.getSystemInfo.html" target="_blank" rel="noopener noreferrer">wx.getSystemInfo(Object object)<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>wx<span class="token punctuation">.</span><span class="token function">getSystemInfo</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  401. <span class="token function">success</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  402. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span> <span class="token comment">//手机信息</span>
  403. <span class="token punctuation">}</span>
  404. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  405. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h4 id="滚动组件"><a href="#滚动组件" class="header-anchor">#</a> 滚动组件</h4> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/component/scroll-view.html" target="_blank" rel="noopener noreferrer">scroll-view<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>scroll-view</span> <span class="token attr-name">scroll-y</span> <span class="token attr-name">scroll-top</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{{scrollTop}}<span class="token punctuation">&quot;</span></span> <span class="token attr-name">scroll-with-animation</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>true<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
  406. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>scroll-view</span><span class="token punctuation">&gt;</span></span>
  407. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="全局属性、方法-类似vuex"><a href="#全局属性、方法-类似vuex" class="header-anchor">#</a> 全局属性、方法(类似vuex)</h4> <div class="language- line-numbers-mode"><pre class="language-text"><code>在app.js中:
  408. onLaunch: function () {
  409. this.globalData = {// 设置全局属性、方法
  410. test: 0
  411. }
  412. },
  413. setGlobalData(dataItem, val) { // 设置全局属性
  414. this.globalData[dataItem] = val
  415. },
  416. getGlobalData(dataItem) { // 获取全局属性
  417. return this.globalData[dataItem]
  418. }
  419. 在需要调用的页面js中:
  420. const app = getApp() // 在最顶部先调用app方法
  421. // 设置全局属性
  422. app.setGlobalData('test', 1)
  423. // 获取全局属性
  424. app.getGlobalData('test')
  425. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><h4 id="消息提示框"><a href="#消息提示框" class="header-anchor">#</a> 消息提示框</h4> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/api/ui/interaction/wx.showToast.html" target="_blank" rel="noopener noreferrer">showToast<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>wx<span class="token punctuation">.</span><span class="token function">showToast</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  426. <span class="token literal-property property">title</span><span class="token operator">:</span> <span class="token string">'成功'</span><span class="token punctuation">,</span>
  427. <span class="token literal-property property">icon</span><span class="token operator">:</span> <span class="token string">'success'</span><span class="token punctuation">,</span> <span class="token comment">//图标: success 成功、loading 加载中、none 无</span>
  428. <span class="token literal-property property">duration</span><span class="token operator">:</span> <span class="token number">2000</span>
  429. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  430. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h2 id="《发现》页面"><a href="#《发现》页面" class="header-anchor">#</a> 《发现》页面</h2> <h4 id="调用组件外部的样式"><a href="#调用组件外部的样式" class="header-anchor">#</a> 调用组件外部的样式</h4> <p>components内部的组件无法直接调用外部的样式。可通过以下方式调用组件外部样式:</p> <p><strong>方法一:</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>父组件wxml:
  431. &lt;!-- iconfont 和 icon-sousuo 是传入组件内部的样式名称,iconfont(自定义名称)=&quot;iconfont(外部样式文件中定义的样式名)&quot; --&gt;
  432. &lt;x-search iconfont=&quot;iconfont&quot; icon-sousuo=&quot;icon-sousuo&quot;/&gt;
  433. 子组件js:
  434. // 组件外部样式
  435. externalClasses: [
  436. 'iconfont', // 对应的是上面等号前面的名称
  437. 'icon-sousuo'
  438. ],
  439. 子组件wxml: 即可实现调用组件外的样式
  440. &lt;i class=&quot;iconfont icon-sousuo&quot; /&gt;
  441. 注意:如果想在组件内部再次修改样式,不能够引用外部传进来的class名称进行修改,可以另起一个class名称进行修改。
  442. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><p><strong>方法二:</strong></p> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/wxml-wxss.html#%E7%BB%84%E4%BB%B6%E6%A0%B7%E5%BC%8F%E9%9A%94%E7%A6%BB" target="_blank" rel="noopener noreferrer">消除样式隔离<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token literal-property property">组件内</span><span class="token operator">:</span>
  443. <span class="token function">Component</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  444. <span class="token literal-property property">options</span><span class="token operator">:</span> <span class="token punctuation">{</span>
  445. <span class="token literal-property property">styleIsolation</span><span class="token operator">:</span> <span class="token string">'apply-shared'</span>
  446. <span class="token punctuation">}</span>
  447. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  448. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h4 id="组件插槽slot"><a href="#组件插槽slot" class="header-anchor">#</a> 组件插槽slot</h4> <p><strong>单个插槽</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>父组件调用传入插槽内容:
  449. &lt;组件标签&gt;
  450. &lt;view&gt;
  451. &lt;view&gt;插槽内容&lt;/view&gt;
  452. &lt;view&gt;插槽内容&lt;/view&gt;
  453. &lt;/view&gt;
  454. &lt;/组件标签&gt;
  455. 组件内部定义slot标签:
  456. &lt;view&gt;
  457. &lt;!-- slot插槽 --&gt;
  458. &lt;slot&gt;&lt;/slot&gt;
  459. &lt;/view&gt;
  460. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><p><strong>如果需要实现多个插槽</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>
  461. 父组件调用传入插槽内容:
  462. &lt;组件标签&gt;
  463. &lt;view slot=&quot;slot2&quot;&gt;
  464. &lt;view&gt;插槽1内容&lt;/view&gt;
  465. &lt;view&gt;插槽1内容&lt;/view&gt;
  466. &lt;/view&gt;
  467. &lt;view slot=&quot;slot1&quot;&gt;
  468. &lt;view&gt;插槽2内容&lt;/view&gt;
  469. &lt;view&gt;插槽2内容&lt;/view&gt;
  470. &lt;/view&gt;
  471. &lt;/组件标签&gt;
  472. 组件js :
  473. options: {// 设置
  474. multipleSlots: true // 打开多个插槽功能
  475. },
  476. 组件内部定义slot标签:
  477. &lt;view&gt;
  478. &lt;!-- slot插槽 具名插槽--&gt;
  479. &lt;slot name=&quot;slot1&quot;&gt;&lt;/slot&gt;
  480. &lt;slot name=&quot;slot2&quot;&gt;&lt;/slot&gt;
  481. &lt;/view&gt;
  482. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br></div></div><h4 id="判断用户授权"><a href="#判断用户授权" class="header-anchor">#</a> 判断用户授权</h4> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/authorize.html" target="_blank" rel="noopener noreferrer">授权<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 判断用户是否授权</span>
  483. wx<span class="token punctuation">.</span><span class="token function">getSetting</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  484. <span class="token function-variable function">success</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span> <span class="token comment">// 这里使用箭头函数可改变内部this指向为外部的this</span>
  485. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span>
  486. <span class="token keyword">if</span> <span class="token punctuation">(</span>res<span class="token punctuation">.</span>authSetting<span class="token punctuation">[</span><span class="token string">'scope.userInfo'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 已授权</span>
  487. wx<span class="token punctuation">.</span><span class="token function">getUserInfo</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token comment">// 获取用户信息</span>
  488. <span class="token function">success</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  489. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span>
  490. <span class="token punctuation">}</span>
  491. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  492. <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token comment">// 未授权</span>
  493. <span class="token punctuation">}</span>
  494. <span class="token punctuation">}</span>
  495. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  496. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h4 id="button的开发能力-获取用户信息-1"><a href="#button的开发能力-获取用户信息-1" class="header-anchor">#</a> button的开发能力(获取用户信息)1</h4> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token operator">&lt;</span>button <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">&quot;login&quot;</span>
  497. open<span class="token operator">-</span>type<span class="token operator">=</span><span class="token string">&quot;getUserInfo&quot;</span>
  498. bindgetuserinfo<span class="token operator">=</span><span class="token string">&quot;onGetUserInfo&quot;</span> <span class="token comment">// bindgetuserinfo 为固定的</span>
  499. <span class="token operator">&gt;</span>
  500. 获取微信授权信息
  501. <span class="token operator">&lt;</span><span class="token operator">/</span>button<span class="token operator">&gt;</span>
  502. bindgetuserinfo 事件会询问用户是否同意授权
  503. js中:
  504. <span class="token function">onGetUserInfo</span><span class="token punctuation">(</span><span class="token parameter">event</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 获取用户信息</span>
  505. <span class="token keyword">const</span> userInfo <span class="token operator">=</span> event<span class="token punctuation">.</span>detail<span class="token punctuation">.</span>userInfo
  506. <span class="token keyword">if</span> <span class="token punctuation">(</span>userInfo<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 用户允许授权</span>
  507. <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setData</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  508. <span class="token literal-property property">modalShow</span><span class="token operator">:</span> <span class="token boolean">false</span>
  509. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  510. <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">triggerEvent</span><span class="token punctuation">(</span><span class="token string">'loginSuccess'</span><span class="token punctuation">,</span> userInfo<span class="token punctuation">)</span> <span class="token comment">// 给父组件传用户数据</span>
  511. <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token comment">// 用户拒绝授权</span>
  512. <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">triggerEvent</span><span class="token punctuation">(</span><span class="token string">'loginFail'</span><span class="token punctuation">)</span>
  513. <span class="token punctuation">}</span>
  514. <span class="token punctuation">}</span>
  515. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><h4 id="原生组件"><a href="#原生组件" class="header-anchor">#</a> 原生组件</h4> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/component/native-component.html" target="_blank" rel="noopener noreferrer">原生组件<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <div class="language-html line-numbers-mode"><pre class="language-html"><code>
  516. auto-focus 自动获取焦点
  517. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>textarea</span>
  518. <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>content<span class="token punctuation">&quot;</span></span>
  519. <span class="token attr-name">placeholder</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>分享新鲜事...<span class="token punctuation">&quot;</span></span>
  520. <span class="token attr-name">maxlength</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>140<span class="token punctuation">&quot;</span></span>
  521. <span class="token attr-name">auto-focus</span>
  522. <span class="token attr-name">bindinput</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>onInput<span class="token punctuation">&quot;</span></span>
  523. <span class="token attr-name">bindfocus</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>onFocus<span class="token punctuation">&quot;</span></span>
  524. <span class="token attr-name">bindblur</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>onBlur<span class="token punctuation">&quot;</span></span>
  525. <span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>textarea</span><span class="token punctuation">&gt;</span></span>
  526. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h4 id="选择上传图片"><a href="#选择上传图片" class="header-anchor">#</a> 选择上传图片</h4> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/api/media/image/wx.chooseImage.html" target="_blank" rel="noopener noreferrer">上传图片<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">let</span> max <span class="token operator">=</span> <span class="token number">9</span> <span class="token operator">-</span> <span class="token keyword">this</span><span class="token punctuation">.</span>data<span class="token punctuation">.</span>images<span class="token punctuation">.</span>length <span class="token comment">// 还能再选几张图片</span>
  527. wx<span class="token punctuation">.</span><span class="token function">chooseImage</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  528. <span class="token literal-property property">count</span><span class="token operator">:</span> max<span class="token punctuation">,</span> <span class="token comment">// 还能再选几张图片</span>
  529. <span class="token literal-property property">sizeType</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'original'</span><span class="token punctuation">,</span> <span class="token string">'compressed'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token comment">// 初始值 and 压缩过的</span>
  530. <span class="token literal-property property">sourceType</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'album'</span><span class="token punctuation">,</span> <span class="token string">'camera'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token comment">// 手机相册选择 and 拍照选择</span>
  531. <span class="token function-variable function">success</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span> <span class="token comment">// 箭头函数改变this指向</span>
  532. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span>
  533. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  534. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  535. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h4 id="图片裁剪"><a href="#图片裁剪" class="header-anchor">#</a> 图片裁剪</h4> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/component/image.html" target="_blank" rel="noopener noreferrer">图片裁剪<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token comment">&lt;!-- mode 图片裁剪 aspectFill 保证短边完整显示 --&gt;</span>
  536. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>image</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>image<span class="token punctuation">&quot;</span></span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{{item}}<span class="token punctuation">&quot;</span></span> <span class="token attr-name">mode</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>aspectFill<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>image</span><span class="token punctuation">&gt;</span></span>
  537. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="获取标签自定义属性data-删除图片的实现"><a href="#获取标签自定义属性data-删除图片的实现" class="header-anchor">#</a> 获取标签自定义属性data-* (删除图片的实现)</h4> <div class="language- line-numbers-mode"><pre class="language-text"><code>&lt;!-- 显示图片 --&gt;
  538. &lt;block wx:for=&quot;{{images}}&quot; wx:key=&quot;*this&quot;&gt;
  539. &lt;view class=&quot;image-wrap&quot;&gt;
  540. &lt;!-- mode 图片裁剪 aspectFill 保证短边完整显示 --&gt;
  541. &lt;image class=&quot;image&quot; src=&quot;{{item}}&quot; mode=&quot;aspectFill&quot;&gt;&lt;/image&gt;
  542. &lt;icon class=&quot;iconfont icon-shanchu&quot; bindtap=&quot;onDelImage&quot; data-index=&quot;{{index}}&quot;&gt;&lt;/icon&gt;
  543. &lt;/view&gt;
  544. &lt;/block&gt;
  545. // 删除图片
  546. onDelImage(event) {
  547. // event.target.dataset.index 获取标签属性data-index的值
  548. this.data.images.splice(event.target.dataset.index, 1) // splice会改变原有数组
  549. this.setData({
  550. images: this.data.images
  551. })
  552. },
  553. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><h4 id="全屏预览图片-点击图片放大预览"><a href="#全屏预览图片-点击图片放大预览" class="header-anchor">#</a> 全屏预览图片(点击图片放大预览)</h4> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/api/media/image/wx.previewImage.html" target="_blank" rel="noopener noreferrer">全屏预览图片<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 全屏预览图片</span>
  554. <span class="token function">onPreviewImage</span><span class="token punctuation">(</span><span class="token parameter">event</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  555. wx<span class="token punctuation">.</span><span class="token function">previewImage</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  556. <span class="token literal-property property">urls</span><span class="token operator">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>data<span class="token punctuation">.</span>images<span class="token punctuation">,</span> <span class="token comment">// 图片地址列表</span>
  557. <span class="token literal-property property">current</span><span class="token operator">:</span> event<span class="token punctuation">.</span>target<span class="token punctuation">.</span>dataset<span class="token punctuation">.</span>imgsrc <span class="token comment">// 当前预览图片地址</span>
  558. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  559. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  560. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h4 id="文件上传云存储-发布博客例子"><a href="#文件上传云存储-发布博客例子" class="header-anchor">#</a> 文件上传云存储(发布博客例子)</h4> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-client-api/storage/uploadFile.html" target="_blank" rel="noopener noreferrer">文件上传云存储<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code> <span class="token comment">// 结合'发布'的例子:</span>
  561. <span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  562. <span class="token comment">// 验证是否输入内容</span>
  563. <span class="token keyword">if</span> <span class="token punctuation">(</span>content<span class="token punctuation">.</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token string">''</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// trim() 去掉字符串空格</span>
  564. wx<span class="token punctuation">.</span><span class="token function">showToast</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  565. <span class="token literal-property property">title</span><span class="token operator">:</span> <span class="token string">'请输入内容'</span><span class="token punctuation">,</span>
  566. <span class="token literal-property property">icon</span><span class="token operator">:</span> <span class="token string">'none'</span>
  567. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  568. <span class="token keyword">return</span>
  569. <span class="token punctuation">}</span>
  570. wx<span class="token punctuation">.</span><span class="token function">showLoading</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  571. <span class="token literal-property property">title</span><span class="token operator">:</span> <span class="token string">'发布中'</span><span class="token punctuation">,</span>
  572. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  573. <span class="token comment">/**
  574. * 实现思路及步骤:
  575. * 1、图片 -&gt; 上传 云存储 -&gt; 生成 图片fineID(云文件ID)
  576. * 2、数据 -&gt; 录入 云数据库
  577. * 数据包括:文字内容、图片fineID、昵称、头像、发布时间、openId(用户唯一标识,在插入数据库是系统会自动添加_openId字段,不需要另外插入)
  578. */</span>
  579. <span class="token keyword">let</span> promiseArr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  580. <span class="token keyword">let</span> fileIds <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  581. <span class="token comment">// 图片上传云存储</span>
  582. <span class="token keyword">this</span><span class="token punctuation">.</span>data<span class="token punctuation">.</span>images<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">item</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  583. <span class="token keyword">let</span> p <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Promise</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">resolve<span class="token punctuation">,</span> reject</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  584. <span class="token keyword">let</span> suffix <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\.\w+$</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token comment">// 文件扩展名(文件后缀)</span>
  585. wx<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span><span class="token function">uploadFile</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token comment">// 每次只能上传一个文件</span>
  586. <span class="token comment">/**
  587. * cloudPath 云路径。如果路径相同,后上传的文件会覆盖原文件
  588. * 路径:blog/云存储中的文件夹 + Date.now()时间戳 + Math.random()*1000000随机数 + 文件后缀
  589. */</span>
  590. <span class="token literal-property property">cloudPath</span><span class="token operator">:</span> <span class="token string">'blog/'</span> <span class="token operator">+</span> Date<span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'-'</span> <span class="token operator">+</span> Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">1000000</span> <span class="token operator">+</span> suffix<span class="token punctuation">,</span>
  591. <span class="token literal-property property">filePath</span><span class="token operator">:</span> item<span class="token punctuation">,</span> <span class="token comment">// 文件本地临时路径</span>
  592. <span class="token function-variable function">success</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  593. fileIds<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>res<span class="token punctuation">.</span>fileID<span class="token punctuation">)</span>
  594. <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  595. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  596. <span class="token function-variable function">fail</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">err</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  597. console<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
  598. <span class="token function">reject</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  599. <span class="token punctuation">}</span>
  600. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  601. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  602. promiseArr<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>p<span class="token punctuation">)</span>
  603. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  604. <span class="token comment">// 存入云数据库</span>
  605. Promise<span class="token punctuation">.</span><span class="token function">all</span><span class="token punctuation">(</span>promiseArr<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  606. db<span class="token punctuation">.</span><span class="token function">collection</span><span class="token punctuation">(</span><span class="token string">'blog'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  607. <span class="token literal-property property">data</span><span class="token operator">:</span> <span class="token punctuation">{</span>
  608. <span class="token operator">...</span>userInfo<span class="token punctuation">,</span> <span class="token comment">// 昵称、头像</span>
  609. content<span class="token punctuation">,</span> <span class="token comment">// 内容</span>
  610. <span class="token literal-property property">img</span><span class="token operator">:</span> fileIds<span class="token punctuation">,</span> <span class="token comment">// 图片fileID列表</span>
  611. <span class="token literal-property property">createTime</span><span class="token operator">:</span> db<span class="token punctuation">.</span><span class="token function">serverDate</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 创建时间,取服务端时间</span>
  612. <span class="token punctuation">}</span>
  613. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  614. wx<span class="token punctuation">.</span><span class="token function">hideLoading</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  615. wx<span class="token punctuation">.</span><span class="token function">showToast</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  616. <span class="token literal-property property">title</span><span class="token operator">:</span> <span class="token string">'发布成功'</span><span class="token punctuation">,</span>
  617. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  618. <span class="token comment">// 返回博客页面,并刷新</span>
  619. wx<span class="token punctuation">.</span><span class="token function">navigateBack</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  620. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  621. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">catch</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">err</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  622. wx<span class="token punctuation">.</span><span class="token function">hideLoading</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  623. wx<span class="token punctuation">.</span><span class="token function">showToast</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  624. <span class="token literal-property property">title</span><span class="token operator">:</span> <span class="token string">'抱歉,发布失败'</span><span class="token punctuation">,</span>
  625. <span class="token literal-property property">icon</span><span class="token operator">:</span> <span class="token string">'none'</span>
  626. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  627. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  628. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  629. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br><span class="line-number">71</span><br></div></div><h4 id="js模块化-时间格式化"><a href="#js模块化-时间格式化" class="header-anchor">#</a> js模块化 (时间格式化)</h4> <p>在目录utils 中新建formatTime.js文件</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 时间格式化 模块封装</span>
  630. module<span class="token punctuation">.</span><span class="token function-variable function">exports</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">date</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span> <span class="token comment">// date 数据格式为 date</span>
  631. <span class="token keyword">let</span> fmt <span class="token operator">=</span> <span class="token string">'yyyy-MM-dd hh:mm:ss'</span> <span class="token comment">// 预定格式</span>
  632. <span class="token keyword">const</span> o <span class="token operator">=</span> <span class="token punctuation">{</span>
  633. <span class="token comment">// + 正则中的1个或多个</span>
  634. <span class="token string-property property">'M+'</span><span class="token operator">:</span> date<span class="token punctuation">.</span><span class="token function">getMonth</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span>
  635. <span class="token string-property property">'d+'</span><span class="token operator">:</span> date<span class="token punctuation">.</span><span class="token function">getDate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  636. <span class="token string-property property">'h+'</span><span class="token operator">:</span> date<span class="token punctuation">.</span><span class="token function">getHours</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  637. <span class="token string-property property">'m+'</span><span class="token operator">:</span> date<span class="token punctuation">.</span><span class="token function">getMinutes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  638. <span class="token string-property property">'s+'</span><span class="token operator">:</span> date<span class="token punctuation">.</span><span class="token function">getSeconds</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  639. <span class="token punctuation">}</span>
  640. <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(y+)</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>fmt<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  641. <span class="token comment">// $1 表示正则中的第一个,即(y+)</span>
  642. fmt <span class="token operator">=</span> fmt<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>RegExp<span class="token punctuation">.</span>$1<span class="token punctuation">,</span> date<span class="token punctuation">.</span><span class="token function">getFullYear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// replace 替换</span>
  643. <span class="token punctuation">}</span>
  644. <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> k <span class="token keyword">in</span> o<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  645. <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">RegExp</span><span class="token punctuation">(</span><span class="token string">'('</span><span class="token operator">+</span> k <span class="token operator">+</span><span class="token string">')'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>fmt<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  646. fmt <span class="token operator">=</span> fmt<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>RegExp<span class="token punctuation">.</span>$1<span class="token punctuation">,</span> o<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">1</span> <span class="token operator">?</span> <span class="token string">'0'</span> <span class="token operator">+</span> o<span class="token punctuation">[</span>k<span class="token punctuation">]</span> <span class="token operator">:</span> o<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">)</span>
  647. <span class="token punctuation">}</span>
  648. <span class="token punctuation">}</span>
  649. <span class="token keyword">return</span> fmt
  650. <span class="token punctuation">}</span>
  651. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><p>在组件引入js模块</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">import</span> formatTime <span class="token keyword">from</span> <span class="token string">'../../utils/formatTime.js'</span>
  652. 使用:
  653. <span class="token function">formatTime</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token string">'Wed Aug 28 2019 16:23:06 GMT+0800 (中国标准时间)'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  654. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h4 id="阻止事件冒泡-2"><a href="#阻止事件冒泡-2" class="header-anchor">#</a> 阻止事件冒泡</h4> <p><code>bind</code> 和 <code>catch</code> 都可以绑定事件,它们的区别是 <code>bind</code> 有事件冒泡,而 <code>catch</code> 没有</p> <h4 id="返回上一个页面并执行方法"><a href="#返回上一个页面并执行方法" class="header-anchor">#</a> 返回上一个页面并执行方法</h4> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.navigateBack.html" target="_blank" rel="noopener noreferrer">API<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code> <span class="token comment">// 返回博客页面,并刷新</span>
  655. wx<span class="token punctuation">.</span><span class="token function">navigateBack</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  656. <span class="token keyword">const</span> pages <span class="token operator">=</span> <span class="token function">getCurrentPages</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 获取当前页面栈</span>
  657. <span class="token keyword">const</span> prevPage <span class="token operator">=</span> pages<span class="token punctuation">[</span>pages<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">2</span><span class="token punctuation">]</span> <span class="token comment">// 取到上一个页面</span>
  658. prevPage<span class="token punctuation">.</span><span class="token function">onPullDownRefresh</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 执行上一个页面的方法 onPullDownRefresh</span>
  659. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h4 id="图片懒加载"><a href="#图片懒加载" class="header-anchor">#</a> 图片懒加载</h4> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/component/image.html" target="_blank" rel="noopener noreferrer">API<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <div class="language-html line-numbers-mode"><pre class="language-html"><code>给image标签设置 lazy-load 为 true
  660. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>image</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>img<span class="token punctuation">&quot;</span></span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{{item}}<span class="token punctuation">&quot;</span></span> <span class="token attr-name">lazy-load</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>true<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>image</span><span class="token punctuation">&gt;</span></span>
  661. .img {
  662. background: #eee;
  663. }
  664. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><strong>懒加载占位图可以给image设置背景图或背景色</strong></p> <h4 id="模糊查询"><a href="#模糊查询" class="header-anchor">#</a> 模糊查询</h4> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 获取博客列表</span>
  665. app<span class="token punctuation">.</span><span class="token function">router</span><span class="token punctuation">(</span><span class="token string">'blogList'</span><span class="token punctuation">,</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token parameter">ctx<span class="token punctuation">,</span> next</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  666. <span class="token keyword">const</span> keyword <span class="token operator">=</span> event<span class="token punctuation">.</span>keyword <span class="token comment">// 搜索关键字 调用接口时传递来的数据</span>
  667. <span class="token keyword">let</span> w <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
  668. <span class="token keyword">if</span> <span class="token punctuation">(</span>keyword<span class="token punctuation">.</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token string">''</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  669. w <span class="token operator">=</span> <span class="token punctuation">{</span>
  670. <span class="token literal-property property">content</span><span class="token operator">:</span> db<span class="token punctuation">.</span><span class="token function">RegExp</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token comment">// 正则</span>
  671. <span class="token literal-property property">regexp</span><span class="token operator">:</span> keyword<span class="token punctuation">,</span>
  672. <span class="token literal-property property">options</span><span class="token operator">:</span> <span class="token string">'i'</span> <span class="token comment">// i表示忽略大小写</span>
  673. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  674. <span class="token punctuation">}</span>
  675. <span class="token punctuation">}</span>
  676. <span class="token comment">// where查询条件 skip 从第几条开始查,limit 查几条数据,orderBy(排序字段,排序方式) 排序,排序方式desc降序/asc升序</span>
  677. ctx<span class="token punctuation">.</span>body <span class="token operator">=</span> <span class="token keyword">await</span> blogCollection<span class="token punctuation">.</span><span class="token function">where</span><span class="token punctuation">(</span>w<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">skip</span><span class="token punctuation">(</span>event<span class="token punctuation">.</span>start<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">limit</span><span class="token punctuation">(</span>event<span class="token punctuation">.</span>count<span class="token punctuation">)</span>
  678. <span class="token punctuation">.</span><span class="token function">orderBy</span><span class="token punctuation">(</span><span class="token string">'createTime'</span><span class="token punctuation">,</span> <span class="token string">'desc'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  679. <span class="token keyword">return</span> res<span class="token punctuation">.</span>data
  680. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  681. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  682. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h4 id="提升模糊查询的效率-添加索引-对数据量大的查询效果明显"><a href="#提升模糊查询的效率-添加索引-对数据量大的查询效果明显" class="header-anchor">#</a> 提升模糊查询的效率 (添加索引,对数据量大的查询效果明显)</h4> <p>云开发控制台 &gt; 数据库相应的集合 &gt; 索引管理 &gt; 添加索引 &gt; 输入自定义索引名称、该字段的值是否唯一、被查询的字段名、升序/降序 &gt; ok</p> <h4 id="小程序端调用云数据库"><a href="#小程序端调用云数据库" class="header-anchor">#</a> 小程序端调用云数据库</h4> <p>一般调用云数据库的操作都写在云函数内,其实小程序端也可以对数据库进行操作。</p> <p>小程序端一次最多只能查询20条数据,云函数端最多可查询100条数据,可使用多次查询拼接的方式突破限制。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 小程序端调用云数据库示例</span>
  683. <span class="token keyword">const</span> db <span class="token operator">=</span> wx<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span><span class="token function">database</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 初始化数据库</span>
  684. db<span class="token punctuation">.</span><span class="token function">collection</span><span class="token punctuation">(</span><span class="token string">'blog'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">orderBy</span><span class="token punctuation">(</span><span class="token string">'createTime'</span><span class="token punctuation">,</span><span class="token string">'deac'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  685. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span>
  686. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  687. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h4 id="云数据库权限管理-2"><a href="#云数据库权限管理-2" class="header-anchor">#</a> 云数据库权限管理</h4> <p><strong>注意:云控制台和服务端(云函数)始终有所有数据读写权限,</strong></p> <p><strong>但权限的管理仅对小程序端发起的请求有效。</strong></p> <ul><li><p>仅创建者可写,所有人可读 (适合于文章)</p></li> <li><p>仅创建者可读写 (适用于私密内容)</p></li> <li><p>仅管理端可写,所有人可读(适用于商品信息)</p></li> <li><p>仅管理端可读写(适用于后台敏感数据)</p></li></ul> <h4 id="数据库中1对n关系的三种设计方式"><a href="#数据库中1对n关系的三种设计方式" class="header-anchor">#</a> 数据库中1对N关系的三种设计方式</h4> <h5 id="第一种-n的数量较少-几十个以内"><a href="#第一种-n的数量较少-几十个以内" class="header-anchor">#</a> 第一种:N的数量较少 几十个以内</h5> <p><strong>1 条记录存储 N 个子数据</strong></p> <p>​ 如一条博客中,最多有9张图片,这9张图片可和其他数据放在一个记录中。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">[</span>
  688. <span class="token punctuation">{</span>
  689. <span class="token literal-property property">id</span><span class="token operator">:</span><span class="token operator">...</span>
  690. <span class="token literal-property property">img</span><span class="token operator">:</span><span class="token punctuation">[</span>
  691. <span class="token string">'...'</span><span class="token punctuation">,</span> <span class="token string">'...'</span><span class="token punctuation">,</span> <span class="token string">'...'</span><span class="token punctuation">,</span> <span class="token string">'...'</span><span class="token punctuation">,</span> <span class="token string">'...'</span><span class="token punctuation">,</span> <span class="token string">'...'</span><span class="token punctuation">,</span> <span class="token string">'...'</span><span class="token punctuation">,</span> <span class="token string">'...'</span><span class="token punctuation">,</span> <span class="token string">'...'</span>
  692. <span class="token punctuation">]</span>
  693. <span class="token punctuation">}</span>
  694. <span class="token punctuation">]</span>
  695. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h5 id="第二种-n的数量较多-几十到几百个"><a href="#第二种-n的数量较多-几十到几百个" class="header-anchor">#</a> 第二种:N的数量较多 几十到几百个</h5> <p><strong>1 存储 每个N的 id</strong></p> <p>可分两个数据库集合,</p> <p>一个为 '目录' 集合,存放 '详情' 集合下的每条数据的 id 目录</p> <p>一个为 '详情' 集合,每条数据对应一个单独的 id 和 详细数据</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>目录集合:
  696. <span class="token punctuation">[</span>
  697. <span class="token punctuation">{</span>
  698. <span class="token string-property property">'id'</span><span class="token operator">:</span><span class="token string">&quot;11&quot;</span><span class="token punctuation">,</span>
  699. <span class="token string-property property">'name'</span><span class="token operator">:</span> <span class="token string">'产品1'</span><span class="token punctuation">,</span>
  700. <span class="token string-property property">'xqs'</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'111'</span><span class="token punctuation">,</span><span class="token string">'222'</span><span class="token punctuation">,</span><span class="token string">'333'</span><span class="token punctuation">,</span> <span class="token operator">...</span> <span class="token punctuation">]</span> <span class="token comment">// 存放 详情集合 中的每条数据 id</span>
  701. <span class="token punctuation">}</span>
  702. <span class="token punctuation">]</span>
  703. 详情集合:
  704. <span class="token punctuation">[</span>
  705. <span class="token punctuation">{</span><span class="token string-property property">'id'</span><span class="token operator">:</span><span class="token string">&quot;111&quot;</span><span class="token punctuation">,</span><span class="token literal-property property">name</span><span class="token operator">:</span><span class="token string">'零件1'</span><span class="token punctuation">,</span><span class="token literal-property property">title</span><span class="token operator">:</span><span class="token string">'...'</span> <span class="token operator">...</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
  706. <span class="token punctuation">{</span><span class="token string-property property">'id'</span><span class="token operator">:</span><span class="token string">&quot;222&quot;</span><span class="token punctuation">,</span><span class="token literal-property property">name</span><span class="token operator">:</span><span class="token string">'零件2'</span><span class="token punctuation">,</span><span class="token literal-property property">title</span><span class="token operator">:</span><span class="token string">'...'</span> <span class="token operator">...</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
  707. <span class="token punctuation">{</span><span class="token string-property property">'id'</span><span class="token operator">:</span><span class="token string">&quot;333&quot;</span><span class="token punctuation">,</span><span class="token literal-property property">name</span><span class="token operator">:</span><span class="token string">'零件3'</span><span class="token punctuation">,</span><span class="token literal-property property">title</span><span class="token operator">:</span><span class="token string">'...'</span> <span class="token operator">...</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
  708. <span class="token operator">...</span>
  709. <span class="token punctuation">]</span>
  710. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><p>如歌单列表,与歌曲详情的数据组合设计。</p> <h5 id="第三种-n的数量巨大-几百成千上万个"><a href="#第三种-n的数量巨大-几百成千上万个" class="header-anchor">#</a> 第三种:N的数量巨大 几百成千上万个</h5> <p><strong>每个 N 都存储 1 的 id</strong></p> <p>如新浪博客中的一条博客下面有几千条评论</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>一条新浪博客:
  711. <span class="token punctuation">[</span><span class="token punctuation">{</span>
  712. <span class="token string-property property">'id'</span><span class="token operator">:</span><span class="token string">'11'</span><span class="token punctuation">,</span>
  713. <span class="token string-property property">'content'</span><span class="token operator">:</span><span class="token string">'博客内容'</span>
  714. <span class="token operator">...</span>
  715. <span class="token punctuation">}</span><span class="token punctuation">]</span>
  716. 上千条评价:
  717. <span class="token punctuation">[</span>
  718. <span class="token punctuation">{</span>
  719. <span class="token string-property property">'id'</span><span class="token operator">:</span><span class="token string">'111111'</span>
  720. <span class="token string-property property">'blogId'</span><span class="token operator">:</span><span class="token string">'11'</span><span class="token punctuation">,</span> <span class="token comment">// 这个id对应的是那一条博客的id</span>
  721. <span class="token string-property property">'content'</span><span class="token operator">:</span> <span class="token string">'评价内容1'</span>
  722. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  723. <span class="token punctuation">{</span>
  724. <span class="token string-property property">'id'</span><span class="token operator">:</span><span class="token string">'222222'</span>
  725. <span class="token string-property property">'blogId'</span><span class="token operator">:</span><span class="token string">'11'</span><span class="token punctuation">,</span> <span class="token comment">// 这个id对应的是那一条博客的id</span>
  726. <span class="token string-property property">'content'</span><span class="token operator">:</span> <span class="token string">'评价内容2'</span>
  727. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  728. <span class="token punctuation">{</span>
  729. <span class="token string-property property">'id'</span><span class="token operator">:</span><span class="token string">'33333'</span>
  730. <span class="token string-property property">'blogId'</span><span class="token operator">:</span><span class="token string">'11'</span><span class="token punctuation">,</span> <span class="token comment">// 这个id对应的是那一条博客的id</span>
  731. <span class="token string-property property">'content'</span><span class="token operator">:</span> <span class="token string">'评价内容3'</span>
  732. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  733. <span class="token operator">...</span>
  734. <span class="token punctuation">]</span>
  735. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br></div></div><h4 id="云调用"><a href="#云调用" class="header-anchor">#</a> 云调用</h4> <p>通过云函数调用服务端的开发接口</p> <p>这些接口如:模板消息推送、生成小程序码...</p> <h4 id="模板消息推送"><a href="#模板消息推送" class="header-anchor">#</a> 模板消息推送</h4> <p><strong>1、使用from表单才能触发消息推送,并设置report-submit=&quot;true&quot;</strong></p> <div class="language-html line-numbers-mode"><pre class="language-html"><code>
  736. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>form</span> <span class="token attr-name">slot</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>modal-content<span class="token punctuation">&quot;</span></span> <span class="token attr-name">report-submit</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>true<span class="token punctuation">&quot;</span></span> <span class="token attr-name"><span class="token namespace">bind:</span>submit</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>onSend<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
  737. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>textarea</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>content<span class="token punctuation">&quot;</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>comment-content<span class="token punctuation">&quot;</span></span> <span class="token attr-name">placeholder</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>写评论<span class="token punctuation">&quot;</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{{content}}<span class="token punctuation">&quot;</span></span> <span class="token attr-name">fixed</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>true<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>textarea</span><span class="token punctuation">&gt;</span></span>
  738. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>send<span class="token punctuation">&quot;</span></span> <span class="token attr-name">form-type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>submit<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>发送<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span>
  739. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>form</span><span class="token punctuation">&gt;</span></span>
  740. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><strong>2、需要到微信公众平台做相应的设置:</strong></p> <p>微信公众平台 &gt; 功能 &gt; 模板消息 &gt; 添加模板 &gt; 选择相应的模板&gt; 添加成功后会有一个模板ID</p> <p><strong>3、新建一个云函数,用于云调用。在该云函数下新建配置文件:config.json ,用于配置权限</strong></p> <p>config.json :</p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
  741. <span class="token property">&quot;permissions&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
  742. <span class="token property">&quot;openapi&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
  743. <span class="token string">&quot;templateMessage.send&quot;</span>
  744. <span class="token punctuation">]</span>
  745. <span class="token punctuation">}</span>
  746. <span class="token punctuation">}</span>
  747. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>云函数设置消息推送:</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 云函数入口函数</span>
  748. exports<span class="token punctuation">.</span><span class="token function-variable function">main</span> <span class="token operator">=</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token parameter">event<span class="token punctuation">,</span> context</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  749. <span class="token comment">// 获取openid</span>
  750. <span class="token keyword">const</span> <span class="token punctuation">{</span> <span class="token constant">OPENID</span> <span class="token punctuation">}</span> <span class="token operator">=</span> cloud<span class="token punctuation">.</span><span class="token function">getWXContext</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  751. <span class="token comment">// 模板推送消息</span>
  752. <span class="token keyword">const</span> result <span class="token operator">=</span> <span class="token keyword">await</span> cloud<span class="token punctuation">.</span>openapi<span class="token punctuation">.</span>templateMessage<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  753. <span class="token literal-property property">touser</span><span class="token operator">:</span> <span class="token constant">OPENID</span><span class="token punctuation">,</span>
  754. <span class="token literal-property property">page</span><span class="token operator">:</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">/pages/blog-comment/blog-comment?blogId=</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>event<span class="token punctuation">.</span>blogId<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">,</span> <span class="token comment">// 用户点击推送消息打开的页面</span>
  755. <span class="token literal-property property">data</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token comment">// 模板的内容,keyword为在公众平台设置模板时对应的字段</span>
  756. <span class="token literal-property property">keyword1</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token comment">// 评价内容</span>
  757. <span class="token literal-property property">value</span><span class="token operator">:</span> event<span class="token punctuation">.</span>context
  758. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  759. <span class="token literal-property property">keyword2</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token comment">// 评价时间</span>
  760. <span class="token literal-property property">value</span><span class="token operator">:</span> event<span class="token punctuation">.</span>time
  761. <span class="token punctuation">}</span>
  762. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  763. <span class="token literal-property property">templateId</span><span class="token operator">:</span> <span class="token string">'LNwKMcYwlz-0HabgBhmZi6CWZrlNSBiNJ2h0SMorcxQ'</span><span class="token punctuation">,</span> <span class="token comment">// 模板id,到公众平台模板消息上获取</span>
  764. <span class="token literal-property property">formId</span><span class="token operator">:</span> event<span class="token punctuation">.</span>formId <span class="token comment">// 触发消息推送的form表单的id</span>
  765. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  766. <span class="token keyword">return</span> result
  767. <span class="token punctuation">}</span>
  768. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><p><strong>4、在提交表单事件完成后调用消息推送云函数</strong></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code> wx<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span><span class="token function">callFunction</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  769. <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'sendMessage'</span><span class="token punctuation">,</span>
  770. <span class="token literal-property property">data</span><span class="token operator">:</span> <span class="token punctuation">{</span>
  771. content<span class="token punctuation">,</span>
  772. formId<span class="token punctuation">,</span>
  773. <span class="token literal-property property">blogId</span><span class="token operator">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>properties<span class="token punctuation">.</span>blogId
  774. <span class="token punctuation">}</span>
  775. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  776. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span>
  777. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  778. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h4 id="云函数多集合查询数据库"><a href="#云函数多集合查询数据库" class="header-anchor">#</a> 云函数多集合查询数据库</h4> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 博客详情(博客内容、评论)</span>
  779. app<span class="token punctuation">.</span><span class="token function">router</span><span class="token punctuation">(</span><span class="token string">'blogDetail'</span><span class="token punctuation">,</span> <span class="token keyword">async</span><span class="token punctuation">(</span><span class="token parameter">ctx<span class="token punctuation">,</span> next</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  780. <span class="token keyword">let</span> blogId <span class="token operator">=</span> event<span class="token punctuation">.</span>blogId
  781. <span class="token comment">// 博客内容</span>
  782. <span class="token keyword">let</span> detail <span class="token operator">=</span> <span class="token keyword">await</span> blogCollection<span class="token punctuation">.</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  783. <span class="token literal-property property">_id</span><span class="token operator">:</span> blogId
  784. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  785. <span class="token keyword">return</span> res<span class="token punctuation">.</span>data
  786. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  787. <span class="token comment">// 评论查询</span>
  788. <span class="token keyword">const</span> countResult <span class="token operator">=</span> <span class="token keyword">await</span> blogCollection<span class="token punctuation">.</span><span class="token function">count</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  789. <span class="token keyword">const</span> total <span class="token operator">=</span> countResult<span class="token punctuation">.</span>total
  790. <span class="token keyword">let</span> commentList <span class="token operator">=</span> <span class="token punctuation">{</span>
  791. <span class="token literal-property property">data</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  792. <span class="token punctuation">}</span>
  793. <span class="token keyword">if</span> <span class="token punctuation">(</span>total <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  794. <span class="token comment">// 突破100条限制</span>
  795. <span class="token keyword">const</span> batchTimes <span class="token operator">=</span> Math<span class="token punctuation">.</span><span class="token function">ceil</span><span class="token punctuation">(</span>total <span class="token operator">/</span> <span class="token constant">MAX_LIMIT</span><span class="token punctuation">)</span>
  796. <span class="token keyword">const</span> tasks <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  797. <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> batchTimes<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  798. <span class="token keyword">let</span> promise <span class="token operator">=</span> db<span class="token punctuation">.</span><span class="token function">collection</span><span class="token punctuation">(</span><span class="token string">'blog-comment'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">skip</span><span class="token punctuation">(</span>i <span class="token operator">*</span> <span class="token constant">MAX_LIMIT</span><span class="token punctuation">)</span>
  799. <span class="token punctuation">.</span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token constant">MAX_LIMIT</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  800. blogId
  801. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">orderBy</span><span class="token punctuation">(</span><span class="token string">'createTime'</span><span class="token punctuation">,</span> <span class="token string">'desc'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  802. tasks<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>promise<span class="token punctuation">)</span>
  803. <span class="token punctuation">}</span>
  804. <span class="token keyword">if</span> <span class="token punctuation">(</span>tasks<span class="token punctuation">.</span>length <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  805. commentList <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">await</span> Promise<span class="token punctuation">.</span><span class="token function">all</span><span class="token punctuation">(</span>tasks<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">acc<span class="token punctuation">,</span> cur</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  806. <span class="token keyword">return</span> <span class="token punctuation">{</span>
  807. <span class="token literal-property property">data</span><span class="token operator">:</span> acc<span class="token punctuation">.</span>data<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>cur<span class="token punctuation">.</span>data<span class="token punctuation">)</span>
  808. <span class="token punctuation">}</span>
  809. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  810. <span class="token punctuation">}</span>
  811. <span class="token punctuation">}</span>
  812. ctx<span class="token punctuation">.</span>body <span class="token operator">=</span> <span class="token punctuation">{</span>
  813. detail<span class="token punctuation">,</span>
  814. commentList
  815. <span class="token punctuation">}</span>
  816. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  817. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br></div></div><h4 id="分享功能"><a href="#分享功能" class="header-anchor">#</a> 分享功能</h4> <p>分享功能需要button标签,设置open-type=&quot;share&quot;</p> <div class="language-html line-numbers-mode"><pre class="language-html"><code>
  818. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">open-type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>share<span class="token punctuation">&quot;</span></span> <span class="token attr-name">data-blogid</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{{blogId}}<span class="token punctuation">&quot;</span></span> <span class="token attr-name">data-blog</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{{blog}}<span class="token punctuation">&quot;</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>share-btn<span class="token punctuation">&quot;</span></span> <span class="token attr-name">hover-class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>share-hover<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
  819. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>i</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>iconfont icon-fenxiang icon<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>i</span><span class="token punctuation">&gt;</span></span>
  820. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>text</span><span class="token punctuation">&gt;</span></span>分享<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>text</span><span class="token punctuation">&gt;</span></span>
  821. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span>
  822. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>在js中有onShareAppMessage方法,点击button会自动执行此方法</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function-variable function">onShareAppMessage</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">event</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  823. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>event<span class="token punctuation">)</span>
  824. <span class="token comment">// 对分享卡片的设置</span>
  825. <span class="token keyword">let</span> blogObj <span class="token operator">=</span> event<span class="token punctuation">.</span>target<span class="token punctuation">.</span>dataset<span class="token punctuation">.</span>blog
  826. <span class="token keyword">return</span> <span class="token punctuation">{</span>
  827. <span class="token literal-property property">title</span><span class="token operator">:</span> blogObj<span class="token punctuation">.</span>content<span class="token punctuation">,</span>
  828. <span class="token literal-property property">path</span><span class="token operator">:</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">/pages/blog-comment/blog-comment?blogId=</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>blogObj<span class="token punctuation">.</span>_id<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">,</span>
  829. <span class="token comment">// imageUrl: '' // 自定义图片,不支持云存储的图片</span>
  830. <span class="token punctuation">}</span>
  831. <span class="token punctuation">}</span>
  832. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h4 id="不同场景获取用户信息的方式"><a href="#不同场景获取用户信息的方式" class="header-anchor">#</a> 不同场景获取用户信息的方式</h4> <h5 id="场景一-只想在界面上显示自己的昵称和头像"><a href="#场景一-只想在界面上显示自己的昵称和头像" class="header-anchor">#</a> 场景一:只想在界面上显示自己的昵称和头像</h5> <p>以组件的方式:根据type类型获取不同用户数据</p> <p><strong>该方式不需要授权,只能用于在wxml显示自己的信息</strong></p> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/component/open-data.html" target="_blank" rel="noopener noreferrer">open-data<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>open-data</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>userAvatarUrl<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>open-data</span><span class="token punctuation">&gt;</span></span>
  833. <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>open-data</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>userNickName<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>open-data</span><span class="token punctuation">&gt;</span></span>
  834. ...
  835. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h5 id="场景二-在js中获取用户信息"><a href="#场景二-在js中获取用户信息" class="header-anchor">#</a> 场景二:在JS中获取用户信息</h5> <p>该方式要在用户授权以后才能获取用户信息</p> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserInfo.html" target="_blank" rel="noopener noreferrer">wx.getUserInfo<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>wx<span class="token punctuation">.</span><span class="token function">getUserInfo</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  836. <span class="token function-variable function">success</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  837. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span>
  838. <span class="token punctuation">}</span>
  839. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  840. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>在未授权的情况下需要用户先授权:</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 判断用户是否授权</span>
  841. wx<span class="token punctuation">.</span><span class="token function">getSetting</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  842. <span class="token function-variable function">success</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span> <span class="token comment">// 这里使用箭头函数可改变内部this指向为外部的this</span>
  843. <span class="token keyword">if</span> <span class="token punctuation">(</span>res<span class="token punctuation">.</span>authSetting<span class="token punctuation">[</span><span class="token string">'scope.userInfo'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 已授权</span>
  844. wx<span class="token punctuation">.</span><span class="token function">getUserInfo</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token comment">// 获取用户信息</span>
  845. <span class="token function-variable function">success</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span> <span class="token comment">// 这里使用箭头函数可改变内部this指向为外部的this</span>
  846. app<span class="token punctuation">.</span><span class="token function">setGlobalData</span><span class="token punctuation">(</span><span class="token string">'userInfo'</span><span class="token punctuation">,</span> res<span class="token punctuation">.</span>userInfo<span class="token punctuation">)</span> <span class="token comment">// 设置app全局属性</span>
  847. <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">onLoginSuccess</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  848. <span class="token literal-property property">detail</span><span class="token operator">:</span> res<span class="token punctuation">.</span>userInfo
  849. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  850. <span class="token punctuation">}</span>
  851. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  852. <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token comment">// 未授权</span>
  853. <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setData</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token comment">// 打开弹出层,显示获取用户信息按钮</span>
  854. <span class="token literal-property property">modalShow</span><span class="token operator">:</span> <span class="token boolean">true</span>
  855. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  856. <span class="token punctuation">}</span>
  857. <span class="token punctuation">}</span>
  858. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  859. 授权按钮
  860. <span class="token operator">&lt;</span>button <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">&quot;login&quot;</span> open<span class="token operator">-</span>type<span class="token operator">=</span><span class="token string">&quot;getUserInfo&quot;</span> bindgetuserinfo<span class="token operator">=</span><span class="token string">&quot;onGetUserInfo&quot;</span><span class="token operator">&gt;</span>获取微信授权信息<span class="token operator">&lt;</span><span class="token operator">/</span>button<span class="token operator">&gt;</span>
  861. <span class="token function">onGetUserInfo</span><span class="token punctuation">(</span><span class="token parameter">event</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 获取用户信息</span>
  862. <span class="token keyword">const</span> userInfo <span class="token operator">=</span> event<span class="token punctuation">.</span>detail<span class="token punctuation">.</span>userInfo
  863. <span class="token keyword">if</span> <span class="token punctuation">(</span>userInfo<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 用户允许授权</span>
  864. <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setData</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  865. <span class="token literal-property property">modalShow</span><span class="token operator">:</span> <span class="token boolean">false</span>
  866. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  867. <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">triggerEvent</span><span class="token punctuation">(</span><span class="token string">'loginSuccess'</span><span class="token punctuation">,</span> userInfo<span class="token punctuation">)</span> <span class="token comment">// 给父组件传用户数据</span>
  868. <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token comment">// 用户拒绝授权</span>
  869. <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">triggerEvent</span><span class="token punctuation">(</span><span class="token string">'loginFail'</span><span class="token punctuation">)</span>
  870. <span class="token punctuation">}</span>
  871. <span class="token punctuation">}</span>
  872. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br></div></div><blockquote><p>注意:上面这种方式没有获取到openId</p></blockquote> <h5 id="场景三-获取openid"><a href="#场景三-获取openid" class="header-anchor">#</a> 场景三:获取openId</h5> <p><strong>获取openId不需要用户授权</strong></p> <p>1、传统开发方式获取openId,后台服务器由自己开发,没使用云开发</p> <p>小程序端 微信服务器 后端服务器</p> <p>步骤:</p> <p>小程序端 调用 wx.login 向微信服务器 获取code</p> <p>小程序端 调用 wx.request 将 code 传递给 后端服务器</p> <p>后端服务器 使用code 向微信服务器 换取openid和session_key</p> <p>后端服务器 将openid 发送给 小程序端</p> <p>2、云开发方式获取openId</p> <p>云函数login中</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 获取 WX Context (微信调用上下文),包括 OPENID、APPID、及 UNIONID(需满足 UNIONID 获取条件)</span>
  873. <span class="token keyword">const</span> wxContext <span class="token operator">=</span> cloud<span class="token punctuation">.</span><span class="token function">getWXContext</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  874. <span class="token keyword">return</span> <span class="token punctuation">{</span>
  875. event<span class="token punctuation">,</span>
  876. <span class="token literal-property property">openid</span><span class="token operator">:</span> wxContext<span class="token punctuation">.</span><span class="token constant">OPENID</span><span class="token punctuation">,</span>
  877. <span class="token literal-property property">appid</span><span class="token operator">:</span> wxContext<span class="token punctuation">.</span><span class="token constant">APPID</span><span class="token punctuation">,</span>
  878. <span class="token literal-property property">unionid</span><span class="token operator">:</span> wxContext<span class="token punctuation">.</span><span class="token constant">UNIONID</span><span class="token punctuation">,</span>
  879. <span class="token punctuation">}</span>
  880. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><div class="language-js line-numbers-mode"><pre class="language-js"><code>普通按钮
  881. <span class="token operator">&lt;</span>button bindtap<span class="token operator">=</span><span class="token string">&quot;getOpenid&quot;</span><span class="token operator">&gt;</span>获取openid<span class="token operator">&lt;</span><span class="token operator">/</span>button<span class="token operator">&gt;</span>
  882. <span class="token function">getOpenid</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  883. wx<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span><span class="token function">callFunction</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  884. <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'login'</span>
  885. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  886. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span>
  887. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  888. <span class="token punctuation">}</span>
  889. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><blockquote><p>openid 在小程序和公众号下是不一样的</p> <p>unionid 在小程序和公众号下都是一样的</p></blockquote> <h2 id="《我的》页面"><a href="#《我的》页面" class="header-anchor">#</a> 《我的》页面</h2> <p>json文件</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string-property property">&quot;navigationBarTitleText&quot;</span><span class="token operator">:</span> <span class="token string">&quot;我的&quot;</span><span class="token punctuation">,</span>
  890. <span class="token string-property property">&quot;disableScroll&quot;</span><span class="token operator">:</span> <span class="token boolean">true</span> <span class="token comment">// 使页面无法滚动</span>
  891. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="导航页面链接跳转"><a href="#导航页面链接跳转" class="header-anchor">#</a> 导航页面链接跳转</h4> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/component/navigator.html" target="_blank" rel="noopener noreferrer">navigator<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h4 id="背景图片"><a href="#背景图片" class="header-anchor">#</a> 背景图片</h4> <p>wxss背景图片不支持本地相对路径的图片,只支持网络图片和base64图片</p> <p>建议使用base64图片,图片文件最好不要太大。</p> <h4 id="每个页面都有的page标签"><a href="#每个页面都有的page标签" class="header-anchor">#</a> 每个页面都有的page标签</h4> <div class="language-js line-numbers-mode"><pre class="language-js"><code>page <span class="token punctuation">{</span>
  892. background<span class="token operator">-</span>color<span class="token operator">:</span> #f1f1f1<span class="token punctuation">;</span>
  893. <span class="token punctuation">}</span>
  894. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h4 id="播放历史与本地存储"><a href="#播放历史与本地存储" class="header-anchor">#</a> 播放历史与本地存储</h4> <p>方案一:播放历史存储在数据库当中,这样在不同设备访问都可查看播放历史。读取速度相对较慢</p> <p>方案二:播放历史存储在本地,仅当前设备可查看播放历史。读取速度较快</p> <p>本项目采用本地存储:</p> <p>使用openid作为本地存储的key,播放历史存入value</p> <p>在app.js中获取openid,即打开小程序就获取openid。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// app.js</span>
  895. <span class="token function-variable function">onLaunch</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  896. <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getOpenid</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 获取openid并存储</span>
  897. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  898. <span class="token function">getOpenid</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 获取openid并存储</span>
  899. wx<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span><span class="token function">callFunction</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  900. <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'login'</span>
  901. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  902. <span class="token keyword">const</span> openid <span class="token operator">=</span> res<span class="token punctuation">.</span>result<span class="token punctuation">.</span>openid
  903. <span class="token keyword">this</span><span class="token punctuation">.</span>globalData<span class="token punctuation">.</span>openid <span class="token operator">=</span> openid <span class="token comment">// 保存到全局变量</span>
  904. <span class="token keyword">if</span> <span class="token punctuation">(</span>wx<span class="token punctuation">.</span><span class="token function">getStorageSync</span><span class="token punctuation">(</span>openid<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token string">''</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 该用户从未打开过小程序,未存储过openid在本地</span>
  905. wx<span class="token punctuation">.</span><span class="token function">setStorageSync</span><span class="token punctuation">(</span>openid<span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">// 存储openid到本地</span>
  906. <span class="token punctuation">}</span>
  907. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  908. <span class="token punctuation">}</span>
  909. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>歌曲播放时</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 保存播放历史到本地存储</span>
  910. <span class="token function">savePlayHistory</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  911. <span class="token keyword">const</span> currentSong <span class="token operator">=</span> musiclist<span class="token punctuation">[</span>nowPlayingIndex<span class="token punctuation">]</span> <span class="token comment">// 当前播放歌曲</span>
  912. <span class="token keyword">const</span> openid <span class="token operator">=</span> app<span class="token punctuation">.</span>globalData<span class="token punctuation">.</span>openid <span class="token comment">// 从全局属性获取openid</span>
  913. <span class="token keyword">const</span> playHistory <span class="token operator">=</span> wx<span class="token punctuation">.</span><span class="token function">getStorageSync</span><span class="token punctuation">(</span>openid<span class="token punctuation">)</span> <span class="token comment">// 从本地存储获取播放历史数组</span>
  914. <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> len <span class="token operator">=</span> playHistory<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i <span class="token operator">&lt;</span> len<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  915. <span class="token keyword">if</span> <span class="token punctuation">(</span>playHistory<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>id <span class="token operator">===</span> currentSong<span class="token punctuation">.</span>id<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 当前播放歌曲已存在播放历史中</span>
  916. playHistory<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// 删除原纪录</span>
  917. <span class="token keyword">break</span>
  918. <span class="token punctuation">}</span>
  919. <span class="token punctuation">}</span>
  920. playHistory<span class="token punctuation">.</span><span class="token function">unshift</span><span class="token punctuation">(</span>currentSong<span class="token punctuation">)</span> <span class="token comment">// 在数组开头插入</span>
  921. wx<span class="token punctuation">.</span><span class="token function">setStorage</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token comment">// 存入本地</span>
  922. <span class="token literal-property property">key</span><span class="token operator">:</span> openid<span class="token punctuation">,</span>
  923. <span class="token literal-property property">data</span><span class="token operator">:</span> playHistory
  924. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  925. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  926. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><p>播放历史页面获取</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function-variable function">onLoad</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">options</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  927. <span class="token keyword">const</span> openid <span class="token operator">=</span> app<span class="token punctuation">.</span>globalData<span class="token punctuation">.</span>openid <span class="token comment">//从全局属性获取openid</span>
  928. <span class="token keyword">const</span> playHistory <span class="token operator">=</span> wx<span class="token punctuation">.</span><span class="token function">getStorageSync</span><span class="token punctuation">(</span>openid<span class="token punctuation">)</span> <span class="token comment">// 读取本地播放历史数据</span>
  929. <span class="token keyword">if</span> <span class="token punctuation">(</span>playHistory<span class="token punctuation">.</span>length <span class="token operator">!==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 有播放历史</span>
  930. <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setData</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  931. playHistory
  932. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  933. wx<span class="token punctuation">.</span><span class="token function">setStorage</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token comment">// storage里把musiclist(播放列表)的内容换成播放历史的列表</span>
  934. <span class="token literal-property property">key</span><span class="token operator">:</span> <span class="token string">'musiclist'</span><span class="token punctuation">,</span>
  935. <span class="token literal-property property">data</span><span class="token operator">:</span> playHistory<span class="token punctuation">,</span>
  936. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  937. <span class="token punctuation">}</span>
  938. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  939. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><h4 id="我的发现"><a href="#我的发现" class="header-anchor">#</a> 我的发现</h4> <p>代码分别演示了从云函数和小程序端获取数据,从小程序端获取数据享有<strong>权限管理</strong>的能力,不需要传openid。</p> <h4 id="小程序码"><a href="#小程序码" class="header-anchor">#</a> 小程序码</h4> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/qr-code.html" target="_blank" rel="noopener noreferrer">获取小程序码<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <p>本项目演示使用<a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html" target="_blank" rel="noopener noreferrer">接口 B:适用于需要的码数量极多的业务场景<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> <code>云调用</code> 的方式。</p> <p>步骤:</p> <ul><li><p>创建云函数 gteQRCode</p></li> <li><p>gteQRCode云函数下创建config.json配置权限,代码如下:</p></li></ul> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
  940. <span class="token property">&quot;permissions&quot;</span><span class="token operator">:</span><span class="token punctuation">{</span>
  941. <span class="token property">&quot;openapi&quot;</span><span class="token operator">:</span><span class="token punctuation">[</span>
  942. <span class="token string">&quot;wxacode.getUnlimited&quot;</span>
  943. <span class="token punctuation">]</span>
  944. <span class="token punctuation">}</span>
  945. <span class="token punctuation">}</span>
  946. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 云函数入口函数</span>
  947. exports<span class="token punctuation">.</span><span class="token function-variable function">main</span> <span class="token operator">=</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token parameter">event<span class="token punctuation">,</span> context</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  948. <span class="token keyword">const</span> wxContext <span class="token operator">=</span> cloud<span class="token punctuation">.</span><span class="token function">getWXContext</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  949. <span class="token keyword">const</span> result <span class="token operator">=</span> <span class="token keyword">await</span> cloud<span class="token punctuation">.</span>openapi<span class="token punctuation">.</span>wxacode<span class="token punctuation">.</span><span class="token function">getUnlimited</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  950. <span class="token literal-property property">scene</span><span class="token operator">:</span> wxContext<span class="token punctuation">.</span><span class="token constant">OPENID</span><span class="token punctuation">,</span> <span class="token comment">// 链接参数 不一定传openid,可传其他任意数据,然后通过此数据,在别人扫码进入时就可用于判断</span>
  951. <span class="token comment">// page: &quot;pages/blog/blog&quot; // 默认进入主页</span>
  952. <span class="token comment">// lineColor: { // 线条颜色</span>
  953. <span class="token comment">// 'r': 211,</span>
  954. <span class="token comment">// 'g': 60,</span>
  955. <span class="token comment">// 'b': 57</span>
  956. <span class="token comment">// },</span>
  957. <span class="token comment">// isHyaline: true // 是否透明</span>
  958. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  959. <span class="token comment">// result为二进制数据, 先上传到云存储</span>
  960. <span class="token comment">// 上传云存储</span>
  961. <span class="token keyword">const</span> upload <span class="token operator">=</span> <span class="token keyword">await</span> cloud<span class="token punctuation">.</span><span class="token function">uploadFile</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  962. <span class="token literal-property property">cloudPath</span><span class="token operator">:</span> <span class="token string">'qrcode/qrcode'</span> <span class="token operator">+</span> Date<span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'.png'</span><span class="token punctuation">,</span>
  963. <span class="token literal-property property">fileContent</span><span class="token operator">:</span> result<span class="token punctuation">.</span>buffer
  964. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  965. <span class="token keyword">return</span> upload<span class="token punctuation">.</span>fileID
  966. <span class="token punctuation">}</span>
  967. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><h5 id="判断是从扫码小程序码进入-以及参数获取"><a href="#判断是从扫码小程序码进入-以及参数获取" class="header-anchor">#</a> 判断是从扫码小程序码进入,以及参数获取</h5> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 在从小程序码进入的页面js,onLoad方法中,</span>
  968. <span class="token function-variable function">onLoad</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">options</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  969. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>options<span class="token punctuation">.</span>scene<span class="token punctuation">)</span> <span class="token comment">// 获取到小程序码进入的参数</span>
  970. <span class="token punctuation">}</span>
  971. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="版本更新检测"><a href="#版本更新检测" class="header-anchor">#</a> 版本更新检测</h3> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// app.js</span>
  972. <span class="token function-variable function">onLaunch</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">options</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  973. <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">checkUpate</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  974. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  975. <span class="token function">checkUpate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  976. <span class="token keyword">const</span> updateManager <span class="token operator">=</span> wx<span class="token punctuation">.</span><span class="token function">getUpdateManager</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  977. <span class="token comment">// 检测版本更新</span>
  978. updateManager<span class="token punctuation">.</span><span class="token function">onCheckForUpdate</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span><span class="token punctuation">{</span>
  979. <span class="token keyword">if</span> <span class="token punctuation">(</span>res<span class="token punctuation">.</span>hasUpdate<span class="token punctuation">)</span><span class="token punctuation">{</span>
  980. updateManager<span class="token punctuation">.</span><span class="token function">onUpdateReady</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span><span class="token punctuation">{</span>
  981. wx<span class="token punctuation">.</span><span class="token function">showModal</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  982. <span class="token literal-property property">title</span><span class="token operator">:</span> <span class="token string">'更新提示'</span><span class="token punctuation">,</span>
  983. <span class="token literal-property property">content</span><span class="token operator">:</span> <span class="token string">'新版本已经准备好,是否重启应用'</span><span class="token punctuation">,</span>
  984. <span class="token function">success</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  985. <span class="token keyword">if</span><span class="token punctuation">(</span>res<span class="token punctuation">.</span>confirm<span class="token punctuation">)</span><span class="token punctuation">{</span>
  986. updateManager<span class="token punctuation">.</span><span class="token function">applyUpdate</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  987. <span class="token punctuation">}</span>
  988. <span class="token punctuation">}</span>
  989. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  990. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  991. <span class="token punctuation">}</span>
  992. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  993. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  994. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><h3 id="性能优化"><a href="#性能优化" class="header-anchor">#</a> 性能优化</h3> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/framework/performance/tips.html" target="_blank" rel="noopener noreferrer">官网文档优化建议<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <p>使用开发者工具的调试器,Audits进行评分,然后根据提示针对项目进行优化。</p> <h3 id="场景值scene的作用与应用场景"><a href="#场景值scene的作用与应用场景" class="header-anchor">#</a> 场景值scene的作用与应用场景</h3> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/framework/app-service/scene.html" target="_blank" rel="noopener noreferrer">场景值<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <p>场景值用来描述用户进入小程序的路径。完整场景值的含义请查看<a href="https://developers.weixin.qq.com/miniprogram/dev/reference/scene-list.html" target="_blank" rel="noopener noreferrer">场景值列表<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>。</p> <p>可根据不同场景进入实现不同业务处理,比如一个点餐小程序,店家内贴了小程序码,用户通过扫码进入,可立即进入点餐页面,等等</p> <p>在app.js中的onLaunch(options) 、onShow(options),options包含scene场景值</p> <p><strong>开发者工具中,切后台,可模拟进入场景。</strong></p> <h3 id="小程序的-seo-页面收录sitemap"><a href="#小程序的-seo-页面收录sitemap" class="header-anchor">#</a> 小程序的&quot;SEO&quot;---页面收录sitemap</h3> <p>在app.js的同级目录下有sitemap.json文件,用于配置收录规则</p> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/framework/sitemap.html" target="_blank" rel="noopener noreferrer">stiemap配置<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <p>作用:</p> <p>使小程序搜索可根据小程序的内容进行搜索到</p> <p>使用方法:</p> <p>1、在微信公众平台,小程序信息 &gt; 页面收录设置 &gt; 打开 (默认是已开启)</p> <p>2、打开sitemap.json文件,配置收录规则</p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
  995. <span class="token property">&quot;desc&quot;</span><span class="token operator">:</span> <span class="token string">&quot;关于本文件的更多信息,请参考文档 https://developers.weixin.qq.com/miniprogram/dev/framework/sitemap.html&quot;</span><span class="token punctuation">,</span>
  996. <span class="token property">&quot;rules&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span> <span class="token comment">// 收录规则</span>
  997. <span class="token property">&quot;action&quot;</span><span class="token operator">:</span> <span class="token string">&quot;allow&quot;</span><span class="token punctuation">,</span><span class="token comment">// 是否被收录,allow允许被收录,disallow不允许</span>
  998. <span class="token property">&quot;page&quot;</span><span class="token operator">:</span> <span class="token string">&quot;*&quot;</span> <span class="token comment">// *星号表示所有页面都被收录</span>
  999. <span class="token punctuation">}</span><span class="token punctuation">]</span>
  1000. <span class="token punctuation">}</span>
  1001. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
  1002. <span class="token property">&quot;desc&quot;</span><span class="token operator">:</span> <span class="token string">&quot;关于本文件的更多信息,请参考文档 https://developers.weixin.qq.com/miniprogram/dev/framework/sitemap.html&quot;</span><span class="token punctuation">,</span>
  1003. <span class="token property">&quot;rules&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span> <span class="token comment">// 收录规则,可添加多条</span>
  1004. <span class="token property">&quot;action&quot;</span><span class="token operator">:</span> <span class="token string">&quot;allow&quot;</span><span class="token punctuation">,</span> <span class="token comment">// 是否被收录</span>
  1005. <span class="token property">&quot;page&quot;</span><span class="token operator">:</span><span class="token string">&quot;pages/player/player&quot;</span><span class="token punctuation">,</span> <span class="token comment">// 页面</span>
  1006. <span class="token property">&quot;params&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">&quot;musicId&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;index&quot;</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token comment">// 链接的动态参数</span>
  1007. <span class="token property">&quot;matching&quot;</span><span class="token operator">:</span>'exact' <span class="token comment">// 表示params的参数是否要准确的匹配</span>
  1008. <span class="token punctuation">}</span>,<span class="token punctuation">{</span>
  1009. <span class="token property">&quot;action&quot;</span><span class="token operator">:</span> <span class="token string">&quot;disallow&quot;</span><span class="token punctuation">,</span> <span class="token comment">// 是否被收录</span>
  1010. <span class="token property">&quot;page&quot;</span><span class="token operator">:</span><span class="token string">&quot;*&quot;</span><span class="token punctuation">,</span> /
  1011. <span class="token punctuation">}</span><span class="token punctuation">]</span>
  1012. <span class="token punctuation">}</span>
  1013. <span class="token comment">// 上面配置规则表示除了player页面被收录外,其他页面不被收录</span>
  1014. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h3 id="小程序上线审核流程"><a href="#小程序上线审核流程" class="header-anchor">#</a> 小程序上线审核流程</h3> <p>微信公众平台,版本管理&gt;把小程序上传为体验版》提交审核》上线</p> <h2 id="后台管理系统"><a href="#后台管理系统" class="header-anchor">#</a> 后台管理系统</h2> <h4 id="架构示意图"><a href="#架构示意图" class="header-anchor">#</a> 架构示意图</h4> <p>​ 前端 后台 小程序云开发</p> <p>vue-admin-template &lt;---通过ajax--&gt; 基于Koa2;HTTP API 或 tcb-admin-node ----&gt;云函数、云数据库、云存储</p> <p>​</p> <h4 id="vue-admin-template构建管理系统前端"><a href="#vue-admin-template构建管理系统前端" class="header-anchor">#</a> vue-admin-template构建管理系统前端</h4> <p><a href="https://github.com/PanJiaChen/vue-element-admin" target="_blank" rel="noopener noreferrer">vue-element-admin<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 基于element的后台管理系统模板</p> <p><a href="https://github.com/PanJiaChen/vue-admin-template" target="_blank" rel="noopener noreferrer">vue-admin-template<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 是 <a href="https://github.com/PanJiaChen/vue-element-admin" target="_blank" rel="noopener noreferrer">vue-element-admin<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>的简化版</p> <p>使用方法查看官方文档。</p> <h4 id="koa2构建管理系统后端"><a href="#koa2构建管理系统后端" class="header-anchor">#</a> Koa2构建管理系统后端</h4> <p>官网: <a href="https://koa.bootcss.com/" target="_blank" rel="noopener noreferrer">https://koa.bootcss.com/<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <p>新建空文件夹wx-music-admin-backend,打开终端:</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token comment"># 生成package.json文件,-y 表示默认的配置</span>
  1015. <span class="token function">npm</span> init <span class="token parameter variable">-y</span>
  1016. <span class="token comment"># 安装koa</span>
  1017. <span class="token function">npm</span> <span class="token function">install</span> koa
  1018. <span class="token comment"># 新建app.js文件 (win10系统命令),作为项目入口文件</span>
  1019. <span class="token builtin class-name">type</span> nul <span class="token operator">&gt;</span> app.js
  1020. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>app.js:</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> Koa <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'koa'</span><span class="token punctuation">)</span>
  1021. <span class="token keyword">const</span> chalk <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'chalk'</span><span class="token punctuation">)</span> <span class="token comment">// 使console.log打印文字有颜色的插件,需: npm i chalk</span>
  1022. <span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Koa</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  1023. app<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span><span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token parameter">ctx</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  1024. ctx<span class="token punctuation">.</span>body <span class="token operator">=</span> <span class="token string">'Hello Wolrd'</span>
  1025. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  1026. <span class="token keyword">const</span> port <span class="token operator">=</span> <span class="token number">3000</span>
  1027. app<span class="token punctuation">.</span><span class="token function">listen</span><span class="token punctuation">(</span>port<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span> <span class="token comment">// 端口号,开启服务后的回调函数</span>
  1028. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>chalk<span class="token punctuation">.</span><span class="token function">green</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">&gt; 服务已开启,访问:http://localhost:</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>port<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  1029. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  1030. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>终端:</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token comment"># node启动项目</span>
  1031. <span class="token function">node</span> app.js
  1032. <span class="token comment"># 访问:http://localhost:3000</span>
  1033. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h4 id="接口调用凭证-access-token-的缓存与更新"><a href="#接口调用凭证-access-token-的缓存与更新" class="header-anchor">#</a> 接口调用凭证 access_token 的缓存与更新</h4> <p>access_token,微信的接口调用凭证,详情:<a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html" target="_blank" rel="noopener noreferrer">https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <p>回到项目wx-music-admin-backend,打开终端:</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token comment"># HTTP 请求 插件</span>
  1034. <span class="token function">npm</span> i request
  1035. <span class="token function">npm</span> i request-promise
  1036. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">/**
  1037. * 获取微信接口调用凭证
  1038. * 详情:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
  1039. */</span>
  1040. <span class="token keyword">const</span> rp <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'request-promise'</span><span class="token punctuation">)</span> <span class="token comment">// node发送http请求的插件</span>
  1041. <span class="token keyword">const</span> fs <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'fs'</span><span class="token punctuation">)</span> <span class="token comment">// node文件模块</span>
  1042. <span class="token keyword">const</span> path <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'path'</span><span class="token punctuation">)</span> <span class="token comment">// node 路径模块</span>
  1043. <span class="token comment">//fileName = __dirname 当前文件所在目录的绝对路径, 加上 './access_token.json'</span>
  1044. <span class="token keyword">const</span> fileName <span class="token operator">=</span> path<span class="token punctuation">.</span><span class="token function">resolve</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">,</span> <span class="token string">'./access_token.json'</span><span class="token punctuation">)</span>
  1045. <span class="token comment">// 这两个参数的获取:微信公众平台&gt;开发&gt;开发设置</span>
  1046. <span class="token keyword">const</span> <span class="token constant">APPID</span> <span class="token operator">=</span> <span class="token string">'wxc4e0b2d98063b103'</span>
  1047. <span class="token keyword">const</span> <span class="token constant">APPSECRET</span> <span class="token operator">=</span> <span class="token string">'xxx'</span> <span class="token comment">//小程序密钥,注意保密!</span>
  1048. <span class="token comment">// 微信 access_token 请求地址</span>
  1049. <span class="token keyword">const</span> <span class="token constant">URL</span> <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token constant">APPID</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">&amp;secret=</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token constant">APPSECRET</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span>
  1050. <span class="token comment">// 发送请求获取AccessToken</span>
  1051. <span class="token keyword">const</span> <span class="token function-variable function">updateAccessToken</span> <span class="token operator">=</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  1052. <span class="token keyword">const</span> resStr <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token function">rp</span><span class="token punctuation">(</span><span class="token constant">URL</span><span class="token punctuation">)</span>
  1053. <span class="token keyword">const</span> res <span class="token operator">=</span> <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>resStr<span class="token punctuation">)</span>
  1054. <span class="token keyword">if</span> <span class="token punctuation">(</span>res<span class="token punctuation">.</span>access_token<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  1055. <span class="token comment">// node写文件,参数:1 文件路径,2 文件内容, 首次写文件为新建,往后为覆盖</span>
  1056. fs<span class="token punctuation">.</span><span class="token function">writeFileSync</span><span class="token punctuation">(</span>fileName<span class="token punctuation">,</span> <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  1057. <span class="token literal-property property">access_token</span><span class="token operator">:</span> res<span class="token punctuation">.</span>access_token<span class="token punctuation">,</span>
  1058. <span class="token literal-property property">createTime</span><span class="token operator">:</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  1059. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  1060. <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token comment">// 如获取不到,再次获取</span>
  1061. <span class="token keyword">await</span> <span class="token function">updateAccessToken</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  1062. <span class="token punctuation">}</span>
  1063. <span class="token punctuation">}</span>
  1064. <span class="token comment">// 读取access_token</span>
  1065. <span class="token keyword">const</span> <span class="token function-variable function">getAccessToken</span> <span class="token operator">=</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  1066. <span class="token keyword">try</span> <span class="token punctuation">{</span>
  1067. <span class="token comment">// node读取文件,参数:1 读取的文件,2 字符集</span>
  1068. <span class="token keyword">const</span> readRes <span class="token operator">=</span> fs<span class="token punctuation">.</span><span class="token function">readFileSync</span><span class="token punctuation">(</span>fileName<span class="token punctuation">,</span> <span class="token string">'utf8'</span><span class="token punctuation">)</span>
  1069. <span class="token keyword">const</span> readObj <span class="token operator">=</span> <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>readRes<span class="token punctuation">)</span>
  1070. <span class="token comment">// 如果服务器宕机导致setInterval无法定时更新,这里需要再次判断access_token的有效性</span>
  1071. <span class="token keyword">const</span> createTime <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span>readObj<span class="token punctuation">.</span>createTime<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  1072. <span class="token keyword">const</span> nowTime <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  1073. <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>nowTime <span class="token operator">-</span> createTime<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">1000</span> <span class="token operator">/</span> <span class="token number">60</span> <span class="token operator">/</span> <span class="token number">60</span> <span class="token operator">&gt;=</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  1074. <span class="token keyword">await</span> <span class="token function">updateAccessToken</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  1075. <span class="token keyword">await</span> <span class="token function">getAccessToken</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  1076. <span class="token keyword">return</span>
  1077. <span class="token punctuation">}</span>
  1078. <span class="token keyword">return</span> readObj<span class="token punctuation">.</span>access_token
  1079. <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">//捕获异常,在未创建文件时,先创建文件</span>
  1080. <span class="token keyword">await</span> <span class="token function">updateAccessToken</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  1081. <span class="token keyword">await</span> <span class="token function">getAccessToken</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  1082. <span class="token punctuation">}</span>
  1083. <span class="token punctuation">}</span>
  1084. <span class="token comment">// access_token有效期为2个小时,定时更新</span>
  1085. <span class="token function">setInterval</span><span class="token punctuation">(</span><span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  1086. <span class="token keyword">await</span> <span class="token function">updateAccessToken</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  1087. <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">7200</span> <span class="token operator">-</span> <span class="token number">300</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">1000</span><span class="token punctuation">)</span>
  1088. module<span class="token punctuation">.</span>exports <span class="token operator">=</span> getAccessToken
  1089. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br></div></div><h4 id="后端代码通过http-api-触发云函数获取数据"><a href="#后端代码通过http-api-触发云函数获取数据" class="header-anchor">#</a> 后端代码通过HTTP API 触发云函数获取数据</h4> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/functions/invokeCloudFunction.html" target="_blank" rel="noopener noreferrer">HTTP API 触发云函数<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h4 id="产生跨域和后端解决跨域问题"><a href="#产生跨域和后端解决跨域问题" class="header-anchor">#</a> 产生跨域和后端解决跨域问题</h4> <p>管理系统前端向管理系统后端请求数据,产生了跨域问题</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code> <span class="token comment">// 产生跨域的几种情况</span>
  1090. <span class="token comment">// http://www.a.com https://www.a.com 协议不同</span>
  1091. <span class="token comment">// http://www.a.com http://www.b.com 域名不同</span>
  1092. <span class="token comment">// http://www.a.com http://news.a.com 主域与子域不同</span>
  1093. <span class="token comment">// http://www.a.com:8080 http://www.a.com:3000 端口不同</span>
  1094. <span class="token comment">// 解决跨域的几种方法</span>
  1095. <span class="token comment">// jsonp</span>
  1096. <span class="token comment">// iframe</span>
  1097. <span class="token comment">// postMessage跨域</span>
  1098. <span class="token comment">// 跨域资源共享(CORS)</span>
  1099. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>管理系统后端,安装</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>// 解决跨域问题的koa包
  1100. <span class="token function">npm</span> i koa2-cors
  1101. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>app.js</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//处理跨域</span>
  1102. app<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span><span class="token function">cors</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  1103. <span class="token literal-property property">origin</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'http://localhost:9528'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token comment">// 允许访问本服务的域</span>
  1104. <span class="token literal-property property">credentials</span><span class="token operator">:</span> <span class="token boolean">true</span>
  1105. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  1106. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h4 id="云数据库的增删改查接口"><a href="#云数据库的增删改查接口" class="header-anchor">#</a> 云数据库的增删改查接口</h4> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseQuery.html" target="_blank" rel="noopener noreferrer">数据库查询记录<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h4 id="后端获取前端post请求传来的数据"><a href="#后端获取前端post请求传来的数据" class="header-anchor">#</a> 后端获取前端post请求传来的数据</h4> <p>get请求可以直接通过ctx.request.query获取,但是post请求需要安装koa-body</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">npm</span> i koa-body
  1107. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>app.js</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> koaBody <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'koa-body'</span><span class="token punctuation">)</span> <span class="token comment">// 对post请求前端传来的数据的获取,需要此依赖</span>
  1108. <span class="token comment">// 接收post参数解析</span>
  1109. app<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span><span class="token function">koaBody</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  1110. <span class="token literal-property property">multipart</span><span class="token operator">:</span> <span class="token boolean">true</span>
  1111. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  1112. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>接口.js</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>router<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string">'/updatePlaylist'</span><span class="token punctuation">,</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token parameter">ctx<span class="token punctuation">,</span> next</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  1113. <span class="token keyword">const</span> params <span class="token operator">=</span> ctx<span class="token punctuation">.</span>request<span class="token punctuation">.</span>body <span class="token comment">// post请求获取前端传来的数据,需安装和配置koa-body</span>
  1114. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  1115. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h4 id="后端获取云存储图片"><a href="#后端获取云存储图片" class="header-anchor">#</a> 后端获取云存储图片</h4> <p>云存储中上传图片,云数据库中新建图片的集合,并添加数据字段,字段包含云文件的fileid。</p> <p>后端项目通过调用云数据库的方式获取数据</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>router<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'/list'</span><span class="token punctuation">,</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token parameter">ctx<span class="token punctuation">,</span> next</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  1116. <span class="token comment">// 接口中读取数据库默认最多10条数据</span>
  1117. <span class="token keyword">const</span> query <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">db.collection('swiper').get()</span><span class="token template-punctuation string">`</span></span>
  1118. <span class="token keyword">const</span> res <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token function">callCloudDB</span><span class="token punctuation">(</span>ctx<span class="token punctuation">,</span> <span class="token string">'databasequery'</span><span class="token punctuation">,</span> query<span class="token punctuation">)</span>
  1119. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span>
  1120. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  1121. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>但获取到的数据为fileid,并不能用于显示图片,需要通过微信HTTP API获取云存储的接口来获取图片地址</p> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/storage/batchDownloadFile.html" target="_blank" rel="noopener noreferrer">获取云存储<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h4 id="后端上传图片到云存储"><a href="#后端上传图片到云存储" class="header-anchor">#</a> 后端上传图片到云存储</h4> <p><a href="https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/storage/uploadFile.html" target="_blank" rel="noopener noreferrer">文件上传<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p></div></div> <div class="page-edit"><div class="edit-link"><a href="https://github.com/heBody/blog/edit/master/docs/01.前端/40.学习笔记/40.小程序笔记.md" target="_blank" rel="noopener noreferrer">编辑</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="tags"><a href="/blog/tags/?tag=%E5%B0%8F%E7%A8%8B%E5%BA%8F" title="标签">#小程序</a></div> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2022/12/14, 19:36:42</span></div></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/blog/pages/51afd6/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">TypeScript笔记</div></a> <a href="/blog/pages/4643cd/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">JS设计模式总结笔记</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
  1122. <a href="/blog/pages/51afd6/" class="prev">TypeScript笔记</a></span> <span class="next"><a href="/blog/pages/4643cd/">JS设计模式总结笔记</a>→
  1123. </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/blog/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/blog/pages/922650/"><div>
  1124. Git修改分支名
  1125. <!----></div></a> <span class="date">08-11</span></dt></dl><dl><dd>02</dd> <dt><a href="/blog/pages/55f894/"><div>
  1126. CSS给table的tbody添加滚动条
  1127. <!----></div></a> <span class="date">06-29</span></dt></dl><dl><dd>03</dd> <dt><a href="/blog/pages/829589/"><div>
  1128. 我做了一个手写春联小网页,祝大家虎年暴富
  1129. <span class="title-tag">
  1130. 原创
  1131. </span></div></a> <span class="date">01-28</span></dt></dl> <dl><dd></dd> <dt><a href="/blog/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:30363811@qq.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/heBody" title="GitHub" target="_blank" class="iconfont icon-github"></a></div>
  1132. Copyright © 2016-2022
  1133. <span>Hesb | <a href="https://github.com/heBody/blob" target="_blank">MIT License</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
  1134. 跟随系统
  1135. </li><li class="iconfont icon-rijianmoshi">
  1136. 浅色模式
  1137. </li><li class="iconfont icon-yejianmoshi">
  1138. 深色模式
  1139. </li><li class="iconfont icon-yuedu">
  1140. 阅读模式
  1141. </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><div></div></div></div>
  1142. <script src="/blog/assets/js/app.28edfb28.js" defer></script><script src="/blog/assets/js/2.395c0d18.js" defer></script><script src="/blog/assets/js/3.6748bd5c.js" defer></script><script src="/blog/assets/js/32.66c0e717.js" defer></script>
  1143. </body>
  1144. </html>