userModel.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. export default {
  2. state: {
  3. mid: 0,
  4. name: "未登录",
  5. showName: "未登录",
  6. phone: "",
  7. isLogin: false,
  8. guideStep: 0,
  9. registerInfo: null,
  10. },
  11. actions: ({ model, setState }) => ({
  12. updateUser({ ...userInfo }) {
  13. setState({ ...userInfo });
  14. },
  15. decrement() {
  16. const { count } = model();
  17. setState({ count: count - 1 });
  18. },
  19. getUserInfo() {
  20. const { updateUser } = model();
  21. const { httpGet } = model("httpModel");
  22. return httpGet("/merchant/my").then((res) => {
  23. if (res) {
  24. updateUser(res);
  25. }
  26. });
  27. },
  28. checkLogin() {
  29. const { getUserInfo, id, aliAccount } = model();
  30. const { getAsyncStorage } = model("httpModel");
  31. return new Promise((resolve) => {
  32. getUserInfo()
  33. .then(async (_) => {
  34. let guideStep = await getAsyncStorage("guideStep");
  35. if (status == "PASS") {
  36. guideStep = 4;
  37. }
  38. console.log(guideStep);
  39. if (guideStep) {
  40. setState({ guideStep: guideStep, isLogin: true });
  41. } else {
  42. setState({ isLogin: true });
  43. }
  44. resolve();
  45. })
  46. .catch((_) => {
  47. setState({ isLogin: false });
  48. resolve();
  49. });
  50. });
  51. },
  52. loginByPassword(phone, password) {
  53. const { httpPost, addAsyncStorage } = model("httpModel");
  54. const { checkLogin } = model();
  55. const { loading, success } = model("loadingModel");
  56. httpPost("/auth/loginByRegister", {
  57. phone: phone,
  58. password: password,
  59. })
  60. .then((res) => {
  61. addAsyncStorage("token", res);
  62. if (res) {
  63. success("登录成功");
  64. checkLogin();
  65. }
  66. })
  67. .catch((e) => {
  68. console.log(e);
  69. });
  70. },
  71. loginByCode(phone, code) {
  72. loading();
  73. httpPost("/auth/phoneLogin", {
  74. phone: phone,
  75. code: code,
  76. })
  77. .then((res) => {
  78. if (res) {
  79. updateUser(res);
  80. }
  81. })
  82. .catch((e) => {
  83. console.log(e);
  84. });
  85. },
  86. registerFirst({ ...data }) {
  87. setState({ registerInfo: data });
  88. const { pushRouter } = model("routersModel");
  89. pushRouter("RegisterSe");
  90. },
  91. registerUser({ ...data }) {
  92. const { checkLogin } = model();
  93. const { loading, success } = model("loadingModel");
  94. const { httpPost, addAsyncStorage } = model("httpModel");
  95. httpPost("/auth/merchantRegister", data).then((res) => {
  96. addAsyncStorage("token", res);
  97. if (res) {
  98. checkLogin();
  99. }
  100. });
  101. // setTimeout(() => {
  102. // success("注册成功");
  103. // }, 1000);
  104. // getUserInfo().then(_=>{
  105. // })
  106. },
  107. registerSecend({ ...data }) {
  108. const { registerInfo, registerUser } = model();
  109. let _registerInfo = {
  110. ...registerInfo,
  111. ...data,
  112. };
  113. registerUser(_registerInfo);
  114. },
  115. changeGuideStep(step, next) {
  116. const { addAsyncStorage } = model("httpModel");
  117. addAsyncStorage("guideStep", step);
  118. if (step == "finish") {
  119. setState({
  120. guideStep: "finish",
  121. });
  122. } else {
  123. const { replaceRouter, resetRouter } = model("routersModel");
  124. replaceRouter(next);
  125. }
  126. },
  127. saveMerchant({ ...data }) {
  128. const { mid, changeGuideStep } = model();
  129. const { loading, success } = model("loadingModel");
  130. const { httpPost, addAsyncStorage } = model("httpModel");
  131. httpPost(
  132. "/merchant/saveDTO",
  133. {
  134. ...data,
  135. mid: mid,
  136. },
  137. { body: "json" }
  138. ).then((res) => {
  139. changeGuideStep(3, "Guide4");
  140. });
  141. },
  142. checkInfo({ aliAccountEvent, aliNameEvent }) {
  143. const { aliAccount, aliName } = model();
  144. aliAccountEvent(aliAccount);
  145. aliNameEvent(aliName);
  146. },
  147. updateMerchant({ ...data }) {
  148. const { mid, getUserInfo } = model();
  149. const { loading, success } = model("loadingModel");
  150. const { httpPost, addAsyncStorage } = model("httpModel");
  151. return httpPost(
  152. "/merchant/saveDTO",
  153. {
  154. ...data,
  155. mid: mid,
  156. },
  157. { body: "json" }
  158. ).then((res) => {
  159. success("修改成功");
  160. if (res) {
  161. getUserInfo(res);
  162. }
  163. });
  164. },
  165. uploadStoreImg(img, type) {
  166. const { updateMerchant } = model();
  167. if (type == "banner") {
  168. return updateMerchant({ banner: img });
  169. } else {
  170. return updateMerchant({ logo: img });
  171. }
  172. },
  173. }),
  174. };