index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. //index.js
  2. //获取应用实例
  3. const app = getApp()
  4. const appConfig = require('../../config');
  5. const common = require('../../common');
  6. const utils = require('../../utils/util');
  7. const cache = require('../../js/cache');
  8. Page({
  9. data: {
  10. motto: '请点击上边图标以授权此应用',
  11. userInfo: {},
  12. hiddenStatus: false,
  13. showLists: false,
  14. hasUserInfo: false,
  15. canIUse: wx.canIUse('button.open-type.getUserInfo'),
  16. serverInfo: null,
  17. hasServerInfo: false,
  18. PageCur: 'passwd-note',
  19. lists: null,
  20. pages: null,
  21. hasPublicKey: false,
  22. iconCls: 3,
  23. background: "",
  24. switchToLocalText: "",
  25. colors: ["bg-yellow", "bg-green", "bg-olive", "bg-green", "bg-cyan", "bg-blue", "bg-purple", "bg-mauve", "bg-pink", "bg-brown", "bg-grey", "bg-black"],
  26. isLoad: false,
  27. cardCur: 0,
  28. DotStyle: "square-dot",
  29. swiperList: [{
  30. id: 0,
  31. type: 'image',
  32. url: '/images/theme/10001.jpg'
  33. }, {
  34. id: 1,
  35. type: 'image',
  36. url: '/images/theme/10002.jpg',
  37. }, {
  38. id: 2,
  39. type: 'image',
  40. url: '/images/theme/10003.jpg'
  41. }, {
  42. id: 3,
  43. type: 'image',
  44. url: '/images/theme/10004.jpg'
  45. }, {
  46. id: 4,
  47. type: 'image',
  48. url: '/images/theme/10005.jpg'
  49. }, {
  50. id: 5,
  51. type: 'image',
  52. url: '/images/theme/10006.jpg'
  53. }, {
  54. id: 6,
  55. type: 'image',
  56. url: '/images/theme/10007.jpg'
  57. }],
  58. },
  59. NavChange(e) {
  60. this.setData({
  61. PageCur: e.currentTarget.dataset.cur,
  62. switchToLocalText: utils.getSwitchToLocalStatus() ? "切换到线上使用" : "切换到本地使用"
  63. })
  64. },
  65. showModal(e) {
  66. this.setData({
  67. modalName: e.currentTarget.dataset.target
  68. })
  69. },
  70. hideModal(e) {
  71. this.setData({
  72. modalName: null
  73. })
  74. },
  75. gridchange: function(e) {
  76. this.setData({
  77. gridCol: e.detail.value
  78. });
  79. },
  80. gridswitch: function(e) {
  81. this.setData({
  82. gridBorder: e.detail.value
  83. });
  84. },
  85. menuBorder: function(e) {
  86. this.setData({
  87. menuBorder: e.detail.value
  88. });
  89. },
  90. menuArrow: function(e) {
  91. this.setData({
  92. menuArrow: e.detail.value
  93. });
  94. },
  95. menuCard: function(e) {
  96. this.setData({
  97. menuCard: e.detail.value
  98. });
  99. },
  100. switchSex: function(e) {
  101. this.setData({
  102. skin: e.detail.value
  103. });
  104. },
  105. // ListTouch触摸开始
  106. ListTouchStart(e) {
  107. this.setData({
  108. ListTouchStart: e.touches[0].pageX
  109. })
  110. },
  111. // ListTouch计算方向
  112. ListTouchMove(e) {
  113. this.setData({
  114. ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
  115. })
  116. },
  117. // ListTouch计算滚动
  118. ListTouchEnd(e) {
  119. if (this.data.ListTouchDirection == 'left') {
  120. this.setData({
  121. modalName: e.currentTarget.dataset.target
  122. })
  123. } else {
  124. this.setData({
  125. modalName: null
  126. })
  127. }
  128. this.setData({
  129. ListTouchDirection: null
  130. })
  131. },
  132. onPullDownRefresh:function(){
  133. wx.showNavigationBarLoading();
  134. common.lists(this, 1, appConfig.pageSize);
  135. wx.stopPullDownRefresh();
  136. },
  137. onReachBottom: function(){
  138. if(!this.data.pages) {
  139. return;
  140. }
  141. if (this.data.pages.currentPage < this.data.pages.totalPage) {
  142. this.setData({
  143. hiddenLoading: false,
  144. });
  145. //加载下一页
  146. common.lists(this, parseInt(this.data.pages.currentPage) + 1, appConfig.pageSize);
  147. }
  148. },
  149. setToBackground: function(e){
  150. var url = e.target.dataset.src;
  151. this.setData({
  152. "background" : url
  153. });
  154. app.globalData.background = url;
  155. utils.setBackground(url);
  156. },
  157. //事件处理函数
  158. bindViewTap: function() {
  159. if (!utils.hasLogined()) {
  160. app.log("bindViewTap reLogin");
  161. this.reLogin();
  162. }
  163. },
  164. clickItem: function (e) {
  165. app.log("utils.hasLogined", utils.hasLogined());
  166. var _this = this;
  167. var method = e.target.dataset.id;
  168. switch (method) {
  169. case "add-password":
  170. if (!utils.hasLogined()) {
  171. wx.showModal({
  172. title: '温馨提示',
  173. content: '您尚未登录,请登录后再试~',
  174. success: function(item) {
  175. if(item.confirm) {
  176. _this.reLogin();
  177. }
  178. }
  179. })
  180. return;
  181. }
  182. if (!utils.hasPublicKey()) {
  183. wx.navigateTo({
  184. url: '../keys/keys?to=' + encodeURIComponent("../password/password"),
  185. })
  186. return;
  187. }
  188. wx.navigateTo({
  189. url: '../password/password',
  190. });
  191. break;
  192. case "request-ssl-key":
  193. if (!utils.hasLogined()) {
  194. wx.showModal({
  195. title: '温馨提示',
  196. content: '您尚未登录,请登录后再试~',
  197. success: function (item) {
  198. if (item.confirm) {
  199. _this.reLogin();
  200. }
  201. }
  202. })
  203. return;
  204. }
  205. wx.navigateTo({
  206. url: '../keys/keys',
  207. });
  208. break;
  209. case "reset-ssl-key":
  210. if (!utils.hasLogined()) {
  211. wx.showModal({
  212. title: '温馨提示',
  213. content: '您尚未登录,请登录后再试~',
  214. success: function (item) {
  215. if (item.confirm) {
  216. _this.reLogin();
  217. }
  218. }
  219. })
  220. return;
  221. }
  222. if (!utils.hasPublicKey()) {
  223. wx.navigateTo({
  224. url: '../keys/keys?to=' + encodeURIComponent("../keys/keys"),
  225. })
  226. return;
  227. }
  228. wx.navigateTo({
  229. url: '../reset/reset',
  230. });
  231. break;
  232. case 'save-to-local':
  233. app.log('save-to-local');
  234. cache.download(1, 10000);
  235. break;
  236. }
  237. },
  238. onShow: function() {
  239. app.globalData.info = null;
  240. if (app.globalData.isReloadLists) {
  241. this.setData({
  242. hiddenLoading: false,
  243. });
  244. common.lists(this, 1, appConfig.pageSize);
  245. app.globalData.isReloadLists = false;
  246. }
  247. if (app.globalData.isReloadIcon) {
  248. this.reloadIcon(false);
  249. app.globalData.isReloadIcon = false;
  250. }
  251. this.setData({
  252. "switchToLocalText": utils.getSwitchToLocalStatus() ? "切换到线上使用" : "切换到本地使用"
  253. });
  254. },
  255. reLogin: function() {
  256. app.login();
  257. },
  258. editPwd: function(e) {
  259. var _this = this;
  260. app.infoSuccessSync = res => {
  261. app.log(res)
  262. if (!res || (res.code && res.code > 0)) {
  263. app.toast(res.msg || "获取失败");
  264. return;
  265. }
  266. app.globalData.info = res.data;
  267. wx.navigateTo({
  268. url: '/pages/password/password?id=' + res.data.id,
  269. })
  270. }
  271. if (!utils.syncServerInfo()) {
  272. wx.showModal({
  273. title: '温馨提示',
  274. content: '您尚未登录,请登录后再试~',
  275. success: function (item) {
  276. if (item.confirm) {
  277. _this.reLogin();
  278. }
  279. }
  280. })
  281. return;
  282. }else if(!utils.hasPublicKey() && !utils.hasPrivateKey()){
  283. wx.showModal({
  284. title: '温馨提示',
  285. content: '请先生成您的公私钥以便加解密密码',
  286. showCancel: false,
  287. success: function (item) {
  288. wx.navigateTo({
  289. url: '/pages/keys/keys',
  290. })
  291. }
  292. })
  293. } else if (!utils.hasPrivateKey()) {
  294. wx.showModal({
  295. title: '温馨提示',
  296. content: '请输入您的私钥以解密您的密码',
  297. showCancel: false,
  298. success: function(item) {
  299. wx.navigateTo({
  300. url: '/pages/privatekey/privatekey',
  301. })
  302. }
  303. })
  304. return;
  305. }
  306. var id = e.target.dataset.id;
  307. common.info(id);
  308. },
  309. removePwd: function(e) {
  310. var id = e.target.dataset.id;
  311. var name = e.target.dataset.name;
  312. app.log(id, name)
  313. app.removeSuccessSync = res => {
  314. app.log("app.removeSuccessSync");
  315. if (!res || (res.code && res.code > 0)) {
  316. app.warning(res.msg || "删除失败");
  317. return;
  318. }
  319. wx.showModal({
  320. title: '温馨提示',
  321. content: '删除成功',
  322. showCancel: false,
  323. success: function(item) {}
  324. });
  325. for (var i in this.data.lists) {
  326. var list = this.data.lists[i];
  327. if (list.id == id) {
  328. this.data.lists.splice(i, 1);
  329. }
  330. }
  331. this.setData({
  332. lists: this.data.lists
  333. })
  334. }
  335. wx.showModal({
  336. title: '温馨提示',
  337. content: '数据一旦删除无法恢复,请确认是否要删除~',
  338. success: function(item) {
  339. if (item.confirm) {
  340. common.remove(id, name)
  341. }
  342. }
  343. });
  344. },
  345. copyPwd: function(e) {
  346. var _this = this;
  347. if(!utils.hasLogined()){
  348. wx.showModal({
  349. title: '温馨提示',
  350. content: '您尚未登录,请登录后再试~',
  351. success: function (item) {
  352. if (item.confirm) {
  353. _this.reLogin();
  354. }
  355. }
  356. })
  357. return;
  358. }
  359. if (!utils.hasPrivateKey()) {
  360. wx.showModal({
  361. title: '温馨提示',
  362. content: '请输入您的私钥以解密您的密码',
  363. success: function(item) {
  364. if(item.confirm) {
  365. wx.navigateTo({
  366. url: '/pages/privatekey/privatekey',
  367. })
  368. }
  369. }
  370. })
  371. return;
  372. }
  373. var str = e.target.dataset.val;
  374. if (!str || str == '') {
  375. app.warning("解密的密码不能为空~");
  376. return;
  377. }
  378. app.sslKeyDecryptSync = res => {
  379. app.log(res);
  380. if (!res || (res.code && res.code > 0)) {
  381. app.warning(res.msg || "解密失败");
  382. return;
  383. }
  384. var password = res.data.password;
  385. wx.showModal({
  386. title: '解密成功',
  387. content: '您的密码是:' + password,
  388. confirmText: '复制',
  389. success: function(item) {
  390. if (item.confirm) {
  391. wx.setClipboardData({
  392. data: password,
  393. success: function(res) {
  394. wx.getClipboardData({
  395. success: function(res) {
  396. }
  397. });
  398. },
  399. fail: function(){
  400. app.toast("复制失败");
  401. }
  402. });
  403. }
  404. }
  405. })
  406. }
  407. var res = { "code": 0, "msg": "解密成功" };
  408. try{
  409. var decrypt = utils.sslDecrypt(str, utils.getPrivateKey())
  410. if (decrypt == false) {
  411. res = { "code": 1, "msg": "解密失败,请检查您的私钥及密码串~" };
  412. } else {
  413. res.data = { password: decrypt };
  414. }
  415. }catch(e){
  416. res.code = 400;
  417. res.msg = "解密失败,请检查您的私钥及密码串~";
  418. }
  419. app.sslKeyDecryptSync(res);
  420. //去掉网络请求
  421. //utils.decrypt(str, utils.getPrivateKey());
  422. },
  423. reloadIcon: function(loadList) {
  424. var _this = this;
  425. if (!utils.syncServerInfo()) {
  426. wx.showModal({
  427. title: '温馨提示',
  428. content: '您尚未登录,请登录后再试~',
  429. success: function (item) {
  430. if (item.confirm) {
  431. _this.reLogin();
  432. }
  433. }
  434. })
  435. return;
  436. } else if (!utils.hasPublicKey() && !utils.hasPrivateKey()){
  437. wx.showModal({
  438. title: '温馨提示',
  439. content: '请先生成您的公私钥以便加解密密码',
  440. success: function (item) {
  441. if (item.confirm) {
  442. wx.navigateTo({
  443. url: '/pages/keys/keys',
  444. })
  445. }
  446. }
  447. });
  448. } /*else if (!utils.hasPrivateKey()) {
  449. wx.showModal({
  450. title: '温馨提示',
  451. content: '请输入您的私钥以解密您的密码',
  452. success: function(item) {
  453. app.log(item);
  454. if (item.confirm) {
  455. wx.navigateTo({
  456. url: '/pages/privatekey/privatekey',
  457. })
  458. }
  459. }
  460. });
  461. }*/
  462. if (utils.hasPublicKey()) {
  463. this.setData({
  464. 'iconCls': appConfig.noPublicKeyIcon.length,
  465. 'iconList': appConfig.noPublicKeyIcon
  466. });
  467. }
  468. if (utils.syncServerInfo() && loadList) {
  469. this.setData({
  470. hiddenLoading: false,
  471. });
  472. common.lists(this, 1, appConfig.pageSize)
  473. }
  474. },
  475. onLoad: function() {
  476. var _this = this;
  477. app.syncRes = res => {
  478. if (res.code == 0) {
  479. app.toast("同步成功~");
  480. }else{
  481. app.warning("同步失败~");
  482. }
  483. }
  484. app.listSync = res => {
  485. if(!res || !res.pages) {
  486. app.warning(res.msg || "获取数据失败");
  487. if (res.code == '40001') {
  488. _this.reLogin();
  489. }
  490. return;
  491. }
  492. this.setData({
  493. hiddenLoading : true,
  494. });
  495. setTimeout(function () {
  496. wx.hideNavigationBarLoading();
  497. }, 1000);
  498. if (res.pages.currentPage == res.pages.totalPage) {
  499. this.setData({
  500. "isLoad" : true
  501. });
  502. this.setData({
  503. hiddenLoading: false,
  504. });
  505. }
  506. var lists = [];
  507. if(res.pages.currentPage == 1) {
  508. lists = res.lists;
  509. }else{
  510. lists = this.data.lists;
  511. for (var i in res.lists) {
  512. lists.push(res.lists[i]);
  513. }
  514. }
  515. this.setData({
  516. lists: lists,
  517. pages: res.pages
  518. });
  519. }
  520. //同步数据回调
  521. app.syncSuccess = res => {
  522. if(!res || res.code > 0) {
  523. app.warning(res.msg || "同步失败");
  524. return;
  525. }
  526. app.toast("同步成功");
  527. if (utils.getSwitchToLocalStatus) {
  528. common.lists(this, 1, appConfig.pageSize);
  529. }
  530. }
  531. //保存数据到本地回调
  532. app.storeToLocal = res => {
  533. app.log(res);
  534. }
  535. app.serInfoReady = res => {
  536. var _this = this;
  537. app.log("app.serInfoReady(serverInfo)", res);
  538. app.log("app.serInfoReady(serverInfo).code", res.code);
  539. if (!res || res.code > 0 || !res.data || !res.data.sslKeys) {
  540. if (res.code == 10005) {
  541. //删除本地登录信息,重新登录
  542. wx.clearStorageSync();
  543. }
  544. wx.showModal({
  545. title: '温馨提示',
  546. content: res.msg || "登录失败,请重试 ~",
  547. showCancel: false,
  548. success: function(item) {
  549. app.log(item);
  550. _this.reLogin();
  551. }
  552. })
  553. return;
  554. }
  555. this.reloadIcon(true);
  556. }
  557. app.globalData.serverInfo = utils.syncServerInfo();
  558. app.globalData.hasPublicKey = utils.hasPublicKey();
  559. if (utils.hasPublicKey()) {
  560. this.setData({
  561. 'iconCls': appConfig.noPublicKeyIcon.length,
  562. 'iconList': appConfig.noPublicKeyIcon
  563. });
  564. } else {
  565. this.setData({
  566. 'iconCls': appConfig.iconList.length,
  567. "iconList": appConfig.iconList
  568. });
  569. }
  570. if (app.globalData.userInfo) {
  571. this.setData({
  572. userInfo: app.globalData.userInfo,
  573. hasUserInfo: true
  574. });
  575. common.login(this);
  576. } else if (this.data.canIUse) {
  577. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  578. // 所以此处加入 callback 以防止这种情况
  579. app.userInfoReadyCallback = res => {
  580. app.log("app.userInfoReadyCallback");
  581. app.globalData.res = res
  582. common.login(this)
  583. app.log(1, res.userInfo)
  584. this.setData({
  585. motto: "正在登录,请稍后...",
  586. userInfo: res.userInfo,
  587. hasUserInfo: true,
  588. })
  589. }
  590. } else {
  591. this.reLogin();
  592. }
  593. this.setData({
  594. background: utils.getBackground()
  595. });
  596. },
  597. getUserInfo: function(e) {
  598. if(!e.detail.userInfo){
  599. app.warning("获取用户信息失败,请先授权此应用访问您的基本信息~");
  600. return;
  601. }
  602. /*
  603. app.globalData.userInfo = e.detail.userInfo;
  604. app.globalData.res = e.detail;
  605. this.setData({
  606. userInfo: e.detail.userInfo,
  607. hasUserInfo: true
  608. })
  609. common.login(this)*/
  610. app.login();
  611. },
  612. onShareAppMessage() {
  613. return {
  614. title: 'mobi密码本,您的密码守护专家',
  615. imageUrl: '/images/share.png',
  616. path: '/pages/index/index'
  617. }
  618. },
  619. })