userModel.js 5.8 KB

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