userModel.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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, status } = 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. if (guideStep) {
  39. setState({ guideStep: guideStep, isLogin: true });
  40. } else {
  41. setState({ isLogin: true });
  42. }
  43. const { isLogin } = model();
  44. console.log(isLogin);
  45. resolve();
  46. })
  47. .catch((_) => {
  48. setState({ isLogin: false });
  49. resolve();
  50. });
  51. });
  52. },
  53. loginByPassword(phone, password) {
  54. const { httpPost, addAsyncStorage } = model("httpModel");
  55. const { checkLogin } = model();
  56. const { loading, success, warnning } = model("loadingModel");
  57. return httpPost("/auth/login", {
  58. username: phone,
  59. password: password,
  60. })
  61. .then((res) => {
  62. if (res) {
  63. addAsyncStorage("token", res);
  64. success("登录成功");
  65. checkLogin();
  66. }
  67. })
  68. .catch((e) => {
  69. warnning(e.error);
  70. });
  71. },
  72. loginByCode(phone, code) {
  73. loading();
  74. httpPost("/auth/phoneLogin", {
  75. phone: phone,
  76. code: code,
  77. })
  78. .then((res) => {
  79. if (res) {
  80. updateUser(res);
  81. }
  82. })
  83. .catch((e) => {
  84. console.log(e);
  85. });
  86. },
  87. registerFirst({ ...data }) {
  88. setState({ registerInfo: data });
  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. replaceRouter(next);
  124. }
  125. },
  126. saveMerchant({ ...data }) {
  127. const { mid, changeGuideStep } = model();
  128. const { loading, success } = model("loadingModel");
  129. const { httpPost, addAsyncStorage } = model("httpModel");
  130. httpPost(
  131. "/merchant/saveDTO",
  132. {
  133. ...data,
  134. mid: mid,
  135. },
  136. { body: "json" }
  137. ).then((res) => {
  138. changeGuideStep(3, "Guide4");
  139. });
  140. },
  141. checkInfo({ aliAccountEvent, aliNameEvent }) {
  142. const { aliAccount, aliName } = model();
  143. aliAccountEvent(aliAccount);
  144. aliNameEvent(aliName);
  145. },
  146. updateMerchant({ ...data }) {
  147. const { mid, getUserInfo } = model();
  148. const { httpPost, addAsyncStorage } = model("httpModel");
  149. return httpPost(
  150. "/merchant/saveDTO",
  151. {
  152. ...data,
  153. mid: mid,
  154. },
  155. { body: "json" }
  156. ).then((res) => {
  157. // success("修改成功");
  158. if (res) {
  159. getUserInfo(res);
  160. }
  161. });
  162. },
  163. uploadStoreImg(img, type) {
  164. const { updateMerchant } = model();
  165. if (type == "banner") {
  166. return updateMerchant({ banner: img });
  167. } else {
  168. return updateMerchant({ logo: img });
  169. }
  170. },
  171. }),
  172. };