userModel.js 11 KB

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