欢迎光临,了解微信小程序开发,就上易用通!

教你写一个云上Hello world小程序

发布:2018-04-17 09:18浏览: 来源:网络 作者:cola

笔者近期接触了不少从事后端开发的Java、C++程序员,纷纷表示了想要了解小程序开发技术的兴趣。下面,结合一个Hello world的小程序示例,给大家简单讲解一下如何在腾讯云上开发一个简单的小程序demo,小程序示例的完成结果如下: 

教你写一个云上Hello world小程序(图1)

1.Hello World 小程序代码结构 

教你写一个云上Hello world小程序(图2)

app.js定义了小程序的启动逻辑 
app.json定义了小程序的页面结构,目前我们的小程序只有一个index页面 
index.wxml定义了欢迎页面的有什么,目前我们放了一张gif、一个按钮和一个文字标签。 
index.wxss 定义了欢迎页面的样式 
index.js定义了欢迎页面的业务逻辑

2.小程序用到的组件与云服务

腾讯云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

 

				
  1. App({
  2. onLaunch: function () {
  3. var logs = wx.getStorageSync('logs') || []
  4. },
  5. globalData:{
  6. userInfo:null
  7. }
  8. })
  9. //app.json
  10.  
  11. {
  12. "pages":[
  13. "pages/index/index"
  14. ],
  15. "window":{
  16. "backgroundTextStyle":"light",
  17. "navigationBarBackgroundColor": "#fff",
  18. "navigationBarTitleText": "WeChat",
  19. "navigationBarTextStyle":"black"
  20. }
  21. }

//index.js

 

				
  1. //获取应用实例
  2. var app = getApp()
  3. Page({
  4. data: {
  5. words: '点按钮让我说话',
  6. userInfo: {}
  7. },
  8.  
  9. say: function( e ) {
  10. var hello = require('../../utils/hello.js');
  11. hello( this );
  12. },
  13. onLoad: function () {
  14. }
  15. })

//index.wxml

 

				
  1. <view class="container">
  2. <view bindtap="bindViewTap" class="userinfo">
  3. <image class="userinfo-avatar" src="/res/face.gif" mode="widthFix"></image>
  4. <text class="userinfo-nickname">{{userInfo.nickName}}</text>
  5. </view>
  6.  
  7. <view class="hello" >
  8. <text>{{words}}</text>
  9. </view>
  10.  
  11. <button type="primary" size="{{primarySize}}" loading="{{loading}}" plain="{{plain}}" disabled="{{disabled}}" bindtap="say"> 请说话 </button>
  12.  
  13. </view>

//Hello.js 定义两个版本的Hello world逻辑,V1是将标签文字替换为“Hello world”,V2是将从腾讯云数据库拉取回的数据(不同语言的hellow world)显示在标签里。

 

				
  1. function hello_v1( page ) {
  2. page.setData({words:'HELLO WORLD!'});
  3. }
  4.  
  5. function hello_v2( page ) {
  6.  
  7. page.setData({words:'LOADING...'});
  8.  
  9. wx.request({
  10. url: 'https://wwp.appcook.cn/test.php', //仅为示例,并非真实的接口地址
  11. data: {t:Date.parse(new Date())},
  12. header: {
  13. 'content-type': 'application/json'
  14. },
  15. success: function(res) {
  16. page.setData({words:res.data});
  17. }
  18. })
  19. }
  20.  
  21. module.exports = hello_v1

4.后端代码

链接腾讯云主机上XMP.JS的Baas服务,把数据库中读取的信息显示在index.wxml页面的<text>{{words}}</text>标签里。  //文件test.PHP

 

				
  1. <?php
  2.  
  3. $mysqli = new mysqli("10.66.151.210", "root", "yun123456", "words");
  4.  
  5. / check connection /
  6. if ($mysqli->connect_errno) {
  7. printf("Connect failed: %s\n", $mysqli->connect_error);
  8. exit();
  9. }
  10.  
  11. $query = "SELECT * FROM hello ORDER BY RAND() LIMIT 1";
  12. $result = $mysqli->query($query);
  13.  
  14. / associative array /
  15. $row = $result->fetch_array(MYSQLI_ASSOC);
  16.  
  17. echo json_encode(end($row));
  18.  
  19. / free result set /
  20. $result->free();
  21.  
  22. / close connection /
  23. $mysqli->close();





免责声明:本站所有文章和图片均来自用户分享和网络收集,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服处理。