|
筆者近期接觸了不少?gòu)氖潞蠖碎_(kāi)發(fā)的Java、C++程序員,紛紛表示了想要了解小程序開(kāi)發(fā)技術(shù)的興趣。下面,結(jié)合一個(gè)Hello world的小程序示例,給大家簡(jiǎn)單講解一下如何在騰訊云上開(kāi)發(fā)一個(gè)簡(jiǎn)單的小程序demo,小程序示例的完成結(jié)果如下:

1.Hello World 小程序代碼結(jié)構(gòu)

app.js定義了小程序的啟動(dòng)邏輯
app.json定義了小程序的頁(yè)面結(jié)構(gòu),目前我們的小程序只有一個(gè)index頁(yè)面
index.wxml定義了歡迎頁(yè)面的有什么,目前我們放了一張gif、一個(gè)按鈕和一個(gè)文字標(biāo)簽。
index.wxss 定義了歡迎頁(yè)面的樣式
index.js定義了歡迎頁(yè)面的業(yè)務(wù)邏輯
2.小程序用到的組件與云服務(wù)
騰訊云CVM:https://www.qcloud.com/product/cvm
騰訊云Mysql:https://www.qcloud.com/product/cdb
XMP.JS:https://git.oschina.net/xpmjs/xpmjs
3.前端代碼
//app.js
-
App({
-
onLaunch: function () {
-
var logs = wx.getStorageSync('logs') || []
-
},
-
globalData:{
-
userInfo:null
-
}
-
})
-
//app.json
-
-
{
-
"pages":[
-
"pages/index/index"
-
],
-
"window":{
-
"backgroundTextStyle":"light",
-
"navigationBarBackgroundColor": "#fff",
-
"navigationBarTitleText": "WeChat",
-
"navigationBarTextStyle":"black"
-
}
-
}
//index.js
-
//獲取應(yīng)用實(shí)例
-
var app = getApp()
-
Page({
-
data: {
-
words: '點(diǎn)按鈕讓我說(shuō)話',
-
userInfo: {}
-
},
-
-
say: function( e ) {
-
var hello = require('../../utils/hello.js');
-
hello( this );
-
},
-
onLoad: function () {
-
}
-
})
//index.wxml
-
<view class="container">
-
<view bindtap="bindViewTap" class="userinfo">
-
<image class="userinfo-avatar" src="/res/face.gif" mode="widthFix"></image>
-
<text class="userinfo-nickname">{{userInfo.nickName}}</text>
-
</view>
-
-
<view class="hello" >
-
<text>{{words}}</text>
-
</view>
-
-
<button type="primary" size="{{primarySize}}" loading="{{loading}}" plain="{{plain}}" disabled="{{disabled}}" bindtap="say"> 請(qǐng)說(shuō)話 </button>
-
-
</view>
//Hello.js 定義兩個(gè)版本的Hello world邏輯,V1是將標(biāo)簽文字替換為“Hello world”,V2是將從騰訊云數(shù)據(jù)庫(kù)拉取回的數(shù)據(jù)(不同語(yǔ)言的hellow world)顯示在標(biāo)簽里。
-
function hello_v1( page ) {
-
page.setData({words:'HELLO WORLD!'});
-
}
-
-
function hello_v2( page ) {
-
-
page.setData({words:'LOADING...'});
-
-
wx.request({
-
url: 'http://wwp.appcook.cn/test.php', //僅為示例,并非真實(shí)的接口地址
-
data: {t:Date.parse(new Date())},
-
header: {
-
'content-type': 'application/json'
-
},
-
success: function(res) {
-
page.setData({words:res.data});
-
}
-
})
-
}
-
-
module.exports = hello_v1
4.后端代碼
鏈接騰訊云主機(jī)上XMP.JS的Baas服務(wù),把數(shù)據(jù)庫(kù)中讀取的信息顯示在index.wxml頁(yè)面的<text>{{words}}</text>標(biāo)簽里。 //文件test.PHP
-
<?php
-
-
$mysqli = new mysqli("10.66.151.210", "root", "yun123456", "words");
-
-
/ check connection /
-
if ($mysqli->connect_errno) {
-
printf("Connect failed: %s\n", $mysqli->connect_error);
-
exit();
-
}
-
-
$query = "SELECT * FROM hello ORDER BY RAND() LIMIT 1";
-
$result = $mysqli->query($query);
-
-
/ associative array /
-
$row = $result->fetch_array(MYSQLI_ASSOC);
-
-
echo json_encode(end($row));
-
-
/ free result set /
-
$result->free();
-
-
/ close connection /
-
$mysqli->close();
|