微信小程序開發— 菜單內容左右聯動 & MD5加密

一、微信小程序菜單內容左右聯動

小程序沒法獲取元素的寬高,位置信息,只能經過後臺計算,可是存在較大的機器偏差,不知有啥好的解決方案?git

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

  • 全部元素大小單位用rpx;express

  • 經過scrollbind(e) 的 e.detail.scrollHeight獲取右側滑動區域的總高度(單位px)apache

  • 經過物品高度和標題高度的比值,計算出各自的實際高度(單位px)小程序

  • 經過修改scrollTop(單位px)改變主體內容位置微信小程序

這樣仍是存在1px-100px的偏差,物品越多,後面的累計偏差會越大,有沒有更好的解決辦法呢?安全

答:測試了一下,的確用scroll-view的scroll-to-view特性能夠解決:服務器

wxml中修改:微信

<scroll-view scroll-y style="height: 31.5em" class="goods" bindscroll="goodsScrollAct"  scroll-into-view="{{toView}}" >
  <view class="box" id="{{item.viewId}}"  wx:for="{{allGoods}}" wx:key="unique" wx:for-index="typeId">

js文件中修改:
page data中增長:app

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

而後把下面函數修改一下:
selectMenuAct: function (e) {

//typename
var 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不能以數字開頭)。設置哪一個方向可滾動,則在哪一個方向滾動到該元素。

二、微信小程序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/49024959
func 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 string
func 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來保證數據安全。

相關文章
相關標籤/搜索