| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- /* eslint-disable no-console */
- /* eslint-disable no-underscore-dangle */
- // 用户状态 (商家状态,全局)
- import * as RootNavigation from "../navigation/RootNavigation";
- export default {
- state: {
- mid: null,
- name: "未登录",
- showName: "未登录",
- phone: "",
- isLogin: false,
- guideStep: "0",
- registerInfo: {},
- initApp: true,
- refreashReason: "login",
- initRoute: "Login",
- userInfo: {},
- },
- actions: ({ model, setState }) => ({
- updateUser(userInfo) {
- setState({ ...userInfo });
- setState({ userInfo });
- },
- getUserInfo() {
- const { updateUser } = model();
- const { httpGet } = model("httpModel");
- const { changeVerfied } = model("verifiedModel");
- return httpGet("/merchant/my", {}, true)
- .then(res => {
- const _res = { ...res };
- if (_res) {
- updateUser(_res);
- changeVerfied(_res.userId);
- }
- })
- .catch(() => {
- setState({ mid: 0 });
- });
- },
- changeInIt() {
- setState({ initApp: false });
- },
- checkLogin() {
- const { getUserInfo, status } = model();
- const { getAsyncStorage } = model("httpModel");
- return new Promise(resolve => {
- getUserInfo()
- .then(async () => {
- let guideStep = await getAsyncStorage("guideStep");
- if (status === "PASS") {
- guideStep = "4";
- }
- if (guideStep) {
- setState({ guideStep, isLogin: true });
- } else {
- setState({ isLogin: true });
- }
- resolve();
- })
- .catch(() => {
- setState({ isLogin: false });
- resolve();
- });
- });
- },
- changeRegisterInfo({ ...info }) {
- const { registerInfo } = model();
- setState({
- registerInfo: {
- ...registerInfo,
- ...info,
- },
- });
- },
- loginByPassword(phone, password) {
- const { httpPost, addAsyncStorage } = model("httpModel");
- const { success, warnning } = model("loadingModel");
- const { getUserInfo } = model();
- return httpPost("/auth/login", {
- username: phone,
- password,
- })
- .then(res => {
- success("登录成功");
- return addAsyncStorage("token", res);
- })
- .then(() => {
- return getUserInfo();
- })
- .catch(e => {
- warnning(e.error);
- });
- },
- loginByCode(phone, code) {
- console.log(phone, code);
- // httpPost("/auth/phoneLogin", {
- // phone,
- // code,
- // })
- // .then(res => {
- // if (res) {
- // updateUser(res);
- // }
- // })
- // .catch(e => {
- // console.log(e);
- // });
- },
- registerFirst({ ...data }) {
- setState({ registerInfo: data });
- RootNavigation.navigate("RegisterSe");
- },
- registerUser({ ...data }) {
- const { getUserInfo } = model();
- const { success } = model("loadingModel");
- const { httpPost, addAsyncStorage } = model("httpModel");
- const { saveVeriFied, registerVerifiedInfo } = model("verifiedModel");
- httpPost("/auth/merchantRegister", data, {}, true)
- .then(res => {
- return addAsyncStorage("token", res);
- })
- .then(() => {
- setState({
- guideStep: "1",
- });
- return addAsyncStorage("guideStep", "1");
- })
- .then(() => {
- return getUserInfo();
- })
- .then(() => {
- return saveVeriFied(registerVerifiedInfo);
- })
- .then(() => {
- success("注册成功");
- });
- // setTimeout(() => {
- // success("注册成功");
- // }, 1000);
- // getUserInfo().then(_=>{
- // })
- },
- registerSecend({ ...data }) {
- const { registerInfo, registerUser } = model();
- const _registerInfo = {
- ...registerInfo,
- ...data,
- };
- registerUser(_registerInfo);
- },
- changeGuideStep(step) {
- const { addAsyncStorage } = model("httpModel");
- setState({ guideStep: step.toString() });
- return addAsyncStorage("guideStep", step.toString());
- },
- saveMerchant({ ...data }) {
- const { mid, changeGuideStep } = model();
- const { httpPost } = model("httpModel");
- httpPost(
- "/merchant/saveDTO",
- {
- ...data,
- mid,
- },
- { body: "json" }
- ).then(() => {
- changeGuideStep("4");
- });
- },
- checkInfo({ aliAccountEvent, aliNameEvent }) {
- const { aliAccount, aliName } = model();
- aliAccountEvent(aliAccount);
- aliNameEvent(aliName);
- },
- updateMerchant({ ...data }) {
- const { mid, getUserInfo } = model();
- const { httpPost } = model("httpModel");
- return httpPost(
- "/merchant/saveDTO",
- {
- ...data,
- mid,
- },
- { body: "json" },
- true
- ).then(res => {
- // success("修改成功");
- if (res) {
- getUserInfo();
- }
- });
- },
- uploadStoreImg(img, type) {
- const { updateMerchant, mid, registerInfo } = model();
- if (mid !== 0) {
- if (type === "banner") {
- return updateMerchant({ banner: img });
- }
- if (type === "qualification") {
- return updateMerchant({ qualification: img });
- }
- return updateMerchant({ logo: img });
- }
- if (type === "qualification") {
- const _registerInfo = { ...registerInfo };
- _registerInfo.qualification = img;
- console.log(_registerInfo);
- setState({
- registerInfo: _registerInfo,
- });
- return Promise.resolve();
- }
- return Promise.reject();
- },
- userLogout() {
- const { removeAsyncStorage } = model("httpModel");
- const { success } = model("loadingModel");
- const { saveVeriFied } = model("verifiedModel");
- return removeAsyncStorage("token")
- .then(() => {
- setState({
- mid: 0,
- name: "未登录",
- showName: "未登录",
- phone: "",
- isLogin: false,
- guideStep: "0",
- registerInfo: null,
- initApp: true,
- refreashReason: "loginOut",
- userId: "",
- });
- // 清空认证信息
- return saveVeriFied({});
- })
- .then(() => {
- success("退出成功");
- });
- },
- getGuideStep() {
- const { mid, status } = model();
- const { getAsyncStorage } = model("httpModel");
- return new Promise(resolve => {
- if (mid === 0) {
- resolve("");
- } else {
- getAsyncStorage("guideStep").then(res => {
- console.log(res);
- let guideStep = res;
- if (status === "PASS" && !guideStep) {
- guideStep = "5";
- } else if (
- status === "DENY" &&
- (!guideStep || guideStep === "finish")
- ) {
- guideStep = "5";
- }
- setState({ guideStep, isLogin: true });
- resolve(guideStep || "1");
- });
- }
- });
- },
- checkNowGuideStep() {
- const { status } = model();
- const { getAsyncStorage } = model("httpModel");
- return getAsyncStorage("guideStep").then(res => {
- console.log(res);
- let guideStep = res;
- if (status === "PASS" && !guideStep) {
- guideStep = "5";
- } else if (
- status === "DENY" &&
- (!guideStep || guideStep === "finish")
- ) {
- guideStep = "5";
- } else if (status === "PENDING") {
- guideStep = "5";
- }
- setState({ guideStep: guideStep || "1" });
- return Promise.resolve(guideStep || "1");
- });
- },
- closeMer() {
- const { isOpening, getUserInfo } = model();
- const { httpGet } = model("httpModel");
- const { success } = model("loadingModel");
- const { showDialog } = model("dialogModel");
- if (isOpening) {
- showDialog({
- bodyText: "停止当前营业,直到下次营业开启",
- status: "danger",
- cancelable: true,
- confirmCallback: () => {
- httpGet("/merchant/closeMer", {}, true).then(() => {
- success("操作成功");
- getUserInfo();
- });
- },
- });
- } else {
- httpGet("/merchant/closeMer", {}, true).then(() => {
- success("操作成功");
- getUserInfo();
- });
- }
- },
- setinitRoute(name) {
- setState({ initRoute: name });
- },
- getNowUser() {
- const {
- mid,
- category,
- address,
- week,
- startTime,
- endTime,
- merchantNatureId,
- qualification,
- logo,
- } = model();
- return {
- mid,
- category,
- address,
- week,
- startTime,
- endTime,
- merchantNatureId,
- qualification,
- logo,
- };
- },
- checkAgain({ ...info }) {
- const { loading } = model("loadingModel");
- const { updateMerchant, changeGuideStep } = model();
- loading();
- updateMerchant({
- ...info,
- status: "PENDING",
- }).then(() => {
- changeGuideStep("5");
- });
- },
- }),
- };
|