login.vue 14 KB

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