userModel.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //用户状态 (商家状态,全局)
  2. import * as RootNavigation from "../navigation/RootNavigation.js";
  3. export default {
  4. state: {
  5. mid: 0,
  6. name: "未登录",
  7. showName: "未登录",
  8. phone: "",
  9. isLogin: false,
  10. guideStep: 0,
  11. registerInfo: null,
  12. initApp: true,
  13. refreashReason: "login",
  14. },
  15. actions: ({ model, setState }) => ({
  16. updateUser(userInfo) {
  17. setState({ ...userInfo });
  18. },
  19. getUserInfo() {
  20. setState({ initApp: false });
  21. const { updateUser } = model();
  22. const { httpGet } = model("httpModel");
  23. return httpGet("/merchant/my").then(res => {
  24. let _res = { ...res };
  25. if (_res) {
  26. updateUser(_res);
  27. }
  28. });
  29. },
  30. checkLogin() {
  31. const { getUserInfo, status } = model();
  32. const { getAsyncStorage } = model("httpModel");
  33. return new Promise(resolve => {
  34. getUserInfo()
  35. .then(async _ => {
  36. let guideStep = await getAsyncStorage("guideStep");
  37. if (status == "PASS") {
  38. guideStep = 4;
  39. }
  40. if (guideStep) {
  41. setState({ guideStep: guideStep, isLogin: true });
  42. } else {
  43. setState({ isLogin: true });
  44. }
  45. const { isLogin } = model();
  46. console.log(isLogin);
  47. resolve();
  48. })
  49. .catch(_ => {
  50. setState({ isLogin: false });
  51. resolve();
  52. });
  53. });
  54. },
  55. loginByPassword(phone, password) {
  56. const { httpPost, addAsyncStorage } = model("httpModel");
  57. const { checkLogin } = model();
  58. const { loading, success, warnning } = model("loadingModel");
  59. return httpPost("/auth/login", {
  60. username: phone,
  61. password: password,
  62. })
  63. .then(res => {
  64. if (res) {
  65. addAsyncStorage("token", res);
  66. success("登录成功");
  67. setState({ initApp: true });
  68. }
  69. })
  70. .catch(e => {
  71. warnning(e.error);
  72. });
  73. },
  74. loginByCode(phone, code) {
  75. loading();
  76. httpPost("/auth/phoneLogin", {
  77. phone: phone,
  78. code: code,
  79. })
  80. .then(res => {
  81. if (res) {
  82. updateUser(res);
  83. }
  84. })
  85. .catch(e => {
  86. console.log(e);
  87. });
  88. },
  89. registerFirst({ ...data }) {
  90. setState({ registerInfo: data });
  91. RootNavigation.navigate("RegisterSe");
  92. },
  93. registerUser({ ...data }) {
  94. const { checkLogin } = model();
  95. const { loading, success } = model("loadingModel");
  96. const { httpPost, addAsyncStorage } = model("httpModel");
  97. httpPost("/auth/merchantRegister", data, {}, true).then(res => {
  98. addAsyncStorage("token", res).then(() => {
  99. if (res) {
  100. success("注册成功");
  101. addAsyncStorage("guideStep", 1).then(() => {
  102. setState({ initApp: true });
  103. });
  104. }
  105. });
  106. });
  107. // setTimeout(() => {
  108. // success("注册成功");
  109. // }, 1000);
  110. // getUserInfo().then(_=>{
  111. // })
  112. },
  113. registerSecend({ ...data }) {
  114. const { registerInfo, registerUser } = model();
  115. let _registerInfo = {
  116. ...registerInfo,
  117. ...data,
  118. };
  119. registerUser(_registerInfo);
  120. },
  121. changeGuideStep(step) {
  122. const { addAsyncStorage } = model("httpModel");
  123. addAsyncStorage("guideStep", step.toString()).then(() => {
  124. setState({ initApp: true, refreashReason: "guideStep" });
  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(4);
  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. true
  158. ).then(res => {
  159. // success("修改成功");
  160. if (res) {
  161. getUserInfo();
  162. }
  163. });
  164. },
  165. uploadStoreImg(img, type) {
  166. const { updateMerchant, mid, registerInfo } = model();
  167. if (mid !== 0) {
  168. if (type == "banner") {
  169. return updateMerchant({ banner: img });
  170. } else if (type == "qualification") {
  171. return updateMerchant({ qualification: img });
  172. } else {
  173. return updateMerchant({ logo: img });
  174. }
  175. } else if (type == "qualification") {
  176. let _registerInfo = { ...registerInfo };
  177. _registerInfo.qualification = img;
  178. console.log(_registerInfo);
  179. setState({
  180. registerInfo: _registerInfo,
  181. });
  182. return Promise.resolve();
  183. }
  184. },
  185. userLogout() {
  186. const { removeAsyncStorage } = model("httpModel");
  187. const { success } = model("loadingModel");
  188. return removeAsyncStorage("token").then(() => {
  189. setState({
  190. mid: 0,
  191. name: "未登录",
  192. showName: "未登录",
  193. phone: "",
  194. isLogin: false,
  195. guideStep: 0,
  196. registerInfo: null,
  197. initApp: true,
  198. refreashReason: "loginOut",
  199. });
  200. success("退出成功");
  201. });
  202. },
  203. getGuideStep() {
  204. const { mid, status } = model();
  205. const { getAsyncStorage } = model("httpModel");
  206. return new Promise(resolve => {
  207. if (mid == 0) {
  208. resolve("");
  209. } else {
  210. getAsyncStorage("guideStep").then(res => {
  211. console.log(res);
  212. let guideStep = res;
  213. if (status == "PASS" && !guideStep) {
  214. guideStep = "5";
  215. }
  216. setState({ guideStep: guideStep, isLogin: true });
  217. resolve(guideStep || "1");
  218. });
  219. }
  220. });
  221. },
  222. }),
  223. };