微信小程序开发— 菜单内容左右联动 & 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来保证数据安全。

相关文章
相关标签/搜索