login.vue 13 KB

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