欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序連接數(shù)據(jù)庫與WXS的使用方法詳細(xì)介紹

 更新時(shí)間:2023年12月30日 12:28:07   作者:Java方文山  
這篇文章主要介紹了微信小程序連接數(shù)據(jù)庫與WXS的使用方法,微信小程序是騰訊內(nèi)部的產(chǎn)品,不能直接打開一個(gè)外部的鏈接,但是騰訊為開發(fā)者封裝好了API用來請求一個(gè)網(wǎng)站的內(nèi)容或者服務(wù),感興趣的同學(xué)可以參考下

前言

很多同志們再寫小程序的過程中,不知道該怎么發(fā)起HTTP請求到后端,在Web環(huán)境中發(fā)起HTTPS請求是很常見的,但是微信小程序是騰訊內(nèi)部的產(chǎn)品,不能直接打開一個(gè)外部的鏈接。例如,在微信小程序中不能直接打開網(wǎng)站,但是,在小程序開發(fā)的時(shí)候,如果需要請求一個(gè)網(wǎng)站的內(nèi)容或者服務(wù),如何實(shí)現(xiàn)?雖然微信小程序里面不能直接訪問外部鏈接,但是騰訊為開發(fā)者封裝好了一個(gè)wx.request(object)的API。

一、搭建數(shù)據(jù)庫連接

1.接口地址URL

其他的東西還是基本與我們發(fā)送ajax請求一致的。

為了后期方便維護(hù),我們先將所有的后端接口通過一個(gè)文件來保存,在根目錄下新建config文件夾隨后建立api.js文件。

// 以下是業(yè)務(wù)服務(wù)器API地址
 // 本機(jī)開發(fā)API地址
 var WxApiRoot = 'http://localhost:8080/wx/';
 // 測試環(huán)境部署api地址
 // var WxApiRoot = 'http://192.168.0.101:8070/demo/wx/';
 // 線上平臺api地址
 //var WxApiRoot = 'https://www.oa-mini.com/demo/wx/';
 module.exports = {
   IndexUrl: WxApiRoot + 'home/index', //首頁數(shù)據(jù)接口
   SwiperImgs: WxApiRoot+'swiperImgs', //輪播圖
   MettingInfos: WxApiRoot+'meeting/list', //會(huì)議信息
 };

先定義本機(jī)開發(fā)的API地址,具體的請求在下面定義方便管理。

2.請求方式的封裝

 loadMeetingInfos(){
    let that=this;
    wx.request({
        url: api.IndexUrl,
        dataType: 'json',
        success(res) {
          console.log(res)
          that.setData({
              lists:res.data.data.infoList
          })
        }
      })
  }

以上是我們發(fā)送請求的代碼,雖然比較簡短但還是要在需要的地方進(jìn)行編寫,簡單的代碼反復(fù)性高那我們就可以對它進(jìn)行封裝。

在/utils/util.js中添加下列代碼

/**
 * 封裝微信的request請求
 */
function request(url, data = {}, method = "GET") {
  return new Promise(function (resolve, reject) {
    wx.request({
      url: url,
      data: data,
      method: method,
      header: {
        'Content-Type': 'application/json',
      },
      success: function (res) {
        if (res.statusCode == 200) {
            resolve(res.data);//會(huì)把進(jìn)行中改變成已成功
        } else {
          reject(res.errMsg);//會(huì)把進(jìn)行中改變成已失敗
        }
      },
      fail: function (err) {
        reject(err)
      }
    })
  });
}

注意在module.exports中導(dǎo)出和需要使用的頁面js中使用實(shí)constutil=require("../../utils/util")

 //首頁會(huì)議信息的ajax
  loadMeetingInfos() {
    let that = this;
    util.request(api.IndexUrl).then(res => {
      this.setData({
        lists: res.data.infoList
      })
    })
  }

是不是看起來又少了很多代碼

3.后端代碼

后端SpringBoot搭建的,引入了mysql、mybatisplus、swagger、lombok等依賴。

