userModel.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. const { updateUser } = model();
  26. const { httpGet } = model("httpModel");
  27. const { changeVerfied } = model("verifiedModel");
  28. return httpGet("/merchant/my", {}, true)
  29. .then(res => {
  30. const _res = { ...res };
  31. if (_res) {
  32. updateUser(_res);
  33. changeVerfied(_res.userId);
  34. }
  35. })
  36. .catch(() => {
  37. setState({ mid: 0 });
  38. });
  39. },
  40. changeInIt() {
  41. setState({ initApp: false });
  42. },
  43. checkLogin() {
  44. const { getUserInfo, status } = model();
  45. const { getAsyncStorage } = model("httpModel");
  46. return new Promise(resolve => {
  47. getUserInfo()
  48. .then(async () => {
  49. let guideStep = await getAsyncStorage("guideStep");
  50. if (status === "PASS") {
  51. guideStep = "4";
  52. }
  53. if (guideStep) {
  54. setState({ guideStep, isLogin: true });
  55. } else {
  56. setState({ isLogin: true });
  57. }
  58. resolve();
  59. })
  60. .catch(() => {
  61. setState({ isLogin: false });
  62. resolve();
  63. });
  64. });
  65. },
  66. changeRegisterInfo({ ...info }) {
  67. const { registerInfo } = model();
  68. setState({
  69. registerInfo: {
  70. ...registerInfo,
  71. ...info,
  72. },
  73. });
  74. },
  75. loginByPassword(phone, password) {
  76. const { httpPost, addAsyncStorage } = model("httpModel");
  77. const { success, warnning } = model("loadingModel");
  78. const { getUserInfo } = model();
  79. return httpPost("/auth/login", {
  80. username: phone,
  81. password,
  82. })
  83. .then(res => {
  84. success("登录成功");
  85. return addAsyncStorage("token", res);
  86. })
  87. .then(() => {
  88. return getUserInfo();
  89. })
  90. .catch(e => {
  91. warnning(e.error);
  92. });
  93. },
  94. loginByCode(phone, code) {
  95. console.log(phone, code);
  96. const { httpPost, addAsyncStorage } = model("httpModel");
  97. const { success, warnning } = model("loadingModel");
  98. const { getUserInfo } = model();
  99. httpPost("/auth/phoneLogin", {
  100. phone: `+86${phone}`,
  101. code,
  102. identity: "MERCHANT",
  103. })
  104. .then(res => {
  105. success("登录成功");
  106. return addAsyncStorage("token", res);
  107. })
  108. .then(() => {
  109. return getUserInfo();
  110. })
  111. .catch(e => {
  112. warnning(e.error);
  113. });
  114. },
  115. saveLocation(address, latitude, longitude) {
  116. const { mid, registerInfo, updateMerchant } = model();
  117. if (mid) {
  118. const { loading, success } = model("loadingModel");
  119. loading();
  120. return updateMerchant({
  121. address,
  122. latitude,
  123. longitude,
  124. }).then(() => {
  125. success("设置成功");
  126. });
  127. }
  128. setState({
  129. registerInfo: { ...registerInfo, address, latitude, longitude },
  130. });
  131. return Promise.resolve();
  132. },
  133. registerFirst({ ...data }) {
  134. setState({ registerInfo: data });
  135. RootNavigation.navigate("RegisterSe");
  136. },
  137. registerUser({ ...data }) {
  138. const { getUserInfo } = model();
  139. const { success } = model("loadingModel");
  140. const { httpPost, addAsyncStorage } = model("httpModel");
  141. const { saveVeriFied, registerVerifiedInfo } = model("verifiedModel");
  142. httpPost("/auth/merchantRegister", data, {}, true)
  143. .then(res => {
  144. return addAsyncStorage("token", res);
  145. })
  146. .then(() => {
  147. setState({
  148. guideStep: "1",
  149. });
  150. return addAsyncStorage("guideStep", "1");
  151. })
  152. .then(() => {
  153. return getUserInfo();
  154. })
  155. .then(() => {
  156. return saveVeriFied(registerVerifiedInfo);
  157. })
  158. .then(() => {
  159. success("注册成功");
  160. });
  161. // setTimeout(() => {
  162. // success("注册成功");
  163. // }, 1000);
  164. // getUserInfo().then(_=>{
  165. // })
  166. },
  167. registerSecend({ ...data }) {
  168. const { registerInfo, registerUser } = model();
  169. const _registerInfo = {
  170. ...registerInfo,
  171. ...data,
  172. };
  173. registerUser(_registerInfo);
  174. },
  175. changeGuideStep(step) {
  176. const { addAsyncStorage } = model("httpModel");
  177. setState({ guideStep: step.toString() });
  178. return addAsyncStorage("guideStep", step.toString());
  179. },
  180. saveMerchant({ ...data }) {
  181. const { mid, changeGuideStep } = model();
  182. const { httpPost } = model("httpModel");
  183. httpPost(
  184. "/merchant/saveDTO",
  185. {
  186. ...data,
  187. mid,
  188. },
  189. { body: "json" }
  190. ).then(() => {
  191. changeGuideStep("4");
  192. });
  193. },
  194. checkInfo({ aliAccountEvent, aliNameEvent }) {
  195. const { aliAccount, aliName } = model();
  196. aliAccountEvent(aliAccount);
  197. aliNameEvent(aliName);
  198. },
  199. updateMerchant({ ...data }) {
  200. const { mid, userInfo } = model();
  201. const { httpPost } = model("httpModel");
  202. return httpPost(
  203. "/merchant/saveDTO",
  204. {
  205. ...data,
  206. mid,
  207. },
  208. { body: "json" },
  209. true
  210. ).then(res => {
  211. // success("修改成功");
  212. if (res) {
  213. // setState({
  214. // userInfo: {
  215. // ...userInfo,
  216. // ...data,
  217. // },
  218. // ...data,
  219. // });
  220. }
  221. });
  222. },
  223. uploadStoreImg(img, type) {
  224. const { updateMerchant, mid, registerInfo } = model();
  225. if (mid !== 0) {
  226. if (type === "banner") {
  227. return updateMerchant({ banner: img });
  228. }
  229. if (type === "qualification") {
  230. return updateMerchant({ qualification: img });
  231. }
  232. return updateMerchant({ logo: img });
  233. }
  234. if (type === "qualification") {
  235. const _registerInfo = { ...registerInfo };
  236. _registerInfo.qualification = img;
  237. console.log(_registerInfo);
  238. setState({
  239. registerInfo: _registerInfo,
  240. });
  241. return Promise.resolve();
  242. }
  243. return Promise.reject();
  244. },
  245. userLogout() {
  246. const { removeAsyncStorage } = model("httpModel");
  247. const { success } = model("loadingModel");
  248. const { saveVeriFied } = model("verifiedModel");
  249. return removeAsyncStorage("token")
  250. .then(() => {
  251. setState({
  252. mid: 0,
  253. name: "未登录",
  254. showName: "未登录",
  255. phone: "",
  256. isLogin: false,
  257. guideStep: "0",
  258. registerInfo: null,
  259. initApp: true,
  260. refreashReason: "loginOut",
  261. userId: "",
  262. });
  263. // 清空认证信息
  264. return saveVeriFied({});
  265. })
  266. .then(() => {
  267. success("退出成功");
  268. });
  269. },
  270. getGuideStep() {
  271. const { mid, status } = model();
  272. const { getAsyncStorage } = model("httpModel");
  273. return new Promise(resolve => {
  274. if (mid === 0) {
  275. resolve("");
  276. } else {
  277. getAsyncStorage("guideStep").then(res => {
  278. console.log(res);
  279. let guideStep = res;
  280. if (status === "PASS" && !guideStep) {
  281. guideStep = "5";
  282. } else if (
  283. status === "DENY" &&
  284. (!guideStep || guideStep === "finish")
  285. ) {
  286. guideStep = "5";
  287. }
  288. setState({ guideStep, isLogin: true });
  289. resolve(guideStep || "1");
  290. });
  291. }
  292. });
  293. },
  294. checkNowGuideStep() {
  295. const { status } = model();
  296. const { getAsyncStorage } = model("httpModel");
  297. return getAsyncStorage("guideStep").then(res => {
  298. console.log(res);
  299. let guideStep = res;
  300. if (status === "PASS" && !guideStep) {
  301. guideStep = "5";
  302. } else if (
  303. status === "DENY" &&
  304. (!guideStep || guideStep === "finish")
  305. ) {
  306. guideStep = "5";
  307. } else if (status === "PENDING" && !guideStep) {
  308. guideStep = "5";
  309. }
  310. setState({ guideStep: guideStep || "1" });
  311. return Promise.resolve(guideStep || "1");
  312. });
  313. },
  314. closeMer() {
  315. const { isOpening, getUserInfo } = model();
  316. const { httpGet } = model("httpModel");
  317. const { success } = model("loadingModel");
  318. const { showDialog } = model("dialogModel");
  319. if (isOpening) {
  320. showDialog({
  321. bodyText: "停止当前营业,直到下次营业开启",
  322. status: "danger",
  323. cancelable: true,
  324. confirmCallback: () => {
  325. httpGet("/merchant/closeMer", {}, true).then(() => {
  326. success("操作成功");
  327. getUserInfo();
  328. });
  329. },
  330. });
  331. } else {
  332. httpGet("/merchant/closeMer", {}, true).then(() => {
  333. success("操作成功");
  334. getUserInfo();
  335. });
  336. }
  337. },
  338. setinitRoute(name) {
  339. setState({ initRoute: name });
  340. },
  341. getNowUser() {
  342. const {
  343. mid,
  344. category,
  345. address,
  346. week,
  347. startTime,
  348. endTime,
  349. merchantNatureId,
  350. qualification,
  351. logo,
  352. } = model();
  353. return {
  354. mid,
  355. category,
  356. address,
  357. week,
  358. startTime,
  359. endTime,
  360. merchantNatureId,
  361. qualification,
  362. logo,
  363. };
  364. },
  365. checkAgain({ ...info }) {
  366. const { loading } = model("loadingModel");
  367. const { updateMerchant, changeGuideStep } = model();
  368. loading();
  369. updateMerchant({
  370. ...info,
  371. status: "PENDING",
  372. }).then(() => {
  373. changeGuideStep("5");
  374. });
  375. },
  376. }),
  377. };