国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

微信小程序連接數(shù)據(jù)庫與WXS的使用

這篇具有很好參考價值的文章主要介紹了微信小程序連接數(shù)據(jù)庫與WXS的使用。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報違法"按鈕提交疑問。

?微信小程序連接數(shù)據(jù)庫與WXS的使用,微信小程序開發(fā)實(shí)戰(zhàn),微信小程序,微信,數(shù)據(jù)庫,wxs

????歡迎來到我的CSDN主頁!????

??我是Java方文山,一個在CSDN分享筆記的博主。????

??推薦給大家我的專欄《微信小程序開發(fā)實(shí)戰(zhàn)》。????

??點(diǎn)擊這里,就可以查看我的主頁啦!????

Java方文山的個人主頁

??如果感覺還不錯的話請給我點(diǎn)贊吧!????

??期待你的加入,一起學(xué)習(xí),一起進(jìn)步!????

微信小程序連接數(shù)據(jù)庫與WXS的使用,微信小程序開發(fā)實(shí)戰(zhàn),微信小程序,微信,數(shù)據(jù)庫,wxs

前言

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

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

1.接口地址URL

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

微信小程序連接數(shù)據(jù)庫與WXS的使用,微信小程序開發(fā)實(shí)戰(zhàn),微信小程序,微信,數(shù)據(jù)庫,wxs

?為了后期方便維護(hù),我們先將所有的后端接口通過一個文件來保存,在根目錄下新建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', //會議信息
 };

先定義本機(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);//會把進(jìn)行中改變成已成功
        } else {
          reject(res.errMsg);//會把進(jìn)行中改變成已失敗
        }
      },
      fail: function (err) {
        reject(err)
      }
    })
  });
}

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

 //首頁會議信息的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ù)庫與WXS的使用,微信小程序開發(fā)實(shí)戰(zhàn),微信小程序,微信,數(shù)據(jù)庫,wxs

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

微信小程序連接數(shù)據(jù)庫與WXS的使用,微信小程序開發(fā)實(shí)戰(zhàn),微信小程序,微信,數(shù)據(jù)庫,wxs

部分代碼:

@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);
    }
}

這里我相信大家都懂不必多說?。。。?!

?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">會議信息</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>人報名</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ù)據(jù)庫與WXS的使用,微信小程序開發(fā)實(shí)戰(zhàn),微信小程序,微信,數(shù)據(jù)庫,wxs

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

二、WXS的使用

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

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é)會了基本的wxs的使用,我們在剛剛的頁面中進(jìn)行操作一手。

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

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

}

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

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

4.解決時間進(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)

  //獲取 時分秒
  if (option == 'hh-mm-ss') return [hour, minute, second].map(formatNumber).join(':')

  //獲取 時分
  if (option == 'hh-mm') return [hour, minute].map(formatNumber).join(':')

  //獲取 分秒
  if (option == 'mm-dd') return [minute, second].map(formatNumber).join(':')

  //獲取 時
  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) 時分秒 年月日
  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

?效果展示

微信小程序連接數(shù)據(jù)庫與WXS的使用,微信小程序開發(fā)實(shí)戰(zhàn),微信小程序,微信,數(shù)據(jù)庫,wxs

?微信小程序連接數(shù)據(jù)庫與WXS的使用,微信小程序開發(fā)實(shí)戰(zhàn),微信小程序,微信,數(shù)據(jù)庫,wxs?

到這里我的分享就結(jié)束了,歡迎到評論區(qū)探討交流??!

??如果覺得有用的話還請點(diǎn)個贊吧 ??

微信小程序連接數(shù)據(jù)庫與WXS的使用,微信小程序開發(fā)實(shí)戰(zhàn),微信小程序,微信,數(shù)據(jù)庫,wxs文章來源地址http://www.zghlxwxcb.cn/news/detail-713246.html

