reset.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // pages/keys/keys.js
  2. const app = getApp()
  3. const appConfig = require('../../config');
  4. const common = require('../../common');
  5. const utils = require('../../utils/util');
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. publicKey: "",
  12. privateKey: "",
  13. opt: "",
  14. background: "",
  15. },
  16. textareaAInput : function(e){
  17. var name = e.target.dataset.id;
  18. app.log(name, e.detail.value);
  19. var data = {};
  20. data[name] = e.detail.value;
  21. this.setData(data);
  22. },
  23. resetPrivateKey: function () {
  24. var _this = this;
  25. wx.showModal({
  26. title: '是否要重置公私钥',
  27. content: '公私钥将重新生成,您保存的密码将无法解密,请确认是否要进行操作',
  28. confirmText: '重置',
  29. success: function (item) {
  30. if (item.confirm) {
  31. var sslKeys = utils.genSslKey();
  32. common.saveClientPublicKey(_this, sslKeys['publicKey'], sslKeys['privateKey'])
  33. }
  34. }
  35. })
  36. },
  37. savePrivateKey: function () {//直接保存用户生成密钥,需要校验公私钥
  38. var _this = this;
  39. //校验公私钥
  40. if (!utils.verifySslKey(_this.data.publicKey, _this.data.privateKey)) {
  41. app.warning("您的公私钥不匹配,请查证后再试~");
  42. return;
  43. }
  44. wx.showModal({
  45. title: '是否要使用自己公私钥',
  46. content: '公私钥保存后,您保存的密码可能无法解密,请确认是否要进行操作',
  47. confirmText: '保存',
  48. success: function (item) {
  49. if (item.confirm) {
  50. common.saveClientPublicKey(_this, _this.data.publicKey, _this.data.privateKey)
  51. }
  52. }
  53. })
  54. },
  55. /**
  56. * 生命周期函数--监听页面加载
  57. */
  58. onLoad: function (options) {
  59. var _this = this;
  60. _this.setData({
  61. background: utils.getBackground()
  62. });
  63. wx.setNavigationBarTitle({
  64. title: '重置私钥'
  65. });
  66. var serverInfo = wx.getStorageSync("serverInfo");
  67. this.setData({
  68. publicKey: utils.getPublicKey(),
  69. privateKey: utils.getPrivateKey(),
  70. });
  71. app.sslKeySaveSync = res => {
  72. if(!res || (res.code && res.code > 0)){
  73. app.warning(res.msg || "更新失败");
  74. return;
  75. }
  76. app.log('sslKeySaveSync', res);
  77. this.data.publicKey = res.data.publicKey;
  78. this.data.privateKey = res.data.privateKey;
  79. this.setData({
  80. publicKey : this.data.publicKey,
  81. privateKey : this.data.privateKey
  82. });
  83. //更新本地公私钥
  84. wx.setStorageSync(appConfig.storeKeys.sslKeys, { 'publicKey': this.data.publicKey, 'privateKey': this.data.privateKey });
  85. //更新serverInfo中用户的公私钥
  86. var serverInfo = wx.getStorageSync(appConfig.storeKeys.serverInfo);
  87. serverInfo.sslKeys = { 'publicKey': this.data.publicKey, 'privateKey': this.data.privateKey };
  88. app.log("更新 serverInfo", serverInfo);
  89. wx.setStorageSync(appConfig.storeKeys.serverInfo, serverInfo);
  90. app.globalData.isReloadLists = true;
  91. wx.showModal({
  92. title: '温馨提示',
  93. content: '请将您的私钥拷贝出来保存到一个安全的地方(点击确定内容将自动复制),以供以后恢复密码使用~',
  94. success:function(item){
  95. if(item.confirm) {
  96. wx.setClipboardData({
  97. data: _this.data.privateKey,
  98. success: function (res) {
  99. wx.getClipboardData({
  100. success: function (res) {
  101. }
  102. });
  103. },
  104. fail: function () {
  105. app.toast("复制失败");
  106. }
  107. });
  108. }
  109. }
  110. });
  111. app.globalData.isReloadIcon = true;
  112. }
  113. },
  114. /**
  115. * 生命周期函数--监听页面初次渲染完成
  116. */
  117. onReady: function () {
  118. },
  119. /**
  120. * 生命周期函数--监听页面显示
  121. */
  122. onShow: function () {
  123. },
  124. /**
  125. * 生命周期函数--监听页面隐藏
  126. */
  127. onHide: function () {
  128. },
  129. /**
  130. * 生命周期函数--监听页面卸载
  131. */
  132. onUnload: function () {
  133. },
  134. /**
  135. * 页面相关事件处理函数--监听用户下拉动作
  136. */
  137. onPullDownRefresh: function () {
  138. common.genKeys(this);
  139. },
  140. /**
  141. * 页面上拉触底事件的处理函数
  142. */
  143. onReachBottom: function () {
  144. },
  145. /**
  146. * 用户点击右上角分享
  147. */
  148. onShareAppMessage: function () {
  149. }
  150. })