login.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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 {
  130. me
  131. } from '@/api/user.js'
  132. import configService from '@/utils/baseurl.js'
  133. export default {
  134. data() {
  135. return {
  136. FILE_URL: configService.FILE_URL,
  137. getPhoneCapt: 0,
  138. canIUseGetUserProfile: false,
  139. loginCode: '',
  140. userBookModal: false,
  141. weixinUserBookModal: false,
  142. // ----验证码 start -----
  143. numberCaptchaForm: {
  144. numberCaptcha: '',
  145. numberCaptchaUuid: ''
  146. },
  147. numberCaptchaSrc: '',
  148. show: false,
  149. tips: '',
  150. seconds: 60,
  151. // ----验证码 end -----
  152. loginType: "phone",
  153. phoneForm: {
  154. phone: '18320082803',
  155. captcha: '',
  156. captchaUuid: ''
  157. },
  158. userNameForm: {
  159. username: "纪学卫",
  160. password: "123456"
  161. },
  162. userBook: false, //协议
  163. redirect: null
  164. }
  165. },
  166. methods: {
  167. // 检查用户手册是否勾选
  168. checkUserBook() {
  169. if (!this.userBook) {
  170. this.userBookModal = true
  171. return false;
  172. }
  173. return true
  174. },
  175. checkWeixinUserBook(){
  176. if (!this.userBook) {
  177. this.weixinUserBookModal = true
  178. return false;
  179. }
  180. return true
  181. },
  182. // 用户手册模拟态确认勾选
  183. confirmModal() {
  184. this.userBook = true;
  185. const loginTypeHandler = {
  186. phone: this.phoneLoginHandle,
  187. username: this.userNameLoginHandle
  188. };
  189. // 调用相应的登录处理函数
  190. (loginTypeHandler[this.loginType] || (() => {})).call(this);
  191. },
  192. confirmModalWeixin() {
  193. this.userBook = true;
  194. },
  195. // 手机登录
  196. async phoneLoginHandle() {
  197. const {
  198. phone,
  199. captcha
  200. } = this.phoneForm;
  201. // 手机号正则验证
  202. if (!/^1[3-9]\d{9}$/.test(phone)) {
  203. msg("请输入正确的手机号");
  204. return;
  205. }
  206. if (this.getPhoneCapt <= 0) {
  207. msg("请先获取手机验证码");
  208. return;
  209. }
  210. // 验证码存在性检查
  211. if (!captcha) {
  212. msg("请输入验证码");
  213. return;
  214. }
  215. let checkUserBook = await this.checkUserBook()
  216. if (!this.checkUserBook()) return;
  217. uni.showLoading({
  218. title: '正在登录...'
  219. })
  220. let res = await loginPhone(this.phoneForm)
  221. if (res.data.token) {
  222. msgSuccess("登录成功!")
  223. this.useAuthStore.setAuthToken(res.data.token)
  224. this.initUser()
  225. // const loginRes = await wx.login();
  226. // const code = loginRes.code;
  227. // wxLoginOpenid({code}).then(resp=>{
  228. // if(resp?.data){
  229. // const { openid, sessionKey } = resp.data
  230. // this.useAuthStore.setOpenid(openid)
  231. // }
  232. // setTimeout(() => {
  233. // if(this.redirect){
  234. // uni.reLaunch({
  235. // url: this.redirect.url+'?id='+this.redirect.id+'&title='+this.redirect.title
  236. // })
  237. // }else{
  238. // uni.switchTab({
  239. // url: "/pages/index/index"
  240. // })
  241. // }
  242. // }, 1000)
  243. // })
  244. }
  245. },
  246. // 账号登录
  247. async userNameLoginHandle() {
  248. if (!this.userNameForm.username) {
  249. msg("请输入账号")
  250. return;
  251. }
  252. if (!this.userNameForm.password) {
  253. msg("请输入密码")
  254. return;
  255. }
  256. let checkUserBook = await this.checkUserBook()
  257. if (!this.checkUserBook()) {
  258. return;
  259. };
  260. const form = {
  261. ...this.userNameForm
  262. }
  263. form.password = encryptAESCBC(form.password);
  264. let res = await usernameLogin(form)
  265. if (res.data.token) {
  266. msgSuccess("登录成功!")
  267. this.useAuthStore.setAuthToken(res.data.token)
  268. this.initUser()
  269. }
  270. },
  271. toPage(url) {
  272. uni.navigateTo({
  273. url: url
  274. })
  275. },
  276. async initUser(){
  277. const loginRes = await wx.login();
  278. const code = loginRes.code;
  279. let userRes = await me();
  280. let wxRes = await wxLoginOpenid({code})
  281. // 加载用户信息
  282. if(userRes && userRes?.code===0){
  283. this.useAuthStore.setUserInfo(userRes.data)
  284. }
  285. // 加载openid
  286. if(wxRes && wxRes?.data){
  287. const { openid, sessionKey } = wxRes.data
  288. this.useAuthStore.setOpenid(openid)
  289. }
  290. setTimeout(() => {
  291. if(this.redirect){
  292. uni.removeStorageSync("redirect")
  293. uni.reLaunch({
  294. url: this.redirect.url+'?id='+this.redirect.id+'&title='+this.redirect.title
  295. })
  296. }else{
  297. uni.switchTab({
  298. url: "/pages/index/index"
  299. })
  300. }
  301. }, 1000)
  302. },
  303. // --------验证码 start---------
  304. codeChange(text) {
  305. this.tips = text;
  306. },
  307. getCode() {
  308. let {
  309. phone
  310. } = this.phoneForm
  311. if (!/^1[3-9]\d{9}$/.test(phone)) {
  312. msg("请输入正确的手机号");
  313. return;
  314. }
  315. if (this.$refs.uCode1.canGetCode) {
  316. this.showPopup()
  317. } else {
  318. msg('倒计时结束后再发送');
  319. }
  320. },
  321. end() {
  322. msg('倒计时结束');
  323. },
  324. start() {
  325. msg('倒计时开始');
  326. },
  327. // 数字校验码
  328. showPopup() {
  329. // 模拟向后端请求验证码
  330. // 1.获取数字校验码图片
  331. // 2.显示弹窗
  332. this.loadImage()
  333. this.show = true;
  334. },
  335. closePopup() {
  336. this.show = false;
  337. this.numberCaptchaForm.numberCaptcha = ''; // 清空输入框
  338. },
  339. async submitCode() {
  340. if (this.numberCaptchaForm.numberCaptcha.length !== 4) {
  341. uni.showToast({
  342. title: '请输入4位验证码',
  343. icon: 'none'
  344. });
  345. return;
  346. }
  347. // 联调 检验是否正确
  348. uni.showLoading({
  349. title: '正在校验验证码'
  350. })
  351. const checkRes = await checknumberCaptcha(this.numberCaptchaForm);
  352. if (!checkRes.data.valid) {
  353. uni.showToast({
  354. title: '验证码输入错误',
  355. icon: 'none'
  356. });
  357. return;
  358. }
  359. // // 发送手机验证码
  360. const codeRes = await smsSend({
  361. phone: this.phoneForm.phone
  362. });
  363. if (codeRes && codeRes.data.captchaUuid) {
  364. this.getPhoneCapt++;
  365. this.phoneForm.captchaUuid = codeRes.data.captchaUuid
  366. // console.log(this.phoneForm)
  367. this.closePopup();
  368. this.$refs.uCode1.start()
  369. }
  370. uni.hideLoading();
  371. },
  372. // 加载验证码图片
  373. async loadImage() {
  374. // 联调
  375. uni.showLoading({
  376. title: '正在获取图片'
  377. })
  378. const img = await getCaptchaImage();
  379. if (!img.data.numberCaptchaSrc) {
  380. return
  381. }
  382. this.numberCaptchaForm.numberCaptchaUuid = img.data.numberCaptchaUuid
  383. this.numberCaptchaSrc = img.data.numberCaptchaSrc;
  384. uni.hideLoading();
  385. },
  386. // --------验证码 end---------
  387. async handleWxLogin() {
  388. const loginRes = await wx.login();
  389. const code = loginRes.code;
  390. wxLoginOpenid({code}).then(res=>{
  391. if(res?.data){
  392. const { openid, sessionKey } = res.data
  393. }
  394. })
  395. },
  396. async checkBook(){
  397. let checkUserBook = await this.checkWeixinUserBook()
  398. if (!this.checkWeixinUserBook()) {
  399. return;
  400. };
  401. },
  402. async decryptPhoneNumber(e){
  403. if (e.detail.encryptedData) {
  404. const {
  405. encryptedData,
  406. iv,
  407. code
  408. } = e.detail;
  409. const loginRes = await wx.login();
  410. const wxcode = loginRes.code;
  411. try{
  412. const openidRes = await wxLoginOpenid({code : wxcode})
  413. if(openidRes?.data){
  414. const { openid, sessionKey } = openidRes.data
  415. const phoneRes = await wxloginPhone({ openid, phoneCode: code})
  416. const { phone, token, isNewUser } = phoneRes.data;
  417. this.useAuthStore.setPhone(phone);
  418. this.useAuthStore.setAuthToken(token);
  419. // uni.reLaunch({
  420. // url: "/pages/register/register?mode=refine"
  421. // })
  422. if(isNewUser){
  423. //新用户,跳转:完善用户信息
  424. // console.log("完善用户信息")
  425. uni.reLaunch({
  426. url: "/pages/register/register?mode=refine"
  427. })
  428. }else{
  429. // 老用户,跳转至:首页
  430. msgSuccess("授权成功!")
  431. let userRes = await me()
  432. if(userRes && userRes?.code===0){
  433. this.useAuthStore.setUserInfo(userRes.data)
  434. }
  435. setTimeout(()=>{
  436. if(this.redirect){
  437. uni.reLaunch({
  438. url: this.redirect.url+'?id='+this.redirect.id+'&title='+this.redirect.title
  439. })
  440. }else{
  441. uni.switchTab({
  442. url: "/pages/index/index"
  443. })
  444. }
  445. }, 1000)
  446. }
  447. }
  448. }catch(err){
  449. console.log("ee", err)
  450. }
  451. // 将 encryptedData 和 iv 发送到后端进行解密
  452. } else {
  453. msg('取消授权手机号');
  454. }
  455. },
  456. getUserProfile() {
  457. // this.toPage("/pages/wxLogin/wxLogin")
  458. // 用户微信登录
  459. let that = this;
  460. // 推荐使用uni.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
  461. // 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  462. uni.getUserProfile({
  463. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  464. success: (resp) => {
  465. //登录远程服务器
  466. console.log(2222, resp)
  467. // if (that.loginCode) {
  468. // //登录远程服务器
  469. // console.log("登录远程服务器")
  470. // // that.loginByWeixin(resp)
  471. // } else {
  472. // uni.login({
  473. // success: function(resp) {
  474. // if (resp.code) {
  475. // that.code = resp.code
  476. // console.log(resp)
  477. // // that.loginByWeixin(resp)
  478. // }
  479. // }
  480. // });
  481. // }
  482. }
  483. })
  484. }
  485. },
  486. // 在组件销毁时清理定时器
  487. beforeDestroy() {
  488. // 在组件销毁时清理定时器
  489. },
  490. computed: {
  491. useAuthStore() {
  492. return useAuthStore(); // 获取 store 实例
  493. }
  494. },
  495. onLoad(option) {
  496. const that = this;
  497. if (uni.getUserProfile) {
  498. // console.log(1)
  499. that.canIUseGetUserProfile = true
  500. }
  501. // if(option.redirect){
  502. // const decodedRedirect = decodeURIComponent(option.redirect);
  503. // uni.setStorageSync("redirect", JSON.parse(decodedRedirect))
  504. // }
  505. if(uni.getStorageSync("redirect")){
  506. this.redirect = uni.getStorageSync("redirect")
  507. }
  508. }
  509. }
  510. </script>
  511. <style lang="scss">
  512. .form-item .u-input--border {
  513. border-radius: 16rpx !important;
  514. }
  515. .custom-button {
  516. .u-btn {
  517. display: block !important;
  518. width: fit-content;
  519. padding: 0 !important;
  520. border-radius: 50rpx;
  521. }
  522. .u-btn--default {
  523. border: none !important;
  524. background-color: transparent !important;
  525. }
  526. }
  527. </style>
  528. <style lang="scss" scoped>
  529. .app-login-page {
  530. background-color: #f7f7f7;
  531. width: 100vw;
  532. height: 100vh;
  533. display: flex;
  534. justify-content: center;
  535. align-items: center;
  536. }
  537. .app-login {
  538. width: 90%;
  539. }
  540. .app-icon {
  541. text-align: center;
  542. }
  543. .app-title {
  544. text-align: center;
  545. font-size: 44rpx;
  546. letter-spacing: 2px;
  547. font-weight: 700;
  548. }
  549. .mb-30 {
  550. margin-bottom: 30rpx;
  551. }
  552. .text-red {
  553. color: #df0505;
  554. }
  555. .login-form {
  556. background-color: #ffffff;
  557. width: 100%;
  558. padding: 50rpx 40rpx;
  559. border-radius: 15rpx;
  560. box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.2);
  561. border: 1px solid #036df6;
  562. }
  563. .loginType {
  564. color: #000000;
  565. margin-bottom: 15rpx;
  566. .loginType-text {
  567. font-size: 30rpx;
  568. margin-right: 30rpx;
  569. }
  570. .loginType-text-active {
  571. font-size: 40rpx;
  572. margin-right: 30rpx;
  573. }
  574. }
  575. .form-item {
  576. display: flex;
  577. align-items: center;
  578. margin-bottom: 60rpx;
  579. .form-item-input {
  580. width: 100%;
  581. background-color: #f7f7f7 !important;
  582. }
  583. .form-item-text {
  584. width: 200rpx;
  585. flex: 0 0 auto;
  586. text-align: center;
  587. color: #000000;
  588. // margin: 0 20px;
  589. }
  590. }
  591. .form-button {
  592. background: linear-gradient(90deg, #006af5, #21b0fc);
  593. border-radius: 20rpx;
  594. line-height: 80rpx;
  595. text-align: center;
  596. color: #fff;
  597. margin-bottom: 50rpx;
  598. }
  599. .risgiter-forget {
  600. font-size: 30rpx;
  601. display: flex;
  602. justify-content: space-between;
  603. }
  604. // 数字验证码
  605. .popup {
  606. padding: 40rpx;
  607. border-radius: 20rpx;
  608. .popup-title {
  609. font-size: 18px;
  610. text-align: center;
  611. display: inline-block;
  612. width: 100%;
  613. // margin-bottom: 15px;
  614. }
  615. .popup-content {
  616. display: flex;
  617. align-items: center;
  618. margin: 50rpx 0;
  619. }
  620. .popup-btn {
  621. display: flex;
  622. justify-content: space-between;
  623. }
  624. }
  625. // 用户手册模拟态
  626. .modal {
  627. .modal-content {
  628. padding: 50rpx;
  629. .content-a {
  630. color: #2979ff;
  631. }
  632. }
  633. }
  634. </style>