savePwd.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. const app = getApp()
  2. const appConfig = require("../config");
  3. const valid = require("../js/valid");
  4. const utils = require("../utils/util");
  5. const cache = require("cache");
  6. var savePwd = function(that, data){
  7. if(!data) {
  8. wx.showToast({
  9. title: '参数不正确,请检查后重试!~',
  10. duration: appConfig.duration
  11. });
  12. return;
  13. }
  14. if (!valid.minlength(data.name, 2)) {
  15. app.warning("名称不能少于2个字符");
  16. return;
  17. }
  18. if (!valid.minlength(data.account, 2)) {
  19. app.warning("账户名称不能少于3个字符");
  20. return;
  21. }
  22. if (!valid.minlength(data.password, 6)) {
  23. app.warning("密码不能少于6个字符");
  24. return;
  25. }
  26. if(!valid.isHttp(data.link)) {
  27. app.warning("链接地址不正确");
  28. return;
  29. }
  30. var header = {
  31. 'content-type': 'application/x-www-form-urlencoded',
  32. 'cookie': wx.getStorageSync(appConfig.storeKeys.sessionId)
  33. };
  34. if (utils.getSwitchToLocalStatus()){
  35. var timestamp = Date.parse(new Date());
  36. data.password = utils.encrypt(data.password, data.pubKey);
  37. delete data.pubKey;
  38. data.create_at = data.update_at = timestamp.toString().substring(0, 10);
  39. data.create_time = utils.showCreateTime();
  40. if(data.isEdit) {
  41. delete data.isEdit;
  42. cache.editSingle(data);
  43. }else{
  44. cache.addItem(data);
  45. }
  46. app.storeSuccess({
  47. code : 0,
  48. msg : "保存成功"
  49. });
  50. return;
  51. }
  52. wx.request({
  53. url: utils.getUrl('savePwd'),
  54. data : data,
  55. header : header,
  56. method : "POST",
  57. dataType : "json",
  58. success : function(res) {
  59. app.log(res)
  60. app.storeSuccess(res.data);
  61. },
  62. fail : function(res) {
  63. wx.showToast({
  64. title: '保存失败,请重试',
  65. duration: appConfig.duration
  66. });
  67. app.storeSuccess(appConfig.networkError);
  68. }
  69. })
  70. }
  71. module.exports = savePwd