123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- import React from "react";
- import Taro from "@tarojs/taro";
- import md5 from "md5";
- import {
- login,
- signin,
- signup,
- currentUser,
- authPhone,
- authUser,
- } from "@/services/wxma";
- import { changePassword } from "@/services/sysuser";
- import { ROLE_CITIZEN } from "@/utils/user";
- import { setToken } from "@/utils/token";
-
- export default function useUser() {
- const [person, setPerson] = React.useState();
- const [user, setUser] = React.useState();
- const [duty, setDuty] = React.useState();
-
- const maLogin = (code) => {
- login(code)
- .then((res) => {
- // setPerson(res.person);
- Taro.setStorage({ key: "personId", data: res.person.personId });
- Taro.setStorage({ key: "sessionKey", data: res.sessionKey });
- })
- .catch((e) => {
- console.error(e);
- });
- };
-
- const updateDuty = (d = ROLE_CITIZEN) => {
- setDuty(d);
- Taro.setStorageSync("duty", d);
- };
-
- const initDuty = (u) => {
- console.log(u);
- const d = Taro.getStorageSync("duty");
- console.log(d);
- const dutyList = u?.dutyList || [];
- if (d) {
- if (dutyList.indexOf(d) < 0) {
- updateDuty(dutyList[0]);
- } else {
- updateDuty(d);
- }
- } else {
- updateDuty(dutyList[0]);
- }
- };
-
- const maSignIn = (params) => {
- const data = {
- ...params,
- password: md5(params.password),
- };
-
- return signin(data).then((res) => {
- setUser(res.user);
- initDuty(res.user);
- });
- };
-
- const maSignup = (data) => {
- return new Promise((resolve, reject) => {
- signup(data).then(res => {
- setPerson(res.person);
- setUser(res.user);
- initDuty(res.user);
- resolve(res);
- }).catch(reject);
- });
- }
-
- const current = () => {
- return new Promise((resolve, reject) => {
- currentUser()
- .then((res) => {
- if (res?.person) {
- setPerson(res.person);
- }
-
- setUser(res?.user);
- initDuty(res?.user);
- resolve(res?.user);
- })
- .catch((e) => {
- console.log(e);
- reject(e);
- });
- });
- };
-
- const signinByPhone = (code) => {
- return authPhone(code).then((res) => {
- setUser(res.user);
- initDuty(res.user);
- // setPerson(res.person);
- });
- };
-
- const authProfile = (data) => {
- const sessionKey = Taro.getStorageSync("sessionKey");
- authUser({ ...data, sessionKey }).then((res) => {
- setPerson(res);
- });
- };
-
- const signOut = () => {
- console.log(111111)
- Taro.navigateTo({
- url: "/pages/login/index",
- });
- setPerson();
- setUser();
- initDuty();
- setToken();
-
- };
-
- const changePwd = (params) => {
- const data = {
- originPassword: md5(params.originPassword),
- newPassword: md5(params.newPassword),
- };
-
- changePassword(data)
- .then((res) => {
- signOut()
-
- })
- .catch((e) => {
- console.error(e);
- });
- };
-
- return {
- user,
- person,
- current,
- duty,
- signin: maSignIn,
- login: maLogin,
- updateDuty,
- signinByPhone,
- authProfile,
- signOut,
- changePwd,
- setPerson,
- signup: maSignup,
- };
- }
|