login.vue 15 KB

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