password.js 5.1 KB

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