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