login.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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, res.data.tokenExp)
  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. uni.showLoading({
  275. title: '正在登录...'
  276. })
  277. let res = await usernameLogin(form)
  278. if (res.data.token) {
  279. msgSuccess("登录成功!")
  280. this.useAuthStore.setAuthToken(res.data.token, res.data.tokenExp)
  281. this.initUser()
  282. }
  283. },
  284. toPage(url) {
  285. uni.navigateTo({
  286. url: url
  287. })
  288. },
  289. async initUser(){
  290. const loginRes = await wx.login();
  291. const code = loginRes.code;
  292. let userRes = await me();
  293. let wxRes = await wxLoginOpenid({code})
  294. // 加载用户信息
  295. if(userRes && userRes?.code===0){
  296. this.useAuthStore.setUserInfo(userRes.data)
  297. }
  298. // 加载openid
  299. if(wxRes && wxRes?.data){
  300. const { openid, sessionKey } = wxRes.data
  301. this.useAuthStore.setOpenid(openid)
  302. }
  303. setTimeout(() => {
  304. if(this.redirect){
  305. uni.removeStorageSync("redirect")
  306. uni.reLaunch({
  307. url: this.redirect.url+'?id='+this.redirect.id+'&title='+this.redirect.title
  308. })
  309. }else{
  310. uni.switchTab({
  311. url: "/pages/index/index"
  312. })
  313. }
  314. }, 1000)
  315. },
  316. // --------验证码 start---------
  317. codeChange(text) {
  318. this.tips = text;
  319. },
  320. getCode() {
  321. let {
  322. phone
  323. } = this.phoneForm
  324. if (!/^1[3-9]\d{9}$/.test(phone)) {
  325. msg("请输入正确的手机号");
  326. return;
  327. }
  328. if (this.$refs.uCode1.canGetCode) {
  329. this.showPopup()
  330. } else {
  331. msg('倒计时结束后再发送');
  332. }
  333. },
  334. end() {
  335. msg('倒计时结束');
  336. },
  337. start() {
  338. msg('倒计时开始');
  339. },
  340. // 数字校验码
  341. showPopup() {
  342. // 模拟向后端请求验证码
  343. // 1.获取数字校验码图片
  344. // 2.显示弹窗
  345. this.loadImage()
  346. this.show = true;
  347. },
  348. closePopup() {
  349. this.show = false;
  350. this.numberCaptchaForm.numberCaptcha = ''; // 清空输入框
  351. },
  352. async submitCode() {
  353. if (this.numberCaptchaForm.numberCaptcha.length !== 4) {
  354. uni.showToast({
  355. title: '请输入4位验证码',
  356. icon: 'none'
  357. });
  358. return;
  359. }
  360. // 联调 检验是否正确
  361. uni.showLoading({
  362. title: '正在校验验证码'
  363. })
  364. const checkRes = await checknumberCaptcha(this.numberCaptchaForm);
  365. if (!checkRes.data.valid) {
  366. uni.showToast({
  367. title: '验证码输入错误',
  368. icon: 'none'
  369. });
  370. return;
  371. }
  372. // // 发送手机验证码
  373. const codeRes = await smsSend({
  374. phone: this.phoneForm.phone
  375. });
  376. if (codeRes && codeRes.data.captchaUuid) {
  377. this.getPhoneCapt++;
  378. this.phoneForm.captchaUuid = codeRes.data.captchaUuid
  379. // console.log(this.phoneForm)
  380. this.closePopup();
  381. this.$refs.uCode1.start()
  382. }
  383. uni.hideLoading();
  384. },
  385. // 加载验证码图片
  386. async loadImage() {
  387. // 联调
  388. uni.showLoading({
  389. title: '正在获取图片'
  390. })
  391. const img = await getCaptchaImage();
  392. if (!img.data.numberCaptchaSrc) {
  393. return
  394. }
  395. this.numberCaptchaForm.numberCaptchaUuid = img.data.numberCaptchaUuid
  396. this.numberCaptchaSrc = img.data.numberCaptchaSrc;
  397. uni.hideLoading();
  398. },
  399. // --------验证码 end---------
  400. async handleWxLogin() {
  401. const loginRes = await wx.login();
  402. const code = loginRes.code;
  403. wxLoginOpenid({code}).then(res=>{
  404. if(res?.data){
  405. const { openid, sessionKey } = res.data
  406. }
  407. })
  408. },
  409. async checkBook(){
  410. let checkUserBook = await this.checkWeixinUserBook()
  411. if (!this.checkWeixinUserBook()) {
  412. return;
  413. };
  414. },
  415. async decryptPhoneNumber(e){
  416. if (e.detail.encryptedData) {
  417. const {
  418. encryptedData,
  419. iv,
  420. code
  421. } = e.detail;
  422. const loginRes = await wx.login();
  423. const wxcode = loginRes.code;
  424. try{
  425. const openidRes = await wxLoginOpenid({code : wxcode})
  426. if(openidRes?.data){
  427. const { openid, sessionKey,tokenExp } = openidRes.data
  428. this.useAuthStore.setOpenid(openid)
  429. const phoneRes = await wxloginPhone({ openid, phoneCode: code})
  430. const { phone, token, isNewUser } = phoneRes.data;
  431. this.useAuthStore.setPhone(phone);
  432. this.useAuthStore.setAuthToken(token, tokenExp);
  433. // uni.reLaunch({
  434. // url: "/pages/register/register?mode=refine"
  435. // })
  436. if(isNewUser){
  437. //新用户,跳转:完善用户信息
  438. // console.log("完善用户信息")
  439. uni.reLaunch({
  440. url: "/pages/register/register?mode=refine"
  441. })
  442. }else{
  443. // 老用户,跳转至:首页
  444. msgSuccess("授权成功!")
  445. uni.showLoading({
  446. title: '正在登录...'
  447. })
  448. let userRes = await me()
  449. if(userRes && userRes?.code===0){
  450. this.useAuthStore.setUserInfo(userRes.data)
  451. }
  452. msgSuccess("登录成功!")
  453. setTimeout(()=>{
  454. if(this.redirect){
  455. uni.reLaunch({
  456. url: this.redirect.url+'?id='+this.redirect.id+'&title='+this.redirect.title
  457. })
  458. }else{
  459. uni.switchTab({
  460. url: "/pages/index/index"
  461. })
  462. }
  463. }, 1000)
  464. }
  465. }
  466. }catch(err){
  467. console.log("ee", err)
  468. }
  469. // 将 encryptedData 和 iv 发送到后端进行解密
  470. } else {
  471. msg('取消授权手机号');
  472. }
  473. },
  474. getUserProfile() {
  475. // this.toPage("/pages/wxLogin/wxLogin")
  476. // 用户微信登录
  477. let that = this;
  478. // 推荐使用uni.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
  479. // 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  480. uni.getUserProfile({
  481. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  482. success: (resp) => {
  483. //登录远程服务器
  484. console.log(2222, resp)
  485. // if (that.loginCode) {
  486. // //登录远程服务器
  487. // console.log("登录远程服务器")
  488. // // that.loginByWeixin(resp)
  489. // } else {
  490. // uni.login({
  491. // success: function(resp) {
  492. // if (resp.code) {
  493. // that.code = resp.code
  494. // console.log(resp)
  495. // // that.loginByWeixin(resp)
  496. // }
  497. // }
  498. // });
  499. // }
  500. }
  501. })
  502. }
  503. },
  504. // 在组件销毁时清理定时器
  505. beforeDestroy() {
  506. // 在组件销毁时清理定时器
  507. },
  508. computed: {
  509. useAuthStore() {
  510. return useAuthStore(); // 获取 store 实例
  511. }
  512. },
  513. onLoad(option) {
  514. const that = this;
  515. if (uni.getUserProfile) {
  516. // console.log(1)
  517. that.canIUseGetUserProfile = true
  518. }
  519. // if(option.redirect){
  520. // const decodedRedirect = decodeURIComponent(option.redirect);
  521. // uni.setStorageSync("redirect", JSON.parse(decodedRedirect))
  522. // }
  523. if(uni.getStorageSync("redirect")){
  524. this.redirect = uni.getStorageSync("redirect")
  525. }
  526. }
  527. }
  528. </script>
  529. <style lang="scss">
  530. .form-item .u-input--border {
  531. border-radius: 16rpx !important;
  532. }
  533. .custom-button {
  534. .u-btn {
  535. display: block !important;
  536. width: fit-content;
  537. padding: 0 !important;
  538. border-radius: 50rpx;
  539. }
  540. .u-btn--default {
  541. border: none !important;
  542. background-color: transparent !important;
  543. }
  544. }
  545. </style>
  546. <style lang="scss" scoped>
  547. .app-login-page {
  548. background-color: #f7f7f7;
  549. width: 100vw;
  550. height: 100vh;
  551. display: flex;
  552. justify-content: center;
  553. align-items: center;
  554. }
  555. .app-login {
  556. width: 90%;
  557. }
  558. .app-icon {
  559. // text-align: center;
  560. display: flex;
  561. justify-content: center;
  562. align-items: center;
  563. height: 90rpx;
  564. text-align: center;
  565. }
  566. .app-title {
  567. text-align: center;
  568. font-size: 44rpx;
  569. letter-spacing: 2px;
  570. font-weight: 700;
  571. }
  572. .mb-30 {
  573. margin-bottom: 30rpx;
  574. }
  575. .text-red {
  576. color: #df0505;
  577. }
  578. .login-form {
  579. background-color: #ffffff;
  580. width: 100%;
  581. padding: 50rpx 40rpx;
  582. border-radius: 15rpx;
  583. box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.2);
  584. border: 1px solid #036df6;
  585. }
  586. .loginType {
  587. color: #000000;
  588. margin-bottom: 15rpx;
  589. .loginType-text {
  590. font-size: 30rpx;
  591. margin-right: 30rpx;
  592. }
  593. .loginType-text-active {
  594. font-size: 40rpx;
  595. margin-right: 30rpx;
  596. }
  597. }
  598. .form-item {
  599. display: flex;
  600. align-items: center;
  601. margin-bottom: 60rpx;
  602. .form-item-input {
  603. width: 100%;
  604. background-color: #f7f7f7 !important;
  605. }
  606. .form-item-text {
  607. width: 200rpx;
  608. flex: 0 0 auto;
  609. text-align: center;
  610. color: #000000;
  611. // margin: 0 20px;
  612. }
  613. }
  614. .form-button {
  615. background: linear-gradient(90deg, #006af5, #21b0fc);
  616. border-radius: 20rpx;
  617. line-height: 80rpx;
  618. text-align: center;
  619. color: #fff;
  620. margin-bottom: 50rpx;
  621. }
  622. .risgiter-forget {
  623. font-size: 30rpx;
  624. display: flex;
  625. justify-content: space-between;
  626. }
  627. // 数字验证码
  628. .popup {
  629. padding: 40rpx;
  630. border-radius: 20rpx;
  631. .popup-title {
  632. font-size: 18px;
  633. text-align: center;
  634. display: inline-block;
  635. width: 100%;
  636. // margin-bottom: 15px;
  637. }
  638. .popup-content {
  639. display: flex;
  640. align-items: center;
  641. margin: 50rpx 0;
  642. }
  643. .popup-btn {
  644. display: flex;
  645. justify-content: space-between;
  646. }
  647. }
  648. // 用户手册模拟态
  649. .modal {
  650. .modal-content {
  651. padding: 50rpx;
  652. .content-a {
  653. color: #2979ff;
  654. }
  655. }
  656. }
  657. </style>