index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. <template>
  2. <view class="login-wrapper" :style="colorStyle">
  3. <view class="shading">
  4. <image :src="logoUrl" v-if="logoUrl" />
  5. <image src="/static/images/logo2.png" v-else />
  6. </view>
  7. <view class="whiteBg" v-if="formItem === 1">
  8. <view class="list" v-if="current !== 1">
  9. <form @submit.prevent="submit">
  10. <view class="item">
  11. <view class="acea-row row-middle">
  12. <image src="/static/images/phone_1.png" style="width: 24rpx; height: 34rpx;"></image>
  13. <input type="text" placeholder="输入手机号码" v-model="account" maxlength="11" required />
  14. </view>
  15. </view>
  16. <view class="item">
  17. <view class="acea-row row-middle">
  18. <image src="/static/images/code_1.png" style="width: 28rpx; height: 32rpx;"></image>
  19. <input type="password" placeholder="填写登录密码" v-model="password" required />
  20. </view>
  21. </view>
  22. </form>
  23. <!-- <navigator class="forgetPwd" hover-class="none" url="/pages/users/retrievePassword/index">
  24. <span class="iconfont icon-wenti"></span>忘记密码
  25. </navigator> -->
  26. </view>
  27. <view class="list" v-if="current !== 0 || appLoginStatus || appleLoginStatus">
  28. <view class="item">
  29. <view class="acea-row row-middle">
  30. <image src="/static/images/phone_1.png" style="width: 24rpx; height: 34rpx;"></image>
  31. <input type="text" placeholder="输入手机号码" v-model="account" maxlength="11" />
  32. </view>
  33. </view>
  34. <view class="item">
  35. <view class="acea-row row-middle">
  36. <image src="/static/images/code_2.png" style="width: 28rpx; height: 32rpx;"></image>
  37. <input type="text" placeholder="填写验证码" maxlength="6" class="codeIput" v-model="captcha" />
  38. <button class="code" :disabled="disabled" :class="disabled === true ? 'on' : ''" @click="code">
  39. {{ text }}
  40. </button>
  41. </view>
  42. </view>
  43. <view class="item" v-if="isShowCode">
  44. <view class="acea-row row-middle">
  45. <image src="/static/images/code_2.png" style="width: 28rpx; height: 32rpx;"></image>
  46. <input type="text" placeholder="填写验证码" class="codeIput" v-model="codeVal" />
  47. <view class="code" @click="again"><img :src="codeUrl" /></view>
  48. </view>
  49. </view>
  50. </view>
  51. <view class="logon" @click="loginMobile" v-if="current !== 0">登录</view>
  52. <view class="logon" @click="submit" v-if="current === 0">登录</view>
  53. <!-- #ifndef APP-PLUS -->
  54. <view class="tips">
  55. <view v-if="current==0" @click="current = 1">快速登录</view>
  56. <view v-if="current==1" @click="current = 0">账号登录</view>
  57. </view>
  58. <!-- #endif -->
  59. <!-- #ifdef APP-PLUS -->
  60. <view class="appLogin" v-if="!appLoginStatus && !appleLoginStatus">
  61. <view class="hds">
  62. <span class="line"></span>
  63. <p>其他方式登录</p>
  64. <span class="line"></span>
  65. </view>
  66. <view class="btn-wrapper">
  67. <view class="btn wx" @click="wxLogin">
  68. <span class="iconfont icon-s-weixindenglu1"></span>
  69. </view>
  70. <view class="btn mima" v-if="current == 1" @click="current =0">
  71. <span class="iconfont icon-s-mimadenglu1"></span>
  72. </view>
  73. <view class="btn yanzheng" v-if="current == 0" @click="current =1">
  74. <span class="iconfont icon-s-yanzhengmadenglu1"></span>
  75. </view>
  76. <view class="apple-btn" @click="appleLogin" v-if="appleShow">
  77. <view class="iconfont icon-s-pingguo"></view>
  78. </view>
  79. </view>
  80. </view>
  81. <!-- #endif -->
  82. </view>
  83. <view class="bottom"></view>
  84. </view>
  85. </template>
  86. <script>
  87. import dayjs from "@/plugin/dayjs/dayjs.min.js";
  88. import sendVerifyCode from "@/mixins/SendVerifyCode";
  89. import {
  90. loginH5,
  91. loginMobile,
  92. registerVerify,
  93. register,
  94. getCodeApi,
  95. getUserInfo,
  96. appleLogin
  97. } from "@/api/user";
  98. import attrs, {
  99. required,
  100. alpha_num,
  101. chs_phone
  102. } from "@/utils/validate";
  103. import {
  104. getLogo
  105. } from "@/api/public";
  106. // import cookie from "@/utils/store/cookie";
  107. import {
  108. VUE_APP_API_URL
  109. } from "@/utils";
  110. // #ifdef APP-PLUS
  111. import {
  112. wechatAppAuth
  113. } from '@/api/api.js'
  114. // #endif
  115. const BACK_URL = "login_back_url";
  116. import colors from '@/mixins/color.js';
  117. export default {
  118. name: "Login",
  119. mixins: [sendVerifyCode, colors],
  120. data: function() {
  121. return {
  122. navList: ["快速登录", "账号登录"],
  123. current: 1,
  124. account: "",
  125. password: "",
  126. captcha: "",
  127. formItem: 1,
  128. type: "login",
  129. logoUrl: "",
  130. keyCode: "",
  131. codeUrl: "",
  132. codeVal: "",
  133. isShowCode: false,
  134. appLoginStatus: false, // 微信登录强制绑定手机号码状态
  135. appUserInfo: null, // 微信登录保存的用户信息
  136. appleLoginStatus: false, // 苹果登录强制绑定手机号码状态
  137. appleUserInfo: null,
  138. appleShow: false, // 苹果登录版本必须要求ios13以上的
  139. keyLock: true
  140. };
  141. },
  142. watch: {
  143. formItem: function(nval, oVal) {
  144. if (nval == 1) {
  145. this.type = 'login'
  146. } else {
  147. this.type = 'register'
  148. }
  149. }
  150. },
  151. onLoad() {
  152. let self = this
  153. uni.getSystemInfo({
  154. success: (res) => {
  155. if (res.platform.toLowerCase() == 'ios' && this.getSystem(res.system)) {
  156. self.appleShow = true
  157. }
  158. }
  159. });
  160. },
  161. mounted: function() {
  162. // this.getCode();
  163. this.getLogoImage();
  164. },
  165. methods: {
  166. // IOS 版本号判断
  167. getSystem(system) {
  168. let str
  169. system.toLowerCase().indexOf('ios') === -1 ? str = system : str = system.split(' ')[1]
  170. if (str.indexOf('.'))
  171. return str.split('.')[0] >= 13
  172. return str >= 13
  173. },
  174. // 苹果登录
  175. appleLogin() {
  176. let self = this
  177. this.account = ''
  178. this.captcha = ''
  179. uni.showLoading({
  180. title: '登录中'
  181. })
  182. uni.login({
  183. provider: 'apple',
  184. timeout: 10000,
  185. success(loginRes) {
  186. uni.getUserInfo({
  187. provider: 'apple',
  188. success: function(infoRes) {
  189. self.appleUserInfo = infoRes.userInfo
  190. self.appleLoginApi()
  191. },
  192. fail() {
  193. uni.showToast({
  194. title: '获取用户信息失败',
  195. icon: 'none',
  196. duration: 2000
  197. })
  198. },
  199. complete() {
  200. uni.hideLoading()
  201. }
  202. });
  203. },
  204. fail(error) {
  205. console.log(error)
  206. }
  207. })
  208. },
  209. // 苹果登录Api
  210. appleLoginApi() {
  211. let self = this
  212. appleLogin({
  213. openId: self.appleUserInfo.openId,
  214. email: self.appleUserInfo.email || '',
  215. phone: this.account,
  216. captcha: this.captcha
  217. }).then(({
  218. data
  219. }) => {
  220. if (data.isbind) {
  221. uni.showModal({
  222. title: '提示',
  223. content: '请绑定手机号后,继续操作',
  224. showCancel: false,
  225. success: function(res) {
  226. if (res.confirm) {
  227. self.current = 1
  228. self.appleLoginStatus = true
  229. }
  230. }
  231. });
  232. } else {
  233. self.$store.commit("LOGIN", {
  234. 'token': data.token,
  235. 'time': data.expires_time - self.$Cache.time()
  236. });
  237. let backUrl = self.$Cache.get(BACK_URL) || "/pages/index/index";
  238. self.$Cache.clear(BACK_URL);
  239. self.$store.commit("SETUID", data.userInfo.uid);
  240. uni.reLaunch({
  241. url: backUrl
  242. });
  243. }
  244. }).catch(error => {
  245. uni.showModal({
  246. title: '提示',
  247. content: `错误信息${error}`,
  248. success: function(res) {
  249. if (res.confirm) {
  250. console.log('用户点击确定');
  251. } else if (res.cancel) {
  252. console.log('用户点击取消');
  253. }
  254. }
  255. });
  256. })
  257. },
  258. // App微信登录
  259. wxLogin() {
  260. let self = this
  261. this.account = ''
  262. this.captcha = ''
  263. uni.showLoading({
  264. title: '登录中'
  265. })
  266. uni.login({
  267. provider: 'weixin',
  268. success: function(loginRes) {
  269. // 获取用户信息
  270. uni.getUserInfo({
  271. provider: 'weixin',
  272. success: function(infoRes) {
  273. self.appUserInfo = infoRes.userInfo
  274. self.wxLoginApi()
  275. },
  276. fail() {
  277. uni.showToast({
  278. title: '获取用户信息失败',
  279. icon: 'none',
  280. duration: 2000
  281. })
  282. },
  283. complete() {
  284. uni.hideLoading()
  285. }
  286. });
  287. },
  288. fail() {
  289. uni.showToast({
  290. title: '登录失败',
  291. icon: 'none',
  292. duration: 2000
  293. })
  294. }
  295. });
  296. },
  297. wxLoginApi() {
  298. let self = this
  299. wechatAppAuth({
  300. userInfo: self.appUserInfo,
  301. phone: this.account,
  302. code: this.captcha
  303. }).then(({
  304. data
  305. }) => {
  306. if (data.isbind) {
  307. uni.showModal({
  308. title: '提示',
  309. content: '请绑定手机号后,继续操作',
  310. showCancel: false,
  311. success: function(res) {
  312. if (res.confirm) {
  313. self.current = 1
  314. self.appLoginStatus = true
  315. }
  316. }
  317. });
  318. } else {
  319. self.$store.commit("LOGIN", {
  320. 'token': data.token,
  321. 'time': data.expires_time - self.$Cache.time()
  322. });
  323. let backUrl = self.$Cache.get(BACK_URL) || "/pages/index/index";
  324. self.$Cache.clear(BACK_URL);
  325. self.$store.commit("SETUID", data.userInfo.uid);
  326. uni.reLaunch({
  327. url: backUrl
  328. });
  329. }
  330. }).catch(error => {
  331. uni.showModal({
  332. title: '提示',
  333. content: `错误信息${error}`,
  334. success: function(res) {
  335. if (res.confirm) {
  336. console.log('用户点击确定');
  337. } else if (res.cancel) {
  338. console.log('用户点击取消');
  339. }
  340. }
  341. });
  342. })
  343. },
  344. again() {
  345. this.codeUrl =
  346. VUE_APP_API_URL +
  347. "/sms_captcha?" +
  348. "key=" +
  349. this.keyCode +
  350. Date.parse(new Date());
  351. },
  352. code() {
  353. let that = this
  354. getCodeApi()
  355. .then(res => {
  356. that.keyCode = res.data.key;
  357. that.getCode();
  358. })
  359. .catch(res => {
  360. that.$util.Tips({
  361. title: res
  362. });
  363. });
  364. },
  365. async getLogoImage() {
  366. let that = this;
  367. getLogo(2).then(res => {
  368. that.logoUrl = res.data.logo_url;
  369. });
  370. },
  371. async loginMobile() {
  372. let that = this;
  373. if (!that.account) return that.$util.Tips({
  374. title: '请填写手机号码'
  375. });
  376. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(that.account)) return that.$util.Tips({
  377. title: '请输入正确的手机号码'
  378. });
  379. if (!that.captcha) return that.$util.Tips({
  380. title: '请填写验证码'
  381. });
  382. if (!/^[\w\d]+$/i.test(that.captcha)) return that.$util.Tips({
  383. title: '请输入正确的验证码'
  384. });
  385. if (that.appLoginStatus) {
  386. that.wxLoginApi()
  387. } else if (that.appleLoginStatus) {
  388. that.appleLoginApi()
  389. } else {
  390. if (this.keyLock) {
  391. this.keyLock = !this.keyLock
  392. } else {
  393. return that.$util.Tips({
  394. title: '请勿重复点击'
  395. });
  396. }
  397. loginMobile({
  398. phone: that.account,
  399. captcha: that.captcha,
  400. spread: that.$Cache.get("spread")
  401. })
  402. .then(res => {
  403. let data = res.data;
  404. that.$store.commit("LOGIN", {
  405. 'token': data.token,
  406. 'time': data.expires_time - this.$Cache.time()
  407. });
  408. let backUrl = that.$Cache.get(BACK_URL) || "/pages/index/index";
  409. that.$Cache.clear(BACK_URL);
  410. getUserInfo().then(res => {
  411. this.keyLock = true
  412. that.$store.commit("SETUID", res.data.uid);
  413. if (backUrl.indexOf('/pages/users/login/index') !== -1) {
  414. backUrl = '/pages/index/index';
  415. }
  416. uni.reLaunch({
  417. url: backUrl
  418. });
  419. })
  420. })
  421. .catch(res => {
  422. this.keyLock = true
  423. that.$util.Tips({
  424. title: res
  425. });
  426. });
  427. }
  428. },
  429. async register() {
  430. let that = this;
  431. if (!that.account) return that.$util.Tips({
  432. title: '请填写手机号码'
  433. });
  434. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(that.account)) return that.$util.Tips({
  435. title: '请输入正确的手机号码'
  436. });
  437. if (!that.captcha) return that.$util.Tips({
  438. title: '请填写验证码'
  439. });
  440. if (!/^[\w\d]+$/i.test(that.captcha)) return that.$util.Tips({
  441. title: '请输入正确的验证码'
  442. });
  443. if (!that.password) return that.$util.Tips({
  444. title: '请填写密码'
  445. });
  446. if (/^([0-9]|[a-z]|[A-Z]){0,6}$/i.test(that.password)) return that.$util.Tips({
  447. title: '您输入的密码过于简单'
  448. });
  449. register({
  450. account: that.account,
  451. captcha: that.captcha,
  452. password: that.password,
  453. spread: that.$Cache.get("spread")
  454. })
  455. .then(res => {
  456. that.$util.Tips({
  457. title: res
  458. });
  459. that.formItem = 1;
  460. })
  461. .catch(res => {
  462. that.$util.Tips({
  463. title: res
  464. });
  465. });
  466. },
  467. async getCode() {
  468. let that = this;
  469. if (!that.account) return that.$util.Tips({
  470. title: '请填写手机号码'
  471. });
  472. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(that.account)) return that.$util.Tips({
  473. title: '请输入正确的手机号码'
  474. });
  475. if (that.formItem == 2) that.type = "register";
  476. await registerVerify({
  477. phone: that.account,
  478. type: that.type,
  479. key: that.keyCode,
  480. code: that.codeVal
  481. })
  482. .then(res => {
  483. that.$util.Tips({
  484. title: res.msg
  485. });
  486. that.sendCode();
  487. })
  488. .catch(res => {
  489. that.$util.Tips({
  490. title: res
  491. });
  492. });
  493. },
  494. navTap: function(index) {
  495. this.current = index;
  496. },
  497. async submit() {
  498. let that = this;
  499. if (!that.account) return that.$util.Tips({
  500. title: '请填写账号'
  501. });
  502. if (!/^[\w\d]{5,16}$/i.test(that.account)) return that.$util.Tips({
  503. title: '请输入正确的账号'
  504. });
  505. if (!that.password) return that.$util.Tips({
  506. title: '请填写密码'
  507. });
  508. if (this.keyLock) {
  509. this.keyLock = !this.keyLock
  510. } else {
  511. return that.$util.Tips({
  512. title: '请勿重复点击'
  513. });
  514. }
  515. loginH5({
  516. account: that.account,
  517. password: that.password,
  518. spread: that.$Cache.get("spread")
  519. })
  520. .then(({
  521. data
  522. }) => {
  523. that.$store.commit("LOGIN", {
  524. 'token': data.token,
  525. 'time': data.expires_time - this.$Cache.time()
  526. });
  527. let backUrl = that.$Cache.get(BACK_URL) || "/pages/index/index";
  528. that.$Cache.clear(BACK_URL);
  529. getUserInfo().then(res => {
  530. this.keyLock = true
  531. that.$store.commit("SETUID", res.data.uid);
  532. uni.reLaunch({
  533. url: backUrl
  534. });
  535. }).catch(error => {
  536. this.keyLock = true
  537. })
  538. })
  539. .catch(e => {
  540. this.keyLock = true
  541. that.$util.Tips({
  542. title: e
  543. });
  544. });
  545. }
  546. }
  547. };
  548. </script>
  549. <style>
  550. page {
  551. background: #fff;
  552. }
  553. </style>
  554. <style lang="scss">
  555. .appLogin {
  556. margin-top: 60rpx;
  557. .hds {
  558. display: flex;
  559. justify-content: center;
  560. align-items: center;
  561. font-size: 24rpx;
  562. color: #B4B4B4;
  563. .line {
  564. width: 68rpx;
  565. height: 1rpx;
  566. background: #CCCCCC;
  567. }
  568. p {
  569. margin: 0 20rpx;
  570. }
  571. }
  572. .btn-wrapper {
  573. display: flex;
  574. align-items: center;
  575. justify-content: center;
  576. margin-top: 30rpx;
  577. .btn {
  578. display: flex;
  579. align-items: center;
  580. justify-content: center;
  581. width: 68rpx;
  582. height: 68rpx;
  583. border-radius: 50%;
  584. }
  585. .apple-btn {
  586. display: flex;
  587. align-items: center;
  588. justify-content: center;
  589. width: 68rpx;
  590. height: 68rpx;
  591. border-radius: 50%;
  592. background: #000;
  593. .icon-s-pingguo {
  594. color: #fff;
  595. font-size: 44rpx;
  596. }
  597. }
  598. .iconfont {
  599. font-size: 40rpx;
  600. color: #fff;
  601. }
  602. .wx {
  603. margin-right: 30rpx;
  604. background-color: #61C64F;
  605. }
  606. .mima {
  607. margin-right: 30rpx;
  608. background-color: #28B3E9;
  609. }
  610. .yanzheng {
  611. margin-right: 30rpx;
  612. background-color: #F89C23;
  613. }
  614. }
  615. }
  616. .code img {
  617. width: 100%;
  618. height: 100%;
  619. }
  620. .acea-row.row-middle {
  621. input {
  622. margin-left: 20rpx;
  623. display: block;
  624. }
  625. }
  626. .login-wrapper {
  627. padding: 30rpx;
  628. .shading {
  629. display: flex;
  630. align-items: center;
  631. justify-content: center;
  632. width: 100%;
  633. /* #ifdef APP-VUE */
  634. margin-top: 50rpx;
  635. /* #endif */
  636. /* #ifndef APP-VUE */
  637. margin-top: 200rpx;
  638. /* #endif */
  639. image {
  640. width: 240rpx;
  641. height: 240rpx;
  642. }
  643. }
  644. .whiteBg {
  645. margin-top: 100rpx;
  646. .list {
  647. border-radius: 16rpx;
  648. overflow: hidden;
  649. .item {
  650. border-bottom: 1px solid #F0F0F0;
  651. background: #fff;
  652. .row-middle {
  653. position: relative;
  654. padding: 16rpx 45rpx;
  655. input {
  656. flex: 1;
  657. font-size: 28rpx;
  658. height: 80rpx;
  659. }
  660. .code {
  661. position: absolute;
  662. right: 30rpx;
  663. top: 50%;
  664. color: var(--view-theme);
  665. font-size: 26rpx;
  666. transform: translateY(-50%);
  667. }
  668. }
  669. }
  670. }
  671. .logon {
  672. display: flex;
  673. align-items: center;
  674. justify-content: center;
  675. width: 100%;
  676. height: 86rpx;
  677. margin-top: 80rpx;
  678. background-color: var(--view-theme);
  679. border-radius: 120rpx;
  680. color: #FFFFFF;
  681. font-size: 30rpx;
  682. }
  683. .tips {
  684. margin: 30rpx;
  685. text-align: center;
  686. color: #999;
  687. }
  688. }
  689. }
  690. </style>