我希望创建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的这个功能,所以任何反馈都有帮助。谢谢!
发布于 2018-06-07 13:30:30
我想我找到了答案。显然,我的本地机器上的某些东西导致哈希打印出相同的md5字符串,但是我继续在我的android设备上测试它,它运行良好!我知道一个事实,这是非常罕见的数据将是相同的,更不用说gps的位置。所以我在这个设备上进行了多次测试,每次都给了我一个唯一的散列!
编辑:我也把代码改了一点,这似乎就是问题所在。
(JSON.stringify(form.value)).appendStr(JSON.stringify(this.chemInfo))我试图使用.toString()来使数组字符串,而不是使用JSON.stringify,后者返回一个对象而不是一个字符串。
https://stackoverflow.com/questions/50727036
复制相似问题