-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.json
1 lines (1 loc) · 335 KB
/
db.json
1
{"meta":{"version":1,"warehouse":"2.2.0"},"models":{"Asset":[{"_id":"themes/diaspora/source/css/index.css","path":"css/index.css","modified":0,"renderable":1},{"_id":"themes/diaspora/source/css/diaspora.css","path":"css/diaspora.css","modified":0,"renderable":1},{"_id":"themes/diaspora/source/img/favicon.png","path":"img/favicon.png","modified":0,"renderable":1},{"_id":"themes/diaspora/source/img/logo.png","path":"img/logo.png","modified":0,"renderable":1},{"_id":"themes/diaspora/source/img/wec.jpg","path":"img/wec.jpg","modified":0,"renderable":1},{"_id":"themes/diaspora/source/js/diaspora.js","path":"js/diaspora.js","modified":0,"renderable":1},{"_id":"themes/diaspora/source/js/plugin.js","path":"js/plugin.js","modified":0,"renderable":1},{"_id":"themes/diaspora/source/photoswipe/photoswipe-ui-default.js","path":"photoswipe/photoswipe-ui-default.js","modified":0,"renderable":1},{"_id":"themes/diaspora/source/photoswipe/photoswipe-ui-default.min.js","path":"photoswipe/photoswipe-ui-default.min.js","modified":0,"renderable":1},{"_id":"themes/diaspora/source/photoswipe/photoswipe.css","path":"photoswipe/photoswipe.css","modified":0,"renderable":1},{"_id":"themes/diaspora/source/photoswipe/photoswipe.min.js","path":"photoswipe/photoswipe.min.js","modified":0,"renderable":1},{"_id":"themes/diaspora/source/img/1.jpg","path":"img/1.jpg","modified":0,"renderable":1},{"_id":"themes/diaspora/source/img/2.jpg","path":"img/2.jpg","modified":0,"renderable":1},{"_id":"themes/diaspora/source/img/3.jpg","path":"img/3.jpg","modified":0,"renderable":1},{"_id":"themes/diaspora/source/img/4.jpg","path":"img/4.jpg","modified":0,"renderable":1},{"_id":"themes/diaspora/source/img/5.jpg","path":"img/5.jpg","modified":0,"renderable":1},{"_id":"themes/diaspora/source/js/jquery.min.js","path":"js/jquery.min.js","modified":0,"renderable":1},{"_id":"themes/diaspora/source/photoswipe/photoswipe.js","path":"photoswipe/photoswipe.js","modified":0,"renderable":1},{"_id":"themes/diaspora/source/img/10.jpg","path":"img/10.jpg","modified":0,"renderable":1},{"_id":"themes/diaspora/source/img/7.jpg","path":"img/7.jpg","modified":0,"renderable":1},{"_id":"themes/diaspora/source/img/9.jpg","path":"img/9.jpg","modified":0,"renderable":1},{"_id":"themes/diaspora/source/img/cover.jpg","path":"img/cover.jpg","modified":0,"renderable":1},{"_id":"themes/diaspora/source/img/8.jpg","path":"img/8.jpg","modified":0,"renderable":1},{"_id":"themes/diaspora/source/photoswipe/default-skin/default-skin.png","path":"photoswipe/default-skin/default-skin.png","modified":0,"renderable":1},{"_id":"themes/diaspora/source/photoswipe/default-skin/default-skin.css","path":"photoswipe/default-skin/default-skin.css","modified":0,"renderable":1},{"_id":"themes/diaspora/source/photoswipe/default-skin/preloader.gif","path":"photoswipe/default-skin/preloader.gif","modified":0,"renderable":1},{"_id":"themes/diaspora/source/photoswipe/default-skin/default-skin.svg","path":"photoswipe/default-skin/default-skin.svg","modified":0,"renderable":1},{"_id":"themes/diaspora/source/img/welcome-cover.jpg","path":"img/welcome-cover.jpg","modified":0,"renderable":1},{"_id":"themes/diaspora/source/img/6.jpg","path":"img/6.jpg","modified":0,"renderable":1}],"Cache":[{"_id":"themes/diaspora/LICENSE","hash":"63175a16773044bed84e1778009a54ad03dd5520","modified":1535527921899},{"_id":"themes/diaspora/README.md","hash":"68d34d4d958661c50e7b6dac3f96b3c2ae19557e","modified":1535527921900},{"_id":"themes/diaspora/.DS_Store","hash":"765a67cb411c4ebed18796e2b4b54cd7e3a29ca5","modified":1535528985111},{"_id":"themes/diaspora/_config.yml","hash":"dcb18f31cf8a00e0b5b1bd724621ac35fd59de29","modified":1535540910976},{"_id":"source/.DS_Store","hash":"544c80a7736995f8fe3bdd1609b11cee12d46a04","modified":1535592217069},{"_id":"themes/diaspora/layout/archive.ejs","hash":"da39a3ee5e6b4b0d3255bfef95601890afd80709","modified":1535527921906},{"_id":"themes/diaspora/layout/category.ejs","hash":"da39a3ee5e6b4b0d3255bfef95601890afd80709","modified":1535527921906},{"_id":"source/_posts/SQL-行转列.md","hash":"9987ce7c3231bb4368160fbbbe296405dbb1d4dc","modified":1542274053967},{"_id":"source/_posts/JS-合并数组.md","hash":"ee3f303c6be46c9a2c30796a54fb8c9d942235d7","modified":1535624421945},{"_id":"source/_posts/hello-world.md","hash":"8eb41fd7a5f74bb4e154da3705edf75abb07bf54","modified":1542274133217},{"_id":"source/_posts/css修改滚动条默认样式.md","hash":"4d50d3aff905a0b724f2ff858f129ddc62ea9c26","modified":1535624863962},{"_id":"source/_posts/javascript正则表达式提取参数.md","hash":"2a01a42808352e16b4c1c430dd45650ed36cddcb","modified":1535624530731},{"_id":"source/_posts/javascript获取Flash下载进度兼容IE-FireFox-Chrome浏览器.md","hash":"13512b2c8c5084f1e548e5320e5de2d3b277a1e4","modified":1535625794225},{"_id":"source/_posts/openresty-nginx-实践.md","hash":"efd45aab7779f9921dc3c6a1f342e86ef7ec96ee","modified":1535630896063},{"_id":"source/_posts/微信JS在H5中的使用.md","hash":"34f5a19ac8a0dfaaf86b44f33e79e0cef1d9f65f","modified":1536834525655},{"_id":"source/_posts/vue-websocket-graphql中大型web应用实践.md","hash":"488254a1adcce6f5d786fc936f3759fe9c08b43b","modified":1535534103148},{"_id":"source/_posts/迭代器-生成器-generator-和Promise的“微妙”关系.md","hash":"ac258b0f7e2ac33a463cab964aafc1b4fff31908","modified":1535531576869},{"_id":"source/habanerna/index.md","hash":"38bb8e3b218e3be6cd1c7f92dbc1c4fa8538abbc","modified":1535627515891},{"_id":"themes/diaspora/languages/default.yml","hash":"3083f319b352d21d80fc5e20113ddf27889c9d11","modified":1535527921900},{"_id":"themes/diaspora/languages/fr.yml","hash":"84ab164b37c6abf625473e9a0c18f6f815dd5fd9","modified":1535527921901},{"_id":"themes/diaspora/languages/nl.yml","hash":"12ed59faba1fc4e8cdd1d42ab55ef518dde8039c","modified":1535527921901},{"_id":"themes/diaspora/languages/no.yml","hash":"965a171e70347215ec726952e63f5b47930931ef","modified":1535527921901},{"_id":"themes/diaspora/languages/ru.yml","hash":"4fda301bbd8b39f2c714e2c934eccc4b27c0a2b0","modified":1535527921901},{"_id":"themes/diaspora/languages/zh-TW.yml","hash":"53ce3000c5f767759c7d2c4efcaa9049788599c3","modified":1535527921902},{"_id":"themes/diaspora/languages/zh-CN.yml","hash":"ca40697097ab0b3672a80b455d3f4081292d1eed","modified":1535527921902},{"_id":"themes/diaspora/layout/page.ejs","hash":"eeda07da6c4f604b91a29a9a679cbf22a5c23469","modified":1535527921907},{"_id":"themes/diaspora/layout/post.ejs","hash":"e585d8b4680c52dccfc40ffa6b2c5df515bf20d2","modified":1535527921908},{"_id":"themes/diaspora/layout/index.ejs","hash":"18a90c45150a15ae0f505519a0b9d7f285b25dff","modified":1535527921907},{"_id":"themes/diaspora/layout/layout.ejs","hash":"6bb3282e8233a2b4d397d023cd81ff6cf952b6ea","modified":1535527921907},{"_id":"themes/diaspora/layout/tag.ejs","hash":"dc62fbc1b0814658a6456b113a2d6880f7b9556d","modified":1535527921908},{"_id":"themes/diaspora/source/.DS_Store","hash":"2583c7159e54833719367ea82e20d27900c6548e","modified":1535528985110},{"_id":"themes/diaspora/layout/_partial/google-analytics.ejs","hash":"da39a3ee5e6b4b0d3255bfef95601890afd80709","modified":1535527921902},{"_id":"themes/diaspora/layout/_partial/list.ejs","hash":"8e03499580aa81ec0b4c8316347b4e3839849007","modified":1535527921903},{"_id":"themes/diaspora/layout/_partial/photoswipe.ejs","hash":"d1d36cf0a2c03ff3c2acbc208fe11cd0afd8b7bd","modified":1535527921904},{"_id":"themes/diaspora/layout/_partial/head.ejs","hash":"f4ccde35521902c8de50e37318543270268a6b16","modified":1535529933612},{"_id":"themes/diaspora/layout/_partial/menu.ejs","hash":"8290c3909bbd01a8532b1f72689f755248a2aac4","modified":1535527921903},{"_id":"themes/diaspora/layout/_partial/screen.ejs","hash":"cf5aac35f3b735315d9d61ba978a858d9eef17a0","modified":1535527921906},{"_id":"themes/diaspora/layout/_partial/scripts.ejs","hash":"041a2e405157447b20b5d339596f35a2ec89808c","modified":1535527921906},{"_id":"themes/diaspora/source/css/index.css","hash":"ac12eb35921007e952613fe94caecbed9e363a62","modified":1535545881620},{"_id":"themes/diaspora/source/css/diaspora.css","hash":"79358615a394d7749efdea65efe2e1d5163faea3","modified":1535527921909},{"_id":"themes/diaspora/source/img/favicon.png","hash":"d1ddf2e84de93be79e237377bb1b5858235a5059","modified":1535528932093},{"_id":"themes/diaspora/source/img/logo.png","hash":"11265c1d326bb413bcac30faad9dfb707f5ef522","modified":1535527921912},{"_id":"themes/diaspora/source/img/wec.jpg","hash":"06832b4dd02179ed13b980d45bf56b63cee1e480","modified":1535627419374},{"_id":"themes/diaspora/source/js/diaspora.js","hash":"a213e865e2c48f15796059ce962fb7594361b3bf","modified":1535527921914},{"_id":"themes/diaspora/source/js/plugin.js","hash":"0ac6bf36a9a11ca6fad0af1f20e5705bc326abf4","modified":1535527921916},{"_id":"themes/diaspora/source/photoswipe/photoswipe-ui-default.js","hash":"c844cca2f9a9969a810a0fb534063e008424c936","modified":1535527921918},{"_id":"themes/diaspora/source/photoswipe/photoswipe-ui-default.min.js","hash":"d80b713ca1bab90f27a8af7d8e43563f0fe81860","modified":1535527921918},{"_id":"themes/diaspora/source/photoswipe/photoswipe.css","hash":"a31fa495095783ef5a84ba4dba0bd6f7de3b07ea","modified":1535527921918},{"_id":"themes/diaspora/source/photoswipe/photoswipe.min.js","hash":"791703278e7c576b08b67f18404bae9eb8cebfaf","modified":1535527921920},{"_id":"themes/diaspora/source/img/1.jpg","hash":"69524a5e33e515b0364fefdb1cfd9aada6b78156","modified":1535529682327},{"_id":"themes/diaspora/source/img/2.jpg","hash":"20faca47d962a451a4d8b1500a322115dc79b465","modified":1535529672544},{"_id":"themes/diaspora/source/img/3.jpg","hash":"8c5ac9d69eb250025427d8eaca30a2cd05e5b84b","modified":1535529662183},{"_id":"themes/diaspora/source/img/4.jpg","hash":"40fdd38015224dc8393f4bbc8eeb1e4a093f00fd","modified":1535529648368},{"_id":"themes/diaspora/source/img/5.jpg","hash":"54f95566c6479109b5c032aee6b0430a8e7f92d3","modified":1535529639061},{"_id":"themes/diaspora/source/js/jquery.min.js","hash":"3f1b0e9e54af1af2db2c8a639530448723462151","modified":1535527921915},{"_id":"themes/diaspora/source/photoswipe/photoswipe.js","hash":"30967789eb2a3e1da9927e71f4f3c5339ebed39a","modified":1535527921919},{"_id":"themes/diaspora/layout/_partial/post/article.ejs","hash":"86cfa39f33ec90027dbaa7c4d3364687219ea7ec","modified":1535527921904},{"_id":"themes/diaspora/layout/_partial/post/gitalk.ejs","hash":"7b684b561eca872b8e18e270c07f459ea3634676","modified":1535527921904},{"_id":"themes/diaspora/layout/_partial/post/header.ejs","hash":"716aa31922caacd176fcad3720f54bebda11e406","modified":1535527921905},{"_id":"themes/diaspora/layout/_partial/post/tag.ejs","hash":"c2b4ce7e1d7eed59f4c333915cd431f008f898e3","modified":1535527921905},{"_id":"themes/diaspora/layout/_partial/post/item.ejs","hash":"7c731538de9750fed2e0865409d24c3afa77644f","modified":1535527921905},{"_id":"themes/diaspora/source/img/10.jpg","hash":"e0513a2389e1a93ef777c839ce117f5f9d7ccca2","modified":1535529571691},{"_id":"themes/diaspora/source/img/7.jpg","hash":"4f32e77a22511c80fd355f64181344ebe50060c9","modified":1535529617739},{"_id":"themes/diaspora/source/img/9.jpg","hash":"9c24df4805c1e897f7c8f72b4573da5e4a8f8436","modified":1535529585904},{"_id":"themes/diaspora/source/img/cover.jpg","hash":"dc7103d4c4e05650595712b3fa395b89726df32b","modified":1535527921912},{"_id":"themes/diaspora/source/img/8.jpg","hash":"df045def19263588aac005570d436e4c56e8c076","modified":1535529610485},{"_id":"themes/diaspora/source/photoswipe/default-skin/default-skin.png","hash":"ed95a8e40a2c3478c5915376acb8e5f33677f24d","modified":1535527921916},{"_id":"themes/diaspora/source/photoswipe/default-skin/default-skin.css","hash":"5763bd1a8015774f37215359bfcb5edd65d198a6","modified":1535527921916},{"_id":"themes/diaspora/source/photoswipe/default-skin/preloader.gif","hash":"6342367c93c82da1b9c620e97c84a389cc43d96d","modified":1535527921917},{"_id":"themes/diaspora/source/photoswipe/default-skin/default-skin.svg","hash":"2ac727c9e092331d35cce95af209ccfac6d4c7c7","modified":1535527921917},{"_id":"themes/diaspora/source/img/welcome-cover.jpg","hash":"c393dfeb52aff20a9921c1f56e2ce127683ab35a","modified":1535527921914},{"_id":"themes/diaspora/source/img/6.jpg","hash":"625284a1ada54bd8b57c4294b378c479db2b9b34","modified":1535529625940},{"_id":"source/_drafts/postMessage-window-name实现iframe跨域通信jquery兼容.md","hash":"ddb8c320d4a96c59d42f628d47797c8558b3cc6c","modified":1542465500781},{"_id":"source/_posts/postMessage-window-name实现iframe跨域通信jquery兼容.md","hash":"ddb8c320d4a96c59d42f628d47797c8558b3cc6c","modified":1542465502354},{"_id":"source/_drafts/日常笔记.md","hash":"c08ad2fa73aec28fc84019cd23e11592109e20b9","modified":1542702278484},{"_id":"source/_drafts/数据库引擎.md","hash":"9389295ee9c9ab305bb2a678dce4f5a2275c95b1","modified":1542706077294}],"Category":[],"Data":[],"Page":[{"title":"habanerna","date":"2018-08-30T11:08:26.000Z","_content":"> 我的微信\n\n\n![weChat](/img/wec.jpg)\n\n> habanerna\n\n> 添加请发送来源:博客","source":"habanerna/index.md","raw":"title: habanerna\ndate: 2018-08-30 19:08:26\n---\n> 我的微信\n\n\n![weChat](/img/wec.jpg)\n\n> habanerna\n\n> 添加请发送来源:博客","updated":"2018-08-30T11:11:55.891Z","path":"habanerna/index.html","comments":1,"layout":"page","_id":"cjoljzh1g00090evogrqn0lh3","content":"<blockquote>\n<p>我的微信</p>\n</blockquote>\n<p><img src=\"/img/wec.jpg\" alt=\"weChat\"></p>\n<blockquote>\n<p>habanerna</p>\n</blockquote>\n<blockquote>\n<p>添加请发送来源:博客</p>\n</blockquote>\n","site":{"data":{}},"excerpt":"","more":"<blockquote>\n<p>我的微信</p>\n</blockquote>\n<p><img src=\"/img/wec.jpg\" alt=\"weChat\"></p>\n<blockquote>\n<p>habanerna</p>\n</blockquote>\n<blockquote>\n<p>添加请发送来源:博客</p>\n</blockquote>\n"}],"Post":[{"title":"SQL 行转列","author":"brian","date":"2018-11-15T09:19:00.000Z","_content":"好久没有触碰SQL,最近遇到一个查询需求,对多行数据并归后,要进行分组显示\n\n```sql\nselect \n tb_member.relate_id as 'UID', \n tb_currency_user.member_id as '用户ID',\n\t max(case tb_currency_user.currency_id when 1 then tb_currency_user.num else 0 end) as 'ETH',\n\t max(case tb_currency_user.currency_id when 25 then tb_currency_user.num else 0 end) as 'USDT',\n\t max(case tb_currency_user.currency_id when 26 then tb_currency_user.num else 0 end) as 'BTC'\n max(case tb_currency_user.currency_id when 28 then tb_currency_user.num else 0 end) as 'WTC'\n\t\nfrom tb_currency_user \nleft join tb_member on tb_currency_user.member_id = tb_member.member_id\nwhere tb_currency_user.num > 0 and tb_currency_user.currency_id in(26, 25, 1)\ngroup by tb_currency_user.member_id;\n\n```\n\n> 结果\n\n| UID | 用户ID | ETH | USDT | BTC | WTC |\n| --- | ----- | --- | ---- | --- | --- |\n| 001 | 1 | 0.001 | 0 | 0.0001 | 2 |\n| 001 | 1 | 0.001 | 0 | 0.0001 | 2 |\n| 001 | 1 | 0.001 | 0 | 0.0001 | 2 |\n| 001 | 1 | 0.001 | 0 | 0.0001 | 2 |\n| 001 | 1 | 0.001 | 0 | 0.0001 | 2 |","source":"_posts/SQL-行转列.md","raw":"title: SQL 行转列\nauthor: brian\ntags: []\ncategories: []\ndate: 2018-11-15 17:19:00\n---\n好久没有触碰SQL,最近遇到一个查询需求,对多行数据并归后,要进行分组显示\n\n```sql\nselect \n tb_member.relate_id as 'UID', \n tb_currency_user.member_id as '用户ID',\n\t max(case tb_currency_user.currency_id when 1 then tb_currency_user.num else 0 end) as 'ETH',\n\t max(case tb_currency_user.currency_id when 25 then tb_currency_user.num else 0 end) as 'USDT',\n\t max(case tb_currency_user.currency_id when 26 then tb_currency_user.num else 0 end) as 'BTC'\n max(case tb_currency_user.currency_id when 28 then tb_currency_user.num else 0 end) as 'WTC'\n\t\nfrom tb_currency_user \nleft join tb_member on tb_currency_user.member_id = tb_member.member_id\nwhere tb_currency_user.num > 0 and tb_currency_user.currency_id in(26, 25, 1)\ngroup by tb_currency_user.member_id;\n\n```\n\n> 结果\n\n| UID | 用户ID | ETH | USDT | BTC | WTC |\n| --- | ----- | --- | ---- | --- | --- |\n| 001 | 1 | 0.001 | 0 | 0.0001 | 2 |\n| 001 | 1 | 0.001 | 0 | 0.0001 | 2 |\n| 001 | 1 | 0.001 | 0 | 0.0001 | 2 |\n| 001 | 1 | 0.001 | 0 | 0.0001 | 2 |\n| 001 | 1 | 0.001 | 0 | 0.0001 | 2 |","slug":"SQL-行转列","published":1,"updated":"2018-11-15T09:27:33.967Z","comments":1,"layout":"post","photos":[],"link":"","_id":"cjoljzh0q00000evoch1lafus","content":"<p>好久没有触碰SQL,最近遇到一个查询需求,对多行数据并归后,要进行分组显示</p>\n<figure class=\"highlight sql\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"keyword\">select</span> </span><br><span class=\"line\"> tb_member.relate_id <span class=\"keyword\">as</span> <span class=\"string\">'UID'</span>, </span><br><span class=\"line\"> tb_currency_user.member_id <span class=\"keyword\">as</span> <span class=\"string\">'用户ID'</span>,</span><br><span class=\"line\">\t <span class=\"keyword\">max</span>(<span class=\"keyword\">case</span> tb_currency_user.currency_id <span class=\"keyword\">when</span> <span class=\"number\">1</span> <span class=\"keyword\">then</span> tb_currency_user.num <span class=\"keyword\">else</span> <span class=\"number\">0</span> <span class=\"keyword\">end</span>) <span class=\"keyword\">as</span> <span class=\"string\">'ETH'</span>,</span><br><span class=\"line\">\t <span class=\"keyword\">max</span>(<span class=\"keyword\">case</span> tb_currency_user.currency_id <span class=\"keyword\">when</span> <span class=\"number\">25</span> <span class=\"keyword\">then</span> tb_currency_user.num <span class=\"keyword\">else</span> <span class=\"number\">0</span> <span class=\"keyword\">end</span>) <span class=\"keyword\">as</span> <span class=\"string\">'USDT'</span>,</span><br><span class=\"line\">\t <span class=\"keyword\">max</span>(<span class=\"keyword\">case</span> tb_currency_user.currency_id <span class=\"keyword\">when</span> <span class=\"number\">26</span> <span class=\"keyword\">then</span> tb_currency_user.num <span class=\"keyword\">else</span> <span class=\"number\">0</span> <span class=\"keyword\">end</span>) <span class=\"keyword\">as</span> <span class=\"string\">'BTC'</span></span><br><span class=\"line\"> <span class=\"keyword\">max</span>(<span class=\"keyword\">case</span> tb_currency_user.currency_id <span class=\"keyword\">when</span> <span class=\"number\">28</span> <span class=\"keyword\">then</span> tb_currency_user.num <span class=\"keyword\">else</span> <span class=\"number\">0</span> <span class=\"keyword\">end</span>) <span class=\"keyword\">as</span> <span class=\"string\">'WTC'</span></span><br><span class=\"line\">\t</span><br><span class=\"line\"><span class=\"keyword\">from</span> tb_currency_user </span><br><span class=\"line\"><span class=\"keyword\">left</span> <span class=\"keyword\">join</span> tb_member <span class=\"keyword\">on</span> tb_currency_user.member_id = tb_member.member_id</span><br><span class=\"line\"><span class=\"keyword\">where</span> tb_currency_user.num > <span class=\"number\">0</span> <span class=\"keyword\">and</span> tb_currency_user.currency_id <span class=\"keyword\">in</span>(<span class=\"number\">26</span>, <span class=\"number\">25</span>, <span class=\"number\">1</span>)</span><br><span class=\"line\"><span class=\"keyword\">group</span> <span class=\"keyword\">by</span> tb_currency_user.member_id;</span><br></pre></td></tr></table></figure>\n<blockquote>\n<p>结果</p>\n</blockquote>\n<table>\n<thead>\n<tr>\n<th>UID</th>\n<th>用户ID</th>\n<th>ETH</th>\n<th>USDT</th>\n<th>BTC</th>\n<th>WTC</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>001</td>\n<td>1</td>\n<td>0.001</td>\n<td>0</td>\n<td>0.0001</td>\n<td>2</td>\n</tr>\n<tr>\n<td>001</td>\n<td>1</td>\n<td>0.001</td>\n<td>0</td>\n<td>0.0001</td>\n<td>2</td>\n</tr>\n<tr>\n<td>001</td>\n<td>1</td>\n<td>0.001</td>\n<td>0</td>\n<td>0.0001</td>\n<td>2</td>\n</tr>\n<tr>\n<td>001</td>\n<td>1</td>\n<td>0.001</td>\n<td>0</td>\n<td>0.0001</td>\n<td>2</td>\n</tr>\n<tr>\n<td>001</td>\n<td>1</td>\n<td>0.001</td>\n<td>0</td>\n<td>0.0001</td>\n<td>2</td>\n</tr>\n</tbody>\n</table>\n","site":{"data":{}},"excerpt":"","more":"<p>好久没有触碰SQL,最近遇到一个查询需求,对多行数据并归后,要进行分组显示</p>\n<figure class=\"highlight sql\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"keyword\">select</span> </span><br><span class=\"line\"> tb_member.relate_id <span class=\"keyword\">as</span> <span class=\"string\">'UID'</span>, </span><br><span class=\"line\"> tb_currency_user.member_id <span class=\"keyword\">as</span> <span class=\"string\">'用户ID'</span>,</span><br><span class=\"line\">\t <span class=\"keyword\">max</span>(<span class=\"keyword\">case</span> tb_currency_user.currency_id <span class=\"keyword\">when</span> <span class=\"number\">1</span> <span class=\"keyword\">then</span> tb_currency_user.num <span class=\"keyword\">else</span> <span class=\"number\">0</span> <span class=\"keyword\">end</span>) <span class=\"keyword\">as</span> <span class=\"string\">'ETH'</span>,</span><br><span class=\"line\">\t <span class=\"keyword\">max</span>(<span class=\"keyword\">case</span> tb_currency_user.currency_id <span class=\"keyword\">when</span> <span class=\"number\">25</span> <span class=\"keyword\">then</span> tb_currency_user.num <span class=\"keyword\">else</span> <span class=\"number\">0</span> <span class=\"keyword\">end</span>) <span class=\"keyword\">as</span> <span class=\"string\">'USDT'</span>,</span><br><span class=\"line\">\t <span class=\"keyword\">max</span>(<span class=\"keyword\">case</span> tb_currency_user.currency_id <span class=\"keyword\">when</span> <span class=\"number\">26</span> <span class=\"keyword\">then</span> tb_currency_user.num <span class=\"keyword\">else</span> <span class=\"number\">0</span> <span class=\"keyword\">end</span>) <span class=\"keyword\">as</span> <span class=\"string\">'BTC'</span></span><br><span class=\"line\"> <span class=\"keyword\">max</span>(<span class=\"keyword\">case</span> tb_currency_user.currency_id <span class=\"keyword\">when</span> <span class=\"number\">28</span> <span class=\"keyword\">then</span> tb_currency_user.num <span class=\"keyword\">else</span> <span class=\"number\">0</span> <span class=\"keyword\">end</span>) <span class=\"keyword\">as</span> <span class=\"string\">'WTC'</span></span><br><span class=\"line\">\t</span><br><span class=\"line\"><span class=\"keyword\">from</span> tb_currency_user </span><br><span class=\"line\"><span class=\"keyword\">left</span> <span class=\"keyword\">join</span> tb_member <span class=\"keyword\">on</span> tb_currency_user.member_id = tb_member.member_id</span><br><span class=\"line\"><span class=\"keyword\">where</span> tb_currency_user.num > <span class=\"number\">0</span> <span class=\"keyword\">and</span> tb_currency_user.currency_id <span class=\"keyword\">in</span>(<span class=\"number\">26</span>, <span class=\"number\">25</span>, <span class=\"number\">1</span>)</span><br><span class=\"line\"><span class=\"keyword\">group</span> <span class=\"keyword\">by</span> tb_currency_user.member_id;</span><br></pre></td></tr></table></figure>\n<blockquote>\n<p>结果</p>\n</blockquote>\n<table>\n<thead>\n<tr>\n<th>UID</th>\n<th>用户ID</th>\n<th>ETH</th>\n<th>USDT</th>\n<th>BTC</th>\n<th>WTC</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>001</td>\n<td>1</td>\n<td>0.001</td>\n<td>0</td>\n<td>0.0001</td>\n<td>2</td>\n</tr>\n<tr>\n<td>001</td>\n<td>1</td>\n<td>0.001</td>\n<td>0</td>\n<td>0.0001</td>\n<td>2</td>\n</tr>\n<tr>\n<td>001</td>\n<td>1</td>\n<td>0.001</td>\n<td>0</td>\n<td>0.0001</td>\n<td>2</td>\n</tr>\n<tr>\n<td>001</td>\n<td>1</td>\n<td>0.001</td>\n<td>0</td>\n<td>0.0001</td>\n<td>2</td>\n</tr>\n<tr>\n<td>001</td>\n<td>1</td>\n<td>0.001</td>\n<td>0</td>\n<td>0.0001</td>\n<td>2</td>\n</tr>\n</tbody>\n</table>\n"},{"title":"JS 合并数组","author":"brian","date":"2012-08-03T14:10:00.000Z","_content":">\n案例说明:\n\n 某充话费充值渠道支持a充值列表,但某充值卡的面额只有b列表面额\n 其中other表示其他输入金额 \n var a = [5, 10, 20, 50, 100, 'other'];\n\n var b = [10, 30, 50];\n 理想结果:a渠道能支持b卡充值:[10, 50]\n \n\n```js\nfunction ArrayIntersection( a, b ) {//合并数组 \n\tvar ai=0, bi=0; \n var result = new Array(); \n while ( ai < a.length && bi < b.length ) { \n \tif ( a[ai] < b[bi] ) { \n \tai++; \n\t\t} else if ( a[ai] > b[bi] ) { \n\t\t\tbi++;\n\t\t} else { \n\t\t\tresult.push ( a[ai] ); \n\t\t\tai++; \n\t\t\tbi++; \n\t\t} \n\t} \n\treturn result; \n} \n```\n\n```bash\n# run\n> var new_item = ArrayIntersection(a, b); \n> console.log(new_item);\n> [10, 50]\n```\n\n ","source":"_posts/JS-合并数组.md","raw":"title: JS 合并数组\nauthor: brian\ntags:\n - javascript\ncategories: []\ndate: 2012-08-03 22:10:00\n---\n>\n案例说明:\n\n 某充话费充值渠道支持a充值列表,但某充值卡的面额只有b列表面额\n 其中other表示其他输入金额 \n var a = [5, 10, 20, 50, 100, 'other'];\n\n var b = [10, 30, 50];\n 理想结果:a渠道能支持b卡充值:[10, 50]\n \n\n```js\nfunction ArrayIntersection( a, b ) {//合并数组 \n\tvar ai=0, bi=0; \n var result = new Array(); \n while ( ai < a.length && bi < b.length ) { \n \tif ( a[ai] < b[bi] ) { \n \tai++; \n\t\t} else if ( a[ai] > b[bi] ) { \n\t\t\tbi++;\n\t\t} else { \n\t\t\tresult.push ( a[ai] ); \n\t\t\tai++; \n\t\t\tbi++; \n\t\t} \n\t} \n\treturn result; \n} \n```\n\n```bash\n# run\n> var new_item = ArrayIntersection(a, b); \n> console.log(new_item);\n> [10, 50]\n```\n\n ","slug":"JS-合并数组","published":1,"updated":"2018-08-30T10:20:21.945Z","comments":1,"layout":"post","photos":[],"link":"","_id":"cjoljzh0w00010evou78xf1nu","content":"<blockquote>\n</blockquote>\n<p>案例说明:</p>\n<pre><code>某充话费充值渠道支持a充值列表,但某充值卡的面额只有b列表面额\n其中other表示其他输入金额 \nvar a = [5, 10, 20, 50, 100, 'other'];\n\nvar b = [10, 30, 50];\n理想结果:a渠道能支持b卡充值:[10, 50]\n</code></pre><figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"function\"><span class=\"keyword\">function</span> <span class=\"title\">ArrayIntersection</span>(<span class=\"params\"> a, b </span>) </span>{<span class=\"comment\">//合并数组 </span></span><br><span class=\"line\">\t<span class=\"keyword\">var</span> ai=<span class=\"number\">0</span>, bi=<span class=\"number\">0</span>; </span><br><span class=\"line\"> <span class=\"keyword\">var</span> result = <span class=\"keyword\">new</span> <span class=\"built_in\">Array</span>(); </span><br><span class=\"line\"> <span class=\"keyword\">while</span> ( ai < a.length && bi < b.length ) { </span><br><span class=\"line\"> \t<span class=\"keyword\">if</span> ( a[ai] < b[bi] ) { </span><br><span class=\"line\"> \tai++; </span><br><span class=\"line\">\t\t} <span class=\"keyword\">else</span> <span class=\"keyword\">if</span> ( a[ai] > b[bi] ) { </span><br><span class=\"line\">\t\t\tbi++;</span><br><span class=\"line\">\t\t} <span class=\"keyword\">else</span> { </span><br><span class=\"line\">\t\t\tresult.push ( a[ai] ); </span><br><span class=\"line\">\t\t\tai++; </span><br><span class=\"line\">\t\t\tbi++; </span><br><span class=\"line\">\t\t} </span><br><span class=\"line\">\t} </span><br><span class=\"line\">\t<span class=\"keyword\">return</span> result; </span><br><span class=\"line\">}</span><br></pre></td></tr></table></figure>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"comment\"># run</span></span><br><span class=\"line\">> var new_item = ArrayIntersection(a, b); </span><br><span class=\"line\">> console.log(new_item);</span><br><span class=\"line\">> [10, 50]</span><br></pre></td></tr></table></figure>\n","site":{"data":{}},"excerpt":"","more":"<blockquote>\n</blockquote>\n<p>案例说明:</p>\n<pre><code>某充话费充值渠道支持a充值列表,但某充值卡的面额只有b列表面额\n其中other表示其他输入金额 \nvar a = [5, 10, 20, 50, 100, 'other'];\n\nvar b = [10, 30, 50];\n理想结果:a渠道能支持b卡充值:[10, 50]\n</code></pre><figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"function\"><span class=\"keyword\">function</span> <span class=\"title\">ArrayIntersection</span>(<span class=\"params\"> a, b </span>) </span>{<span class=\"comment\">//合并数组 </span></span><br><span class=\"line\">\t<span class=\"keyword\">var</span> ai=<span class=\"number\">0</span>, bi=<span class=\"number\">0</span>; </span><br><span class=\"line\"> <span class=\"keyword\">var</span> result = <span class=\"keyword\">new</span> <span class=\"built_in\">Array</span>(); </span><br><span class=\"line\"> <span class=\"keyword\">while</span> ( ai < a.length && bi < b.length ) { </span><br><span class=\"line\"> \t<span class=\"keyword\">if</span> ( a[ai] < b[bi] ) { </span><br><span class=\"line\"> \tai++; </span><br><span class=\"line\">\t\t} <span class=\"keyword\">else</span> <span class=\"keyword\">if</span> ( a[ai] > b[bi] ) { </span><br><span class=\"line\">\t\t\tbi++;</span><br><span class=\"line\">\t\t} <span class=\"keyword\">else</span> { </span><br><span class=\"line\">\t\t\tresult.push ( a[ai] ); </span><br><span class=\"line\">\t\t\tai++; </span><br><span class=\"line\">\t\t\tbi++; </span><br><span class=\"line\">\t\t} </span><br><span class=\"line\">\t} </span><br><span class=\"line\">\t<span class=\"keyword\">return</span> result; </span><br><span class=\"line\">}</span><br></pre></td></tr></table></figure>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"comment\"># run</span></span><br><span class=\"line\">> var new_item = ArrayIntersection(a, b); </span><br><span class=\"line\">> console.log(new_item);</span><br><span class=\"line\">> [10, 50]</span><br></pre></td></tr></table></figure>\n"},{"title":"开篇","author":"Brian","date":"2010-05-11T06:22:00.000Z","_content":"Welcome to [Hexo](https://hexo.io/)! This is your very first post. Check [documentation](https://hexo.io/docs/) for more info. If you get any problems when using Hexo, you can find the answer in [troubleshooting](https://hexo.io/docs/troubleshooting.html) or you can ask me on [GitHub](https://github.com/hexojs/hexo/issues).\n\n## Quick Start\n\n### Create a new post\n\n``` bash\n$ hexo new \"My New Post\"\n```\n\nMore info: [Writing](https://hexo.io/docs/writing.html)\n\n### Run server\n\n``` bash\n$ hexo server\n```\n\nMore info: [Server](https://hexo.io/docs/server.html)\n\n### Generate static files\n\n``` bash\n$ hexo generate\n```\n\nMore info: [Generating](https://hexo.io/docs/generating.html)\n\n### Deploy to remote sites\n\n``` bash\n$ hexo deploy\n```\n\nMore info: [Deployment](https://hexo.io/docs/deployment.html)\n","source":"_posts/hello-world.md","raw":"title: 开篇\ntags: []\ncategories: []\nauthor: Brian\ndate: 2010-05-11 14:22:00\n---\nWelcome to [Hexo](https://hexo.io/)! This is your very first post. Check [documentation](https://hexo.io/docs/) for more info. If you get any problems when using Hexo, you can find the answer in [troubleshooting](https://hexo.io/docs/troubleshooting.html) or you can ask me on [GitHub](https://github.com/hexojs/hexo/issues).\n\n## Quick Start\n\n### Create a new post\n\n``` bash\n$ hexo new \"My New Post\"\n```\n\nMore info: [Writing](https://hexo.io/docs/writing.html)\n\n### Run server\n\n``` bash\n$ hexo server\n```\n\nMore info: [Server](https://hexo.io/docs/server.html)\n\n### Generate static files\n\n``` bash\n$ hexo generate\n```\n\nMore info: [Generating](https://hexo.io/docs/generating.html)\n\n### Deploy to remote sites\n\n``` bash\n$ hexo deploy\n```\n\nMore info: [Deployment](https://hexo.io/docs/deployment.html)\n","slug":"hello-world","published":1,"updated":"2018-11-15T09:28:53.217Z","comments":1,"layout":"post","photos":[],"link":"","_id":"cjoljzh0z00020evozh2rq7rq","content":"<p>Welcome to <a href=\"https://hexo.io/\" target=\"_blank\" rel=\"noopener\">Hexo</a>! This is your very first post. Check <a href=\"https://hexo.io/docs/\" target=\"_blank\" rel=\"noopener\">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href=\"https://hexo.io/docs/troubleshooting.html\" target=\"_blank\" rel=\"noopener\">troubleshooting</a> or you can ask me on <a href=\"https://github.com/hexojs/hexo/issues\" target=\"_blank\" rel=\"noopener\">GitHub</a>.</p>\n<h2 id=\"Quick-Start\"><a href=\"#Quick-Start\" class=\"headerlink\" title=\"Quick Start\"></a>Quick Start</h2><h3 id=\"Create-a-new-post\"><a href=\"#Create-a-new-post\" class=\"headerlink\" title=\"Create a new post\"></a>Create a new post</h3><figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">$ hexo new <span class=\"string\">\"My New Post\"</span></span><br></pre></td></tr></table></figure>\n<p>More info: <a href=\"https://hexo.io/docs/writing.html\" target=\"_blank\" rel=\"noopener\">Writing</a></p>\n<h3 id=\"Run-server\"><a href=\"#Run-server\" class=\"headerlink\" title=\"Run server\"></a>Run server</h3><figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">$ hexo server</span><br></pre></td></tr></table></figure>\n<p>More info: <a href=\"https://hexo.io/docs/server.html\" target=\"_blank\" rel=\"noopener\">Server</a></p>\n<h3 id=\"Generate-static-files\"><a href=\"#Generate-static-files\" class=\"headerlink\" title=\"Generate static files\"></a>Generate static files</h3><figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">$ hexo generate</span><br></pre></td></tr></table></figure>\n<p>More info: <a href=\"https://hexo.io/docs/generating.html\" target=\"_blank\" rel=\"noopener\">Generating</a></p>\n<h3 id=\"Deploy-to-remote-sites\"><a href=\"#Deploy-to-remote-sites\" class=\"headerlink\" title=\"Deploy to remote sites\"></a>Deploy to remote sites</h3><figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">$ hexo deploy</span><br></pre></td></tr></table></figure>\n<p>More info: <a href=\"https://hexo.io/docs/deployment.html\" target=\"_blank\" rel=\"noopener\">Deployment</a></p>\n","site":{"data":{}},"excerpt":"","more":"<p>Welcome to <a href=\"https://hexo.io/\" target=\"_blank\" rel=\"noopener\">Hexo</a>! This is your very first post. Check <a href=\"https://hexo.io/docs/\" target=\"_blank\" rel=\"noopener\">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href=\"https://hexo.io/docs/troubleshooting.html\" target=\"_blank\" rel=\"noopener\">troubleshooting</a> or you can ask me on <a href=\"https://github.com/hexojs/hexo/issues\" target=\"_blank\" rel=\"noopener\">GitHub</a>.</p>\n<h2 id=\"Quick-Start\"><a href=\"#Quick-Start\" class=\"headerlink\" title=\"Quick Start\"></a>Quick Start</h2><h3 id=\"Create-a-new-post\"><a href=\"#Create-a-new-post\" class=\"headerlink\" title=\"Create a new post\"></a>Create a new post</h3><figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">$ hexo new <span class=\"string\">\"My New Post\"</span></span><br></pre></td></tr></table></figure>\n<p>More info: <a href=\"https://hexo.io/docs/writing.html\" target=\"_blank\" rel=\"noopener\">Writing</a></p>\n<h3 id=\"Run-server\"><a href=\"#Run-server\" class=\"headerlink\" title=\"Run server\"></a>Run server</h3><figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">$ hexo server</span><br></pre></td></tr></table></figure>\n<p>More info: <a href=\"https://hexo.io/docs/server.html\" target=\"_blank\" rel=\"noopener\">Server</a></p>\n<h3 id=\"Generate-static-files\"><a href=\"#Generate-static-files\" class=\"headerlink\" title=\"Generate static files\"></a>Generate static files</h3><figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">$ hexo generate</span><br></pre></td></tr></table></figure>\n<p>More info: <a href=\"https://hexo.io/docs/generating.html\" target=\"_blank\" rel=\"noopener\">Generating</a></p>\n<h3 id=\"Deploy-to-remote-sites\"><a href=\"#Deploy-to-remote-sites\" class=\"headerlink\" title=\"Deploy to remote sites\"></a>Deploy to remote sites</h3><figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">$ hexo deploy</span><br></pre></td></tr></table></figure>\n<p>More info: <a href=\"https://hexo.io/docs/deployment.html\" target=\"_blank\" rel=\"noopener\">Deployment</a></p>\n"},{"title":"css修改滚动条默认样式","author":"brian","date":"2016-08-10T11:22:00.000Z","cover":"/img/2.jpg","_content":">写之前,先了解一下css3 webkit支持的特性\n\n\t::-webkit-scrollbar //滚动条整体部分 \n \t::-webkit-scrollbar-button //滚动条两端的按钮 \n \t::-webkit-scrollbar-track // 外层轨道 \n \t::-webkit-scrollbar-track-piece //内层轨道,滚动条中间部分(除去) \n \t::-webkit-scrollbar-thumb //滚动条里面可以拖动的那个 \n \t::-webkit-scrollbar-corner //边角 \n \t::-webkit-resizer ///定义右下角拖动块的样式 \n\n```scss\n/*scss 代码*/\n/*滚动条样式*/ \n .innerbox::-webkit-scrollbar {/*滚动条整体样式*/ \n width: 4px; /*高宽分别对应横竖滚动条的尺寸*/ \n height: 4px; \n } \n .innerbox::-webkit-scrollbar-thumb {/*滚动条里面小方块*/ \n border-radius: 5px; \n -webkit-box-shadow: inset 0 0 5px rgba(0,0,0,0.2); \n background: rgba(0,0,0,0.2); \n } \n .innerbox::-webkit-scrollbar-track {/*滚动条里面轨道*/ \n -webkit-box-shadow: inset 0 0 5px rgba(0,0,0,0.2); \n border-radius: 0; \n background: rgba(0,0,0,0.1); \n } \n```\n>设置scrollbar的为CSS伪元素,对应上图的数字:\n\n```bash\n::-webkit-scrollbar { /* 1 */ } \n::-webkit-scrollbar-button { /* 2 */ } \n::-webkit-scrollbar-track { /* 3 */ } \n::-webkit-scrollbar-track-piece { /* 4 */ } \n::-webkit-scrollbar-thumb { /* 5 */ } \n::-webkit-scrollbar-corner { /* 6 */ } \n::-webkit-resizer { /* 7 */ } \n```","source":"_posts/css修改滚动条默认样式.md","raw":"title: css修改滚动条默认样式\nauthor: brian\ntags:\n - css3\n - ''\n - ''\ncategories: []\ndate: 2016-08-10 19:22:00\ncover: /img/2.jpg\n---\n>写之前,先了解一下css3 webkit支持的特性\n\n\t::-webkit-scrollbar //滚动条整体部分 \n \t::-webkit-scrollbar-button //滚动条两端的按钮 \n \t::-webkit-scrollbar-track // 外层轨道 \n \t::-webkit-scrollbar-track-piece //内层轨道,滚动条中间部分(除去) \n \t::-webkit-scrollbar-thumb //滚动条里面可以拖动的那个 \n \t::-webkit-scrollbar-corner //边角 \n \t::-webkit-resizer ///定义右下角拖动块的样式 \n\n```scss\n/*scss 代码*/\n/*滚动条样式*/ \n .innerbox::-webkit-scrollbar {/*滚动条整体样式*/ \n width: 4px; /*高宽分别对应横竖滚动条的尺寸*/ \n height: 4px; \n } \n .innerbox::-webkit-scrollbar-thumb {/*滚动条里面小方块*/ \n border-radius: 5px; \n -webkit-box-shadow: inset 0 0 5px rgba(0,0,0,0.2); \n background: rgba(0,0,0,0.2); \n } \n .innerbox::-webkit-scrollbar-track {/*滚动条里面轨道*/ \n -webkit-box-shadow: inset 0 0 5px rgba(0,0,0,0.2); \n border-radius: 0; \n background: rgba(0,0,0,0.1); \n } \n```\n>设置scrollbar的为CSS伪元素,对应上图的数字:\n\n```bash\n::-webkit-scrollbar { /* 1 */ } \n::-webkit-scrollbar-button { /* 2 */ } \n::-webkit-scrollbar-track { /* 3 */ } \n::-webkit-scrollbar-track-piece { /* 4 */ } \n::-webkit-scrollbar-thumb { /* 5 */ } \n::-webkit-scrollbar-corner { /* 6 */ } \n::-webkit-resizer { /* 7 */ } \n```","slug":"css修改滚动条默认样式","published":1,"updated":"2018-08-30T10:27:43.962Z","comments":1,"layout":"post","photos":[],"link":"","_id":"cjoljzh1200040evopjfeat60","content":"<blockquote>\n<p>写之前,先了解一下css3 webkit支持的特性</p>\n</blockquote>\n<pre><code>::-webkit-scrollbar //滚动条整体部分 \n ::-webkit-scrollbar-button //滚动条两端的按钮 \n ::-webkit-scrollbar-track // 外层轨道 \n ::-webkit-scrollbar-track-piece //内层轨道,滚动条中间部分(除去) \n ::-webkit-scrollbar-thumb //滚动条里面可以拖动的那个 \n ::-webkit-scrollbar-corner //边角 \n ::-webkit-resizer ///定义右下角拖动块的样式 \n</code></pre><figure class=\"highlight scss\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"comment\">/*scss 代码*/</span></span><br><span class=\"line\"><span class=\"comment\">/*滚动条样式*/</span> </span><br><span class=\"line\"> <span class=\"selector-class\">.innerbox</span>::-webkit-scrollbar {/*滚动条整体样式*/ </span><br><span class=\"line\"> width: <span class=\"number\">4px</span>; <span class=\"comment\">/*高宽分别对应横竖滚动条的尺寸*/</span> </span><br><span class=\"line\"> <span class=\"attribute\">height</span>: <span class=\"number\">4px</span>; </span><br><span class=\"line\"> } </span><br><span class=\"line\"> <span class=\"selector-class\">.innerbox</span>::-webkit-scrollbar-thumb {/*滚动条里面小方块*/ </span><br><span class=\"line\"> border-radius: <span class=\"number\">5px</span>; </span><br><span class=\"line\"> -webkit-<span class=\"attribute\">box-shadow</span>: inset <span class=\"number\">0</span> <span class=\"number\">0</span> <span class=\"number\">5px</span> rgba(<span class=\"number\">0</span>,<span class=\"number\">0</span>,<span class=\"number\">0</span>,<span class=\"number\">0.2</span>); </span><br><span class=\"line\"> <span class=\"attribute\">background</span>: rgba(<span class=\"number\">0</span>,<span class=\"number\">0</span>,<span class=\"number\">0</span>,<span class=\"number\">0.2</span>); </span><br><span class=\"line\"> } </span><br><span class=\"line\"> <span class=\"selector-class\">.innerbox</span>::-webkit-scrollbar-track {/*滚动条里面轨道*/ </span><br><span class=\"line\"> -webkit-box-shadow: inset <span class=\"number\">0</span> <span class=\"number\">0</span> <span class=\"number\">5px</span> rgba(<span class=\"number\">0</span>,<span class=\"number\">0</span>,<span class=\"number\">0</span>,<span class=\"number\">0.2</span>); </span><br><span class=\"line\"> <span class=\"attribute\">border-radius</span>: <span class=\"number\">0</span>; </span><br><span class=\"line\"> <span class=\"attribute\">background</span>: rgba(<span class=\"number\">0</span>,<span class=\"number\">0</span>,<span class=\"number\">0</span>,<span class=\"number\">0.1</span>); </span><br><span class=\"line\"> }</span><br></pre></td></tr></table></figure>\n<blockquote>\n<p>设置scrollbar的为CSS伪元素,对应上图的数字:</p>\n</blockquote>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">::-webkit-scrollbar { /* 1 */ } </span><br><span class=\"line\">::-webkit-scrollbar-button { /* 2 */ } </span><br><span class=\"line\">::-webkit-scrollbar-track { /* 3 */ } </span><br><span class=\"line\">::-webkit-scrollbar-track-piece { /* 4 */ } </span><br><span class=\"line\">::-webkit-scrollbar-thumb { /* 5 */ } </span><br><span class=\"line\">::-webkit-scrollbar-corner { /* 6 */ } </span><br><span class=\"line\">::-webkit-resizer { /* 7 */ }</span><br></pre></td></tr></table></figure>","site":{"data":{}},"excerpt":"","more":"<blockquote>\n<p>写之前,先了解一下css3 webkit支持的特性</p>\n</blockquote>\n<pre><code>::-webkit-scrollbar //滚动条整体部分 \n ::-webkit-scrollbar-button //滚动条两端的按钮 \n ::-webkit-scrollbar-track // 外层轨道 \n ::-webkit-scrollbar-track-piece //内层轨道,滚动条中间部分(除去) \n ::-webkit-scrollbar-thumb //滚动条里面可以拖动的那个 \n ::-webkit-scrollbar-corner //边角 \n ::-webkit-resizer ///定义右下角拖动块的样式 \n</code></pre><figure class=\"highlight scss\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"comment\">/*scss 代码*/</span></span><br><span class=\"line\"><span class=\"comment\">/*滚动条样式*/</span> </span><br><span class=\"line\"> <span class=\"selector-class\">.innerbox</span>::-webkit-scrollbar {/*滚动条整体样式*/ </span><br><span class=\"line\"> width: <span class=\"number\">4px</span>; <span class=\"comment\">/*高宽分别对应横竖滚动条的尺寸*/</span> </span><br><span class=\"line\"> <span class=\"attribute\">height</span>: <span class=\"number\">4px</span>; </span><br><span class=\"line\"> } </span><br><span class=\"line\"> <span class=\"selector-class\">.innerbox</span>::-webkit-scrollbar-thumb {/*滚动条里面小方块*/ </span><br><span class=\"line\"> border-radius: <span class=\"number\">5px</span>; </span><br><span class=\"line\"> -webkit-<span class=\"attribute\">box-shadow</span>: inset <span class=\"number\">0</span> <span class=\"number\">0</span> <span class=\"number\">5px</span> rgba(<span class=\"number\">0</span>,<span class=\"number\">0</span>,<span class=\"number\">0</span>,<span class=\"number\">0.2</span>); </span><br><span class=\"line\"> <span class=\"attribute\">background</span>: rgba(<span class=\"number\">0</span>,<span class=\"number\">0</span>,<span class=\"number\">0</span>,<span class=\"number\">0.2</span>); </span><br><span class=\"line\"> } </span><br><span class=\"line\"> <span class=\"selector-class\">.innerbox</span>::-webkit-scrollbar-track {/*滚动条里面轨道*/ </span><br><span class=\"line\"> -webkit-box-shadow: inset <span class=\"number\">0</span> <span class=\"number\">0</span> <span class=\"number\">5px</span> rgba(<span class=\"number\">0</span>,<span class=\"number\">0</span>,<span class=\"number\">0</span>,<span class=\"number\">0.2</span>); </span><br><span class=\"line\"> <span class=\"attribute\">border-radius</span>: <span class=\"number\">0</span>; </span><br><span class=\"line\"> <span class=\"attribute\">background</span>: rgba(<span class=\"number\">0</span>,<span class=\"number\">0</span>,<span class=\"number\">0</span>,<span class=\"number\">0.1</span>); </span><br><span class=\"line\"> }</span><br></pre></td></tr></table></figure>\n<blockquote>\n<p>设置scrollbar的为CSS伪元素,对应上图的数字:</p>\n</blockquote>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">::-webkit-scrollbar { /* 1 */ } </span><br><span class=\"line\">::-webkit-scrollbar-button { /* 2 */ } </span><br><span class=\"line\">::-webkit-scrollbar-track { /* 3 */ } </span><br><span class=\"line\">::-webkit-scrollbar-track-piece { /* 4 */ } </span><br><span class=\"line\">::-webkit-scrollbar-thumb { /* 5 */ } </span><br><span class=\"line\">::-webkit-scrollbar-corner { /* 6 */ } </span><br><span class=\"line\">::-webkit-resizer { /* 7 */ }</span><br></pre></td></tr></table></figure>"},{"title":"javascript正则表达式提取指定字符","author":"brian","date":"2013-08-19T01:36:00.000Z","_content":"```js\n\nvar url = \"http://v.youku.com/v_show/id_XMjM0OTI4NzMy.html\";\nfunction getInfo(source,start,end){\n var oReg=new RegExp(start+\".*?\"+end,\"img\");\n var oRegStart=new RegExp(start,\"g\");\n var oRegEnd=new RegExp(end,\"g\");\n return source.match(oReg).join(\"=\").replace(oRegStart,\"\").replace(oRegEnd,\"\").split(\"=\");\n}\ngetInfo(url, \"id_\", \".html\");\n```\n\n```bash\n> XMjM0OTI4NzMy\n```","source":"_posts/javascript正则表达式提取参数.md","raw":"title: javascript正则表达式提取指定字符\nauthor: brian\ntags:\n - js\ncategories: []\ndate: 2013-08-19 09:36:00\n---\n```js\n\nvar url = \"http://v.youku.com/v_show/id_XMjM0OTI4NzMy.html\";\nfunction getInfo(source,start,end){\n var oReg=new RegExp(start+\".*?\"+end,\"img\");\n var oRegStart=new RegExp(start,\"g\");\n var oRegEnd=new RegExp(end,\"g\");\n return source.match(oReg).join(\"=\").replace(oRegStart,\"\").replace(oRegEnd,\"\").split(\"=\");\n}\ngetInfo(url, \"id_\", \".html\");\n```\n\n```bash\n> XMjM0OTI4NzMy\n```","slug":"javascript正则表达式提取参数","published":1,"updated":"2018-08-30T10:22:10.731Z","comments":1,"layout":"post","photos":[],"link":"","_id":"cjoljzh1f00080evowklnxplg","content":"<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"></span><br><span class=\"line\"><span class=\"keyword\">var</span> url = <span class=\"string\">\"http://v.youku.com/v_show/id_XMjM0OTI4NzMy.html\"</span>;</span><br><span class=\"line\"><span class=\"function\"><span class=\"keyword\">function</span> <span class=\"title\">getInfo</span>(<span class=\"params\">source,start,end</span>)</span>{</span><br><span class=\"line\"> <span class=\"keyword\">var</span> oReg=<span class=\"keyword\">new</span> <span class=\"built_in\">RegExp</span>(start+<span class=\"string\">\".*?\"</span>+end,<span class=\"string\">\"img\"</span>);</span><br><span class=\"line\"> <span class=\"keyword\">var</span> oRegStart=<span class=\"keyword\">new</span> <span class=\"built_in\">RegExp</span>(start,<span class=\"string\">\"g\"</span>);</span><br><span class=\"line\"> <span class=\"keyword\">var</span> oRegEnd=<span class=\"keyword\">new</span> <span class=\"built_in\">RegExp</span>(end,<span class=\"string\">\"g\"</span>);</span><br><span class=\"line\"> <span class=\"keyword\">return</span> source.match(oReg).join(<span class=\"string\">\"=\"</span>).replace(oRegStart,<span class=\"string\">\"\"</span>).replace(oRegEnd,<span class=\"string\">\"\"</span>).split(<span class=\"string\">\"=\"</span>);</span><br><span class=\"line\">}</span><br><span class=\"line\">getInfo(url, <span class=\"string\">\"id_\"</span>, <span class=\"string\">\".html\"</span>);</span><br></pre></td></tr></table></figure>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">> XMjM0OTI4NzMy</span><br></pre></td></tr></table></figure>","site":{"data":{}},"excerpt":"","more":"<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"></span><br><span class=\"line\"><span class=\"keyword\">var</span> url = <span class=\"string\">\"http://v.youku.com/v_show/id_XMjM0OTI4NzMy.html\"</span>;</span><br><span class=\"line\"><span class=\"function\"><span class=\"keyword\">function</span> <span class=\"title\">getInfo</span>(<span class=\"params\">source,start,end</span>)</span>{</span><br><span class=\"line\"> <span class=\"keyword\">var</span> oReg=<span class=\"keyword\">new</span> <span class=\"built_in\">RegExp</span>(start+<span class=\"string\">\".*?\"</span>+end,<span class=\"string\">\"img\"</span>);</span><br><span class=\"line\"> <span class=\"keyword\">var</span> oRegStart=<span class=\"keyword\">new</span> <span class=\"built_in\">RegExp</span>(start,<span class=\"string\">\"g\"</span>);</span><br><span class=\"line\"> <span class=\"keyword\">var</span> oRegEnd=<span class=\"keyword\">new</span> <span class=\"built_in\">RegExp</span>(end,<span class=\"string\">\"g\"</span>);</span><br><span class=\"line\"> <span class=\"keyword\">return</span> source.match(oReg).join(<span class=\"string\">\"=\"</span>).replace(oRegStart,<span class=\"string\">\"\"</span>).replace(oRegEnd,<span class=\"string\">\"\"</span>).split(<span class=\"string\">\"=\"</span>);</span><br><span class=\"line\">}</span><br><span class=\"line\">getInfo(url, <span class=\"string\">\"id_\"</span>, <span class=\"string\">\".html\"</span>);</span><br></pre></td></tr></table></figure>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">> XMjM0OTI4NzMy</span><br></pre></td></tr></table></figure>"},{"title":"javascript获取Flash下载进度兼容IE, FireFox,Chrome浏览器","author":"brian","date":"2011-08-09T15:25:00.000Z","_content":"```html\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />\n<title>flash load progress</title>\n<script>\n</script>\n</head>\n<body>\n<p><span class=\"intro\">下载进度:</span>\n <p class=\"barborder\">\n <p class=\"bar\" id=\"loadpercent\"></p>\n </p>\n</p>\n<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\" width=\"700px\" height=\"350\" id=\"ieGame\">\n <param name=\"movie\" value=\"http://360.kuaiwan.com/games/6/6C36DF75DE92FF1148592FF0F093E5C00DA17A91.swf\">\n <param name=\"quality\" value=\"high\"><param name=\"wmode\" value=\"transparent\">\n <embed \n wmode=\"transparent\" src=\"http://360.kuaiwan.com/games/6/6C36DF75DE92FF1148592FF0F093E5C00DA17A91.swf\" \n quality=\"high\" \n type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" \n width=\"700px\" height=\"350px\" id=\"fireFoxGame\" name=\"fireFoxGame\">\n </embed>\n</object>\n</body>\n</html>\n```\n\n```js\nif(window.navigator.userAgent.indexOf(\"Firefox\")>=1 || window.navigator.userAgent.indexOf(\"AppleWebKit\")>=1) {\n /*is fireFox*/\n var progress = self.setInterval(function() { \n var progress = document.fireFoxGame;\n document.getElementById(\"loadpercent\").innerHTML=\"\"+progress.PercentLoaded();\n }, 5);\n //document.getElementById(\"loadpercent\").innerHTML=\"\"+Math.abs(progress)+\"%\";\n } else { /*is IE*/\n\trefreshProgress();\n }\n function show(){//隐藏AD层、显示FLASH层\n document.getElementById(\"showAD\").style.display='none';\n //document.getElementById(\"showSWF\").style.display='block';\n }\n \n function refreshProgress() { //刷新进度条函数\n var bar = document.getElementById(\"loadpercent\");\n var movie = document.getElementById(\"ieGame\");\n var nPercentLoaded = Math.abs(movie.PercentLoaded());\n bar.innerHTML= nPercentLoaded+\"%\";\n if(nPercentLoaded==100) {\n bar.innerHTML= \"Game download is complete\";\n } else {\n setTimeout('refreshProgress()',0);\n }\n }\n```","source":"_posts/javascript获取Flash下载进度兼容IE-FireFox-Chrome浏览器.md","raw":"title: 'javascript获取Flash下载进度兼容IE, FireFox,Chrome浏览器'\nauthor: brian\ntags:\n - html\n - javascript\ncategories: []\ndate: 2011-08-09 23:25:00\n---\n```html\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />\n<title>flash load progress</title>\n<script>\n</script>\n</head>\n<body>\n<p><span class=\"intro\">下载进度:</span>\n <p class=\"barborder\">\n <p class=\"bar\" id=\"loadpercent\"></p>\n </p>\n</p>\n<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\" width=\"700px\" height=\"350\" id=\"ieGame\">\n <param name=\"movie\" value=\"http://360.kuaiwan.com/games/6/6C36DF75DE92FF1148592FF0F093E5C00DA17A91.swf\">\n <param name=\"quality\" value=\"high\"><param name=\"wmode\" value=\"transparent\">\n <embed \n wmode=\"transparent\" src=\"http://360.kuaiwan.com/games/6/6C36DF75DE92FF1148592FF0F093E5C00DA17A91.swf\" \n quality=\"high\" \n type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" \n width=\"700px\" height=\"350px\" id=\"fireFoxGame\" name=\"fireFoxGame\">\n </embed>\n</object>\n</body>\n</html>\n```\n\n```js\nif(window.navigator.userAgent.indexOf(\"Firefox\")>=1 || window.navigator.userAgent.indexOf(\"AppleWebKit\")>=1) {\n /*is fireFox*/\n var progress = self.setInterval(function() { \n var progress = document.fireFoxGame;\n document.getElementById(\"loadpercent\").innerHTML=\"\"+progress.PercentLoaded();\n }, 5);\n //document.getElementById(\"loadpercent\").innerHTML=\"\"+Math.abs(progress)+\"%\";\n } else { /*is IE*/\n\trefreshProgress();\n }\n function show(){//隐藏AD层、显示FLASH层\n document.getElementById(\"showAD\").style.display='none';\n //document.getElementById(\"showSWF\").style.display='block';\n }\n \n function refreshProgress() { //刷新进度条函数\n var bar = document.getElementById(\"loadpercent\");\n var movie = document.getElementById(\"ieGame\");\n var nPercentLoaded = Math.abs(movie.PercentLoaded());\n bar.innerHTML= nPercentLoaded+\"%\";\n if(nPercentLoaded==100) {\n bar.innerHTML= \"Game download is complete\";\n } else {\n setTimeout('refreshProgress()',0);\n }\n }\n```","slug":"javascript获取Flash下载进度兼容IE-FireFox-Chrome浏览器","published":1,"updated":"2018-08-30T10:43:14.225Z","comments":1,"layout":"post","photos":[],"link":"","_id":"cjoljzh1h000a0evoo5kml2lp","content":"<figure class=\"highlight html\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br><span class=\"line\">17</span><br><span class=\"line\">18</span><br><span class=\"line\">19</span><br><span class=\"line\">20</span><br><span class=\"line\">21</span><br><span class=\"line\">22</span><br><span class=\"line\">23</span><br><span class=\"line\">24</span><br><span class=\"line\">25</span><br><span class=\"line\">26</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"meta\"><!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"></span></span><br><span class=\"line\"><span class=\"tag\"><<span class=\"name\">html</span> <span class=\"attr\">xmlns</span>=<span class=\"string\">\"http://www.w3.org/1999/xhtml\"</span>></span></span><br><span class=\"line\"><span class=\"tag\"><<span class=\"name\">head</span>></span></span><br><span class=\"line\"><span class=\"tag\"><<span class=\"name\">meta</span> <span class=\"attr\">http-equiv</span>=<span class=\"string\">\"Content-Type\"</span> <span class=\"attr\">content</span>=<span class=\"string\">\"text/html; charset=gb2312\"</span> /></span></span><br><span class=\"line\"><span class=\"tag\"><<span class=\"name\">title</span>></span>flash load progress<span class=\"tag\"></<span class=\"name\">title</span>></span></span><br><span class=\"line\"><span class=\"tag\"><<span class=\"name\">script</span>></span><span class=\"undefined\"></span></span><br><span class=\"line\"><span class=\"undefined\"></span><span class=\"tag\"></<span class=\"name\">script</span>></span></span><br><span class=\"line\"><span class=\"tag\"></<span class=\"name\">head</span>></span></span><br><span class=\"line\"><span class=\"tag\"><<span class=\"name\">body</span>></span></span><br><span class=\"line\"><span class=\"tag\"><<span class=\"name\">p</span>></span><span class=\"tag\"><<span class=\"name\">span</span> <span class=\"attr\">class</span>=<span class=\"string\">\"intro\"</span>></span>下载进度:<span class=\"tag\"></<span class=\"name\">span</span>></span></span><br><span class=\"line\"> <span class=\"tag\"><<span class=\"name\">p</span> <span class=\"attr\">class</span>=<span class=\"string\">\"barborder\"</span>></span></span><br><span class=\"line\"> <span class=\"tag\"><<span class=\"name\">p</span> <span class=\"attr\">class</span>=<span class=\"string\">\"bar\"</span> <span class=\"attr\">id</span>=<span class=\"string\">\"loadpercent\"</span>></span><span class=\"tag\"></<span class=\"name\">p</span>></span></span><br><span class=\"line\"> <span class=\"tag\"></<span class=\"name\">p</span>></span></span><br><span class=\"line\"><span class=\"tag\"></<span class=\"name\">p</span>></span></span><br><span class=\"line\"><span class=\"tag\"><<span class=\"name\">object</span> <span class=\"attr\">classid</span>=<span class=\"string\">\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"</span> <span class=\"attr\">codebase</span>=<span class=\"string\">\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\"</span> <span class=\"attr\">width</span>=<span class=\"string\">\"700px\"</span> <span class=\"attr\">height</span>=<span class=\"string\">\"350\"</span> <span class=\"attr\">id</span>=<span class=\"string\">\"ieGame\"</span>></span></span><br><span class=\"line\"> <span class=\"tag\"><<span class=\"name\">param</span> <span class=\"attr\">name</span>=<span class=\"string\">\"movie\"</span> <span class=\"attr\">value</span>=<span class=\"string\">\"http://360.kuaiwan.com/games/6/6C36DF75DE92FF1148592FF0F093E5C00DA17A91.swf\"</span>></span></span><br><span class=\"line\"> <span class=\"tag\"><<span class=\"name\">param</span> <span class=\"attr\">name</span>=<span class=\"string\">\"quality\"</span> <span class=\"attr\">value</span>=<span class=\"string\">\"high\"</span>></span><span class=\"tag\"><<span class=\"name\">param</span> <span class=\"attr\">name</span>=<span class=\"string\">\"wmode\"</span> <span class=\"attr\">value</span>=<span class=\"string\">\"transparent\"</span>></span></span><br><span class=\"line\"> <span class=\"tag\"><<span class=\"name\">embed</span> </span></span><br><span class=\"line\"><span class=\"tag\"> <span class=\"attr\">wmode</span>=<span class=\"string\">\"transparent\"</span> <span class=\"attr\">src</span>=<span class=\"string\">\"http://360.kuaiwan.com/games/6/6C36DF75DE92FF1148592FF0F093E5C00DA17A91.swf\"</span> </span></span><br><span class=\"line\"><span class=\"tag\"> <span class=\"attr\">quality</span>=<span class=\"string\">\"high\"</span> </span></span><br><span class=\"line\"><span class=\"tag\"> <span class=\"attr\">type</span>=<span class=\"string\">\"application/x-shockwave-flash\"</span> <span class=\"attr\">pluginspage</span>=<span class=\"string\">\"http://www.macromedia.com/go/getflashplayer\"</span> </span></span><br><span class=\"line\"><span class=\"tag\"> <span class=\"attr\">width</span>=<span class=\"string\">\"700px\"</span> <span class=\"attr\">height</span>=<span class=\"string\">\"350px\"</span> <span class=\"attr\">id</span>=<span class=\"string\">\"fireFoxGame\"</span> <span class=\"attr\">name</span>=<span class=\"string\">\"fireFoxGame\"</span>></span></span><br><span class=\"line\"> <span class=\"tag\"></<span class=\"name\">embed</span>></span></span><br><span class=\"line\"><span class=\"tag\"></<span class=\"name\">object</span>></span></span><br><span class=\"line\"><span class=\"tag\"></<span class=\"name\">body</span>></span></span><br><span class=\"line\"><span class=\"tag\"></<span class=\"name\">html</span>></span></span><br></pre></td></tr></table></figure>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br><span class=\"line\">17</span><br><span class=\"line\">18</span><br><span class=\"line\">19</span><br><span class=\"line\">20</span><br><span class=\"line\">21</span><br><span class=\"line\">22</span><br><span class=\"line\">23</span><br><span class=\"line\">24</span><br><span class=\"line\">25</span><br><span class=\"line\">26</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"keyword\">if</span>(<span class=\"built_in\">window</span>.navigator.userAgent.indexOf(<span class=\"string\">\"Firefox\"</span>)>=<span class=\"number\">1</span> || <span class=\"built_in\">window</span>.navigator.userAgent.indexOf(<span class=\"string\">\"AppleWebKit\"</span>)>=<span class=\"number\">1</span>) {</span><br><span class=\"line\"> <span class=\"comment\">/*is fireFox*/</span></span><br><span class=\"line\"> <span class=\"keyword\">var</span> progress = self.setInterval(<span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\"></span>) </span>{ </span><br><span class=\"line\"> <span class=\"keyword\">var</span> progress = <span class=\"built_in\">document</span>.fireFoxGame;</span><br><span class=\"line\"> <span class=\"built_in\">document</span>.getElementById(<span class=\"string\">\"loadpercent\"</span>).innerHTML=<span class=\"string\">\"\"</span>+progress.PercentLoaded();</span><br><span class=\"line\"> }, <span class=\"number\">5</span>);</span><br><span class=\"line\"> <span class=\"comment\">//document.getElementById(\"loadpercent\").innerHTML=\"\"+Math.abs(progress)+\"%\";</span></span><br><span class=\"line\"> } <span class=\"keyword\">else</span> { <span class=\"comment\">/*is IE*/</span></span><br><span class=\"line\">\trefreshProgress();</span><br><span class=\"line\"> }</span><br><span class=\"line\"> <span class=\"function\"><span class=\"keyword\">function</span> <span class=\"title\">show</span>(<span class=\"params\"></span>)</span>{<span class=\"comment\">//隐藏AD层、显示FLASH层</span></span><br><span class=\"line\"> <span class=\"built_in\">document</span>.getElementById(<span class=\"string\">\"showAD\"</span>).style.display=<span class=\"string\">'none'</span>;</span><br><span class=\"line\"> <span class=\"comment\">//document.getElementById(\"showSWF\").style.display='block';</span></span><br><span class=\"line\"> }</span><br><span class=\"line\"> </span><br><span class=\"line\"> <span class=\"function\"><span class=\"keyword\">function</span> <span class=\"title\">refreshProgress</span>(<span class=\"params\"></span>) </span>{ <span class=\"comment\">//刷新进度条函数</span></span><br><span class=\"line\"> <span class=\"keyword\">var</span> bar = <span class=\"built_in\">document</span>.getElementById(<span class=\"string\">\"loadpercent\"</span>);</span><br><span class=\"line\"> <span class=\"keyword\">var</span> movie = <span class=\"built_in\">document</span>.getElementById(<span class=\"string\">\"ieGame\"</span>);</span><br><span class=\"line\"> <span class=\"keyword\">var</span> nPercentLoaded = <span class=\"built_in\">Math</span>.abs(movie.PercentLoaded());</span><br><span class=\"line\"> bar.innerHTML= nPercentLoaded+<span class=\"string\">\"%\"</span>;</span><br><span class=\"line\"> <span class=\"keyword\">if</span>(nPercentLoaded==<span class=\"number\">100</span>) {</span><br><span class=\"line\"> bar.innerHTML= <span class=\"string\">\"Game download is complete\"</span>;</span><br><span class=\"line\"> } <span class=\"keyword\">else</span> {</span><br><span class=\"line\"> setTimeout(<span class=\"string\">'refreshProgress()'</span>,<span class=\"number\">0</span>);</span><br><span class=\"line\"> }</span><br><span class=\"line\"> }</span><br></pre></td></tr></table></figure>","site":{"data":{}},"excerpt":"","more":"<figure class=\"highlight html\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br><span class=\"line\">17</span><br><span class=\"line\">18</span><br><span class=\"line\">19</span><br><span class=\"line\">20</span><br><span class=\"line\">21</span><br><span class=\"line\">22</span><br><span class=\"line\">23</span><br><span class=\"line\">24</span><br><span class=\"line\">25</span><br><span class=\"line\">26</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"meta\"><!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"></span></span><br><span class=\"line\"><span class=\"tag\"><<span class=\"name\">html</span> <span class=\"attr\">xmlns</span>=<span class=\"string\">\"http://www.w3.org/1999/xhtml\"</span>></span></span><br><span class=\"line\"><span class=\"tag\"><<span class=\"name\">head</span>></span></span><br><span class=\"line\"><span class=\"tag\"><<span class=\"name\">meta</span> <span class=\"attr\">http-equiv</span>=<span class=\"string\">\"Content-Type\"</span> <span class=\"attr\">content</span>=<span class=\"string\">\"text/html; charset=gb2312\"</span> /></span></span><br><span class=\"line\"><span class=\"tag\"><<span class=\"name\">title</span>></span>flash load progress<span class=\"tag\"></<span class=\"name\">title</span>></span></span><br><span class=\"line\"><span class=\"tag\"><<span class=\"name\">script</span>></span><span class=\"undefined\"></span></span><br><span class=\"line\"><span class=\"undefined\"></span><span class=\"tag\"></<span class=\"name\">script</span>></span></span><br><span class=\"line\"><span class=\"tag\"></<span class=\"name\">head</span>></span></span><br><span class=\"line\"><span class=\"tag\"><<span class=\"name\">body</span>></span></span><br><span class=\"line\"><span class=\"tag\"><<span class=\"name\">p</span>></span><span class=\"tag\"><<span class=\"name\">span</span> <span class=\"attr\">class</span>=<span class=\"string\">\"intro\"</span>></span>下载进度:<span class=\"tag\"></<span class=\"name\">span</span>></span></span><br><span class=\"line\"> <span class=\"tag\"><<span class=\"name\">p</span> <span class=\"attr\">class</span>=<span class=\"string\">\"barborder\"</span>></span></span><br><span class=\"line\"> <span class=\"tag\"><<span class=\"name\">p</span> <span class=\"attr\">class</span>=<span class=\"string\">\"bar\"</span> <span class=\"attr\">id</span>=<span class=\"string\">\"loadpercent\"</span>></span><span class=\"tag\"></<span class=\"name\">p</span>></span></span><br><span class=\"line\"> <span class=\"tag\"></<span class=\"name\">p</span>></span></span><br><span class=\"line\"><span class=\"tag\"></<span class=\"name\">p</span>></span></span><br><span class=\"line\"><span class=\"tag\"><<span class=\"name\">object</span> <span class=\"attr\">classid</span>=<span class=\"string\">\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"</span> <span class=\"attr\">codebase</span>=<span class=\"string\">\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\"</span> <span class=\"attr\">width</span>=<span class=\"string\">\"700px\"</span> <span class=\"attr\">height</span>=<span class=\"string\">\"350\"</span> <span class=\"attr\">id</span>=<span class=\"string\">\"ieGame\"</span>></span></span><br><span class=\"line\"> <span class=\"tag\"><<span class=\"name\">param</span> <span class=\"attr\">name</span>=<span class=\"string\">\"movie\"</span> <span class=\"attr\">value</span>=<span class=\"string\">\"http://360.kuaiwan.com/games/6/6C36DF75DE92FF1148592FF0F093E5C00DA17A91.swf\"</span>></span></span><br><span class=\"line\"> <span class=\"tag\"><<span class=\"name\">param</span> <span class=\"attr\">name</span>=<span class=\"string\">\"quality\"</span> <span class=\"attr\">value</span>=<span class=\"string\">\"high\"</span>></span><span class=\"tag\"><<span class=\"name\">param</span> <span class=\"attr\">name</span>=<span class=\"string\">\"wmode\"</span> <span class=\"attr\">value</span>=<span class=\"string\">\"transparent\"</span>></span></span><br><span class=\"line\"> <span class=\"tag\"><<span class=\"name\">embed</span> </span></span><br><span class=\"line\"><span class=\"tag\"> <span class=\"attr\">wmode</span>=<span class=\"string\">\"transparent\"</span> <span class=\"attr\">src</span>=<span class=\"string\">\"http://360.kuaiwan.com/games/6/6C36DF75DE92FF1148592FF0F093E5C00DA17A91.swf\"</span> </span></span><br><span class=\"line\"><span class=\"tag\"> <span class=\"attr\">quality</span>=<span class=\"string\">\"high\"</span> </span></span><br><span class=\"line\"><span class=\"tag\"> <span class=\"attr\">type</span>=<span class=\"string\">\"application/x-shockwave-flash\"</span> <span class=\"attr\">pluginspage</span>=<span class=\"string\">\"http://www.macromedia.com/go/getflashplayer\"</span> </span></span><br><span class=\"line\"><span class=\"tag\"> <span class=\"attr\">width</span>=<span class=\"string\">\"700px\"</span> <span class=\"attr\">height</span>=<span class=\"string\">\"350px\"</span> <span class=\"attr\">id</span>=<span class=\"string\">\"fireFoxGame\"</span> <span class=\"attr\">name</span>=<span class=\"string\">\"fireFoxGame\"</span>></span></span><br><span class=\"line\"> <span class=\"tag\"></<span class=\"name\">embed</span>></span></span><br><span class=\"line\"><span class=\"tag\"></<span class=\"name\">object</span>></span></span><br><span class=\"line\"><span class=\"tag\"></<span class=\"name\">body</span>></span></span><br><span class=\"line\"><span class=\"tag\"></<span class=\"name\">html</span>></span></span><br></pre></td></tr></table></figure>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br><span class=\"line\">17</span><br><span class=\"line\">18</span><br><span class=\"line\">19</span><br><span class=\"line\">20</span><br><span class=\"line\">21</span><br><span class=\"line\">22</span><br><span class=\"line\">23</span><br><span class=\"line\">24</span><br><span class=\"line\">25</span><br><span class=\"line\">26</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"keyword\">if</span>(<span class=\"built_in\">window</span>.navigator.userAgent.indexOf(<span class=\"string\">\"Firefox\"</span>)>=<span class=\"number\">1</span> || <span class=\"built_in\">window</span>.navigator.userAgent.indexOf(<span class=\"string\">\"AppleWebKit\"</span>)>=<span class=\"number\">1</span>) {</span><br><span class=\"line\"> <span class=\"comment\">/*is fireFox*/</span></span><br><span class=\"line\"> <span class=\"keyword\">var</span> progress = self.setInterval(<span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\"></span>) </span>{ </span><br><span class=\"line\"> <span class=\"keyword\">var</span> progress = <span class=\"built_in\">document</span>.fireFoxGame;</span><br><span class=\"line\"> <span class=\"built_in\">document</span>.getElementById(<span class=\"string\">\"loadpercent\"</span>).innerHTML=<span class=\"string\">\"\"</span>+progress.PercentLoaded();</span><br><span class=\"line\"> }, <span class=\"number\">5</span>);</span><br><span class=\"line\"> <span class=\"comment\">//document.getElementById(\"loadpercent\").innerHTML=\"\"+Math.abs(progress)+\"%\";</span></span><br><span class=\"line\"> } <span class=\"keyword\">else</span> { <span class=\"comment\">/*is IE*/</span></span><br><span class=\"line\">\trefreshProgress();</span><br><span class=\"line\"> }</span><br><span class=\"line\"> <span class=\"function\"><span class=\"keyword\">function</span> <span class=\"title\">show</span>(<span class=\"params\"></span>)</span>{<span class=\"comment\">//隐藏AD层、显示FLASH层</span></span><br><span class=\"line\"> <span class=\"built_in\">document</span>.getElementById(<span class=\"string\">\"showAD\"</span>).style.display=<span class=\"string\">'none'</span>;</span><br><span class=\"line\"> <span class=\"comment\">//document.getElementById(\"showSWF\").style.display='block';</span></span><br><span class=\"line\"> }</span><br><span class=\"line\"> </span><br><span class=\"line\"> <span class=\"function\"><span class=\"keyword\">function</span> <span class=\"title\">refreshProgress</span>(<span class=\"params\"></span>) </span>{ <span class=\"comment\">//刷新进度条函数</span></span><br><span class=\"line\"> <span class=\"keyword\">var</span> bar = <span class=\"built_in\">document</span>.getElementById(<span class=\"string\">\"loadpercent\"</span>);</span><br><span class=\"line\"> <span class=\"keyword\">var</span> movie = <span class=\"built_in\">document</span>.getElementById(<span class=\"string\">\"ieGame\"</span>);</span><br><span class=\"line\"> <span class=\"keyword\">var</span> nPercentLoaded = <span class=\"built_in\">Math</span>.abs(movie.PercentLoaded());</span><br><span class=\"line\"> bar.innerHTML= nPercentLoaded+<span class=\"string\">\"%\"</span>;</span><br><span class=\"line\"> <span class=\"keyword\">if</span>(nPercentLoaded==<span class=\"number\">100</span>) {</span><br><span class=\"line\"> bar.innerHTML= <span class=\"string\">\"Game download is complete\"</span>;</span><br><span class=\"line\"> } <span class=\"keyword\">else</span> {</span><br><span class=\"line\"> setTimeout(<span class=\"string\">'refreshProgress()'</span>,<span class=\"number\">0</span>);</span><br><span class=\"line\"> }</span><br><span class=\"line\"> }</span><br></pre></td></tr></table></figure>"},{"title":"openresty+nginx 实践","author":"brian","date":"2018-08-30T11:44:49.000Z","_content":"##### Openresty Nginx 实践\n----\n\n> 首先我们先了解一下Openresty\n\n\tOpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。\n\n OpenResty® 通过汇聚各种设计精良的 Nginx 模块(主要由 OpenResty 团队自主开发),从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。\n\n OpenResty® 的目标是让你的Web服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。\n> 官方中文地址: [openresty](https://openresty.org/cn/)\n\n```bash\n# 1. 安装(Ubuntu)\n> apt-get install libpcre3-dev \\\n libssl-dev perl make build-essential curl\n\n# 导入 GPG 密钥:\n> wget -qO - https://openresty.org/package/pubkey.gpg | sudo apt-key add -\n# 安装 add-apt-repository 命令\n> sudo apt-get -y install software-properties-common\n# 添加官方 official APT 仓库:\n> sudo add-apt-repository -y \"deb http://openresty.org/package/ubuntu $(lsb_release -sc) main\"\n\n# 更新 APT 索引:\n> sudo apt-get update\n\n# 安装openresty\n> sudo apt-get install openresty\n```\n\n> 如果你的环境默认安装了nginx请在安装 openresty 之前停止nginx运行,再执行安装命令,否则会出现模块安装失败\n\n\t好了,通过以上步骤后,你的服务器就安装好openresty了,可以通过路径查看\n \n```bash\n# 默认安装路径\n> cd /usr/local/openresty\n# nginx模块目录\n> cd /usr/local/openresty/nginx\n\n# 启动nginx\n> cd /usr/local/openresty/nginx/sbin/\n> ./nginx # 执行启动\n```\n\n\tnginx启动后可通过ip访问,可以查看到nginx response headers中的server\n 已经变成:openresty\n \n> 可以通过配置nginx 中.conf文件来实现openresty lua的执行\n\n```bash\n location /lua {\n default_type text/html;\n content_by_lua '\n ngx.say(\"<p>hello, lua openresty</p>\")\n ';\n }\n```\n\n> 配置完后,重新加载nginx配置\n\n访问 /lua 这个地址,会发现页面打开后会看到 \"hello, lua openresty\" 这样一个页面[demo](https://netmad.me/lua)\n\n好啦,一个简单的openresty + nginx的应用就搞定了\n\n相关资料\n\n[OpenResty Git](https://github.com/openresty/)\n\n[agentzh-nginx](https://openresty.org/download/agentzh-nginx-tutorials-en.html)","source":"_posts/openresty-nginx-实践.md","raw":"title: openresty+nginx 实践\nauthor: brian\ndate: 2018-08-30 19:44:49\ntags:\n---\n##### Openresty Nginx 实践\n----\n\n> 首先我们先了解一下Openresty\n\n\tOpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。\n\n OpenResty® 通过汇聚各种设计精良的 Nginx 模块(主要由 OpenResty 团队自主开发),从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。\n\n OpenResty® 的目标是让你的Web服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。\n> 官方中文地址: [openresty](https://openresty.org/cn/)\n\n```bash\n# 1. 安装(Ubuntu)\n> apt-get install libpcre3-dev \\\n libssl-dev perl make build-essential curl\n\n# 导入 GPG 密钥:\n> wget -qO - https://openresty.org/package/pubkey.gpg | sudo apt-key add -\n# 安装 add-apt-repository 命令\n> sudo apt-get -y install software-properties-common\n# 添加官方 official APT 仓库:\n> sudo add-apt-repository -y \"deb http://openresty.org/package/ubuntu $(lsb_release -sc) main\"\n\n# 更新 APT 索引:\n> sudo apt-get update\n\n# 安装openresty\n> sudo apt-get install openresty\n```\n\n> 如果你的环境默认安装了nginx请在安装 openresty 之前停止nginx运行,再执行安装命令,否则会出现模块安装失败\n\n\t好了,通过以上步骤后,你的服务器就安装好openresty了,可以通过路径查看\n \n```bash\n# 默认安装路径\n> cd /usr/local/openresty\n# nginx模块目录\n> cd /usr/local/openresty/nginx\n\n# 启动nginx\n> cd /usr/local/openresty/nginx/sbin/\n> ./nginx # 执行启动\n```\n\n\tnginx启动后可通过ip访问,可以查看到nginx response headers中的server\n 已经变成:openresty\n \n> 可以通过配置nginx 中.conf文件来实现openresty lua的执行\n\n```bash\n location /lua {\n default_type text/html;\n content_by_lua '\n ngx.say(\"<p>hello, lua openresty</p>\")\n ';\n }\n```\n\n> 配置完后,重新加载nginx配置\n\n访问 /lua 这个地址,会发现页面打开后会看到 \"hello, lua openresty\" 这样一个页面[demo](https://netmad.me/lua)\n\n好啦,一个简单的openresty + nginx的应用就搞定了\n\n相关资料\n\n[OpenResty Git](https://github.com/openresty/)\n\n[agentzh-nginx](https://openresty.org/download/agentzh-nginx-tutorials-en.html)","slug":"openresty-nginx-实践","published":1,"updated":"2018-08-30T12:08:16.063Z","comments":1,"layout":"post","photos":[],"link":"","_id":"cjoljzh1i000c0evon7r4qsz9","content":"<h5 id=\"Openresty-Nginx-实践\"><a href=\"#Openresty-Nginx-实践\" class=\"headerlink\" title=\"Openresty Nginx 实践\"></a>Openresty Nginx 实践</h5><hr>\n<blockquote>\n<p>首先我们先了解一下Openresty</p>\n</blockquote>\n<pre><code>OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。\n\nOpenResty® 通过汇聚各种设计精良的 Nginx 模块(主要由 OpenResty 团队自主开发),从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。\n\nOpenResty® 的目标是让你的Web服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。\n</code></pre><blockquote>\n<p>官方中文地址: <a href=\"https://openresty.org/cn/\" target=\"_blank\" rel=\"noopener\">openresty</a></p>\n</blockquote>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"comment\"># 1. 安装(Ubuntu)</span></span><br><span class=\"line\">> apt-get install libpcre3-dev \\</span><br><span class=\"line\"> libssl-dev perl make build-essential curl</span><br><span class=\"line\"></span><br><span class=\"line\"><span class=\"comment\"># 导入 GPG 密钥:</span></span><br><span class=\"line\">> wget -qO - https://openresty.org/package/pubkey.gpg | sudo apt-key add -</span><br><span class=\"line\"><span class=\"comment\"># 安装 add-apt-repository 命令</span></span><br><span class=\"line\">> sudo apt-get -y install software-properties-common</span><br><span class=\"line\"><span class=\"comment\"># 添加官方 official APT 仓库:</span></span><br><span class=\"line\">> sudo add-apt-repository -y <span class=\"string\">\"deb http://openresty.org/package/ubuntu <span class=\"variable\">$(lsb_release -sc)</span> main\"</span></span><br><span class=\"line\"></span><br><span class=\"line\"><span class=\"comment\"># 更新 APT 索引:</span></span><br><span class=\"line\">> sudo apt-get update</span><br><span class=\"line\"></span><br><span class=\"line\"><span class=\"comment\"># 安装openresty</span></span><br><span class=\"line\">> sudo apt-get install openresty</span><br></pre></td></tr></table></figure>\n<blockquote>\n<p>如果你的环境默认安装了nginx请在安装 openresty 之前停止nginx运行,再执行安装命令,否则会出现模块安装失败</p>\n</blockquote>\n<pre><code>好了,通过以上步骤后,你的服务器就安装好openresty了,可以通过路径查看\n</code></pre><figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"comment\"># 默认安装路径</span></span><br><span class=\"line\">> <span class=\"built_in\">cd</span> /usr/<span class=\"built_in\">local</span>/openresty</span><br><span class=\"line\"><span class=\"comment\"># nginx模块目录</span></span><br><span class=\"line\">> <span class=\"built_in\">cd</span> /usr/<span class=\"built_in\">local</span>/openresty/nginx</span><br><span class=\"line\"></span><br><span class=\"line\"><span class=\"comment\"># 启动nginx</span></span><br><span class=\"line\">> <span class=\"built_in\">cd</span> /usr/<span class=\"built_in\">local</span>/openresty/nginx/sbin/</span><br><span class=\"line\">> ./nginx <span class=\"comment\"># 执行启动</span></span><br></pre></td></tr></table></figure>\n<pre><code>nginx启动后可通过ip访问,可以查看到nginx response headers中的server\n已经变成:openresty\n</code></pre><blockquote>\n<p>可以通过配置nginx 中.conf文件来实现openresty lua的执行</p>\n</blockquote>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">location /lua {</span><br><span class=\"line\"> default_type text/html;</span><br><span class=\"line\"> content_by_lua <span class=\"string\">'</span></span><br><span class=\"line\"><span class=\"string\"> ngx.say(\"<p>hello, lua openresty</p>\")</span></span><br><span class=\"line\"><span class=\"string\"> '</span>;</span><br><span class=\"line\">}</span><br></pre></td></tr></table></figure>\n<blockquote>\n<p>配置完后,重新加载nginx配置</p>\n</blockquote>\n<p>访问 /lua 这个地址,会发现页面打开后会看到 “hello, lua openresty” 这样一个页面<a href=\"https://netmad.me/lua\">demo</a></p>\n<p>好啦,一个简单的openresty + nginx的应用就搞定了</p>\n<p>相关资料</p>\n<p><a href=\"https://github.com/openresty/\" target=\"_blank\" rel=\"noopener\">OpenResty Git</a></p>\n<p><a href=\"https://openresty.org/download/agentzh-nginx-tutorials-en.html\" target=\"_blank\" rel=\"noopener\">agentzh-nginx</a></p>\n","site":{"data":{}},"excerpt":"","more":"<h5 id=\"Openresty-Nginx-实践\"><a href=\"#Openresty-Nginx-实践\" class=\"headerlink\" title=\"Openresty Nginx 实践\"></a>Openresty Nginx 实践</h5><hr>\n<blockquote>\n<p>首先我们先了解一下Openresty</p>\n</blockquote>\n<pre><code>OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。\n\nOpenResty® 通过汇聚各种设计精良的 Nginx 模块(主要由 OpenResty 团队自主开发),从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。\n\nOpenResty® 的目标是让你的Web服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。\n</code></pre><blockquote>\n<p>官方中文地址: <a href=\"https://openresty.org/cn/\" target=\"_blank\" rel=\"noopener\">openresty</a></p>\n</blockquote>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"comment\"># 1. 安装(Ubuntu)</span></span><br><span class=\"line\">> apt-get install libpcre3-dev \\</span><br><span class=\"line\"> libssl-dev perl make build-essential curl</span><br><span class=\"line\"></span><br><span class=\"line\"><span class=\"comment\"># 导入 GPG 密钥:</span></span><br><span class=\"line\">> wget -qO - https://openresty.org/package/pubkey.gpg | sudo apt-key add -</span><br><span class=\"line\"><span class=\"comment\"># 安装 add-apt-repository 命令</span></span><br><span class=\"line\">> sudo apt-get -y install software-properties-common</span><br><span class=\"line\"><span class=\"comment\"># 添加官方 official APT 仓库:</span></span><br><span class=\"line\">> sudo add-apt-repository -y <span class=\"string\">\"deb http://openresty.org/package/ubuntu <span class=\"variable\">$(lsb_release -sc)</span> main\"</span></span><br><span class=\"line\"></span><br><span class=\"line\"><span class=\"comment\"># 更新 APT 索引:</span></span><br><span class=\"line\">> sudo apt-get update</span><br><span class=\"line\"></span><br><span class=\"line\"><span class=\"comment\"># 安装openresty</span></span><br><span class=\"line\">> sudo apt-get install openresty</span><br></pre></td></tr></table></figure>\n<blockquote>\n<p>如果你的环境默认安装了nginx请在安装 openresty 之前停止nginx运行,再执行安装命令,否则会出现模块安装失败</p>\n</blockquote>\n<pre><code>好了,通过以上步骤后,你的服务器就安装好openresty了,可以通过路径查看\n</code></pre><figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"comment\"># 默认安装路径</span></span><br><span class=\"line\">> <span class=\"built_in\">cd</span> /usr/<span class=\"built_in\">local</span>/openresty</span><br><span class=\"line\"><span class=\"comment\"># nginx模块目录</span></span><br><span class=\"line\">> <span class=\"built_in\">cd</span> /usr/<span class=\"built_in\">local</span>/openresty/nginx</span><br><span class=\"line\"></span><br><span class=\"line\"><span class=\"comment\"># 启动nginx</span></span><br><span class=\"line\">> <span class=\"built_in\">cd</span> /usr/<span class=\"built_in\">local</span>/openresty/nginx/sbin/</span><br><span class=\"line\">> ./nginx <span class=\"comment\"># 执行启动</span></span><br></pre></td></tr></table></figure>\n<pre><code>nginx启动后可通过ip访问,可以查看到nginx response headers中的server\n已经变成:openresty\n</code></pre><blockquote>\n<p>可以通过配置nginx 中.conf文件来实现openresty lua的执行</p>\n</blockquote>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">location /lua {</span><br><span class=\"line\"> default_type text/html;</span><br><span class=\"line\"> content_by_lua <span class=\"string\">'</span></span><br><span class=\"line\"><span class=\"string\"> ngx.say(\"<p>hello, lua openresty</p>\")</span></span><br><span class=\"line\"><span class=\"string\"> '</span>;</span><br><span class=\"line\">}</span><br></pre></td></tr></table></figure>\n<blockquote>\n<p>配置完后,重新加载nginx配置</p>\n</blockquote>\n<p>访问 /lua 这个地址,会发现页面打开后会看到 “hello, lua openresty” 这样一个页面<a href=\"https://netmad.me/lua\">demo</a></p>\n<p>好啦,一个简单的openresty + nginx的应用就搞定了</p>\n<p>相关资料</p>\n<p><a href=\"https://github.com/openresty/\" target=\"_blank\" rel=\"noopener\">OpenResty Git</a></p>\n<p><a href=\"https://openresty.org/download/agentzh-nginx-tutorials-en.html\" target=\"_blank\" rel=\"noopener\">agentzh-nginx</a></p>\n"},{"title":"vue+websocket+graphql中大型web应用实践","author":"brian","date":"2018-08-29T08:39:17.000Z","_content":"> 随着前端框架的不断成熟,MVVM已成为主流的WEB前端开发框架,今天我给大家带来之前项目中的vue的实践\n\n--------------\n> 项目介绍,这个项目主要是app内嵌h5,前后端分离都是共识了,前后端分离不知大家有没有遇到这样一个问题(通病),后端提供接口开发的人员往往写好接口给前端开发对接后基本没文档,如接口发生改变,传参,返回数据有变动,前端异常情况,背锅的总是前端出问题。在这样的前提下,我们找到了一个比较的解决方案Graphql的应用,下面我就一一给大家讲解一下项目中如何使用\n\n\n如果对graphql还不大了解的可以去官网了解下 https://graphql.cn/\n>GraphQL 既是一种用于 API 的查询语言也是一个满足你数据查询的运行时。 GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述,使得客户端能够准确地获得它需要的数据,而且没有任何冗余,也让 API 更容易地随着时间推移而演进,还能用于构建强大的开发者工具。\n\n他的好处就是,后端提供多少可用使用的数据字段,前端开发可根据需求传递定制。\n\n\n项目结构:\n\n```bash\n> build/\n config/\n src/\n\tassets/\n \t# css, scss, img\n components/\n \t# 组建\n controller/\n \tindex.js # 入口文件\n \tgraphql.js # graphql 封装\n websocket.js # 长链接封装\n router/\n \trouter.js # vue-router 路由\n utils/\n \tapi.js # axios ajax 封装\n utils.js # 通用库\n views/\n \t# page\n vuex/\n \t# vuex store module\n static/\n \t# 公共图片,js\n test/\n \t# test.js\n index.html\n```\n\n> 整个项目使用vue构建,vue-router做页面路由,vuex 状态树管理\n\n关键流程实现\n\n1. ajax的封装\n\n```js\n// api.js 代码片段\n\nlet ajax = (opt) => {\n\tlet options = {\n \tmethod: opt.type,\n url: opt.url,\n headers: opt.headers,\n timeout: 1000\n }\n return axios(options).catch((e) => {\n \tif (e) {\n \tconsole.log('ajax error', e.toString())\n }\n })\n}\n```\n\n2. graphql 接口实现\n\n```js\n// graphql.js 代码片段\nconst postGraphql = (query, data) => {\n\treturn Prom((resolve, reject) => {\n \tajax.post({\n \turl: <url>,\n data: {\n \tquery: query,\n ...data\n }\n }).then(({ data }) => {\n \tresolve(data)\n }).catch(e => {\n \treject(e)\n })\n })\n}\nexport {\n\tpostGraphql\n}\n```\n\n3. 实现调用\n\n```js\n// index.js 调度模块\nlet getData = (data)=> {\n\tlet queryString = `\n\tquery {\n\trankList(slug: \"${slug}\", page: ${page}, pageSize: ${page_size}) {\n userId\n rank\n score\n user {\n ${DATA_MODEL.USER}\n glamour\n level{\n level\n exp\n levelName\n }\n }\n }\n }\n `\n return postGraphql(queryString)\n}\n```\n\n4. 页面调用\n\n```js\n# vue 页面调用\nimport getData from index\nlet data = getData({page: 1, slug: 'a', page_size: 10})\n```","source":"_posts/vue-websocket-graphql中大型web应用实践.md","raw":"title: vue+websocket+graphql中大型web应用实践\nauthor: brian\ndate: 2018-08-29 16:39:17\ntags:\n---\n> 随着前端框架的不断成熟,MVVM已成为主流的WEB前端开发框架,今天我给大家带来之前项目中的vue的实践\n\n--------------\n> 项目介绍,这个项目主要是app内嵌h5,前后端分离都是共识了,前后端分离不知大家有没有遇到这样一个问题(通病),后端提供接口开发的人员往往写好接口给前端开发对接后基本没文档,如接口发生改变,传参,返回数据有变动,前端异常情况,背锅的总是前端出问题。在这样的前提下,我们找到了一个比较的解决方案Graphql的应用,下面我就一一给大家讲解一下项目中如何使用\n\n\n如果对graphql还不大了解的可以去官网了解下 https://graphql.cn/\n>GraphQL 既是一种用于 API 的查询语言也是一个满足你数据查询的运行时。 GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述,使得客户端能够准确地获得它需要的数据,而且没有任何冗余,也让 API 更容易地随着时间推移而演进,还能用于构建强大的开发者工具。\n\n他的好处就是,后端提供多少可用使用的数据字段,前端开发可根据需求传递定制。\n\n\n项目结构:\n\n```bash\n> build/\n config/\n src/\n\tassets/\n \t# css, scss, img\n components/\n \t# 组建\n controller/\n \tindex.js # 入口文件\n \tgraphql.js # graphql 封装\n websocket.js # 长链接封装\n router/\n \trouter.js # vue-router 路由\n utils/\n \tapi.js # axios ajax 封装\n utils.js # 通用库\n views/\n \t# page\n vuex/\n \t# vuex store module\n static/\n \t# 公共图片,js\n test/\n \t# test.js\n index.html\n```\n\n> 整个项目使用vue构建,vue-router做页面路由,vuex 状态树管理\n\n关键流程实现\n\n1. ajax的封装\n\n```js\n// api.js 代码片段\n\nlet ajax = (opt) => {\n\tlet options = {\n \tmethod: opt.type,\n url: opt.url,\n headers: opt.headers,\n timeout: 1000\n }\n return axios(options).catch((e) => {\n \tif (e) {\n \tconsole.log('ajax error', e.toString())\n }\n })\n}\n```\n\n2. graphql 接口实现\n\n```js\n// graphql.js 代码片段\nconst postGraphql = (query, data) => {\n\treturn Prom((resolve, reject) => {\n \tajax.post({\n \turl: <url>,\n data: {\n \tquery: query,\n ...data\n }\n }).then(({ data }) => {\n \tresolve(data)\n }).catch(e => {\n \treject(e)\n })\n })\n}\nexport {\n\tpostGraphql\n}\n```\n\n3. 实现调用\n\n```js\n// index.js 调度模块\nlet getData = (data)=> {\n\tlet queryString = `\n\tquery {\n\trankList(slug: \"${slug}\", page: ${page}, pageSize: ${page_size}) {\n userId\n rank\n score\n user {\n ${DATA_MODEL.USER}\n glamour\n level{\n level\n exp\n levelName\n }\n }\n }\n }\n `\n return postGraphql(queryString)\n}\n```\n\n4. 页面调用\n\n```js\n# vue 页面调用\nimport getData from index\nlet data = getData({page: 1, slug: 'a', page_size: 10})\n```","slug":"vue-websocket-graphql中大型web应用实践","published":1,"updated":"2018-08-29T09:15:03.148Z","comments":1,"layout":"post","photos":[],"link":"","_id":"cjoljzh1k000d0evoo3zoj02m","content":"<blockquote>\n<p>随着前端框架的不断成熟,MVVM已成为主流的WEB前端开发框架,今天我给大家带来之前项目中的vue的实践</p>\n</blockquote>\n<hr>\n<blockquote>\n<p>项目介绍,这个项目主要是app内嵌h5,前后端分离都是共识了,前后端分离不知大家有没有遇到这样一个问题(通病),后端提供接口开发的人员往往写好接口给前端开发对接后基本没文档,如接口发生改变,传参,返回数据有变动,前端异常情况,背锅的总是前端出问题。在这样的前提下,我们找到了一个比较的解决方案Graphql的应用,下面我就一一给大家讲解一下项目中如何使用</p>\n</blockquote>\n<p>如果对graphql还不大了解的可以去官网了解下 <a href=\"https://graphql.cn/\" target=\"_blank\" rel=\"noopener\">https://graphql.cn/</a></p>\n<blockquote>\n<p>GraphQL 既是一种用于 API 的查询语言也是一个满足你数据查询的运行时。 GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述,使得客户端能够准确地获得它需要的数据,而且没有任何冗余,也让 API 更容易地随着时间推移而演进,还能用于构建强大的开发者工具。</p>\n</blockquote>\n<p>他的好处就是,后端提供多少可用使用的数据字段,前端开发可根据需求传递定制。</p>\n<p>项目结构:</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br><span class=\"line\">17</span><br><span class=\"line\">18</span><br><span class=\"line\">19</span><br><span class=\"line\">20</span><br><span class=\"line\">21</span><br><span class=\"line\">22</span><br><span class=\"line\">23</span><br><span class=\"line\">24</span><br><span class=\"line\">25</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">> build/</span><br><span class=\"line\"> config/</span><br><span class=\"line\"> src/</span><br><span class=\"line\">\tassets/</span><br><span class=\"line\"> \t<span class=\"comment\"># css, scss, img</span></span><br><span class=\"line\"> components/</span><br><span class=\"line\"> \t<span class=\"comment\"># 组建</span></span><br><span class=\"line\"> controller/</span><br><span class=\"line\"> \tindex.js <span class=\"comment\"># 入口文件</span></span><br><span class=\"line\"> \tgraphql.js <span class=\"comment\"># graphql 封装</span></span><br><span class=\"line\"> websocket.js <span class=\"comment\"># 长链接封装</span></span><br><span class=\"line\"> router/</span><br><span class=\"line\"> \trouter.js <span class=\"comment\"># vue-router 路由</span></span><br><span class=\"line\"> utils/</span><br><span class=\"line\"> \tapi.js <span class=\"comment\"># axios ajax 封装</span></span><br><span class=\"line\"> utils.js <span class=\"comment\"># 通用库</span></span><br><span class=\"line\"> views/</span><br><span class=\"line\"> \t<span class=\"comment\"># page</span></span><br><span class=\"line\"> vuex/</span><br><span class=\"line\"> \t<span class=\"comment\"># vuex store module</span></span><br><span class=\"line\"> static/</span><br><span class=\"line\"> \t<span class=\"comment\"># 公共图片,js</span></span><br><span class=\"line\"> <span class=\"built_in\">test</span>/</span><br><span class=\"line\"> \t<span class=\"comment\"># test.js</span></span><br><span class=\"line\"> index.html</span><br></pre></td></tr></table></figure>\n<blockquote>\n<p>整个项目使用vue构建,vue-router做页面路由,vuex 状态树管理</p>\n</blockquote>\n<p>关键流程实现</p>\n<ol>\n<li>ajax的封装</li>\n</ol>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"comment\">// api.js 代码片段</span></span><br><span class=\"line\"></span><br><span class=\"line\"><span class=\"keyword\">let</span> ajax = <span class=\"function\">(<span class=\"params\">opt</span>) =></span> {</span><br><span class=\"line\">\t<span class=\"keyword\">let</span> options = {</span><br><span class=\"line\"> \tmethod: opt.type,</span><br><span class=\"line\"> url: opt.url,</span><br><span class=\"line\"> headers: opt.headers,</span><br><span class=\"line\"> timeout: <span class=\"number\">1000</span></span><br><span class=\"line\"> }</span><br><span class=\"line\"> <span class=\"keyword\">return</span> axios(options).catch(<span class=\"function\">(<span class=\"params\">e</span>) =></span> {</span><br><span class=\"line\"> \t<span class=\"keyword\">if</span> (e) {</span><br><span class=\"line\"> \t<span class=\"built_in\">console</span>.log(<span class=\"string\">'ajax error'</span>, e.toString())</span><br><span class=\"line\"> }</span><br><span class=\"line\"> })</span><br><span class=\"line\">}</span><br></pre></td></tr></table></figure>\n<ol start=\"2\">\n<li>graphql 接口实现</li>\n</ol>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br><span class=\"line\">17</span><br><span class=\"line\">18</span><br><span class=\"line\">19</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"comment\">// graphql.js 代码片段</span></span><br><span class=\"line\"><span class=\"keyword\">const</span> postGraphql = <span class=\"function\">(<span class=\"params\">query, data</span>) =></span> {</span><br><span class=\"line\">\t<span class=\"keyword\">return</span> Prom(<span class=\"function\">(<span class=\"params\">resolve, reject</span>) =></span> {</span><br><span class=\"line\"> \tajax.post({</span><br><span class=\"line\"> \turl: <span class=\"xml\"><span class=\"tag\"><<span class=\"name\">url</span>></span>,</span></span><br><span class=\"line\"><span class=\"xml\"> data: {</span></span><br><span class=\"line\"><span class=\"xml\"> \tquery: query,</span></span><br><span class=\"line\"><span class=\"xml\"> ...data</span></span><br><span class=\"line\"><span class=\"xml\"> }</span></span><br><span class=\"line\"><span class=\"xml\"> }).then(({ data }) => {</span></span><br><span class=\"line\"><span class=\"xml\"> \tresolve(data)</span></span><br><span class=\"line\"><span class=\"xml\"> }).catch(e => {</span></span><br><span class=\"line\"><span class=\"xml\"> \treject(e)</span></span><br><span class=\"line\"><span class=\"xml\"> })</span></span><br><span class=\"line\"><span class=\"xml\"> })</span></span><br><span class=\"line\"><span class=\"xml\">}</span></span><br><span class=\"line\"><span class=\"xml\">export {</span></span><br><span class=\"line\"><span class=\"xml\">\tpostGraphql</span></span><br><span class=\"line\"><span class=\"xml\">}</span></span><br></pre></td></tr></table></figure>\n<ol start=\"3\">\n<li>实现调用</li>\n</ol>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br><span class=\"line\">17</span><br><span class=\"line\">18</span><br><span class=\"line\">19</span><br><span class=\"line\">20</span><br><span class=\"line\">21</span><br><span class=\"line\">22</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"comment\">// index.js 调度模块</span></span><br><span class=\"line\"><span class=\"keyword\">let</span> getData = <span class=\"function\">(<span class=\"params\">data</span>)=></span> {</span><br><span class=\"line\">\t<span class=\"keyword\">let</span> queryString = <span class=\"string\">`</span></span><br><span class=\"line\"><span class=\"string\">\tquery {</span></span><br><span class=\"line\"><span class=\"string\">\trankList(slug: \"<span class=\"subst\">${slug}</span>\", page: <span class=\"subst\">${page}</span>, pageSize: <span class=\"subst\">${page_size}</span>) {</span></span><br><span class=\"line\"><span class=\"string\"> userId</span></span><br><span class=\"line\"><span class=\"string\"> rank</span></span><br><span class=\"line\"><span class=\"string\"> score</span></span><br><span class=\"line\"><span class=\"string\"> user {</span></span><br><span class=\"line\"><span class=\"string\"> <span class=\"subst\">${DATA_MODEL.USER}</span></span></span><br><span class=\"line\"><span class=\"string\"> glamour</span></span><br><span class=\"line\"><span class=\"string\"> level{</span></span><br><span class=\"line\"><span class=\"string\"> level</span></span><br><span class=\"line\"><span class=\"string\"> exp</span></span><br><span class=\"line\"><span class=\"string\"> levelName</span></span><br><span class=\"line\"><span class=\"string\"> }</span></span><br><span class=\"line\"><span class=\"string\"> }</span></span><br><span class=\"line\"><span class=\"string\"> }</span></span><br><span class=\"line\"><span class=\"string\"> }</span></span><br><span class=\"line\"><span class=\"string\"> `</span></span><br><span class=\"line\"> <span class=\"keyword\">return</span> postGraphql(queryString)</span><br><span class=\"line\">}</span><br></pre></td></tr></table></figure>\n<ol start=\"4\">\n<li>页面调用</li>\n</ol>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"># vue 页面调用</span><br><span class=\"line\"><span class=\"keyword\">import</span> getData <span class=\"keyword\">from</span> index</span><br><span class=\"line\"><span class=\"keyword\">let</span> data = getData({<span class=\"attr\">page</span>: <span class=\"number\">1</span>, <span class=\"attr\">slug</span>: <span class=\"string\">'a'</span>, <span class=\"attr\">page_size</span>: <span class=\"number\">10</span>})</span><br></pre></td></tr></table></figure>","site":{"data":{}},"excerpt":"","more":"<blockquote>\n<p>随着前端框架的不断成熟,MVVM已成为主流的WEB前端开发框架,今天我给大家带来之前项目中的vue的实践</p>\n</blockquote>\n<hr>\n<blockquote>\n<p>项目介绍,这个项目主要是app内嵌h5,前后端分离都是共识了,前后端分离不知大家有没有遇到这样一个问题(通病),后端提供接口开发的人员往往写好接口给前端开发对接后基本没文档,如接口发生改变,传参,返回数据有变动,前端异常情况,背锅的总是前端出问题。在这样的前提下,我们找到了一个比较的解决方案Graphql的应用,下面我就一一给大家讲解一下项目中如何使用</p>\n</blockquote>\n<p>如果对graphql还不大了解的可以去官网了解下 <a href=\"https://graphql.cn/\" target=\"_blank\" rel=\"noopener\">https://graphql.cn/</a></p>\n<blockquote>\n<p>GraphQL 既是一种用于 API 的查询语言也是一个满足你数据查询的运行时。 GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述,使得客户端能够准确地获得它需要的数据,而且没有任何冗余,也让 API 更容易地随着时间推移而演进,还能用于构建强大的开发者工具。</p>\n</blockquote>\n<p>他的好处就是,后端提供多少可用使用的数据字段,前端开发可根据需求传递定制。</p>\n<p>项目结构:</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br><span class=\"line\">17</span><br><span class=\"line\">18</span><br><span class=\"line\">19</span><br><span class=\"line\">20</span><br><span class=\"line\">21</span><br><span class=\"line\">22</span><br><span class=\"line\">23</span><br><span class=\"line\">24</span><br><span class=\"line\">25</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">> build/</span><br><span class=\"line\"> config/</span><br><span class=\"line\"> src/</span><br><span class=\"line\">\tassets/</span><br><span class=\"line\"> \t<span class=\"comment\"># css, scss, img</span></span><br><span class=\"line\"> components/</span><br><span class=\"line\"> \t<span class=\"comment\"># 组建</span></span><br><span class=\"line\"> controller/</span><br><span class=\"line\"> \tindex.js <span class=\"comment\"># 入口文件</span></span><br><span class=\"line\"> \tgraphql.js <span class=\"comment\"># graphql 封装</span></span><br><span class=\"line\"> websocket.js <span class=\"comment\"># 长链接封装</span></span><br><span class=\"line\"> router/</span><br><span class=\"line\"> \trouter.js <span class=\"comment\"># vue-router 路由</span></span><br><span class=\"line\"> utils/</span><br><span class=\"line\"> \tapi.js <span class=\"comment\"># axios ajax 封装</span></span><br><span class=\"line\"> utils.js <span class=\"comment\"># 通用库</span></span><br><span class=\"line\"> views/</span><br><span class=\"line\"> \t<span class=\"comment\"># page</span></span><br><span class=\"line\"> vuex/</span><br><span class=\"line\"> \t<span class=\"comment\"># vuex store module</span></span><br><span class=\"line\"> static/</span><br><span class=\"line\"> \t<span class=\"comment\"># 公共图片,js</span></span><br><span class=\"line\"> <span class=\"built_in\">test</span>/</span><br><span class=\"line\"> \t<span class=\"comment\"># test.js</span></span><br><span class=\"line\"> index.html</span><br></pre></td></tr></table></figure>\n<blockquote>\n<p>整个项目使用vue构建,vue-router做页面路由,vuex 状态树管理</p>\n</blockquote>\n<p>关键流程实现</p>\n<ol>\n<li>ajax的封装</li>\n</ol>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"comment\">// api.js 代码片段</span></span><br><span class=\"line\"></span><br><span class=\"line\"><span class=\"keyword\">let</span> ajax = <span class=\"function\">(<span class=\"params\">opt</span>) =></span> {</span><br><span class=\"line\">\t<span class=\"keyword\">let</span> options = {</span><br><span class=\"line\"> \tmethod: opt.type,</span><br><span class=\"line\"> url: opt.url,</span><br><span class=\"line\"> headers: opt.headers,</span><br><span class=\"line\"> timeout: <span class=\"number\">1000</span></span><br><span class=\"line\"> }</span><br><span class=\"line\"> <span class=\"keyword\">return</span> axios(options).catch(<span class=\"function\">(<span class=\"params\">e</span>) =></span> {</span><br><span class=\"line\"> \t<span class=\"keyword\">if</span> (e) {</span><br><span class=\"line\"> \t<span class=\"built_in\">console</span>.log(<span class=\"string\">'ajax error'</span>, e.toString())</span><br><span class=\"line\"> }</span><br><span class=\"line\"> })</span><br><span class=\"line\">}</span><br></pre></td></tr></table></figure>\n<ol start=\"2\">\n<li>graphql 接口实现</li>\n</ol>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br><span class=\"line\">17</span><br><span class=\"line\">18</span><br><span class=\"line\">19</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"comment\">// graphql.js 代码片段</span></span><br><span class=\"line\"><span class=\"keyword\">const</span> postGraphql = <span class=\"function\">(<span class=\"params\">query, data</span>) =></span> {</span><br><span class=\"line\">\t<span class=\"keyword\">return</span> Prom(<span class=\"function\">(<span class=\"params\">resolve, reject</span>) =></span> {</span><br><span class=\"line\"> \tajax.post({</span><br><span class=\"line\"> \turl: <span class=\"xml\"><span class=\"tag\"><<span class=\"name\">url</span>></span>,</span></span><br><span class=\"line\"><span class=\"xml\"> data: {</span></span><br><span class=\"line\"><span class=\"xml\"> \tquery: query,</span></span><br><span class=\"line\"><span class=\"xml\"> ...data</span></span><br><span class=\"line\"><span class=\"xml\"> }</span></span><br><span class=\"line\"><span class=\"xml\"> }).then(({ data }) => {</span></span><br><span class=\"line\"><span class=\"xml\"> \tresolve(data)</span></span><br><span class=\"line\"><span class=\"xml\"> }).catch(e => {</span></span><br><span class=\"line\"><span class=\"xml\"> \treject(e)</span></span><br><span class=\"line\"><span class=\"xml\"> })</span></span><br><span class=\"line\"><span class=\"xml\"> })</span></span><br><span class=\"line\"><span class=\"xml\">}</span></span><br><span class=\"line\"><span class=\"xml\">export {</span></span><br><span class=\"line\"><span class=\"xml\">\tpostGraphql</span></span><br><span class=\"line\"><span class=\"xml\">}</span></span><br></pre></td></tr></table></figure>\n<ol start=\"3\">\n<li>实现调用</li>\n</ol>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br><span class=\"line\">17</span><br><span class=\"line\">18</span><br><span class=\"line\">19</span><br><span class=\"line\">20</span><br><span class=\"line\">21</span><br><span class=\"line\">22</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"comment\">// index.js 调度模块</span></span><br><span class=\"line\"><span class=\"keyword\">let</span> getData = <span class=\"function\">(<span class=\"params\">data</span>)=></span> {</span><br><span class=\"line\">\t<span class=\"keyword\">let</span> queryString = <span class=\"string\">`</span></span><br><span class=\"line\"><span class=\"string\">\tquery {</span></span><br><span class=\"line\"><span class=\"string\">\trankList(slug: \"<span class=\"subst\">${slug}</span>\", page: <span class=\"subst\">${page}</span>, pageSize: <span class=\"subst\">${page_size}</span>) {</span></span><br><span class=\"line\"><span class=\"string\"> userId</span></span><br><span class=\"line\"><span class=\"string\"> rank</span></span><br><span class=\"line\"><span class=\"string\"> score</span></span><br><span class=\"line\"><span class=\"string\"> user {</span></span><br><span class=\"line\"><span class=\"string\"> <span class=\"subst\">${DATA_MODEL.USER}</span></span></span><br><span class=\"line\"><span class=\"string\"> glamour</span></span><br><span class=\"line\"><span class=\"string\"> level{</span></span><br><span class=\"line\"><span class=\"string\"> level</span></span><br><span class=\"line\"><span class=\"string\"> exp</span></span><br><span class=\"line\"><span class=\"string\"> levelName</span></span><br><span class=\"line\"><span class=\"string\"> }</span></span><br><span class=\"line\"><span class=\"string\"> }</span></span><br><span class=\"line\"><span class=\"string\"> }</span></span><br><span class=\"line\"><span class=\"string\"> }</span></span><br><span class=\"line\"><span class=\"string\"> `</span></span><br><span class=\"line\"> <span class=\"keyword\">return</span> postGraphql(queryString)</span><br><span class=\"line\">}</span><br></pre></td></tr></table></figure>\n<ol start=\"4\">\n<li>页面调用</li>\n</ol>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"># vue 页面调用</span><br><span class=\"line\"><span class=\"keyword\">import</span> getData <span class=\"keyword\">from</span> index</span><br><span class=\"line\"><span class=\"keyword\">let</span> data = getData({<span class=\"attr\">page</span>: <span class=\"number\">1</span>, <span class=\"attr\">slug</span>: <span class=\"string\">'a'</span>, <span class=\"attr\">page_size</span>: <span class=\"number\">10</span>})</span><br></pre></td></tr></table></figure>"},{"title":"微信JS-SDK在H5中的使用","author":"brian","date":"2018-09-13T09:30:40.000Z","_content":"#### 微信JS SDK的功能\n\n> \n\t微信JS-SDK是微信公众平台面向网页开发者提供的基于微信内的网页开发工具包。\n\t通过使用微信JS-SDK,网页开发者可借助微信高效地使用拍照、选图、语音、位置等手机系统的能力,同时可以直接使用微信分享、扫一扫等微信特有的能力,为微信用户提供更优质的网页体验。\n \n通过使用微信的JS-SDK,你可以让你网页在微信内调用拍照、语音、支付、位置、扫一扫这些只能在微信内使用的功能。进过下面的步骤,一步一步的配置,就可以让你正确的在项目中引入微信的JS-SDK。\n\n再正式使用微信JS SDK前,请查阅官方文档中5个重要步骤。\n\n[官方文档](https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115)\n\n\n#### 引入微信的JS文件\n\n1. 在需要调用JS接口的页面引入如下JS文件,(支持https)http://res.wx.qq.com/open/js/jweixin-1.4.0.js\n\n\n ```html\n <script src=\"http://res.wx.qq.com/open/js/jweixin-1.4.0.js\"></script>\n ```\n\n2. 但是只支持使用 AMD/CMD 标准模块加载方法加载。于是我就在npm的官网上找到了发布后的js-sdk,支持CommonJS的引入方式。npm的地址在[这里](https://www.npmjs.com/package/weixin-js-sdk)。可以在你的项目中使用如下命令安装。\n\n ```bash\n npm install weixin-js-sdk\n ```\n\n安装好后在js代码中引入\n\n ```js\n // 使用CommonJS规范\n\tlet wx = require('weixin-js-sdk')\n // CoffeeScript\n wx = require 'weixin-js-sdk'\n // ES6\n import wx from 'weixin-js-sdk'\n ```\n\n完成js引入后下面我们讲讲如何确实的在前端js中使用\n\n#### wx.config 鉴权签名算法\n\n>\t\n\t生成签名之前必须先了解一下jsapi_ticket,jsapi_ticket是企业号号用于调用微信JS接口的临时票据。正常情况下,jsapi_ticket的有效期为7200秒,通过access_token来获取。由于获取jsapi_ticket的api调用次数非常有限,频繁刷新jsapi_ticket会导致api调用受限,影响自身业务,开发者必须在自己的服务全局缓存jsapi_ticket。\n\n1.参考以下文档获取access_token(有效期7200秒,开发者必须在自己的服务全局缓存access_token):../15/54ce45d8d30b6bf6758f68d2e95bc627.html\n\n2.用第一步拿到的access_token 采用http GET方式请求获得jsapi_ticket(有效期7200秒,开发者必须在自己的服务全局缓存jsapi_ticket):https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi\n\n```json\n{\n 'errcode': 0,\n 'errmsg': 'ok',\n 'ticket': 'bxLdikRXVbTPdHSM05e5u5sUoXNKd841ZO3MhKoyN5OfkWITDGgnr2fwJ0m9E8NYzWKVZvdVtaUgWvsdshFKA',\n 'expires_in': 7200,\n}\n\n```\n\n##### 签名算法\n\n签名生成规则如下:参与签名的字段包括noncestr(随机字符串), 有效的jsapi_ticket, timestamp(时间戳), url(当前网页的URL,不包含#及其后面部分) 。对所有待签名参数按照字段名的ASCII 码从小到大排序(字典序)后,使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串string1。这里需要注意的是所有参数名均为小写字符。对string1作sha1加密,字段名和字段值都采用原始值,不进行URL 转义。\n\n即signature=sha1(string1)。 示例:\n\n```js\nnoncestr=Wm3WZYTPz0wzccnW\njsapi_ticket=sM4AOVdWfPE4DxkXGEs8VMCPGGVi4C3VM0P37wVUCFvkVAy_90u5h9nbSlYy3-Sl-HhTdfl2fzFy1AOcHKP7qg\ntimestamp=1414587457\nurl=http://mp.weixin.qq.com?params=value\n```\n步骤1. 对所有待签名参数按照字段名的ASCII 码从小到大排序(字典序)后,使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串string1:\n\n```js\njsapi_ticket=sM4AOVdWfPE4DxkXGEs8VMCPGGVi4C3VM0P37wVUCFvkVAy_90u5h9nbSlYy3-Sl-HhTdfl2fzFy1AOcHKP7qg&noncestr=Wm3WZYTPz0wzccnW×tamp=1414587457&url=http://mp.weixin.qq.com?params=value\n```\n\n步骤2. 对string1进行sha1签名,得到signature:\n\n```js\n0f9de62fce790f9a083d5c99e95740ceb90c27ed\n```\n\n注意事项\n\n\t1.签名用的noncestr和timestamp必须与wx.config中的nonceStr和timestamp相同。\n\t2.签名用的url必须是调用JS接口页面的完整URL。\n\t3.出于安全考虑,开发者必须在服务器端实现签名的逻辑。\n\n\n[签名校验工具](https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign)\n\n完成签名生成后,即可开始配置wxjs-sdk的使用了\n\n```js\nwx.config({\n debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。\n appId: '', // 必填,企业号的唯一标识,此处填写企业号corpid\n timestamp: , // 必填,生成签名的时间戳\n nonceStr: '', // 必填,生成签名的随机串\n signature: '',// 必填,签名,见附录1\n jsApiList: [] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2\n});\n```\n[weixinJS-SDK接口列表](https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115)\n\n写了这么多,我们来一段实际应用的代码\n\n```js\nimport wx from 'weixin-js-sdk'\najax.get({url: <getSignature>}).then((resp) => {\n\tif (resp.ok) {\n let signature = resp.data\n signature.debug = false\n signature.signature = signature.sign\n signature.nonceStr = signature.noncestr\n signature.appId = signature.appid\n signature.jsApiList = ['onMenuShareTimeline', 'checkJsApi',\n 'hideOptionMenu', 'showOptionMenu', 'onMenuShareAppMessage'\n ]\n wx.config(signature)\n wx.ready(()=> {\n \tlet callback = {\n \ttitle: 'test',\n desc: 'test',\n imgUrl: 'http://yourdomain.com',\n link: 'http://yourdomain.com',\n success: () => {\n \tconsole.log('分享成功')\n },\n cancel: () => {\n \tconsole.log('分享失败')\n },\n trigger: () => {\n \tconsole.log('')\n },\n complete: () => {\n \tconsole.log('接口调用完成')\n },\n fail () {\n \tconsole.log('调用失败')\n }\n }\n wx.onMenuShareAppMessage(callBack)\n wx.onMenuShareTimeline(callBack)\n wx.onMenuShareQQ(callBack)\n wx.onMenuShareWeibo(callBack)\n wx.onMenuShareQZone(callBack)\n })\n }\n})\n```\najax 调用就是获取签名数据后实现签名注册\n\n\t注意:不要尝试在trigger中使用ajax异步请求修改本次分享的内容,因为客户端分享操作是一个同步操作,这时候使用ajax的回包会还没有返回。\n\n\n##### 最后\n---\n\n\t如果页面的url发生了变化,在新的url下调用js-sdk一定要再调用一次签名接口,用新的url再进行一次签名,否则会调用不成功。\n \n[官方文档](https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115)\n\n[在线调试工具](https://mp.weixin.qq.com/debug)\n\n[签名校验工具](https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign)\n","source":"_posts/微信JS在H5中的使用.md","raw":"title: 微信JS-SDK在H5中的使用\nauthor: brian\ndate: 2018-09-13 17:30:40\ntags:\n---\n#### 微信JS SDK的功能\n\n> \n\t微信JS-SDK是微信公众平台面向网页开发者提供的基于微信内的网页开发工具包。\n\t通过使用微信JS-SDK,网页开发者可借助微信高效地使用拍照、选图、语音、位置等手机系统的能力,同时可以直接使用微信分享、扫一扫等微信特有的能力,为微信用户提供更优质的网页体验。\n \n通过使用微信的JS-SDK,你可以让你网页在微信内调用拍照、语音、支付、位置、扫一扫这些只能在微信内使用的功能。进过下面的步骤,一步一步的配置,就可以让你正确的在项目中引入微信的JS-SDK。\n\n再正式使用微信JS SDK前,请查阅官方文档中5个重要步骤。\n\n[官方文档](https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115)\n\n\n#### 引入微信的JS文件\n\n1. 在需要调用JS接口的页面引入如下JS文件,(支持https)http://res.wx.qq.com/open/js/jweixin-1.4.0.js\n\n\n ```html\n <script src=\"http://res.wx.qq.com/open/js/jweixin-1.4.0.js\"></script>\n ```\n\n2. 但是只支持使用 AMD/CMD 标准模块加载方法加载。于是我就在npm的官网上找到了发布后的js-sdk,支持CommonJS的引入方式。npm的地址在[这里](https://www.npmjs.com/package/weixin-js-sdk)。可以在你的项目中使用如下命令安装。\n\n ```bash\n npm install weixin-js-sdk\n ```\n\n安装好后在js代码中引入\n\n ```js\n // 使用CommonJS规范\n\tlet wx = require('weixin-js-sdk')\n // CoffeeScript\n wx = require 'weixin-js-sdk'\n // ES6\n import wx from 'weixin-js-sdk'\n ```\n\n完成js引入后下面我们讲讲如何确实的在前端js中使用\n\n#### wx.config 鉴权签名算法\n\n>\t\n\t生成签名之前必须先了解一下jsapi_ticket,jsapi_ticket是企业号号用于调用微信JS接口的临时票据。正常情况下,jsapi_ticket的有效期为7200秒,通过access_token来获取。由于获取jsapi_ticket的api调用次数非常有限,频繁刷新jsapi_ticket会导致api调用受限,影响自身业务,开发者必须在自己的服务全局缓存jsapi_ticket。\n\n1.参考以下文档获取access_token(有效期7200秒,开发者必须在自己的服务全局缓存access_token):../15/54ce45d8d30b6bf6758f68d2e95bc627.html\n\n2.用第一步拿到的access_token 采用http GET方式请求获得jsapi_ticket(有效期7200秒,开发者必须在自己的服务全局缓存jsapi_ticket):https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi\n\n```json\n{\n 'errcode': 0,\n 'errmsg': 'ok',\n 'ticket': 'bxLdikRXVbTPdHSM05e5u5sUoXNKd841ZO3MhKoyN5OfkWITDGgnr2fwJ0m9E8NYzWKVZvdVtaUgWvsdshFKA',\n 'expires_in': 7200,\n}\n\n```\n\n##### 签名算法\n\n签名生成规则如下:参与签名的字段包括noncestr(随机字符串), 有效的jsapi_ticket, timestamp(时间戳), url(当前网页的URL,不包含#及其后面部分) 。对所有待签名参数按照字段名的ASCII 码从小到大排序(字典序)后,使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串string1。这里需要注意的是所有参数名均为小写字符。对string1作sha1加密,字段名和字段值都采用原始值,不进行URL 转义。\n\n即signature=sha1(string1)。 示例:\n\n```js\nnoncestr=Wm3WZYTPz0wzccnW\njsapi_ticket=sM4AOVdWfPE4DxkXGEs8VMCPGGVi4C3VM0P37wVUCFvkVAy_90u5h9nbSlYy3-Sl-HhTdfl2fzFy1AOcHKP7qg\ntimestamp=1414587457\nurl=http://mp.weixin.qq.com?params=value\n```\n步骤1. 对所有待签名参数按照字段名的ASCII 码从小到大排序(字典序)后,使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串string1:\n\n```js\njsapi_ticket=sM4AOVdWfPE4DxkXGEs8VMCPGGVi4C3VM0P37wVUCFvkVAy_90u5h9nbSlYy3-Sl-HhTdfl2fzFy1AOcHKP7qg&noncestr=Wm3WZYTPz0wzccnW×tamp=1414587457&url=http://mp.weixin.qq.com?params=value\n```\n\n步骤2. 对string1进行sha1签名,得到signature:\n\n```js\n0f9de62fce790f9a083d5c99e95740ceb90c27ed\n```\n\n注意事项\n\n\t1.签名用的noncestr和timestamp必须与wx.config中的nonceStr和timestamp相同。\n\t2.签名用的url必须是调用JS接口页面的完整URL。\n\t3.出于安全考虑,开发者必须在服务器端实现签名的逻辑。\n\n\n[签名校验工具](https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign)\n\n完成签名生成后,即可开始配置wxjs-sdk的使用了\n\n```js\nwx.config({\n debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。\n appId: '', // 必填,企业号的唯一标识,此处填写企业号corpid\n timestamp: , // 必填,生成签名的时间戳\n nonceStr: '', // 必填,生成签名的随机串\n signature: '',// 必填,签名,见附录1\n jsApiList: [] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2\n});\n```\n[weixinJS-SDK接口列表](https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115)\n\n写了这么多,我们来一段实际应用的代码\n\n```js\nimport wx from 'weixin-js-sdk'\najax.get({url: <getSignature>}).then((resp) => {\n\tif (resp.ok) {\n let signature = resp.data\n signature.debug = false\n signature.signature = signature.sign\n signature.nonceStr = signature.noncestr\n signature.appId = signature.appid\n signature.jsApiList = ['onMenuShareTimeline', 'checkJsApi',\n 'hideOptionMenu', 'showOptionMenu', 'onMenuShareAppMessage'\n ]\n wx.config(signature)\n wx.ready(()=> {\n \tlet callback = {\n \ttitle: 'test',\n desc: 'test',\n imgUrl: 'http://yourdomain.com',\n link: 'http://yourdomain.com',\n success: () => {\n \tconsole.log('分享成功')\n },\n cancel: () => {\n \tconsole.log('分享失败')\n },\n trigger: () => {\n \tconsole.log('')\n },\n complete: () => {\n \tconsole.log('接口调用完成')\n },\n fail () {\n \tconsole.log('调用失败')\n }\n }\n wx.onMenuShareAppMessage(callBack)\n wx.onMenuShareTimeline(callBack)\n wx.onMenuShareQQ(callBack)\n wx.onMenuShareWeibo(callBack)\n wx.onMenuShareQZone(callBack)\n })\n }\n})\n```\najax 调用就是获取签名数据后实现签名注册\n\n\t注意:不要尝试在trigger中使用ajax异步请求修改本次分享的内容,因为客户端分享操作是一个同步操作,这时候使用ajax的回包会还没有返回。\n\n\n##### 最后\n---\n\n\t如果页面的url发生了变化,在新的url下调用js-sdk一定要再调用一次签名接口,用新的url再进行一次签名,否则会调用不成功。\n \n[官方文档](https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115)\n\n[在线调试工具](https://mp.weixin.qq.com/debug)\n\n[签名校验工具](https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign)\n","slug":"微信JS在H5中的使用","published":1,"updated":"2018-09-13T10:28:45.655Z","comments":1,"layout":"post","photos":[],"link":"","_id":"cjoljzh1m000e0evofqgsm6ut","content":"<h4 id=\"微信JS-SDK的功能\"><a href=\"#微信JS-SDK的功能\" class=\"headerlink\" title=\"微信JS SDK的功能\"></a>微信JS SDK的功能</h4><blockquote>\n</blockquote>\n<pre><code>微信JS-SDK是微信公众平台面向网页开发者提供的基于微信内的网页开发工具包。\n通过使用微信JS-SDK,网页开发者可借助微信高效地使用拍照、选图、语音、位置等手机系统的能力,同时可以直接使用微信分享、扫一扫等微信特有的能力,为微信用户提供更优质的网页体验。\n</code></pre><p>通过使用微信的JS-SDK,你可以让你网页在微信内调用拍照、语音、支付、位置、扫一扫这些只能在微信内使用的功能。进过下面的步骤,一步一步的配置,就可以让你正确的在项目中引入微信的JS-SDK。</p>\n<p>再正式使用微信JS SDK前,请查阅官方文档中5个重要步骤。</p>\n<p><a href=\"https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115\" target=\"_blank\" rel=\"noopener\">官方文档</a></p>\n<h4 id=\"引入微信的JS文件\"><a href=\"#引入微信的JS文件\" class=\"headerlink\" title=\"引入微信的JS文件\"></a>引入微信的JS文件</h4><ol>\n<li>在需要调用JS接口的页面引入如下JS文件,(支持https)<a href=\"http://res.wx.qq.com/open/js/jweixin-1.4.0.js\" target=\"_blank\" rel=\"noopener\">http://res.wx.qq.com/open/js/jweixin-1.4.0.js</a></li>\n</ol>\n <figure class=\"highlight html\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"tag\"><<span class=\"name\">script</span> <span class=\"attr\">src</span>=<span class=\"string\">\"http://res.wx.qq.com/open/js/jweixin-1.4.0.js\"</span>></span><span class=\"undefined\"></span><span class=\"tag\"></<span class=\"name\">script</span>></span></span><br></pre></td></tr></table></figure>\n<ol start=\"2\">\n<li><p>但是只支持使用 AMD/CMD 标准模块加载方法加载。于是我就在npm的官网上找到了发布后的js-sdk,支持CommonJS的引入方式。npm的地址在<a href=\"https://www.npmjs.com/package/weixin-js-sdk\" target=\"_blank\" rel=\"noopener\">这里</a>。可以在你的项目中使用如下命令安装。</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">npm install weixin-js-sdk</span><br></pre></td></tr></table></figure>\n</li>\n</ol>\n<p>安装好后在js代码中引入</p>\n <figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"> <span class=\"comment\">// 使用CommonJS规范</span></span><br><span class=\"line\"><span class=\"keyword\">let</span> wx = <span class=\"built_in\">require</span>(<span class=\"string\">'weixin-js-sdk'</span>)</span><br><span class=\"line\"> <span class=\"comment\">// CoffeeScript</span></span><br><span class=\"line\"> wx = <span class=\"built_in\">require</span> <span class=\"string\">'weixin-js-sdk'</span></span><br><span class=\"line\"> <span class=\"comment\">// ES6</span></span><br><span class=\"line\"> <span class=\"keyword\">import</span> wx <span class=\"keyword\">from</span> <span class=\"string\">'weixin-js-sdk'</span></span><br></pre></td></tr></table></figure>\n<p>完成js引入后下面我们讲讲如何确实的在前端js中使用</p>\n<h4 id=\"wx-config-鉴权签名算法\"><a href=\"#wx-config-鉴权签名算法\" class=\"headerlink\" title=\"wx.config 鉴权签名算法\"></a>wx.config 鉴权签名算法</h4><blockquote>\n<pre><code>生成签名之前必须先了解一下jsapi_ticket,jsapi_ticket是企业号号用于调用微信JS接口的临时票据。正常情况下,jsapi_ticket的有效期为7200秒,通过access_token来获取。由于获取jsapi_ticket的api调用次数非常有限,频繁刷新jsapi_ticket会导致api调用受限,影响自身业务,开发者必须在自己的服务全局缓存jsapi_ticket。\n</code></pre></blockquote>\n<p>1.参考以下文档获取access_token(有效期7200秒,开发者必须在自己的服务全局缓存access_token):../15/54ce45d8d30b6bf6758f68d2e95bc627.html</p>\n<p>2.用第一步拿到的access_token 采用http GET方式请求获得jsapi_ticket(有效期7200秒,开发者必须在自己的服务全局缓存jsapi_ticket):<a href=\"https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi\" target=\"_blank\" rel=\"noopener\">https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi</a></p>\n<figure class=\"highlight\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">{</span><br><span class=\"line\"> 'errcode': 0,</span><br><span class=\"line\"> 'errmsg': 'ok',</span><br><span class=\"line\"> 'ticket': 'bxLdikRXVbTPdHSM05e5u5sUoXNKd841ZO3MhKoyN5OfkWITDGgnr2fwJ0m9E8NYzWKVZvdVtaUgWvsdshFKA',</span><br><span class=\"line\"> 'expires_in': 7200,</span><br><span class=\"line\">}</span><br></pre></td></tr></table></figure>\n<h5 id=\"签名算法\"><a href=\"#签名算法\" class=\"headerlink\" title=\"签名算法\"></a>签名算法</h5><p>签名生成规则如下:参与签名的字段包括noncestr(随机字符串), 有效的jsapi_ticket, timestamp(时间戳), url(当前网页的URL,不包含#及其后面部分) 。对所有待签名参数按照字段名的ASCII 码从小到大排序(字典序)后,使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串string1。这里需要注意的是所有参数名均为小写字符。对string1作sha1加密,字段名和字段值都采用原始值,不进行URL 转义。</p>\n<p>即signature=sha1(string1)。 示例:</p>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">noncestr=Wm3WZYTPz0wzccnW</span><br><span class=\"line\">jsapi_ticket=sM4AOVdWfPE4DxkXGEs8VMCPGGVi4C3VM0P37wVUCFvkVAy_90u5h9nbSlYy3-Sl-HhTdfl2fzFy1AOcHKP7qg</span><br><span class=\"line\">timestamp=<span class=\"number\">1414587457</span></span><br><span class=\"line\">url=http:<span class=\"comment\">//mp.weixin.qq.com?params=value</span></span><br></pre></td></tr></table></figure>\n<p>步骤1. 对所有待签名参数按照字段名的ASCII 码从小到大排序(字典序)后,使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串string1:</p>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">jsapi_ticket=sM4AOVdWfPE4DxkXGEs8VMCPGGVi4C3VM0P37wVUCFvkVAy_90u5h9nbSlYy3-Sl-HhTdfl2fzFy1AOcHKP7qg&noncestr=Wm3WZYTPz0wzccnW&timestamp=<span class=\"number\">1414587457</span>&url=http:<span class=\"comment\">//mp.weixin.qq.com?params=value</span></span><br></pre></td></tr></table></figure>\n<p>步骤2. 对string1进行sha1签名,得到signature:</p>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"number\">0</span>f9de62fce790f9a083d5c99e95740ceb90c27ed</span><br></pre></td></tr></table></figure>\n<p>注意事项</p>\n<pre><code>1.签名用的noncestr和timestamp必须与wx.config中的nonceStr和timestamp相同。\n2.签名用的url必须是调用JS接口页面的完整URL。\n3.出于安全考虑,开发者必须在服务器端实现签名的逻辑。\n</code></pre><p><a href=\"https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign\" target=\"_blank\" rel=\"noopener\">签名校验工具</a></p>\n<p>完成签名生成后,即可开始配置wxjs-sdk的使用了</p>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">wx.config({</span><br><span class=\"line\"> debug: <span class=\"literal\">true</span>, <span class=\"comment\">// 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。</span></span><br><span class=\"line\"> appId: <span class=\"string\">''</span>, <span class=\"comment\">// 必填,企业号的唯一标识,此处填写企业号corpid</span></span><br><span class=\"line\"> timestamp: , <span class=\"comment\">// 必填,生成签名的时间戳</span></span><br><span class=\"line\"> nonceStr: <span class=\"string\">''</span>, <span class=\"comment\">// 必填,生成签名的随机串</span></span><br><span class=\"line\"> signature: <span class=\"string\">''</span>,<span class=\"comment\">// 必填,签名,见附录1</span></span><br><span class=\"line\"> jsApiList: [] <span class=\"comment\">// 必填,需要使用的JS接口列表,所有JS接口列表见附录2</span></span><br><span class=\"line\">});</span><br></pre></td></tr></table></figure>\n<p><a href=\"https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115\" target=\"_blank\" rel=\"noopener\">weixinJS-SDK接口列表</a></p>\n<p>写了这么多,我们来一段实际应用的代码</p>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br><span class=\"line\">17</span><br><span class=\"line\">18</span><br><span class=\"line\">19</span><br><span class=\"line\">20</span><br><span class=\"line\">21</span><br><span class=\"line\">22</span><br><span class=\"line\">23</span><br><span class=\"line\">24</span><br><span class=\"line\">25</span><br><span class=\"line\">26</span><br><span class=\"line\">27</span><br><span class=\"line\">28</span><br><span class=\"line\">29</span><br><span class=\"line\">30</span><br><span class=\"line\">31</span><br><span class=\"line\">32</span><br><span class=\"line\">33</span><br><span class=\"line\">34</span><br><span class=\"line\">35</span><br><span class=\"line\">36</span><br><span class=\"line\">37</span><br><span class=\"line\">38</span><br><span class=\"line\">39</span><br><span class=\"line\">40</span><br><span class=\"line\">41</span><br><span class=\"line\">42</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"keyword\">import</span> wx <span class=\"keyword\">from</span> <span class=\"string\">'weixin-js-sdk'</span></span><br><span class=\"line\">ajax.get({<span class=\"attr\">url</span>: <span class=\"xml\"><span class=\"tag\"><<span class=\"name\">getSignature</span>></span>}).then((resp) => {</span></span><br><span class=\"line\"><span class=\"xml\">\tif (resp.ok) {</span></span><br><span class=\"line\"><span class=\"xml\"> let signature = resp.data</span></span><br><span class=\"line\"><span class=\"xml\"> signature.debug = false</span></span><br><span class=\"line\"><span class=\"xml\"> signature.signature = signature.sign</span></span><br><span class=\"line\"><span class=\"xml\"> signature.nonceStr = signature.noncestr</span></span><br><span class=\"line\"><span class=\"xml\"> signature.appId = signature.appid</span></span><br><span class=\"line\"><span class=\"xml\"> signature.jsApiList = ['onMenuShareTimeline', 'checkJsApi',</span></span><br><span class=\"line\"><span class=\"xml\"> 'hideOptionMenu', 'showOptionMenu', 'onMenuShareAppMessage'</span></span><br><span class=\"line\"><span class=\"xml\"> ]</span></span><br><span class=\"line\"><span class=\"xml\"> wx.config(signature)</span></span><br><span class=\"line\"><span class=\"xml\"> wx.ready(()=> {</span></span><br><span class=\"line\"><span class=\"xml\"> \tlet callback = {</span></span><br><span class=\"line\"><span class=\"xml\"> \ttitle: 'test',</span></span><br><span class=\"line\"><span class=\"xml\"> desc: 'test',</span></span><br><span class=\"line\"><span class=\"xml\"> imgUrl: 'http://yourdomain.com',</span></span><br><span class=\"line\"><span class=\"xml\"> link: 'http://yourdomain.com',</span></span><br><span class=\"line\"><span class=\"xml\"> success: () => {</span></span><br><span class=\"line\"><span class=\"xml\"> \tconsole.log('分享成功')</span></span><br><span class=\"line\"><span class=\"xml\"> },</span></span><br><span class=\"line\"><span class=\"xml\"> cancel: () => {</span></span><br><span class=\"line\"><span class=\"xml\"> \tconsole.log('分享失败')</span></span><br><span class=\"line\"><span class=\"xml\"> },</span></span><br><span class=\"line\"><span class=\"xml\"> trigger: () => {</span></span><br><span class=\"line\"><span class=\"xml\"> \tconsole.log('')</span></span><br><span class=\"line\"><span class=\"xml\"> },</span></span><br><span class=\"line\"><span class=\"xml\"> complete: () => {</span></span><br><span class=\"line\"><span class=\"xml\"> \tconsole.log('接口调用完成')</span></span><br><span class=\"line\"><span class=\"xml\"> },</span></span><br><span class=\"line\"><span class=\"xml\"> fail () {</span></span><br><span class=\"line\"><span class=\"xml\"> \tconsole.log('调用失败')</span></span><br><span class=\"line\"><span class=\"xml\"> }</span></span><br><span class=\"line\"><span class=\"xml\"> }</span></span><br><span class=\"line\"><span class=\"xml\"> wx.onMenuShareAppMessage(callBack)</span></span><br><span class=\"line\"><span class=\"xml\"> wx.onMenuShareTimeline(callBack)</span></span><br><span class=\"line\"><span class=\"xml\"> wx.onMenuShareQQ(callBack)</span></span><br><span class=\"line\"><span class=\"xml\"> wx.onMenuShareWeibo(callBack)</span></span><br><span class=\"line\"><span class=\"xml\"> wx.onMenuShareQZone(callBack)</span></span><br><span class=\"line\"><span class=\"xml\"> })</span></span><br><span class=\"line\"><span class=\"xml\"> }</span></span><br><span class=\"line\"><span class=\"xml\">})</span></span><br></pre></td></tr></table></figure>\n<p>ajax 调用就是获取签名数据后实现签名注册</p>\n<pre><code>注意:不要尝试在trigger中使用ajax异步请求修改本次分享的内容,因为客户端分享操作是一个同步操作,这时候使用ajax的回包会还没有返回。\n</code></pre><h5 id=\"最后\"><a href=\"#最后\" class=\"headerlink\" title=\"最后\"></a>最后</h5><hr>\n<pre><code>如果页面的url发生了变化,在新的url下调用js-sdk一定要再调用一次签名接口,用新的url再进行一次签名,否则会调用不成功。\n</code></pre><p><a href=\"https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115\" target=\"_blank\" rel=\"noopener\">官方文档</a></p>\n<p><a href=\"https://mp.weixin.qq.com/debug\" target=\"_blank\" rel=\"noopener\">在线调试工具</a></p>\n<p><a href=\"https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign\" target=\"_blank\" rel=\"noopener\">签名校验工具</a></p>\n","site":{"data":{}},"excerpt":"","more":"<h4 id=\"微信JS-SDK的功能\"><a href=\"#微信JS-SDK的功能\" class=\"headerlink\" title=\"微信JS SDK的功能\"></a>微信JS SDK的功能</h4><blockquote>\n</blockquote>\n<pre><code>微信JS-SDK是微信公众平台面向网页开发者提供的基于微信内的网页开发工具包。\n通过使用微信JS-SDK,网页开发者可借助微信高效地使用拍照、选图、语音、位置等手机系统的能力,同时可以直接使用微信分享、扫一扫等微信特有的能力,为微信用户提供更优质的网页体验。\n</code></pre><p>通过使用微信的JS-SDK,你可以让你网页在微信内调用拍照、语音、支付、位置、扫一扫这些只能在微信内使用的功能。进过下面的步骤,一步一步的配置,就可以让你正确的在项目中引入微信的JS-SDK。</p>\n<p>再正式使用微信JS SDK前,请查阅官方文档中5个重要步骤。</p>\n<p><a href=\"https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115\" target=\"_blank\" rel=\"noopener\">官方文档</a></p>\n<h4 id=\"引入微信的JS文件\"><a href=\"#引入微信的JS文件\" class=\"headerlink\" title=\"引入微信的JS文件\"></a>引入微信的JS文件</h4><ol>\n<li>在需要调用JS接口的页面引入如下JS文件,(支持https)<a href=\"http://res.wx.qq.com/open/js/jweixin-1.4.0.js\" target=\"_blank\" rel=\"noopener\">http://res.wx.qq.com/open/js/jweixin-1.4.0.js</a></li>\n</ol>\n <figure class=\"highlight html\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"tag\"><<span class=\"name\">script</span> <span class=\"attr\">src</span>=<span class=\"string\">\"http://res.wx.qq.com/open/js/jweixin-1.4.0.js\"</span>></span><span class=\"undefined\"></span><span class=\"tag\"></<span class=\"name\">script</span>></span></span><br></pre></td></tr></table></figure>\n<ol start=\"2\">\n<li><p>但是只支持使用 AMD/CMD 标准模块加载方法加载。于是我就在npm的官网上找到了发布后的js-sdk,支持CommonJS的引入方式。npm的地址在<a href=\"https://www.npmjs.com/package/weixin-js-sdk\" target=\"_blank\" rel=\"noopener\">这里</a>。可以在你的项目中使用如下命令安装。</p>\n<figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">npm install weixin-js-sdk</span><br></pre></td></tr></table></figure>\n</li>\n</ol>\n<p>安装好后在js代码中引入</p>\n <figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"> <span class=\"comment\">// 使用CommonJS规范</span></span><br><span class=\"line\"><span class=\"keyword\">let</span> wx = <span class=\"built_in\">require</span>(<span class=\"string\">'weixin-js-sdk'</span>)</span><br><span class=\"line\"> <span class=\"comment\">// CoffeeScript</span></span><br><span class=\"line\"> wx = <span class=\"built_in\">require</span> <span class=\"string\">'weixin-js-sdk'</span></span><br><span class=\"line\"> <span class=\"comment\">// ES6</span></span><br><span class=\"line\"> <span class=\"keyword\">import</span> wx <span class=\"keyword\">from</span> <span class=\"string\">'weixin-js-sdk'</span></span><br></pre></td></tr></table></figure>\n<p>完成js引入后下面我们讲讲如何确实的在前端js中使用</p>\n<h4 id=\"wx-config-鉴权签名算法\"><a href=\"#wx-config-鉴权签名算法\" class=\"headerlink\" title=\"wx.config 鉴权签名算法\"></a>wx.config 鉴权签名算法</h4><blockquote>\n<pre><code>生成签名之前必须先了解一下jsapi_ticket,jsapi_ticket是企业号号用于调用微信JS接口的临时票据。正常情况下,jsapi_ticket的有效期为7200秒,通过access_token来获取。由于获取jsapi_ticket的api调用次数非常有限,频繁刷新jsapi_ticket会导致api调用受限,影响自身业务,开发者必须在自己的服务全局缓存jsapi_ticket。\n</code></pre></blockquote>\n<p>1.参考以下文档获取access_token(有效期7200秒,开发者必须在自己的服务全局缓存access_token):../15/54ce45d8d30b6bf6758f68d2e95bc627.html</p>\n<p>2.用第一步拿到的access_token 采用http GET方式请求获得jsapi_ticket(有效期7200秒,开发者必须在自己的服务全局缓存jsapi_ticket):<a href=\"https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi\" target=\"_blank\" rel=\"noopener\">https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi</a></p>\n<figure class=\"highlight\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">{</span><br><span class=\"line\"> 'errcode': 0,</span><br><span class=\"line\"> 'errmsg': 'ok',</span><br><span class=\"line\"> 'ticket': 'bxLdikRXVbTPdHSM05e5u5sUoXNKd841ZO3MhKoyN5OfkWITDGgnr2fwJ0m9E8NYzWKVZvdVtaUgWvsdshFKA',</span><br><span class=\"line\"> 'expires_in': 7200,</span><br><span class=\"line\">}</span><br></pre></td></tr></table></figure>\n<h5 id=\"签名算法\"><a href=\"#签名算法\" class=\"headerlink\" title=\"签名算法\"></a>签名算法</h5><p>签名生成规则如下:参与签名的字段包括noncestr(随机字符串), 有效的jsapi_ticket, timestamp(时间戳), url(当前网页的URL,不包含#及其后面部分) 。对所有待签名参数按照字段名的ASCII 码从小到大排序(字典序)后,使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串string1。这里需要注意的是所有参数名均为小写字符。对string1作sha1加密,字段名和字段值都采用原始值,不进行URL 转义。</p>\n<p>即signature=sha1(string1)。 示例:</p>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">noncestr=Wm3WZYTPz0wzccnW</span><br><span class=\"line\">jsapi_ticket=sM4AOVdWfPE4DxkXGEs8VMCPGGVi4C3VM0P37wVUCFvkVAy_90u5h9nbSlYy3-Sl-HhTdfl2fzFy1AOcHKP7qg</span><br><span class=\"line\">timestamp=<span class=\"number\">1414587457</span></span><br><span class=\"line\">url=http:<span class=\"comment\">//mp.weixin.qq.com?params=value</span></span><br></pre></td></tr></table></figure>\n<p>步骤1. 对所有待签名参数按照字段名的ASCII 码从小到大排序(字典序)后,使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串string1:</p>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">jsapi_ticket=sM4AOVdWfPE4DxkXGEs8VMCPGGVi4C3VM0P37wVUCFvkVAy_90u5h9nbSlYy3-Sl-HhTdfl2fzFy1AOcHKP7qg&noncestr=Wm3WZYTPz0wzccnW&timestamp=<span class=\"number\">1414587457</span>&url=http:<span class=\"comment\">//mp.weixin.qq.com?params=value</span></span><br></pre></td></tr></table></figure>\n<p>步骤2. 对string1进行sha1签名,得到signature:</p>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"number\">0</span>f9de62fce790f9a083d5c99e95740ceb90c27ed</span><br></pre></td></tr></table></figure>\n<p>注意事项</p>\n<pre><code>1.签名用的noncestr和timestamp必须与wx.config中的nonceStr和timestamp相同。\n2.签名用的url必须是调用JS接口页面的完整URL。\n3.出于安全考虑,开发者必须在服务器端实现签名的逻辑。\n</code></pre><p><a href=\"https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign\" target=\"_blank\" rel=\"noopener\">签名校验工具</a></p>\n<p>完成签名生成后,即可开始配置wxjs-sdk的使用了</p>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">wx.config({</span><br><span class=\"line\"> debug: <span class=\"literal\">true</span>, <span class=\"comment\">// 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。</span></span><br><span class=\"line\"> appId: <span class=\"string\">''</span>, <span class=\"comment\">// 必填,企业号的唯一标识,此处填写企业号corpid</span></span><br><span class=\"line\"> timestamp: , <span class=\"comment\">// 必填,生成签名的时间戳</span></span><br><span class=\"line\"> nonceStr: <span class=\"string\">''</span>, <span class=\"comment\">// 必填,生成签名的随机串</span></span><br><span class=\"line\"> signature: <span class=\"string\">''</span>,<span class=\"comment\">// 必填,签名,见附录1</span></span><br><span class=\"line\"> jsApiList: [] <span class=\"comment\">// 必填,需要使用的JS接口列表,所有JS接口列表见附录2</span></span><br><span class=\"line\">});</span><br></pre></td></tr></table></figure>\n<p><a href=\"https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115\" target=\"_blank\" rel=\"noopener\">weixinJS-SDK接口列表</a></p>\n<p>写了这么多,我们来一段实际应用的代码</p>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br><span class=\"line\">17</span><br><span class=\"line\">18</span><br><span class=\"line\">19</span><br><span class=\"line\">20</span><br><span class=\"line\">21</span><br><span class=\"line\">22</span><br><span class=\"line\">23</span><br><span class=\"line\">24</span><br><span class=\"line\">25</span><br><span class=\"line\">26</span><br><span class=\"line\">27</span><br><span class=\"line\">28</span><br><span class=\"line\">29</span><br><span class=\"line\">30</span><br><span class=\"line\">31</span><br><span class=\"line\">32</span><br><span class=\"line\">33</span><br><span class=\"line\">34</span><br><span class=\"line\">35</span><br><span class=\"line\">36</span><br><span class=\"line\">37</span><br><span class=\"line\">38</span><br><span class=\"line\">39</span><br><span class=\"line\">40</span><br><span class=\"line\">41</span><br><span class=\"line\">42</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"keyword\">import</span> wx <span class=\"keyword\">from</span> <span class=\"string\">'weixin-js-sdk'</span></span><br><span class=\"line\">ajax.get({<span class=\"attr\">url</span>: <span class=\"xml\"><span class=\"tag\"><<span class=\"name\">getSignature</span>></span>}).then((resp) => {</span></span><br><span class=\"line\"><span class=\"xml\">\tif (resp.ok) {</span></span><br><span class=\"line\"><span class=\"xml\"> let signature = resp.data</span></span><br><span class=\"line\"><span class=\"xml\"> signature.debug = false</span></span><br><span class=\"line\"><span class=\"xml\"> signature.signature = signature.sign</span></span><br><span class=\"line\"><span class=\"xml\"> signature.nonceStr = signature.noncestr</span></span><br><span class=\"line\"><span class=\"xml\"> signature.appId = signature.appid</span></span><br><span class=\"line\"><span class=\"xml\"> signature.jsApiList = ['onMenuShareTimeline', 'checkJsApi',</span></span><br><span class=\"line\"><span class=\"xml\"> 'hideOptionMenu', 'showOptionMenu', 'onMenuShareAppMessage'</span></span><br><span class=\"line\"><span class=\"xml\"> ]</span></span><br><span class=\"line\"><span class=\"xml\"> wx.config(signature)</span></span><br><span class=\"line\"><span class=\"xml\"> wx.ready(()=> {</span></span><br><span class=\"line\"><span class=\"xml\"> \tlet callback = {</span></span><br><span class=\"line\"><span class=\"xml\"> \ttitle: 'test',</span></span><br><span class=\"line\"><span class=\"xml\"> desc: 'test',</span></span><br><span class=\"line\"><span class=\"xml\"> imgUrl: 'http://yourdomain.com',</span></span><br><span class=\"line\"><span class=\"xml\"> link: 'http://yourdomain.com',</span></span><br><span class=\"line\"><span class=\"xml\"> success: () => {</span></span><br><span class=\"line\"><span class=\"xml\"> \tconsole.log('分享成功')</span></span><br><span class=\"line\"><span class=\"xml\"> },</span></span><br><span class=\"line\"><span class=\"xml\"> cancel: () => {</span></span><br><span class=\"line\"><span class=\"xml\"> \tconsole.log('分享失败')</span></span><br><span class=\"line\"><span class=\"xml\"> },</span></span><br><span class=\"line\"><span class=\"xml\"> trigger: () => {</span></span><br><span class=\"line\"><span class=\"xml\"> \tconsole.log('')</span></span><br><span class=\"line\"><span class=\"xml\"> },</span></span><br><span class=\"line\"><span class=\"xml\"> complete: () => {</span></span><br><span class=\"line\"><span class=\"xml\"> \tconsole.log('接口调用完成')</span></span><br><span class=\"line\"><span class=\"xml\"> },</span></span><br><span class=\"line\"><span class=\"xml\"> fail () {</span></span><br><span class=\"line\"><span class=\"xml\"> \tconsole.log('调用失败')</span></span><br><span class=\"line\"><span class=\"xml\"> }</span></span><br><span class=\"line\"><span class=\"xml\"> }</span></span><br><span class=\"line\"><span class=\"xml\"> wx.onMenuShareAppMessage(callBack)</span></span><br><span class=\"line\"><span class=\"xml\"> wx.onMenuShareTimeline(callBack)</span></span><br><span class=\"line\"><span class=\"xml\"> wx.onMenuShareQQ(callBack)</span></span><br><span class=\"line\"><span class=\"xml\"> wx.onMenuShareWeibo(callBack)</span></span><br><span class=\"line\"><span class=\"xml\"> wx.onMenuShareQZone(callBack)</span></span><br><span class=\"line\"><span class=\"xml\"> })</span></span><br><span class=\"line\"><span class=\"xml\"> }</span></span><br><span class=\"line\"><span class=\"xml\">})</span></span><br></pre></td></tr></table></figure>\n<p>ajax 调用就是获取签名数据后实现签名注册</p>\n<pre><code>注意:不要尝试在trigger中使用ajax异步请求修改本次分享的内容,因为客户端分享操作是一个同步操作,这时候使用ajax的回包会还没有返回。\n</code></pre><h5 id=\"最后\"><a href=\"#最后\" class=\"headerlink\" title=\"最后\"></a>最后</h5><hr>\n<pre><code>如果页面的url发生了变化,在新的url下调用js-sdk一定要再调用一次签名接口,用新的url再进行一次签名,否则会调用不成功。\n</code></pre><p><a href=\"https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115\" target=\"_blank\" rel=\"noopener\">官方文档</a></p>\n<p><a href=\"https://mp.weixin.qq.com/debug\" target=\"_blank\" rel=\"noopener\">在线调试工具</a></p>\n<p><a href=\"https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign\" target=\"_blank\" rel=\"noopener\">签名校验工具</a></p>\n"},{"title":"迭代器,生成器(generator)和Promise的“微妙”关系","author":"brian","date":"2018-08-29T08:09:00.000Z","_content":"> 好久没写blog了,重新开始,今天来聊聊generator,promise的关系\n\n这次主要讲讲(iterator)和生成器*/yield之间的联系和各自的用法,以及生成器的高配版本aysnc/await的使用。\n\n栏目:\n - 迭代器(iterator)\n - 生成器 */yield\n - 异步版生成器 aysnc/await\n\n\n##### 迭代器(iterator)\n----------------------\n\n\t首先迭代器这个词是什么意思呢?每一次“过程”的重复,称之为迭代。不过迭代是会保留结果的,也就说每次都是以上一次迭代的结果为基准,开始下一次的迭代。举个例子,迭代这个词经常出现在产品开发之中,每个周期都会有产品的迭代开发,但是不可能每次都是从零开始做产品,肯定是基于上一版本的产品进行开发,也就是进行迭代。\n \n从中我们可以整理出关于迭代关键点:\n- 过程是重复的\n- 返回上一次的迭代结果\n\n那么javascript中的“迭代器”是个怎样的概念呢?\n\n\tjavascript中的迭代器,就是一个数组对象,不断地调用next重复获取过程,然后每次都返回一个结果。等到没有东西可返回了,就终止。因此next的返回对象有两个属性done和value。done表示是否结束了,value表示当前迭代的结果。当done为true的时候,表示迭代已结束,这时候是没有返回结果的也就是没有value这个属性。\n \n- 关于迭代器,就是我们上面讨论的next方法,返回done和value(done:true时可以省略)两个参数。\n\n```js\n function iteratorFunc(){\n let arr=[...arguments]\n let nIndex=0\n return {\n next:()=>{\n return nIndex<arr.length?\n {value:arr[nIndex++],done:false}:{done:true}\n }\n }\n}\nlet a=iteratorFunc(1,2,3)\nconsole.log(a.next())//{done:false,value:1}\nconsole.log(a.next())//{done:false,value:2}\nconsole.log(a.next())//{done:false,value:3}\nconsole.log(a.next())//{done:true}\n```\n###### 可迭代“对象”\n- 关于可迭代“对象”,我们需要再对象上实现@@iterator方法,也就是[Symbol.iterator],返回一个自定义的迭代方法,以表明这个对象是可以迭代的。有些JS内置的对象就是可迭代的,比如String,Array。\n\n> 自带的迭代事例\n\n```js\n let str=\"我是欢乐的迭代器\"\n let b=str[Symbol.iterator]()\n console.log(b.next())//{value: \"I\", done: false}\n console.log(b.next())//{value: \"M\", done: false}\n console.log(b.next())//{value: \"B\", done: false}\n```\n\n> 自定义迭代方法\n\n```js\n str[Symbol.iterator] = function() {\n return { // this is the iterator object, returning a single element, the string \"bye\"\n next: function() {\n this._index += 2\n if (this._index<str.length) {\n return { value: str[this._index], done: false };\n } else {\n return { done: true };\n }\n },\n _index:-2\n };\n };\n let c=str[Symbol.iterator]()\n console.log(c.next())//{value: \"I\", done: false}\n console.log(c.next())//{value: \"M\", done: false}\n console.log(c.next())//{value: \"B\", done: false}\n console.log(c.next())//{value: \"R\", done: false}\n console.log(c.next())//{done: true}\n```\n\n##### 生成器-generator\n----------------------\n\n> 生成器(generator),专门帮我们生成迭代器\n\n```js\n function * g(){}\n let it= g()\n console.log(it.next())//{value: undefined, done: true}\n```\n\n> 比较老土的实现方法\n```js\n str[Symbol.iterator]= function * (){\n let index=-2;\n while(index<this.length){\n index += 2\n yield this[index]\n }\n }\n let kk=str[Symbol.iterator]()\n console.log(kk.next())//{value: \"I\", done: false}\n console.log(kk.next())//{value: \"M\", done: false}\n console.log(kk.next())//{value: \"B\", done: false}\n console.log(kk.next())//{value: \"R\", done: false}\n```\n> 生成器到底干了什么?\n- yield 的返回值是啥?\n- 执行顺序是什么样的?\n\n```js\n function * gy(){\n console.log(\"zero\")\n let fisrt=yield \"first\"\n console.log(\"fisrt\",fisrt)\n let second=yield \"first\"\n console.log(\"second\",second)\n }\n let ity= gy()\n```\n> run code\n```bash\n> ity.next() # zero\n> ity.next() # first undefined\n> ity.next(\"third\") # second third\n```\n> 由此可见next每次都停止在yield就不再继续执行。yield每次返回的都是当前的ity.next(value)和value的值\n\n###### aysnc/await\n\n> 对于Promise这个对象的迭代器,我们该怎么处理。也就是每个迭代器都是异步的\n\n```js\n function setTime(value,id){\n return new Promise((r,j)=>setTimeout(() => {\n console.log(value)\n r(id)\n }, 10))\n }\n function *a(){\n let r1 = yield setTime(\"first\",1)\n console.log(r1)\n let r2 =yield setTime(\"second\",2)\n console.log(r2)\n let r3 =yield setTime(\"third\",3)\n console.log(r3)\n }\n let k=a();\n new Promise((resolve,reject)=>{\n function next(data){\n let {value,done}=k.next(data)\n //k.next()返回一个promise,因此可以then\n if(!done){\n value.then((data)=>{\n console.log(data)\n next(data)\n })\n }\n }\n next();\n })\n```\n\n> 这个时候aysnc/await就可以出场了。只需要把*/yield无缝改成aysnc/await即可。\n\n```js\nasync function a() {\n let r1 = await setTime(\"first\",1)\n console.log(r1)\n let r2 = await setTime(\"second\",2)\n console.log(r2)\n let r3 = await setTime(\"third\",3)\n console.log(r3)\n}\na()\n```","source":"_posts/迭代器-生成器-generator-和Promise的“微妙”关系.md","raw":"title: '迭代器,生成器(generator)和Promise的“微妙”关系'\nauthor: brian\ntags: []\ncategories: []\ndate: 2018-08-29 16:09:00\n---\n> 好久没写blog了,重新开始,今天来聊聊generator,promise的关系\n\n这次主要讲讲(iterator)和生成器*/yield之间的联系和各自的用法,以及生成器的高配版本aysnc/await的使用。\n\n栏目:\n - 迭代器(iterator)\n - 生成器 */yield\n - 异步版生成器 aysnc/await\n\n\n##### 迭代器(iterator)\n----------------------\n\n\t首先迭代器这个词是什么意思呢?每一次“过程”的重复,称之为迭代。不过迭代是会保留结果的,也就说每次都是以上一次迭代的结果为基准,开始下一次的迭代。举个例子,迭代这个词经常出现在产品开发之中,每个周期都会有产品的迭代开发,但是不可能每次都是从零开始做产品,肯定是基于上一版本的产品进行开发,也就是进行迭代。\n \n从中我们可以整理出关于迭代关键点:\n- 过程是重复的\n- 返回上一次的迭代结果\n\n那么javascript中的“迭代器”是个怎样的概念呢?\n\n\tjavascript中的迭代器,就是一个数组对象,不断地调用next重复获取过程,然后每次都返回一个结果。等到没有东西可返回了,就终止。因此next的返回对象有两个属性done和value。done表示是否结束了,value表示当前迭代的结果。当done为true的时候,表示迭代已结束,这时候是没有返回结果的也就是没有value这个属性。\n \n- 关于迭代器,就是我们上面讨论的next方法,返回done和value(done:true时可以省略)两个参数。\n\n```js\n function iteratorFunc(){\n let arr=[...arguments]\n let nIndex=0\n return {\n next:()=>{\n return nIndex<arr.length?\n {value:arr[nIndex++],done:false}:{done:true}\n }\n }\n}\nlet a=iteratorFunc(1,2,3)\nconsole.log(a.next())//{done:false,value:1}\nconsole.log(a.next())//{done:false,value:2}\nconsole.log(a.next())//{done:false,value:3}\nconsole.log(a.next())//{done:true}\n```\n###### 可迭代“对象”\n- 关于可迭代“对象”,我们需要再对象上实现@@iterator方法,也就是[Symbol.iterator],返回一个自定义的迭代方法,以表明这个对象是可以迭代的。有些JS内置的对象就是可迭代的,比如String,Array。\n\n> 自带的迭代事例\n\n```js\n let str=\"我是欢乐的迭代器\"\n let b=str[Symbol.iterator]()\n console.log(b.next())//{value: \"I\", done: false}\n console.log(b.next())//{value: \"M\", done: false}\n console.log(b.next())//{value: \"B\", done: false}\n```\n\n> 自定义迭代方法\n\n```js\n str[Symbol.iterator] = function() {\n return { // this is the iterator object, returning a single element, the string \"bye\"\n next: function() {\n this._index += 2\n if (this._index<str.length) {\n return { value: str[this._index], done: false };\n } else {\n return { done: true };\n }\n },\n _index:-2\n };\n };\n let c=str[Symbol.iterator]()\n console.log(c.next())//{value: \"I\", done: false}\n console.log(c.next())//{value: \"M\", done: false}\n console.log(c.next())//{value: \"B\", done: false}\n console.log(c.next())//{value: \"R\", done: false}\n console.log(c.next())//{done: true}\n```\n\n##### 生成器-generator\n----------------------\n\n> 生成器(generator),专门帮我们生成迭代器\n\n```js\n function * g(){}\n let it= g()\n console.log(it.next())//{value: undefined, done: true}\n```\n\n> 比较老土的实现方法\n```js\n str[Symbol.iterator]= function * (){\n let index=-2;\n while(index<this.length){\n index += 2\n yield this[index]\n }\n }\n let kk=str[Symbol.iterator]()\n console.log(kk.next())//{value: \"I\", done: false}\n console.log(kk.next())//{value: \"M\", done: false}\n console.log(kk.next())//{value: \"B\", done: false}\n console.log(kk.next())//{value: \"R\", done: false}\n```\n> 生成器到底干了什么?\n- yield 的返回值是啥?\n- 执行顺序是什么样的?\n\n```js\n function * gy(){\n console.log(\"zero\")\n let fisrt=yield \"first\"\n console.log(\"fisrt\",fisrt)\n let second=yield \"first\"\n console.log(\"second\",second)\n }\n let ity= gy()\n```\n> run code\n```bash\n> ity.next() # zero\n> ity.next() # first undefined\n> ity.next(\"third\") # second third\n```\n> 由此可见next每次都停止在yield就不再继续执行。yield每次返回的都是当前的ity.next(value)和value的值\n\n###### aysnc/await\n\n> 对于Promise这个对象的迭代器,我们该怎么处理。也就是每个迭代器都是异步的\n\n```js\n function setTime(value,id){\n return new Promise((r,j)=>setTimeout(() => {\n console.log(value)\n r(id)\n }, 10))\n }\n function *a(){\n let r1 = yield setTime(\"first\",1)\n console.log(r1)\n let r2 =yield setTime(\"second\",2)\n console.log(r2)\n let r3 =yield setTime(\"third\",3)\n console.log(r3)\n }\n let k=a();\n new Promise((resolve,reject)=>{\n function next(data){\n let {value,done}=k.next(data)\n //k.next()返回一个promise,因此可以then\n if(!done){\n value.then((data)=>{\n console.log(data)\n next(data)\n })\n }\n }\n next();\n })\n```\n\n> 这个时候aysnc/await就可以出场了。只需要把*/yield无缝改成aysnc/await即可。\n\n```js\nasync function a() {\n let r1 = await setTime(\"first\",1)\n console.log(r1)\n let r2 = await setTime(\"second\",2)\n console.log(r2)\n let r3 = await setTime(\"third\",3)\n console.log(r3)\n}\na()\n```","slug":"迭代器-生成器-generator-和Promise的“微妙”关系","published":1,"updated":"2018-08-29T08:32:56.869Z","comments":1,"layout":"post","photos":[],"link":"","_id":"cjoljzh1o000h0evomsod5j2o","content":"<blockquote>\n<p>好久没写blog了,重新开始,今天来聊聊generator,promise的关系</p>\n</blockquote>\n<p>这次主要讲讲(iterator)和生成器*/yield之间的联系和各自的用法,以及生成器的高配版本aysnc/await的使用。</p>\n<p>栏目:</p>\n<ul>\n<li>迭代器(iterator)</li>\n<li>生成器 */yield</li>\n<li>异步版生成器 aysnc/await</li>\n</ul>\n<h5 id=\"迭代器(iterator)\"><a href=\"#迭代器(iterator)\" class=\"headerlink\" title=\"迭代器(iterator)\"></a>迭代器(iterator)</h5><hr>\n<pre><code>首先迭代器这个词是什么意思呢?每一次“过程”的重复,称之为迭代。不过迭代是会保留结果的,也就说每次都是以上一次迭代的结果为基准,开始下一次的迭代。举个例子,迭代这个词经常出现在产品开发之中,每个周期都会有产品的迭代开发,但是不可能每次都是从零开始做产品,肯定是基于上一版本的产品进行开发,也就是进行迭代。\n</code></pre><p>从中我们可以整理出关于迭代关键点:</p>\n<ul>\n<li>过程是重复的</li>\n<li>返回上一次的迭代结果</li>\n</ul>\n<p>那么javascript中的“迭代器”是个怎样的概念呢?</p>\n<pre><code>javascript中的迭代器,就是一个数组对象,不断地调用next重复获取过程,然后每次都返回一个结果。等到没有东西可返回了,就终止。因此next的返回对象有两个属性done和value。done表示是否结束了,value表示当前迭代的结果。当done为true的时候,表示迭代已结束,这时候是没有返回结果的也就是没有value这个属性。\n</code></pre><ul>\n<li>关于迭代器,就是我们上面讨论的next方法,返回done和value(done:true时可以省略)两个参数。</li>\n</ul>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"> <span class=\"function\"><span class=\"keyword\">function</span> <span class=\"title\">iteratorFunc</span>(<span class=\"params\"></span>)</span>{</span><br><span class=\"line\"> <span class=\"keyword\">let</span> arr=[...arguments]</span><br><span class=\"line\"> <span class=\"keyword\">let</span> nIndex=<span class=\"number\">0</span></span><br><span class=\"line\"> <span class=\"keyword\">return</span> {</span><br><span class=\"line\"> next:<span class=\"function\"><span class=\"params\">()</span>=></span>{</span><br><span class=\"line\"> <span class=\"keyword\">return</span> nIndex<arr.length?</span><br><span class=\"line\"> {<span class=\"attr\">value</span>:arr[nIndex++],<span class=\"attr\">done</span>:<span class=\"literal\">false</span>}:{<span class=\"attr\">done</span>:<span class=\"literal\">true</span>}</span><br><span class=\"line\"> }</span><br><span class=\"line\"> }</span><br><span class=\"line\">}</span><br><span class=\"line\"><span class=\"keyword\">let</span> a=iteratorFunc(<span class=\"number\">1</span>,<span class=\"number\">2</span>,<span class=\"number\">3</span>)</span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(a.next())<span class=\"comment\">//{done:false,value:1}</span></span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(a.next())<span class=\"comment\">//{done:false,value:2}</span></span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(a.next())<span class=\"comment\">//{done:false,value:3}</span></span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(a.next())<span class=\"comment\">//{done:true}</span></span><br></pre></td></tr></table></figure>\n<h6 id=\"可迭代“对象”\"><a href=\"#可迭代“对象”\" class=\"headerlink\" title=\"可迭代“对象”\"></a>可迭代“对象”</h6><ul>\n<li>关于可迭代“对象”,我们需要再对象上实现@@iterator方法,也就是[Symbol.iterator],返回一个自定义的迭代方法,以表明这个对象是可以迭代的。有些JS内置的对象就是可迭代的,比如String,Array。</li>\n</ul>\n<blockquote>\n<p>自带的迭代事例</p>\n</blockquote>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"keyword\">let</span> str=<span class=\"string\">\"我是欢乐的迭代器\"</span></span><br><span class=\"line\"><span class=\"keyword\">let</span> b=str[<span class=\"built_in\">Symbol</span>.iterator]()</span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(b.next())<span class=\"comment\">//{value: \"I\", done: false}</span></span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(b.next())<span class=\"comment\">//{value: \"M\", done: false}</span></span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(b.next())<span class=\"comment\">//{value: \"B\", done: false}</span></span><br></pre></td></tr></table></figure>\n<blockquote>\n<p>自定义迭代方法</p>\n</blockquote>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br><span class=\"line\">17</span><br><span class=\"line\">18</span><br><span class=\"line\">19</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">str[<span class=\"built_in\">Symbol</span>.iterator] = <span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\"></span>) </span>{</span><br><span class=\"line\"> <span class=\"keyword\">return</span> { <span class=\"comment\">// this is the iterator object, returning a single element, the string \"bye\"</span></span><br><span class=\"line\"> next: <span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\"></span>) </span>{</span><br><span class=\"line\"> <span class=\"keyword\">this</span>._index += <span class=\"number\">2</span></span><br><span class=\"line\"> <span class=\"keyword\">if</span> (<span class=\"keyword\">this</span>._index<str.length) {</span><br><span class=\"line\"> <span class=\"keyword\">return</span> { <span class=\"attr\">value</span>: str[<span class=\"keyword\">this</span>._index], <span class=\"attr\">done</span>: <span class=\"literal\">false</span> };</span><br><span class=\"line\"> } <span class=\"keyword\">else</span> {</span><br><span class=\"line\"> <span class=\"keyword\">return</span> { <span class=\"attr\">done</span>: <span class=\"literal\">true</span> };</span><br><span class=\"line\"> }</span><br><span class=\"line\"> },</span><br><span class=\"line\"> _index:<span class=\"number\">-2</span></span><br><span class=\"line\"> };</span><br><span class=\"line\">};</span><br><span class=\"line\"><span class=\"keyword\">let</span> c=str[<span class=\"built_in\">Symbol</span>.iterator]()</span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(c.next())<span class=\"comment\">//{value: \"I\", done: false}</span></span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(c.next())<span class=\"comment\">//{value: \"M\", done: false}</span></span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(c.next())<span class=\"comment\">//{value: \"B\", done: false}</span></span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(c.next())<span class=\"comment\">//{value: \"R\", done: false}</span></span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(c.next())<span class=\"comment\">//{done: true}</span></span><br></pre></td></tr></table></figure>\n<h5 id=\"生成器-generator\"><a href=\"#生成器-generator\" class=\"headerlink\" title=\"生成器-generator\"></a>生成器-generator</h5><hr>\n<blockquote>\n<p>生成器(generator),专门帮我们生成迭代器</p>\n</blockquote>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"function\"><span class=\"keyword\">function</span> * <span class=\"title\">g</span>(<span class=\"params\"></span>)</span>{}</span><br><span class=\"line\"><span class=\"keyword\">let</span> it= g()</span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(it.next())<span class=\"comment\">//{value: undefined, done: true}</span></span><br></pre></td></tr></table></figure>\n<blockquote>\n<p>比较老土的实现方法<br><figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">str[<span class=\"built_in\">Symbol</span>.iterator]= <span class=\"function\"><span class=\"keyword\">function</span> * (<span class=\"params\"></span>)</span>{</span><br><span class=\"line\"> <span class=\"keyword\">let</span> index=<span class=\"number\">-2</span>;</span><br><span class=\"line\"> <span class=\"keyword\">while</span>(index<<span class=\"keyword\">this</span>.length){</span><br><span class=\"line\"> index += <span class=\"number\">2</span></span><br><span class=\"line\"> <span class=\"keyword\">yield</span> <span class=\"keyword\">this</span>[index]</span><br><span class=\"line\"> }</span><br><span class=\"line\">}</span><br><span class=\"line\"><span class=\"keyword\">let</span> kk=str[<span class=\"built_in\">Symbol</span>.iterator]()</span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(kk.next())<span class=\"comment\">//{value: \"I\", done: false}</span></span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(kk.next())<span class=\"comment\">//{value: \"M\", done: false}</span></span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(kk.next())<span class=\"comment\">//{value: \"B\", done: false}</span></span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(kk.next())<span class=\"comment\">//{value: \"R\", done: false}</span></span><br></pre></td></tr></table></figure></p>\n</blockquote>\n<blockquote>\n<p>生成器到底干了什么?</p>\n<ul>\n<li>yield 的返回值是啥?</li>\n<li>执行顺序是什么样的?</li>\n</ul>\n</blockquote>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"function\"><span class=\"keyword\">function</span> * <span class=\"title\">gy</span>(<span class=\"params\"></span>)</span>{</span><br><span class=\"line\"> <span class=\"built_in\">console</span>.log(<span class=\"string\">\"zero\"</span>)</span><br><span class=\"line\"> <span class=\"keyword\">let</span> fisrt=<span class=\"keyword\">yield</span> <span class=\"string\">\"first\"</span></span><br><span class=\"line\"> <span class=\"built_in\">console</span>.log(<span class=\"string\">\"fisrt\"</span>,fisrt)</span><br><span class=\"line\"> <span class=\"keyword\">let</span> second=<span class=\"keyword\">yield</span> <span class=\"string\">\"first\"</span></span><br><span class=\"line\"> <span class=\"built_in\">console</span>.log(<span class=\"string\">\"second\"</span>,second)</span><br><span class=\"line\">}</span><br><span class=\"line\"><span class=\"keyword\">let</span> ity= gy()</span><br></pre></td></tr></table></figure>\n<blockquote>\n<p>run code<br><figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">> ity.next() <span class=\"comment\"># zero</span></span><br><span class=\"line\">> ity.next() <span class=\"comment\"># first undefined</span></span><br><span class=\"line\">> ity.next(<span class=\"string\">\"third\"</span>) <span class=\"comment\"># second third</span></span><br></pre></td></tr></table></figure></p>\n</blockquote>\n<blockquote>\n<p>由此可见next每次都停止在yield就不再继续执行。yield每次返回的都是当前的ity.next(value)和value的值</p>\n</blockquote>\n<h6 id=\"aysnc-await\"><a href=\"#aysnc-await\" class=\"headerlink\" title=\"aysnc/await\"></a>aysnc/await</h6><blockquote>\n<p>对于Promise这个对象的迭代器,我们该怎么处理。也就是每个迭代器都是异步的</p>\n</blockquote>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br><span class=\"line\">17</span><br><span class=\"line\">18</span><br><span class=\"line\">19</span><br><span class=\"line\">20</span><br><span class=\"line\">21</span><br><span class=\"line\">22</span><br><span class=\"line\">23</span><br><span class=\"line\">24</span><br><span class=\"line\">25</span><br><span class=\"line\">26</span><br><span class=\"line\">27</span><br><span class=\"line\">28</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"function\"><span class=\"keyword\">function</span> <span class=\"title\">setTime</span>(<span class=\"params\">value,id</span>)</span>{</span><br><span class=\"line\"> <span class=\"keyword\">return</span> <span class=\"keyword\">new</span> <span class=\"built_in\">Promise</span>(<span class=\"function\">(<span class=\"params\">r,j</span>)=></span>setTimeout(<span class=\"function\"><span class=\"params\">()</span> =></span> {</span><br><span class=\"line\"> <span class=\"built_in\">console</span>.log(value)</span><br><span class=\"line\"> r(id)</span><br><span class=\"line\"> }, <span class=\"number\">10</span>))</span><br><span class=\"line\">}</span><br><span class=\"line\"><span class=\"function\"><span class=\"keyword\">function</span> *<span class=\"title\">a</span>(<span class=\"params\"></span>)</span>{</span><br><span class=\"line\"> <span class=\"keyword\">let</span> r1 = <span class=\"keyword\">yield</span> setTime(<span class=\"string\">\"first\"</span>,<span class=\"number\">1</span>)</span><br><span class=\"line\"> <span class=\"built_in\">console</span>.log(r1)</span><br><span class=\"line\"> <span class=\"keyword\">let</span> r2 =<span class=\"keyword\">yield</span> setTime(<span class=\"string\">\"second\"</span>,<span class=\"number\">2</span>)</span><br><span class=\"line\"> <span class=\"built_in\">console</span>.log(r2)</span><br><span class=\"line\"> <span class=\"keyword\">let</span> r3 =<span class=\"keyword\">yield</span> setTime(<span class=\"string\">\"third\"</span>,<span class=\"number\">3</span>)</span><br><span class=\"line\"> <span class=\"built_in\">console</span>.log(r3)</span><br><span class=\"line\">}</span><br><span class=\"line\"><span class=\"keyword\">let</span> k=a();</span><br><span class=\"line\"><span class=\"keyword\">new</span> <span class=\"built_in\">Promise</span>(<span class=\"function\">(<span class=\"params\">resolve,reject</span>)=></span>{</span><br><span class=\"line\"> <span class=\"function\"><span class=\"keyword\">function</span> <span class=\"title\">next</span>(<span class=\"params\">data</span>)</span>{</span><br><span class=\"line\"> <span class=\"keyword\">let</span> {value,done}=k.next(data)</span><br><span class=\"line\"> <span class=\"comment\">//k.next()返回一个promise,因此可以then</span></span><br><span class=\"line\"> <span class=\"keyword\">if</span>(!done){</span><br><span class=\"line\"> value.then(<span class=\"function\">(<span class=\"params\">data</span>)=></span>{</span><br><span class=\"line\"> <span class=\"built_in\">console</span>.log(data)</span><br><span class=\"line\"> next(data)</span><br><span class=\"line\"> })</span><br><span class=\"line\"> }</span><br><span class=\"line\"> }</span><br><span class=\"line\"> next();</span><br><span class=\"line\">})</span><br></pre></td></tr></table></figure>\n<blockquote>\n<p>这个时候aysnc/await就可以出场了。只需要把*/yield无缝改成aysnc/await即可。</p>\n</blockquote>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"keyword\">async</span> <span class=\"function\"><span class=\"keyword\">function</span> <span class=\"title\">a</span>(<span class=\"params\"></span>) </span>{</span><br><span class=\"line\"> <span class=\"keyword\">let</span> r1 = <span class=\"keyword\">await</span> setTime(<span class=\"string\">\"first\"</span>,<span class=\"number\">1</span>)</span><br><span class=\"line\"> <span class=\"built_in\">console</span>.log(r1)</span><br><span class=\"line\"> <span class=\"keyword\">let</span> r2 = <span class=\"keyword\">await</span> setTime(<span class=\"string\">\"second\"</span>,<span class=\"number\">2</span>)</span><br><span class=\"line\"> <span class=\"built_in\">console</span>.log(r2)</span><br><span class=\"line\"> <span class=\"keyword\">let</span> r3 = <span class=\"keyword\">await</span> setTime(<span class=\"string\">\"third\"</span>,<span class=\"number\">3</span>)</span><br><span class=\"line\"> <span class=\"built_in\">console</span>.log(r3)</span><br><span class=\"line\">}</span><br><span class=\"line\">a()</span><br></pre></td></tr></table></figure>","site":{"data":{}},"excerpt":"","more":"<blockquote>\n<p>好久没写blog了,重新开始,今天来聊聊generator,promise的关系</p>\n</blockquote>\n<p>这次主要讲讲(iterator)和生成器*/yield之间的联系和各自的用法,以及生成器的高配版本aysnc/await的使用。</p>\n<p>栏目:</p>\n<ul>\n<li>迭代器(iterator)</li>\n<li>生成器 */yield</li>\n<li>异步版生成器 aysnc/await</li>\n</ul>\n<h5 id=\"迭代器(iterator)\"><a href=\"#迭代器(iterator)\" class=\"headerlink\" title=\"迭代器(iterator)\"></a>迭代器(iterator)</h5><hr>\n<pre><code>首先迭代器这个词是什么意思呢?每一次“过程”的重复,称之为迭代。不过迭代是会保留结果的,也就说每次都是以上一次迭代的结果为基准,开始下一次的迭代。举个例子,迭代这个词经常出现在产品开发之中,每个周期都会有产品的迭代开发,但是不可能每次都是从零开始做产品,肯定是基于上一版本的产品进行开发,也就是进行迭代。\n</code></pre><p>从中我们可以整理出关于迭代关键点:</p>\n<ul>\n<li>过程是重复的</li>\n<li>返回上一次的迭代结果</li>\n</ul>\n<p>那么javascript中的“迭代器”是个怎样的概念呢?</p>\n<pre><code>javascript中的迭代器,就是一个数组对象,不断地调用next重复获取过程,然后每次都返回一个结果。等到没有东西可返回了,就终止。因此next的返回对象有两个属性done和value。done表示是否结束了,value表示当前迭代的结果。当done为true的时候,表示迭代已结束,这时候是没有返回结果的也就是没有value这个属性。\n</code></pre><ul>\n<li>关于迭代器,就是我们上面讨论的next方法,返回done和value(done:true时可以省略)两个参数。</li>\n</ul>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"> <span class=\"function\"><span class=\"keyword\">function</span> <span class=\"title\">iteratorFunc</span>(<span class=\"params\"></span>)</span>{</span><br><span class=\"line\"> <span class=\"keyword\">let</span> arr=[...arguments]</span><br><span class=\"line\"> <span class=\"keyword\">let</span> nIndex=<span class=\"number\">0</span></span><br><span class=\"line\"> <span class=\"keyword\">return</span> {</span><br><span class=\"line\"> next:<span class=\"function\"><span class=\"params\">()</span>=></span>{</span><br><span class=\"line\"> <span class=\"keyword\">return</span> nIndex<arr.length?</span><br><span class=\"line\"> {<span class=\"attr\">value</span>:arr[nIndex++],<span class=\"attr\">done</span>:<span class=\"literal\">false</span>}:{<span class=\"attr\">done</span>:<span class=\"literal\">true</span>}</span><br><span class=\"line\"> }</span><br><span class=\"line\"> }</span><br><span class=\"line\">}</span><br><span class=\"line\"><span class=\"keyword\">let</span> a=iteratorFunc(<span class=\"number\">1</span>,<span class=\"number\">2</span>,<span class=\"number\">3</span>)</span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(a.next())<span class=\"comment\">//{done:false,value:1}</span></span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(a.next())<span class=\"comment\">//{done:false,value:2}</span></span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(a.next())<span class=\"comment\">//{done:false,value:3}</span></span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(a.next())<span class=\"comment\">//{done:true}</span></span><br></pre></td></tr></table></figure>\n<h6 id=\"可迭代“对象”\"><a href=\"#可迭代“对象”\" class=\"headerlink\" title=\"可迭代“对象”\"></a>可迭代“对象”</h6><ul>\n<li>关于可迭代“对象”,我们需要再对象上实现@@iterator方法,也就是[Symbol.iterator],返回一个自定义的迭代方法,以表明这个对象是可以迭代的。有些JS内置的对象就是可迭代的,比如String,Array。</li>\n</ul>\n<blockquote>\n<p>自带的迭代事例</p>\n</blockquote>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"keyword\">let</span> str=<span class=\"string\">\"我是欢乐的迭代器\"</span></span><br><span class=\"line\"><span class=\"keyword\">let</span> b=str[<span class=\"built_in\">Symbol</span>.iterator]()</span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(b.next())<span class=\"comment\">//{value: \"I\", done: false}</span></span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(b.next())<span class=\"comment\">//{value: \"M\", done: false}</span></span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(b.next())<span class=\"comment\">//{value: \"B\", done: false}</span></span><br></pre></td></tr></table></figure>\n<blockquote>\n<p>自定义迭代方法</p>\n</blockquote>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br><span class=\"line\">17</span><br><span class=\"line\">18</span><br><span class=\"line\">19</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">str[<span class=\"built_in\">Symbol</span>.iterator] = <span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\"></span>) </span>{</span><br><span class=\"line\"> <span class=\"keyword\">return</span> { <span class=\"comment\">// this is the iterator object, returning a single element, the string \"bye\"</span></span><br><span class=\"line\"> next: <span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\"></span>) </span>{</span><br><span class=\"line\"> <span class=\"keyword\">this</span>._index += <span class=\"number\">2</span></span><br><span class=\"line\"> <span class=\"keyword\">if</span> (<span class=\"keyword\">this</span>._index<str.length) {</span><br><span class=\"line\"> <span class=\"keyword\">return</span> { <span class=\"attr\">value</span>: str[<span class=\"keyword\">this</span>._index], <span class=\"attr\">done</span>: <span class=\"literal\">false</span> };</span><br><span class=\"line\"> } <span class=\"keyword\">else</span> {</span><br><span class=\"line\"> <span class=\"keyword\">return</span> { <span class=\"attr\">done</span>: <span class=\"literal\">true</span> };</span><br><span class=\"line\"> }</span><br><span class=\"line\"> },</span><br><span class=\"line\"> _index:<span class=\"number\">-2</span></span><br><span class=\"line\"> };</span><br><span class=\"line\">};</span><br><span class=\"line\"><span class=\"keyword\">let</span> c=str[<span class=\"built_in\">Symbol</span>.iterator]()</span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(c.next())<span class=\"comment\">//{value: \"I\", done: false}</span></span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(c.next())<span class=\"comment\">//{value: \"M\", done: false}</span></span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(c.next())<span class=\"comment\">//{value: \"B\", done: false}</span></span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(c.next())<span class=\"comment\">//{value: \"R\", done: false}</span></span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(c.next())<span class=\"comment\">//{done: true}</span></span><br></pre></td></tr></table></figure>\n<h5 id=\"生成器-generator\"><a href=\"#生成器-generator\" class=\"headerlink\" title=\"生成器-generator\"></a>生成器-generator</h5><hr>\n<blockquote>\n<p>生成器(generator),专门帮我们生成迭代器</p>\n</blockquote>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"function\"><span class=\"keyword\">function</span> * <span class=\"title\">g</span>(<span class=\"params\"></span>)</span>{}</span><br><span class=\"line\"><span class=\"keyword\">let</span> it= g()</span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(it.next())<span class=\"comment\">//{value: undefined, done: true}</span></span><br></pre></td></tr></table></figure>\n<blockquote>\n<p>比较老土的实现方法<br><figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">str[<span class=\"built_in\">Symbol</span>.iterator]= <span class=\"function\"><span class=\"keyword\">function</span> * (<span class=\"params\"></span>)</span>{</span><br><span class=\"line\"> <span class=\"keyword\">let</span> index=<span class=\"number\">-2</span>;</span><br><span class=\"line\"> <span class=\"keyword\">while</span>(index<<span class=\"keyword\">this</span>.length){</span><br><span class=\"line\"> index += <span class=\"number\">2</span></span><br><span class=\"line\"> <span class=\"keyword\">yield</span> <span class=\"keyword\">this</span>[index]</span><br><span class=\"line\"> }</span><br><span class=\"line\">}</span><br><span class=\"line\"><span class=\"keyword\">let</span> kk=str[<span class=\"built_in\">Symbol</span>.iterator]()</span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(kk.next())<span class=\"comment\">//{value: \"I\", done: false}</span></span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(kk.next())<span class=\"comment\">//{value: \"M\", done: false}</span></span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(kk.next())<span class=\"comment\">//{value: \"B\", done: false}</span></span><br><span class=\"line\"><span class=\"built_in\">console</span>.log(kk.next())<span class=\"comment\">//{value: \"R\", done: false}</span></span><br></pre></td></tr></table></figure></p>\n</blockquote>\n<blockquote>\n<p>生成器到底干了什么?</p>\n<ul>\n<li>yield 的返回值是啥?</li>\n<li>执行顺序是什么样的?</li>\n</ul>\n</blockquote>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"function\"><span class=\"keyword\">function</span> * <span class=\"title\">gy</span>(<span class=\"params\"></span>)</span>{</span><br><span class=\"line\"> <span class=\"built_in\">console</span>.log(<span class=\"string\">\"zero\"</span>)</span><br><span class=\"line\"> <span class=\"keyword\">let</span> fisrt=<span class=\"keyword\">yield</span> <span class=\"string\">\"first\"</span></span><br><span class=\"line\"> <span class=\"built_in\">console</span>.log(<span class=\"string\">\"fisrt\"</span>,fisrt)</span><br><span class=\"line\"> <span class=\"keyword\">let</span> second=<span class=\"keyword\">yield</span> <span class=\"string\">\"first\"</span></span><br><span class=\"line\"> <span class=\"built_in\">console</span>.log(<span class=\"string\">\"second\"</span>,second)</span><br><span class=\"line\">}</span><br><span class=\"line\"><span class=\"keyword\">let</span> ity= gy()</span><br></pre></td></tr></table></figure>\n<blockquote>\n<p>run code<br><figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">> ity.next() <span class=\"comment\"># zero</span></span><br><span class=\"line\">> ity.next() <span class=\"comment\"># first undefined</span></span><br><span class=\"line\">> ity.next(<span class=\"string\">\"third\"</span>) <span class=\"comment\"># second third</span></span><br></pre></td></tr></table></figure></p>\n</blockquote>\n<blockquote>\n<p>由此可见next每次都停止在yield就不再继续执行。yield每次返回的都是当前的ity.next(value)和value的值</p>\n</blockquote>\n<h6 id=\"aysnc-await\"><a href=\"#aysnc-await\" class=\"headerlink\" title=\"aysnc/await\"></a>aysnc/await</h6><blockquote>\n<p>对于Promise这个对象的迭代器,我们该怎么处理。也就是每个迭代器都是异步的</p>\n</blockquote>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br><span class=\"line\">17</span><br><span class=\"line\">18</span><br><span class=\"line\">19</span><br><span class=\"line\">20</span><br><span class=\"line\">21</span><br><span class=\"line\">22</span><br><span class=\"line\">23</span><br><span class=\"line\">24</span><br><span class=\"line\">25</span><br><span class=\"line\">26</span><br><span class=\"line\">27</span><br><span class=\"line\">28</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"function\"><span class=\"keyword\">function</span> <span class=\"title\">setTime</span>(<span class=\"params\">value,id</span>)</span>{</span><br><span class=\"line\"> <span class=\"keyword\">return</span> <span class=\"keyword\">new</span> <span class=\"built_in\">Promise</span>(<span class=\"function\">(<span class=\"params\">r,j</span>)=></span>setTimeout(<span class=\"function\"><span class=\"params\">()</span> =></span> {</span><br><span class=\"line\"> <span class=\"built_in\">console</span>.log(value)</span><br><span class=\"line\"> r(id)</span><br><span class=\"line\"> }, <span class=\"number\">10</span>))</span><br><span class=\"line\">}</span><br><span class=\"line\"><span class=\"function\"><span class=\"keyword\">function</span> *<span class=\"title\">a</span>(<span class=\"params\"></span>)</span>{</span><br><span class=\"line\"> <span class=\"keyword\">let</span> r1 = <span class=\"keyword\">yield</span> setTime(<span class=\"string\">\"first\"</span>,<span class=\"number\">1</span>)</span><br><span class=\"line\"> <span class=\"built_in\">console</span>.log(r1)</span><br><span class=\"line\"> <span class=\"keyword\">let</span> r2 =<span class=\"keyword\">yield</span> setTime(<span class=\"string\">\"second\"</span>,<span class=\"number\">2</span>)</span><br><span class=\"line\"> <span class=\"built_in\">console</span>.log(r2)</span><br><span class=\"line\"> <span class=\"keyword\">let</span> r3 =<span class=\"keyword\">yield</span> setTime(<span class=\"string\">\"third\"</span>,<span class=\"number\">3</span>)</span><br><span class=\"line\"> <span class=\"built_in\">console</span>.log(r3)</span><br><span class=\"line\">}</span><br><span class=\"line\"><span class=\"keyword\">let</span> k=a();</span><br><span class=\"line\"><span class=\"keyword\">new</span> <span class=\"built_in\">Promise</span>(<span class=\"function\">(<span class=\"params\">resolve,reject</span>)=></span>{</span><br><span class=\"line\"> <span class=\"function\"><span class=\"keyword\">function</span> <span class=\"title\">next</span>(<span class=\"params\">data</span>)</span>{</span><br><span class=\"line\"> <span class=\"keyword\">let</span> {value,done}=k.next(data)</span><br><span class=\"line\"> <span class=\"comment\">//k.next()返回一个promise,因此可以then</span></span><br><span class=\"line\"> <span class=\"keyword\">if</span>(!done){</span><br><span class=\"line\"> value.then(<span class=\"function\">(<span class=\"params\">data</span>)=></span>{</span><br><span class=\"line\"> <span class=\"built_in\">console</span>.log(data)</span><br><span class=\"line\"> next(data)</span><br><span class=\"line\"> })</span><br><span class=\"line\"> }</span><br><span class=\"line\"> }</span><br><span class=\"line\"> next();</span><br><span class=\"line\">})</span><br></pre></td></tr></table></figure>\n<blockquote>\n<p>这个时候aysnc/await就可以出场了。只需要把*/yield无缝改成aysnc/await即可。</p>\n</blockquote>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"keyword\">async</span> <span class=\"function\"><span class=\"keyword\">function</span> <span class=\"title\">a</span>(<span class=\"params\"></span>) </span>{</span><br><span class=\"line\"> <span class=\"keyword\">let</span> r1 = <span class=\"keyword\">await</span> setTime(<span class=\"string\">\"first\"</span>,<span class=\"number\">1</span>)</span><br><span class=\"line\"> <span class=\"built_in\">console</span>.log(r1)</span><br><span class=\"line\"> <span class=\"keyword\">let</span> r2 = <span class=\"keyword\">await</span> setTime(<span class=\"string\">\"second\"</span>,<span class=\"number\">2</span>)</span><br><span class=\"line\"> <span class=\"built_in\">console</span>.log(r2)</span><br><span class=\"line\"> <span class=\"keyword\">let</span> r3 = <span class=\"keyword\">await</span> setTime(<span class=\"string\">\"third\"</span>,<span class=\"number\">3</span>)</span><br><span class=\"line\"> <span class=\"built_in\">console</span>.log(r3)</span><br><span class=\"line\">}</span><br><span class=\"line\">a()</span><br></pre></td></tr></table></figure>"},{"title":"postMessage+window.name实现iframe跨域通信jquery兼容","author":"brian","date":"2017-04-12T11:03:32.000Z","_content":"在标准的浏览器上面可以用 postMessage 进行通信,那么在不支持这种方法的低版本 IE 上面就不行了,低版本的 IE 可以使用 window 对象的 name 属性来进行跨域的数据通信。\n\nwindow.name 在低版本 IE 下面有跨域可以在 iframe 之间互相读取而不受限,所以在低版本 IE 下就能通过 互相重写,读取而进行数据的相互交换,但有个问题就是只能是字符串类型的,可以通过传输 JSON 对象数据转化来的字符串。\n\n```js\n(function() {\n var util = {\n mix: $.extend,\n stringify: JSON.stringify,\n parse: JSON.parse\n }; // 基础的一些方法\n var usePM = ( typeof window.postMessage !== 'undefined'); // 是否支持 postMessage\n var PM = function( win ) {\n this._win = win;\n this._event = function(){};\n this._initialize();\n };\n util.mix( PM.prototype, {\n _initialize: function() { // 初始化\n var me = this;\n if ( usePM ) {\n if( window.addEventListener ) {\n window.addEventListener('message', function(e){\n me._event( util.parse(e.data));\n });\n } else {\n window.attachEvent('onmessage', function(e){\n me._event( util.parse(e.data));\n });\n }\n } else {\n var lastName = window.name;\n setInterval( function() {\n if( window.name != lastName && window.name != '' ) {\n lastName = window.name;\n me._event( util.parse(lastName));\n }\n }, 50);\n }\n },\n onmessage: function(callback) { // 添加 onmessage 方法\n this._event = callback;\n },\n send: function( data, origin ) { // send 方法\n var wl = window.location;\n var sendOrigin = wl.href.substr( 0, wl.href.indexOf( wl.pathname ) + 1 );\n var sendData = {\n data: data,\n ts: (+(new Date)).toString(10),\n origin: sendOrigin\n }\n\n if( usePM ) {\n this._win.postMessage( util.stringify(sendData), origin || '*' );\n } else {\n this._win.name = util.stringify(sendData);\n }\n }\n });\n window.XPM = PM;\n})();\n```\n\n#### 使用方法\n```js\nvar f = window.parent;\nvar PM = new XPM(f);\nPM.onmessage(function(e){\n alert(e.data);\n console.log(e);\n});\n$('button').bind('click',function(){\n var text = \"this is a message!\";\n PM.send(text);\n});\n```","source":"_posts/postMessage-window-name实现iframe跨域通信jquery兼容.md","raw":"title: postMessage+window.name实现iframe跨域通信jquery兼容\nauthor: brian\ntags:\n - javascript\ncategories: []\ndate: 2017-04-12 19:03:32\n---\n在标准的浏览器上面可以用 postMessage 进行通信,那么在不支持这种方法的低版本 IE 上面就不行了,低版本的 IE 可以使用 window 对象的 name 属性来进行跨域的数据通信。\n\nwindow.name 在低版本 IE 下面有跨域可以在 iframe 之间互相读取而不受限,所以在低版本 IE 下就能通过 互相重写,读取而进行数据的相互交换,但有个问题就是只能是字符串类型的,可以通过传输 JSON 对象数据转化来的字符串。\n\n```js\n(function() {\n var util = {\n mix: $.extend,\n stringify: JSON.stringify,\n parse: JSON.parse\n }; // 基础的一些方法\n var usePM = ( typeof window.postMessage !== 'undefined'); // 是否支持 postMessage\n var PM = function( win ) {\n this._win = win;\n this._event = function(){};\n this._initialize();\n };\n util.mix( PM.prototype, {\n _initialize: function() { // 初始化\n var me = this;\n if ( usePM ) {\n if( window.addEventListener ) {\n window.addEventListener('message', function(e){\n me._event( util.parse(e.data));\n });\n } else {\n window.attachEvent('onmessage', function(e){\n me._event( util.parse(e.data));\n });\n }\n } else {\n var lastName = window.name;\n setInterval( function() {\n if( window.name != lastName && window.name != '' ) {\n lastName = window.name;\n me._event( util.parse(lastName));\n }\n }, 50);\n }\n },\n onmessage: function(callback) { // 添加 onmessage 方法\n this._event = callback;\n },\n send: function( data, origin ) { // send 方法\n var wl = window.location;\n var sendOrigin = wl.href.substr( 0, wl.href.indexOf( wl.pathname ) + 1 );\n var sendData = {\n data: data,\n ts: (+(new Date)).toString(10),\n origin: sendOrigin\n }\n\n if( usePM ) {\n this._win.postMessage( util.stringify(sendData), origin || '*' );\n } else {\n this._win.name = util.stringify(sendData);\n }\n }\n });\n window.XPM = PM;\n})();\n```\n\n#### 使用方法\n```js\nvar f = window.parent;\nvar PM = new XPM(f);\nPM.onmessage(function(e){\n alert(e.data);\n console.log(e);\n});\n$('button').bind('click',function(){\n var text = \"this is a message!\";\n PM.send(text);\n});\n```","slug":"postMessage-window-name实现iframe跨域通信jquery兼容","published":1,"updated":"2018-11-17T14:38:22.354Z","_id":"cjolk1spf000k0evoxdp3sndm","comments":1,"layout":"post","photos":[],"link":"","content":"<p>在标准的浏览器上面可以用 postMessage 进行通信,那么在不支持这种方法的低版本 IE 上面就不行了,低版本的 IE 可以使用 window 对象的 name 属性来进行跨域的数据通信。</p>\n<p>window.name 在低版本 IE 下面有跨域可以在 iframe 之间互相读取而不受限,所以在低版本 IE 下就能通过 互相重写,读取而进行数据的相互交换,但有个问题就是只能是字符串类型的,可以通过传输 JSON 对象数据转化来的字符串。</p>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br><span class=\"line\">17</span><br><span class=\"line\">18</span><br><span class=\"line\">19</span><br><span class=\"line\">20</span><br><span class=\"line\">21</span><br><span class=\"line\">22</span><br><span class=\"line\">23</span><br><span class=\"line\">24</span><br><span class=\"line\">25</span><br><span class=\"line\">26</span><br><span class=\"line\">27</span><br><span class=\"line\">28</span><br><span class=\"line\">29</span><br><span class=\"line\">30</span><br><span class=\"line\">31</span><br><span class=\"line\">32</span><br><span class=\"line\">33</span><br><span class=\"line\">34</span><br><span class=\"line\">35</span><br><span class=\"line\">36</span><br><span class=\"line\">37</span><br><span class=\"line\">38</span><br><span class=\"line\">39</span><br><span class=\"line\">40</span><br><span class=\"line\">41</span><br><span class=\"line\">42</span><br><span class=\"line\">43</span><br><span class=\"line\">44</span><br><span class=\"line\">45</span><br><span class=\"line\">46</span><br><span class=\"line\">47</span><br><span class=\"line\">48</span><br><span class=\"line\">49</span><br><span class=\"line\">50</span><br><span class=\"line\">51</span><br><span class=\"line\">52</span><br><span class=\"line\">53</span><br><span class=\"line\">54</span><br><span class=\"line\">55</span><br><span class=\"line\">56</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">(<span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\"></span>) </span>{</span><br><span class=\"line\"> <span class=\"keyword\">var</span> util = {</span><br><span class=\"line\"> mix: $.extend,</span><br><span class=\"line\"> stringify: <span class=\"built_in\">JSON</span>.stringify,</span><br><span class=\"line\"> parse: <span class=\"built_in\">JSON</span>.parse</span><br><span class=\"line\"> }; <span class=\"comment\">// 基础的一些方法</span></span><br><span class=\"line\"> <span class=\"keyword\">var</span> usePM = ( <span class=\"keyword\">typeof</span> <span class=\"built_in\">window</span>.postMessage !== <span class=\"string\">'undefined'</span>); <span class=\"comment\">// 是否支持 postMessage</span></span><br><span class=\"line\"> <span class=\"keyword\">var</span> PM = <span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\"> win </span>) </span>{</span><br><span class=\"line\"> <span class=\"keyword\">this</span>._win = win;</span><br><span class=\"line\"> <span class=\"keyword\">this</span>._event = <span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\"></span>)</span>{};</span><br><span class=\"line\"> <span class=\"keyword\">this</span>._initialize();</span><br><span class=\"line\"> };</span><br><span class=\"line\"> util.mix( PM.prototype, {</span><br><span class=\"line\"> _initialize: <span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\"></span>) </span>{ <span class=\"comment\">// 初始化</span></span><br><span class=\"line\"> <span class=\"keyword\">var</span> me = <span class=\"keyword\">this</span>;</span><br><span class=\"line\"> <span class=\"keyword\">if</span> ( usePM ) {</span><br><span class=\"line\"> <span class=\"keyword\">if</span>( <span class=\"built_in\">window</span>.addEventListener ) {</span><br><span class=\"line\"> <span class=\"built_in\">window</span>.addEventListener(<span class=\"string\">'message'</span>, <span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\">e</span>)</span>{</span><br><span class=\"line\"> me._event( util.parse(e.data));</span><br><span class=\"line\"> });</span><br><span class=\"line\"> } <span class=\"keyword\">else</span> {</span><br><span class=\"line\"> <span class=\"built_in\">window</span>.attachEvent(<span class=\"string\">'onmessage'</span>, <span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\">e</span>)</span>{</span><br><span class=\"line\"> me._event( util.parse(e.data));</span><br><span class=\"line\"> });</span><br><span class=\"line\"> }</span><br><span class=\"line\"> } <span class=\"keyword\">else</span> {</span><br><span class=\"line\"> <span class=\"keyword\">var</span> lastName = <span class=\"built_in\">window</span>.name;</span><br><span class=\"line\"> setInterval( <span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\"></span>) </span>{</span><br><span class=\"line\"> <span class=\"keyword\">if</span>( <span class=\"built_in\">window</span>.name != lastName && <span class=\"built_in\">window</span>.name != <span class=\"string\">''</span> ) {</span><br><span class=\"line\"> lastName = <span class=\"built_in\">window</span>.name;</span><br><span class=\"line\"> me._event( util.parse(lastName));</span><br><span class=\"line\"> }</span><br><span class=\"line\"> }, <span class=\"number\">50</span>);</span><br><span class=\"line\"> }</span><br><span class=\"line\"> },</span><br><span class=\"line\"> onmessage: <span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\">callback</span>) </span>{ <span class=\"comment\">// 添加 onmessage 方法</span></span><br><span class=\"line\"> <span class=\"keyword\">this</span>._event = callback;</span><br><span class=\"line\"> },</span><br><span class=\"line\"> send: <span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\"> data, origin </span>) </span>{ <span class=\"comment\">// send 方法</span></span><br><span class=\"line\"> <span class=\"keyword\">var</span> wl = <span class=\"built_in\">window</span>.location;</span><br><span class=\"line\"> <span class=\"keyword\">var</span> sendOrigin = wl.href.substr( <span class=\"number\">0</span>, wl.href.indexOf( wl.pathname ) + <span class=\"number\">1</span> );</span><br><span class=\"line\"> <span class=\"keyword\">var</span> sendData = {</span><br><span class=\"line\"> data: data,</span><br><span class=\"line\"> ts: (+(<span class=\"keyword\">new</span> <span class=\"built_in\">Date</span>)).toString(<span class=\"number\">10</span>),</span><br><span class=\"line\"> origin: sendOrigin</span><br><span class=\"line\"> }</span><br><span class=\"line\"></span><br><span class=\"line\"> <span class=\"keyword\">if</span>( usePM ) {</span><br><span class=\"line\"> <span class=\"keyword\">this</span>._win.postMessage( util.stringify(sendData), origin || <span class=\"string\">'*'</span> );</span><br><span class=\"line\"> } <span class=\"keyword\">else</span> {</span><br><span class=\"line\"> <span class=\"keyword\">this</span>._win.name = util.stringify(sendData);</span><br><span class=\"line\"> }</span><br><span class=\"line\"> }</span><br><span class=\"line\"> });</span><br><span class=\"line\"> <span class=\"built_in\">window</span>.XPM = PM;</span><br><span class=\"line\">})();</span><br></pre></td></tr></table></figure>\n<h4 id=\"使用方法\"><a href=\"#使用方法\" class=\"headerlink\" title=\"使用方法\"></a>使用方法</h4><figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"keyword\">var</span> f = <span class=\"built_in\">window</span>.parent;</span><br><span class=\"line\"><span class=\"keyword\">var</span> PM = <span class=\"keyword\">new</span> XPM(f);</span><br><span class=\"line\">PM.onmessage(<span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\">e</span>)</span>{</span><br><span class=\"line\"> alert(e.data);</span><br><span class=\"line\"> <span class=\"built_in\">console</span>.log(e);</span><br><span class=\"line\">});</span><br><span class=\"line\">$(<span class=\"string\">'button'</span>).bind(<span class=\"string\">'click'</span>,<span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\"></span>)</span>{</span><br><span class=\"line\"> <span class=\"keyword\">var</span> text = <span class=\"string\">\"this is a message!\"</span>;</span><br><span class=\"line\"> PM.send(text);</span><br><span class=\"line\">});</span><br></pre></td></tr></table></figure>","site":{"data":{}},"excerpt":"","more":"<p>在标准的浏览器上面可以用 postMessage 进行通信,那么在不支持这种方法的低版本 IE 上面就不行了,低版本的 IE 可以使用 window 对象的 name 属性来进行跨域的数据通信。</p>\n<p>window.name 在低版本 IE 下面有跨域可以在 iframe 之间互相读取而不受限,所以在低版本 IE 下就能通过 互相重写,读取而进行数据的相互交换,但有个问题就是只能是字符串类型的,可以通过传输 JSON 对象数据转化来的字符串。</p>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br><span class=\"line\">17</span><br><span class=\"line\">18</span><br><span class=\"line\">19</span><br><span class=\"line\">20</span><br><span class=\"line\">21</span><br><span class=\"line\">22</span><br><span class=\"line\">23</span><br><span class=\"line\">24</span><br><span class=\"line\">25</span><br><span class=\"line\">26</span><br><span class=\"line\">27</span><br><span class=\"line\">28</span><br><span class=\"line\">29</span><br><span class=\"line\">30</span><br><span class=\"line\">31</span><br><span class=\"line\">32</span><br><span class=\"line\">33</span><br><span class=\"line\">34</span><br><span class=\"line\">35</span><br><span class=\"line\">36</span><br><span class=\"line\">37</span><br><span class=\"line\">38</span><br><span class=\"line\">39</span><br><span class=\"line\">40</span><br><span class=\"line\">41</span><br><span class=\"line\">42</span><br><span class=\"line\">43</span><br><span class=\"line\">44</span><br><span class=\"line\">45</span><br><span class=\"line\">46</span><br><span class=\"line\">47</span><br><span class=\"line\">48</span><br><span class=\"line\">49</span><br><span class=\"line\">50</span><br><span class=\"line\">51</span><br><span class=\"line\">52</span><br><span class=\"line\">53</span><br><span class=\"line\">54</span><br><span class=\"line\">55</span><br><span class=\"line\">56</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">(<span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\"></span>) </span>{</span><br><span class=\"line\"> <span class=\"keyword\">var</span> util = {</span><br><span class=\"line\"> mix: $.extend,</span><br><span class=\"line\"> stringify: <span class=\"built_in\">JSON</span>.stringify,</span><br><span class=\"line\"> parse: <span class=\"built_in\">JSON</span>.parse</span><br><span class=\"line\"> }; <span class=\"comment\">// 基础的一些方法</span></span><br><span class=\"line\"> <span class=\"keyword\">var</span> usePM = ( <span class=\"keyword\">typeof</span> <span class=\"built_in\">window</span>.postMessage !== <span class=\"string\">'undefined'</span>); <span class=\"comment\">// 是否支持 postMessage</span></span><br><span class=\"line\"> <span class=\"keyword\">var</span> PM = <span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\"> win </span>) </span>{</span><br><span class=\"line\"> <span class=\"keyword\">this</span>._win = win;</span><br><span class=\"line\"> <span class=\"keyword\">this</span>._event = <span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\"></span>)</span>{};</span><br><span class=\"line\"> <span class=\"keyword\">this</span>._initialize();</span><br><span class=\"line\"> };</span><br><span class=\"line\"> util.mix( PM.prototype, {</span><br><span class=\"line\"> _initialize: <span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\"></span>) </span>{ <span class=\"comment\">// 初始化</span></span><br><span class=\"line\"> <span class=\"keyword\">var</span> me = <span class=\"keyword\">this</span>;</span><br><span class=\"line\"> <span class=\"keyword\">if</span> ( usePM ) {</span><br><span class=\"line\"> <span class=\"keyword\">if</span>( <span class=\"built_in\">window</span>.addEventListener ) {</span><br><span class=\"line\"> <span class=\"built_in\">window</span>.addEventListener(<span class=\"string\">'message'</span>, <span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\">e</span>)</span>{</span><br><span class=\"line\"> me._event( util.parse(e.data));</span><br><span class=\"line\"> });</span><br><span class=\"line\"> } <span class=\"keyword\">else</span> {</span><br><span class=\"line\"> <span class=\"built_in\">window</span>.attachEvent(<span class=\"string\">'onmessage'</span>, <span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\">e</span>)</span>{</span><br><span class=\"line\"> me._event( util.parse(e.data));</span><br><span class=\"line\"> });</span><br><span class=\"line\"> }</span><br><span class=\"line\"> } <span class=\"keyword\">else</span> {</span><br><span class=\"line\"> <span class=\"keyword\">var</span> lastName = <span class=\"built_in\">window</span>.name;</span><br><span class=\"line\"> setInterval( <span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\"></span>) </span>{</span><br><span class=\"line\"> <span class=\"keyword\">if</span>( <span class=\"built_in\">window</span>.name != lastName && <span class=\"built_in\">window</span>.name != <span class=\"string\">''</span> ) {</span><br><span class=\"line\"> lastName = <span class=\"built_in\">window</span>.name;</span><br><span class=\"line\"> me._event( util.parse(lastName));</span><br><span class=\"line\"> }</span><br><span class=\"line\"> }, <span class=\"number\">50</span>);</span><br><span class=\"line\"> }</span><br><span class=\"line\"> },</span><br><span class=\"line\"> onmessage: <span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\">callback</span>) </span>{ <span class=\"comment\">// 添加 onmessage 方法</span></span><br><span class=\"line\"> <span class=\"keyword\">this</span>._event = callback;</span><br><span class=\"line\"> },</span><br><span class=\"line\"> send: <span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\"> data, origin </span>) </span>{ <span class=\"comment\">// send 方法</span></span><br><span class=\"line\"> <span class=\"keyword\">var</span> wl = <span class=\"built_in\">window</span>.location;</span><br><span class=\"line\"> <span class=\"keyword\">var</span> sendOrigin = wl.href.substr( <span class=\"number\">0</span>, wl.href.indexOf( wl.pathname ) + <span class=\"number\">1</span> );</span><br><span class=\"line\"> <span class=\"keyword\">var</span> sendData = {</span><br><span class=\"line\"> data: data,</span><br><span class=\"line\"> ts: (+(<span class=\"keyword\">new</span> <span class=\"built_in\">Date</span>)).toString(<span class=\"number\">10</span>),</span><br><span class=\"line\"> origin: sendOrigin</span><br><span class=\"line\"> }</span><br><span class=\"line\"></span><br><span class=\"line\"> <span class=\"keyword\">if</span>( usePM ) {</span><br><span class=\"line\"> <span class=\"keyword\">this</span>._win.postMessage( util.stringify(sendData), origin || <span class=\"string\">'*'</span> );</span><br><span class=\"line\"> } <span class=\"keyword\">else</span> {</span><br><span class=\"line\"> <span class=\"keyword\">this</span>._win.name = util.stringify(sendData);</span><br><span class=\"line\"> }</span><br><span class=\"line\"> }</span><br><span class=\"line\"> });</span><br><span class=\"line\"> <span class=\"built_in\">window</span>.XPM = PM;</span><br><span class=\"line\">})();</span><br></pre></td></tr></table></figure>\n<h4 id=\"使用方法\"><a href=\"#使用方法\" class=\"headerlink\" title=\"使用方法\"></a>使用方法</h4><figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"keyword\">var</span> f = <span class=\"built_in\">window</span>.parent;</span><br><span class=\"line\"><span class=\"keyword\">var</span> PM = <span class=\"keyword\">new</span> XPM(f);</span><br><span class=\"line\">PM.onmessage(<span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\">e</span>)</span>{</span><br><span class=\"line\"> alert(e.data);</span><br><span class=\"line\"> <span class=\"built_in\">console</span>.log(e);</span><br><span class=\"line\">});</span><br><span class=\"line\">$(<span class=\"string\">'button'</span>).bind(<span class=\"string\">'click'</span>,<span class=\"function\"><span class=\"keyword\">function</span>(<span class=\"params\"></span>)</span>{</span><br><span class=\"line\"> <span class=\"keyword\">var</span> text = <span class=\"string\">\"this is a message!\"</span>;</span><br><span class=\"line\"> PM.send(text);</span><br><span class=\"line\">});</span><br></pre></td></tr></table></figure>"},{"title":"日常笔记","author":"brian","date":"2018-11-20T07:21:50.000Z","_content":"#### NODE rabbitMQ\n\n> NODE使用rabbitMQ 方法 其一(git)[https://github.com/squaremo/amqp.node]\n\n```shell\n# install\nnpm install amqp --save\n```\n\n```js\nimport amqp from 'amqp'\nlet mq_connection = amqp.createConnection({\n host: '127.0.0.1',\n login: 'test', // 安装mq时 设置的用户\n password: 'text' // 安装mq时设置的密码\n})\nmq_connection.on('error', (e)=> {\n\tconsole.log(e) // 连接错误异常\n})\nlet qpex = null //设置订阅者对象\nlet queue_name = 'test'\nmq_connection.on('ready', ()=> {\n\tqpex = mq_connection.exchange(queue_name, {type: 'dirct', autoDelete: false}, (e)=> {\n })\n mq_connection.queue(queue_name, {durable: true, autoDelete: false}, (queue)=> {\n \tqueue.bind(queue_name, queue_name)\n })\n})\n```\n\n> NODE使用rabbitMQ 方法 其二(git)[https://github.com/postwait/node-amqp]\n\n```shell\n# install \nnpm install node-amqp\n```\n\n```js\nimport nodeAmqp from 'node-amqp'\n\nlet queue_name = 'test'\nlet Publisher = nodeAmqp.Publisher\nlet publisher = new Publisher({\n\t'host': 'amqp://test:[email protected]',\n\t'exchangeName': queue_name\n})\n```\n\n> node 参数加密 base64\n\n```js\nlet get_sign = (data)=> {\n\tlet _data = {}\n if (data) {\n \tlet _par = ''\n if (data.params) {\n \tfor(let i in data.params) {\n \t_par += `${i}=${data.params[i]}&`\n }\n let par = new Buffer(_par.substring(0, _par.length - 1))\n // or let par = new Buffer(_par.slice(0, _par.length - 1))\n par = par.toString('base64')\n _data.param_value = par // 根据后端接口参数定义 param_value 的名字\n }\n }\n}\n\n// 调用\nlet sign_data = get_sign({\n\tparams: {\n \tparams1: 'test',\n params2: 'test',\n params3: 'test',\n params4: 'test',\n params5: 'test',\n }\n})\n\n//sign_data=dGhpcyBpcyBhIGV4YW1wbGVzaWduX2RhdGE=\n```\n\n\n> nodejs global 全局变量(doc)[http://nodejs.cn/api/globals.html]\n\t\n global 全局变量\n\n\n> redis 使用方法(git)[https://github.com/NodeRedis/node_redis]\n```shell\nnpm install redis\n```\n```js\nimport redis from 'redis'\nlet client = redis.createClient({\n\thost : '127.0.0.1',\n port : 6379,\n connect_timeout: 60 * 60 * 100\n password : '***',\n db : ''\n})\n```\n\n> mysql 使用方法(git)[https://github.com/mysqljs/mysql]\n```shell\n# install\nnpm install mysql\n```\n\n```js\nimport mysql from 'mysql'\nconst connection = mysql.createPool({\n\tconnectionLimit: 10000, //连接池的最大连接数\n connectTimout : 60 * 60 * 1000, // 在初始化连接mysql服务器发生超时的时间\n acquireTimeout : 60 * 60 * 1000, //连接获取期间发生超时前的毫秒数。这与稍微不同connectTimeout,因为获取池连接并不总是涉及建立连接。(默认值:10000)\n timeout : 60 * 60 * 1000,\n host : '127.0.0.1', // 要连接的数据库主机名\n port : 3306, //要连接的端口\n user : 'root', // 进行身份验证的mysql用户名\n password : '***', //mysql密码\n databases : 'test', // 连接的数据库名称\n multipleStatements: true //is True MULTI_STATEMENTS - 客户端可以为每个查询或语句准备发送多个语句\n}).on('connection', (conn) => {\n\tconsole.log('连接成功')\n})\n\nlet test_query = ()=> {\n\tconnection.getConnection((err, conn)=> {\n \tif (err) {\n \tthrow err\n }\n let query_string = 'select c_name1, c_name2 from tab';\n conn.query(query_string, (error, results, fields) => {\n \tif (results) {\n \tconsole.log(results)\n }\n })\n })\n}\n```","source":"_drafts/日常笔记.md","raw":"title: 日常笔记\nauthor: brian\ndate: 2018-11-20 15:21:50\ntags:\n---\n#### NODE rabbitMQ\n\n> NODE使用rabbitMQ 方法 其一(git)[https://github.com/squaremo/amqp.node]\n\n```shell\n# install\nnpm install amqp --save\n```\n\n```js\nimport amqp from 'amqp'\nlet mq_connection = amqp.createConnection({\n host: '127.0.0.1',\n login: 'test', // 安装mq时 设置的用户\n password: 'text' // 安装mq时设置的密码\n})\nmq_connection.on('error', (e)=> {\n\tconsole.log(e) // 连接错误异常\n})\nlet qpex = null //设置订阅者对象\nlet queue_name = 'test'\nmq_connection.on('ready', ()=> {\n\tqpex = mq_connection.exchange(queue_name, {type: 'dirct', autoDelete: false}, (e)=> {\n })\n mq_connection.queue(queue_name, {durable: true, autoDelete: false}, (queue)=> {\n \tqueue.bind(queue_name, queue_name)\n })\n})\n```\n\n> NODE使用rabbitMQ 方法 其二(git)[https://github.com/postwait/node-amqp]\n\n```shell\n# install \nnpm install node-amqp\n```\n\n```js\nimport nodeAmqp from 'node-amqp'\n\nlet queue_name = 'test'\nlet Publisher = nodeAmqp.Publisher\nlet publisher = new Publisher({\n\t'host': 'amqp://test:[email protected]',\n\t'exchangeName': queue_name\n})\n```\n\n> node 参数加密 base64\n\n```js\nlet get_sign = (data)=> {\n\tlet _data = {}\n if (data) {\n \tlet _par = ''\n if (data.params) {\n \tfor(let i in data.params) {\n \t_par += `${i}=${data.params[i]}&`\n }\n let par = new Buffer(_par.substring(0, _par.length - 1))\n // or let par = new Buffer(_par.slice(0, _par.length - 1))\n par = par.toString('base64')\n _data.param_value = par // 根据后端接口参数定义 param_value 的名字\n }\n }\n}\n\n// 调用\nlet sign_data = get_sign({\n\tparams: {\n \tparams1: 'test',\n params2: 'test',\n params3: 'test',\n params4: 'test',\n params5: 'test',\n }\n})\n\n//sign_data=dGhpcyBpcyBhIGV4YW1wbGVzaWduX2RhdGE=\n```\n\n\n> nodejs global 全局变量(doc)[http://nodejs.cn/api/globals.html]\n\t\n global 全局变量\n\n\n> redis 使用方法(git)[https://github.com/NodeRedis/node_redis]\n```shell\nnpm install redis\n```\n```js\nimport redis from 'redis'\nlet client = redis.createClient({\n\thost : '127.0.0.1',\n port : 6379,\n connect_timeout: 60 * 60 * 100\n password : '***',\n db : ''\n})\n```\n\n> mysql 使用方法(git)[https://github.com/mysqljs/mysql]\n```shell\n# install\nnpm install mysql\n```\n\n```js\nimport mysql from 'mysql'\nconst connection = mysql.createPool({\n\tconnectionLimit: 10000, //连接池的最大连接数\n connectTimout : 60 * 60 * 1000, // 在初始化连接mysql服务器发生超时的时间\n acquireTimeout : 60 * 60 * 1000, //连接获取期间发生超时前的毫秒数。这与稍微不同connectTimeout,因为获取池连接并不总是涉及建立连接。(默认值:10000)\n timeout : 60 * 60 * 1000,\n host : '127.0.0.1', // 要连接的数据库主机名\n port : 3306, //要连接的端口\n user : 'root', // 进行身份验证的mysql用户名\n password : '***', //mysql密码\n databases : 'test', // 连接的数据库名称\n multipleStatements: true //is True MULTI_STATEMENTS - 客户端可以为每个查询或语句准备发送多个语句\n}).on('connection', (conn) => {\n\tconsole.log('连接成功')\n})\n\nlet test_query = ()=> {\n\tconnection.getConnection((err, conn)=> {\n \tif (err) {\n \tthrow err\n }\n let query_string = 'select c_name1, c_name2 from tab';\n conn.query(query_string, (error, results, fields) => {\n \tif (results) {\n \tconsole.log(results)\n }\n })\n })\n}\n```","slug":"日常笔记","published":0,"updated":"2018-11-20T08:24:38.484Z","_id":"cjopeusnb000051von2t0ti6c","comments":1,"layout":"post","photos":[],"link":"","content":"<h4 id=\"NODE-rabbitMQ\"><a href=\"#NODE-rabbitMQ\" class=\"headerlink\" title=\"NODE rabbitMQ\"></a>NODE rabbitMQ</h4><blockquote>\n<p>NODE使用rabbitMQ 方法 其一(git)[<a href=\"https://github.com/squaremo/amqp.node]\" target=\"_blank\" rel=\"noopener\">https://github.com/squaremo/amqp.node]</a></p>\n</blockquote>\n<figure class=\"highlight shell\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"meta\">#</span> install</span><br><span class=\"line\">npm install amqp --save</span><br></pre></td></tr></table></figure>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br><span class=\"line\">17</span><br><span class=\"line\">18</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"keyword\">import</span> amqp <span class=\"keyword\">from</span> <span class=\"string\">'amqp'</span></span><br><span class=\"line\"><span class=\"keyword\">let</span> mq_connection = amqp.createConnection({</span><br><span class=\"line\"> host: <span class=\"string\">'127.0.0.1'</span>,</span><br><span class=\"line\"> login: <span class=\"string\">'test'</span>, <span class=\"comment\">// 安装mq时 设置的用户</span></span><br><span class=\"line\"> password: <span class=\"string\">'text'</span> <span class=\"comment\">// 安装mq时设置的密码</span></span><br><span class=\"line\">})</span><br><span class=\"line\">mq_connection.on(<span class=\"string\">'error'</span>, (e)=> {</span><br><span class=\"line\">\t<span class=\"built_in\">console</span>.log(e) <span class=\"comment\">// 连接错误异常</span></span><br><span class=\"line\">})</span><br><span class=\"line\"><span class=\"keyword\">let</span> qpex = <span class=\"literal\">null</span> <span class=\"comment\">//设置订阅者对象</span></span><br><span class=\"line\"><span class=\"keyword\">let</span> queue_name = <span class=\"string\">'test'</span></span><br><span class=\"line\">mq_connection.on(<span class=\"string\">'ready'</span>, ()=> {</span><br><span class=\"line\">\tqpex = mq_connection.exchange(queue_name, {<span class=\"attr\">type</span>: <span class=\"string\">'dirct'</span>, <span class=\"attr\">autoDelete</span>: <span class=\"literal\">false</span>}, (e)=> {</span><br><span class=\"line\"> })</span><br><span class=\"line\"> mq_connection.queue(queue_name, {<span class=\"attr\">durable</span>: <span class=\"literal\">true</span>, <span class=\"attr\">autoDelete</span>: <span class=\"literal\">false</span>}, (queue)=> {</span><br><span class=\"line\"> \tqueue.bind(queue_name, queue_name)</span><br><span class=\"line\"> })</span><br><span class=\"line\">})</span><br></pre></td></tr></table></figure>\n<blockquote>\n<p>NODE使用rabbitMQ 方法 其二(git)[<a href=\"https://github.com/postwait/node-amqp]\" target=\"_blank\" rel=\"noopener\">https://github.com/postwait/node-amqp]</a></p>\n</blockquote>\n<figure class=\"highlight shell\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"meta\">#</span> install </span><br><span class=\"line\">npm install node-amqp</span><br></pre></td></tr></table></figure>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"keyword\">import</span> nodeAmqp <span class=\"keyword\">from</span> <span class=\"string\">'node-amqp'</span></span><br><span class=\"line\"></span><br><span class=\"line\"><span class=\"keyword\">let</span> queue_name = <span class=\"string\">'test'</span></span><br><span class=\"line\"><span class=\"keyword\">let</span> Publisher = nodeAmqp.Publisher</span><br><span class=\"line\"><span class=\"keyword\">let</span> publisher = <span class=\"keyword\">new</span> Publisher({</span><br><span class=\"line\">\t<span class=\"string\">'host'</span>: <span class=\"string\">'amqp://test:[email protected]'</span>,</span><br><span class=\"line\">\t<span class=\"string\">'exchangeName'</span>: queue_name</span><br><span class=\"line\">})</span><br></pre></td></tr></table></figure>\n<blockquote>\n<p>node 参数加密 base64</p>\n</blockquote>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br><span class=\"line\">17</span><br><span class=\"line\">18</span><br><span class=\"line\">19</span><br><span class=\"line\">20</span><br><span class=\"line\">21</span><br><span class=\"line\">22</span><br><span class=\"line\">23</span><br><span class=\"line\">24</span><br><span class=\"line\">25</span><br><span class=\"line\">26</span><br><span class=\"line\">27</span><br><span class=\"line\">28</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"keyword\">let</span> get_sign = <span class=\"function\">(<span class=\"params\">data</span>)=></span> {</span><br><span class=\"line\">\t<span class=\"keyword\">let</span> _data = {}</span><br><span class=\"line\"> <span class=\"keyword\">if</span> (data) {</span><br><span class=\"line\"> \t<span class=\"keyword\">let</span> _par = <span class=\"string\">''</span></span><br><span class=\"line\"> <span class=\"keyword\">if</span> (data.params) {</span><br><span class=\"line\"> \t<span class=\"keyword\">for</span>(<span class=\"keyword\">let</span> i <span class=\"keyword\">in</span> data.params) {</span><br><span class=\"line\"> \t_par += <span class=\"string\">`<span class=\"subst\">${i}</span>=<span class=\"subst\">${data.params[i]}</span>&`</span></span><br><span class=\"line\"> }</span><br><span class=\"line\"> <span class=\"keyword\">let</span> par = <span class=\"keyword\">new</span> Buffer(_par.substring(<span class=\"number\">0</span>, _par.length - <span class=\"number\">1</span>))</span><br><span class=\"line\"> <span class=\"comment\">// or let par = new Buffer(_par.slice(0, _par.length - 1))</span></span><br><span class=\"line\"> par = par.toString(<span class=\"string\">'base64'</span>)</span><br><span class=\"line\"> _data.param_value = par <span class=\"comment\">// 根据后端接口参数定义 param_value 的名字</span></span><br><span class=\"line\"> }</span><br><span class=\"line\"> }</span><br><span class=\"line\">}</span><br><span class=\"line\"></span><br><span class=\"line\"><span class=\"comment\">// 调用</span></span><br><span class=\"line\"><span class=\"keyword\">let</span> sign_data = get_sign({</span><br><span class=\"line\">\tparams: {</span><br><span class=\"line\"> \tparams1: <span class=\"string\">'test'</span>,</span><br><span class=\"line\"> params2: <span class=\"string\">'test'</span>,</span><br><span class=\"line\"> params3: <span class=\"string\">'test'</span>,</span><br><span class=\"line\"> params4: <span class=\"string\">'test'</span>,</span><br><span class=\"line\"> params5: <span class=\"string\">'test'</span>,</span><br><span class=\"line\"> }</span><br><span class=\"line\">})</span><br><span class=\"line\"></span><br><span class=\"line\"><span class=\"comment\">//sign_data=dGhpcyBpcyBhIGV4YW1wbGVzaWduX2RhdGE=</span></span><br></pre></td></tr></table></figure>\n<blockquote>\n<p>nodejs global 全局变量(doc)[<a href=\"http://nodejs.cn/api/globals.html]\" target=\"_blank\" rel=\"noopener\">http://nodejs.cn/api/globals.html]</a></p>\n</blockquote>\n<pre><code>global 全局变量\n</code></pre><blockquote>\n<p>redis 使用方法(git)[<a href=\"https://github.com/NodeRedis/node_redis]\" target=\"_blank\" rel=\"noopener\">https://github.com/NodeRedis/node_redis]</a><br><figure class=\"highlight shell\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">npm install redis</span><br></pre></td></tr></table></figure></p>\n</blockquote>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"keyword\">import</span> redis <span class=\"keyword\">from</span> <span class=\"string\">'redis'</span></span><br><span class=\"line\"><span class=\"keyword\">let</span> client = redis.createClient({</span><br><span class=\"line\">\thost : <span class=\"string\">'127.0.0.1'</span>,</span><br><span class=\"line\"> port : <span class=\"number\">6379</span>,</span><br><span class=\"line\"> connect_timeout: <span class=\"number\">60</span> * <span class=\"number\">60</span> * <span class=\"number\">100</span></span><br><span class=\"line\"> password : <span class=\"string\">'***'</span>,</span><br><span class=\"line\"> db : <span class=\"string\">''</span></span><br><span class=\"line\">})</span><br></pre></td></tr></table></figure>\n<blockquote>\n<p>mysql 使用方法(git)[<a href=\"https://github.com/mysqljs/mysql]\" target=\"_blank\" rel=\"noopener\">https://github.com/mysqljs/mysql]</a><br><figure class=\"highlight shell\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"meta\">#</span> install</span><br><span class=\"line\">npm install mysql</span><br></pre></td></tr></table></figure></p>\n</blockquote>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br><span class=\"line\">17</span><br><span class=\"line\">18</span><br><span class=\"line\">19</span><br><span class=\"line\">20</span><br><span class=\"line\">21</span><br><span class=\"line\">22</span><br><span class=\"line\">23</span><br><span class=\"line\">24</span><br><span class=\"line\">25</span><br><span class=\"line\">26</span><br><span class=\"line\">27</span><br><span class=\"line\">28</span><br><span class=\"line\">29</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"keyword\">import</span> mysql <span class=\"keyword\">from</span> <span class=\"string\">'mysql'</span></span><br><span class=\"line\"><span class=\"keyword\">const</span> connection = mysql.createPool({</span><br><span class=\"line\">\tconnectionLimit: <span class=\"number\">10000</span>, <span class=\"comment\">//连接池的最大连接数</span></span><br><span class=\"line\"> connectTimout : <span class=\"number\">60</span> * <span class=\"number\">60</span> * <span class=\"number\">1000</span>, <span class=\"comment\">// 在初始化连接mysql服务器发生超时的时间</span></span><br><span class=\"line\"> acquireTimeout : <span class=\"number\">60</span> * <span class=\"number\">60</span> * <span class=\"number\">1000</span>, <span class=\"comment\">//连接获取期间发生超时前的毫秒数。这与稍微不同connectTimeout,因为获取池连接并不总是涉及建立连接。(默认值:10000)</span></span><br><span class=\"line\"> timeout : <span class=\"number\">60</span> * <span class=\"number\">60</span> * <span class=\"number\">1000</span>,</span><br><span class=\"line\"> host : <span class=\"string\">'127.0.0.1'</span>, <span class=\"comment\">// 要连接的数据库主机名</span></span><br><span class=\"line\"> port : <span class=\"number\">3306</span>, <span class=\"comment\">//要连接的端口</span></span><br><span class=\"line\"> user : <span class=\"string\">'root'</span>, <span class=\"comment\">// 进行身份验证的mysql用户名</span></span><br><span class=\"line\"> password : <span class=\"string\">'***'</span>, <span class=\"comment\">//mysql密码</span></span><br><span class=\"line\"> databases : <span class=\"string\">'test'</span>, <span class=\"comment\">// 连接的数据库名称</span></span><br><span class=\"line\"> multipleStatements: <span class=\"literal\">true</span> <span class=\"comment\">//is True MULTI_STATEMENTS - 客户端可以为每个查询或语句准备发送多个语句</span></span><br><span class=\"line\">}).on(<span class=\"string\">'connection'</span>, (conn) => {</span><br><span class=\"line\">\t<span class=\"built_in\">console</span>.log(<span class=\"string\">'连接成功'</span>)</span><br><span class=\"line\">})</span><br><span class=\"line\"></span><br><span class=\"line\"><span class=\"keyword\">let</span> test_query = <span class=\"function\"><span class=\"params\">()</span>=></span> {</span><br><span class=\"line\">\tconnection.getConnection(<span class=\"function\">(<span class=\"params\">err, conn</span>)=></span> {</span><br><span class=\"line\"> \t<span class=\"keyword\">if</span> (err) {</span><br><span class=\"line\"> \t<span class=\"keyword\">throw</span> err</span><br><span class=\"line\"> }</span><br><span class=\"line\"> <span class=\"keyword\">let</span> query_string = <span class=\"string\">'select c_name1, c_name2 from tab'</span>;</span><br><span class=\"line\"> conn.query(query_string, (error, results, fields) => {</span><br><span class=\"line\"> \t<span class=\"keyword\">if</span> (results) {</span><br><span class=\"line\"> \t<span class=\"built_in\">console</span>.log(results)</span><br><span class=\"line\"> }</span><br><span class=\"line\"> })</span><br><span class=\"line\"> })</span><br><span class=\"line\">}</span><br></pre></td></tr></table></figure>","site":{"data":{}},"excerpt":"","more":"<h4 id=\"NODE-rabbitMQ\"><a href=\"#NODE-rabbitMQ\" class=\"headerlink\" title=\"NODE rabbitMQ\"></a>NODE rabbitMQ</h4><blockquote>\n<p>NODE使用rabbitMQ 方法 其一(git)[<a href=\"https://github.com/squaremo/amqp.node]\" target=\"_blank\" rel=\"noopener\">https://github.com/squaremo/amqp.node]</a></p>\n</blockquote>\n<figure class=\"highlight shell\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"meta\">#</span> install</span><br><span class=\"line\">npm install amqp --save</span><br></pre></td></tr></table></figure>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br><span class=\"line\">17</span><br><span class=\"line\">18</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"keyword\">import</span> amqp <span class=\"keyword\">from</span> <span class=\"string\">'amqp'</span></span><br><span class=\"line\"><span class=\"keyword\">let</span> mq_connection = amqp.createConnection({</span><br><span class=\"line\"> host: <span class=\"string\">'127.0.0.1'</span>,</span><br><span class=\"line\"> login: <span class=\"string\">'test'</span>, <span class=\"comment\">// 安装mq时 设置的用户</span></span><br><span class=\"line\"> password: <span class=\"string\">'text'</span> <span class=\"comment\">// 安装mq时设置的密码</span></span><br><span class=\"line\">})</span><br><span class=\"line\">mq_connection.on(<span class=\"string\">'error'</span>, (e)=> {</span><br><span class=\"line\">\t<span class=\"built_in\">console</span>.log(e) <span class=\"comment\">// 连接错误异常</span></span><br><span class=\"line\">})</span><br><span class=\"line\"><span class=\"keyword\">let</span> qpex = <span class=\"literal\">null</span> <span class=\"comment\">//设置订阅者对象</span></span><br><span class=\"line\"><span class=\"keyword\">let</span> queue_name = <span class=\"string\">'test'</span></span><br><span class=\"line\">mq_connection.on(<span class=\"string\">'ready'</span>, ()=> {</span><br><span class=\"line\">\tqpex = mq_connection.exchange(queue_name, {<span class=\"attr\">type</span>: <span class=\"string\">'dirct'</span>, <span class=\"attr\">autoDelete</span>: <span class=\"literal\">false</span>}, (e)=> {</span><br><span class=\"line\"> })</span><br><span class=\"line\"> mq_connection.queue(queue_name, {<span class=\"attr\">durable</span>: <span class=\"literal\">true</span>, <span class=\"attr\">autoDelete</span>: <span class=\"literal\">false</span>}, (queue)=> {</span><br><span class=\"line\"> \tqueue.bind(queue_name, queue_name)</span><br><span class=\"line\"> })</span><br><span class=\"line\">})</span><br></pre></td></tr></table></figure>\n<blockquote>\n<p>NODE使用rabbitMQ 方法 其二(git)[<a href=\"https://github.com/postwait/node-amqp]\" target=\"_blank\" rel=\"noopener\">https://github.com/postwait/node-amqp]</a></p>\n</blockquote>\n<figure class=\"highlight shell\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"meta\">#</span> install </span><br><span class=\"line\">npm install node-amqp</span><br></pre></td></tr></table></figure>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"keyword\">import</span> nodeAmqp <span class=\"keyword\">from</span> <span class=\"string\">'node-amqp'</span></span><br><span class=\"line\"></span><br><span class=\"line\"><span class=\"keyword\">let</span> queue_name = <span class=\"string\">'test'</span></span><br><span class=\"line\"><span class=\"keyword\">let</span> Publisher = nodeAmqp.Publisher</span><br><span class=\"line\"><span class=\"keyword\">let</span> publisher = <span class=\"keyword\">new</span> Publisher({</span><br><span class=\"line\">\t<span class=\"string\">'host'</span>: <span class=\"string\">'amqp://test:[email protected]'</span>,</span><br><span class=\"line\">\t<span class=\"string\">'exchangeName'</span>: queue_name</span><br><span class=\"line\">})</span><br></pre></td></tr></table></figure>\n<blockquote>\n<p>node 参数加密 base64</p>\n</blockquote>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br><span class=\"line\">17</span><br><span class=\"line\">18</span><br><span class=\"line\">19</span><br><span class=\"line\">20</span><br><span class=\"line\">21</span><br><span class=\"line\">22</span><br><span class=\"line\">23</span><br><span class=\"line\">24</span><br><span class=\"line\">25</span><br><span class=\"line\">26</span><br><span class=\"line\">27</span><br><span class=\"line\">28</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"keyword\">let</span> get_sign = <span class=\"function\">(<span class=\"params\">data</span>)=></span> {</span><br><span class=\"line\">\t<span class=\"keyword\">let</span> _data = {}</span><br><span class=\"line\"> <span class=\"keyword\">if</span> (data) {</span><br><span class=\"line\"> \t<span class=\"keyword\">let</span> _par = <span class=\"string\">''</span></span><br><span class=\"line\"> <span class=\"keyword\">if</span> (data.params) {</span><br><span class=\"line\"> \t<span class=\"keyword\">for</span>(<span class=\"keyword\">let</span> i <span class=\"keyword\">in</span> data.params) {</span><br><span class=\"line\"> \t_par += <span class=\"string\">`<span class=\"subst\">${i}</span>=<span class=\"subst\">${data.params[i]}</span>&`</span></span><br><span class=\"line\"> }</span><br><span class=\"line\"> <span class=\"keyword\">let</span> par = <span class=\"keyword\">new</span> Buffer(_par.substring(<span class=\"number\">0</span>, _par.length - <span class=\"number\">1</span>))</span><br><span class=\"line\"> <span class=\"comment\">// or let par = new Buffer(_par.slice(0, _par.length - 1))</span></span><br><span class=\"line\"> par = par.toString(<span class=\"string\">'base64'</span>)</span><br><span class=\"line\"> _data.param_value = par <span class=\"comment\">// 根据后端接口参数定义 param_value 的名字</span></span><br><span class=\"line\"> }</span><br><span class=\"line\"> }</span><br><span class=\"line\">}</span><br><span class=\"line\"></span><br><span class=\"line\"><span class=\"comment\">// 调用</span></span><br><span class=\"line\"><span class=\"keyword\">let</span> sign_data = get_sign({</span><br><span class=\"line\">\tparams: {</span><br><span class=\"line\"> \tparams1: <span class=\"string\">'test'</span>,</span><br><span class=\"line\"> params2: <span class=\"string\">'test'</span>,</span><br><span class=\"line\"> params3: <span class=\"string\">'test'</span>,</span><br><span class=\"line\"> params4: <span class=\"string\">'test'</span>,</span><br><span class=\"line\"> params5: <span class=\"string\">'test'</span>,</span><br><span class=\"line\"> }</span><br><span class=\"line\">})</span><br><span class=\"line\"></span><br><span class=\"line\"><span class=\"comment\">//sign_data=dGhpcyBpcyBhIGV4YW1wbGVzaWduX2RhdGE=</span></span><br></pre></td></tr></table></figure>\n<blockquote>\n<p>nodejs global 全局变量(doc)[<a href=\"http://nodejs.cn/api/globals.html]\" target=\"_blank\" rel=\"noopener\">http://nodejs.cn/api/globals.html]</a></p>\n</blockquote>\n<pre><code>global 全局变量\n</code></pre><blockquote>\n<p>redis 使用方法(git)[<a href=\"https://github.com/NodeRedis/node_redis]\" target=\"_blank\" rel=\"noopener\">https://github.com/NodeRedis/node_redis]</a><br><figure class=\"highlight shell\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">npm install redis</span><br></pre></td></tr></table></figure></p>\n</blockquote>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"keyword\">import</span> redis <span class=\"keyword\">from</span> <span class=\"string\">'redis'</span></span><br><span class=\"line\"><span class=\"keyword\">let</span> client = redis.createClient({</span><br><span class=\"line\">\thost : <span class=\"string\">'127.0.0.1'</span>,</span><br><span class=\"line\"> port : <span class=\"number\">6379</span>,</span><br><span class=\"line\"> connect_timeout: <span class=\"number\">60</span> * <span class=\"number\">60</span> * <span class=\"number\">100</span></span><br><span class=\"line\"> password : <span class=\"string\">'***'</span>,</span><br><span class=\"line\"> db : <span class=\"string\">''</span></span><br><span class=\"line\">})</span><br></pre></td></tr></table></figure>\n<blockquote>\n<p>mysql 使用方法(git)[<a href=\"https://github.com/mysqljs/mysql]\" target=\"_blank\" rel=\"noopener\">https://github.com/mysqljs/mysql]</a><br><figure class=\"highlight shell\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"meta\">#</span> install</span><br><span class=\"line\">npm install mysql</span><br></pre></td></tr></table></figure></p>\n</blockquote>\n<figure class=\"highlight js\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br><span class=\"line\">5</span><br><span class=\"line\">6</span><br><span class=\"line\">7</span><br><span class=\"line\">8</span><br><span class=\"line\">9</span><br><span class=\"line\">10</span><br><span class=\"line\">11</span><br><span class=\"line\">12</span><br><span class=\"line\">13</span><br><span class=\"line\">14</span><br><span class=\"line\">15</span><br><span class=\"line\">16</span><br><span class=\"line\">17</span><br><span class=\"line\">18</span><br><span class=\"line\">19</span><br><span class=\"line\">20</span><br><span class=\"line\">21</span><br><span class=\"line\">22</span><br><span class=\"line\">23</span><br><span class=\"line\">24</span><br><span class=\"line\">25</span><br><span class=\"line\">26</span><br><span class=\"line\">27</span><br><span class=\"line\">28</span><br><span class=\"line\">29</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"keyword\">import</span> mysql <span class=\"keyword\">from</span> <span class=\"string\">'mysql'</span></span><br><span class=\"line\"><span class=\"keyword\">const</span> connection = mysql.createPool({</span><br><span class=\"line\">\tconnectionLimit: <span class=\"number\">10000</span>, <span class=\"comment\">//连接池的最大连接数</span></span><br><span class=\"line\"> connectTimout : <span class=\"number\">60</span> * <span class=\"number\">60</span> * <span class=\"number\">1000</span>, <span class=\"comment\">// 在初始化连接mysql服务器发生超时的时间</span></span><br><span class=\"line\"> acquireTimeout : <span class=\"number\">60</span> * <span class=\"number\">60</span> * <span class=\"number\">1000</span>, <span class=\"comment\">//连接获取期间发生超时前的毫秒数。这与稍微不同connectTimeout,因为获取池连接并不总是涉及建立连接。(默认值:10000)</span></span><br><span class=\"line\"> timeout : <span class=\"number\">60</span> * <span class=\"number\">60</span> * <span class=\"number\">1000</span>,</span><br><span class=\"line\"> host : <span class=\"string\">'127.0.0.1'</span>, <span class=\"comment\">// 要连接的数据库主机名</span></span><br><span class=\"line\"> port : <span class=\"number\">3306</span>, <span class=\"comment\">//要连接的端口</span></span><br><span class=\"line\"> user : <span class=\"string\">'root'</span>, <span class=\"comment\">// 进行身份验证的mysql用户名</span></span><br><span class=\"line\"> password : <span class=\"string\">'***'</span>, <span class=\"comment\">//mysql密码</span></span><br><span class=\"line\"> databases : <span class=\"string\">'test'</span>, <span class=\"comment\">// 连接的数据库名称</span></span><br><span class=\"line\"> multipleStatements: <span class=\"literal\">true</span> <span class=\"comment\">//is True MULTI_STATEMENTS - 客户端可以为每个查询或语句准备发送多个语句</span></span><br><span class=\"line\">}).on(<span class=\"string\">'connection'</span>, (conn) => {</span><br><span class=\"line\">\t<span class=\"built_in\">console</span>.log(<span class=\"string\">'连接成功'</span>)</span><br><span class=\"line\">})</span><br><span class=\"line\"></span><br><span class=\"line\"><span class=\"keyword\">let</span> test_query = <span class=\"function\"><span class=\"params\">()</span>=></span> {</span><br><span class=\"line\">\tconnection.getConnection(<span class=\"function\">(<span class=\"params\">err, conn</span>)=></span> {</span><br><span class=\"line\"> \t<span class=\"keyword\">if</span> (err) {</span><br><span class=\"line\"> \t<span class=\"keyword\">throw</span> err</span><br><span class=\"line\"> }</span><br><span class=\"line\"> <span class=\"keyword\">let</span> query_string = <span class=\"string\">'select c_name1, c_name2 from tab'</span>;</span><br><span class=\"line\"> conn.query(query_string, (error, results, fields) => {</span><br><span class=\"line\"> \t<span class=\"keyword\">if</span> (results) {</span><br><span class=\"line\"> \t<span class=\"built_in\">console</span>.log(results)</span><br><span class=\"line\"> }</span><br><span class=\"line\"> })</span><br><span class=\"line\"> })</span><br><span class=\"line\">}</span><br></pre></td></tr></table></figure>"},{"title":"数据库引擎","author":"brian","date":"2018-11-20T09:14:02.000Z","_content":"#### MYSQL数据库引擎\n\n一、数据库引擎\n> 数据库引擎是用于存储、处理和保护数据的核心服务。利用数据库引擎可控制访问权限并快速处理事务,从而满足企业内大多数需要处理大量数据的应用程序的要求。 使用数据库引擎创建用于联机事务处理或联机分析处理数据的关系数据库。这包括创建用于存储数据的表和用于查看、管理和保护数据安全的数据库对象(如索引、视图和存储过程)。\n\n二、数据库引擎任务\n> 在数据库引擎文档中,各主题的顺序遵循用于实现使用数据库引擎进行数据存储的系统的任务的主要顺序。\n\n\n1.\t设计并创建数据库以保存系统所需的关系或XML文档\n2.\t实现系统以访问和更改数据库中存储的数据。包括实现网站或使用数据的应用程序,还包括生成使用SQL Server工具和实用工具以使用数据的过程。\n3.\t为单位或客户部署实现的系统\n4.\t提供日常管理支持以优化数据库的性能\n\n三、MySQL数据库引擎类别\n\n\t你能用的数据库引擎取决于mysql在安装的时候是如何被编译的。要添加一个新的引擎,就必须重新编译MYSQL。在缺省情况下,MYSQL支持三个引擎:ISAM、MYISAM和HEAP。另外两种类型INNODB和BERKLEY(BDB),也常常可以使用。\n\n#### ISAM\n-------\n\tISAM是一个定义明确且历经时间考验的数据表格管理方法,它在设计之时就考虑到数据库被查询的次数要远大于更新的次数。因此,ISAM执行读取操作的速度很快,而且不占用大量的内存和存储资源。ISAM的两个主要不足之处在于,它不支持事务处理,也不能够容错:如果你的硬盘崩溃了,那么数据文件就无法恢复了。如果你正在把ISAM用在关键任务应用程序里,那就必须经常备份你所有的实时数据,通过其复制特性,MYSQL能够支持这样的备份应用程序。\n\n#### MYISAM\n----\n\tMYISAM是MYSQL的ISAM扩展格式和缺省的数据库引擎。除了提供ISAM里所没有的索引和字段管理的功能,MYISAM还使用一种表格锁定的机制,来优化多个并发的读写操作。其代价是你需要经常运行OPTIMIZE TABLE命令,来恢复被更新机制所浪费的空间。MYISAM还有一些有用的扩展,例如用来修复数据库文件的MYISAMCHK工具和用来恢复浪费空间的MYISAMPACK工具。\t\n MYISAM强调了快速读取操作,这可能就是为什么MYSQL受到了WEB开发如此青睐的主要原因:在WEB开发中你所进行的大量数据操作都是读取操作。所以,大多数虚拟主机提供商和INTERNET平台提供商只允许使用MYISAM格式。\n \n#### HEAP\n----\n\tHEAP允许只驻留在内存里的临时表格。驻留在内存里让HEAP要比ISAM和MYISAM都快,但是它所管理的数据是不稳定的,而且如果在关机之前没有进行保存,那么所有的数据都会丢失。在数据行被删除的时候,HEAP也不会浪费大量的空间。HEAP表格在你需要使用SELECT表达式来选择和操控数据的时候非常有用。要记住,在用完表格之后就删除表格。\n#### INNODB ADN BERKLEYDB\n---\n\tINNODB和BERKLEYDB(BDB)数据库引擎都是造就MYSQL灵活性的技术的直接产品,这项技术就是MYSQL++ API。在使用MYSQL的时候,你所面对的每一个挑战几乎都源于ISAM和MYISAM数据库引擎不支持事务处理也不支持外来键。尽管要比ISAM和MYISAM引擎慢很多,但是INNODB和BDB包括了对事务处理和外来键的支持,这两点都是前两个引擎所没有的。如前所述,如果你的设计需要这些特性中的一者或者两者,那你就要被迫使用后两个引擎中的一个了。\n \n四、mysql数据引擎更换方式\n\n> 1、查看当前数据库支持的引擎和默认的数据库引擎:\n```bash\nshow engines;\n```\n> 2、更改数据库引擎\n\n2.1、更改方式1:修改配置文件my.ini\n\n\t将my-small.ini另存为my.ini,在[mysqld]后面添加default-storage-engine=InnoDB,重启服务,数据库默认的引擎修改为InnoDB\n\n2.2、更改方式2:在建表的时候指定\n```sql\ncreate table mytbl( \n id int primary key, \n name varchar(50) \n)type=MyISAM;\n```\n2.3、更改方式3:建表后更改\n```bash\nalter table mytbl2 type = InnoDB;\n```\n> 3、查看修改结果\n```bash\nshow table status from mytest; \nor\nshow create table table_name\n```\n\n五、MyIASM 和 Innodb引擎详解\n\n#### Innodb引擎\n---\n\tInnodb引擎提供了对数据库ACID事务的支持,并且实现了SQL标准的四种隔离级别,关于数据库事务与其隔离级别的内容请见数据库事务与其隔离级别这篇文章。该引擎还提供了行级锁和外键约束,它的设计目标是处理大容量数据库系统,它本身其实就是基于MySQL后台的完整数据库系统,MySQL运行时Innodb会在内存中建立缓冲池,用于缓冲数据和索引。但是该引擎不支持FULLTEXT类型的索引,而且它没有保存表的行数,当SELECT COUNT(*) FROM TABLE时需要扫描全表。当需要使用数据库事务时,该引擎当然是首选。由于锁的粒度更小,写操作不会锁定全表,所以在并发较高时,使用Innodb引擎会提升效率。但是使用行级锁也不是绝对的,如果在执行一个SQL语句时MySQL不能确定要扫描的范围,InnoDB表同样会锁全表。\n\n#### ACID(Atomicity, Consistency, Isolation, Durability)\n---\n\t1. A 事务的原子性(Atomicity):指一个事务要么全部执行,要么不执行.也就是说一个事务不可能只执行了一半就停止了.比如你从取款机取钱,这个事务可以分成两个步骤:1划卡,2出钱.不可能划了卡,而钱却没出来.这两步必须同时完成.要么就不完成.\n\t2. C 事务的一致性(Consistency):指事务的运行并不改变数据库中数据的一致性.例如,完整性约束了a+b=10,一个事务改变了a,那么b也应该随之改变.\n\t3. I 独立性(Isolation):事务的独立性也有称作隔离性,是指两个以上的事务不会出现交错执行的状态.因为这样可能会导致数据不一致.\n\t4. D 持久性(Durability):事务的持久性是指事务执行成功以后,该事务所对数据库所作的更改便是持久的保存在数据库之中,不会无缘无故的回滚.\n \n \n#### MYIASM引擎\n---\n\tMyIASM是MySQL默认的引擎,但是它没有提供对数据库事务的支持,也不支持行级锁和外键,因此当INSERT(插入)或UPDATE(更新)数据时即写操作需要锁定整个表,效率便会低一些。不过和Innodb不同,MyIASM中存储了表的行数,于是SELECT COUNT(*) FROM TABLE时只需要直接读取已经保存好的值而不需要进行全表扫描。如果表的读操作远远多于写操作且不需要数据库事务的支持,那么MyIASM也是很好的选择。\n\n> 两种引擎的选择\n\n\t大尺寸的数据集趋向于选择InnoDB引擎,因为它支持事务处理和故障恢复。数据库的大小决定了故障恢复的时间长短,InnoDB可以利用事务日志进行数据恢复,这会比较快。主键查询在InnoDB引擎下也会相当快,不过需要注意的是如果主键太长也会导致性能问题,关于这个问题我会在下文中讲到。大批的INSERT语句(在每个INSERT语句中写入多行,批量插入)在MyISAM下会快一些,但是UPDATE语句在InnoDB下则会更快一些,尤其是在并发量大的时候。\n\n\n#### Index——索引\n----\n\t索引(Index)是帮助MySQL高效获取数据的数据结构。MyIASM和Innodb都使用了树这种数据结构做为索引。下面我接着讲这两种引擎使用的索引结构,讲到这里,首先应该谈一下B-Tree和B+Tree。\n\n","source":"_drafts/数据库引擎.md","raw":"title: 数据库引擎\nauthor: brian\ndate: 2018-11-20 17:14:02\ntags:\n---\n#### MYSQL数据库引擎\n\n一、数据库引擎\n> 数据库引擎是用于存储、处理和保护数据的核心服务。利用数据库引擎可控制访问权限并快速处理事务,从而满足企业内大多数需要处理大量数据的应用程序的要求。 使用数据库引擎创建用于联机事务处理或联机分析处理数据的关系数据库。这包括创建用于存储数据的表和用于查看、管理和保护数据安全的数据库对象(如索引、视图和存储过程)。\n\n二、数据库引擎任务\n> 在数据库引擎文档中,各主题的顺序遵循用于实现使用数据库引擎进行数据存储的系统的任务的主要顺序。\n\n\n1.\t设计并创建数据库以保存系统所需的关系或XML文档\n2.\t实现系统以访问和更改数据库中存储的数据。包括实现网站或使用数据的应用程序,还包括生成使用SQL Server工具和实用工具以使用数据的过程。\n3.\t为单位或客户部署实现的系统\n4.\t提供日常管理支持以优化数据库的性能\n\n三、MySQL数据库引擎类别\n\n\t你能用的数据库引擎取决于mysql在安装的时候是如何被编译的。要添加一个新的引擎,就必须重新编译MYSQL。在缺省情况下,MYSQL支持三个引擎:ISAM、MYISAM和HEAP。另外两种类型INNODB和BERKLEY(BDB),也常常可以使用。\n\n#### ISAM\n-------\n\tISAM是一个定义明确且历经时间考验的数据表格管理方法,它在设计之时就考虑到数据库被查询的次数要远大于更新的次数。因此,ISAM执行读取操作的速度很快,而且不占用大量的内存和存储资源。ISAM的两个主要不足之处在于,它不支持事务处理,也不能够容错:如果你的硬盘崩溃了,那么数据文件就无法恢复了。如果你正在把ISAM用在关键任务应用程序里,那就必须经常备份你所有的实时数据,通过其复制特性,MYSQL能够支持这样的备份应用程序。\n\n#### MYISAM\n----\n\tMYISAM是MYSQL的ISAM扩展格式和缺省的数据库引擎。除了提供ISAM里所没有的索引和字段管理的功能,MYISAM还使用一种表格锁定的机制,来优化多个并发的读写操作。其代价是你需要经常运行OPTIMIZE TABLE命令,来恢复被更新机制所浪费的空间。MYISAM还有一些有用的扩展,例如用来修复数据库文件的MYISAMCHK工具和用来恢复浪费空间的MYISAMPACK工具。\t\n MYISAM强调了快速读取操作,这可能就是为什么MYSQL受到了WEB开发如此青睐的主要原因:在WEB开发中你所进行的大量数据操作都是读取操作。所以,大多数虚拟主机提供商和INTERNET平台提供商只允许使用MYISAM格式。\n \n#### HEAP\n----\n\tHEAP允许只驻留在内存里的临时表格。驻留在内存里让HEAP要比ISAM和MYISAM都快,但是它所管理的数据是不稳定的,而且如果在关机之前没有进行保存,那么所有的数据都会丢失。在数据行被删除的时候,HEAP也不会浪费大量的空间。HEAP表格在你需要使用SELECT表达式来选择和操控数据的时候非常有用。要记住,在用完表格之后就删除表格。\n#### INNODB ADN BERKLEYDB\n---\n\tINNODB和BERKLEYDB(BDB)数据库引擎都是造就MYSQL灵活性的技术的直接产品,这项技术就是MYSQL++ API。在使用MYSQL的时候,你所面对的每一个挑战几乎都源于ISAM和MYISAM数据库引擎不支持事务处理也不支持外来键。尽管要比ISAM和MYISAM引擎慢很多,但是INNODB和BDB包括了对事务处理和外来键的支持,这两点都是前两个引擎所没有的。如前所述,如果你的设计需要这些特性中的一者或者两者,那你就要被迫使用后两个引擎中的一个了。\n \n四、mysql数据引擎更换方式\n\n> 1、查看当前数据库支持的引擎和默认的数据库引擎:\n```bash\nshow engines;\n```\n> 2、更改数据库引擎\n\n2.1、更改方式1:修改配置文件my.ini\n\n\t将my-small.ini另存为my.ini,在[mysqld]后面添加default-storage-engine=InnoDB,重启服务,数据库默认的引擎修改为InnoDB\n\n2.2、更改方式2:在建表的时候指定\n```sql\ncreate table mytbl( \n id int primary key, \n name varchar(50) \n)type=MyISAM;\n```\n2.3、更改方式3:建表后更改\n```bash\nalter table mytbl2 type = InnoDB;\n```\n> 3、查看修改结果\n```bash\nshow table status from mytest; \nor\nshow create table table_name\n```\n\n五、MyIASM 和 Innodb引擎详解\n\n#### Innodb引擎\n---\n\tInnodb引擎提供了对数据库ACID事务的支持,并且实现了SQL标准的四种隔离级别,关于数据库事务与其隔离级别的内容请见数据库事务与其隔离级别这篇文章。该引擎还提供了行级锁和外键约束,它的设计目标是处理大容量数据库系统,它本身其实就是基于MySQL后台的完整数据库系统,MySQL运行时Innodb会在内存中建立缓冲池,用于缓冲数据和索引。但是该引擎不支持FULLTEXT类型的索引,而且它没有保存表的行数,当SELECT COUNT(*) FROM TABLE时需要扫描全表。当需要使用数据库事务时,该引擎当然是首选。由于锁的粒度更小,写操作不会锁定全表,所以在并发较高时,使用Innodb引擎会提升效率。但是使用行级锁也不是绝对的,如果在执行一个SQL语句时MySQL不能确定要扫描的范围,InnoDB表同样会锁全表。\n\n#### ACID(Atomicity, Consistency, Isolation, Durability)\n---\n\t1. A 事务的原子性(Atomicity):指一个事务要么全部执行,要么不执行.也就是说一个事务不可能只执行了一半就停止了.比如你从取款机取钱,这个事务可以分成两个步骤:1划卡,2出钱.不可能划了卡,而钱却没出来.这两步必须同时完成.要么就不完成.\n\t2. C 事务的一致性(Consistency):指事务的运行并不改变数据库中数据的一致性.例如,完整性约束了a+b=10,一个事务改变了a,那么b也应该随之改变.\n\t3. I 独立性(Isolation):事务的独立性也有称作隔离性,是指两个以上的事务不会出现交错执行的状态.因为这样可能会导致数据不一致.\n\t4. D 持久性(Durability):事务的持久性是指事务执行成功以后,该事务所对数据库所作的更改便是持久的保存在数据库之中,不会无缘无故的回滚.\n \n \n#### MYIASM引擎\n---\n\tMyIASM是MySQL默认的引擎,但是它没有提供对数据库事务的支持,也不支持行级锁和外键,因此当INSERT(插入)或UPDATE(更新)数据时即写操作需要锁定整个表,效率便会低一些。不过和Innodb不同,MyIASM中存储了表的行数,于是SELECT COUNT(*) FROM TABLE时只需要直接读取已经保存好的值而不需要进行全表扫描。如果表的读操作远远多于写操作且不需要数据库事务的支持,那么MyIASM也是很好的选择。\n\n> 两种引擎的选择\n\n\t大尺寸的数据集趋向于选择InnoDB引擎,因为它支持事务处理和故障恢复。数据库的大小决定了故障恢复的时间长短,InnoDB可以利用事务日志进行数据恢复,这会比较快。主键查询在InnoDB引擎下也会相当快,不过需要注意的是如果主键太长也会导致性能问题,关于这个问题我会在下文中讲到。大批的INSERT语句(在每个INSERT语句中写入多行,批量插入)在MyISAM下会快一些,但是UPDATE语句在InnoDB下则会更快一些,尤其是在并发量大的时候。\n\n\n#### Index——索引\n----\n\t索引(Index)是帮助MySQL高效获取数据的数据结构。MyIASM和Innodb都使用了树这种数据结构做为索引。下面我接着讲这两种引擎使用的索引结构,讲到这里,首先应该谈一下B-Tree和B+Tree。\n\n","slug":"数据库引擎","published":0,"updated":"2018-11-20T09:27:57.294Z","_id":"cjopiv1iu000151vofipsa698","comments":1,"layout":"post","photos":[],"link":"","content":"<h4 id=\"MYSQL数据库引擎\"><a href=\"#MYSQL数据库引擎\" class=\"headerlink\" title=\"MYSQL数据库引擎\"></a>MYSQL数据库引擎</h4><p>一、数据库引擎</p>\n<blockquote>\n<p>数据库引擎是用于存储、处理和保护数据的核心服务。利用数据库引擎可控制访问权限并快速处理事务,从而满足企业内大多数需要处理大量数据的应用程序的要求。 使用数据库引擎创建用于联机事务处理或联机分析处理数据的关系数据库。这包括创建用于存储数据的表和用于查看、管理和保护数据安全的数据库对象(如索引、视图和存储过程)。</p>\n</blockquote>\n<p>二、数据库引擎任务</p>\n<blockquote>\n<p>在数据库引擎文档中,各主题的顺序遵循用于实现使用数据库引擎进行数据存储的系统的任务的主要顺序。</p>\n</blockquote>\n<ol>\n<li>设计并创建数据库以保存系统所需的关系或XML文档</li>\n<li>实现系统以访问和更改数据库中存储的数据。包括实现网站或使用数据的应用程序,还包括生成使用SQL Server工具和实用工具以使用数据的过程。</li>\n<li>为单位或客户部署实现的系统</li>\n<li>提供日常管理支持以优化数据库的性能</li>\n</ol>\n<p>三、MySQL数据库引擎类别</p>\n<pre><code>你能用的数据库引擎取决于mysql在安装的时候是如何被编译的。要添加一个新的引擎,就必须重新编译MYSQL。在缺省情况下,MYSQL支持三个引擎:ISAM、MYISAM和HEAP。另外两种类型INNODB和BERKLEY(BDB),也常常可以使用。\n</code></pre><h4 id=\"ISAM\"><a href=\"#ISAM\" class=\"headerlink\" title=\"ISAM\"></a>ISAM</h4><hr>\n<pre><code>ISAM是一个定义明确且历经时间考验的数据表格管理方法,它在设计之时就考虑到数据库被查询的次数要远大于更新的次数。因此,ISAM执行读取操作的速度很快,而且不占用大量的内存和存储资源。ISAM的两个主要不足之处在于,它不支持事务处理,也不能够容错:如果你的硬盘崩溃了,那么数据文件就无法恢复了。如果你正在把ISAM用在关键任务应用程序里,那就必须经常备份你所有的实时数据,通过其复制特性,MYSQL能够支持这样的备份应用程序。\n</code></pre><h4 id=\"MYISAM\"><a href=\"#MYISAM\" class=\"headerlink\" title=\"MYISAM\"></a>MYISAM</h4><hr>\n<pre><code>MYISAM是MYSQL的ISAM扩展格式和缺省的数据库引擎。除了提供ISAM里所没有的索引和字段管理的功能,MYISAM还使用一种表格锁定的机制,来优化多个并发的读写操作。其代价是你需要经常运行OPTIMIZE TABLE命令,来恢复被更新机制所浪费的空间。MYISAM还有一些有用的扩展,例如用来修复数据库文件的MYISAMCHK工具和用来恢复浪费空间的MYISAMPACK工具。 \nMYISAM强调了快速读取操作,这可能就是为什么MYSQL受到了WEB开发如此青睐的主要原因:在WEB开发中你所进行的大量数据操作都是读取操作。所以,大多数虚拟主机提供商和INTERNET平台提供商只允许使用MYISAM格式。\n</code></pre><h4 id=\"HEAP\"><a href=\"#HEAP\" class=\"headerlink\" title=\"HEAP\"></a>HEAP</h4><hr>\n<pre><code>HEAP允许只驻留在内存里的临时表格。驻留在内存里让HEAP要比ISAM和MYISAM都快,但是它所管理的数据是不稳定的,而且如果在关机之前没有进行保存,那么所有的数据都会丢失。在数据行被删除的时候,HEAP也不会浪费大量的空间。HEAP表格在你需要使用SELECT表达式来选择和操控数据的时候非常有用。要记住,在用完表格之后就删除表格。\n</code></pre><h4 id=\"INNODB-ADN-BERKLEYDB\"><a href=\"#INNODB-ADN-BERKLEYDB\" class=\"headerlink\" title=\"INNODB ADN BERKLEYDB\"></a>INNODB ADN BERKLEYDB</h4><hr>\n<pre><code>INNODB和BERKLEYDB(BDB)数据库引擎都是造就MYSQL灵活性的技术的直接产品,这项技术就是MYSQL++ API。在使用MYSQL的时候,你所面对的每一个挑战几乎都源于ISAM和MYISAM数据库引擎不支持事务处理也不支持外来键。尽管要比ISAM和MYISAM引擎慢很多,但是INNODB和BDB包括了对事务处理和外来键的支持,这两点都是前两个引擎所没有的。如前所述,如果你的设计需要这些特性中的一者或者两者,那你就要被迫使用后两个引擎中的一个了。\n</code></pre><p>四、mysql数据引擎更换方式</p>\n<blockquote>\n<p>1、查看当前数据库支持的引擎和默认的数据库引擎:<br><figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">show engines;</span><br></pre></td></tr></table></figure></p>\n</blockquote>\n<blockquote>\n<p>2、更改数据库引擎</p>\n</blockquote>\n<p>2.1、更改方式1:修改配置文件my.ini</p>\n<pre><code>将my-small.ini另存为my.ini,在[mysqld]后面添加default-storage-engine=InnoDB,重启服务,数据库默认的引擎修改为InnoDB\n</code></pre><p>2.2、更改方式2:在建表的时候指定<br><figure class=\"highlight sql\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"keyword\">create</span> <span class=\"keyword\">table</span> mytbl( </span><br><span class=\"line\"> <span class=\"keyword\">id</span> <span class=\"built_in\">int</span> primary <span class=\"keyword\">key</span>, </span><br><span class=\"line\"> <span class=\"keyword\">name</span> <span class=\"built_in\">varchar</span>(<span class=\"number\">50</span>) </span><br><span class=\"line\">)<span class=\"keyword\">type</span>=MyISAM;</span><br></pre></td></tr></table></figure></p>\n<p>2.3、更改方式3:建表后更改<br><figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">alter table mytbl2 <span class=\"built_in\">type</span> = InnoDB;</span><br></pre></td></tr></table></figure></p>\n<blockquote>\n<p>3、查看修改结果<br><figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">show table status from mytest; </span><br><span class=\"line\">or</span><br><span class=\"line\">show create table table_name</span><br></pre></td></tr></table></figure></p>\n</blockquote>\n<p>五、MyIASM 和 Innodb引擎详解</p>\n<h4 id=\"Innodb引擎\"><a href=\"#Innodb引擎\" class=\"headerlink\" title=\"Innodb引擎\"></a>Innodb引擎</h4><hr>\n<pre><code>Innodb引擎提供了对数据库ACID事务的支持,并且实现了SQL标准的四种隔离级别,关于数据库事务与其隔离级别的内容请见数据库事务与其隔离级别这篇文章。该引擎还提供了行级锁和外键约束,它的设计目标是处理大容量数据库系统,它本身其实就是基于MySQL后台的完整数据库系统,MySQL运行时Innodb会在内存中建立缓冲池,用于缓冲数据和索引。但是该引擎不支持FULLTEXT类型的索引,而且它没有保存表的行数,当SELECT COUNT(*) FROM TABLE时需要扫描全表。当需要使用数据库事务时,该引擎当然是首选。由于锁的粒度更小,写操作不会锁定全表,所以在并发较高时,使用Innodb引擎会提升效率。但是使用行级锁也不是绝对的,如果在执行一个SQL语句时MySQL不能确定要扫描的范围,InnoDB表同样会锁全表。\n</code></pre><h4 id=\"ACID(Atomicity-Consistency-Isolation-Durability)\"><a href=\"#ACID(Atomicity-Consistency-Isolation-Durability)\" class=\"headerlink\" title=\"ACID(Atomicity, Consistency, Isolation, Durability)\"></a>ACID(Atomicity, Consistency, Isolation, Durability)</h4><hr>\n<pre><code>1. A 事务的原子性(Atomicity):指一个事务要么全部执行,要么不执行.也就是说一个事务不可能只执行了一半就停止了.比如你从取款机取钱,这个事务可以分成两个步骤:1划卡,2出钱.不可能划了卡,而钱却没出来.这两步必须同时完成.要么就不完成.\n2. C 事务的一致性(Consistency):指事务的运行并不改变数据库中数据的一致性.例如,完整性约束了a+b=10,一个事务改变了a,那么b也应该随之改变.\n3. I 独立性(Isolation):事务的独立性也有称作隔离性,是指两个以上的事务不会出现交错执行的状态.因为这样可能会导致数据不一致.\n4. D 持久性(Durability):事务的持久性是指事务执行成功以后,该事务所对数据库所作的更改便是持久的保存在数据库之中,不会无缘无故的回滚.\n</code></pre><h4 id=\"MYIASM引擎\"><a href=\"#MYIASM引擎\" class=\"headerlink\" title=\"MYIASM引擎\"></a>MYIASM引擎</h4><hr>\n<pre><code>MyIASM是MySQL默认的引擎,但是它没有提供对数据库事务的支持,也不支持行级锁和外键,因此当INSERT(插入)或UPDATE(更新)数据时即写操作需要锁定整个表,效率便会低一些。不过和Innodb不同,MyIASM中存储了表的行数,于是SELECT COUNT(*) FROM TABLE时只需要直接读取已经保存好的值而不需要进行全表扫描。如果表的读操作远远多于写操作且不需要数据库事务的支持,那么MyIASM也是很好的选择。\n</code></pre><blockquote>\n<p>两种引擎的选择</p>\n</blockquote>\n<pre><code>大尺寸的数据集趋向于选择InnoDB引擎,因为它支持事务处理和故障恢复。数据库的大小决定了故障恢复的时间长短,InnoDB可以利用事务日志进行数据恢复,这会比较快。主键查询在InnoDB引擎下也会相当快,不过需要注意的是如果主键太长也会导致性能问题,关于这个问题我会在下文中讲到。大批的INSERT语句(在每个INSERT语句中写入多行,批量插入)在MyISAM下会快一些,但是UPDATE语句在InnoDB下则会更快一些,尤其是在并发量大的时候。\n</code></pre><h4 id=\"Index——索引\"><a href=\"#Index——索引\" class=\"headerlink\" title=\"Index——索引\"></a>Index——索引</h4><hr>\n<pre><code>索引(Index)是帮助MySQL高效获取数据的数据结构。MyIASM和Innodb都使用了树这种数据结构做为索引。下面我接着讲这两种引擎使用的索引结构,讲到这里,首先应该谈一下B-Tree和B+Tree。\n</code></pre>","site":{"data":{}},"excerpt":"","more":"<h4 id=\"MYSQL数据库引擎\"><a href=\"#MYSQL数据库引擎\" class=\"headerlink\" title=\"MYSQL数据库引擎\"></a>MYSQL数据库引擎</h4><p>一、数据库引擎</p>\n<blockquote>\n<p>数据库引擎是用于存储、处理和保护数据的核心服务。利用数据库引擎可控制访问权限并快速处理事务,从而满足企业内大多数需要处理大量数据的应用程序的要求。 使用数据库引擎创建用于联机事务处理或联机分析处理数据的关系数据库。这包括创建用于存储数据的表和用于查看、管理和保护数据安全的数据库对象(如索引、视图和存储过程)。</p>\n</blockquote>\n<p>二、数据库引擎任务</p>\n<blockquote>\n<p>在数据库引擎文档中,各主题的顺序遵循用于实现使用数据库引擎进行数据存储的系统的任务的主要顺序。</p>\n</blockquote>\n<ol>\n<li>设计并创建数据库以保存系统所需的关系或XML文档</li>\n<li>实现系统以访问和更改数据库中存储的数据。包括实现网站或使用数据的应用程序,还包括生成使用SQL Server工具和实用工具以使用数据的过程。</li>\n<li>为单位或客户部署实现的系统</li>\n<li>提供日常管理支持以优化数据库的性能</li>\n</ol>\n<p>三、MySQL数据库引擎类别</p>\n<pre><code>你能用的数据库引擎取决于mysql在安装的时候是如何被编译的。要添加一个新的引擎,就必须重新编译MYSQL。在缺省情况下,MYSQL支持三个引擎:ISAM、MYISAM和HEAP。另外两种类型INNODB和BERKLEY(BDB),也常常可以使用。\n</code></pre><h4 id=\"ISAM\"><a href=\"#ISAM\" class=\"headerlink\" title=\"ISAM\"></a>ISAM</h4><hr>\n<pre><code>ISAM是一个定义明确且历经时间考验的数据表格管理方法,它在设计之时就考虑到数据库被查询的次数要远大于更新的次数。因此,ISAM执行读取操作的速度很快,而且不占用大量的内存和存储资源。ISAM的两个主要不足之处在于,它不支持事务处理,也不能够容错:如果你的硬盘崩溃了,那么数据文件就无法恢复了。如果你正在把ISAM用在关键任务应用程序里,那就必须经常备份你所有的实时数据,通过其复制特性,MYSQL能够支持这样的备份应用程序。\n</code></pre><h4 id=\"MYISAM\"><a href=\"#MYISAM\" class=\"headerlink\" title=\"MYISAM\"></a>MYISAM</h4><hr>\n<pre><code>MYISAM是MYSQL的ISAM扩展格式和缺省的数据库引擎。除了提供ISAM里所没有的索引和字段管理的功能,MYISAM还使用一种表格锁定的机制,来优化多个并发的读写操作。其代价是你需要经常运行OPTIMIZE TABLE命令,来恢复被更新机制所浪费的空间。MYISAM还有一些有用的扩展,例如用来修复数据库文件的MYISAMCHK工具和用来恢复浪费空间的MYISAMPACK工具。 \nMYISAM强调了快速读取操作,这可能就是为什么MYSQL受到了WEB开发如此青睐的主要原因:在WEB开发中你所进行的大量数据操作都是读取操作。所以,大多数虚拟主机提供商和INTERNET平台提供商只允许使用MYISAM格式。\n</code></pre><h4 id=\"HEAP\"><a href=\"#HEAP\" class=\"headerlink\" title=\"HEAP\"></a>HEAP</h4><hr>\n<pre><code>HEAP允许只驻留在内存里的临时表格。驻留在内存里让HEAP要比ISAM和MYISAM都快,但是它所管理的数据是不稳定的,而且如果在关机之前没有进行保存,那么所有的数据都会丢失。在数据行被删除的时候,HEAP也不会浪费大量的空间。HEAP表格在你需要使用SELECT表达式来选择和操控数据的时候非常有用。要记住,在用完表格之后就删除表格。\n</code></pre><h4 id=\"INNODB-ADN-BERKLEYDB\"><a href=\"#INNODB-ADN-BERKLEYDB\" class=\"headerlink\" title=\"INNODB ADN BERKLEYDB\"></a>INNODB ADN BERKLEYDB</h4><hr>\n<pre><code>INNODB和BERKLEYDB(BDB)数据库引擎都是造就MYSQL灵活性的技术的直接产品,这项技术就是MYSQL++ API。在使用MYSQL的时候,你所面对的每一个挑战几乎都源于ISAM和MYISAM数据库引擎不支持事务处理也不支持外来键。尽管要比ISAM和MYISAM引擎慢很多,但是INNODB和BDB包括了对事务处理和外来键的支持,这两点都是前两个引擎所没有的。如前所述,如果你的设计需要这些特性中的一者或者两者,那你就要被迫使用后两个引擎中的一个了。\n</code></pre><p>四、mysql数据引擎更换方式</p>\n<blockquote>\n<p>1、查看当前数据库支持的引擎和默认的数据库引擎:<br><figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">show engines;</span><br></pre></td></tr></table></figure></p>\n</blockquote>\n<blockquote>\n<p>2、更改数据库引擎</p>\n</blockquote>\n<p>2.1、更改方式1:修改配置文件my.ini</p>\n<pre><code>将my-small.ini另存为my.ini,在[mysqld]后面添加default-storage-engine=InnoDB,重启服务,数据库默认的引擎修改为InnoDB\n</code></pre><p>2.2、更改方式2:在建表的时候指定<br><figure class=\"highlight sql\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br><span class=\"line\">4</span><br></pre></td><td class=\"code\"><pre><span class=\"line\"><span class=\"keyword\">create</span> <span class=\"keyword\">table</span> mytbl( </span><br><span class=\"line\"> <span class=\"keyword\">id</span> <span class=\"built_in\">int</span> primary <span class=\"keyword\">key</span>, </span><br><span class=\"line\"> <span class=\"keyword\">name</span> <span class=\"built_in\">varchar</span>(<span class=\"number\">50</span>) </span><br><span class=\"line\">)<span class=\"keyword\">type</span>=MyISAM;</span><br></pre></td></tr></table></figure></p>\n<p>2.3、更改方式3:建表后更改<br><figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">alter table mytbl2 <span class=\"built_in\">type</span> = InnoDB;</span><br></pre></td></tr></table></figure></p>\n<blockquote>\n<p>3、查看修改结果<br><figure class=\"highlight bash\"><table><tr><td class=\"gutter\"><pre><span class=\"line\">1</span><br><span class=\"line\">2</span><br><span class=\"line\">3</span><br></pre></td><td class=\"code\"><pre><span class=\"line\">show table status from mytest; </span><br><span class=\"line\">or</span><br><span class=\"line\">show create table table_name</span><br></pre></td></tr></table></figure></p>\n</blockquote>\n<p>五、MyIASM 和 Innodb引擎详解</p>\n<h4 id=\"Innodb引擎\"><a href=\"#Innodb引擎\" class=\"headerlink\" title=\"Innodb引擎\"></a>Innodb引擎</h4><hr>\n<pre><code>Innodb引擎提供了对数据库ACID事务的支持,并且实现了SQL标准的四种隔离级别,关于数据库事务与其隔离级别的内容请见数据库事务与其隔离级别这篇文章。该引擎还提供了行级锁和外键约束,它的设计目标是处理大容量数据库系统,它本身其实就是基于MySQL后台的完整数据库系统,MySQL运行时Innodb会在内存中建立缓冲池,用于缓冲数据和索引。但是该引擎不支持FULLTEXT类型的索引,而且它没有保存表的行数,当SELECT COUNT(*) FROM TABLE时需要扫描全表。当需要使用数据库事务时,该引擎当然是首选。由于锁的粒度更小,写操作不会锁定全表,所以在并发较高时,使用Innodb引擎会提升效率。但是使用行级锁也不是绝对的,如果在执行一个SQL语句时MySQL不能确定要扫描的范围,InnoDB表同样会锁全表。\n</code></pre><h4 id=\"ACID(Atomicity-Consistency-Isolation-Durability)\"><a href=\"#ACID(Atomicity-Consistency-Isolation-Durability)\" class=\"headerlink\" title=\"ACID(Atomicity, Consistency, Isolation, Durability)\"></a>ACID(Atomicity, Consistency, Isolation, Durability)</h4><hr>\n<pre><code>1. A 事务的原子性(Atomicity):指一个事务要么全部执行,要么不执行.也就是说一个事务不可能只执行了一半就停止了.比如你从取款机取钱,这个事务可以分成两个步骤:1划卡,2出钱.不可能划了卡,而钱却没出来.这两步必须同时完成.要么就不完成.\n2. C 事务的一致性(Consistency):指事务的运行并不改变数据库中数据的一致性.例如,完整性约束了a+b=10,一个事务改变了a,那么b也应该随之改变.\n3. I 独立性(Isolation):事务的独立性也有称作隔离性,是指两个以上的事务不会出现交错执行的状态.因为这样可能会导致数据不一致.\n4. D 持久性(Durability):事务的持久性是指事务执行成功以后,该事务所对数据库所作的更改便是持久的保存在数据库之中,不会无缘无故的回滚.\n</code></pre><h4 id=\"MYIASM引擎\"><a href=\"#MYIASM引擎\" class=\"headerlink\" title=\"MYIASM引擎\"></a>MYIASM引擎</h4><hr>\n<pre><code>MyIASM是MySQL默认的引擎,但是它没有提供对数据库事务的支持,也不支持行级锁和外键,因此当INSERT(插入)或UPDATE(更新)数据时即写操作需要锁定整个表,效率便会低一些。不过和Innodb不同,MyIASM中存储了表的行数,于是SELECT COUNT(*) FROM TABLE时只需要直接读取已经保存好的值而不需要进行全表扫描。如果表的读操作远远多于写操作且不需要数据库事务的支持,那么MyIASM也是很好的选择。\n</code></pre><blockquote>\n<p>两种引擎的选择</p>\n</blockquote>\n<pre><code>大尺寸的数据集趋向于选择InnoDB引擎,因为它支持事务处理和故障恢复。数据库的大小决定了故障恢复的时间长短,InnoDB可以利用事务日志进行数据恢复,这会比较快。主键查询在InnoDB引擎下也会相当快,不过需要注意的是如果主键太长也会导致性能问题,关于这个问题我会在下文中讲到。大批的INSERT语句(在每个INSERT语句中写入多行,批量插入)在MyISAM下会快一些,但是UPDATE语句在InnoDB下则会更快一些,尤其是在并发量大的时候。\n</code></pre><h4 id=\"Index——索引\"><a href=\"#Index——索引\" class=\"headerlink\" title=\"Index——索引\"></a>Index——索引</h4><hr>\n<pre><code>索引(Index)是帮助MySQL高效获取数据的数据结构。MyIASM和Innodb都使用了树这种数据结构做为索引。下面我接着讲这两种引擎使用的索引结构,讲到这里,首先应该谈一下B-Tree和B+Tree。\n</code></pre>"}],"PostAsset":[],"PostCategory":[],"PostTag":[{"post_id":"cjoljzh0w00010evou78xf1nu","tag_id":"cjoljzh1000030evo5hok7vcg","_id":"cjoljzh1400060evoj3jizfrm"},{"post_id":"cjoljzh1200040evopjfeat60","tag_id":"cjoljzh1400050evolepf936x","_id":"cjoljzh1500070evotepq97mp"},{"post_id":"cjoljzh1f00080evowklnxplg","tag_id":"cjoljzh1i000b0evo3xd1jjfr","_id":"cjoljzh1o000g0evo4mio1rjd"},{"post_id":"cjoljzh1h000a0evoo5kml2lp","tag_id":"cjoljzh1n000f0evovik0m9vb","_id":"cjoljzh1q000i0evo1wir9u13"},{"post_id":"cjoljzh1h000a0evoo5kml2lp","tag_id":"cjoljzh1000030evo5hok7vcg","_id":"cjoljzh1q000j0evosz3d2p06"},{"post_id":"cjolk1spf000k0evoxdp3sndm","tag_id":"cjoljzh1000030evo5hok7vcg","_id":"cjolk2ne1000l0evoe9bqgans"}],"Tag":[{"name":"javascript","_id":"cjoljzh1000030evo5hok7vcg"},{"name":"css3","_id":"cjoljzh1400050evolepf936x"},{"name":"js","_id":"cjoljzh1i000b0evo3xd1jjfr"},{"name":"html","_id":"cjoljzh1n000f0evovik0m9vb"}]}}