userModel.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. setState({ initApp: false });
  26. const { updateUser } = model();
  27. const { httpGet } = model("httpModel");
  28. const { changeVerfied } = model("verifiedModel");
  29. return httpGet("/merchant/my")
  30. .then(res => {
  31. const _res = { ...res };
  32. if (_res) {
  33. updateUser(_res);
  34. changeVerfied(_res.userId);
  35. }
  36. })
  37. .catch(() => {
  38. setState({ mid: 0 });
  39. });
  40. },
  41. changeInIt() {
  42. setState({ initApp: false });
  43. },
  44. checkLogin() {
  45. const { getUserInfo, status } = model();
  46. const { getAsyncStorage } = model("httpModel");
  47. return new Promise(resolve => {
  48. getUserInfo()
  49. .then(async () => {
  50. let guideStep = await getAsyncStorage("guideStep");
  51. if (status === "PASS") {
  52. guideStep = "4";
  53. }
  54. if (guideStep) {
  55. setState({ guideStep, isLogin: true });
  56. } else {
  57. setState({ isLogin: true });
  58. }
  59. resolve();
  60. })
  61. .catch(() => {
  62. setState({ isLogin: false });
  63. resolve();
  64. });
  65. });
  66. },
  67. changeRegisterInfo({ ...info }) {
  68. const { registerInfo } = model();
  69. setState({
  70. registerInfo: {
  71. ...registerInfo,
  72. ...info,
  73. },
  74. });
  75. },
  76. loginByPassword(phone, password) {
  77. const { httpPost, addAsyncStorage } = model("httpModel");
  78. const { success, warnning } = model("loadingModel");
  79. return httpPost("/auth/login", {
  80. username: phone,
  81. password,
  82. })
  83. .then(res => {
  84. if (res) {
  85. addAsyncStorage("token", res);
  86. success("登录成功");
  87. setState({ initApp: true });
  88. }
  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. }
  272. setState({ guideStep: guideStep || "1" });
  273. return Promise.resolve(guideStep || "1");
  274. });
  275. },
  276. closeMer() {
  277. const { isOpening, getUserInfo } = model();
  278. const { httpGet } = model("httpModel");
  279. const { success } = model("loadingModel");
  280. const { showDialog } = model("dialogModel");
  281. if (isOpening) {
  282. showDialog({
  283. bodyText: "停止当前营业,直到下次营业开启",
  284. status: "danger",
  285. cancelable: true,
  286. confirmCallback: () => {
  287. httpGet("/merchant/closeMer", {}, true).then(() => {
  288. success("操作成功");
  289. getUserInfo();
  290. });
  291. },
  292. });
  293. } else {
  294. httpGet("/merchant/closeMer", {}, true).then(() => {
  295. success("操作成功");
  296. getUserInfo();
  297. });
  298. }
  299. },
  300. setinitRoute(name) {
  301. setState({ initRoute: name });
  302. },
  303. getNowUser() {
  304. const {
  305. mid,
  306. category,
  307. address,
  308. week,
  309. startTime,
  310. endTime,
  311. merchantNatureId,
  312. qualification,
  313. logo,
  314. } = model();
  315. return {
  316. mid,
  317. category,
  318. address,
  319. week,
  320. startTime,
  321. endTime,
  322. merchantNatureId,
  323. qualification,
  324. logo,
  325. };
  326. },
  327. checkAgain({ ...info }) {
  328. const { loading } = model("loadingModel");
  329. const { updateMerchant, changeGuideStep } = model();
  330. loading();
  331. updateMerchant({
  332. ...info,
  333. status: "PENDING",
  334. }).then(() => {
  335. changeGuideStep("5");
  336. });
  337. },
  338. }),
  339. };