搜尋
首頁微信小程式小程式開發小程式開發有哪些困難

小程式開發有哪些困難

Dec 16, 2020 am 09:19 AM
小程式難點

小程式開發有哪些困難

文中列舉了幾個開發小程式的困難知識,希望能幫助大家。

(學習影片分享:程式設計影片

1、小程式產生二維碼

小程式產生二維碼 小程式產生二維碼其實是需要後端調用,然後前端調用後端介面即可。

在下面的例子中,我們傳給後端scene就是額外參數(長度最大為32個字符,只支援數字,大小寫英文以及部分特殊字符:!#$&'()* , /:;=?@-._~,其它字元請自行編碼為合法字元),在開啟的小程式用到的。

    let scene = 't=3&n='+ this.inputVal+'&sale='+this.saleId;
    request(qcode,{isHyaline:false,qrcodeType:2,scene:scene,width:255,color:{r:'255',g:'255',b:'255'}})
      .then(res=>{})
    })

這裡的坑在於前端如何取得使用者透過掃二維碼如何取得參數

  onLoad(opts){
    var scene = decodeURIComponent(opts.scene); // scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
     
  }

2、小程式跳小程式

小程式呼叫toMiniProgram,我在這裡做了個簡單的封裝

function toMiniProgram(appid, path, extraData) {
  wx.navigateToMiniProgram({
    appId: appid,  // 要打开的小程序 appId
    path: path,   // 打开的页面路径
    envVersion: 'develop',  //要打开的小程序版本。仅在当前小程序为开发版或体验版时此参数有效。
    extraData: extraData,  // 需要传递给目标小程序的数据
    success: function (res) {
      console.log('打开成功')
    },
    fail: function (res) {}
  })
}

在頁面中使用

 toMiniProgram('appid','pages/customer/goods-detail?id='+id+'&saleId='+this.saleId,{});

在另一個小程式中取得參數

onLoad(opts){
    this.goodsId=opts.id
    this.saleId=opts.saleId
}

3、吸頂效果的實作

吸頂效果的原理是將捲動到一定高度的tab 重新佈局為fixed

html

<view class="tab-ctn">
  <view class="tabs flex flex-justify {{isTabFixed?&#39;fixed&#39;:&#39;&#39;}}" id="tabs">
    <view class="item pr {{tabIndex===0?&#39;on&#39;:&#39;&#39;}}" @tap="toggleTab(0)">商品介绍</view>
    <view class="item pr {{tabIndex===1?&#39;on&#39;:&#39;&#39;}}" @tap="toggleTab(1)">规格参数</view>
  </view>
</view>

css

.tabs{  padding:0 176rpx;
        font-size:30rpx;
        height:90rpx;
        border-bottom:0.5px solid #E4E4E4;}
.item{
    height:100%;
    position:relative;
    padding-top:20rpx;
    color:#999;
    &.on{
    color:#FD343B;
    font-weight:bold;
    }
}
.fixed{
    position:fixed;
    top:0;
    left:0;
    right:0;
    z-index:9;
}

js

data={
    detailTopH:300, 
}
onLoad(){
    this.getElHeight(&#39;&#39;) // tab上面元素的高度
}
/**封装根据id获取元素高度 */
getElHeightById(id){
  return new Promise(function(resolve,reject){
    const query = wx.createSelectorQuery()
    query.select(&#39;#&#39;+id).boundingClientRect()
    query.selectViewport().scrollOffset()
    query.exec(function (res) {
      resolve(res[0].height)
    })
  })
}
// 调用
  getElHeight(id){
    this.getElHeightById(id).then(res=>{
      this.detailTopH = res
    })
  }
/**监听页面滚动事件 */
  onPageScroll(e){
    if(e.scrollTop>=this.detailTopH && !this.isTabFixed){
      this.isTabFixed=true
    }else if(e.scrollTop<=this.detailTopH && this.isTabFixed){
      this.isTabFixed=false
    }
  }

4 、封裝時間戳記

function formatTime(timestamp, type = "date") {
  var date = new Date(timestamp);
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
 
  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()
  if (type == "date") {
    return [year, month, day].map(formatNumber).join(&#39;-&#39;);
  } else if (type == "all") {
    return [year, month, day].map(formatNumber).join(&#39;-&#39;) + &#39; &#39; + [hour, minute, second].map(formatNumber).join(&#39;:&#39;)
  } else if (type == "time") {
    return [hour, minute, second].map(formatNumber).join(&#39;:&#39;)
  } else if (type == "time2") {
    return [hour, minute].map(formatNumber).join(&#39;:&#39;)
  } else if (type == &#39;month&#39;) {
    return [month, day].map(formatNumber).join(&#39;-&#39;);
  }
 
}
/**niu 封装时间戳格式化辅助,将年月日先转为字符串,再判断是否加0*/
function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : &#39;0&#39; + n
}
 
formatTime(1236547891,&#39;all&#39;) // 2019-11-02 03:11:11
formatTime(1236547891,&#39;time&#39;)  // 03:11:22
formatTime(1236547891,&#39;time2&#39;)  // 03:11
formatTime(1236547891,&#39;month&#39;)  // 03-03
formatTime(1236547891,&#39;date&#39;)  // 2019-11-02

相關推薦:小程式開發

以上是小程式開發有哪些困難的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:csdn。如有侵權,請聯絡admin@php.cn刪除

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境