userModel.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. return getUserInfo();
  124. })
  125. .then(() => {
  126. return saveVeriFied(registerVerifiedInfo);
  127. })
  128. .then(() => {
  129. success("注册成功");
  130. return addAsyncStorage("guideStep", "1");
  131. })
  132. .then(() => {
  133. setState({ initApp: true });
  134. });
  135. // setTimeout(() => {
  136. // success("注册成功");
  137. // }, 1000);
  138. // getUserInfo().then(_=>{
  139. // })
  140. },
  141. registerSecend({ ...data }) {
  142. const { registerInfo, registerUser } = model();
  143. const _registerInfo = {
  144. ...registerInfo,
  145. ...data,
  146. };
  147. registerUser(_registerInfo);
  148. },
  149. changeGuideStep(step) {
  150. const { addAsyncStorage } = model("httpModel");
  151. setState({ guideStep: step });
  152. return addAsyncStorage("guideStep", step.toString());
  153. },
  154. saveMerchant({ ...data }) {
  155. const { mid, changeGuideStep } = model();
  156. const { httpPost } = model("httpModel");
  157. httpPost(
  158. "/merchant/saveDTO",
  159. {
  160. ...data,
  161. mid,
  162. },
  163. { body: "json" }
  164. ).then(() => {
  165. changeGuideStep("5");
  166. });
  167. },
  168. checkInfo({ aliAccountEvent, aliNameEvent }) {
  169. const { aliAccount, aliName } = model();
  170. aliAccountEvent(aliAccount);
  171. aliNameEvent(aliName);
  172. },
  173. updateMerchant({ ...data }) {
  174. const { mid, getUserInfo } = model();
  175. const { httpPost } = model("httpModel");
  176. return httpPost(
  177. "/merchant/saveDTO",
  178. {
  179. ...data,
  180. mid,
  181. },
  182. { body: "json" },
  183. true
  184. ).then(res => {
  185. // success("修改成功");
  186. if (res) {
  187. getUserInfo();
  188. }
  189. });
  190. },
  191. uploadStoreImg(img, type) {
  192. const { updateMerchant, mid, registerInfo } = model();
  193. if (mid !== 0) {
  194. if (type === "banner") {
  195. return updateMerchant({ banner: img });
  196. }
  197. if (type === "qualification") {
  198. return updateMerchant({ qualification: img });
  199. }
  200. return updateMerchant({ logo: img });
  201. }
  202. if (type === "qualification") {
  203. const _registerInfo = { ...registerInfo };
  204. _registerInfo.qualification = img;
  205. console.log(_registerInfo);
  206. setState({
  207. registerInfo: _registerInfo,
  208. });
  209. return Promise.resolve();
  210. }
  211. return Promise.reject();
  212. },
  213. userLogout() {
  214. const { removeAsyncStorage } = model("httpModel");
  215. const { success } = model("loadingModel");
  216. return removeAsyncStorage("token").then(() => {
  217. setState({
  218. mid: 0,
  219. name: "未登录",
  220. showName: "未登录",
  221. phone: "",
  222. isLogin: false,
  223. guideStep: 0,
  224. registerInfo: null,
  225. initApp: true,
  226. refreashReason: "loginOut",
  227. userId: "",
  228. });
  229. success("退出成功");
  230. });
  231. },
  232. getGuideStep() {
  233. const { mid, status } = model();
  234. const { getAsyncStorage } = model("httpModel");
  235. return new Promise(resolve => {
  236. if (mid === 0) {
  237. resolve("");
  238. } else {
  239. getAsyncStorage("guideStep").then(res => {
  240. console.log(res);
  241. let guideStep = res;
  242. if (status === "PASS" && !guideStep) {
  243. guideStep = "5";
  244. } else if (
  245. status === "DENY" &&
  246. (!guideStep || guideStep === "finish")
  247. ) {
  248. guideStep = "5";
  249. }
  250. setState({ guideStep, isLogin: true });
  251. resolve(guideStep || "1");
  252. });
  253. }
  254. });
  255. },
  256. checkNowGuideStep() {
  257. const { status } = model();
  258. const { getAsyncStorage } = model("httpModel");
  259. return getAsyncStorage("guideStep").then(res => {
  260. console.log(res);
  261. let guideStep = res;
  262. if (status === "PASS" && !guideStep) {
  263. guideStep = "5";
  264. } else if (
  265. status === "DENY" &&
  266. (!guideStep || guideStep === "finish")
  267. ) {
  268. guideStep = "5";
  269. }
  270. setState({ guideStep: guideStep || "1" });
  271. return Promise.resolve(guideStep || "1");
  272. });
  273. },
  274. closeMer() {
  275. const { isOpening, getUserInfo } = model();
  276. const { httpGet } = model("httpModel");
  277. const { success } = model("loadingModel");
  278. const { showDialog } = model("dialogModel");
  279. if (isOpening) {
  280. showDialog({
  281. bodyText: "停止当前营业,直到下次营业开启",
  282. status: "danger",
  283. cancelable: true,
  284. confirmCallback: () => {
  285. httpGet("/merchant/closeMer", {}, true).then(() => {
  286. success("操作成功");
  287. getUserInfo();
  288. });
  289. },
  290. });
  291. } else {
  292. httpGet("/merchant/closeMer", {}, true).then(() => {
  293. success("操作成功");
  294. getUserInfo();
  295. });
  296. }
  297. },
  298. setinitRoute(name) {
  299. setState({ initRoute: name });
  300. },
  301. getNowUser() {
  302. const {
  303. mid,
  304. category,
  305. address,
  306. week,
  307. startTime,
  308. endTime,
  309. merchantNatureId,
  310. qualification,
  311. logo,
  312. } = model();
  313. return {
  314. mid,
  315. category,
  316. address,
  317. week,
  318. startTime,
  319. endTime,
  320. merchantNatureId,
  321. qualification,
  322. logo,
  323. };
  324. },
  325. checkAgain({ ...info }) {
  326. const { loading } = model("loadingModel");
  327. const { updateMerchant, changeGuideStep } = model();
  328. loading();
  329. updateMerchant({
  330. ...info,
  331. status: "PENDING",
  332. }).then(() => {
  333. changeGuideStep("5");
  334. });
  335. },
  336. }),
  337. };