數(shù)據(jù)庫表結(jié)構(gòu):

部分代碼:

@RestController
@RequestMapping("/wx/home")
public class WxHomeController {
    @Autowired
    private InfoMapper infoMapper;
    @RequestMapping("/index")
    public Object index(Info info) {
        List<Info> infoList = infoMapper.list(info);
        Map<Object, Object> data = new HashMap<Object, Object>();
        data.put("infoList",infoList);
        return ResponseUtil.ok(data);
    }
}

這里我相信大家都懂不必多說?。。。。?/p>

4.前端代碼

wxml

<!--index.wxml-->
<view>
  <swiper autoplay="true" indicator-dots="true">
    <block wx:for="{{imgSrcs}}" wx:key="text">
      <swiper-item>
        <view>
          <image src="{{item.img}}" class="swiper-item" />
        </view>
      </swiper-item>
    </block>
  </swiper>
</view>
<view class="mobi-title">
  <text class="mobi-icon"></text>
  <text class="mobi-text">會(huì)議信息</text>
</view>
<block wx:for-items="{{lists}}" wx:for-item="item" wx:key="item.id">
  <view class="list" data-id="{{item.id}}">
    <view class="list-img">
      <image class="video-img" mode="scaleToFill" src="{{item.image !=null? item.image : '/static/persons/6.png'}}"></image>
    </view>
    <view class="list-detail">
      <view class="list-title"><text>{{item.title}}</text></view>
      <view class="list-tag">
        <view class="state">{{item.state}}</view>
        <view class="join"><text class="list-num">{{item.num}}</text>人報(bào)名</view>
      </view>
      <view class="list-info"><text>{{item.location}}</text>|<text>{{item.starttime}}</text></view>
    </view>
  </view>
</block>
<view class="section">
  <text>到底啦</text>
</view>

wxss

/**index.wxss**/
.section{
  color: #aaa;
  display: flex;
  justify-content: center;
}
.list-info {
  color: #aaa;
}
.list-num {
  color: #e40909;
  font-weight: 700;
}
.join {
  padding: 0px 0px 0px 10px;
  color: #aaa;
}
.state {
  margin: 0px 6px 0px 6px;
  border: 1px solid #93b9ff;
  color: #93b9ff;
}
.list-tag {
  padding: 3px 0px 10px 0px;
  display: flex;
  align-items: center;
}
.list-title {
  display: flex;
  justify-content: space-between;
  font-size: 11pt;
  color: #333;
  font-weight: bold;
}
.list-detail {
  display: flex;
  flex-direction: column;
  margin: 0px 0px 0px 15px;
}
.video-img {
  width: 80px;
  height: 80px;
}
.list {
  display: flex;
  flex-direction: row;
  border-bottom: 1px solid #6b6e74;
  padding: 10px;
}
.mobi-text {
  font-weight: 700;
  padding: 15px;
}
.mobi-icon {
  border-left: 5px solid #e40909;
}
.mobi-title {
  background-color: rgba(158, 158, 142, 0.678);
  margin: 10px 0px 10px 0px;
}
.swiper-item {
  height: 300rpx;
  width: 100%;
  border-radius: 10rpx;
}
.userinfo {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: #aaa;
}
.userinfo-avatar {
  overflow: hidden;
  width: 128rpx;
  height: 128rpx;
  margin: 20rpx;
  border-radius: 50%;
}
.usermotto {
  margin-top: 200px;
}

效果展示:

其實(shí)我們這個(gè)頁面還存在著一些的問題,比如說這個(gè)會(huì)議的狀態(tài)不應(yīng)該顯示數(shù)字,而是數(shù)字對應(yīng)的狀態(tài)是什么?參會(huì)的人數(shù)有多少?會(huì)議的時(shí)間顯示等問題...下面就用wxs帶大家解決該問題。

二、WXS的使用

