userModel.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /* eslint-disable no-console */
  2. /* eslint-disable no-underscore-dangle */
  3. // 用户状态 (商家状态,全局)
  4. import * as RootNavigation from "../navigation/RootNavigation";
  5. export default {
  6. state: {
  7. mid: null,
  8. name: "未登录",
  9. showName: "未登录",
  10. phone: "",
  11. isLogin: false,
  12. guideStep: "0",
  13. registerInfo: {},
  14. initApp: true,
  15. refreashReason: "login",
  16. initRoute: "Login",
  17. userInfo: {},
  18. },
  19. actions: ({ model, setState }) => ({
  20. updateUser(userInfo) {
  21. setState({ ...userInfo });
  22. setState({ userInfo });
  23. },
  24. getUserInfo() {
  25. const { updateUser } = model();
  26. const { httpGet } = model("httpModel");
  27. const { changeVerfied } = model("verifiedModel");
  28. return httpGet("/merchant/my")
  29. .then(res => {
  30. const _res = { ...res };
  31. if (_res) {
  32. updateUser(_res);
  33. changeVerfied(_res.userId);
  34. }
  35. })
  36. .catch(() => {
  37. setState({ mid: 0 });
  38. });
  39. },
  40. changeInIt() {
  41. setState({ initApp: false });
  42. },
  43. checkLogin() {
  44. const { getUserInfo, status } = model();
  45. const { getAsyncStorage } = model("httpModel");
  46. return new Promise(resolve => {
  47. getUserInfo()
  48. .then(async () => {
  49. let guideStep = await getAsyncStorage("guideStep");
  50. if (status === "PASS") {
  51. guideStep = "4";
  52. }
  53. if (guideStep) {
  54. setState({ guideStep, isLogin: true });
  55. } else {
  56. setState({ isLogin: true });
  57. }
  58. resolve();
  59. })
  60. .catch(() => {
  61. setState({ isLogin: false });
  62. resolve();
  63. });
  64. });
  65. },
  66. changeRegisterInfo({ ...info }) {
  67. const { registerInfo } = model();
  68. setState({
  69. registerInfo: {
  70. ...registerInfo,
  71. ...info,
  72. },
  73. });
  74. },
  75. loginByPassword(phone, password) {
  76. const { httpPost, addAsyncStorage } = model("httpModel");
  77. const { success, warnning } = model("loadingModel");
  78. const { getUserInfo } = model();
  79. return httpPost("/auth/login", {
  80. username: phone,
  81. password,
  82. })
  83. .then(res => {
  84. success("登录成功");
  85. return addAsyncStorage("token", res);
  86. })
  87. .then(() => {
  88. return getUserInfo();
  89. })
  90. .catch(e => {
  91. warnning(e.error);
  92. });
  93. },
  94. loginByCode(phone, code) {
  95. console.log(phone, code);
  96. // httpPost("/auth/phoneLogin", {
  97. // phone,
  98. // code,
  99. // })
  100. // .then(res => {
  101. // if (res) {
  102. // updateUser(res);
  103. // }
  104. // })
  105. // .catch(e => {
  106. // console.log(e);
  107. // });
  108. },
  109. registerFirst({ ...data }) {
  110. setState({ registerInfo: data });
  111. RootNavigation.navigate("RegisterSe");
  112. },
  113. registerUser({ ...data }) {
  114. const { getUserInfo } = model();
  115. const { success } = model("loadingModel");
  116. const { httpPost, addAsyncStorage } = model("httpModel");
  117. const { saveVeriFied, registerVerifiedInfo } = model("verifiedModel");
  118. httpPost("/auth/merchantRegister", data, {}, true)
  119. .then(res => {
  120. return addAsyncStorage("token", res);
  121. })
  122. .then(() => {
  123. setState({
  124. guideStep: "1",
  125. });
  126. return addAsyncStorage("guideStep", "1");
  127. })
  128. .then(() => {
  129. return getUserInfo();
  130. })
  131. .then(() => {
  132. return saveVeriFied(registerVerifiedInfo);
  133. })
  134. .then(() => {
  135. success("注册成功");
  136. });
  137. // setTimeout(() => {
  138. // success("注册成功");
  139. // }, 1000);
  140. // getUserInfo().then(_=>{
  141. // })
  142. },
  143. registerSecend({ ...data }) {
  144. const { registerInfo, registerUser } = model();
  145. const _registerInfo = {
  146. ...registerInfo,
  147. ...data,
  148. };
  149. registerUser(_registerInfo);
  150. },
  151. changeGuideStep(step) {
  152. const { addAsyncStorage } = model("httpModel");
  153. setState({ guideStep: step.toString() });
  154. return addAsyncStorage("guideStep", step.toString());
  155. },
  156. saveMerchant({ ...data }) {
  157. const { mid, changeGuideStep } = model();
  158. const { httpPost } = model("httpModel");
  159. httpPost(
  160. "/merchant/saveDTO",
  161. {
  162. ...data,
  163. mid,
  164. },
  165. { body: "json" }
  166. ).then(() => {
  167. changeGuideStep("4");
  168. });
  169. },
  170. checkInfo({ aliAccountEvent, aliNameEvent }) {
  171. const { aliAccount, aliName } = model();
  172. aliAccountEvent(aliAccount);
  173. aliNameEvent(aliName);
  174. },
  175. updateMerchant({ ...data }) {
  176. const { mid, getUserInfo } = model();
  177. const { httpPost } = model("httpModel");
  178. return httpPost(
  179. "/merchant/saveDTO",
  180. {
  181. ...data,
  182. mid,
  183. },
  184. { body: "json" },
  185. true
  186. ).then(res => {
  187. // success("修改成功");
  188. if (res) {
  189. getUserInfo();
  190. }
  191. });
  192. },
  193. uploadStoreImg(img, type) {
  194. const { updateMerchant, mid, registerInfo } = model();
  195. if (mid !== 0) {
  196. if (type === "banner") {
  197. return updateMerchant({ banner: img });
  198. }
  199. if (type === "qualification") {
  200. return updateMerchant({ qualification: img });
  201. }
  202. return updateMerchant({ logo: img });
  203. }
  204. if (type === "qualification") {
  205. const _registerInfo = { ...registerInfo };
  206. _registerInfo.qualification = img;
  207. console.log(_registerInfo);
  208. setState({
  209. registerInfo: _registerInfo,
  210. });
  211. return Promise.resolve();
  212. }
  213. return Promise.reject();
  214. },
  215. userLogout() {
  216. const { removeAsyncStorage } = model("httpModel");
  217. const { success } = model("loadingModel");
  218. return removeAsyncStorage("token").then(() => {
  219. setState({
  220. mid: 0,
  221. name: "未登录",
  222. showName: "未登录",
  223. phone: "",
  224. isLogin: false,
  225. guideStep: "0",
  226. registerInfo: null,
  227. initApp: true,
  228. refreashReason: "loginOut",
  229. userId: "",
  230. });
  231. success("退出成功");
  232. });
  233. },
  234. getGuideStep() {
  235. const { mid, status } = model();
  236. const { getAsyncStorage } = model("httpModel");
  237. return new Promise(resolve => {
  238. if (mid === 0) {
  239. resolve("");
  240. } else {
  241. getAsyncStorage("guideStep").then(res => {
  242. console.log(res);
  243. let guideStep = res;
  244. if (status === "PASS" && !guideStep) {
  245. guideStep = "5";
  246. } else if (
  247. status === "DENY" &&
  248. (!guideStep || guideStep === "finish")
  249. ) {
  250. guideStep = "5";
  251. }
  252. setState({ guideStep, isLogin: true });
  253. resolve(guideStep || "1");
  254. });
  255. }
  256. });
  257. },
  258. checkNowGuideStep() {
  259. const { status } = model();
  260. const { getAsyncStorage } = model("httpModel");
  261. return getAsyncStorage("guideStep").then(res => {
  262. console.log(res);
  263. let guideStep = res;
  264. if (status === "PASS" && !guideStep) {
  265. guideStep = "5";
  266. } else if (
  267. status === "DENY" &&
  268. (!guideStep || guideStep === "finish")
  269. ) {
  270. guideStep = "5";
  271. } else if (status === "PENDING") {
  272. guideStep = "5";
  273. }
  274. setState({ guideStep: guideStep || "1" });
  275. return Promise.resolve(guideStep || "1");
  276. });
  277. },
  278. closeMer() {
  279. const { isOpening, getUserInfo } = model();
  280. const { httpGet } = model("httpModel");
  281. const { success } = model("loadingModel");
  282. const { showDialog } = model("dialogModel");
  283. if (isOpening) {
  284. showDialog({
  285. bodyText: "停止当前营业,直到下次营业开启",
  286. status: "danger",
  287. cancelable: true,
  288. confirmCallback: () => {
  289. httpGet("/merchant/closeMer", {}, true).then(() => {
  290. success("操作成功");
  291. getUserInfo();
  292. });
  293. },
  294. });
  295. } else {
  296. httpGet("/merchant/closeMer", {}, true).then(() => {
  297. success("操作成功");
  298. getUserInfo();
  299. });
  300. }
  301. },
  302. setinitRoute(name) {
  303. setState({ initRoute: name });
  304. },
  305. getNowUser() {
  306. const {
  307. mid,
  308. category,
  309. address,
  310. week,
  311. startTime,
  312. endTime,
  313. merchantNatureId,
  314. qualification,
  315. logo,
  316. } = model();
  317. return {
  318. mid,
  319. category,
  320. address,
  321. week,
  322. startTime,
  323. endTime,
  324. merchantNatureId,
  325. qualification,
  326. logo,
  327. };
  328. },
  329. checkAgain({ ...info }) {
  330. const { loading } = model("loadingModel");
  331. const { updateMerchant, changeGuideStep } = model();
  332. loading();
  333. updateMerchant({
  334. ...info,
  335. status: "PENDING",
  336. }).then(() => {
  337. changeGuideStep("5");
  338. });
  339. },
  340. }),
  341. };