index.js 7.84 KB
Newer Older
FE committed
1
import React, { Component } from 'react';
FE committed
2
import { Link } from 'react-router-dom';
FE committed
3
import {CopyToClipboard} from 'react-copy-to-clipboard';
FE committed
4
import AceEditor from 'react-ace';
FE committed
5
import { Toast } from "antd-mobile"
FE committed
6
import {HeaderBar} from '@/common';
xuzhenghua committed
7
import { browser, http, getParam, wxShare } from '@/utils';
FE committed
8
import './index.scss';
FE committed
9 10
import 'ace-builds/src-noconflict/mode-python';
import 'ace-builds/src-noconflict/theme-dracula';
FE committed
11 12 13 14 15 16

class PythonClass extends Component {

  constructor(props) {
    super(props);
    this.state = {
FE committed
17
      isWechat: browser.isWeixin,
FE committed
18
      isShare: true,
FE committed
19
      type: '1', // 1:课后习题,2:课堂习题
xuzhenghua committed
20 21
      entryMode: 0, // 0:扫码页,1:落地页
      isGuide: false, // 是否展示引导
FE committed
22
      isExecute: false,
FE committed
23
      isCopy: false,
FE committed
24
      command: '',
FE committed
25
      data: {}
FE committed
26 27 28
    }
  }

FE committed
29 30
  componentDidMount() {
    this.handleFetchInfo();
FE committed
31
    this.initPageStatus();
FE committed
32 33 34 35 36
    this.initCommand();
  }

  initCommand = () => {
    this.setState({
xuzhenghua committed
37
      command: `${API.m}/pythonShare?id=${getParam('id')}&type=${getParam('type')}&ques=${getParam('ques')}&origin=python`
FE committed
38
    })
FE committed
39
  }
FE committed
40

FE committed
41
  initPageStatus = () => {
xuzhenghua committed
42
    if(getParam('origin') === 'barcode') {
FE committed
43 44 45 46
      this.setState({
        entryMode: 0
      });
    }
xuzhenghua committed
47
    if(getParam('origin') === 'python') {
FE committed
48
      this.setState({
xuzhenghua committed
49
        entryMode: 1
FE committed
50 51
      });
    }
FE committed
52 53 54 55 56 57 58 59 60 61 62 63 64
    if(getParam('type') === '1') {
      this.setState({
        isShare: true
      });
    }
    if(getParam('type') === '2') {
      this.setState({
        isShare: false
      });
    }
    this.setState({
      type: getParam('ques') || '1'
    });
FE committed
65 66 67
  }

  handleFetchInfo = () => {
FE committed
68
    const id = getParam('id') || 10;
FE committed
69 70 71 72 73 74
    // http.get(`${API.home}/web/python/share/help/${id}`).then(res => {
    http.get(`${API.home}/m/it/share/show`, {
      params: {
        id
      }
    }).then(res => {
FE committed
75 76 77 78 79 80
      const { code, data } = res.data;
      if(code === 200) {
        this.setState({
          data,
        });
      }
FE committed
81 82 83
    })
  }

xuzhenghua committed
84
  handleToSend = (params) => {
FE committed
85
    const { history } = this.props;
xuzhenghua committed
86
    const { isShare, entryMode } = this.state;
FE committed
87
    if(browser.isWeixin) {
xuzhenghua committed
88
      history.push(`/pythonShare?id=${getParam('id')}&type=${getParam('type')}&ques=${getParam('ques')}&origin=python`);
FE committed
89
      this.setState({
xuzhenghua committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
        isGuide: true
      });
      let title = '';
      let labelName = this.formatTitle(params);
      if(entryMode !== 0 && !isShare) {
        title = `我在${params.course_name}${labelName}遇到了困难`;
      }
      if(entryMode !== 0 && isShare) {
        title = `我已在【${params.course_name}】上运行了行代码了${params.code_lines}`
      }
      wxShare({
        title,
        desc: labelName,
        link: encodeURI(location.href),
        imgUrl: params.course_img,
FE committed
105
      });
xuzhenghua committed
106 107 108 109 110 111 112 113 114 115
    }
  }

  formatTitle = (params) => {
    const { type } = this.state;
    if(type === '1') {
      return `练习-${params.ques_name}`;
    }
    if(type === '2') {
      return `课堂-${params.video_name}`;
FE committed
116 117 118 119 120 121 122 123 124 125
    }
  }

  copyToSuccess = () => {
    Toast.info('已复制链接,快去粘贴发给好友吧~');
    this.setState({
      isCopy: true
    });
  }

FE committed
126 127 128 129 130 131
  handleToExecute = () => {
    this.setState({
      isExecute: true
    });
  }

FE committed
132 133
  handleToHide = () => {
    this.setState({
xuzhenghua committed
134
      isGuide: false
FE committed
135 136 137
    });
  }

FE committed
138
  render() {
xuzhenghua committed
139
    const { isWechat, isShare, isExecute, entryMode, command, isCopy, isGuide, data } = this.state;
FE committed
140 141 142 143 144 145 146 147 148 149
    return (
      <>
        <HeaderBar
          title='Python基础语法'
          arrow={true}
          home={true}
        />
        <PythonContent
          isWechat={isWechat}
          isShare={isShare}
FE committed
150
          isExecute={isExecute}
FE committed
151
          entryMode={entryMode}
xuzhenghua committed
152
          isGuide={isGuide}
FE committed
153 154
          isCopy={isCopy}
          command={command}
FE committed
155
          data={data}
xuzhenghua committed
156
          labelName={this.formatTitle(data)}
FE committed
157
          handleToExecute={this.handleToExecute}
FE committed
158 159
          handleToSend={this.handleToSend}
          copyToSuccess={this.copyToSuccess}
FE committed
160
          handleToHide={this.handleToHide}
FE committed
161 162 163 164 165 166
        />
      </>
    );
  }
}

