index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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. this.album.onPullDownRefresh();
  155. return;
  156. }
  157. if (this.data.PageCur != 'passwd-note') {
  158. return;
  159. }
  160. this.setData({
  161. isLoad: false,
  162. hiddenLoading: false,
  163. });
  164. wx.showNavigationBarLoading();
  165. common.lists(this, 1, appConfig.pageSize);
  166. wx.stopPullDownRefresh();
  167. },
  168. onReachBottom: function () {
  169. if (this.data.PageCur == 'album') {
  170. this.album.onReachBottom();
  171. return;
  172. }
  173. if (this.data.PageCur != 'passwd-note') {
  174. this.setData({
  175. "onReachBottom": true,
  176. });
  177. return;
  178. }
  179. if(!this.data.pages) {
  180. return;
  181. }
  182. if (this.data.pages.currentPage < this.data.pages.totalPage) {
  183. this.setData({
  184. hiddenLoading: false,
  185. });
  186. //加载下一页
  187. common.lists(this, parseInt(this.data.pages.currentPage) + 1, appConfig.pageSize);
  188. }
  189. },
  190. setToBackground: function(e){
  191. var url = e.target.dataset.src;
  192. this.setData({
  193. "background" : url
  194. });
  195. app.globalData.background = url;
  196. utils.setBackground(url);
  197. },
  198. //事件处理函数
  199. bindViewTap: function() {
  200. if (!utils.hasLogined()) {
  201. this.reLogin();
  202. }
  203. },
  204. clickItem: function (e) {
  205. var _this = this;
  206. var method = e.target.dataset.id;
  207. switch (method) {
  208. case "add-password":
  209. if (!utils.hasLogined()) {
  210. wx.showModal({
  211. title: '温馨提示',
  212. content: '您尚未登录,请登录后再试~',
  213. success: function(item) {
  214. if(item.confirm) {
  215. _this.reLogin();
  216. }
  217. }
  218. })
  219. return;
  220. }
  221. if (!utils.hasPublicKey()) {
  222. wx.navigateTo({
  223. url: '../keys/keys?to=' + encodeURIComponent("../password/password"),
  224. })
  225. return;
  226. }
  227. wx.navigateTo({
  228. url: '../password/password',
  229. });
  230. break;
  231. case "request-ssl-key":
  232. if (!utils.hasLogined()) {
  233. wx.showModal({
  234. title: '温馨提示',
  235. content: '您尚未登录,请登录后再试~',
  236. success: function (item) {
  237. if (item.confirm) {
  238. _this.reLogin();
  239. }
  240. }
  241. })
  242. return;
  243. }
  244. wx.navigateTo({
  245. url: '../keys/keys',
  246. });
  247. break;
  248. case "reset-ssl-key":
  249. if (!utils.hasLogined()) {
  250. wx.showModal({
  251. title: '温馨提示',
  252. content: '您尚未登录,请登录后再试~',
  253. success: function (item) {
  254. if (item.confirm) {
  255. _this.reLogin();
  256. }
  257. }
  258. })
  259. return;
  260. }
  261. if (!utils.hasPublicKey()) {
  262. wx.navigateTo({
  263. url: '../keys/keys?to=' + encodeURIComponent("../keys/keys"),
  264. })
  265. return;
  266. }
  267. wx.navigateTo({
  268. url: '../reset/reset',
  269. });
  270. break;
  271. case 'save-to-local':
  272. cache.download(1, 10000);
  273. break;
  274. }
  275. },
  276. onShow: function() {
  277. app.globalData.info = null;
  278. if (app.globalData.isReloadLists) {
  279. this.setData({
  280. hiddenLoading: false,
  281. });
  282. common.lists(this, 1, appConfig.pageSize);
  283. app.globalData.isReloadLists = false;
  284. }
  285. if (app.globalData.isReloadIcon) {
  286. this.reloadIcon(false);
  287. app.globalData.isReloadIcon = false;
  288. }
  289. this.setData({
  290. "switchToLocalText": utils.getSwitchToLocalStatus() ? "切换到线上使用" : "切换到本地使用"
  291. });
  292. },
  293. reLogin: function() {
  294. app.login();
  295. },
  296. editPwd: function(e) {
  297. var _this = this;
  298. app.infoSuccessSync = res => {
  299. if (!res || (res.code && res.code > 0)) {
  300. app.toast(res.msg || "获取失败");
  301. return;
  302. }
  303. app.globalData.info = res.data;
  304. wx.navigateTo({
  305. url: '/pages/password/password?id=' + res.data.id,
  306. })
  307. }
  308. if (!utils.syncServerInfo()) {
  309. wx.showModal({
  310. title: '温馨提示',
  311. content: '您尚未登录,请登录后再试~',
  312. success: function (item) {
  313. if (item.confirm) {
  314. _this.reLogin();
  315. }
  316. }
  317. })
  318. return;
  319. }else if(!utils.hasPublicKey() && !utils.hasPrivateKey()){
  320. wx.showModal({
  321. title: '温馨提示',
  322. content: '请先生成您的公私钥以便加解密密码',
  323. showCancel: false,
  324. success: function (item) {
  325. wx.navigateTo({
  326. url: '/pages/keys/keys',
  327. })
  328. }
  329. })
  330. } else if (!utils.hasPrivateKey()) {
  331. wx.showModal({
  332. title: '温馨提示',
  333. content: '请输入您的私钥以解密您的密码',
  334. showCancel: false,
  335. success: function(item) {
  336. wx.navigateTo({
  337. url: '/pages/privatekey/privatekey',
  338. })
  339. }
  340. })
  341. return;
  342. }
  343. var id = e.target.dataset.id;
  344. common.info(id);
  345. },
  346. removePwd: function(e) {
  347. var id = e.target.dataset.id;
  348. var name = e.target.dataset.name;
  349. app.removeSuccessSync = res => {
  350. if (!res || (res.code && res.code > 0)) {
  351. app.warning(res.msg || "删除失败");
  352. return;
  353. }
  354. wx.showModal({
  355. title: '温馨提示',
  356. content: '删除成功',
  357. showCancel: false,
  358. success: function(item) {}
  359. });
  360. for (var i in this.data.lists) {
  361. var list = this.data.lists[i];
  362. if (list.id == id) {
  363. this.data.lists.splice(i, 1);
  364. }
  365. }
  366. this.setData({
  367. lists: this.data.lists
  368. })
  369. }
  370. wx.showModal({
  371. title: '温馨提示',
  372. content: '数据一旦删除无法恢复,请确认是否要删除~',
  373. success: function(item) {
  374. if (item.confirm) {
  375. common.remove(id, name)
  376. }
  377. }
  378. });
  379. },
  380. copyPwd: function(e) {
  381. var _this = this;
  382. if(!utils.hasLogined()){
  383. wx.showModal({
  384. title: '温馨提示',
  385. content: '您尚未登录,请登录后再试~',
  386. success: function (item) {
  387. if (item.confirm) {
  388. _this.reLogin();
  389. }
  390. }
  391. })
  392. return;
  393. }
  394. if (!utils.hasPrivateKey()) {
  395. wx.showModal({
  396. title: '温馨提示',
  397. content: '请输入您的私钥以解密您的密码',
  398. success: function(item) {
  399. if(item.confirm) {
  400. wx.navigateTo({
  401. url: '/pages/privatekey/privatekey',
  402. })
  403. }
  404. }
  405. })
  406. return;
  407. }
  408. var str = e.target.dataset.val;
  409. if (!str || str == '') {
  410. app.warning("解密的密码不能为空~");
  411. return;
  412. }
  413. app.sslKeyDecryptSync = res => {
  414. if (!res || (res.code && res.code > 0)) {
  415. app.warning(res.msg || "解密失败");
  416. return;
  417. }
  418. var password = res.data.password;
  419. wx.showModal({
  420. title: '解密成功',
  421. content: '您的密码是:' + password,
  422. confirmText: '复制',
  423. success: function(item) {
  424. if (item.confirm) {
  425. wx.setClipboardData({
  426. data: password,
  427. success: function(res) {
  428. wx.getClipboardData({
  429. success: function(res) {
  430. }
  431. });
  432. },
  433. fail: function(){
  434. app.toast("复制失败");
  435. }
  436. });
  437. }
  438. }
  439. })
  440. }
  441. var res = { "code": 0, "msg": "解密成功" };
  442. try{
  443. var decrypt = utils.sslDecrypt(str, utils.getPrivateKey())
  444. if (decrypt == false) {
  445. res = { "code": 1, "msg": "解密失败,请检查您的私钥及密码串~" };
  446. } else {
  447. res.data = { password: decrypt };
  448. }
  449. }catch(e){
  450. res.code = 400;
  451. res.msg = "解密失败,请检查您的私钥及密码串~";
  452. }
  453. app.sslKeyDecryptSync(res);
  454. //去掉网络请求
  455. //utils.decrypt(str, utils.getPrivateKey());
  456. },
  457. reloadIcon: function(loadList) {
  458. var _this = this;
  459. if (!utils.syncServerInfo()) {
  460. wx.showModal({
  461. title: '温馨提示',
  462. content: '您尚未登录,请登录后再试~',
  463. success: function (item) {
  464. if (item.confirm) {
  465. _this.reLogin();
  466. }
  467. }
  468. })
  469. return;
  470. } else if (!utils.hasPublicKey() && !utils.hasPrivateKey()){
  471. wx.showModal({
  472. title: '温馨提示',
  473. content: '请先生成您的公私钥以便加解密密码',
  474. success: function (item) {
  475. if (item.confirm) {
  476. wx.navigateTo({
  477. url: '/pages/keys/keys',
  478. })
  479. }
  480. }
  481. });
  482. } /*else if (!utils.hasPrivateKey()) {
  483. wx.showModal({
  484. title: '温馨提示',
  485. content: '请输入您的私钥以解密您的密码',
  486. success: function(item) {
  487. if (item.confirm) {
  488. wx.navigateTo({
  489. url: '/pages/privatekey/privatekey',
  490. })
  491. }
  492. }
  493. });
  494. }*/
  495. if (utils.hasPublicKey()) {
  496. this.setData({
  497. 'iconCls': appConfig.noPublicKeyIcon.length,
  498. 'iconList': appConfig.noPublicKeyIcon
  499. });
  500. }
  501. if (utils.syncServerInfo() && loadList) {
  502. this.setData({
  503. hiddenLoading: false,
  504. });
  505. common.lists(this, 1, appConfig.pageSize)
  506. }
  507. },
  508. onLoad: function() {
  509. var _this = this;
  510. app.syncRes = res => {
  511. if (res.code == 0) {
  512. app.toast("同步成功~");
  513. }else{
  514. app.warning("同步失败~");
  515. }
  516. }
  517. app.listSync = res => {
  518. if(!res || !res.pages) {
  519. app.warning(res.msg || "获取数据失败");
  520. if (res.code == '40001') {
  521. _this.reLogin();
  522. }
  523. return;
  524. }
  525. this.setData({
  526. hiddenLoading : true,
  527. });
  528. setTimeout(function () {
  529. wx.hideNavigationBarLoading();
  530. }, 1000);
  531. if (res.pages.currentPage == res.pages.totalPage) {
  532. this.setData({
  533. "isLoad" : true
  534. });
  535. this.setData({
  536. hiddenLoading: false,
  537. });
  538. }
  539. var lists = [];
  540. if(res.pages.currentPage == 1) {
  541. lists = res.lists;
  542. }else{
  543. lists = this.data.lists;
  544. for (var i in res.lists) {
  545. lists.push(res.lists[i]);
  546. }
  547. }
  548. this.setData({
  549. lists: lists,
  550. pages: res.pages
  551. });
  552. }
  553. //同步数据回调
  554. app.syncSuccess = res => {
  555. if(!res || res.code > 0) {
  556. app.warning(res.msg || "同步失败");
  557. return;
  558. }
  559. app.toast("同步成功");
  560. if (utils.getSwitchToLocalStatus) {
  561. common.lists(this, 1, appConfig.pageSize);
  562. }
  563. }
  564. //保存数据到本地回调
  565. app.storeToLocal = res => {
  566. }
  567. app.serInfoReady = res => {
  568. var _this = this;
  569. if (!res || res.code > 0 || !res.data || !res.data.sslKeys) {
  570. if (res.code == 10005) {
  571. //删除本地登录信息,重新登录
  572. wx.clearStorageSync();
  573. }
  574. wx.showModal({
  575. title: '温馨提示',
  576. content: res.msg || "登录失败,请重试 ~",
  577. showCancel: false,
  578. success: function(item) {
  579. _this.reLogin();
  580. }
  581. })
  582. return;
  583. }
  584. this.reloadIcon(true);
  585. }
  586. app.globalData.serverInfo = utils.syncServerInfo();
  587. app.globalData.hasPublicKey = utils.hasPublicKey();
  588. if (utils.hasPublicKey()) {
  589. this.setData({
  590. 'iconCls': appConfig.noPublicKeyIcon.length,
  591. 'iconList': appConfig.noPublicKeyIcon
  592. });
  593. } else {
  594. this.setData({
  595. 'iconCls': appConfig.iconList.length,
  596. "iconList": appConfig.iconList
  597. });
  598. }
  599. if (app.globalData.userInfo) {
  600. this.setData({
  601. userInfo: app.globalData.userInfo,
  602. hasUserInfo: true
  603. });
  604. common.login(this);
  605. } else if (this.data.canIUse) {
  606. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  607. // 所以此处加入 callback 以防止这种情况
  608. app.userInfoReadyCallback = res => {
  609. app.globalData.res = res
  610. common.login(this)
  611. this.setData({
  612. motto: "正在登录,请稍后...",
  613. userInfo: res.userInfo,
  614. hasUserInfo: true,
  615. })
  616. }
  617. } else {
  618. this.reLogin();
  619. }
  620. this.setData({
  621. background: utils.getBackground()
  622. });
  623. },
  624. getUserInfo: function(e) {
  625. if(!e.detail.userInfo){
  626. app.warning("获取用户信息失败,请先授权此应用访问您的基本信息~");
  627. return;
  628. }
  629. /*
  630. app.globalData.userInfo = e.detail.userInfo;
  631. app.globalData.res = e.detail;
  632. this.setData({
  633. userInfo: e.detail.userInfo,
  634. hasUserInfo: true
  635. })
  636. common.login(this)*/
  637. app.login();
  638. },
  639. onShareAppMessage() {
  640. return {
  641. title: 'mobi密码本,您的密码守护专家',
  642. imageUrl: '/images/share.png',
  643. path: '/pages/index/index'
  644. }
  645. },
  646. })