login.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. <template>
  2. <view>
  3. <!-- 状态栏 -->
  4. <uni-status-bar></uni-status-bar>
  5. <!-- 公共组件-每个页面必须引入 -->
  6. <public-module></public-module>
  7. <!-- 欢迎语 -->
  8. <view class="w-100 flex-column box-sizing font-weight" style="height: 450rpx;padding:135rpx 60rpx;">
  9. <view class="font-lgr">您好,</view>
  10. <view class="font-lgr">欢迎来到掌柜科技</view>
  11. </view>
  12. <!-- 登录区域Start -->
  13. <view class="mx-3">
  14. <!-- 账号密码登录 false代表账号密码登录 -->
  15. <template v-if="!status">
  16. <view class="mt-1 border-bottom">
  17. <input type="text" v-model="username" class="uni-input common-input" maxlength="17"
  18. placeholder="请输入会员号" />
  19. </view>
  20. <view class="mt-2 position-relative border-bottom">
  21. <input type="text" :password="isHidePassword" v-model="password" class="uni-input common-input"
  22. placeholder="请输入密码" style="padding-right: 200rpx;" maxlength="15" />
  23. <view class=" position-absolute top-0 right-0 d-flex a-center j-center"
  24. style="width: 200rpx;height: 100%;">
  25. <view style="font-size: 40rpx;" class="icon iconfont"
  26. :class="isHidePassword?'icon-buxianshimima':'icon-xianshimima'"
  27. @tap="isHidePassword = !isHidePassword"></view>
  28. </view>
  29. </view>
  30. <view class="mt-2 position-relative border-bottom">
  31. <input type="text" v-model="imgcode" class="uni-input common-input" maxlength="5"
  32. placeholder="请输入验证码" style="padding-right: 200rpx;" />
  33. <image style="height: 80%;padding-top: 4rpx;width: 200rpx;" mode="aspectFill"
  34. class="position-absolute top-0 right-0 d-flex a-center j-center text-light-muted"
  35. :src="imgcodesrc" @tap="refreshCaptcha"></image>
  36. </view>
  37. </template>
  38. <!-- 手机验证码登录 true代表手机验证码登录 -->
  39. <template v-else>
  40. <view class="mt-1 position-relative border-bottom">
  41. <view class="position-absolute d-flex a-center j-center left-0 top-0 font-weight"
  42. style="width: 100rpx;height: 100%;">+86</view>
  43. <input type="text" v-model="phone" class="uni-input common-input" style="padding-left: 100rpx;"
  44. placeholder="手机号" />
  45. </view>
  46. <view class="mt-2 position-relative border-bottom">
  47. <input type="text" v-model="checknum" class="uni-input common-input" maxlength="4"
  48. style="padding-right: 240rpx;" placeholder="请输入验证码" />
  49. <view class="position-absolute top-0 right-0 d-flex a-center j-center text-light-muted"
  50. style="width: 240rpx;height: 100%;" @tap="getCheckNum">
  51. <view class="d-flex a-center j-center "
  52. :class="!codetime?'yanzhengmaView1 main-text-color':'yanzhengmaView2'">
  53. {{!codetime?'获取验证码':codetime+' S'+'后重新获取'}}
  54. </view>
  55. </view>
  56. </view>
  57. </template>
  58. <button class="my-3 mt-4 d-flex a-center j-center font-weight main-bg-color" :loading="loading"
  59. :class="{'main-bf-hover-color':disabled}" type="primary" @tap="submit" :disabled="disabled"
  60. style="letter-spacing: 20rpx;">登录</button>
  61. <!-- 登录状态切换 -->
  62. <view class="pt-1 px-3 pb-0 text-light-muted d-flex j-sb">
  63. <view @tap="changeStatus">
  64. {{status?'密码登录':'验证码登录'}}
  65. </view>
  66. <!-- <view>
  67. <view @tap="navigate({url:'/pages/register/register'})">用户注册</view>
  68. </view> -->
  69. </view>
  70. <view class="third_party_login_box">
  71. <view class="third_party_title"><text>第三方登录</text></view>
  72. <view class="third_party_content flex-wrap">
  73. <image src="/static/icon/weixin.png" @click=" onWxAppLogin" mode="aspectFit"></image>
  74. <!-- <image src="/static/icon/zhifubao.png" mode="aspectFit"></image> -->
  75. </view>
  76. </view>
  77. <view class="position-fixed left-0 right-0 d-flex a-center j-center pt-2 text-light-muted"
  78. style="bottom:30rpx">
  79. 登录/<span>注册</span>表示您已同意<view> <text @tap="nav('/pages/login/xieyi')"
  80. class="main-text-color">平台协议</text>及<text class="main-text-color"
  81. @tap="nav('/pages/login/mimi')">隐私协议</text>政策</view>
  82. </view>
  83. </view>
  84. <u-popup v-model="show" border-radius="14" mode="center" width="70%">
  85. <view class="shu1 shili">
  86. <text>温馨提示</text>
  87. <text>是否更新账号密码?</text>
  88. </view>
  89. <view class="heng shili_btn">
  90. <view class="heng1" @click="fanhui">取消</view>
  91. <view class="heng1" @click="zhifu" style="background-color: orangered;">确认</view>
  92. </view>
  93. </u-popup>
  94. </view>
  95. </template>
  96. <script>
  97. import md5 from '@/plugins/md5';
  98. let clear;
  99. import {
  100. mapState,
  101. mapMutations
  102. } from 'vuex';
  103. import socket from '@/config/socket';
  104. import DB from "@/common/sqlite";
  105. export default {
  106. data() {
  107. return {
  108. show: false,
  109. contentSlot: true,
  110. defaultid: '', //登录默认会员号
  111. status: false, //false代表账号密码登录,true代表手机验证码登录
  112. disabled: true, //登录按钮是否可用
  113. loading: false, //是否显示按钮加载状态
  114. username: "", //登录用户名
  115. password: "", //登录密码
  116. imgcode: "", //图片验证码0
  117. imgcodesrc: "", //验证码图片
  118. isHidePassword: true, //是否隐藏密码
  119. phone: "", //登录手机号
  120. checknum: "", //登录验证码
  121. codetime: 0, //验证码获取倒计时
  122. code: "", //用户code
  123. listData: [],
  124. };
  125. },
  126. //第一次加载
  127. onLoad(e) {
  128. // #ifdef APP-PLUS
  129. this.openSQL();
  130. this.selectTableData();
  131. // #endif
  132. this.username = this.defaultid;
  133. },
  134. //页面显示
  135. onShow() {
  136. uni.getClipboardData({
  137. success: (res) => {
  138. let id = (res.data.replace(/(^\s*)|(\s*$)/g, "")).toUpperCase();
  139. if ((id.length == 17) && (id.indexOf('99') == 0) && (id.indexOf('D') > 0) && (id.indexOf(
  140. 'M') > 0) && (id !== this.username)) {
  141. uni.showModal({
  142. content: '是否自动填充会员号',
  143. cancelText: '否',
  144. confirmText: '是',
  145. success: (res1) => {
  146. if (res1.confirm) {
  147. this.username = id;
  148. }
  149. }
  150. });
  151. }
  152. }
  153. })
  154. },
  155. watch: {
  156. username(val) {
  157. this.username = this.username.toUpperCase();
  158. this.OnBtnChange();
  159. },
  160. password(val) {
  161. this.OnBtnChange();
  162. },
  163. imgcode(val) {
  164. this.OnBtnChange();
  165. },
  166. phone(val) {
  167. this.OnBtnChange();
  168. },
  169. checknum(val) {
  170. this.OnBtnChange();
  171. }
  172. },
  173. onShow() {
  174. this.refreshCaptcha();
  175. uni.getClipboardData({
  176. success: (res) => {
  177. let id = (res.data.replace(/(^\s*)|(\s*$)/g, "")).toUpperCase();
  178. if ((id.length == 17) && (id.indexOf('99') == 0) && (id.indexOf('D') > 0) && (id.indexOf(
  179. 'M') > 0) && (id !== this.username)) {
  180. uni.showModal({
  181. content: '是否自动填充会员号',
  182. cancelText: '否',
  183. confirmText: '是',
  184. success: (res1) => {
  185. if (res1.confirm) {
  186. this.username = id;
  187. }
  188. }
  189. });
  190. }
  191. }
  192. })
  193. },
  194. onUnload() {
  195. this.closeSQL();
  196. },
  197. computed: {
  198. ...mapState(["userLoginId"])
  199. },
  200. //方法
  201. methods: {
  202. ...mapMutations(['setUserInfo']),
  203. // 跳转页面
  204. nav(url) {
  205. uni.navigateTo({
  206. url: url
  207. });
  208. },
  209. // 图片验证码
  210. refreshCaptcha() {
  211. this.imgcodesrc = this.$base.baseUrl + '/captcha.jpg?t=' + new Date().getTime();
  212. },
  213. // 切换登录状态
  214. changeStatus() {
  215. uni.hideKeyboard();
  216. this.initInput();
  217. this.username = this.defaultid;
  218. this.status = !this.status;
  219. this.refreshCaptcha();
  220. },
  221. // 初始化表单
  222. initInput() {
  223. this.username = '';
  224. this.password = '';
  225. this.phone = '';
  226. this.checknum = '';
  227. this.imgcode = "";
  228. },
  229. // 改变按钮状态
  230. OnBtnChange() {
  231. if ((this.username && this.password && this.imgcode) || (this.phone && this.checknum)) {
  232. this.disabled = false;
  233. return;
  234. }
  235. this.disabled = true;
  236. },
  237. // 获取验证码
  238. async getCheckNum() {
  239. uni.hideKeyboard();
  240. if (this.codetime > 0) return;
  241. // 验证手机号合法性
  242. if (!this.phone) {
  243. return uni.showToast({
  244. title: '请输入手机号',
  245. icon: 'none'
  246. });
  247. }
  248. if (!this.$base.phoneRegular.test(this.phone)) {
  249. return uni.showToast({
  250. title: '请输入正确的手机号码',
  251. icon: 'none'
  252. });
  253. }
  254. // 请求服务器,发送验证码
  255. let res = await this.$http.get('/sendMsg', {
  256. phone: this.phone,
  257. type: "0"
  258. });
  259. // 请求失败处理
  260. if (res.code != 200) {
  261. return uni.showToast({
  262. title: res.data.msg,
  263. icon: "none"
  264. });
  265. } else {
  266. uni.showToast({
  267. title: "发送成功",
  268. icon: "none"
  269. });
  270. }
  271. // 发送成功,开启倒计时
  272. this.codetime = 60;
  273. let timer = setInterval(() => {
  274. this.codetime--;
  275. if (this.codetime < 1) {
  276. clearInterval(timer);
  277. this.codetime = 0;
  278. }
  279. }, 1000);
  280. },
  281. async getCheckNum1() {
  282. uni.hideKeyboard();
  283. if (this.codetime > 0) return;
  284. // 验证手机号合法性
  285. if (!this.phone) {
  286. return uni.showToast({
  287. title: '请输入手机号',
  288. icon: 'none'
  289. });
  290. }
  291. if (!this.$base.phoneRegular.test(this.phone)) {
  292. return uni.showToast({
  293. title: '请输入正确的手机号码',
  294. icon: 'none'
  295. });
  296. }
  297. // 请求服务器,发送验证码
  298. let res = await this.$http.get('/sendMsg', {
  299. phone: this.phone,
  300. type: "0"
  301. });
  302. // 请求失败处理
  303. if (res.code != 200) {
  304. return uni.showToast({
  305. title: res.data.msg,
  306. icon: "none"
  307. });
  308. } else {
  309. uni.showToast({
  310. title: "发送成功",
  311. icon: "none"
  312. });
  313. }
  314. // 发送成功,开启倒计时
  315. this.codetime = 60;
  316. let timer = setInterval(() => {
  317. this.codetime--;
  318. if (this.codetime < 1) {
  319. clearInterval(timer);
  320. this.codetime = 0;
  321. }
  322. }, 1000);
  323. },
  324. //#ifdef APP-PLUS
  325. //确认离开
  326. fanhui() {
  327. this.show = false;
  328. this.$login({
  329. url: "/login",
  330. data: {
  331. account: this.username,
  332. password: this.password,
  333. captcha: this.imgcode.toLowerCase()
  334. }
  335. }).then(res => {
  336. socket.init();
  337. return;
  338. })
  339. },
  340. //立即支付
  341. zhifu() {
  342. this.show = false;
  343. this.updateTableData();
  344. this.$login({
  345. url: "/login",
  346. data: {
  347. account: this.username,
  348. password: this.password,
  349. captcha: this.imgcode.toLowerCase()
  350. }
  351. }).then(res => {
  352. socket.init();
  353. return;
  354. })
  355. },
  356. //#endif
  357. // 提交登录
  358. submit() {
  359. // // 账号密码登录
  360. uni.hideKeyboard();
  361. if (!this.status) {
  362. //#ifdef H5
  363. if (this.username == 'ADMIN') {
  364. return uni.showToast({
  365. title: '无效账号',
  366. icon: "none"
  367. });
  368. } else {
  369. this.$login({
  370. url: "/login",
  371. data: {
  372. account: this.username,
  373. password: this.password,
  374. captcha: this.imgcode.toLowerCase()
  375. }
  376. }).then(res => {
  377. socket.init();
  378. return;
  379. })
  380. }
  381. //#endif
  382. //#ifdef APP-PLUS
  383. if (this.listData.length > 0) {
  384. console.log(this.status)
  385. this.listData.map(ele => {
  386. if (this.username != ele.username || this.password != ele.password) {
  387. this.show = true;
  388. } else {
  389. this.$login({
  390. url: "/login",
  391. data: {
  392. account: this.username,
  393. password: this.password,
  394. captcha: this.imgcode.toLowerCase()
  395. }
  396. }).then(res => {
  397. socket.init();
  398. return;
  399. })
  400. }
  401. })
  402. } else {
  403. this.$login({
  404. url: "/login",
  405. data: {
  406. account: this.username,
  407. password: this.password,
  408. captcha: this.imgcode.toLowerCase()
  409. }
  410. }).then(res => {
  411. this.openSQL(); //打开数据库
  412. this.createTable(); //创建表
  413. this.insertTableData(this.username, this.password); //新增表数据
  414. socket.init();
  415. return;
  416. })
  417. }
  418. //#endif
  419. } else {
  420. // 验证码登录
  421. // 验证手机号合法性
  422. if (!this.$base.phoneRegular.test(this.phone)) {
  423. return uni.showToast({
  424. title: '请输入正确的手机号码',
  425. icon: 'none'
  426. });
  427. }
  428. this.$login({
  429. url: "/loginByPhone?phone=" + this.phone + "&phoneMsg=" + this.checknum,
  430. }).then(res => {
  431. // socket.init();
  432. return;
  433. })
  434. }
  435. },
  436. // 微信APP登录
  437. onWxAppLogin() {
  438. var that = this;
  439. uni.login({
  440. provider: 'weixin',
  441. scopes: "auth_user",
  442. onlyAuthorize: true,
  443. success: function(loginRes) {
  444. that.codeRes(loginRes);
  445. }
  446. });
  447. },
  448. async codeRes(data) {
  449. this.$login({
  450. url: "/wechat/login",
  451. data: {
  452. code: data.code,
  453. }
  454. }).then(res => {
  455. socket.init();
  456. return;
  457. })
  458. },
  459. // 打开数据库
  460. openSQL() {
  461. // 这个是查询有没有打开数据库
  462. let open = DB.isOpen();
  463. console.log("数据库状态", open ? "开启" : "关闭");
  464. // if (!open) {
  465. DB.openSqlite()
  466. .then(res => {})
  467. .catch(error => {
  468. console.log(error)
  469. });
  470. // }
  471. },
  472. // 关闭数据库
  473. closeSQL() {
  474. // 这个是查询有没有打开数据库
  475. let open = DB.isOpen();
  476. if (open) {
  477. DB.closeSqlite()
  478. .then(res => {
  479. // this.showToast("数据库已关闭");
  480. })
  481. .catch(error => {
  482. // this.showToast("数据库关闭失败");
  483. });
  484. }
  485. },
  486. // 创建表
  487. createTable() {
  488. let open = DB.isOpen();
  489. if (open) {
  490. this.openSQL();
  491. let sql =
  492. '"id" INTEGER PRIMARY KEY AUTOINCREMENT,"username" text,"password" text,"defaultid" text,"time" text';
  493. // 创建表 DB.createTable(表名, 表的列)
  494. DB.createTable("chat", sql)
  495. .then(res => {
  496. // this.showToast("创建chat表成功");
  497. })
  498. .catch(error => {});
  499. } else {}
  500. },
  501. // 新增表数据
  502. insertTableData(user, word) {
  503. let open = DB.isOpen();
  504. if (open) {
  505. let arr = [{
  506. username: user,
  507. password: word,
  508. defaultid: user,
  509. }]
  510. arr.map(item => {
  511. let time = this.formatDate(new Date().getTime());
  512. let sql = `'${item.username}','${item.password}','${item.defaultid}','${time}'`;
  513. let condition = "'username','password','defaultid','time'";
  514. // 新增 DB.insertTableData(表名, 对应表头列的数据)
  515. DB.insertTableData("chat", sql, condition)
  516. .then(res => {
  517. this.selectTableData();
  518. })
  519. .catch(error => {});
  520. })
  521. } else {}
  522. },
  523. // 查询表数据
  524. selectTableData() {
  525. let open = DB.isOpen();
  526. console.log(open)
  527. if (open) {
  528. // 查询表 DB.selectTableData(表名,查询条件列名,查询条件列值)
  529. DB.selectTableData("chat")
  530. .then(res => {
  531. console.log("contact表数据", res);
  532. this.listData = res;
  533. this.username = res[0].username;
  534. this.defaultid = res[0].defaultid;
  535. this.password = res[0].password;
  536. })
  537. .catch(error => {});
  538. } else {}
  539. },
  540. // 修改表数据
  541. updateTableData() {
  542. let open = DB.isOpen();
  543. if (open) {
  544. let time = this.formatDate(new Date().getTime());
  545. let data = `username ='${this.username}',password ='${this.password}',defaultid='${this.username}'`;
  546. // 修改表数据 DB.updateTableData(表名, 要修改的列名=修改后列值, 修改条件的列名, 修改条件的列值)
  547. DB.updateTableData("chat", data)
  548. .then(res => {
  549. console.log(res)
  550. this.showToast("保存成功");
  551. this.selectTableData();
  552. })
  553. .catch(error => {
  554. console.log(error)
  555. });
  556. } else {}
  557. },
  558. // 删除表数据
  559. deleteTableData() {
  560. let open = DB.isOpen();
  561. if (open) {
  562. // 删除表 DB.deleteTableData(表名,查询条件列名,查询条件列值)
  563. DB.deleteTableData("chat")
  564. .then(res => {
  565. this.selectTableData();
  566. })
  567. .catch(error => {});
  568. } else {}
  569. },
  570. // 提示框
  571. showToast: function(str) {
  572. uni.showToast({
  573. icon: "none",
  574. title: str,
  575. mask: true
  576. });
  577. },
  578. // 时间戳转年月日
  579. formatDate(data) {
  580. let now = new Date(data);
  581. var year = now.getFullYear(); //取得4位数的年份
  582. var month =
  583. now.getMonth() + 1 < 10 ?
  584. "0" + (now.getMonth() + 1) :
  585. now.getMonth() + 1;
  586. var date = now.getDate() < 10 ? "0" + now.getDate() : now.getDate();
  587. var hour = now.getHours() < 10 ? "0" + now.getHours() : now.getHours();
  588. var minute =
  589. now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes();
  590. var second =
  591. now.getSeconds() < 10 ? "0" + now.getSeconds() : now.getSeconds();
  592. return (
  593. year +
  594. "-" +
  595. month +
  596. "-" +
  597. date +
  598. " " +
  599. hour +
  600. ":" +
  601. minute +
  602. ":" +
  603. second
  604. );
  605. }
  606. },
  607. //页面隐藏
  608. onHide() {},
  609. //页面卸载
  610. onUnload() {},
  611. //页面下来刷新
  612. onPullDownRefresh() {},
  613. //页面上拉触底
  614. onReachBottom() {},
  615. };
  616. </script>
  617. <style scoped>
  618. .shu1 {
  619. display: flex;
  620. flex-direction: column;
  621. justify-content: space-between;
  622. align-items: center;
  623. }
  624. .shili_btn {
  625. width: 100%;
  626. height: 90rpx;
  627. border-top: 2rpx solid #F2F2F2;
  628. }
  629. .shili_btn>view {
  630. width: 50%;
  631. height: 100%;
  632. background-color: #ffffff;
  633. cursor: pointer;
  634. }
  635. .shili_btn>view:nth-child(2) {
  636. color: #FFFFFF;
  637. background-color: #007BFF;
  638. }
  639. .shili>text:nth-child(1) {
  640. font-weight: bold;
  641. }
  642. .shili {
  643. padding: 40rpx;
  644. box-sizing: border-box;
  645. }
  646. .heng {
  647. display: flex;
  648. justify-content: space-between;
  649. align-items: center;
  650. }
  651. .heng1 {
  652. display: flex;
  653. justify-content: center;
  654. align-items: center;
  655. }
  656. /* 第三方登录Start */
  657. .third_party_login_box {
  658. position: fixed;
  659. bottom: 100rpx;
  660. left: 0;
  661. right: 0;
  662. height: 170rpx;
  663. padding: 0 30rpx;
  664. }
  665. .third_party_login_box .third_party_title {
  666. display: flex;
  667. align-items: center;
  668. }
  669. .third_party_login_box .third_party_title:before,
  670. .third_party_login_box .third_party_title:after {
  671. content: "";
  672. flex: 1;
  673. height: 2rpx;
  674. background-color: #f5f5f5;
  675. }
  676. .third_party_login_box .third_party_title text {
  677. font-size: 24rpx;
  678. color: #999999;
  679. flex-shrink: 0;
  680. padding: 0 20rpx;
  681. }
  682. .third_party_login_box .third_party_content {
  683. height: 140rpx;
  684. display: flex;
  685. justify-content: center;
  686. align-items: center;
  687. }
  688. .third_party_login_box .third_party_content image {
  689. margin: 0rpx 20rpx;
  690. width: 80rpx;
  691. height: 80rpx;
  692. }
  693. /* 第三方登录End */
  694. .yanzhengmaView1 {
  695. background: #FFFFFF;
  696. border-radius: 10rpx;
  697. width: 150rpx;
  698. padding: 5rpx 0;
  699. }
  700. .yanzhengmaView2 {
  701. background: #FFFFFF;
  702. border-radius: 10rpx;
  703. width: 220rpx;
  704. padding: 5rpx 0;
  705. }
  706. </style>