Skip to content

Commit

Permalink
add demo for miniprogram and wepy
Browse files Browse the repository at this point in the history
  • Loading branch information
Delong Zhu committed Dec 17, 2017
1 parent 648b177 commit 4df23dd
Show file tree
Hide file tree
Showing 130 changed files with 20,089 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
node_modules/
example/node_modules/
example4wepy/node_modules/
2 changes: 1 addition & 1 deletion Rx.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion RxWX.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions example/README.md
@@ -0,0 +1,3 @@
RxWX小程序示例

示例代码目录: pages/demo
39 changes: 39 additions & 0 deletions example/app.js
@@ -0,0 +1,39 @@
//app.js
App({
onLaunch: function () {
// 展示本地存储能力
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)

// 登录
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
}
})
// 获取用户信息
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: res => {
// 可以将 res 发送给后台解码出 unionId
this.globalData.userInfo = res.userInfo

// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
if (this.userInfoReadyCallback) {
this.userInfoReadyCallback(res)
}
}
})
}
}
})
},
globalData: {
userInfo: null
}
})
12 changes: 12 additions & 0 deletions example/app.json
@@ -0,0 +1,12 @@
{
"pages":[
"pages/index/index",
"pages/logs/logs"
],
"window":{
"backgroundTextStyle":"light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "WeChat",
"navigationBarTextStyle":"black"
}
}
10 changes: 10 additions & 0 deletions example/app.wxss
@@ -0,0 +1,10 @@
/**app.wxss**/
.container {
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
padding: 200rpx 0;
box-sizing: border-box;
}
26 changes: 26 additions & 0 deletions example/pages/demo/combine.js
@@ -0,0 +1,26 @@
/**
* 合并函数结果
*/

import rxwx, {Rx} from '../../utils/RxWX.js'
// 调用小程序API
let getUser = new Promise((success, fail) => {
wx.getUserInfo({
success,
fail
})
})
let getSystem = new Promise((success, fail) => {
wx.getSystemInfo({
success,
fail
})
})

Promise.all([getUser, getSystem])
.then((resp) => console.log(resp), e => console.error(e))

// 调用RxWX
Rx.Observable.zip(rxwx.getUserInfo(), rxwx.getSystemInfo())
.catch(e => console.error(e))
.subscribe(resp => console.log(resp))
25 changes: 25 additions & 0 deletions example/pages/demo/nest.js
@@ -0,0 +1,25 @@
/**
* 嵌套调用
*/
import rxwx from '../../utils/RxWX.js'
// 调用小程序API
wx.login({
success(res) {
wx.getUserInfo({
success(res) {
console.log(res.userInfo)
},
fail(e) {
console.error(e)
}
})
},
fail(e) {
console.error(e)
}
})
// 调用RxWX
rxwx.login()
.switchMap(() => rxwx.getUserInfo())
.catch(e => console.error(e))
.subscribe(res => console.log(res.userInfo))
34 changes: 34 additions & 0 deletions example/pages/demo/sync_async.js
@@ -0,0 +1,34 @@
/**
* 同步
*/
import rxwx from '../../utils/RxWX.js'

// 调用小程序API
try {
let result = wx.removeStorageSync('xx')
console.log(result)
} catch (e) {
console.error('小程序API发现错误')
}
// 调用RxWX
rxwx.removeStorageSync('xx')
.catch((e) => console.error('RxWX发现错误'))
.subscribe((resp) => console.log(resp))

/**
* 异步
*/
// 调用小程序API
wx.removeStorage({
key: 'xx',
success: function (res) {
console.log(res)
},
error: function (e) {
console.error('小程序API发现错误')
}
})
// 调用RxWX
rxwx.removeStorage({ key: 'xx' })
.catch((e) => console.error('RxWX发现错误'))
.subscribe((resp) => console.log(resp))
58 changes: 58 additions & 0 deletions example/pages/index/index.js
@@ -0,0 +1,58 @@
import a from '../demo/sync_async.js'
import b from '../demo/nest.js'
import c from '../demo/combine.js'

//index.js
//获取应用实例
const app = getApp()

