From 23db03121226f527a7d3d7d0b64415ab2122e891 Mon Sep 17 00:00:00 2001 From: yms Date: Fri, 16 Aug 2024 11:04:59 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E6=88=90=E8=AE=A4=E8=AF=81?= =?UTF-8?q?=E5=AF=B9=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/index.ts | 1 + package.json | 3 +- src/pages/authentication/index.tsx | 29 ++++++-- src/pages/index/index.tsx | 5 +- src/pages/index/login/index.tsx | 111 +++++++++++++++++++++++------ src/pages/user/index.less | 10 ++- src/pages/user/index.tsx | 104 ++++++++++++++++----------- src/services/core/api.ts | 3 + src/services/core/apiCore.d.ts | 24 ++++++- src/services/core/request.ts | 12 ++-- src/services/index.ts | 5 ++ src/services/loginController.ts | 80 +++++++++++++++++++++ src/store/index.ts | 28 ++++++++ src/utils/index.ts | 9 ++- yarn.lock | 12 ++++ 15 files changed, 352 insertions(+), 84 deletions(-) create mode 100644 src/services/index.ts create mode 100644 src/services/loginController.ts create mode 100644 src/store/index.ts diff --git a/config/index.ts b/config/index.ts index 432e870..872778b 100644 --- a/config/index.ts +++ b/config/index.ts @@ -28,6 +28,7 @@ export default defineConfig<"vite">(async (merge, {}) => { "@/services": path.resolve(__dirname, "../src/services"), "@/assets": path.resolve(__dirname, "../src/assets"), "@/utils": path.resolve(__dirname, "../src/utils"), + "@/store": path.resolve(__dirname, "../src/store"), }, framework: "react", compiler: "vite", diff --git a/package.json b/package.json index 50a4e26..d8e1cf7 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,8 @@ "@tarojs/taro": "4.0.3", "babel-plugin-import": "^1.13.8", "react": "^18.0.0", - "react-dom": "^18.0.0" + "react-dom": "^18.0.0", + "zustand": "^4.5.4" }, "devDependencies": { "@babel/core": "^7.24.4", diff --git a/src/pages/authentication/index.tsx b/src/pages/authentication/index.tsx index 54f5255..ef0e58c 100644 --- a/src/pages/authentication/index.tsx +++ b/src/pages/authentication/index.tsx @@ -7,16 +7,17 @@ import { Button, Toast } from "@taroify/core"; import api from "@/services/core/api"; import { useState } from "react"; import { composeImgUrl } from "@/utils"; +import services from "@/services"; export default function Index() { const [imgUrl, setImgUrl] = useState(""); - const [originData, setOriginData] = useState(); + const [originData, setOriginData] = useState(); const [form, setForm] = useState({}); useLoad(() => { const params = Taro.getCurrentInstance().router?.params; if (params) { - setOriginData(params); + setOriginData(params as unknown as API.TypeOfLegalPerson); } }); const chooseImage = () => { @@ -83,18 +84,32 @@ export default function Index() { }); return; } - // console.log() + const params = { - ...form, - imgUrl, + name: form.name, + unifiedSocialCreditCode: form.creditCode, + contactName: form.contact, + phone: form.phone, + attachmentUrl: imgUrl, + entityTypeId: originData?.id, }; - console.log("填写成功", params); + services.login.submitAuthentication(params).then((res) => { + if (res.code === 200) { + Taro.showToast({ + title: "认证成功", + icon: "none", + }); + setTimeout(() => { + Taro.navigateBack(); + }, 500); + } + }); }; return ( - {originData?.title} + {originData?.name} diff --git a/src/pages/index/index.tsx b/src/pages/index/index.tsx index ebb76a9..8953be9 100644 --- a/src/pages/index/index.tsx +++ b/src/pages/index/index.tsx @@ -2,10 +2,13 @@ import { View, Text, ScrollView } from "@tarojs/components"; import Taro, { useLoad } from "@tarojs/taro"; import "./index.less"; import Login from "./login"; +import useGlobalStore from "@/store"; export default function Index() { + const { updateUserInfo } = useGlobalStore(); + useLoad(() => { - console.log("Page loaded."); + updateUserInfo(); }); return ( diff --git a/src/pages/index/login/index.tsx b/src/pages/index/login/index.tsx index bd9153c..1bbd9be 100644 --- a/src/pages/index/login/index.tsx +++ b/src/pages/index/login/index.tsx @@ -1,22 +1,43 @@ -import { View, Image } from "@tarojs/components"; +import { View, Image, Input } from "@tarojs/components"; import "./index.less"; -import { Button, Popup, Input, Field, Flex } from "@taroify/core"; +import { Button, Popup, Field, Flex } from "@taroify/core"; import { useState } from "react"; import icon from "../../../assets/icon.png"; import Taro from "@tarojs/taro"; +import services from "@/services"; +import useGlobalStore from "@/store"; +import api from "@/services/core/api"; const defaultAvatarUrl = "https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0"; -let loginStatus = false; const Login = () => { const [open, setOpen] = useState(false); - const [avatarUrl, setAvatarUrl] = useState(defaultAvatarUrl); + const [avatarUrl, setAvatarUrl] = useState(); + const { userInfo, updateUserInfo } = useGlobalStore(); - const getRealtimePhoneNumber = (e) => { - console.log(e.detail.code); // 动态令牌 - console.log(e.detail.errMsg); // 回调信息(成功失败都会返回) - console.log(e.detail.errno); // 错误码(失败时返回) - setOpen(true); + const [nickName, setNickname] = useState(""); + // console.log(userInfo); + const getRealtimePhoneNumber = (e: any) => { + services.login + .login(e.detail.code) + .then((res) => { + Taro.setStorageSync("token", res.data.access_token); + if (res.code === 200) { + if (res.data.user_info.nickname !== null) { + Taro.showToast({ + title: "登录成功", + }); + updateUserInfo(); + } else { + setOpen(true); + } + } + }) + .catch(() => { + Taro.showToast({ + title: "登录失败", + }); + }); }; const onChooseAvatar = (e) => { @@ -24,9 +45,43 @@ const Login = () => { setAvatarUrl(avatarUrl); }; - const onLogin = () => { - setOpen(false); - loginStatus = true; + const onSetInfo = () => { + if (!avatarUrl || avatarUrl?.length == 0) { + Taro.showToast({ + title: "请上传头像", + icon: "error", + }); + return; + } + if (nickName.length === 0) { + Taro.showToast({ + title: "请输入昵称", + icon: "error", + }); + return; + } + + Taro.uploadFile({ + url: api.uploadUrl, //仅为示例,非真实的接口地址 + filePath: avatarUrl, + name: "file", + success(res) { + if (res.statusCode === 200) { + const data = JSON.parse(res.data); + services.login + .updateUserInfo({ + avatar: data.data, + nickname: nickName, + }) + .then(() => { + setTimeout(() => { + updateUserInfo(); + setOpen(false); + }, 100); + }); + } + }, + }); }; const onclickAction = () => { @@ -38,9 +93,9 @@ const Login = () => { return ( - {loginStatus ? ( + {userInfo ? ( - 大院河鲜鱼庄 + {userInfo?.nickname} ) : ( @@ -48,7 +103,7 @@ const Login = () => { )} - {loginStatus ? ( + {userInfo ? ( @@ -56,8 +111,8 @@ const Login = () => { @@ -69,6 +124,7 @@ const Login = () => { rounded onClose={() => { setOpen(false); + Taro.removeStorageSync("token"); }} placement="bottom" style={{ height: "50%" }} @@ -84,7 +140,14 @@ const Login = () => { variant="text" onChooseAvatar={onChooseAvatar} > - + 0 + ? avatarUrl + : defaultAvatarUrl + } + > @@ -92,6 +155,14 @@ const Login = () => { type="nickname" className="weui-input" placeholder="请输入昵称" + holdKeyboard + onBlur={(e) => { + console.log(e); + setNickname(e.detail.value); + }} + onInput={(e) => { + setNickname(e.detail.value); + }} /> @@ -100,9 +171,9 @@ const Login = () => { style={{ marginTop: 24 }} block color="primary" - onClick={onLogin} + onClick={onSetInfo} > - 完成设置 + 完成注册 diff --git a/src/pages/user/index.less b/src/pages/user/index.less index 662a92e..69855d7 100644 --- a/src/pages/user/index.less +++ b/src/pages/user/index.less @@ -61,6 +61,7 @@ font-weight: 800; font-size: 44px; color: #2a2d31; + margin-bottom: 24px; } .popup-content_itemIcon { @@ -71,18 +72,23 @@ .popup-content_itemCard { display: flex; - height: 154px; + height: 124px; background: #fff; - margin-top: 24px; + // margin-top: 24px; border-radius: 13px; border: 1px solid #f1f1f1; align-items: center; padding: 0 44px; font-weight: 500; font-size: 33px; + margin-bottom: 24px; color: #2a2d31; &:active { opacity: 0.6; } } + +.popup-content_scrollView { + height: 800px; +} diff --git a/src/pages/user/index.tsx b/src/pages/user/index.tsx index 2b9ce3b..140ccff 100644 --- a/src/pages/user/index.tsx +++ b/src/pages/user/index.tsx @@ -1,31 +1,31 @@ -import { View, Image } from "@tarojs/components"; +import { View, Image, ScrollView } from "@tarojs/components"; import "./index.less"; import { Avatar, ConfigProvider, Flex, Navbar, Popup } from "@taroify/core"; -import Taro, { useLoad } from "@tarojs/taro"; -import { useState } from "react"; +import Taro, { useDidShow, useLoad } from "@tarojs/taro"; +import { useEffect, useState } from "react"; import icon from "../../assets/icon.png"; import icon1 from "../../assets/icon1.png"; -import icon2 from "../../assets/icon2.png"; -import icon3 from "../../assets/icon3.png"; -const authenticationList = [ - { - icon: icon1, - title: "物业认证", - }, - { - icon: icon2, - title: "商铺认证", - }, - { - icon: icon3, - title: "外包单位认证", - }, -]; +import useGlobalStore from "@/store"; +import services from "@/services"; +import { composeImgUrl } from "@/utils"; export default function Index() { const [statusBarHeight, setStatusBarHeight] = useState(0); const [open, setOpen] = useState(false); + const { userInfo } = useGlobalStore(); + const [authenticationTypesList, setAuthenticationTypesList] = useState< + API.TypeOfLegalPerson[] + >([]); + const [isAuth, setIsAuth] = useState(false); + + useDidShow(() => { + services.login.queryAuthenticationInfo().then((res) => { + if (res.code === 200) { + setIsAuth(true); + } + }); + }); const onClick = () => { Taro.navigateBack(); }; @@ -34,11 +34,31 @@ export default function Index() { const systemInfo = Taro.getSystemInfoSync(); setStatusBarHeight(systemInfo?.statusBarHeight ?? 0); }); - const onAuthentication = (data: any) => { + const onAuthentication = (data: API.TypeOfLegalPerson) => { Taro.navigateTo({ - url: `/pages/authentication/index?title=${data.title}`, + url: `/pages/authentication/index?name=${data.name}&id=${data.id}`, + }).then(() => { + setOpen(false); }); }; + + const openAuthenticationSelect = () => { + if (isAuth) { + return; + } + services.login.queryAuthenticationTypes().then((res) => { + if (res.code !== 200) { + Taro.showToast({ + title: "无法获取法人类型", + icon: "none", + }); + return; + } + setAuthenticationTypesList(res.data); + setOpen(true); + }); + }; + return ( @@ -61,36 +81,40 @@ export default function Index() { > 认证 - {authenticationList.map((item) => { - return ( - - - - {item.title} - - - ); - })} + + {authenticationTypesList.map((item) => { + return ( + + + + {item.name} + + + ); + })} + - - P - + 别暴躁 setOpen(true)} + onClick={openAuthenticationSelect} className="authenticationBtn" > - 去认证 + {isAuth ? "已认证" : "去认证"} diff --git a/src/services/core/api.ts b/src/services/core/api.ts index 3ecbf72..00e3b17 100644 --- a/src/services/core/api.ts +++ b/src/services/core/api.ts @@ -1,4 +1,7 @@ export default { dev: "http://192.168.2.21:21610", uploadUrl: "http://192.168.2.21:21610/oss/upload?bucketName=cgtf", + loginDev: "http://192.168.2.21:21601", + authenticationUrl: "http://172.23.0.113:21622", + assetsUrl: "https://xct.cdhncy.cn/file", }; diff --git a/src/services/core/apiCore.d.ts b/src/services/core/apiCore.d.ts index 510ecbf..b07e97c 100644 --- a/src/services/core/apiCore.d.ts +++ b/src/services/core/apiCore.d.ts @@ -1,6 +1,24 @@ +declare interface Resp { + code: number; + data: T; +} declare namespace API { - interface Resp { - code: string | number; - data: T; + interface LoginRespDto { + access_token: string; + user_info: { + nickname: string | null; + }; + } + interface UserInfoRespDto { + appUser: { + id: string; + nickname: string; + avatar: string; + }; + } + + interface TypeOfLegalPerson { + id: string; + name: string; } } diff --git a/src/services/core/request.ts b/src/services/core/request.ts index b9f7f9c..6da97c8 100644 --- a/src/services/core/request.ts +++ b/src/services/core/request.ts @@ -2,7 +2,7 @@ import Taro from "@tarojs/taro"; import apiConfig from "./api"; /** 对象转参数 */ -function objectToQuery(obj) { +export function objectToQuery(obj) { return Object.keys(obj) .map((k) => encodeURIComponent(k) + "=" + encodeURIComponent(obj[k])) .join("&"); @@ -42,10 +42,10 @@ const HttpRequestInterceptor = () => {}; const HttpResponseInterceptor = () => {}; /** 简单封装 */ -const request = ( +export default function request( url: string, - option: Taro.request.Option & { params?: any } -) => { + option: Partial & { params?: any } +): Promise { const params = option.params; url = urlRegExp(url) ? url : apiConfig.dev + url; @@ -73,6 +73,4 @@ const request = ( }, }); }); -}; - -export default request; +} diff --git a/src/services/index.ts b/src/services/index.ts new file mode 100644 index 0000000..d7c2796 --- /dev/null +++ b/src/services/index.ts @@ -0,0 +1,5 @@ +import * as loginController from "./loginController"; + +export default { + login: loginController, +}; diff --git a/src/services/loginController.ts b/src/services/loginController.ts new file mode 100644 index 0000000..13b4744 --- /dev/null +++ b/src/services/loginController.ts @@ -0,0 +1,80 @@ +import api from "./core/api"; +import request from "./core/request"; +/** 登陆 */ +export function login(code: string) { + const appid = "wx9f26d47068186f86"; + return request>(api.loginDev + "/auth/oauth2/token", { + method: "POST", + header: { + Authorization: "Basic c29jaWFsOnNvY2lhbA==", + "CLIENT-TOC": "Y", + "content-type": "application/x-www-form-urlencoded", + }, + + data: { + grant_type: "mobile", + mobile: `APP-SMS@${appid}_encrypt:MINI-_-${code}`, + }, + }); +} + +export function getUserInfo() { + return request>( + api.loginDev + "/app/app-user/info", + { + method: "GET", + header: { + "CLIENT-TOC": "Y", + }, + } + ); +} +/** 更新信息 */ +export function updateUserInfo(data: any) { + return request(api.loginDev + "/app/app-user/edit", { + method: "PUT", + header: { + "CLIENT-TOC": "Y", + }, + data, + }); +} + +/** 查询法人信息 */ +export function queryAuthenticationInfo() { + return request>( + api.authenticationUrl + "/hncy/entities/selectIsAuthentication", + { + method: "POST", + header: { + "CLIENT-TOC": "Y", + }, + } + ); +} +/** 查询法人类型 */ +export function queryAuthenticationTypes() { + return request>( + api.authenticationUrl + "/hncy/entities/selectByEntityTypes", + { + method: "POST", + header: { + "CLIENT-TOC": "Y", + }, + } + ); +} + +/** 提交认证信息 */ +export function submitAuthentication(data: any) { + return request>( + api.authenticationUrl + "/hncy/entities/authenticationSMZS", + { + method: "POST", + header: { + "CLIENT-TOC": "Y", + }, + data, + } + ); +} diff --git a/src/store/index.ts b/src/store/index.ts new file mode 100644 index 0000000..5c5f2a9 --- /dev/null +++ b/src/store/index.ts @@ -0,0 +1,28 @@ +import services from "@/services"; +import Taro from "@tarojs/taro"; +import { create } from "zustand"; + +interface GlobalStoreType { + userInfo?: API.UserInfoRespDto["appUser"]; + updateUserInfo(): void; +} +const useGlobalStore = create((set) => ({ + userInfo: undefined, + /** 更新用户信息 */ + updateUserInfo: () => { + const token = Taro.getStorageSync("token"); + if (token) { + services.login.getUserInfo().then((res) => { + if (res.code === 200) { + if (res.data.appUser.nickname !== null) { + set(() => ({ userInfo: res.data.appUser })); + } else { + Taro.removeStorageSync("token"); + } + } + }); + } + }, +})); + +export default useGlobalStore; diff --git a/src/utils/index.ts b/src/utils/index.ts index 2a027b5..1bc53b4 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -23,8 +23,11 @@ export function buildListByGroupId(target) { return groupList; } -export function composeImgUrl(imgStr: string) { - return `${api.dev}${imgStr}`; +export function composeImgUrl(imgStr?: string) { + if (!imgStr) { + return ""; + } + return `${api.assetsUrl}${imgStr}`; } export function inversionSerializationRule(inputStr) { @@ -39,4 +42,4 @@ export function inversionSerializationRule(inputStr) { return value; } -// code \ No newline at end of file +// code diff --git a/yarn.lock b/yarn.lock index c683a9b..bd4f322 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7270,6 +7270,11 @@ url-to-options@^1.0.1: resolved "https://registry.yarnpkg.com/url-to-options/-/url-to-options-1.0.1.tgz#1505a03a289a48cbd7a434efbaeec5055f5633a9" integrity sha512-0kQLIzG4fdk/G5NONku64rSH/x32NOA39LVQqlK8Le6lvTF6GGRJpqaQFGgU+CLwySIqBSMdwYM0sYcW9f6P4A== +use-sync-external-store@1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/use-sync-external-store/-/use-sync-external-store-1.2.0.tgz#7dbefd6ef3fe4e767a0cf5d7287aacfb5846928a" + integrity sha512-eEgnFxGQ1Ife9bzYs6VLi8/4X6CObHMw9Qr9tPY43iKwsPw8xE8+EFsf/2cFZ5S3esXgpWgtSCtLNS41F+sKPA== + util-deprecate@^1.0.1, util-deprecate@^1.0.2, util-deprecate@~1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf" @@ -7501,3 +7506,10 @@ yup@^1.2.0: tiny-case "^1.0.3" toposort "^2.0.2" type-fest "^2.19.0" + +zustand@^4.5.4: + version "4.5.4" + resolved "https://registry.yarnpkg.com/zustand/-/zustand-4.5.4.tgz#63abdd81edfb190bc61e0bbae045cc4d52158a05" + integrity sha512-/BPMyLKJPtFEvVL0E9E9BTUM63MNyhPGlvxk1XjrfWTUlV+BR8jufjsovHzrtR6YNcBEcL7cMHovL1n9xHawEg== + dependencies: + use-sync-external-store "1.2.0" -- GitLab