login.vue 19 KB

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