//用户状态 (商家状态,全局) export default { state: { mid: 0, name: "未登录", showName: "未登录", phone: "", isLogin: false, guideStep: 0, registerInfo: null, }, actions: ({ model, setState }) => ({ updateUser({ ...userInfo }) { setState({ ...userInfo }); }, decrement() { const { count } = model(); setState({ count: count - 1 }); }, async getUserInfo() { const { updateUser } = model(); const { httpGet } = model("httpModel"); return httpGet("/merchant/my").then((res) => { if (res) { updateUser(res); } }); }, 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("登录成功"); checkLogin(); } }) .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 }); pushRouter("RegisterSe"); }, registerUser({ ...data }) { const { checkLogin } = model(); const { loading, success } = model("loadingModel"); const { httpPost, addAsyncStorage } = model("httpModel"); httpPost("/auth/merchantRegister", data).then((res) => { addAsyncStorage("token", res); if (res) { checkLogin(); } }); // setTimeout(() => { // success("注册成功"); // }, 1000); // getUserInfo().then(_=>{ // }) }, registerSecend({ ...data }) { const { registerInfo, registerUser } = model(); let _registerInfo = { ...registerInfo, ...data, }; registerUser(_registerInfo); }, changeGuideStep(step, next) { const { addAsyncStorage } = model("httpModel"); addAsyncStorage("guideStep", step); if (step == "finish") { setState({ guideStep: "finish", }); } else { replaceRouter(next); } }, 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(3, "Guide4"); }); }, 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" } ).then((res) => { // success("修改成功"); if (res) { getUserInfo(res); } }); }, uploadStoreImg(img, type) { const { updateMerchant } = model(); if (type == "banner") { return updateMerchant({ banner: img }); } else { return updateMerchant({ logo: img }); } }, }), };