到了這里,關(guān)于微信小程序連接數(shù)據(jù)庫與WXS的使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 微信小程序數(shù)據(jù)交互------WXS的使用

    微信小程序數(shù)據(jù)交互------WXS的使用

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? 艷艷耶??:個人主頁 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? 個人專欄 :《Spring與Mybatis集成整合》《Vue.js使用》 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? 越努力 ,越幸運(yùn)

    2024年02月05日
    瀏覽(24)
  • “編輯微信小程序與后臺數(shù)據(jù)交互與微信小程序wxs的使用“

    “編輯微信小程序與后臺數(shù)據(jù)交互與微信小程序wxs的使用“

    在現(xiàn)代移動應(yīng)用開發(fā)中,微信小程序已經(jīng)成為了一個非常流行和廣泛使用的平臺。為了使小程序能夠展示豐富的內(nèi)容和實(shí)現(xiàn)復(fù)雜的功能,與后臺數(shù)據(jù)的交互是至關(guān)重要的。同時,微信小程序還提供了一種特殊的腳本語言——wxs,用于增強(qiáng)小程序的業(yè)務(wù)邏輯處理能力。本篇博客

    2024年02月08日
    瀏覽(25)
  • 微信小程序數(shù)據(jù)庫更新update的使用

    ????????微信小程序數(shù)據(jù)庫的update功能不可以直接使用db.collection.update,此功能雖然在collection的使用說明文檔中有,但是經(jīng)過實(shí)際操作之后是無法成功更新的,必須使用db.collection.doc.update才可以完成。 ????????使用db.collection.doc.update方式時,數(shù)據(jù)記錄則應(yīng)由add功能添加

    2024年02月03日
    瀏覽(24)
  • 微信小程序云開發(fā)(云數(shù)據(jù)庫的使用)

    云數(shù)據(jù)庫提供高性能的數(shù)據(jù)庫寫入和查詢服務(wù)。通過騰訊云開發(fā)(Tencent CloudBase.TCB)的SDK,可以直接在客戶端對數(shù)據(jù)進(jìn)行讀寫,也可以在云函數(shù)中讀寫數(shù)據(jù),還可以通過控制臺對數(shù)據(jù)進(jìn)行可視化的增、刪、查、改等操作。微信小程序云開發(fā)所使用的數(shù)據(jù)庫本質(zhì)上就是一MongoD

    2024年02月09日
    瀏覽(17)
  • 微信小程序使用PHP調(diào)用后臺mysql數(shù)據(jù)庫-小白版

    微信小程序使用PHP調(diào)用后臺mysql數(shù)據(jù)庫-小白版

    1.域名備案 首先,需要有一個域名,且這個域名是已經(jīng)備過案的。(如果小程序不發(fā)布正式版只用于線上測試則不影響) 2.后臺服務(wù)器 其次,需要一個服務(wù)器,我這里使用的是寶塔面板,對小白很友好,很方便操作。 也可以買個虛擬主機(jī),一個月幾塊錢左右,很適合小白弄

    2024年04月14日
    瀏覽(39)
  • 微信小程序云開發(fā)—01(云數(shù)據(jù)庫、云函數(shù)的創(chuàng)建與基本使用)

    微信小程序云開發(fā)—01(云數(shù)據(jù)庫、云函數(shù)的創(chuàng)建與基本使用)

    1. 小程序云開發(fā),讓前端程序員擁有后端的能力 2. 云函數(shù) (nodejs) 3. 云數(shù)據(jù)庫 (mogodb) 4. 云存儲 5. 前端寫好云函數(shù) - 上傳到云服務(wù)器 -實(shí)現(xiàn)自定云部署 6. 前端去調(diào)用云函數(shù)=間接通過云函數(shù)對數(shù)據(jù)庫的操作 7. 前端=》全棧 1. 在app.js 2. 云函數(shù)index.js 定義id 3. 云id來自 云開發(fā)

    2024年02月15日
    瀏覽(50)
  • 微信小程序之會議OA首頁數(shù)據(jù)交互,會議狀態(tài),會議人數(shù)轉(zhuǎn)換,會議室交互,WXS的使用

    微信小程序之會議OA首頁數(shù)據(jù)交互,會議狀態(tài),會議人數(shù)轉(zhuǎn)換,會議室交互,WXS的使用

    前言: 本篇博客使用結(jié)合了SpringMVC,mybatis,maven,小程序,如果不熟悉使用可以翻看我之前的博客,以便大家可以更好的學(xué)習(xí)?。?! 這是我們今天完成后的效果: 1.1啟動開發(fā)工具,導(dǎo)入后臺 導(dǎo)入框架: 配置maven 注意數(shù)據(jù)庫的名稱: 啟動 1.2導(dǎo)入數(shù)據(jù)表 1.3前臺頁面的編碼(

    2024年02月08日
    瀏覽(19)
  • 微信小程序WXS模塊的使用

    微信小程序WXS模塊的使用

    common.wxs var foo=“‘hello world’ from common.wxs” //inArray方法 判斷數(shù)組中是否存在某元素 function inArray(arr,val){ return arr.indexOf(val)-1 } var msg=“some msg” //module 對象 每個 wxs 模塊均有一個內(nèi)置的 module 對象。 //屬性 exports: 通過該屬性,可以對外共享本模塊的私有變量與函數(shù)。 module.

    2024年04月13日
    瀏覽(19)
  • 原生微信小程序使用 wxs;微信小程序使用 vant-weapp組件

    原生微信小程序使用 wxs;微信小程序使用 vant-weapp組件

    在這里插入圖片描述 操作順序 :文檔地址 如果使用 typescript 需要操作步驟3,否則不需要操作 2.1 在根目錄下 操作 2.2 安裝版本 2.3 構(gòu)建包 - 詳情-勾選使用 npm 2.4 使用 vant 組件

    2024年02月11日
    瀏覽(97)
  • 微信小程序——操作數(shù)據(jù)庫

    微信小程序——操作數(shù)據(jù)庫

    訪問次數(shù)應(yīng)該與用戶進(jìn)行關(guān)聯(lián) 業(yè)務(wù)邏輯: 如果用戶是第一次訪問此程序,向數(shù)據(jù)庫添加一條記錄:{openid:45454545,count:1} 如果用戶不是第一次訪問,首先獲取數(shù)據(jù)庫中改用戶的訪問次數(shù)然后+1,再保存到數(shù)據(jù)庫中,然后更新頁面中的訪問次數(shù) 實(shí)現(xiàn)步驟: 在項(xiàng)目的pages中創(chuàng)建

    2024年04月17日
    瀏覽(22)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包