login.uvue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. <template>
  2. <view class="page-container">
  3. <view class="close-btn" @click="goBack">
  4. </view>
  5. <view class="header-section">
  6. <text class="hello-text">
  7. Hello!
  8. </text>
  9. <text class="welcome-text">
  10. 欢迎来到小丁到家
  11. </text>
  12. <image src="/static/logo-massage.png" class="logo-img" mode="aspectFit">
  13. </image>
  14. </view>
  15. <view class="form-section">
  16. <view class="input-box">
  17. <input class="input-field" type="number" placeholder="请输入手机号码" v-model="mobile" maxlength="11" />
  18. </view>
  19. <view class="input-box row-between">
  20. <input class="input-field" type="number" placeholder="请输入验证码" v-model="mobileCode" maxlength="6" />
  21. <text class="code-btn" :class="{ disabled: !canSend }" @click="sendCode">
  22. {{ codeText }}
  23. </text>
  24. </view>
  25. <view class="login-btn" :class="{ disabled: !canLogin }" @click="doLogin">
  26. 登录
  27. </view>
  28. <view class="agree-row row-start">
  29. <radio :value="isAgree" color="#FFCC33" style="transform: scale(0.7);" class="radio r" />
  30. <view class="agree-content">
  31. <text class="agree-text" @click="showModal=true">
  32. 我已阅读并同意
  33. <text class="link-text">
  34. 《用户协议》
  35. </text>
  36. <text class="link-text">
  37. 《隐私政策》
  38. </text>
  39. <text class="link-text">
  40. 《上门按摩服务行业平台公约》
  41. </text>
  42. ,未注册的手机号将自动创建小丁到家账号
  43. </text>
  44. </view>
  45. </view>
  46. </view>
  47. <view class="footer-section">
  48. <view class="divider-row row-center">
  49. <view class="line">
  50. </view>
  51. <text class="divider-text">
  52. 其他登录方式
  53. </text>
  54. <view class="line">
  55. </view>
  56. </view>
  57. <view class="methods-row row-center">
  58. <view class="method-item column-center" @click="customLoginIn()">
  59. <view class="icon-box blue-bg">
  60. <u-icon name="phone" :size="24" />
  61. </view>
  62. <text class="method-name">
  63. 一键登录
  64. </text>
  65. </view>
  66. <view class="method-item column-center" @click="wechatLogin">
  67. <view class="icon-box green-bg">
  68. <uni-icons type="weixin" size="20" color="#fff">
  69. </uni-icons>
  70. </view>
  71. <text class="method-name">
  72. 微信登录
  73. </text>
  74. </view>
  75. </view>
  76. </view>
  77. <view v-if="showModal" class="modal-mask" @click="showModal=false">
  78. <view class="modal-box" @click.stop>
  79. <text class="modal-title">
  80. 服务协议及隐私政策
  81. </text>
  82. <scroll-view scroll-y class="modal-scroll">
  83. <text class="modal-welcome">
  84. 欢迎您使用小丁到家!
  85. </text>
  86. <text class="modal-desc">
  87. 请你务必审慎阅读、并充分理解
  88. <text class="modal-link">
  89. 《用户协议》
  90. </text>
  91. <text class="modal-link">
  92. 《隐私政策》
  93. </text>
  94. ,协议内容包括但不限于:
  95. </text>
  96. <text class="modal-list">
  97. 1、在您使用软件及服务的过程中,向您提供相关基本功能,我们将根据合法、正当、必要的原则,收集或使用必要的个人信息;
  98. </text>
  99. <text class="modal-list">
  100. 2、基于您的授权,我们可能会获取您的地理位置、相册、相机等相关软件权限;
  101. </text>
  102. <text class="modal-list">
  103. 3、我们会采取符合标准的技术措施和数据安全措施来保护您的个人信息安全;
  104. </text>
  105. <text class="modal-list">
  106. 4、您可以查询,更正,管理您的个人信息,我们也提供账户注销的渠道;
  107. </text>
  108. <text class="modal-footer">
  109. 如您同意以上协议内容,请点击“同意”开始使用我们的产品和服务,我们依法尽全力保护您的个人信息。
  110. </text>
  111. </scroll-view>
  112. <view class="modal-btns row-between">
  113. <text class="modal-btn reject" @click="rejectAgreement">
  114. 拒绝并退出
  115. </text>
  116. <text class="modal-btn agree" @click="agreeAgreement">
  117. 同意
  118. </text>
  119. </view>
  120. </view>
  121. </view>
  122. </view>
  123. </template>
  124. <script setup lang="uts">
  125. import { ref, computed } from 'vue';
  126. import { fetchSendcode, fetchLogin } from '@/utils/api/login.uts'
  127. const slogan = ref('');
  128. const privacyName = ref('');
  129. const privacyUrl = ref('');
  130. const mobile = ref('');
  131. const phone = ref<string>('');
  132. const mobileCode = ref<string>('');
  133. const isAgree = ref<boolean>(false);
  134. const showModal = ref<boolean>(false);
  135. const countdown = ref<number>(0);
  136. const uniVerifyManager = ref(null as UniVerifyManager | null);
  137. const isAppEnv = process.env.UNI_PLATFORM === 'app'
  138. // 定义定时器变量
  139. let timer : number | null = null;
  140. const canSend = computed(() => mobile.value.length === 11 && countdown.value === 0);
  141. const canLogin = computed(() => phone.value.length === 11 && mobileCode.value.length === 6 && isAgree.value);
  142. const codeText = computed(() => countdown.value > 0 ? `${countdown.value}s 后重试` : '获取验证码');
  143. const goBack = () => uni.navigateBack();
  144. // const oneKeyNav = () => uni.navigateTo({ url: '/pages/login/login-one-key' });
  145. const wechatLogin = () => uni.showToast({ title: '微信登录开发中', icon: 'none' });
  146. const sendCode = async () => {
  147. if (!canSend.value) return;
  148. console.log(mobile.value, 'mobile')
  149. const res = await fetchSendcode({ mobile: mobile.value });
  150. const currentTimer = timer;
  151. if (currentTimer !== null) {
  152. clearInterval(currentTimer);
  153. }
  154. timer = null;
  155. countdown.value = 60;
  156. uni.showToast({ title: '验证码已发送', icon: 'success' });
  157. timer = setInterval(() => {
  158. countdown.value--;
  159. if (countdown.value <= 0) {
  160. const t = timer;
  161. if (t !== null) {
  162. clearInterval(t);
  163. }
  164. timer = null;
  165. countdown.value = 0;
  166. }
  167. }, 1000);
  168. };
  169. const onAgreeChange = (e : any) => {
  170. // UTS 中 any 类型必须 as 成具体类型再访问属性
  171. const evt = e as UTSJSONObject;
  172. console.log(evt, 'evt')
  173. if (evt != null) {
  174. const detail = evt["detail"] as UTSJSONObject | null;
  175. if (detail != null) {
  176. const value = detail["value"] as boolean | null;
  177. isAgree.value = (value != null) && value;
  178. return;
  179. }
  180. }
  181. isAgree.value = false;
  182. };
  183. const doLogin = async () => {
  184. // if (!canLogin.value) {
  185. // console.log(isAgree.value)
  186. // uni.showToast({ title: isAgree.value ? '请填写完整信息' : '请先同意协议', icon: 'none' });
  187. // return;
  188. // }
  189. uni.showLoading({ title: '登录中...' });
  190. const res = await fetchLogin({ mobile: mobile.value, code: mobileCode.value });
  191. console.log(res, 'res');
  192. const code = (res as UTSJSONObject)["code"] as number | null;
  193. const message = (res as UTSJSONObject)["message"] as string ?? '';
  194. const data = (res as UTSJSONObject)["data"] as UTSJSONObject | null;
  195. if (code == 200 && data != null) {
  196. const token = data["token"] as string | null;
  197. if (token != null && token.length > 0) {
  198. uni.setStorageSync('token', token);
  199. uni.hideLoading();
  200. uni.showToast({ title: '登录成功', icon: 'success' });
  201. setTimeout(() => {
  202. uni.reLaunch({
  203. url: '/pages/index/console'
  204. });
  205. }, 500);
  206. }
  207. } else {
  208. uni.hideLoading();
  209. // const toastMsg = (message ?? '') as string;
  210. uni.showToast({ title: message, icon: 'none' });
  211. }
  212. // setTimeout(() => {
  213. // uni.hideLoading();
  214. // uni.showToast({ title: '登录成功', icon: 'success' });
  215. // }, 1500);
  216. };
  217. const rejectAgreement = () => {
  218. showModal.value = false;
  219. uni.showToast({ title: '您拒绝了协议', icon: 'none' });
  220. };
  221. const agreeAgreement = () => {
  222. showModal.value = false;
  223. isAgree.value = true;
  224. uni.showToast({ title: '已同意协议', icon: 'success' });
  225. };
  226. // 解析一键登录错误信息,提取 errorDesc
  227. const getErrorMessage = (err : any) : string => {
  228. const str = JSON.stringify(err)
  229. // 根据错误码和关键词判断(按优先级排序)
  230. if (str.includes('410021') || str.includes('数据网络未开启')) {
  231. return '请开启移动数据网络'
  232. }
  233. if (str.includes('-20202') || str.includes('蜂窝网络')) {
  234. return '请开启蜂窝网络'
  235. }
  236. if (str.includes('无SIM卡') || str.includes('SIM卡不存在')) {
  237. return '请插入SIM卡'
  238. }
  239. if (str.includes('超时') || str.includes('timeout')) {
  240. return '网络连接超时,请稍后重试'
  241. }
  242. if (str.includes('30005')) {
  243. return '预登录失败,请检查网络设置'
  244. }
  245. // 提取具体错误信息作为备选
  246. let match = str.match(/"resultMsg":"([^"]+)"/)
  247. if (match == null) {
  248. match = str.match(/"errorDesc":"([^"]+)"/)
  249. }
  250. if (match == null) {
  251. match = str.match(/"errMsg":"([^"]+)"/)
  252. }
  253. if (match != null && match.length > 1) {
  254. const msg = match[1]
  255. if (msg != null) {
  256. return msg
  257. }
  258. }
  259. return '登录失败,请检查网络'
  260. }
  261. const preLogin = (callback : (() => void)) => {
  262. uniVerifyManager.value?.preLogin({
  263. success: (res) => {
  264. phone.value = res.number;
  265. slogan.value = res.slogan;
  266. privacyName.value = res.privacyName;
  267. privacyUrl.value = res.privacyUrl;
  268. callback();
  269. },
  270. fail: (err) => {
  271. const hasCauseMessage = (err.cause?.cause?.message ?? '').length > 0
  272. console.log(JSON.parse(err.cause?.cause?.message as string))
  273. const msgObj = JSON.parse(err.cause?.cause?.message as string) as UTSJSONObject
  274. if (hasCauseMessage) {
  275. uni.showModal({
  276. title: msgObj.getString('resultMsg'),
  277. content: getErrorMessage(err),
  278. showCancel: false
  279. });
  280. }
  281. }
  282. });
  283. }
  284. const pushCustomPage = () => {
  285. const url = '/pages/login/verifyManager?phone=' + phone.value + '&slogan=' + slogan.value + '&name=' + privacyName.value + '&link=' + privacyUrl.value;
  286. uni.openDialogPage({
  287. url: url,
  288. animationType: 'slide-in-bottom',
  289. success(res) {
  290. console.log("成功打开自定义登录页面");
  291. },
  292. fail(err) {
  293. console.log(err);
  294. }
  295. })
  296. }
  297. const customLoginIn = () => {
  298. if ('production' === process.env.NODE_ENV && '__UNI__HelloUniAppX' === uni.getAppBaseInfo().appId) {
  299. uni.showModal({
  300. title: '提示',
  301. content: '一键登录为收费功能,当前环境暂不支持。请在HBuilderX中新建Hello uni-app x项目真机运行体验!',
  302. showCancel: false
  303. })
  304. return
  305. }
  306. const isPreLoginValid = uniVerifyManager.value?.isPreLoginValid() ?? false;
  307. if (isPreLoginValid) {
  308. pushCustomPage();
  309. } else {
  310. preLogin(() => {
  311. pushCustomPage();
  312. })
  313. }
  314. }
  315. onLoad(() => {
  316. if (isAppEnv) {
  317. uniVerifyManager.value = uni.getUniVerifyManager();
  318. }
  319. // 预登录
  320. preLogin(() => { });
  321. })
  322. </script>
  323. <style>
  324. .page-container {
  325. /* background: linear-gradient(180deg, #e0f7fa 0%, #fff8e1 100%); */
  326. height: 100%;
  327. padding: 40rpx 30rpx;
  328. box-sizing: border-box;
  329. }
  330. .close-btn {
  331. position: absolute;
  332. top: 40rpx;
  333. right: 30rpx;
  334. z-index: 10;
  335. }
  336. .header-section {
  337. align-items: center;
  338. margin-bottom: 60rpx;
  339. }
  340. .hello-text {
  341. font-size: 48rpx;
  342. font-weight: bold;
  343. color: #333;
  344. margin-bottom: 10rpx;
  345. }
  346. .welcome-text {
  347. font-size: 32rpx;
  348. color: #666;
  349. margin-bottom: 30rpx;
  350. }
  351. .logo-img {
  352. width: 300rpx;
  353. height: 300rpx;
  354. border-radius: 150rpx;
  355. background-color: rgba(255, 255, 255, 0.3);
  356. }
  357. linear-gradient .form-section {
  358. margin-bottom: 60rpx;
  359. }
  360. .input-box {
  361. background-color: #ffffff;
  362. border-radius: 30rpx;
  363. padding: 20rpx 30rpx;
  364. margin-bottom: 30rpx;
  365. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
  366. }
  367. .input-field {
  368. flex: 1;
  369. font-size: 28rpx;
  370. color: #333;
  371. }
  372. .code-btn {
  373. font-size: 26rpx;
  374. color: #ffc107;
  375. font-weight: bold;
  376. white-space: nowrap;
  377. }
  378. .code-btn.disabled {
  379. color: #ccc;
  380. }
  381. .login-btn {
  382. /* background: linear-gradient(90deg, #ffc107, #ffca2c); */
  383. color: #ffffff;
  384. font-size: 32rpx;
  385. font-weight: bold;
  386. text-align: center;
  387. padding: 24rpx 0;
  388. border-radius: 30rpx;
  389. margin-bottom: 30rpx;
  390. box-shadow: 0 4rpx 12rpx rgba(255, 193, 7, 0.3);
  391. }
  392. .login-btn.disabled {
  393. background: #ddd;
  394. color: #999;
  395. box-shadow: none;
  396. }
  397. .agree-row {
  398. align-items: flex-start;
  399. }
  400. .agree-content {
  401. flex: 1;
  402. margin-left: 10rpx;
  403. }
  404. .agree-text {
  405. font-size: 24rpx;
  406. color: #999;
  407. line-height: 1.6;
  408. }
  409. .link-text {
  410. color: #ffe205;
  411. border-bottom: 1rpx solid #ffff7f;
  412. }
  413. .footer-section {
  414. margin-top: auto;
  415. }
  416. .divider-row {
  417. margin-bottom: 40rpx;
  418. align-items: center;
  419. }
  420. .line {
  421. flex: 1;
  422. height: 1rpx;
  423. background-color: #ddd;
  424. }
  425. .divider-text {
  426. font-size: 26rpx;
  427. color: #999;
  428. margin: 0 20rpx;
  429. }
  430. .methods-row {
  431. justify-content: center;
  432. margin-bottom: 40rpx;
  433. }
  434. .method-item {
  435. margin-right: 60rpx;
  436. align-items: center;
  437. }
  438. .method-item:last-child {
  439. margin-right: 0;
  440. }
  441. .icon-box {
  442. width: 80rpx;
  443. height: 80rpx;
  444. border-radius: 40rpx;
  445. justify-content: center;
  446. align-items: center;
  447. margin-bottom: 15rpx;
  448. }
  449. /* .blue-bg {
  450. background: linear-gradient(135deg, #4a90e2, #67b26f);
  451. }
  452. .green-bg {
  453. background: linear-gradient(135deg, #00b894, #00cec9);
  454. } */
  455. .method-name {
  456. font-size: 26rpx;
  457. color: #666;
  458. }
  459. .modal-mask {
  460. position: fixed;
  461. top: 0;
  462. left: 0;
  463. right: 0;
  464. bottom: 0;
  465. background-color: rgba(0, 0, 0, 0.5);
  466. justify-content: center;
  467. align-items: center;
  468. z-index: 100;
  469. }
  470. .modal-box {
  471. background-color: #ffffff;
  472. border-radius: 20rpx;
  473. padding: 40rpx 30rpx;
  474. width: 600rpx;
  475. max-height: 800rpx;
  476. flex-direction: column;
  477. }
  478. .modal-title {
  479. font-size: 36rpx;
  480. font-weight: bold;
  481. color: #333;
  482. text-align: center;
  483. margin-bottom: 20rpx;
  484. }
  485. .modal-scroll {
  486. flex: 1;
  487. margin-bottom: 20rpx;
  488. }
  489. .modal-welcome {
  490. font-size: 28rpx;
  491. color: #333;
  492. margin-bottom: 15rpx;
  493. }
  494. .modal-desc {
  495. font-size: 26rpx;
  496. color: #666;
  497. line-height: 1.6;
  498. margin-bottom: 15rpx;
  499. }
  500. .modal-link {
  501. color: #00b894;
  502. border-bottom: 1rpx solid #00b894;
  503. }
  504. .modal-list {
  505. font-size: 26rpx;
  506. color: #666;
  507. line-height: 1.6;
  508. margin-bottom: 10rpx;
  509. }
  510. .modal-footer {
  511. font-size: 26rpx;
  512. color: #666;
  513. line-height: 1.6;
  514. margin-bottom: 10rpx;
  515. }
  516. .modal-btns {
  517. border-top: 1rpx solid #eee;
  518. padding-top: 20rpx;
  519. }
  520. .modal-btn {
  521. flex: 1;
  522. text-align: center;
  523. font-size: 28rpx;
  524. font-weight: bold;
  525. padding: 15rpx 0;
  526. }
  527. .modal-btn.reject {
  528. color: #999;
  529. margin-right: 20rpx;
  530. }
  531. .modal-btn.agree {
  532. color: #00b894;
  533. }
  534. .row-between {
  535. flex-direction: row;
  536. justify-content: space-between;
  537. align-items: center;
  538. }
  539. .row-start {
  540. flex-direction: row;
  541. align-items: flex-start;
  542. }
  543. .row-center {
  544. flex-direction: row;
  545. justify-content: center;
  546. align-items: center;
  547. }
  548. .column-center {
  549. flex-direction: column;
  550. justify-content: center;
  551. align-items: center;
  552. }
  553. </style>