FE committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
function SelfAceEditor(props) {
  return (
    <AceEditor 
      mode="python"
      theme="dracula"
      readOnly={true}
      showPrintMargin={false}
      value={props.code}
      style={{
        width: '100%',
        height: '100%'
      }}
    />
  )
}

FE committed
183
function PythonContent(props) {
FE committed
184 185 186 187 188 189 190
  const { 
    isWechat, 
    isShare, 
    isExecute, 
    entryMode, 
    isCopy, 
    command, 
xuzhenghua committed
191 192 193
    labelName,
    isGuide,
    data: { head_img, nickname, code_lines, code, result, course_name, course_id },
FE committed
194 195
    handleToSend, 
    copyToSuccess, 
FE committed
196 197
    handleToExecute,
    handleToHide
FE committed
198
  } = props;
FE committed
199
  return (
FE committed
200
    <div className="python-container">
FE committed
201
      {
xuzhenghua committed
202
        isGuide && 
FE committed
203
        <div className="python-popup" onClick={handleToHide}>
FE committed
204 205 206 207
          <div className="python-header">
            <p className="python-wechat__title">请点击右上角分享</p>
            <i className="iconfont iconyindao"></i>
          </div>
FE committed
208 209 210 211
        </div>
      }
      <div className="python-content">
        <div className="python-user">
xuzhenghua committed
212
          <i className="python-user__portrait" style={{backgroundImage: `url(${head_img})`}}></i>
FE committed
213
          <h2 className="python-user__id">{nickname}</h2>
FE committed
214 215 216 217 218 219

          {/* 分享 */}
          {
            (entryMode === 0 && isShare) &&
            <p className="python-user__desc">
              完成了
FE committed
220
              <span>{labelName}</span>
FE committed
221 222 223
            </p>
          }
          {
xuzhenghua committed
224
            (entryMode === 1 && isShare) &&
FE committed
225 226
            <p className="python-user__desc">
              
FE committed
227 228
              <span>{course_name}</span>完成了<br />
              {labelName}
FE committed
229 230 231 232 233 234 235 236
            </p>
          }

          {/* 求助 */}
          {
            (entryMode === 0 && !isShare) &&
            <p className="python-user__desc">
              
FE committed
237
              <span>{labelName}</span>
FE committed
238 239 240 241
              遇到了困难
            </p>
          }
          {
xuzhenghua committed
242
            (entryMode === 1 && !isShare) &&
FE committed
243 244
            <p className="python-user__desc">
              
FE committed
245 246
              <span>{course_name}</span>的<br />
              <span>{labelName}</span>遇到了困
FE committed
247 248 249 250
            </p>
          }
        </div>
        <h4 className="python-code__title">
xuzhenghua committed
251
          {entryMode === 1 && isShare? `这是Ta的第${code_lines}行代码` : '运行结果'}
FE committed
252 253
        </h4>
        <div className="python-code__content">
xuzhenghua committed
254
          <SelfAceEditor code={entryMode === 1 && isShare? code : result} />
FE committed
255
        </div>
xuzhenghua committed
256 257 258
        <h4 className="python-code__title">
          {entryMode === 1 && isShare? '运行结果' : '代码'}
        </h4>
FE committed
259 260
        <div className="python-code__content">
          {
xuzhenghua committed
261
            entryMode === 1 && isShare
FE committed
262 263
            ? <SelfAceEditor code={isExecute? result : ''} />
            : <SelfAceEditor code={code} />
FE committed
264 265
          }
          {
xuzhenghua committed
266
            (entryMode === 1 && isShare && !isExecute) &&
FE committed
267
            <button className="python-button python-button__execute" onClick={handleToExecute}>运行看看</button>
FE committed
268 269 270 271 272
          }
        </div>
      </div>

      {
FE committed
273
        (entryMode === 0 && isWechat) &&
FE committed
274 275 276
        <button className="python-button python-button__study" onClick={handleToSend}>
          {isShare? '分享给好友' : '发给好友求助'}
        </button>
FE committed
277 278 279 280 281 282 283 284
      }

      {
        (entryMode === 0 && !isWechat && !isCopy) &&
        <CopyToClipboard
          text={command}
          onCopy={copyToSuccess}
        >
FE committed
285 286 287
          <button className="python-button python-button__study">
            {isShare? '分享给好友' : '发给好友求助'}
          </button>
FE committed
288 289 290 291 292
        </CopyToClipboard>
      }

      {
        (entryMode === 0 && !isWechat && isCopy) &&
FE committed
293
        <p className="python-button__tip">已复制链接,快去粘贴发给好友吧~</p>
FE committed
294 295 296
      }

      {
xuzhenghua committed
297
        entryMode === 1 &&
FE committed
298
        <Link className="python-button python-button__study" to={`/python?id=${course_id}`}>我也要学Python</Link>
FE committed
299 300 301 302 303 304
      }
    </div>
  );
}

export default PythonClass;