小程式開發備忘
阿新 • • 發佈:2018-11-30
相關資料
小程式簡易教程 https://developers.weixin.qq.com/miniprogram/dev/ 框架介紹 https://developers.weixin.qq.com/miniprogram/dev/framework/MINA.html 元件介紹 https://developers.weixin.qq.com/miniprogram/dev/component/ Api https://developers.weixin.qq.com/miniprogram/dev/api/PART1 構成
小程式包含一個描述整體程式的 app 和多個描述各自頁面的 page。檔案 | 必需 | 作用 |
---|---|---|
app.js | 是 | 小程式邏輯(配置第一次開啟時, 崩潰,後臺切換時需要做的邏輯處理) |
app.json | 是 | 小程式公共配置 (小程式有哪些頁面page, 視窗的背景/顏色, 超時設定等) |
app.wxss | 否 | 小程式公共樣式表 |
檔案型別 | 必需 | 作用 |
---|---|---|
js | 是 | 頁面邏輯 |
wxml | 是 | 頁面結構 |
json | 否 | 頁面配置 |
wxss | 否 | 頁面樣式表 |
比如整個小程式所有的page的路徑資訊需要配置到app.json中,舉例如下:
17 17 1{ "pages": [ "pages/login/login", "pages/index/index", "pages/order/order", "pages/solution/solution", "pages/personal/personal", "pages/navigation/navigation", "pages/solutionChart/solutionChart" ], "window": { "backgroundTextStyle": "light", "navigationBarBackgroundColor": "#fff", "navigationBarTitleText": "WeChat", "navigationBarTextStyle": "black" } }
{
2
"pages": [
3
"pages/login/login",
4
"pages/index/index",
5
"pages/order/order",
6
"pages/solution/solution",
7
"pages/personal/personal",
8
"pages/navigation/navigation",
9
"pages/solutionChart/solutionChart"
10
],
11
"window": {
12
"backgroundTextStyle": "light",
13
"navigationBarBackgroundColor": "#fff",
14
"navigationBarTitleText": "WeChat",
15
"navigationBarTextStyle": "black"
16
}
17
}
"pages" #用於描述當前小程式所有頁面路徑,這是為了讓微信客戶端知道當前你的小程式頁面定義在哪個目錄。
用於指定小程式由哪些頁面組成,每一項都對應一個頁面的 路徑+檔名 資訊。檔名不需要寫檔案字尾,框架會自動去尋找對於位置的 .json
, .js
, .wxml
, .wxss
四個檔案進行處理。
陣列的第一項代表小程式的初始頁面(首頁)。小程式中新增/減少頁面,都需要對 pages 陣列進行修改。
如開發目錄為:
├── app.js #整個小程式的通用配置(配置第一次開啟時, 崩潰,後臺切換時需要做的邏輯處理) ├── app.json #小程式公共配置 (小程式有哪些頁面page, 視窗的背景/顏色, 超時設定等) ├── app.wxss #小程式整體通用公共樣式表 ├── pages #小程式具體的各個子頁面目錄 │ │── index #一個小程式page對應以下4個檔案組成 │ │ ├── index.wxml #相當於web的html頁面 │ │ ├── index.js
#控制頁面的邏輯
│ │ ├── index.json #配置本頁面的標題,背景等 │ │ └── index.wxss #頁面的個性化樣式 │ └── logs │ ├── logs.wxml │ └── logs.js └── utils #使用者自定義用於存放工具類
└── images #使用者自定義存放圖片路徑
則需要在 app.json 中寫
{
"pages":[
"pages/index/index",//這裡對應的就是pages/index/index.wxml頁面
"pages/logs/logs"
]
}
"window" #定義小程式所有頁面的頂部背景顏色,文字顏色定義等
app.json具體配置項見https://developers.weixin.qq.com/miniprogram/dev/framework/config.html
PART2 邏輯層和檢視層
以資料繫結的程式為例 https://developers.weixin.qq.com/s/HofnzKmb6fZe index目錄就是一個具體的頁面, 下面有js,wxml,wxss等檔案用來描述index.wxml這個頁面 app.json中的pages配置了index這個頁面{
"pages":[
"index/index"
],
"window":{
"backgroundTextStyle":"light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "WeChat",
"navigationBarTextStyle":"black"
}
}
12
12
1
{
2
"pages":[
3
"index/index"
4
],
5
"window":{
6
"backgroundTextStyle":"light",
7
"navigationBarBackgroundColor": "#fff",
8
"navigationBarTitleText": "WeChat",
9
"navigationBarTextStyle":"black"
10
}
11
}
12
開啟
index.wxml頁面
<!--index.wxml-->
<
view
>
{{text}}
</
view
>
<
button
bindtap
=
"changeText">
Change normal data
</
button
>
<
view
>
{{num}}
</
view
>
<
button
bindtap
=
"changeNum">
Change normal num
</
button
>
<
view
>
{{array[0].text}}
</
view
>
<
button
bindtap
=
"changeItemInArray">
Change Array data
</
button
>
<
view
>
{{object.text}}
</
view
>
<
button
bindtap
=
"changeItemInObject">
Change Object data
</
button
>
<
view
>
{{newField.text}}
</
view
>
<
button
bindtap
=
"addNewField">
Add new data
</
button
>
其中<view> 和<button>等分別是小程式提供的檢視元件和表單元件, 可以理解為html中的標籤 可直接使用並配置相關引數(事件,大小等屬性), 並由小程式渲染.
index.js 程式碼如下
// pages/index/index.js
Page({
/**
* 頁面的初始資料
*/
data: {
array: ['當天', '本月', '近三月','近半年','本年'],
index: 0,
date: '2016-09-01',
time: '12:01',
barItem: [
{
name: '利潤彙總',
image: '../../images/menu1_unfocus.png',
selectedImage: '../../images/menu1_focus.png',
selected: false,
color: '#666666',
url: '/pages/solution/solution'
},
{
name: '每日營業情況',
image: '../../images/menu3_unfocus.png',
selectedImage: '../../images/menu3_focus.png',
selected: true,
color:'#FF0000',
url: '/pages/index/index'
},
{
name: '運營支出',
image: '../../images/menu2_unfocus.png',
selectedImage: '../../images/menu2_focus.png',
selected: false,
color: '#666666',
url: '/pages/order/order'
},
// {
// name: '',
// image: '../../images/menu4_unfocus.png',
// selectedImage: '../../images/menu4_focus.png',
// selected: false,
// url: '/pages/personal/personal'
// },
],
successresponseData: [],
datalist:[]
},
//更新日期選型,重新整理取值
bindPickerChange: function (e) {
var that = this;
console.log('picker傳送選擇改變,攜帶值為', e.detail.value)
this.setData({
index: e.detail.value
})
wx.request({
url: require('../../config').getnumUrl,
//url: 'http://localhost:8081/framework-protal/main/gas/getsale',
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
data: {
gasid: wx.getStorageSync('gasid'),
type: e.detail.value
},
success: function (res) {
console.log('success')
console.log(res.data)
that.setData({
numresponseData: res.data.response,
})
},
fail: function (res) {
console.log('fail')
}
})
wx.request({
url: require('../../config').getsaleUrl,
//url: 'http://localhost:8081/framework-protal/main/gas/getsale',
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
data: { gasid: wx.getStorageSync('gasid') },
success: function (res) {
console.log('success')
console.log(res.data)
that.setData({
responseData: res.data.response,
})
var gasnameArr = wx.getStorageSync('gasname');
var gasname;
if (gasnameArr.length != 0 && gasnameArr != undefined) {
gasname = gasnameArr.join(",");
if (gasname.length > 10) {
gasname = gasname.substring(0, 10) + "...";
}
}
that.setData({
gasname: gasname,
gasnameArr: wx.getStorageSync('gasname'),
})
},
fail: function (res) {
console.log('fail')
}
})
},
/**
* 生命週期函式--監聽頁面載入
*/
onLoad: function (options) {
let that =this;
var gasnameArr = wx.getStorageSync('gasname');
var gasname;
if (gasnameArr.length != 0 && gasnameArr != undefined) {
gasname = gasnameArr.join(",");
if (gasname.length > 10) {
gasname = gasname.substring(0, 10) + "...";
}
}
that.setData({
gasname: gasname,
gasnameArr: wx.getStorageSync('gasname')
})
/*wx.request({
url: require('../../config').getsaleUrl,
//url: 'http://localhost:8081/framework-protal/main/gas/getsale',
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
data: { gasid: wx.getStorageSync('gasid') },
success: function (res) {
console.log('success')
console.log(res.data)
that.setData({
responseData: res.data.response,
})
},
fail: function (res) {
console.log('fail')
}
}) */
wx.request({
url: require('../../config').getnumUrl,
//url: 'http://localhost:8081/framework-protal/main/gas/getsale',
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
data: {
gasid: wx.getStorageSync('gasid'),
type:0 //預設值為當天
},
success: function (res) {
console.log('success')
console.log(res.data)
that.setData({
numresponseData: res.data.response,
})
},
fail: function (res) {
console.log('fail')
}
})
},
//加油站點選事件
bindButtonTap: function (e) {
wx.redirectTo({
url: '/pages/personal/personal?userid=' + wx.getStorageSync('userid') + '&type=1',
})
},
/**
* 生命週期函式--監聽頁面初次渲染完成
*/
onReady: function () {
},
/**
* 生命週期函式--監聽頁面顯示
*/
onShow: function () {
},
/**
* 生命週期函式--監聽頁面隱藏
*/
onHide: function () {
},
/**
* 生命週期函式--監聽頁面解除安裝
*/
onUnload: function () {
},
/**
* 頁面相關事件處理函式--監聽使用者下拉動作
*/
onPullDownRefresh: function () {
},
/**
* 頁面上拉觸底事件的處理函式
*/
onReachBottom: function () {
},
/**
* 使用者點選右上角分享
*/
onShareAppMessage: function () {
},
/**
* 選單單擊
*/
onBarItemClick: function (e) {
let url = this.data.barItem[e.currentTarget.dataset.sno].url;
if (url == null || url == '') {
return;
}
wx.redirectTo({
url: url
})
},
/**
* 導航欄單擊
*/
onNavigation: function () {
wx.navigateTo({
url: '/pages/navigation/navigation'
})
},
onHotPro2: function(e) {
wx.navigateTo({
url: '/pages/groupLine/groupLine?id=' + e.currentTarget.id,
})
},
onHotPro3: function(e) {
wx.login({ //呼叫微信登入介面
success: function (res) {
wx.setStorageSync("code", res.code)
let code = wx.getStorageSync("code")
//
wx.request({
url: 'http://172.16.16.145:8180/wxService/wx/queryProductDetail',
method: 'GET',
data: {
id: 3,
},
success: function (res) {
console.log('success')
console.log(res)
},
fail: function (res) {
console.log('fail')
}
});
},
fail: function () {
}
});
wx.navigateTo({
url: '/pages/flowPay/flowPay',
})
},
onCase1: function(e) {
wx.navigateTo({
url: '/pages/case/case?id=' + e.currentTarget.id,
})
},
//輸入搜尋內容
searchmessage: function (e) {
this.setData({
searchmessage: e.detail.value
})
},
//點選搜尋
onsearch: function (e) {
wx.navigateTo({
url: '/pages/order/order?productName=' + this.data.searchmessage,
})
},
//點選註冊
regist: function (e) {
wx.navigateTo({
url: '/pages/login/reg/reg',
})
}
})
324
324
1
// pages/index/index.js
2
Page({
3
4
/**
5
* 頁面的初始資料
6
*/
7
data: {
8
array: ['當天', '本月', '近三月','近半年','本年'],
9
index: 0,
10
date: '2016-09-01',
11
time: '12:01',
12
barItem: [
13
{
14
name: '利潤彙總',
15
image: '../../images/menu1_unfocus.png',
16
selectedImage: '../../images/menu1_focus.png',
17
selected: false,
18
color: '#666666',
19
url: '/pages/solution/solution'
20
},
21
{
22
name: '每日營業情況',
23
image: '../../images/menu3_unfocus.png',
24
selectedImage: '../../images/menu3_focus.png',
25
selected: true,
26
color:'#FF0000',
27
url: '/pages/index/index'
28
},
29
{
30
name: '運營支出',
31
image: '../../images/menu2_unfocus.png',
32
selectedImage: '../../images/menu2_focus.png',
33
selected: false,
34
color: '#666666',
35
url: '/pages/order/order'
36
},
37
// {
38
// name: '',
39
// image: '../../images/menu4_unfocus.png',
40
// selectedImage: '../../images/menu4_focus.png',
41
// selected: false,
42
// url: '/pages/personal/personal'
43
// },
44
],
45
successresponseData: [],
46
datalist:[]
47
},
48
49
//更新日期選型,重新整理取值
50
bindPickerChange: function (e) {
51
52
var that = this;
53
console.log('picker傳送選擇改變,攜帶值為', e.detail.value)
54
this.setData({
55
index: e.detail.value
56
})