(function (global,doc) {
/*
account:账户【dom => input】
password:密码【dom => input】
iStorage:记住【dom => input】
setRun:1||2||3:存|取|忘 【存和取都是记住状态,删一定是false】
queryState:fn 查询
remove: fn 删除
add: 添加
update: 更新密码
init: 初始化
*/
const vf = function (dataPack) {
//0.入参
this.opt = Object.assign({},dataPack);
const Account = this.opt.account;
const Password = this.opt.password;
const iStorage = this.opt.iStorage;
//1.查询
this.queryState = function () {
const user = JSON.parse(localStorage.getItem("QSLOGINLIST2018")) || false;
if (user) {
return user;
}else {
return false;
}
};
//2.添加
this.add = function (user,password,iStorage) {
const _this = this;
let dataArr = JSON.parse(localStorage.getItem('QSLOGINLIST2018'));
if (dataArr) {
const index = dataArr.findIndex(item => item.account === user);
if (index === -1){
dataArr.unshift({
account:user,
password:password,
iStorage:iStorage
});
localStorage.setItem("QSLOGINLIST2018",JSON.stringify(dataArr));
}else {
_this.update(dataArr,index,password,iStorage);
}
}else {
localStorage.setItem('QSLOGINLIST2018',JSON.stringify([
{
account:user,
password:password,
iStorage:iStorage
}
]));
}
};
//3.更新
this.update = function (dataArr,index,pwd,iStorage) {
const dataItem = dataArr[index];
dataItem.password = pwd;
dataItem.iStorage = iStorage;
dataArr.splice(index,1);
dataArr.unshift(dataItem);
localStorage.setItem("QSLOGINLIST2018",JSON.stringify(dataArr));
};
//4.删除
this.remove = function (dataArr,user,iStorage) {
const index = dataArr.findIndex(item => item.account === user);
if (index !== -1){
dataArr[index].password = '';
dataArr[index].iStorage = iStorage;
localStorage.setItem("QSLOGINLIST2018",JSON.stringify(dataArr));
}
};
//5.初始
this.init = function () {
const _this = this;
switch (_this.opt.setRun ){
//存|取|删//
case 1:
const user = Account.val();
const pwd = Password.val();
user !=='' && pwd !==''?
_this.add(user,pwd,iStorage):alert("账户密码不许为空!");
break;
case 2:
const data = _this.queryState();
if (data !== false) {
Account.val(data[0].account);
Password.val(data[0].password);
iStorage.prop('checked',data[0].iStorage);
Account.on("keyup",{data:data},_this.switch);
}else {
return console.warn("没有存储任何数据")
}
break;
case 3:
const data_ = _this.queryState();
if (data_ !== false) {
if (Account.val() !== '' && Password.val() !== ''){
_this.remove(data_,Account.val(),iStorage);
} else {
return alert("账户密码不许为空!");
}
}
break;
}
};
//6.切换用户
this.switch = function (e) {
const dataForm = e.data.data;
const query = function (d) {
Password.val('');
const index = dataForm.findIndex(item => item.account === d);
index !== -1 ? Password.val(dataForm[index].password):'';
};
query(this.value);
};
//自初始
this.init();
return this;
};
//兼容CommonJs规范
if (typeof module !== 'undefined' && module.exports) {
module.exports = FormTest;
}
//兼容AMD/CMD规范
if (typeof define === 'function') define(function () {
return vf;
});
doc.addEventListener('drop', function(e) {
e.stopPropagation();
e.preventDefault();
}, false);
//注册全局变量,兼容直接使用script标签引入插件
global.FormTest = vf;
})(window,document);