【微信小程式開發教程】選單內容左右聯動 & MD5加密

weixin_34007020發表於2017-08-25

1****、****微信小程式****選單內容左右聯動
小程式無法獲取元素的寬高,位置資訊,只能通過後臺計算,但是存在較大的機器誤差,不知有啥好的解決方案?

7377468-5e11f4301a9b6022.gif

如圖所以,左側是選單欄,右側是主體內容,點選左側選單,右側滑動到相應的位置;右側滑動過程,也會改變左側選單的選中狀態。本人的實現方案:

所有元素大小單位用rpx;
通過scrollbind(e) 的 e.detail.scrollHeight獲取右側滑動區域的總高度(單位px)
通過物品高度和標題高度的比值,計算出各自的實際高度(單位px)
通過修改scrollTop(單位px)改變主體內容位置

這樣還是存在1px-100px的誤差,物品越多,後面的累計誤差會越大,有沒有更好的解決辦法呢?
答:測試了一下,的確用scroll-view的scroll-to-view特性可以解決:
wxml****中修改:

js****檔案中修改:

page data中增加:

menuType:['food','dust','bowl','cages','toys','tools'],toView:'cages',

然後把下面函式修改一下:

selectMenuAct: function (e) {
//typenamevar id = e.target.dataset.id;var tType=this.data.menuType[id];
console.log(e),this.setData({
  scrollNum: id,
  toView: tType
  //scrollTop: this.data.heightList[id]
});
},

測試環境下通過。

scroll-into-view值應為某子元素id(id不能以數字開頭)。設定哪個方向可滾動,則在哪個方向滾動到該元素。

2****、微信小程式****MD5****加密
一般很多語言都有MD5加密的庫。
如果你指的是資料加密,怕資料明文不安全,我建議使用base64 + 一些字首或者字尾進行加密,然後將資料傳到伺服器,伺服器再進行解密後去掉這些前字尾。比如,明文是abc,你可以加一下字首,變成123abc,然後加密成為MTIzYWJj再發出去,然後再解密就行了。
一般MD5加密是不可逆的。而base64可以編碼解碼,如下:

package main
import (
    "fmt"
    "github.com/hunterhug/GoSpider/util"
)
func main() {
    s := "abc"
    prefix := "123"
    base64e := util.Base64E(prefix + s)
    fmt.Println("加密:" + base64e)
    fmt.Println("再解密:" + util.Base64D(base64e))
}

結果

加密:MTIzYWJj
再解密:123abc

百度百科介紹:Base64編碼可用於在HTTP環境下傳遞較長的標識資訊。例如,在Java Persistence系統Hibernate中,就採用了Base64來將一個較長的唯一識別符號(一般為128-bit的UUID)編碼為一個字串,用作HTTP表單和HTTP GET URL中的引數。在其他應用程式中,也常常需要把二進位制資料編碼為適合放在URL(包括隱藏表單域)中的形式。此時,採用Base64編碼不僅比較簡短,同時也具有不可讀性,即所編碼的資料不會被人用肉眼所直接看到。

我的Golang語言自己封裝的加密庫一般是這樣的:

/*
Copyright 2017 by GoSpider author.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package util
import (
    "crypto/hmac"
    "crypto/md5"
    "crypto/sha256"
    "encoding/base64"
    "encoding/hex"
    "fmt"
    "net/url"
    "strings"
)
// HMAC with the SHA256  http://blog.csdn.net/js_sky/article/details/49024959func ComputeHmac256(message string, secret string) string {
    key := []byte(secret)
    h := hmac.New(sha256.New, key)
    h.Write([]byte(message))
    return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
// create md5 stringfunc Strtomd5(s string) string {
    h := md5.New()
    h.Write([]byte(s))
    rs := hex.EncodeToString(h.Sum(nil))
    return rs
}
func Md5(str string) string {
    return Strtomd5(str)
}
// 字串base64加密func Base64E(urlstring string) string {
    str := []byte(urlstring)
    data := base64.StdEncoding.EncodeToString(str)
    return data
}
// 字串base64解密func Base64D(urlxxstring string) string {
    data, err := base64.StdEncoding.DecodeString(urlxxstring)
    if err != nil {
        return ""
    }
    s := fmt.Sprintf("%q", data)
    s = strings.Replace(s, "\"", "", -1)
    return s
}
//url轉義func UrlE(s string) string {
    return url.QueryEscape(s)
}
//url解義func UrlD(s string) string {
    s, e := url.QueryUnescape(s)
    if e != nil {
        return e.Error()
    } else {
        return s
    }
}

現在大部分站點都開啟https來保證資料安全。

相關文章