savePwd.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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.storeSuccess(res.data);
  60. },
  61. fail : function(res) {
  62. wx.showToast({
  63. title: '保存失败,请重试',
  64. duration: appConfig.duration
  65. });
  66. app.storeSuccess(appConfig.networkError);
  67. }
  68. })
  69. }
  70. module.exports = savePwd