login.vue 14 KB

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