password.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // pages/password/password.js
  2. const app = getApp()
  3. const appConfig = require('../../config');
  4. const common = require('../../common');
  5. const rand = require('../../js/rand');
  6. const utils = require('../../utils/util');
  7. const cache = require('../../js/cache');
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. id: "",
  14. name: "",
  15. password: "",
  16. account: "",
  17. link: "",
  18. note: "",
  19. pubKey: "",
  20. background: "",
  21. isEdit : false,
  22. passwordLength : 12,
  23. },
  24. inputAction: function(e) {
  25. var name = e.target.dataset.id;
  26. var data = {};
  27. if (name == 'passwordLength') {
  28. if(e.detail.value < 6) {
  29. data[name] = 6;
  30. this.setData(data);
  31. return;
  32. }
  33. }
  34. data[name] = e.detail.value;
  35. this.setData(data);
  36. },
  37. getBack: function() {
  38. wx.navigateBack();
  39. },
  40. randPassword: function() {
  41. var passwd = rand.randCombin("alpha,smallLetter,bigLetter,specialChars", this.data.passwordLength, true);
  42. this.setData({
  43. password: passwd
  44. });
  45. },
  46. minusLength: function(){
  47. if(this.data.passwordLength <= 6) {
  48. return;
  49. }
  50. this.data.passwordLength = this.data.passwordLength - 1;
  51. this.setData({
  52. passwordLength: this.data.passwordLength
  53. });
  54. this.randPassword();
  55. },
  56. addLength: function () {
  57. if(this.data.passwordLength >= 32) {
  58. return;
  59. }
  60. this.data.passwordLength = this.data.passwordLength + 1;
  61. this.setData({
  62. passwordLength: this.data.passwordLength
  63. });
  64. this.randPassword();
  65. },
  66. submitPassword: function() {
  67. var submitData = {};
  68. submitData.id = this.data.id;
  69. submitData.name = this.data.name;
  70. submitData.account = this.data.account;
  71. submitData.password = this.data.password;
  72. submitData.link = this.data.link;
  73. submitData.note = this.data.note;
  74. submitData.pubKey = this.data.pubKey;
  75. submitData.isEdit = this.data.isEdit;
  76. common.savePwd(this, submitData);
  77. },
  78. /**
  79. * 生命周期函数--监听页面加载
  80. */
  81. onLoad: function(options) {
  82. this.setData({
  83. background: utils.getBackground()
  84. });
  85. wx.setNavigationBarTitle({
  86. title: '创建我的密码'
  87. });
  88. this.setData({
  89. "title" : '创建我的密码'
  90. });
  91. var serverInfo = wx.getStorageSync("serverInfo");
  92. app.sslKeyDecryptSync = res => {
  93. this.setData(app.globalData.info, utils.getPrivateKey());
  94. if (!res || (res.code && res.code > 0)) {
  95. app.warning(res.msg || "解密失败,请确认您的私钥是否正确~");
  96. this.setData({
  97. password: "",
  98. });
  99. return;
  100. }
  101. this.setData({
  102. password: res.data.password
  103. });
  104. }
  105. app.storeSuccess = res => {
  106. if (!res || (res.code && res.code > 0)) {
  107. app.warning(res.msg || "保存失败");
  108. return;
  109. }
  110. //如果是线上的话就编辑本地数据
  111. if (!utils.getSwitchToLocalStatus()) {
  112. cache.editSingle(res.data);
  113. }
  114. wx.showModal({
  115. title: '温馨提示',
  116. content: res.msg || "保存成功",
  117. showCancel: false,
  118. success: function(item) {
  119. if (item.confirm) {
  120. app.globalData.isReloadLists = true;
  121. setTimeout(function() {
  122. wx.navigateBack();
  123. }, appConfig.duration);
  124. }
  125. }
  126. })
  127. }
  128. app.log("serverInfo", serverInfo)
  129. if (app.globalData.info != null && typeof app.globalData.info == 'object') {
  130. //去掉服务端解密过程
  131. //common.decrypt(app.globalData.info.password, serverInfo.sslKeys.privateKey);
  132. var res = { "code": 0, "msg": "解密成功" };
  133. try {
  134. var decrypt = utils.sslDecrypt(app.globalData.info.password, serverInfo.sslKeys.privateKey)
  135. if (decrypt == false) {
  136. res = { "code": 1, "msg": "解密失败,请检查您的私钥及密码串~" };
  137. } else {
  138. res.data = { password: decrypt };
  139. }
  140. } catch (e) {
  141. res.code = 400;
  142. res.msg = "解密失败,请检查您的私钥及密码串~";
  143. }
  144. app.sslKeyDecryptSync(res);
  145. wx.setNavigationBarTitle({
  146. title: '编辑我的密码',
  147. });
  148. this.setData({
  149. "title": '编辑我的密码'
  150. });
  151. this.setData({
  152. isEdit : true
  153. });
  154. }
  155. this.setData({
  156. 'pubKey': serverInfo.sslKeys.publicKey
  157. });
  158. },
  159. SetShadow(e) {
  160. this.setData({
  161. shadow: e.detail.value
  162. })
  163. },
  164. SetBorderSize(e) {
  165. this.setData({
  166. bordersize: e.detail.value
  167. })
  168. },
  169. /**
  170. * 生命周期函数--监听页面初次渲染完成
  171. */
  172. onReady: function() {
  173. },
  174. /**
  175. * 生命周期函数--监听页面显示
  176. */
  177. onShow: function() {
  178. },
  179. /**
  180. * 生命周期函数--监听页面隐藏
  181. */
  182. onHide: function() {
  183. },
  184. /**
  185. * 生命周期函数--监听页面卸载
  186. */
  187. onUnload: function() {
  188. },
  189. /**
  190. * 页面相关事件处理函数--监听用户下拉动作
  191. */
  192. onPullDownRefresh: function() {
  193. },
  194. /**
  195. * 页面上拉触底事件的处理函数
  196. */
  197. onReachBottom: function() {
  198. },
  199. /**
  200. * 用户点击右上角分享
  201. */
  202. onShareAppMessage: function() {
  203. }
  204. })