123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- import BaseStore from '@src/stores/base';
- import * as querystring from 'querystring';
- import { getMap } from '@src/services/Tools';
- // import * as querystring from 'querystring';
- import { ServiceParamMap, OrderInfoMap, ServiceKey } from '../../Constant';
- const ServiceParamRelation = getMap(
- Object.keys(ServiceParamMap).map(key => {
- return {
- map: getMap(
- ServiceParamMap[key].map((row, index) => {
- row.index = index;
- return row;
- }),
- 'value',
- 'index',
- ),
- key,
- };
- }),
- 'key',
- 'map',
- );
- function formatTitle(record) {
- if (record.productType === 'course_package') {
- return (record.coursePackage || {}).title;
- }
- if (record.productType === 'course') {
- record.minNumber = record.course.minNumber;
- record.maxNumber = record.course.maxNumber;
- return (record.course || {}).title;
- }
- if (record.productType === 'data') {
- return (record.data || {}).title;
- }
- if (record.productType === 'service') {
- return record.info.label || (record.serviceInfo || {}).title;
- }
- return '';
- }
- function formatGift(record) {
- let gift = null;
- if (record.productType === 'course_package') {
- ({ gift } = record.coursePackage || {});
- }
- if (!gift) return null;
- return ServiceKey.map(row => {
- if (!gift[row.value]) return null;
- const list = ServiceParamMap[row.value];
- if (list) {
- const map = getMap(list, 'value');
- return Object.assign({ param: map[gift[row.value]] }, map[gift[row.value]], row);
- }
- return Object.assign({ number: gift[row.value] }, row);
- }).filter(row => row);
- }
- function formatCheckout(checkouts) {
- checkouts.forEach(checkout => {
- checkout.key = checkout.id;
- checkout.info = OrderInfoMap[checkout.productType];
- if (checkout.productType === 'service') {
- const index =
- (ServiceParamRelation[checkout.service] && ServiceParamRelation[checkout.service][checkout.param]) || 0;
- checkout.info = Object.assign({}, checkout.info[checkout.service], checkout.serviceInfo.package[index]);
- }
- checkout.title = formatTitle(checkout);
- checkout.gift = formatGift(checkout);
- if (checkout.children) formatCheckout(checkout.children);
- });
- }
- export default class UserStore extends BaseStore {
- constructor(props) {
- super(props);
- this.adminLogin = null;
- const { token } = querystring.parse(window.location.search.replace('?', ''));
- if (token) {
- this.adminLogin = token;
- }
- }
- initState() {
- if (this.adminLogin) this.setToken(this.adminLogin);
- return { login: !!this.adminLogin, needPay: false, needLogin: false };
- }
- fixState() {
- return { needPay: false, needLogin: false };
- }
- initAfter() {
- if (this.adminLogin || this.state.login) {
- this.refreshToken().then(() => {
- if (this.adminLogin) {
- window.location.href = window.location.href.replace(`token=${this.adminLogin}`, '').replace('&&', '&');
- }
- }).catch(() => {
- if (this.adminLogin) {
- window.location.href = window.location.href.replace(`token=${this.adminLogin}`, '').replace('&&', '&');
- }
- });
- }
- }
- needPay(order) {
- return new Promise((resolve, reject) => {
- this.successCB = resolve;
- this.failCB = reject;
- formatCheckout(order.checkouts);
- this.setState({ needPay: true, order });
- });
- }
- formatCheckout(record) {
- formatCheckout(record);
- }
- formatOrder(order) {
- formatCheckout(order.checkouts);
- }
- closePay(err) {
- this.setState({ needPay: false });
- if (err) {
- if (this.failCB) this.failCB();
- } else if (this.successCB) this.successCB();
- this.successCB = null;
- this.failCB = null;
- }
- needLogin() {
- if (this.state.login) {
- return Promise.resolve();
- }
- return new Promise((resolve, reject) => {
- this.successCB = resolve;
- this.failCB = reject;
- this.setState({ needLogin: true });
- });
- }
- closeLogin(err) {
- this.setState({ needLogin: false });
- if (err) {
- if (this.failCB) this.failCB();
- } else if (this.successCB) this.successCB();
- this.successCB = null;
- this.failCB = null;
- }
- /**
- * 验证token
- */
- refreshToken() {
- return this.apiPost('/auth/token')
- .then(result => {
- this.infoHandle(result);
- })
- .catch(() => {
- this.logout(false);
- });
- }
- infoHandle(result, auto = true) {
- if (result.token) this.setToken(result.token);
- this.setState({ login: result.id, needLogin: !auto, info: result, username: result.username });
- }
- originInviteCode(inviteCode) {
- this.setState({
- inviteCode,
- });
- }
- addSearch(keyword) {
- const { searchHistoryList = [] } = this.state;
- searchHistoryList.unshift(keyword);
- searchHistoryList.splice(5);
- this.setState({ searchHistoryList });
- }
- removeSearchIndex(index) {
- const { searchHistoryList = [] } = this.state;
- searchHistoryList.splice(index, 1);
- this.setState({ searchHistoryList });
- }
- clearSearch() {
- this.setState({ searchHistoryList: [] });
- }
- /**
- * 设置长难句试用
- */
- sentenceTrail() {
- this.setState({ sentenceTrail: true });
- }
- /**
- * 清除长难句试用
- */
- clearSentenceTrail() {
- this.setState({ sentenceTrail: null });
- }
- /**
- * 登陆
- * @param {*} mobile 手机号
- * @param {*} mobileVerifyCode 手机验证码
- * @param {*} email 绑定邮箱
- * @param {*} inviteCode 邀请人手机/邀请码
- */
- login(area, mobile, mobileVerifyCode, email, inviteCode, auto) {
- if (!inviteCode) {
- ({ inviteCode } = this.state);
- }
- return this.apiPost('/auth/login', { area, mobile, mobileVerifyCode, inviteCode, email }).then(result => {
- this.infoHandle(result, auto);
- return result;
- });
- }
- loginWechat(code, login, auto) {
- return this.apiGet('/auth/wechat_pc', { code, login }).then(result => {
- this.infoHandle(result, auto);
- return result;
- });
- }
- /**
- * 登出
- */
- logout(login = true) {
- return Promise.resolve()
- .then(() => {
- if (login) {
- return this.apiPost('/auth/logout', {});
- }
- return true;
- })
- .then(() => {
- this.setToken('');
- this.setState({ login: false, info: {}, username: '' });
- })
- .then(() => {
- linkTo(this.project.loginPath);
- });
- }
- /**
- * 绑定手机
- * @param {*} area 区域码
- * @param {*} mobile 手机号
- * @param {*} mobileVerifyCode 手机验证码
- * @param {*} email 绑定邮箱
- * @param {*} inviteCode 邀请人手机/邀请码
- */
- bind(area, mobile, mobileVerifyCode, email, inviteCode) {
- if (!inviteCode) {
- ({ inviteCode } = this.state);
- }
- return this.apiPost('/auth/bind', { area, mobile, mobileVerifyCode, inviteCode, email }).then(result => {
- this.infoHandle(result);
- return result;
- });
- }
- /**
- * 查询邀请码对应账号
- * @param {*} code 邀请码
- */
- validInviteCode(code) {
- return this.apiGet('/auth/valid/invite_code', { code });
- }
- /**
- * 查询手机对应账号
- */
- validMobile(area, mobile) {
- return this.apiGet('/auth/valid/mobile', { area, mobile });
- }
- /**
- * 查询邮箱对应账号
- */
- validEmail(email) {
- return this.apiGet('/auth/valid/email', { email });
- }
- /**
- * 查询手机是否绑定微信
- */
- validWechat(area, mobile) {
- return this.apiGet('/auth/valid/wechat', { area, mobile });
- }
- }
- export const User = new UserStore({ key: 'user', local: true });
|