keys.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // pages/keys/keys.js
  2. const app = getApp()
  3. const appConfig = require('../../config');
  4. const common = require('../../common');
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. publicKey : "",
  11. privateKey : "",
  12. background: "",
  13. },
  14. savePrivateKey : function(){
  15. if(this.data.publicKey != "" && this.data.privateKey != ""){
  16. common.saveKeys(this)
  17. }
  18. },
  19. /**
  20. * 生命周期函数--监听页面加载
  21. */
  22. onLoad: function (options) {
  23. var _this = this;
  24. _this.setData({
  25. background: common.getBackground()
  26. });
  27. wx.setNavigationBarTitle({
  28. title: '创建公私钥'
  29. });
  30. //如果已经有公私钥了就退出当前页面
  31. var sslKeys = wx.getStorageSync(appConfig.storeKeys.sslKeys)
  32. var serverInfo = wx.getStorageSync(appConfig.storeKeys.serverInfo)
  33. if (serverInfo['sslKeys'] && sslKeys['publicKey'] == serverInfo['sslKeys']['publicKey']){
  34. wx.showModal({
  35. title: '温馨提示',
  36. content: '您已经创建了公私钥,如果丢失,请重置公私钥~',
  37. success: function(item){
  38. app.globalData.isReloadIcon = true;
  39. wx.navigateBack();
  40. }
  41. })
  42. return;
  43. }
  44. app.sslKeyRequestSync = res => {
  45. if (res && res.code == 0) {
  46. _this.setData({
  47. publicKey: res.data.publicKey,
  48. privateKey: res.data.privateKey,
  49. });
  50. }else if(res && res.code == 5) {
  51. //force update
  52. }
  53. }
  54. app.sslKeySaveSync = res => {
  55. app.log('sslKeySaveSync', res);
  56. //保存公私钥到本地
  57. wx.setStorageSync(appConfig.storeKeys.sslKeys, { 'publicKey': this.data.publicKey, 'privateKey': this.data.privateKey });
  58. //更新serverInfo中用户的公私钥
  59. var serverInfo = wx.getStorageSync(appConfig.storeKeys.serverInfo);
  60. serverInfo.sslKeys = { 'publicKey': this.data.publicKey, 'privateKey': this.data.privateKey };
  61. wx.setStorageSync(appConfig.storeKeys.serverInfo, serverInfo);
  62. app.globalData.isReloadIcon = true;
  63. wx.showModal({
  64. title: '温馨提示',
  65. content: res.msg || "保存成功",
  66. showCancel: false,
  67. success: function(item){
  68. wx.navigateBack();
  69. }
  70. })
  71. }
  72. common.genKeys(this);
  73. },
  74. /**
  75. * 生命周期函数--监听页面初次渲染完成
  76. */
  77. onReady: function () {
  78. },
  79. /**
  80. * 生命周期函数--监听页面显示
  81. */
  82. onShow: function () {
  83. },
  84. /**
  85. * 生命周期函数--监听页面隐藏
  86. */
  87. onHide: function () {
  88. },
  89. /**
  90. * 生命周期函数--监听页面卸载
  91. */
  92. onUnload: function () {
  93. },
  94. /**
  95. * 页面相关事件处理函数--监听用户下拉动作
  96. */
  97. onPullDownRefresh: function () {
  98. },
  99. /**
  100. * 页面上拉触底事件的处理函数
  101. */
  102. onReachBottom: function () {
  103. },
  104. /**
  105. * 用户点击右上角分享
  106. */
  107. onShareAppMessage: function () {
  108. }
  109. })