index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. <template>
  2. <view class="login-wrapper">
  3. <view
  4. class="back_extend"
  5. v-if="appStore?.$wxConfig?.auditModeEnabled"
  6. @click="backHome"
  7. >
  8. {{ "<" }} 回到首页
  9. </view>
  10. <view class="shading">
  11. <image :src="logoUrl" />
  12. <!-- <image src="/static/images/logo2.png" v-if="!logoUrl" /> -->
  13. </view>
  14. <view class="whiteBg" v-if="formItem === 1">
  15. <viewview class="list" v-if="current !== 1">
  16. <form @submit.prevent="submit">
  17. <view class="item">
  18. <view class="acea-row row-middle">
  19. <image
  20. src="https://sb-admin.oss-cn-shenzhen.aliyuncs.com/crmebimage/public/maintain/2025/08/28/phone_1.png"
  21. style="width: 24rpx; height: 34rpx"
  22. ></image>
  23. <input
  24. type="text"
  25. class="texts"
  26. placeholder="输入手机号码"
  27. v-model="account"
  28. required
  29. />
  30. </view>
  31. </view>
  32. <view class="item">
  33. <view class="acea-row row-middle">
  34. <image
  35. src="https://sb-admin.oss-cn-shenzhen.aliyuncs.com/crmebimage/public/maintain/2025/08/28/1cfaad5f37a64884b3eef985d254606eqzlswf1m9g.png"
  36. style="width: 28rpx; height: 32rpx"
  37. ></image>
  38. <input
  39. :password="true"
  40. class="texts"
  41. placeholder="填写登录密码"
  42. v-model="password"
  43. required
  44. :focus="isFocus"
  45. confirm-type="done"
  46. @focus="isFocus = true"
  47. @blur="isFocus =false"
  48. />
  49. </view>
  50. </view>
  51. </form>
  52. </viewview>
  53. <view
  54. class="list"
  55. v-if="current !== 0 || appLoginStatus || appleLoginStatus"
  56. >
  57. <view class="item">
  58. <view class="acea-row row-middle">
  59. <image
  60. src="https://sb-admin.oss-cn-shenzhen.aliyuncs.com/crmebimage/public/maintain/2025/08/28/phone_1.png"
  61. style="width: 24rpx; height: 34rpx"
  62. ></image>
  63. <input
  64. type="text"
  65. class="texts"
  66. placeholder="输入手机号码"
  67. v-model="account"
  68. />
  69. </view>
  70. </view>
  71. <view class="item">
  72. <view class="acea-row row-middle">
  73. <image
  74. src="https://sb-admin.oss-cn-shenzhen.aliyuncs.com/crmebimage/public/maintain/2025/08/28/1cfaad5f37a64884b3eef985d254606eqzlswf1m9g.png"
  75. style="width: 28rpx; height: 32rpx"
  76. ></image>
  77. <input
  78. type="text"
  79. placeholder="填写验证码"
  80. class="codeIput"
  81. v-model="captcha"
  82. />
  83. <button
  84. class="code"
  85. :disabled="disabled"
  86. :class="disabled === true ? 'on' : ''"
  87. @click="getCode"
  88. >
  89. {{ text }}
  90. </button>
  91. </view>
  92. </view>
  93. <view class="item">
  94. <view class="text-row row-middle">
  95. 新用户可使用验证码登录,将自动注册账号
  96. </view>
  97. <view class="text-row row-middle" v-if="invite_code">
  98. 邀请码:{{ invite_code }}
  99. </view>
  100. </view>
  101. <view class="item" v-if="isShowCode">
  102. <view class="acea-row row-middle">
  103. <image
  104. src="https://sb-admin.oss-cn-shenzhen.aliyuncs.com/crmebimage/public/maintain/2025/08/28/1cfaad5f37a64884b3eef985d254606eqzlswf1m9g.png"
  105. style="width: 28rpx; height: 32rpx"
  106. ></image>
  107. <input
  108. type="text"
  109. placeholder="填写验证码"
  110. class="codeIput"
  111. v-model="codeVal"
  112. />
  113. <view class="code" @click="again"><img :src="codeUrl" /></view>
  114. </view>
  115. </view>
  116. </view>
  117. <view class="tips">
  118. <view v-if="current == 0" @click="current = 1">快速登录</view>
  119. <view v-if="current == 1" @click="current = 0">账号登录</view>
  120. <view @click="change_password">忘记密码</view>
  121. </view>
  122. <view class="logon" @click="loginMobile" v-if="current !== 0">登录</view>
  123. <view class="logon" @click="submit" v-if="current === 0">登录</view>
  124. </view>
  125. <view class="bottom"></view>
  126. </view>
  127. </template>
  128. <script setup>
  129. import { ref, watch, onMounted } from "vue";
  130. import { onLoad, onBackPress as uniOnBackPress } from "@dcloudio/uni-app";
  131. import { useAppStore } from "@/stores/app";
  132. import {
  133. loginH5,
  134. loginMobile as loginMobileApi,
  135. registerVerify,
  136. register,
  137. getUserInfo,
  138. getUserOpenId,
  139. } from "@/api/user";
  140. import { getLogo, appAuth, appleLogin } from "@/api/public";
  141. import { VUE_APP_API_URL } from "@/utils";
  142. import { useSendCode } from "@/hooks/useSendCode";
  143. import Cache from "@/utils/cache";
  144. import { EXPIRES_TIME } from "@/config/cache";
  145. import { useToast } from "@/hooks/useToast.js";
  146. import { getSceneInfo } from '@/utils/util.js';
  147. import { footprintScan } from "@/api/merchant.js";
  148. const appStore = useAppStore();
  149. const { Toast } = useToast();
  150. const BACK_URL = "login_back_url";
  151. // Reactive state
  152. const navList = ref(["快速登录", "账号登录"]);
  153. const current = ref(1);
  154. const account = ref("");
  155. const password = ref("");
  156. const captcha = ref("");
  157. const formItem = ref(1);
  158. const type = ref("login");
  159. const logoUrl = ref("");
  160. const keyCode = ref("");
  161. const codeUrl = ref("");
  162. const codeVal = ref("");
  163. const isShowCode = ref(false);
  164. const platform = ref("");
  165. const appLoginStatus = ref(false);
  166. const appUserInfo = ref(null);
  167. const appleLoginStatus = ref(false);
  168. const appleUserInfo = ref(null);
  169. const appleShow = ref(false);
  170. const invite_code = ref("");
  171. const merchantId = ref('');
  172. const userInfo = ref({});
  173. const isFocus = ref(false)
  174. // Watch formItem to update type
  175. watch(formItem, (newVal) => {
  176. type.value = newVal === 1 ? "login" : "register";
  177. });
  178. const { disabled, text, sendCode } = useSendCode();
  179. // Refresh captcha image
  180. const again = () => {
  181. codeUrl.value = `${VUE_APP_API_URL}/sms_captcha?key=${
  182. keyCode.value
  183. }${Date.parse(new Date())}`;
  184. };
  185. // Get logo image
  186. const getLogoImage = async () => {
  187. try {
  188. const res = await getLogo();
  189. console.log('=========')
  190. logoUrl.value = res.data.logoUrl || "/static/images/logo2.png";
  191. } catch (err) {
  192. console.error(err);
  193. }
  194. };
  195. const getWechatOpenid = async () => {
  196. try {
  197. const { code, errMsg } = await uni.login();
  198. // if (!errMsg) return uni.showToast({ title: errMsg, icon: "none" }); // debug
  199. // if (!code) return uni.showToast({ title: "无code", icon: "none" }); // debug
  200. console.log(code,errMsg)
  201. const res = await getUserOpenId({ code });
  202. if (res.data?.code === 200 && res.data?.data?.openid) {
  203. const openid = res.data.data.openid;
  204. Cache.set("wxOpenid", openid);
  205. appStore.SET_WX_OPENID(openid);
  206. }
  207. } catch (err) {
  208. console.error("获取openid过程出错:", err);
  209. }
  210. };
  211. // Mobile login
  212. const loginMobile = async () => {
  213. if (!account.value)
  214. return uni.showToast({ title: "请填写手机号码", icon: "none" });
  215. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(account.value))
  216. return uni.showToast({ title: "请输入正确的手机号码", icon: "none" });
  217. if (!captcha.value)
  218. return uni.showToast({ title: "请填写验证码", icon: "none" });
  219. if (!/^[\w\d]+$/i.test(captcha.value))
  220. return uni.showToast({ title: "请输入正确的验证码", icon: "none" });
  221. try {
  222. const res = await loginMobileApi({
  223. phone: account.value,
  224. captcha: captcha.value,
  225. spread_spid: Cache.get("spread"),
  226. invite_code: invite_code.value,
  227. });
  228. // 使用 LOGIN action 保存 token 到缓存
  229. appStore.LOGIN({ token: res.data.token });
  230. // 保存过期时间(7天后过期)
  231. const expiresTime = Math.round(new Date() / 1000) + 7 * 24 * 60 * 60;
  232. Cache.set(EXPIRES_TIME, expiresTime, 0);
  233. await getUserInfoFn(res.data);
  234. } catch (res) {
  235. uni.showToast({ title: res.message, icon: "none" });
  236. }
  237. };
  238. // Register
  239. const registerFn = async () => {
  240. if (!account.value)
  241. return uni.showToast({ title: "请填写手机号码", icon: "none" });
  242. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(account.value))
  243. return uni.showToast({ title: "请输入正确的手机号码", icon: "none" });
  244. if (!captcha.value)
  245. return uni.showToast({ title: "请填写验证码", icon: "none" });
  246. if (!/^[\w\d]+$/i.test(captcha.value))
  247. return uni.showToast({ title: "请输入正确的验证码", icon: "none" });
  248. if (!password.value)
  249. return uni.showToast({ title: "请填写密码", icon: "none" });
  250. if (!/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/i.test(password.value))
  251. return uni.showToast({ title: "您输入的密码过于简单", icon: "none" });
  252. try {
  253. const res = await register({
  254. account: account.value,
  255. captcha: captcha.value,
  256. password: password.value,
  257. spread: Cache.get("spread"),
  258. });
  259. uni.showToast({ title: res.message, icon: "none" });
  260. formItem.value = 1;
  261. } catch (res) {
  262. uni.showToast({ title: res.message, icon: "none" });
  263. }
  264. };
  265. // Send verification code
  266. const getCode = async () => {
  267. if (!account.value)
  268. return uni.showToast({ title: "请填写手机号码", icon: "none" });
  269. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(account.value))
  270. return uni.showToast({ title: "请输入正确的手机号码", icon: "none" });
  271. if (formItem.value === 2) type.value = "register";
  272. try {
  273. const res = await registerVerify(account.value);
  274. uni.showToast({ title: res.message, icon: "none" });
  275. sendCode();
  276. } catch (err) {
  277. uni.showToast({ title: err.message, icon: "none" });
  278. }
  279. };
  280. // Navigation tab switch
  281. const navTap = (index) => {
  282. current.value = index;
  283. };
  284. // h5 login
  285. const submit = async () => {
  286. if (!account.value) return Toast({ title: "请填写账号", icon: "none" });
  287. if (!/^[\w\d]{5,16}$/i.test(account.value))
  288. return Toast({ title: "请输入正确的账号", icon: "none" });
  289. if (!password.value) return Toast({ title: "请填写密码", icon: "none" });
  290. try {
  291. const { data } = await loginH5({
  292. account: account.value,
  293. password: password.value,
  294. spread: Cache.get("spread"),
  295. });
  296. appStore.LOGIN({ token: data.token });
  297. // 保存过期时间(7天后过期)
  298. const expiresTime = Math.round(new Date() / 1000) + 7 * 24 * 60 * 60;
  299. Cache.set(EXPIRES_TIME, expiresTime, 0);
  300. await getUserInfoFn(data);
  301. } catch (err) {
  302. console.log("submit error", err);
  303. Toast({ title: err, icon: "none" });
  304. }
  305. };
  306. const getUserInfoFn = async (data) => {
  307. try {
  308. appStore.SETUID(data.uid);
  309. await getWechatOpenid();
  310. const res = await getUserInfo();
  311. appStore.UPDATE_USERINFO(res.data);
  312. userInfo.value = res.data;
  313. if(merchantId.value != ''){
  314. let obj ={
  315. merchantId:merchantId.value,
  316. userId:res.data.userId
  317. }
  318. await footprintScanFn(obj)
  319. }else{
  320. appStore.UPDATE_MERCHANT_ID('')
  321. Toast({ title: "登录成功" });
  322. backHome();
  323. }
  324. } catch (err) {
  325. console.error(err);
  326. uni.showToast({ title: err.msg, icon: "none" });
  327. }
  328. };
  329. onLoad((options) => {
  330. merchantId.value = appStore.merchantId || '';
  331. getLogoImage();
  332. });
  333. onMounted(() => {
  334. uni.getDeviceInfo({
  335. success(res) {
  336. // 获取平台(如 "ios"、"android")
  337. platform.value = res.platform.toLowerCase();
  338. // 解析系统版本(以 iOS 为例,res.system 格式如 "iOS 16.5")
  339. if (platform.value === "ios") {
  340. const systemParts = res.system.split(" ");
  341. if (systemParts.length > 1) {
  342. const iosVersion = parseFloat(systemParts[1]);
  343. // iOS 版本 >= 13 时显示 Apple 登录相关内容
  344. if (iosVersion >= 13) {
  345. appleShow.value = true;
  346. }
  347. }
  348. }
  349. },
  350. fail(err) {
  351. console.error("获取设备信息失败:", err);
  352. },
  353. });
  354. });
  355. const backHome = () => {
  356. if(userInfo.value.merchant && userInfo.value.merchant.id){
  357. uni.navigateTo({
  358. url:"/pages/merchantCenter/index"
  359. })
  360. }else{
  361. uni.switchTab({
  362. url: "/pages/index/index",
  363. });
  364. }
  365. };
  366. const change_password = () => {
  367. uni.navigateTo({
  368. url: "/pages/change_password/change_password",
  369. });
  370. };
  371. const footprintScanFn =async (data) => {
  372. const res = await footprintScan(data);
  373. Toast({ title: "登录成功" });
  374. backHome();
  375. }
  376. </script>
  377. <style>
  378. page {
  379. background: #fff !important;
  380. }
  381. </style>
  382. <style lang="scss" scoped>
  383. .appLogin {
  384. margin-top: 60rpx;
  385. .hds {
  386. display: flex;
  387. justify-content: center;
  388. align-items: center;
  389. font-size: 24rpx;
  390. color: #b4b4b4;
  391. .line {
  392. width: 68rpx;
  393. height: 1rpx;
  394. background: #cccccc;
  395. }
  396. p {
  397. margin: 0 20rpx;
  398. }
  399. }
  400. .btn-wrapper {
  401. display: flex;
  402. align-items: center;
  403. justify-content: center;
  404. margin-top: 30rpx;
  405. .btn {
  406. display: flex;
  407. align-items: center;
  408. justify-content: center;
  409. width: 68rpx;
  410. height: 68rpx;
  411. border-radius: 50%;
  412. }
  413. .apple-btn {
  414. display: flex;
  415. align-items: center;
  416. justify-content: center;
  417. margin-left: 30rpx;
  418. background: #000;
  419. border-radius: 34rpx;
  420. font-size: 40rpx;
  421. .icon-s-pingguo {
  422. color: #fff;
  423. font-size: 40rpx;
  424. }
  425. }
  426. .iconfont {
  427. font-size: 40rpx;
  428. color: #fff;
  429. }
  430. .wx {
  431. margin-right: 30rpx;
  432. background-color: #61c64f;
  433. }
  434. .mima {
  435. background-color: #28b3e9;
  436. }
  437. .yanzheng {
  438. background-color: #f89c23;
  439. }
  440. }
  441. }
  442. .code img {
  443. width: 100%;
  444. height: 100%;
  445. }
  446. .acea-row.row-middle {
  447. input {
  448. margin-left: 20rpx;
  449. display: block;
  450. }
  451. }
  452. .text-row {
  453. font-size: 22rpx;
  454. color: #999;
  455. }
  456. .login-wrapper {
  457. padding: 0 30rpx;
  458. height: 100vh;
  459. .back_extend {
  460. position: fixed;
  461. top: 50px;
  462. left: 10px;
  463. font-size: 26rpx;
  464. color: #999;
  465. }
  466. .shading {
  467. display: flex;
  468. align-items: center;
  469. justify-content: center;
  470. width: 100%;
  471. /* #ifdef APP-VUE */
  472. margin-top: 50rpx;
  473. /* #endif */
  474. /* #ifndef APP-VUE */
  475. margin-top: 200rpx;
  476. /* #endif */
  477. image {
  478. width: 180rpx;
  479. height: 180rpx;
  480. border-radius: 50%;
  481. }
  482. }
  483. .whiteBg {
  484. margin-top: 100rpx;
  485. .list {
  486. border-radius: 16rpx;
  487. overflow: hidden;
  488. .item {
  489. border-bottom: 1px solid #f0f0f0;
  490. background: #fff;
  491. .row-middle {
  492. position: relative;
  493. padding: 16rpx 45rpx;
  494. .texts {
  495. flex: 1;
  496. font-size: 28rpx;
  497. height: 80rpx;
  498. line-height: 80rpx;
  499. //display: flex;
  500. //justify-content: center;
  501. //align-items: center;
  502. }
  503. input {
  504. flex: 1;
  505. font-size: 28rpx;
  506. height: 80rpx;
  507. line-height: 80rpx;
  508. //display: flex;
  509. //justify-content: center;
  510. //align-items: center;
  511. }
  512. .code {
  513. position: absolute;
  514. right: 30rpx;
  515. top: 50%;
  516. color: $theme-color;
  517. font-size: 26rpx;
  518. z-index: 999;
  519. transform: translateY(-50%);
  520. border: 1px solid $theme-color;
  521. border-radius: 14rpx;
  522. padding: 8rpx 10rpx;
  523. }
  524. }
  525. }
  526. }
  527. .logon {
  528. display: flex;
  529. align-items: center;
  530. justify-content: center;
  531. width: 100%;
  532. height: 86rpx;
  533. margin-top: 80rpx;
  534. background-color: $theme-color;
  535. border-radius: 16rpx;
  536. color: #ffffff;
  537. font-size: 30rpx;
  538. }
  539. .tips {
  540. margin: 0.9375rem;
  541. text-align: center;
  542. color: #999;
  543. display: flex;
  544. justify-content: space-between;
  545. align-items: center;
  546. }
  547. }
  548. }
  549. </style>