user.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import BaseStore from '@src/stores/base';
  2. import * as querystring from 'querystring';
  3. import { getMap } from '@src/services/Tools';
  4. // import * as querystring from 'querystring';
  5. import { ServiceParamMap, OrderInfoMap, ServiceKey } from '../../Constant';
  6. const ServiceParamRelation = getMap(Object.keys(ServiceParamMap).map(key => {
  7. return {
  8. map: getMap(ServiceParamMap[key].map((row, index) => {
  9. row.index = index;
  10. return row;
  11. }), 'value', 'index'),
  12. key,
  13. };
  14. }), 'key', 'map');
  15. function formatTitle(record) {
  16. if (record.productType === 'course_package') {
  17. return (record.coursePackage || {}).title;
  18. }
  19. if (record.productType === 'course') {
  20. record.minNumber = record.course.minNumber;
  21. record.maxNumber = record.course.maxNumber;
  22. return (record.course || {}).title;
  23. }
  24. if (record.productType === 'data') {
  25. return (record.data || {}).title;
  26. }
  27. if (record.productType === 'service') {
  28. return record.info.label || ((record.serviceInfo || {}).title);
  29. }
  30. return '';
  31. }
  32. function formatGift(record) {
  33. let gift = null;
  34. if (record.productType === 'course_package') {
  35. ({ gift } = record.coursePackage || {});
  36. }
  37. if (!gift) return null;
  38. return ServiceKey.map(row => {
  39. if (!gift[row.value]) return null;
  40. const list = ServiceParamMap[row.value];
  41. if (list) {
  42. const map = getMap(list, 'value');
  43. return Object.assign({ param: map[gift[row.value]] }, map[gift[row.value]], row);
  44. }
  45. return Object.assign({ number: gift[row.value] }, row);
  46. }).filter(row => row);
  47. }
  48. function formatCheckout(checkouts) {
  49. checkouts.forEach(checkout => {
  50. checkout.key = checkout.id;
  51. checkout.info = OrderInfoMap[checkout.productType];
  52. if (checkout.productType === 'service') {
  53. const index = (ServiceParamRelation[checkout.service] && ServiceParamRelation[checkout.service][checkout.param]) || 0;
  54. checkout.info = Object.assign({}, checkout.info[checkout.service], checkout.serviceInfo.package[index]);
  55. }
  56. checkout.title = formatTitle(checkout);
  57. checkout.gift = formatGift(checkout);
  58. if (checkout.children) formatCheckout(checkout.children);
  59. });
  60. }
  61. export default class UserStore extends BaseStore {
  62. constructor(props) {
  63. super(props);
  64. this.adminLogin = null;
  65. const { token } = querystring.parse(window.location.search.replace('?', ''));
  66. if (token) {
  67. this.adminLogin = token;
  68. }
  69. }
  70. initState() {
  71. if (this.adminLogin) this.setToken(this.adminLogin);
  72. return { login: !!this.adminLogin };
  73. }
  74. initAfter() {
  75. this.refreshToken().then(() => {
  76. if (this.adminLogin) {
  77. window.location.href = window.location.href.replace(`token=${this.adminLogin}`, '').replace('&&', '&');
  78. }
  79. });
  80. }
  81. needPay(order) {
  82. return new Promise((resolve, reject) => {
  83. this.successCB = resolve;
  84. this.failCB = reject;
  85. formatCheckout(order.checkouts);
  86. this.setState({ needPay: true, order });
  87. });
  88. }
  89. formatCheckout(record) {
  90. formatCheckout(record);
  91. }
  92. formatOrder(order) {
  93. formatCheckout(order.checkouts);
  94. }
  95. closePay(err) {
  96. this.setState({ needPay: false });
  97. if (err) {
  98. if (this.failCB) this.failCB();
  99. } else if (this.successCB) this.successCB();
  100. this.successCB = null;
  101. this.failCB = null;
  102. }
  103. needLogin() {
  104. if (this.state.login) {
  105. return Promise.resolve();
  106. }
  107. return new Promise((resolve, reject) => {
  108. this.successCB = resolve;
  109. this.failCB = reject;
  110. this.setState({ needLogin: true });
  111. });
  112. }
  113. closeLogin(err) {
  114. this.setState({ needLogin: false });
  115. if (err) {
  116. if (this.failCB) this.failCB();
  117. } else if (this.successCB) this.successCB();
  118. this.successCB = null;
  119. this.failCB = null;
  120. }
  121. /**
  122. * 验证token
  123. */
  124. refreshToken() {
  125. return this.apiPost('/auth/token')
  126. .then(result => {
  127. this.infoHandle(result);
  128. })
  129. .catch(() => {
  130. this.logout(false);
  131. });
  132. }
  133. infoHandle(result, auto = true) {
  134. if (result.token) this.setToken(result.token);
  135. this.setState({ login: result.id, needLogin: !auto, info: result, username: result.username });
  136. }
  137. originInviteCode(inviteCode) {
  138. this.setState({
  139. inviteCode,
  140. });
  141. }
  142. addSearch(keyword) {
  143. const { searchHistoryList = [] } = this.state;
  144. searchHistoryList.unshift(keyword);
  145. searchHistoryList.splice(5);
  146. this.setState({ searchHistoryList });
  147. }
  148. removeSearchIndex(index) {
  149. const { searchHistoryList = [] } = this.state;
  150. searchHistoryList.splice(index, 1);
  151. this.setState({ searchHistoryList });
  152. }
  153. clearSearch() {
  154. this.setState({ searchHistoryList: [] });
  155. }
  156. /**
  157. * 设置长难句试用
  158. */
  159. sentenceTrail() {
  160. this.setState({ sentenceTrail: true });
  161. }
  162. /**
  163. * 清除长难句试用
  164. */
  165. clearSentenceTrail() {
  166. this.setState({ sentenceTrail: null });
  167. }
  168. /**
  169. * 登陆
  170. * @param {*} mobile 手机号
  171. * @param {*} mobileVerifyCode 手机验证码
  172. * @param {*} email 绑定邮箱
  173. * @param {*} inviteCode 邀请人手机/邀请码
  174. */
  175. login(area, mobile, mobileVerifyCode, email, inviteCode, auto) {
  176. if (!inviteCode) {
  177. ({ inviteCode } = this.state);
  178. }
  179. return this.apiPost('/auth/login', { area, mobile, mobileVerifyCode, inviteCode, email }).then(result => {
  180. this.infoHandle(result, auto);
  181. return result;
  182. });
  183. }
  184. loginWechat(code, auto) {
  185. return this.apiGet('/auth/wechat_pc', { code }).then(result => {
  186. this.infoHandle(result, auto);
  187. return result;
  188. });
  189. }
  190. /**
  191. * 登出
  192. */
  193. logout(login = true) {
  194. return Promise.resolve()
  195. .then(() => {
  196. if (login) {
  197. return this.apiPost('/auth/logout', {});
  198. }
  199. return true;
  200. })
  201. .then(() => {
  202. this.setState({ login: false, info: {}, username: '' });
  203. })
  204. .then(() => {
  205. linkTo(this.project.loginPath);
  206. });
  207. }
  208. /**
  209. * 绑定手机
  210. * @param {*} area 区域码
  211. * @param {*} mobile 手机号
  212. * @param {*} mobileVerifyCode 手机验证码
  213. * @param {*} email 绑定邮箱
  214. * @param {*} inviteCode 邀请人手机/邀请码
  215. */
  216. bind(area, mobile, mobileVerifyCode, email, inviteCode) {
  217. if (!inviteCode) {
  218. ({ inviteCode } = this.state);
  219. }
  220. return this.apiPost('/auth/bind', { area, mobile, mobileVerifyCode, inviteCode, email }).then(result => {
  221. this.infoHandle(result);
  222. return result;
  223. });
  224. }
  225. /**
  226. * 查询邀请码对应账号
  227. * @param {*} code 邀请码
  228. */
  229. validInviteCode(code) {
  230. return this.apiGet('/auth/valid/invite_code', { code });
  231. }
  232. /**
  233. * 查询手机对应账号
  234. */
  235. validMobile(area, mobile) {
  236. return this.apiGet('/auth/valid/mobile', { area, mobile });
  237. }
  238. /**
  239. * 查询手机是否绑定微信
  240. */
  241. validWechat(area, mobile) {
  242. return this.apiGet('/auth/valid/wechat', { area, mobile });
  243. }
  244. }
  245. export const User = new UserStore({ key: 'user', local: true });