小編給大家分享一下小程序開發的難點分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
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?'fixed':''}}" id="tabs">
<view class="item pr {{tabIndex===0?'on':''}}" @tap="toggleTab(0)">商品介紹</view>
<view class="item pr {{tabIndex===1?'on':''}}" @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('') // tab上面元素的高度
}
/**封裝根據id獲取元素高度 */
getElHeightById(id){
return new Promise(function(resolve,reject){
const query = wx.createSelectorQuery()
query.select('#'+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('-');
} else if (type == "all") {
return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
} else if (type == "time") {
return [hour, minute, second].map(formatNumber).join(':')
} else if (type == "time2") {
return [hour, minute].map(formatNumber).join(':')
} else if (type == 'month') {
return [month, day].map(formatNumber).join('-');
}
}
/**niu 封裝時間戳格式化輔助,將年月日先轉為字符串,再判斷是否加0*/
function formatNumber(n) {
n = n.toString()
return n[1] ? n : '0' + n
}
formatTime(1236547891,'all') // 2019-11-02 03:11:11
formatTime(1236547891,'time') // 03:11:22
formatTime(1236547891,'time2') // 03:11
formatTime(1236547891,'month') // 03-03
formatTime(1236547891,'date') // 2019-11-02以上是“小程序開發的難點分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。