| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- //用户状态 (商家状态,全局)
- import * as RootNavigation from "../navigation/RootNavigation.js";
- export default {
- state: {
- mid: 0,
- name: "未登录",
- showName: "未登录",
- phone: "",
- isLogin: false,
- guideStep: 0,
- registerInfo: null,
- initApp: true,
- refreashReason: "login",
- },
- actions: ({ model, setState }) => ({
- updateUser(userInfo) {
- setState({ ...userInfo });
- },
- getUserInfo() {
- setState({ initApp: false });
- const { updateUser } = model();
- const { httpGet } = model("httpModel");
- return httpGet("/merchant/my").then(res => {
- let _res = { ...res };
- if (_res) {
- updateUser(_res);
- }
- });
- },
- 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: guideStep, isLogin: true });
- } else {
- setState({ isLogin: true });
- }
- const { isLogin } = model();
- console.log(isLogin);
- resolve();
- })
- .catch(_ => {
- setState({ isLogin: false });
- resolve();
- });
- });
- },
- loginByPassword(phone, password) {
- const { httpPost, addAsyncStorage } = model("httpModel");
- const { checkLogin } = model();
- const { loading, success, warnning } = model("loadingModel");
- return httpPost("/auth/login", {
- username: phone,
- password: password,
- })
- .then(res => {
- if (res) {
- addAsyncStorage("token", res);
- success("登录成功");
- setState({ initApp: true });
- }
- })
- .catch(e => {
- warnning(e.error);
- });
- },
- loginByCode(phone, code) {
- loading();
- httpPost("/auth/phoneLogin", {
- phone: phone,
- code: code,
- })
- .then(res => {
- if (res) {
- updateUser(res);
- }
- })
- .catch(e => {
- console.log(e);
- });
- },
- registerFirst({ ...data }) {
- setState({ registerInfo: data });
- RootNavigation.navigate("RegisterSe");
- },
- registerUser({ ...data }) {
- const { checkLogin } = model();
- const { loading, success } = model("loadingModel");
- const { httpPost, addAsyncStorage } = model("httpModel");
- httpPost("/auth/merchantRegister", data, {}, true).then(res => {
- addAsyncStorage("token", res).then(() => {
- if (res) {
- success("注册成功");
- addAsyncStorage("guideStep", "1").then(() => {
- setState({ initApp: true });
- });
- }
- });
- });
- // setTimeout(() => {
- // success("注册成功");
- // }, 1000);
- // getUserInfo().then(_=>{
- // })
- },
- registerSecend({ ...data }) {
- const { registerInfo, registerUser } = model();
- let _registerInfo = {
- ...registerInfo,
- ...data,
- };
- registerUser(_registerInfo);
- },
- changeGuideStep(step) {
- console.log("a");
- console.log(step);
- const { addAsyncStorage } = model("httpModel");
- addAsyncStorage("guideStep", step.toString()).then(() => {
- setState({ initApp: true, refreashReason: "guideStep" });
- setTimeout(() => {
- setState({ initApp: false });
- }, 500);
- });
- },
- saveMerchant({ ...data }) {
- const { mid, changeGuideStep } = model();
- const { loading, success } = model("loadingModel");
- const { httpPost, addAsyncStorage } = model("httpModel");
- httpPost(
- "/merchant/saveDTO",
- {
- ...data,
- mid: mid,
- },
- { body: "json" }
- ).then(res => {
- changeGuideStep("5");
- });
- },
- checkInfo({ aliAccountEvent, aliNameEvent }) {
- const { aliAccount, aliName } = model();
- aliAccountEvent(aliAccount);
- aliNameEvent(aliName);
- },
- updateMerchant({ ...data }) {
- const { mid, getUserInfo } = model();
- const { httpPost, addAsyncStorage } = model("httpModel");
- return httpPost(
- "/merchant/saveDTO",
- {
- ...data,
- mid: 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 });
- } else if (type == "qualification") {
- return updateMerchant({ qualification: img });
- } else {
- return updateMerchant({ logo: img });
- }
- } else if (type == "qualification") {
- let _registerInfo = { ...registerInfo };
- _registerInfo.qualification = img;
- console.log(_registerInfo);
- setState({
- registerInfo: _registerInfo,
- });
- return Promise.resolve();
- }
- },
- userLogout() {
- const { removeAsyncStorage } = model("httpModel");
- const { success } = model("loadingModel");
- return removeAsyncStorage("token").then(() => {
- setState({
- mid: 0,
- name: "未登录",
- showName: "未登录",
- phone: "",
- isLogin: false,
- guideStep: 0,
- registerInfo: null,
- initApp: true,
- refreashReason: "loginOut",
- });
- 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";
- }
- setState({ guideStep: guideStep, isLogin: true });
- resolve(guideStep || "1");
- });
- }
- });
- },
- closeMer() {
- const { isOpen ,getUserInfo} = model();
- const { httpGet } = model("httpModel");
- const { success } = model("loadingModel");
- let { showDialog } = model("dialogModel");
- let { removeTips, editSuccess } = model("wordsModel");
- if (isOpen) {
- showDialog({
- bodyText: "停止当前营业,直到下次营业开启",
- status: "danger",
- cancelable: true,
- confirmCallback: () => {
- httpGet("/merchant/closeMer", {}, true).then(res => {
- success("操作成功");
- getUserInfo()
- });
- },
- });
- } else {
- httpGet("/merchant/closeMer", {}, true).then(res => {
- success("操作成功");
- getUserInfo()
- });
- }
- },
- }),
- };
|