login.uvue 15 KB

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