index.js 17 KB

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