user.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. if (this.state.login || this.adminLogin) {
  76. this.refreshToken().then(() => {
  77. if (this.adminLogin) {
  78. window.location.href = window.location.href.replace(`token=${this.adminLogin}`, '').replace('&&', '&');
  79. }
  80. });
  81. }
  82. }
  83. needPay(order) {
  84. return new Promise((resolve, reject) => {
  85. this.successCB = resolve;
  86. this.failCB = reject;
  87. formatCheckout(order.checkouts);
  88. this.setState({ needPay: true, order });
  89. });
  90. }
  91. formatCheckout(record) {
  92. formatCheckout(record);
  93. }
  94. formatOrder(order) {
  95. formatCheckout(order.checkouts);
  96. }
  97. closePay(err) {
  98. this.setState({ needPay: false });
  99. if (err) {
  100. if (this.failCB) this.failCB();
  101. } else if (this.successCB) this.successCB();
  102. this.successCB = null;
  103. this.failCB = null;
  104. }
  105. needLogin() {
  106. if (this.state.login) {
  107. return Promise.resolve();
  108. }
  109. return new Promise((resolve, reject) => {
  110. this.successCB = resolve;
  111. this.failCB = reject;
  112. this.setState({ needLogin: true });
  113. });
  114. }
  115. closeLogin(err) {
  116. this.setState({ needLogin: !!err });
  117. if (err) {
  118. if (this.failCB) this.failCB();
  119. } else if (this.successCB) this.successCB();
  120. this.successCB = null;
  121. this.failCB = null;
  122. }
  123. /**
  124. * 验证token
  125. */
  126. refreshToken() {
  127. return this.apiPost('/auth/token')
  128. .then(result => {
  129. this.infoHandle(result);
  130. })
  131. .catch(() => {
  132. this.logout(false);
  133. });
  134. }
  135. infoHandle(result, auto = true) {
  136. if (result.token) this.setToken(result.token);
  137. this.setState({ login: result.id, needLogin: !auto, info: result, username: result.username });
  138. }
  139. originInviteCode(inviteCode) {
  140. this.setState({
  141. inviteCode,
  142. });
  143. }
  144. /**
  145. * 设置长难句试用
  146. */
  147. sentenceTrail() {
  148. this.setState({ sentenceTrail: true });
  149. }
  150. /**
  151. * 清除长难句试用
  152. */
  153. clearSentenceTrail() {
  154. this.setState({ sentenceTrail: null });
  155. }
  156. /**
  157. * 登陆
  158. * @param {*} mobile 手机号
  159. * @param {*} mobileVerifyCode 手机验证码
  160. * @param {*} email 绑定邮箱
  161. * @param {*} inviteCode 邀请人手机/邀请码
  162. */
  163. login(area, mobile, mobileVerifyCode, email, inviteCode, auto) {
  164. if (!inviteCode) {
  165. ({ inviteCode } = this.state);
  166. }
  167. return this.apiPost('/auth/login', { area, mobile, mobileVerifyCode, inviteCode, email }).then(result => {
  168. this.infoHandle(result, auto);
  169. return result;
  170. });
  171. }
  172. loginWechat(code, auto) {
  173. return this.apiGet('/auth/wechat_pc', { code }).then(result => {
  174. this.infoHandle(result, auto);
  175. return result;
  176. });
  177. }
  178. /**
  179. * 登出
  180. */
  181. logout(login = true) {
  182. return Promise.resolve()
  183. .then(() => {
  184. if (login) {
  185. return this.apiPost('/auth/logout', {});
  186. }
  187. return true;
  188. })
  189. .then(() => {
  190. this.setState({ login: false, info: {}, username: '' });
  191. })
  192. .then(() => {
  193. linkTo(this.project.loginPath);
  194. });
  195. }
  196. /**
  197. * 绑定手机
  198. * @param {*} area 区域码
  199. * @param {*} mobile 手机号
  200. * @param {*} mobileVerifyCode 手机验证码
  201. * @param {*} email 绑定邮箱
  202. * @param {*} inviteCode 邀请人手机/邀请码
  203. */
  204. bind(area, mobile, mobileVerifyCode, email, inviteCode) {
  205. if (!inviteCode) {
  206. ({ inviteCode } = this.state);
  207. }
  208. return this.apiPost('/auth/bind', { area, mobile, mobileVerifyCode, inviteCode, email }).then(result => {
  209. this.infoHandle(result);
  210. return result;
  211. });
  212. }
  213. /**
  214. * 查询邀请码对应账号
  215. * @param {*} code 邀请码
  216. */
  217. validInviteCode(code) {
  218. return this.apiGet('/auth/valid/invite_code', { code });
  219. }
  220. /**
  221. * 查询手机对应账号
  222. */
  223. validMobile(area, mobile) {
  224. return this.apiGet('/auth/valid/mobile', { area, mobile });
  225. }
  226. /**
  227. * 查询手机是否绑定微信
  228. */
  229. validWechat(area, mobile) {
  230. return this.apiGet('/auth/valid/wechat', { area, mobile });
  231. }
  232. }
  233. export const User = new UserStore({ key: 'user', local: true });