index.js 16 KB

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