login.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. <template>
  2. <view class="app-login-page">
  3. <view class="app-login">
  4. <view class="app-icon mb-30">
  5. <image src="/static/images/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="usenameLoginHandle" />
  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="usenameLoginHandle" />
  43. </view>
  44. <view class="form-button" @click="usenameLoginHandle">
  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="/static/images/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="/static/images/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="/static/images/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.usenameLoginHandle
  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. this.useAuthStore.setAuthToken("test")
  181. uni.setStorageSync("userinfo", {
  182. userId: "2121"
  183. })
  184. uni.switchTab({
  185. url: "/pages/index/index"
  186. })
  187. },
  188. // 账号登录
  189. async usenameLoginHandle() {
  190. if (!this.usernameForm.zhanghao) {
  191. msg("请输入账号")
  192. return;
  193. }
  194. if (!this.usernameForm.mima) {
  195. msg("请输入密码")
  196. return;
  197. }
  198. // usernameLogin(this.usernameForm).then(res=>{
  199. // console.log(res,45699)
  200. // })
  201. let checkUserBook = await this.checkUserBook()
  202. if (!this.checkUserBook()) return;
  203. console.log(this.usernameForm)
  204. // 联调
  205. // 1.登录 发送请求
  206. // 2.保存 token
  207. // 3.获取个人信息,保存个人新
  208. this.useAuthStore.setAuthToken("test")
  209. uni.setStorageSync("userinfo", {
  210. userId: "2121",
  211. isMember: true
  212. })
  213. uni.switchTab({
  214. url: "/pages/index/index"
  215. })
  216. },
  217. toPage(url) {
  218. uni.navigateTo({
  219. url: url
  220. })
  221. },
  222. // --------验证码 start---------
  223. codeChange(text) {
  224. this.tips = text;
  225. },
  226. getCode() {
  227. let {
  228. shoujihao
  229. } = this.phoneForm
  230. if (!/^1[3-9]\d{9}$/.test(shoujihao)) {
  231. msg("请输入正确的手机号");
  232. return;
  233. }
  234. if (this.$refs.uCode1.canGetCode) {
  235. this.showPopup()
  236. } else {
  237. msg('倒计时结束后再发送');
  238. }
  239. },
  240. end() {
  241. msg('倒计时结束');
  242. },
  243. start() {
  244. msg('倒计时开始');
  245. },
  246. showPopup() {
  247. // 模拟向后端请求验证码
  248. // 1.获取数字校验码图片
  249. // 2.显示弹窗
  250. uni.showLoading({
  251. title: '正在获取图片'
  252. })
  253. setTimeout(() => {
  254. uni.hideLoading();
  255. msg('获取成功');
  256. // 显示模拟态,把获取过来的数字校对码的uuid赋给 shuzijiaoduimaForm.shuzijiaoduimaUuid
  257. this.show = true;
  258. }, 1500);
  259. // 联调
  260. // this.loadImage()
  261. // this.show = true;
  262. },
  263. closePopup() {
  264. this.show = false;
  265. this.shuzijiaoduimaForm.shuzijiaoduima = ''; // 清空输入框
  266. },
  267. async submitCode() {
  268. if (this.shuzijiaoduimaForm.shuzijiaoduima.length !== 6) {
  269. uni.showToast({
  270. title: '请输入6位验证码',
  271. icon: 'none'
  272. });
  273. return;
  274. }
  275. // 联调 检验是否正确
  276. // const checkRes = await checkShuzijiaoduima();
  277. // if(!checkRes){
  278. // uni.showToast({
  279. // title: '验证码输入错误',
  280. // icon: 'none'
  281. // });
  282. // return;
  283. // }
  284. // // 发送手机验证码
  285. // const codeRes = await getCodeApi();
  286. // if(codeRes && codeRes.yanzhengmaUuid){
  287. // this.phoneForm.yanzhengmaUuid = codeRes.yanzhengmaUuid
  288. // this.closePopup();
  289. // this.$refs.uCode1.start()
  290. // }
  291. uni.showLoading({
  292. title: '正在校验验证码'
  293. })
  294. setTimeout(() => {
  295. uni.hideLoading();
  296. msg('填写正确');
  297. this.closePopup();
  298. this.$refs.uCode1.start()
  299. }, 1500);
  300. },
  301. // 加载验证码图片
  302. async loadImage() {
  303. // 联调
  304. // const img = await loadShuzijiaoduima();
  305. // this.shuzijiaoduimaForm.shuzijiaoduimaUuid = img.shuzijiaoduimaUuid
  306. // this.shuzijiaoduimaSrc = img.shuzijiaoduimaSrc
  307. console.log("加载图片")
  308. },
  309. // --------验证码 end---------
  310. handleWxLogin() {
  311. console.log("weixindenglu")
  312. },
  313. decryptPhoneNumber(e) {
  314. if (e.detail.encryptedData) {
  315. const {
  316. encryptedData,
  317. iv
  318. } = e.detail;
  319. // 将 encryptedData 和 iv 发送到后端进行解密
  320. } else {
  321. msg('取消授权手机号');
  322. }
  323. },
  324. getUserProfile() {
  325. this.toPage("/pages/wxLogin/wxLogin")
  326. // 用户微信登录
  327. // let that = this;
  328. // 推荐使用uni.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
  329. // 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  330. // uni.getUserProfile({
  331. // desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  332. // success: (resp) => {
  333. // //登录远程服务器
  334. // console.log(resp)
  335. // if (that.loginCode) {
  336. // //登录远程服务器
  337. // console.log("登录远程服务器")
  338. // // that.loginByWeixin(resp)
  339. // } else {
  340. // uni.login({
  341. // success: function(resp) {
  342. // if (resp.code) {
  343. // that.code = resp.code
  344. // console.log(resp)
  345. // // that.loginByWeixin(resp)
  346. // }
  347. // }
  348. // });
  349. // }
  350. // }
  351. // })
  352. }
  353. },
  354. // 在组件销毁时清理定时器
  355. beforeDestroy() {
  356. // 在组件销毁时清理定时器
  357. },
  358. computed: {
  359. useAuthStore() {
  360. return useAuthStore(); // 获取 store 实例
  361. }
  362. },
  363. onLoad() {
  364. const that = this;
  365. if (uni.getUserProfile) {
  366. that.canIUseGetUserProfile = true
  367. }
  368. }
  369. }
  370. </script>
  371. <style lang="scss">
  372. .form-item .u-input--border {
  373. border-radius: 16rpx !important;
  374. }
  375. .custom-button {
  376. .u-btn {
  377. display: block !important;
  378. width: fit-content;
  379. padding: 0 !important;
  380. border-radius: 50rpx;
  381. }
  382. }
  383. </style>
  384. <style lang="scss" scoped>
  385. .app-login-page {
  386. background-color: #f7f7f7;
  387. width: 100vw;
  388. height: 100vh;
  389. display: flex;
  390. justify-content: center;
  391. align-items: center;
  392. }
  393. .app-login {
  394. width: 90%;
  395. }
  396. .app-icon {
  397. text-align: center;
  398. }
  399. .app-title {
  400. text-align: center;
  401. font-size: 44rpx;
  402. letter-spacing: 2px;
  403. font-weight: 700;
  404. }
  405. .mb-30 {
  406. margin-bottom: 30rpx;
  407. }
  408. .text-red {
  409. color: #df0505;
  410. }
  411. .login-form {
  412. background-color: #ffffff;
  413. width: 100%;
  414. padding: 50rpx 40rpx;
  415. border-radius: 15rpx;
  416. box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.2);
  417. border: 1px solid #036df6;
  418. }
  419. .loginType {
  420. color: #000000;
  421. margin-bottom: 15rpx;
  422. .loginType-text {
  423. font-size: 30rpx;
  424. margin-right: 30rpx;
  425. }
  426. .loginType-text-active {
  427. font-size: 40rpx;
  428. margin-right: 30rpx;
  429. }
  430. }
  431. .form-item {
  432. display: flex;
  433. align-items: center;
  434. margin-bottom: 60rpx;
  435. .form-item-input {
  436. width: 100%;
  437. background-color: #f7f7f7 !important;
  438. }
  439. .form-item-text {
  440. width: 200rpx;
  441. flex: 0 0 auto;
  442. text-align: center;
  443. color: #000000;
  444. // margin: 0 20px;
  445. }
  446. }
  447. .form-button {
  448. background: linear-gradient(90deg, #006af5, #21b0fc);
  449. border-radius: 20rpx;
  450. line-height: 80rpx;
  451. text-align: center;
  452. color: #fff;
  453. margin-bottom: 50rpx;
  454. }
  455. .risgiter-forget {
  456. font-size: 30rpx;
  457. display: flex;
  458. justify-content: space-between;
  459. }
  460. // 数字验证码
  461. .popup {
  462. padding: 40rpx;
  463. border-radius: 20rpx;
  464. .popup-title {
  465. font-size: 18px;
  466. text-align: center;
  467. display: inline-block;
  468. width: 100%;
  469. // margin-bottom: 15px;
  470. }
  471. .popup-content {
  472. display: flex;
  473. align-items: center;
  474. margin: 50rpx 0;
  475. }
  476. .popup-btn {
  477. display: flex;
  478. justify-content: space-between;
  479. }
  480. }
  481. // 用户手册模拟态
  482. .modal {
  483. .modal-content {
  484. padding: 50rpx;
  485. .content-a {
  486. color: #2979ff;
  487. }
  488. }
  489. }
  490. </style>