一:記賬小應用var util = require("../../utils/util.js");//獲取應用實例var app = getApp();Page({ data: { userInfo: {}, buttonLoading: false, accountData:, accountTotal:0 }, ...
var util = require("../../utils/util.js");
//獲取應用實例
var app = getApp();
Page({
data: {
useo?rInfo: {},
buttonLoading: falseo?,
accountData:[],
accountTotal:0
},
onLoad: function () {
console.log('onLoad')
var that = this;
// 獲取記錄
var tempAccountData = wx.getStorageSync("accountData") || [];
this.caculateTotal(tempAccountData);
this.setData({
accountData: tempAccountData
});
},
// 計算總額
caculateTotal:function(data){
var tempTotal = 0;
for(var x in data){
tempTotal += parseFloat(data[x].amount);
}
this.setData({
accountTotal: tempTotal
});
},
//表單提交
formSubmit:function(e){
this.setData({
buttonLoading: true
});
var that = this;
setTimeout(function(){
var inDetail = e.detail.value.inputdetail;
var inAmount = e.detail.value.inputamount;
if(inDetail.toString().length <= 0 || inAmount.toString().length <= 0){
console.log("can not empty");
that.setData({
buttonLoading: false
});
return false;
}
//新增記錄
var tempAccountData = wx.getStorageSync("accountData") || [];
tempAccountData.unshift({detail:inDetail,amount:inAmount});
wx.setStorageSync("accountData",tempAccountData);
that.caculateTotal(tempAccountData);
that.setData({
accountData: tempAccountData,
buttonLoading: false
});
},1000);
},
//刪除行
deleteRow: function(e){
var that = this;
var index = e.target.dataset.indexKey;
var tempAccountData = wx.getStorageSync("accountData") || [];
tempAccountData.splice(index,1);
wx.setStorageSync("accountData",tempAccountData);
that.caculateTotal(tempAccountData);
that.setData({
accountData: tempAccountData,
});
}
})
項目地址:https://github.com/HowName/account-note項目下載:account-note-master.zip
項目為仿今日頭條,使用了百度ApiStore接口查詢數據,使用微信組件/api有 封裝請求方法,底部tab,啟動頁動畫,loading,scroll-view,swiper,列表頁支持上下拉加載更多
效果圖:
啟動歡迎頁,幾行代碼可實現旋轉與縮放:
//flash.js
onReady:function(){
// 頁面渲染完成
var that = this,duration = 1500;
var animation = wx.createAnimation({
duration: duration,
});
//step() 方法表示一組動畫的結束
animation.scale(2).rotate(360).step();
animation.scale(1).step();
this.setData({
animationData : animation.export()
});
var timestamp = new Date().getTime();
setTimeout(function(){
wx.redirectTo({
url: '../index/index?time='+timestamp
})
},duration*2.5);
},
//flash.wxml
<image class="flash-img" animation="{{animationData}}" src="{{src}}" ></image>
網絡請求方法:
//app.js
req: function(url,data,param){
var requestData = {
url: url,
data: typeof data == 'object' ? data : {},
method: typeof param.method == 'string' && param.method.length > 0 ? param.method.toUpperCase() : 'GET',
header: typeof param.header == 'object' ? param.header : {},
success: function(res) {
typeof param.success == 'function' && param.success(res);
},
fail: function(res){
typeof param.fail == 'function' && param.fail(res);
},
complete: function(res){
typeof param.complete == 'function' && param.complete(res);
}
};
wx.request(requestData);
},
列表頁:
//index.js
var app = getApp(),currentPage = 1;
const URL = "http://apis.baidu.com/showapi_open_bus/channel_news/search_news";
Page({
data:{
imgUrls: [
'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
],
toView: "",
loadingHidden:true,
renderData:[],
},
onLoad:function(options){
this.loadDataFromServer();
},
//api讀取數據
loadDataFromServer: function(){
var that = this;
that.changeLoadingStatus(false);
app.req(URL,{
page : currentPage,
needContent : 1,
},{
header: { apikey: app.globalData.apikey },
success:function(resp){
console.log(resp);
console.log("成功加載頁數 "+currentPage);
var tempData = resp.data.showapi_res_body.pagebean.contentlist;
var toViewId = currentPage % 2 == 0 ? "top-id" : "top-id2"; //需要改變值頁面才會重新渲染
that.setData({
//renderData: that.data.renderData.concat(tempData), 合并數組容易超出長度,無法做到無限加載
renderData: tempData,
toView: toViewId,
});
that.changeLoadingStatus(true);
}
});
},
//加載上一頁或者下拉刷新
refresh:function(e){
currentPage = currentPage > 1 ? --currentPage : 1;
this.loadDataFromServer();
},
//加載下一頁
loadMore:function(e){
++currentPage;
this.loadDataFromServer();
},
//改變loading狀態(tài)
changeLoadingStatus: function(bool){
this.setData({
loadingHidden: bool
});
},
onReady:function(){
// 頁面渲染完成
wx.setNavigationBarTitle({
title: '列表'
});
},
onShow:function(){
// 頁面顯示
},
onHide:function(){
// 頁面隱藏
},
onUnload:function(){
// 頁面關閉
}
});
//index.wxml
<loading hidden="{{loadingHidden}}">
加載中...
</loading>
<scroll-view scroll-y="true" style="height: 100%;" scroll-into-view="{{toView}}" upper-threshold="5" lower-threshold="5" bindscrolltoupper="refresh" bindscrolltolower="loadMore">
<swiper indicator-dots="true" id="swiper-view" autoplay="true" interval="2000">
<block wx:for="{{imgUrls}}">
<swiper-item>
<image src="{{item}}" width="100%" height="150"/>
</swiper-item>
</block>
</swiper>
<view id="top-id"> </view>
<view id="top-id2"> </view>
<block wx:for="{{renderData}}">
<view class="container">
<view class="title">
<text class="title-text">{{item.title}}</text>
</view>
<view class="images" wx:if="{{item.imageurls.length > 0}}">
<block wx:for="{{item.imageurls}}" wx:for-item="imgItem" wx:for-index="imgIndex">
<image wx:if="{{imgIndex <= 2}}" src="{{imgItem.url}}"></image>
</block>
</view>
<view class="source">{{item.source}} {{item.pubDate}}</view>
</view>
</block>
</scroll-view>
項目地址:https://github.com/HowName/toutiao項目下載:toutiao-master.zip
項目為智能應答機器人,使用了圖靈機器人接口,慢慢調戲吧
首頁,主要處理頁:
//index.js
var app = getApp();
var that;
var chatListData = [];
Page({
data: {
askWord: '',
userInfo: {},
chatList: [],
},
onLoad: function () {
that = this;
//獲取用戶信息
app.getUserInfo(function (userInfo) {
that.setData({
userInfo: userInfo
});
});
},
onReady: function () {
//問候語
setTimeout(function () {
that.addChat('你好啊!', 'l');
}, 1000);
},
sendChat: function (e) {
let word = e.detail.value.ask_word ? e.detail.value.ask_word : e.detail.value;//支持兩種提交方式
that.addChat(word, 'r');
//請求api獲取回答
app.req('post', 'openapi/api', {
'data': { 'info': word, 'loc': '廣州', 'userid': '123' },
'success': function (resp) {
that.addChat(resp.text, 'l');
if (resp.url) {
that.addChat(resp.url, 'l');
}
},
});
//清空輸入框
that.setData({
askWord: ''
});
},
//新增聊天列表
addChat: function (word, orientation) {
let ch = { 'text': word, 'time': new Date().getTime(), 'orientation': orientation };
chatListData.push(ch);
that.setData({
chatList: chatListData
});
}
})
頁面:
//index.wxml
<view class="container">
<scroll-view class="scrool-view" scroll-y="true">
<view class="chat-list">
<block wx:for="{{chatList}}" wx:key="time">
<view class="chat-left" wx:if="{{item.orientation == 'l'}}">
<image class="avatar-img" src="../../res/image/wechat-logo.png"></image>
<text>{{item.text}}</text>
</view>
<view class="chat-right" wx:if="{{item.orientation == 'r'}}">
<text>{{item.text}}{{item.url}}</text>
<image class="avatar-img" src="{{userInfo.avatarUrl}}"></image>
</view>
</block>
</view>
</scroll-view>
<form bindsubmit="sendChat">
<view class="ask-input-word">
<input placeholder="" name="ask_word" type="text" bindconfirm="sendChat" value="{{askWord}}" />
<button formType="submit" size="mini">發(fā)送</button>
</view>
</form>
</view>
網絡請求方法:
//app.js
req: function (method, url, arg) {
let domian = 'http://www.tuling123.com/', data = { 'key': '9d2ff29d44b54e55acadbf5643569584' }, dataType = 'json';//為方便廣大群眾,提供key
let header = { 'content-type': 'application/x-www-form-urlencoded' };
if (arg.data) {
data = Object.assign(data, arg.data);
}
if (arg.header) {
header = Object.assign(header, arg.header);
}
if (arg.dataType) {
dataType = arg.dataType;
}
let request = {
method: method.toUpperCase(),
url: domian + url,
data: data,
dataType: dataType,
header: header,
success: function (resp) {
console.log('response content:', resp.data);
let data = resp.data;
typeof arg.success == "function" && arg.success(data);
},
fail: function () {
wx.showToast({
title: '請求失敗,請稍后再試',
icon: 'success',
duration: 2000
});
typeof arg.fail == "function" && arg.fail();
},
complete: function () {
typeof arg.complete == "function" && arg.complete();
}
};
wx.request(request);
}
項目地址:https://github.com/HowName/smart-robot項目下載:smart-robot-master.zip