├── LICENSE ├── README.md ├── app.js ├── app.json ├── app.wxss ├── pages ├── copylink │ ├── copylink.js │ ├── copylink.json │ ├── copylink.wxml │ └── copylink.wxss ├── images │ └── juewang.jpg └── index │ ├── index.js │ ├── index.json │ ├── index.wxml │ ├── index.wxss │ ├── other.wxml │ ├── recommend.wxml │ └── welfare.wxml └── tutorial ├── jgank_1.md ├── jgank_2.md └── jgank_3.md /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | ## 微信小程序 Jgank 3 | 一个 gank.io 的第三方微信小程序 4 | 5 | ![](https://pikachu666.oss-cn-hongkong.aliyuncs.com/github/Jgank/github_jgank_readme_1.png) 6 | 7 | 具体实现细节请看: 8 | [微信小程序 Jgank 开发(一)](tutorial/jgank_1.md) 9 | [微信小程序 Jgank 开发(二)](tutorial/jgank_2.md) 10 | [微信小程序 Jgank 开发(三)](tutorial/jgank_3.md) 11 | 12 | #### License 13 | ``` 14 | Copyright 2017 jianshijiuyou 15 | 16 | Licensed under the Apache License, Version 2.0 (the "License"); 17 | you may not use this file except in compliance with the License. 18 | You may obtain a copy of the License at 19 | 20 | http://www.apache.org/licenses/LICENSE-2.0 21 | 22 | Unless required by applicable law or agreed to in writing, software 23 | distributed under the License is distributed on an "AS IS" BASIS, 24 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 25 | See the License for the specific language governing permissions and 26 | limitations under the License. 27 | ``` -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jianshijiuyou/Jgank/ceeb38f909757cb9f724e5011e9523d3a3a06c56/app.js -------------------------------------------------------------------------------- /app.json: -------------------------------------------------------------------------------- 1 | { 2 | "pages": [ 3 | "pages/index/index", 4 | "pages/copylink/copylink" 5 | ], 6 | "window": { 7 | "navigationBarTitleText": "Jgank", 8 | "backgroundColor": "#088", 9 | "navigationBarBackgroundColor": "#088", 10 | "backgroundTextStyle": "dark" 11 | }, 12 | "networkTimeout": { 13 | "request": 10000, 14 | "downloadFile": 10000 15 | } 16 | } -------------------------------------------------------------------------------- /app.wxss: -------------------------------------------------------------------------------- 1 | page { 2 | height: 100%; 3 | } 4 | .theme-color{ 5 | background-color: #088; 6 | } -------------------------------------------------------------------------------- /pages/copylink/copylink.js: -------------------------------------------------------------------------------- 1 | // index.js 2 | Page({ 3 | 4 | /** 5 | * 页面的初始数据 6 | */ 7 | data: { 8 | }, 9 | 10 | /** 11 | * 生命周期函数--监听页面加载 12 | */ 13 | onLoad: function (options) { 14 | 15 | this.setData({ 16 | url: options.url 17 | }) 18 | }, 19 | copyBtnClick:function(e){ 20 | var url = e.currentTarget.dataset.url 21 | wx.setClipboardData({ 22 | data:url, 23 | success: function (res) { 24 | wx.showToast({ 25 | title: '已复制' 26 | }) 27 | } 28 | }) 29 | } 30 | }) -------------------------------------------------------------------------------- /pages/copylink/copylink.json: -------------------------------------------------------------------------------- 1 | {} -------------------------------------------------------------------------------- /pages/copylink/copylink.wxml: -------------------------------------------------------------------------------- 1 | 2 | 因小程序限制,无法直接打开原文链接 3 | 4 | 只能复制链接自己解决咯 5 | {{url}} 6 | 复制 -------------------------------------------------------------------------------- /pages/copylink/copylink.wxss: -------------------------------------------------------------------------------- 1 | view{ 2 | width: 90%; 3 | margin: 0 auto; 4 | margin-top: 20rpx; 5 | margin-bottom: 20rpx; 6 | } 7 | 8 | .btn{ 9 | text-align: center; 10 | line-height: 80rpx; 11 | background-color: #088; 12 | border-radius: 10rpx; 13 | height: 80rpx; 14 | color: #fff; 15 | } 16 | 17 | .textGray{ 18 | color: #aaa; 19 | word-wrap: break-word; 20 | } -------------------------------------------------------------------------------- /pages/images/juewang.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jianshijiuyou/Jgank/ceeb38f909757cb9f724e5011e9523d3a3a06c56/pages/images/juewang.jpg -------------------------------------------------------------------------------- /pages/index/index.js: -------------------------------------------------------------------------------- 1 | Page({ 2 | data:{ 3 | other:[], 4 | classify: [{ id: 0, name: "推荐" }, 5 | { id: 1, name: "福利" }, 6 | { id: 2, name: "Android" }, 7 | { id: 3, name: "ios" }, 8 | { id: 4, name: "休息视频" }, 9 | { id: 5, name: "拓展资源" }, 10 | { id: 6, name: "前端" }], 11 | curTab: 0 12 | }, 13 | onReady:function(){ 14 | var page = this 15 | wx.showLoading({ 16 | title: "加载中", 17 | mask: true 18 | }) 19 | wx.request({ 20 | url: encodeURI('https://gank.io/api/day/history'), 21 | method:"GET", 22 | success: function (res) { 23 | 24 | var url = "https://gank.io/api/day/"+res.data.results[0].replace(/-/g, "/") 25 | page.requestRecommend(encodeURI(url)) 26 | }, 27 | fail:function(){ 28 | wx.hideLoading() 29 | } 30 | }) 31 | }, 32 | classifyClick:function(e){ 33 | 34 | if (this.data.curTab == e.currentTarget.dataset.type) { 35 | return 36 | } 37 | 38 | var page = this 39 | 40 | this.setData({ 41 | curTab: e.currentTarget.dataset.type, 42 | curPage: 1, 43 | other:[] 44 | }) 45 | 46 | if (e.currentTarget.dataset.type==1){ 47 | //请求福利数据 48 | this.requestData("https://gank.io/api/data/福利/10/1", function (res) { 49 | wx.hideLoading() 50 | page.setData({ 51 | welfare: res.data.results 52 | }) 53 | }) 54 | } else if (e.currentTarget.dataset.type == 2){ 55 | //请求android数据 56 | this.reqeustOtherData("Android",1) 57 | } else if (e.currentTarget.dataset.type == 3) { 58 | this.reqeustOtherData("iOS", 1) 59 | } else if (e.currentTarget.dataset.type == 4) { 60 | this.reqeustOtherData("休息视频", 1) 61 | } else if (e.currentTarget.dataset.type == 5) { 62 | this.reqeustOtherData("拓展资源", 1) 63 | } else if (e.currentTarget.dataset.type == 6) { 64 | this.reqeustOtherData("前端", 1) 65 | } 66 | }, 67 | requestRecommend:function(url){ 68 | var page = this 69 | wx.request({ 70 | url: url, 71 | method: "GET", 72 | success: function (res) { 73 | wx.hideLoading() 74 | page.setData({ 75 | recommend: res.data.results 76 | }) 77 | }, 78 | fail: function () { 79 | wx.hideLoading() 80 | } 81 | }) 82 | }, 83 | imgClick:function(e){ 84 | var url = e.currentTarget.dataset.imgUrl 85 | wx.previewImage({ 86 | urls: [url] 87 | }) 88 | }, 89 | itemClick:function(e){ 90 | var url=e.currentTarget.dataset.url 91 | wx.navigateTo({ 92 | url: "../copylink/copylink?url=" + url 93 | }) 94 | }, 95 | requestData:function(url,callback){ 96 | var url = encodeURI(url) 97 | wx.showLoading({ 98 | title: "加载中", 99 | mask: true 100 | }) 101 | wx.request({ 102 | url: url, 103 | method: "GET", 104 | success: callback, 105 | fail: function () { 106 | wx.hideLoading() 107 | } 108 | }) 109 | }, 110 | reqeustOtherData:function(rtype,pageNumber){ 111 | var page=this 112 | var url = encodeURI("https://gank.io/api/data/" + rtype + "/10/" + pageNumber) 113 | 114 | wx.showLoading({ 115 | title: "加载中", 116 | mask: true 117 | }) 118 | wx.request({ 119 | url: url, 120 | method: "GET", 121 | success: function (res) { 122 | wx.hideLoading() 123 | page.setData({ 124 | other: page.data.other.concat(res.data.results), 125 | scrollTop: 0 126 | }) 127 | }, 128 | fail: function () { 129 | wx.hideLoading() 130 | } 131 | }) 132 | }, 133 | imgListClick:function(e){ 134 | var url = e.currentTarget.dataset.url 135 | 136 | var list = new Array() 137 | for (var i = 0;i 2 | 3 | {{item.name}} 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /pages/index/index.wxss: -------------------------------------------------------------------------------- 1 | .scroll-view_H{ 2 | white-space: nowrap; 3 | position: fixed; 4 | z-index: 999; 5 | } 6 | 7 | .scroll-view-item_H{ 8 | display: inline-block; 9 | line-height: 80rpx; 10 | width: 180rpx; 11 | text-align: center; 12 | } 13 | 14 | .sv-item-on{ 15 | background-color: #0aa; 16 | color: #ddd; 17 | } 18 | 19 | .sv-item-off{ 20 | background-color: #088; 21 | color: #fff; 22 | } 23 | 24 | /*common*/ 25 | .list_item{ 26 | border: 1rpx solid #ddd; 27 | margin: 15rpx; 28 | padding: 15rpx; 29 | border-radius: 10rpx; 30 | } 31 | 32 | .who{ 33 | text-align: right; 34 | color: #aaa; 35 | } 36 | 37 | .empty-head{ 38 | height: 80rpx; 39 | } 40 | 41 | /*推荐部分*/ 42 | .recommend_head{ 43 | width: 100%; 44 | height: 400rpx; 45 | margin-top: 80rpx; 46 | } 47 | 48 | .recommend_title{ 49 | display: inline-block; 50 | padding: 10rpx; 51 | margin-top: 10rpx; 52 | margin-left: 15rpx; 53 | border-radius: 10rpx; 54 | color: #fff; 55 | } 56 | 57 | /*福利*/ 58 | 59 | .welfare-img-item{ 60 | display: inline-block; 61 | width: 50%; 62 | } -------------------------------------------------------------------------------- /pages/index/other.wxml: -------------------------------------------------------------------------------- 1 | 2 | 3 | {{item.desc}} 4 | 5 | 6 | 7 | {{item.who}} 8 | -------------------------------------------------------------------------------- /pages/index/recommend.wxml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {{rItem[0].type}} 6 | 7 | {{item.desc}} 8 | 9 | 10 | 11 | {{item.who}} 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /pages/index/welfare.wxml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /tutorial/jgank_1.md: -------------------------------------------------------------------------------- 1 | ## 写在前面 2 | 最近突然想学下小程序,说不定以后会用的到,就算用不到,也能在简历上多添一笔(哈哈...)。最好的学习方式当然是以实战项目驱动,所以我打算用 [gank.io(干货集中营)](http://gank.io/)的 [api](http://gank.io/api) 数据做一个小程序,刚好界面也直接照搬了,懒得自己设计,毕竟我对自己的审美认知还是很到位的,哈哈~ 3 | 4 | 先看官方的效果 5 | ![](http://os6ycxx7w.bkt.clouddn.com/images/345cd5c1-715f-464c-8a89-1f97317f999f.gif) 6 | 7 | 整体可以看作两部分,上面是显示分类的选项卡,下面就是具体分类的内容了,然后内容又有三种布局,推荐(混合布局),福利(两列图片),其他分类(普通文字)。 8 | ## 准备 9 | 首先要先有一个微信公众平台的开发者账号,去[官网](https://mp.weixin.qq.com/)注册,账号类型选择『小程序』。 10 | > 这里要吐槽下,如果你之前用邮箱注册了订阅号的开发或者注册了微信开放平台,那就不能用这个邮箱了,意思就是如果你同时需要开发公众号,小程序,还需要使用微信开放平台(微信登录,支付,分享等等),那你就要用三个邮箱!!!不知道微信为什么要这么搞,反正作为一个用户来说,我用着很不爽。 11 | 12 | 注册完账号后,跟着提示一步一做就行了,最后就是下载微信的小程序开发工具进行开发。 13 | 14 | 上面的都搞定了后就开始看官方文档吧,文档还是挺详细的,把 简易教程,框架,先看一遍,组件 和 API 打开看下有哪些东西,看完后就可以开始撸代码了(文档不是很多啦,不要虚)。 15 | ## 开始 16 | 首先做一下全局的配置信息。 17 | 样式只有一个,就是所有页面高度百分百: 18 | app.wxss 19 | ``` css 20 | page { 21 | height: 100%; 22 | } 23 | ``` 24 | 然后是主题颜色,页面等信息的配置: 25 | app.json 26 | ``` json 27 | { 28 | "pages": [ 29 | "pages/index/index" 30 | ], 31 | "window": { 32 | "navigationBarTitleText": "Jgank", 33 | "backgroundColor": "#088", 34 | "navigationBarBackgroundColor": "#088", 35 | "backgroundTextStyle": "dark" 36 | }, 37 | "networkTimeout": { 38 | "request": 10000, 39 | "downloadFile": 10000 40 | }, 41 | "debug": true 42 | } 43 | ``` 44 | 从配置信息可以看出,我们有一个页面 `pages/index/index`。 45 | 46 | ## 选项卡 47 | 先完成上面的选项卡部分,选项卡是一个横向的可以拖动的列表,看下官方文档的组件部分可以发现,`scroll-view` 可以实现该效果,看下代码: 48 | index.wxml 49 | ``` xml 50 | 51 | 52 | {{item.name}} 53 | 54 | 55 | ``` 56 | 对应的样式: 57 | index.wxss 58 | ``` css 59 | .scroll-view_H{ 60 | white-space: nowrap; 61 | } 62 | 63 | .scroll-view-item_H{ 64 | display: inline-block; 65 | line-height: 80rpx; 66 | width: 180rpx; 67 | text-align: center; 68 | } 69 | 70 | .sv-item-on{ 71 | background-color: #0aa; 72 | color: #ddd; 73 | } 74 | 75 | .sv-item-off{ 76 | background-color: #088; 77 | color: #fff; 78 | } 79 | ``` 80 | 对应的业务逻辑; 81 | index.js 82 | ``` javascript 83 | Page({ 84 | data:{ 85 | classify: [{ id: 0, name: "推荐" }, 86 | { id: 1, name: "福利" }, 87 | { id: 2, name: "Android" }, 88 | { id: 3, name: "ios" }, 89 | { id: 4, name: "休息视频" }, 90 | { id: 5, name: "拓展资源" }, 91 | { id: 6, name: "前端" }], 92 | curTab: 0 93 | }, 94 | classifyClick:function(e){ 95 | //判断如果点击的是当前的选项卡则不做任何处理 96 | if (this.data.curTab == e.currentTarget.dataset.type){ 97 | return 98 | } 99 | console.log("切换") 100 | this.setData({ 101 | curTab: e.currentTarget.dataset.type 102 | }) 103 | } 104 | }) 105 | ``` 106 | `classify` 就是所有的分类,用来和页面进行数据绑定,`curTab` 用来记录当前选项卡的 `id` ,`classifyClick` 用来监听选项卡的点击事件,点击不同的选项卡,就修改 `curTab` 为对应的值,页面上通过判断 `curTab` 的变化来显示不同的内容。 107 | 所以页面上再添加如下代码: 108 | index.wxml 109 | ``` xml 110 | 111 | 112 | {{item.name}} 113 | 114 | 115 | 116 | {{classify[curTab].name}} 117 | {{classify[curTab].name}} 118 | {{classify[curTab].name}} 119 | {{classify[curTab].name}} 120 | {{classify[curTab].name}} 121 | {{classify[curTab].name}} 122 | {{classify[curTab].name}} 123 | ``` 124 | 下面看下效果: 125 | ![](http://os6ycxx7w.bkt.clouddn.com/images/0c754b1a-0a9b-472a-a7cc-9d46ec36e848.gif) 126 | 127 | 这次就先完成选项卡功能,内容部分下次在说。 128 | -------------------------------------------------------------------------------- /tutorial/jgank_2.md: -------------------------------------------------------------------------------- 1 | 2 | ## 开始 3 | 4 | 今天完成了 推荐列表页面 和 复制链接页面,先看下效果吧: 5 | ![](http://os6ycxx7w.bkt.clouddn.com/github/Jgank/jgank_2_1.gif) 6 | 7 | ## 数据 8 | 9 | 先说数据部分,推荐列表的数据接口是 `https://gank.io/api/day/2017/08/31`,后面的日期可以改变的,想获取哪一天的推荐就用哪一天的日期。 10 | > 这里要说下,并不是每一天的日期都有数据返回哟,所以要先用接口 `https://gank.io/api/day/history` 获取到有数据的日期列表,然后用最新的日期去获取最新的推荐数据。 11 | >(吐槽)奇葩的接口设计,难道服务器就不能自动返回距离请求日期最近的日期推荐数据吗,还是说只是我没发现这个接口~ 12 | 13 | 我是在页面初次渲染完成时请求数据的: 14 | 先请求有数据的日期列表 15 | ``` javascript 16 | onReady:function(){ 17 | var page = this 18 | wx.showLoading({ 19 | title: "加载中", 20 | mask: true 21 | }) 22 | wx.request({ 23 | url: encodeURI('https://gank.io/api/day/history'), 24 | method:"GET", 25 | success: function (res) { 26 | 27 | var url = "https://gank.io/api/day/"+res.data.results[0].replace(/-/g, "/") 28 | page.requestRecommend(encodeURI(url)) 29 | }, 30 | fail:function(){ 31 | wx.hideLoading() 32 | } 33 | }) 34 | } 35 | ``` 36 | 再请求具体日期的推荐数据: 37 | ``` javascript 38 | requestRecommend:function(url){ 39 | var page = this 40 | wx.request({ 41 | url: url, 42 | method: "GET", 43 | success: function (res) { 44 | wx.hideLoading() 45 | page.setData({ 46 | recommend: res.data.results 47 | }) 48 | }, 49 | fail: function () { 50 | wx.hideLoading() 51 | } 52 | }) 53 | } 54 | ``` 55 | 把请求到的数据设置到 data 中,页面根据数据再渲染: 56 | 57 | ## 视图 58 | 视图我单独放到一个 wxml 文件中,在主页面再 include 进来,这样看起来舒服点。 59 | 60 | 推荐页可以看成两部份,最上面的福利图和下面的各大分类板块,而各大板块的布局是一样的,所以一个 for 循环就搞定了: 61 | ``` xml 62 | 63 | 64 | 65 | 66 | {{rItem[0].type}} 67 | 68 | {{item.desc}} 69 | 70 | 71 | 72 | {{item.who}} 73 | 74 | 75 | 76 | ``` 77 | 各大板块用的双 for 循环,外层循环分类,内层循环具体类别的列表,而分类的标题是通过类别第一项数据的 type 项获得的。 78 | > 注意,这里要做一下判断,把福利分类去除掉,不显示,因为福利分类我们只要那张图,且要放在最上面。 79 | 80 | 然后就是设置图片和列表的点击事件,当然是用微信的 api 查看图片,点击列表是跳转到复制链接的页面,因为小程序是无法打开网页链接的。 81 | **注意,图片的点击事件用的是 `catchtap` 而不是 `bindtap` ,如果用后者,点击图片后不仅会触发图片的点击事件,还会触发列表的点击事件,所以这里用前者可以把事件拦截下来,禁止继续往外冒泡传递。** 82 | > 小程序不能打开网页链接.....难道是怕别人再用小程序做个小小程序吗....哈哈 83 | 84 | ``` javascript 85 | imgClick:function(e){ 86 | var url = e.currentTarget.dataset.imgUrl 87 | wx.previewImage({ 88 | urls: [url] 89 | }) 90 | }, 91 | itemClick:function(e){ 92 | var url=e.currentTarget.dataset.url 93 | wx.navigateTo({ 94 | url: "../copylink/copylink?url=" + url 95 | }) 96 | } 97 | ``` 98 | 99 | 样式部分的代码就不贴上来了,反正就是 css 那些东西,至于复制链接的页面也就一个复制的功能,大家自家看就好啦,今天的源码我打了一个 tag 方便大家查看。 100 | 101 | 代码地址:[https://github.com/jianshijiuyou/Jgank/tree/v0.1](https://github.com/jianshijiuyou/Jgank/tree/v0.1) 102 | -------------------------------------------------------------------------------- /tutorial/jgank_3.md: -------------------------------------------------------------------------------- 1 | 今天再水最后一篇,福利列表和其他页面的完成,其他的页面可以看成一类页面,就是单一的图文列表。 2 | 3 | ## 福利页面 4 | 试图上很简单,就一个 scroll-view 而已: 5 | ``` xml 6 | 7 | 8 | 9 | ``` 10 | 因为要显示成两列,所以需要把图片的宽度设置成 50% ,这样每行就是两张图片了,具体操作可以看源码。 11 | 12 | ## 其他页面 13 | 剩下的页面布局中的图文和推荐页面的图文实际是一样的布局,所以可以直接照搬: 14 | ``` xml 15 | 16 | {{item.desc}} 17 | 18 | 19 | 20 | {{item.who}} 21 | 22 | ``` 23 | ## 分页 24 | 分页是真的遇到坑了,分页其实有两种方法,一种是用 scroll-view 的 bindscrolltolower 事件触发滚动到底部刷新,一种是用 page 的 onReachBottom 事件触发滚动到底部刷新。 25 | 26 | 如果是用 bindscrolltolower ,必须要给 scroll-view 设置个固定高度,不然无法触发事件,也只有设置了固定高度后,scroll-top 属性才有效,但但但是,我在测试的时候 bindscrolltolower 事件会多次触发,所以我最后选择用 onReachBottom 了。 27 | 28 | 如果用了 scroll-view 并设置了固定高度,是不会触发 onReachBottom 事件的....,所以我最后直接用的最简单的 view 控件的 循环。 29 | 30 | 请求分页的时候用一个变量 curPage 来记录当前页面共请求了几页数据,每次请求刷新,就给 curPage 加 1 ,当页面切换时 curPage 重新设置为 1。 31 | 32 | 福利页的数据请求和其他页的数据请求,我是分开的,因为两者对应的视图上绑定的变量是不一样的。 33 | 34 | 这里就只放上请求其他页数据的代码: 35 | rtype:请求的类型,是 android 还是 ios,前端等... 36 | pageNumber:请求的页数 37 | ``` javascript 38 | reqeustOtherData:function(rtype,pageNumber){ 39 | var page=this 40 | var url = encodeURI("https://gank.io/api/data/" + rtype + "/10/" + pageNumber) 41 | 42 | wx.showLoading({ 43 | title: "加载中", 44 | mask: true 45 | }) 46 | wx.request({ 47 | url: url, 48 | method: "GET", 49 | success: function (res) { 50 | wx.hideLoading() 51 | page.setData({ 52 | other: page.data.other.concat(res.data.results) 53 | }) 54 | }, 55 | fail: function () { 56 | wx.hideLoading() 57 | } 58 | }) 59 | } 60 | ``` 61 | 62 | 最后看下整个效果吧 63 | ![](http://os6ycxx7w.bkt.clouddn.com/github/Jgank/github_jgank_readme_1.png) 64 | --------------------------------------------------------------------------------