password.js 5.3 KB

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