login.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. <template>
  2. <view class="app-login-page">
  3. <view class="app-login">
  4. <view class="app-icon mb-30">
  5. <image :src="`${FILE_URL}/login-icon.png`"
  6. mode="aspectFit" style="height: 40px;"></image>
  7. </view>
  8. <view class="app-title mb-30">
  9. 欢迎使用会员中心系统小程序
  10. </view>
  11. <view class="mb-30 login-form">
  12. <view class="loginType">
  13. <text :class="[loginType=='phone'? 'loginType-text-active': 'loginType-text']"
  14. @click="loginType = 'phone'">手机登录</text>
  15. <text :class="[loginType=='username'? 'loginType-text-active': 'loginType-text']"
  16. @click="loginType = 'username'">账号登录</text>
  17. </view>
  18. <view class="phone-form" v-if="loginType=='phone'">、
  19. <view class="form-item">
  20. <u-input height="80" placeholder="请输入手机号" border-color="#d0d0d0" v-model="phoneForm.phone"
  21. :border="true" class="form-item-input" type="number" @confirm="phoneLoginHandle" />
  22. </view>
  23. <view class="form-item">
  24. <u-input height="80" placeholder="请输入验证码" border-color="#d0d0d0" v-model="phoneForm.captcha"
  25. :border="true" class="form-item-input" @confirm="phoneLoginHandle" />
  26. <text class="form-item-text" @click="getCode">{{tips}}</text>
  27. <!-- <text class="form-item-text" v-if="!isCounting" @click="requestVerificationCode">
  28. 发送验证码</text>
  29. <text class="form-item-text" v-else>
  30. {{ `发送验证码(${countdown})` }}</text> -->
  31. </view>
  32. <view class="form-button" @click="phoneLoginHandle">
  33. 立即登录
  34. </view>
  35. </view>
  36. <view class="phone-form" v-if="loginType=='username'">、
  37. <view class="form-item">
  38. <u-input height="80" placeholder="请输入账号" border-color="#d0d0d0" v-model="userNameForm.username"
  39. :border="true" class="form-item-input" @confirm="userNameLoginHandle" />
  40. </view>
  41. <view class="form-item">
  42. <u-input height="80" placeholder="请输入密码" border-color="#d0d0d0" v-model="userNameForm.password"
  43. :border="true" class="form-item-input" @confirm="userNameLoginHandle" type="password"
  44. password-icon />
  45. </view>
  46. <view class="form-button" @click="userNameLoginHandle">
  47. 立即登录
  48. </view>
  49. </view>
  50. <view class="risgiter-forget">
  51. <view @click="toPage('/pages/forget/forget')">忘记密码?</view>
  52. <view @click="toPage('/pages/register/register')">无账号?<text class="text-red">立即注册</text></view>
  53. </view>
  54. </view>
  55. <view class="app-user-book mb-30">
  56. <u-checkbox-group style="display: flex;justify-content: center;">
  57. <u-checkbox v-model="userBook" name="yes" shape="circle">我已阅读&#x300A;用户手册&#x300B;及隐私政策</u-checkbox>
  58. </u-checkbox-group>
  59. </view>
  60. <u-divider bg-color="transparent" margin-bottom="30" color="#010101" border-color="#010101"
  61. half-width="200">使用第三方账号登录</u-divider>
  62. <view class="app-icon mb-30">
  63. <!-- <u-button @click="handleWxLogin()" class="custom-button" style="width: 40px;" plain :hair-line="false">
  64. <image :src="`${FILE_URL}/weixin.png`"
  65. mode="aspectFit" style="width: 40px;height: 40px;" />
  66. </u-button> -->
  67. <u-button v-if="userBook" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber" class="custom-button" style="width: 40px;" plain :hair-line="false">
  68. <image :src="`${FILE_URL}/weixin.png`"
  69. mode="aspectFit" style="width: 40px;height: 40px;" />
  70. </u-button>
  71. <u-button v-else class="custom-button" style="width: 40px;" plain :hair-line="false" @click="checkBook">
  72. <image :src="`${FILE_URL}/weixin.png`"
  73. mode="aspectFit" style="width: 40px;height: 40px;" />
  74. </u-button>
  75. </view>
  76. </view>
  77. <u-verification-code :seconds="seconds" ref="uCode1" @change="codeChange"></u-verification-code>
  78. <u-popup v-model="show" mode="center" border-radius="10">
  79. <view class="popup">
  80. <text class="popup-title">请输入验证码</text>
  81. <view class="popup-content">
  82. <u-input height="80" placeholder="请输入验证码" border-color="#d0d0d0"
  83. v-model="numberCaptchaForm.numberCaptcha" :border="true" class="form-item-input"
  84. style="flex-grow: 1;" @confirm="submitCode" />
  85. <image :src="numberCaptchaSrc" mode="aspectFit"
  86. style="height:40px;margin-left:20rpx;width: 180rpx;flex: 0 0 auto;" @click="loadImage"></image>
  87. </view>
  88. <view class="popup-btn">
  89. <u-button type="primary" @click="submitCode" style="width: 48%;flex: 0 0 auto;">提交</u-button>
  90. <u-button type="default" @click="closePopup" style="width: 48%;flex: 0 0 auto;">取消</u-button>
  91. </view>
  92. </view>
  93. </u-popup>
  94. <u-modal v-model="userBookModal" mask-close-able class="modal" show-cancel-button
  95. :title-style="{fontWeight: 'bold'}" @confirm="confirmModal">
  96. <view class="modal-content">
  97. 为了更好地保障你的合法权益,进行下一步前,请阅读并同意
  98. <text class="content-a">&#x300A;用户手册&#x300B;及隐私政策</text>
  99. </view>
  100. </u-modal>
  101. <u-modal v-model="weixinUserBookModal" mask-close-able class="modal" show-cancel-button
  102. :title-style="{fontWeight: 'bold'}" @confirm="confirmModalWeixin">
  103. <view class="modal-content">
  104. 为了更好地保障你的合法权益,进行下一步前,请阅读并同意
  105. <text class="content-a">&#x300A;用户手册&#x300B;及隐私政策</text>
  106. </view>
  107. </u-modal>
  108. </view>
  109. </template>
  110. <script>
  111. import {
  112. usernameLogin,
  113. getCaptchaImage,
  114. checknumberCaptcha,
  115. smsSend,
  116. loginPhone,
  117. wxLoginOpenid, wxloginPhone
  118. } from "@/api/login.js"
  119. import {
  120. msg,
  121. msgSuccess
  122. } from "@/utils/common.js"
  123. import {
  124. useAuthStore
  125. } from '@/store/authStore'
  126. import {
  127. encryptAESCBC
  128. } from "@/utils/crypto.js"
  129. import configService from '@/utils/baseurl.js'
  130. export default {
  131. data() {
  132. return {
  133. FILE_URL: configService.FILE_URL,
  134. getPhoneCapt: 0,
  135. canIUseGetUserProfile: false,
  136. loginCode: '',
  137. userBookModal: false,
  138. weixinUserBookModal: false,
  139. // ----验证码 start -----
  140. numberCaptchaForm: {
  141. numberCaptcha: '',
  142. numberCaptchaUuid: ''
  143. },
  144. numberCaptchaSrc: '',
  145. show: false,
  146. tips: '',
  147. seconds: 60,
  148. // ----验证码 end -----
  149. loginType: "phone",
  150. phoneForm: {
  151. phone: '18320082803',
  152. captcha: '',
  153. captchaUuid: ''
  154. },
  155. userNameForm: {
  156. username: "纪学卫",
  157. password: "123456"
  158. },
  159. userBook: false, //协议
  160. }
  161. },
  162. methods: {
  163. // 检查用户手册是否勾选
  164. checkUserBook() {
  165. if (!this.userBook) {
  166. this.userBookModal = true
  167. return false;
  168. }
  169. return true
  170. },
  171. checkWeixinUserBook(){
  172. if (!this.userBook) {
  173. this.weixinUserBookModal = true
  174. return false;
  175. }
  176. return true
  177. },
  178. // 用户手册模拟态确认勾选
  179. confirmModal() {
  180. this.userBook = true;
  181. const loginTypeHandler = {
  182. phone: this.phoneLoginHandle,
  183. username: this.userNameLoginHandle
  184. };
  185. // 调用相应的登录处理函数
  186. (loginTypeHandler[this.loginType] || (() => {})).call(this);
  187. },
  188. confirmModalWeixin() {
  189. this.userBook = true;
  190. },
  191. // 手机登录
  192. async phoneLoginHandle() {
  193. const {
  194. phone,
  195. captcha
  196. } = this.phoneForm;
  197. // 手机号正则验证
  198. if (!/^1[3-9]\d{9}$/.test(phone)) {
  199. msg("请输入正确的手机号");
  200. return;
  201. }
  202. if (this.getPhoneCapt <= 0) {
  203. msg("请先获取手机验证码");
  204. return;
  205. }
  206. // 验证码存在性检查
  207. if (!captcha) {
  208. msg("请输入验证码");
  209. return;
  210. }
  211. let checkUserBook = await this.checkUserBook()
  212. if (!this.checkUserBook()) return;
  213. uni.showLoading({
  214. title: '正在登录...'
  215. })
  216. let res = await loginPhone(this.phoneForm)
  217. if (res.data.token) {
  218. msgSuccess("登录成功!")
  219. this.useAuthStore.setAuthToken(res.data.token)
  220. const loginRes = await wx.login();
  221. const code = loginRes.code;
  222. wxLoginOpenid({code}).then(resp=>{
  223. if(resp?.data){
  224. const { openid, sessionKey } = resp.data
  225. this.useAuthStore.setOpenid(openid)
  226. }
  227. setTimeout(() => {
  228. uni.switchTab({
  229. url: "/pages/index/index"
  230. })
  231. }, 1000)
  232. })
  233. }
  234. },
  235. // 账号登录
  236. async userNameLoginHandle() {
  237. if (!this.userNameForm.username) {
  238. msg("请输入账号")
  239. return;
  240. }
  241. if (!this.userNameForm.password) {
  242. msg("请输入密码")
  243. return;
  244. }
  245. let checkUserBook = await this.checkUserBook()
  246. if (!this.checkUserBook()) {
  247. return;
  248. };
  249. const form = {
  250. ...this.userNameForm
  251. }
  252. form.password = encryptAESCBC(form.password);
  253. let res = await usernameLogin(form)
  254. if (res.data.token) {
  255. msgSuccess("登录成功!")
  256. this.useAuthStore.setAuthToken(res.data.token)
  257. const loginRes = await wx.login();
  258. const code = loginRes.code;
  259. wxLoginOpenid({code}).then(resp=>{
  260. if(resp?.data){
  261. const { openid, sessionKey } = resp.data
  262. this.useAuthStore.setOpenid(openid)
  263. }
  264. setTimeout(() => {
  265. uni.switchTab({
  266. url: "/pages/index/index"
  267. })
  268. }, 1000)
  269. })
  270. // setTimeout(() => {
  271. // uni.switchTab({
  272. // url: "/pages/index/index"
  273. // })
  274. // }, 1000)
  275. }
  276. },
  277. toPage(url) {
  278. uni.navigateTo({
  279. url: url
  280. })
  281. },
  282. // --------验证码 start---------
  283. codeChange(text) {
  284. this.tips = text;
  285. },
  286. getCode() {
  287. let {
  288. phone
  289. } = this.phoneForm
  290. if (!/^1[3-9]\d{9}$/.test(phone)) {
  291. msg("请输入正确的手机号");
  292. return;
  293. }
  294. if (this.$refs.uCode1.canGetCode) {
  295. this.showPopup()
  296. } else {
  297. msg('倒计时结束后再发送');
  298. }
  299. },
  300. end() {
  301. msg('倒计时结束');
  302. },
  303. start() {
  304. msg('倒计时开始');
  305. },
  306. // 数字校验码
  307. showPopup() {
  308. // 模拟向后端请求验证码
  309. // 1.获取数字校验码图片
  310. // 2.显示弹窗
  311. this.loadImage()
  312. this.show = true;
  313. },
  314. closePopup() {
  315. this.show = false;
  316. this.numberCaptchaForm.numberCaptcha = ''; // 清空输入框
  317. },
  318. async submitCode() {
  319. if (this.numberCaptchaForm.numberCaptcha.length !== 4) {
  320. uni.showToast({
  321. title: '请输入4位验证码',
  322. icon: 'none'
  323. });
  324. return;
  325. }
  326. // 联调 检验是否正确
  327. uni.showLoading({
  328. title: '正在校验验证码'
  329. })
  330. const checkRes = await checknumberCaptcha(this.numberCaptchaForm);
  331. if (!checkRes.data.valid) {
  332. uni.showToast({
  333. title: '验证码输入错误',
  334. icon: 'none'
  335. });
  336. return;
  337. }
  338. // // 发送手机验证码
  339. const codeRes = await smsSend({
  340. phone: this.phoneForm.phone
  341. });
  342. if (codeRes && codeRes.data.captchaUuid) {
  343. this.getPhoneCapt++;
  344. this.phoneForm.captchaUuid = codeRes.data.captchaUuid
  345. // console.log(this.phoneForm)
  346. this.closePopup();
  347. this.$refs.uCode1.start()
  348. }
  349. uni.hideLoading();
  350. },
  351. // 加载验证码图片
  352. async loadImage() {
  353. // 联调
  354. uni.showLoading({
  355. title: '正在获取图片'
  356. })
  357. const img = await getCaptchaImage();
  358. if (!img.data.numberCaptchaSrc) {
  359. return
  360. }
  361. this.numberCaptchaForm.numberCaptchaUuid = img.data.numberCaptchaUuid
  362. this.numberCaptchaSrc = img.data.numberCaptchaSrc;
  363. uni.hideLoading();
  364. },
  365. // --------验证码 end---------
  366. async handleWxLogin() {
  367. const loginRes = await wx.login();
  368. const code = loginRes.code;
  369. wxLoginOpenid({code}).then(res=>{
  370. if(res?.data){
  371. const { openid, sessionKey } = res.data
  372. }
  373. })
  374. // try {
  375. // // 获取code
  376. // const code = loginRes.code;
  377. // console.log(code)
  378. // wxLoginOpenid({code}).then(res=>{
  379. // console.log("weixinlogin", res)
  380. // })
  381. // } catch (error) {
  382. // console.log('微信登录失败:', error);
  383. // }
  384. },
  385. async checkBook(){
  386. let checkUserBook = await this.checkWeixinUserBook()
  387. if (!this.checkWeixinUserBook()) {
  388. return;
  389. };
  390. },
  391. async decryptPhoneNumber(e){
  392. if (e.detail.encryptedData) {
  393. const {
  394. encryptedData,
  395. iv,
  396. code
  397. } = e.detail;
  398. const loginRes = await wx.login();
  399. const wxcode = loginRes.code;
  400. try{
  401. const openidRes = await wxLoginOpenid({code : wxcode})
  402. if(openidRes?.data){
  403. const { openid, sessionKey } = openidRes.data
  404. const phoneRes = await wxloginPhone({ openid, phoneCode: code})
  405. const { phone, token, isNewUser } = phoneRes.data;
  406. this.useAuthStore.setPhone(phone);
  407. this.useAuthStore.setAuthToken(token);
  408. // uni.reLaunch({
  409. // url: "/pages/register/register?mode=refine"
  410. // })
  411. if(isNewUser){
  412. //新用户,跳转:完善用户信息
  413. // console.log("完善用户信息")
  414. uni.reLaunch({
  415. url: "/pages/register/register?mode=refine"
  416. })
  417. }else{
  418. // 老用户,跳转至:首页
  419. msgSuccess("授权成功!")
  420. setTimeout(()=>{
  421. uni.switchTab({
  422. url: "/pages/index/index"
  423. })
  424. }, 1000)
  425. }
  426. }
  427. }catch(err){
  428. console.log("ee", err)
  429. }
  430. // 将 encryptedData 和 iv 发送到后端进行解密
  431. } else {
  432. msg('取消授权手机号');
  433. }
  434. },
  435. getUserProfile() {
  436. // this.toPage("/pages/wxLogin/wxLogin")
  437. // 用户微信登录
  438. let that = this;
  439. // 推荐使用uni.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
  440. // 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  441. uni.getUserProfile({
  442. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  443. success: (resp) => {
  444. //登录远程服务器
  445. console.log(2222, resp)
  446. // if (that.loginCode) {
  447. // //登录远程服务器
  448. // console.log("登录远程服务器")
  449. // // that.loginByWeixin(resp)
  450. // } else {
  451. // uni.login({
  452. // success: function(resp) {
  453. // if (resp.code) {
  454. // that.code = resp.code
  455. // console.log(resp)
  456. // // that.loginByWeixin(resp)
  457. // }
  458. // }
  459. // });
  460. // }
  461. }
  462. })
  463. }
  464. },
  465. // 在组件销毁时清理定时器
  466. beforeDestroy() {
  467. // 在组件销毁时清理定时器
  468. },
  469. computed: {
  470. useAuthStore() {
  471. return useAuthStore(); // 获取 store 实例
  472. }
  473. },
  474. onLoad() {
  475. const that = this;
  476. if (uni.getUserProfile) {
  477. console.log(1)
  478. that.canIUseGetUserProfile = true
  479. }
  480. }
  481. }
  482. </script>
  483. <style lang="scss">
  484. .form-item .u-input--border {
  485. border-radius: 16rpx !important;
  486. }
  487. .custom-button {
  488. .u-btn {
  489. display: block !important;
  490. width: fit-content;
  491. padding: 0 !important;
  492. border-radius: 50rpx;
  493. }
  494. .u-btn--default {
  495. border: none !important;
  496. background-color: transparent !important;
  497. }
  498. }
  499. </style>
  500. <style lang="scss" scoped>
  501. .app-login-page {
  502. background-color: #f7f7f7;
  503. width: 100vw;
  504. height: 100vh;
  505. display: flex;
  506. justify-content: center;
  507. align-items: center;
  508. }
  509. .app-login {
  510. width: 90%;
  511. }
  512. .app-icon {
  513. text-align: center;
  514. }
  515. .app-title {
  516. text-align: center;
  517. font-size: 44rpx;
  518. letter-spacing: 2px;
  519. font-weight: 700;
  520. }
  521. .mb-30 {
  522. margin-bottom: 30rpx;
  523. }
  524. .text-red {
  525. color: #df0505;
  526. }
  527. .login-form {
  528. background-color: #ffffff;
  529. width: 100%;
  530. padding: 50rpx 40rpx;
  531. border-radius: 15rpx;
  532. box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.2);
  533. border: 1px solid #036df6;
  534. }
  535. .loginType {
  536. color: #000000;
  537. margin-bottom: 15rpx;
  538. .loginType-text {
  539. font-size: 30rpx;
  540. margin-right: 30rpx;
  541. }
  542. .loginType-text-active {
  543. font-size: 40rpx;
  544. margin-right: 30rpx;
  545. }
  546. }
  547. .form-item {
  548. display: flex;
  549. align-items: center;
  550. margin-bottom: 60rpx;
  551. .form-item-input {
  552. width: 100%;
  553. background-color: #f7f7f7 !important;
  554. }
  555. .form-item-text {
  556. width: 200rpx;
  557. flex: 0 0 auto;
  558. text-align: center;
  559. color: #000000;
  560. // margin: 0 20px;
  561. }
  562. }
  563. .form-button {
  564. background: linear-gradient(90deg, #006af5, #21b0fc);
  565. border-radius: 20rpx;
  566. line-height: 80rpx;
  567. text-align: center;
  568. color: #fff;
  569. margin-bottom: 50rpx;
  570. }
  571. .risgiter-forget {
  572. font-size: 30rpx;
  573. display: flex;
  574. justify-content: space-between;
  575. }
  576. // 数字验证码
  577. .popup {
  578. padding: 40rpx;
  579. border-radius: 20rpx;
  580. .popup-title {
  581. font-size: 18px;
  582. text-align: center;
  583. display: inline-block;
  584. width: 100%;
  585. // margin-bottom: 15px;
  586. }
  587. .popup-content {
  588. display: flex;
  589. align-items: center;
  590. margin: 50rpx 0;
  591. }
  592. .popup-btn {
  593. display: flex;
  594. justify-content: space-between;
  595. }
  596. }
  597. // 用户手册模拟态
  598. .modal {
  599. .modal-content {
  600. padding: 50rpx;
  601. .content-a {
  602. color: #2979ff;
  603. }
  604. }
  605. }
  606. </style>