md5 变量赋值相同

md5 variable assigns same value

我想创建 md5 变量并使用它来哈希唯一表单值并发送到 API 以获得唯一数据,但是每次我提交数据时 md5 值都是相同的。

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, Platform  } from 'ionic-angular';
import { ModalController, ViewController } from 'ionic-angular';
import { NgForm } from '@angular/forms';
import { Md5 } from 'ts-md5/dist/md5';
import { Geolocation } from '@ionic-native/geolocation';

@IonicPage()
@Component({
selector: 'page-bol',
templateUrl: 'bol.html',
})
export class BolPage {

private chemInfo:any[] = [];
private submitAllData:any[] = [];
private lonlat:any = [];
private md5Data:any;
constructor(public navCtrl: NavController,
          public navParams: NavParams,
          public modalCtrl: ModalController,
          private geolocation: Geolocation
          private platform: Platform) {
  }

ionViewDidLoad() {

   /* Ensure the platform is ready */
   this.platform.ready().then(() => {
   /* Perform initial geolocation */
   this.geolocation.getCurrentPosition().then((resp) => {
       this.lonlat = [resp.coords.latitude,resp.coords.longitude];
       console.log(this.lonlat);
     }).catch((error) => {
       console.log('Error getting location', error);
     });
   });
}

submitBOL(form: NgForm){
   //console.log(form.value);
   var md5 = new Md5();
   this.submitAllData.push(form.value,{'sub':this.chemInfo}, 
  {'gpsLoc':this.lonlat.toString()});

   //In theory this value should be unique every time
   this.md5Data = md5.appendStr(form.value.toString()).appendStr(this.chemInfo.toString()).appendStr(this.lonlat.toString()).end();
   this.submitAllData.push({'md5':this.md5Data});
   console.log(this.submitAllData);
}

我一直从控制台获取这个值: {"md5":"703137aef9805f0ca95b8c8b56619f84"} 我不确定为什么每次都是相同的值。我对 Ionic 中的此功能不是很熟悉,因此任何反馈都会有所帮助。谢谢!

我相信我找到了答案。显然我本地机器上的某些东西导致哈希打印出相同的 md5 字符串,但我继续在我的 android 设备上测试它并且它工作正常! 我知道事实上很少有数据会相同,更不用说 gps 位置了。所以我在设备上测试了多次,它每次都给了我一个唯一的哈希值!

编辑:我也稍微更改了代码,这似乎是问题所在。

  (JSON.stringify(form.value)).appendStr(JSON.stringify(this.chemInfo))

我试图使用 .toString() 而不是 JSON.stringify 将数组转换为字符串,后者返回的是对象而不是字符串。