WXS(WeChat Mini Program Storage)是微信小程序提供的本地存儲(chǔ)方案,用于在小程序中進(jìn)行數(shù)據(jù)的存儲(chǔ)和管理。相比遠(yuǎn)程數(shù)據(jù)庫,WXS更適合于小規(guī)模、簡單的數(shù)據(jù)存儲(chǔ)需求。

1.wxs文件

在微信開發(fā)者工具里面,右鍵可以直接創(chuàng)建 .wxs 文件,在其中直接編寫 WXS 腳本。

// /pages/comm.wxs
var foo = "'hello world' from comm.wxs";
var bar = function(d) {
  return d;
}
module.exports = {
  foo: foo,
  bar: bar
};

exports: 通過該屬性,可以對外共享本模塊的私有變量與函數(shù)。

在需要使用的頁面進(jìn)行引用即可

<wxs src="./../tools.wxs" module="tools" />
<view> {{tools.msg}} </view>
<view> {{tools.bar(tools.FOO)}} </view>

頁面輸出:

some msg
'hello world' from tools.wxs

學(xué)會(huì)了基本的wxs的使用,我們在剛剛的頁面中進(jìn)行操作一手。

2.解決數(shù)據(jù)顯示數(shù)字問題

function getState(state){
  // 狀態(tài):0取消會(huì)議1待審核2駁回3待開4進(jìn)行中5開啟投票6結(jié)束會(huì)議,默認(rèn)值為1
  if(state == 0 ){
      return '取消會(huì)議';
  }else if(state == 1 ){
      return '待審核';
  }else if(state == 2 ){
      return '駁回';
  }else if(state == 3 ){
      return '待開';
  }else if(state == 4 ){
      return '進(jìn)行中';
  }else if(state == 5 ){
      return '開啟投票';
  }else if(state == 6 ){
      return '結(jié)束會(huì)議';
  }
  return '其它';
}

3.解決統(tǒng)計(jì)人數(shù)問題

function formatNumber(liexize,canyuze,zhuchiren) {
    var person = liexize+","+canyuze+","+zhuchiren;
    return person.split(',').length;
}

4.解決時(shí)間進(jìn)制問題

function formatDate(ts, option) {
  var date = getDate(ts)
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  var week = date.getDay()
  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()
  //獲取 年月日
  if (option == 'YY-MM-DD') return [year, month, day].map(formatNumber).join('-')
  //獲取 年月
  if (option == 'YY-MM') return [year, month].map(formatNumber).join('-')
  //獲取 年
  if (option == 'YY') return [year].map(formatNumber).toString()
  //獲取 月
  if (option == 'MM') return  [mont].map(formatNumber).toString()
  //獲取 日
  if (option == 'DD') return [day].map(formatNumber).toString()
  //獲取 年月日 周一 至 周日
  if (option == 'YY-MM-DD Week')  return [year, month, day].map(formatNumber).join('-') + ' ' + getWeek(week)
  //獲取 月日 周一 至 周日
  if (option == 'MM-DD Week')  return [month, day].map(formatNumber).join('-') + ' ' + getWeek(week)
  //獲取 周一 至 周日
  if (option == 'Week')  return getWeek(week)
  //獲取 時(shí)分秒
  if (option == 'hh-mm-ss') return [hour, minute, second].map(formatNumber).join(':')
  //獲取 時(shí)分
  if (option == 'hh-mm') return [hour, minute].map(formatNumber).join(':')
  //獲取 分秒
  if (option == 'mm-dd') return [minute, second].map(formatNumber).join(':')
  //獲取 時(shí)
  if (option == 'hh')  return [hour].map(formatNumber).toString()
  //獲取 分
  if (option == 'mm')  return [minute].map(formatNumber).toString()
  //獲取 秒
  if (option == 'ss') return [second].map(formatNumber).toString()
  //默認(rèn) 時(shí)分秒 年月日
  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

效果展示

以上就是微信小程序連接數(shù)據(jù)庫與WXS的使用方法詳細(xì)介紹的詳細(xì)內(nèi)容,更多關(guān)于小程序連接數(shù)據(jù)庫的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論