Page({
data: {
motto: 'Hello World',
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo')
},
//事件处理函数
bindViewTap: function() {
wx.navigateTo({
url: '../logs/logs'
})
},
onLoad: function () {
if (app.globalData.userInfo) {
this.setData({
userInfo: app.globalData.userInfo,
hasUserInfo: true
})
} else if (this.data.canIUse){
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
app.userInfoReadyCallback = res => {
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
} else {
// 在没有 open-type=getUserInfo 版本的兼容处理
wx.getUserInfo({
success: res => {
app.globalData.userInfo = res.userInfo
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
})
}
},
getUserInfo: function(e) {
console.log(e)
app.globalData.userInfo = e.detail.userInfo
this.setData({
userInfo: e.detail.userInfo,
hasUserInfo: true
})
}
})
13 changes: 13 additions & 0 deletions example/pages/index/index.wxml
@@ -0,0 +1,13 @@
<!--index.wxml-->
<view class="container">
<view class="userinfo">
<button wx:if="{{!hasUserInfo && canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 获取头像昵称 </button>
<block wx:else>
<image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" background-size="cover"></image>
<text class="userinfo-nickname">{{userInfo.nickName}}</text>
</block>
</view>
<view class="usermotto">
<text class="user-motto">{{motto}}</text>
</view>
</view>
21 changes: 21 additions & 0 deletions example/pages/index/index.wxss
@@ -0,0 +1,21 @@
/**index.wxss**/
.userinfo {
display: flex;
flex-direction: column;
align-items: center;
}

.userinfo-avatar {
width: 128rpx;
height: 128rpx;
margin: 20rpx;
border-radius: 50%;
}

.userinfo-nickname {
color: #aaa;
}

.usermotto {
margin-top: 200px;
}
15 changes: 15 additions & 0 deletions example/pages/logs/logs.js
@@ -0,0 +1,15 @@
//logs.js
const util = require('../../utils/util.js')

Page({
data: {
logs: []
},
onLoad: function () {
this.setData({
logs: (wx.getStorageSync('logs') || []).map(log => {
return util.formatTime(new Date(log))
})
})
}
})
3 changes: 3 additions & 0 deletions example/pages/logs/logs.json
@@ -0,0 +1,3 @@
{
"navigationBarTitleText": "查看启动日志"
}
6 changes: 6 additions & 0 deletions example/pages/logs/logs.wxml
@@ -0,0 +1,6 @@
<!--logs.wxml-->
<view class="container log-list">
<block wx:for="{{logs}}" wx:for-item="log">
<text class="log-item">{{index + 1}}. {{log}}</text>
</block>
</view>
8 changes: 8 additions & 0 deletions example/pages/logs/logs.wxss
@@ -0,0 +1,8 @@
.log-list {
display: flex;
flex-direction: column;
padding: 40rpx;
}
.log-item {
margin: 10rpx;
}
28 changes: 28 additions & 0 deletions example/project.config.json
@@ -0,0 +1,28 @@
{
"description": "项目配置文件。",
"setting": {
"urlCheck": false,
"es6": true,
"postcss": true,
"minified": true,
"newFeature": true
},
"compileType": "miniprogram",
"libVersion": "1.7.0",
"appid": "wx96b37adf7f3506a7",
"projectname": "rxwx-example",
"condition": {
"search": {
"current": -1,
"list": []
},
"conversation": {
"current": -1,
"list": []
},
"miniprogram": {
"current": -1,
"list": []
}
}
}
1 change: 1 addition & 0 deletions example/utils/Rx.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions example/utils/RxWX.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions example/utils/util.js
@@ -0,0 +1,19 @@
const formatTime = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()

return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}

module.exports = {
formatTime: formatTime
}
1 change: 1 addition & 0 deletions example4wepy/.wepycache
@@ -0,0 +1 @@
{"/app/node_modules/wepy/lib/wepy.js":1513479571246,"/app/node_modules/wepy-async-function/index.js":1513479536089,"/app/node_modules/wepy-com-toast/toast.wpy":1513479536211,"/app/node_modules/rxjs-wx/RxWX.js":1513479533008,"/app/node_modules/wepy/lib/app.js":1513479571172,"/app/node_modules/wepy/lib/page.js":1513479571254,"/app/node_modules/wepy/lib/component.js":1513479571214,"/app/node_modules/wepy/lib/event.js":1513479571165,"/app/node_modules/wepy/lib/base.js":1513479571172,"/app/node_modules/wepy/lib/util.js":1513479571302,"/app/node_modules/wepy/lib/mixin.js":1513479571223,"/app/node_modules/wepy-async-function/global.js":1513479536089,"/app/node_modules/promise-polyfill/promise.js":1513479530126,"/app/node_modules/regenerator-runtime/runtime.js":1513479531228,"/app/node_modules/rxjs-wx/Rx.js":1513479533070,"/app/node_modules/wepy/lib/native.js":1513479571199}
4 changes: 4 additions & 0 deletions example4wepy/README.md
@@ -0,0 +1,4 @@
RxWX在wepy中使用的示例
示例代码:
src/app.wpy
src/pages/index.wpy
9 changes: 9 additions & 0 deletions example4wepy/demo/.editorconfig
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
1 change: 1 addition & 0 deletions example4wepy/demo/.eslintignore
@@ -0,0 +1 @@
dist/*

0 comments on commit 4df23dd

Please sign in to comment.