大礼包 | 来拿小程序大礼包啦!

本文介绍了如何使用SpringBoot构建后端架构,并与微信小程序进行交互。内容包括SpringBoot项目的创建,配置文件设置,后端RESTful接口的实现,以及微信小程序的API调用和网络请求。此外,还讲解了HTTPS证书的申请和项目在Linux服务器上的部署流程。
摘要由CSDN通过智能技术生成

Hi! 我是小小,今天是本周的第五天,今天主要内容是讲解关于微信小程序开发的基本知识,后端是Java的哦~

前言

微信小程序使用的人数逐渐的增加,有不少人通过各种途径也尝试了微信小程序的开发,现在就开发一个新款的微信小程序实战。

主要内容

SpringBoot后端架构构建 小程序项目构建 小程序api调用 后端resetful接口调用 免费的https申请 linux下部署上线。

项目构建

在本地编写的时候,需要把详情下的项目设置下的不校验域名安全性勾选。微信小程序的基本组件 https://developers.weixin.qq.com/miniprogram/dev/component/

微信小程序的api https://developers.weixin.qq.com/miniprogram/dev/api/

后端详解

后端主要使用的是Java,主要框架为Spring Boot,开发工具为myeclipse,服务器为阿里云服务器。创建一个maven项目,导入相关的依赖。pom.xml依赖。

<!-- 统一版本控制 -->
<parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>1.5.9.RELEASE</version>
</parent>
<dependencies>
 <!-- freemarker渲染页面 -->
 <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-freemarker -->
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-freemarker</artifactId>
 </dependency>

 <!-- spring boot 核心 -->
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
 </dependency>

 <!-- springboot整合jsp -->
 <!-- tomcat 的支持. -->
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <exclusions>
   <exclusion>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
   </exclusion>
  </exclusions>
 </dependency>

 <dependency>
  <groupId>org.apache.tomcat.embed</groupId>
  <artifactId>tomcat-embed-jasper</artifactId>
 </dependency>
</dependencies>

在配置文件src/main.resources线面创建application.properties文件,输入如下的内容:

#jsp支持
spring.mvc.view.suffix=.jsp
spring.mvc.view.prefix=/WEB-INF/jsp/
#this is set port
#server.port=80
server.port=443
#添加ssl证书
#ssl证书文件名
server.ssl.key-store=classpath:xxxxxxx.pfx
server.ssl.key-store-password=xxxxxxxx
server.ssl.keyStoreType=xxxxxxxx

还有一些数据库,mybatis的配置文件,都需要添加。然后创建入口文件,app.java,输入如下代码:

@ComponentScan(basePackages= "com.bin")//添加扫包@ComponentScan(basePackages= "")
@EnableAutoConfiguration
public class App{

 //启动springboot
 public static void main(String[] args) {
  SpringApplication.run(App.class, args);
 }
}

再次编辑一个Controller层。

@RestController
@SpringBootApplication
public class ControllerText {
 
 @RequestMapping("getUser")
 public Map<String, Object> getUser(){
  System.out.println("微信小程序正在调用。。。");
  Map<String, Object> map = new HashMap<String, Object>();
  List<String> list = new ArrayList<String>();
   list.add("zhangsan");
   list.add("lisi");
   list.add("wanger");
   list.add("mazi");
   map.put("list",list);
  System.out.println("微信小程序调用完成。。。");
  return map;
 }
 
 @RequestMapping("getWord")
 public Map<String, Object> getText(String word){
  Map<String, Object> map = new HashMap<String, Object>();
  String message = "我能力有限,不要为难我";
  if ("后来".equals(word)) {
   message="正在热映的后来的我们是刘若英的处女作。";
  }else if("微信小程序".equals(word)){
   message= "想获取更多微信小程序相关知识,请更多的阅读微信官方文档,还有其他更多微信开发相关的内容,学无止境。";
  }else if("西安工业大学".equals(word)){
   message="西安工业大学(Xi'an Technological University)简称”西安工大“,位于世界历史名城古都西安,是中国西北地区唯一一所以兵工为特色,以工为主,理、文、经、管、法协调发展的教学研究型大学。原中华人民共和国兵器工业部直属的七所本科院校之一(“兵工七子”),陕西省重点建设的高水平教学研究型大学、陕西省人民政府与中国兵器工业集团、国防科技工业局共建高校、教育部“卓越工程师教育培养计划”试点高校、陕西省大学生创新能力培养综合改革试点学校。国家二级保密资格单位,是一所以"军民结合,寓军于民"的国防科研高校。";
  }
  map.put("message", message);
  return map;
 }
 
 @RequestMapping("")
 public String getText(){
  return "hello world";
 }

}

然后简易项目启动完成。

小程序发起网络请求

配置wxml文件如下所示:

<button bindtap='houduanButton1'>点击发起请求</button>
<view wx:for="{{list}}">
    姓名:{{item}}
  </view>

配置js文件

 /**
   * 页面的初始数据
   */
  data: {
    list: '',
    word: '',
    message:''

  },
  houduanButton1: function () {
    var that = this;
    wx.request({
      url: 'http://localhost:443/getUser',
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data)//打印到控制台
        var list = res.data.list;
        if (list == null) {
          var toastText = '数据获取失败';
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            list: list
          })
        }
      }
    })
  }

接下来以搜索类型的请求为例子,配置wxml文件,

 <input type="text" class="houduanTab_input" placeholder="请输入你要查询的内容" bindinput='houduanTab_input'></input>
  <button bindtap='houduanButton2'>查询</button>
  <view wx:if="{{message!=''}}">
    {{message}}
  </view>

配置js文件,在变量的定义上添加一个js文件

//获取输入框的内容
  houduanTab_input: function (e) {
    this.setData({
      word: e.detail.value
    })
  },
  // houduanButton2的网络请求
  houduanButton2: function () {
    var that = this;
    wx.request({
      url: 'http://localhost:443/getWord',
      data:{
        word: that.data.word
      },
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data)//打印到控制台
        var message = res.data.message;
        if (message == null) {
          var toastText = '数据获取失败';
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            message: message
          })
        }
      }
    })
  }

这样就完成了一个简单的通信。演示效果

https申请

在前面配置文件,application.properties中配置相关的整数,把整数pfx文件添加到后端项目中。

购买服务器部署后端代码

直接打包为jar文件,然后运行命令,进行部署。

 nohup java -jar helloworld.jar &

关于作者

我是小小,双鱼座的程序猿,我们下期再见~bye

END

「 往期文章 」

WebSocket | 为什么你前后端推送不会用?因为你少了WebSocket的帮忙

Lombook | 你的代码真正元凶找到了:Lombook

工具 | 终于等到你!地表最强工具来袭!

扫描二维码

获取更多精彩

小明菜市场

来源:网络(侵删)

图片来源:网络(侵删)

点个在看你最好看

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值