login.vue 18 KB

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