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

class PythonClass extends Component {

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

  componentDidMount() {
    this.handleFetchInfo();
    this.initPageStatus();
    this.initCommand();
  }

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

  initPageStatus = () => {
FE committed
42
    if(getParam('origin') === 'barcode') {
FE committed
43 44 45 46
      this.setState({
        entryMode: 0
      });
    }
FE committed
47
    if(getParam('origin') === 'python') {
FE committed
48
      this.setState({
FE committed
49
        entryMode: 1
FE committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
      });
    }
    if(getParam('type') === '1') {
      this.setState({
        isShare: true
      });
    }
    if(getParam('type') === '2') {
      this.setState({
        isShare: false
      });
    }
    this.setState({
      type: getParam('ques') || '1'
    });
  }

  handleFetchInfo = () => {
    const id = getParam('id') || 10;
    // http.get(`${API.home}/web/python/share/help/${id}`).then(res => {
    http.get(`${API.home}/m/it/share/show`, {
      params: {
        id
      }
    }).then(res => {
      const { code, data } = res.data;
      if(code === 200) {
        this.setState({
          data,
        });
      }
    })
  }

FE committed
84
  handleToSend = (params) => {
FE committed
85
    const { history } = this.props;
FE committed
86
    const { isShare, entryMode } = this.state;
FE committed
87
    if(browser.isWeixin) {
FE committed
88
      history.push(`/pythonShare?id=${getParam('id')}&type=${getParam('type')}&ques=${getParam('ques')}&origin=python`);
FE committed
89
      this.setState({
FE committed
90
        isGuide: true
FE committed
91
      });
FE committed
92 93 94 95 96 97 98 99 100 101 102 103 104
      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
      });
FE 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 126 127 128 129 130 131 132 133
    }
  }

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

  handleToExecute = () => {
    this.setState({
      isExecute: true
    });
  }

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

  render() {
FE 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 150 151
    return (
      <>
        <HeaderBar
          title='Python基础语法'
          arrow={true}
          home={true}
        />
        <PythonContent
          isWechat={isWechat}
          isShare={isShare}
          isExecute={isExecute}
          entryMode={entryMode}
FE committed
152
          isGuide={isGuide}
FE committed
153 154 155
          isCopy={isCopy}
          command={command}
          data={data}
FE committed
156
          labelName={this.formatTitle(data)}
FE committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
          handleToExecute={this.handleToExecute}
          handleToSend={this.handleToSend}
          copyToSuccess={this.copyToSuccess}
          handleToHide={this.handleToHide}
        />
      </>
    );
  }
}

function SelfAceEditor(props) {
  return (
    <AceEditor 
      mode="python"
      theme="dracula"
      readOnly={true}
      showPrintMargin={false}
      value={props.code}
      style={{
        width: '100%',
        height: '100%'
      }}
    />
  )
}

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

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

          {/* 求助 */}
          {
            (entryMode === 0 && !isShare) &&
            <p className="python-user__desc">
              
              <span>{labelName}</span>
              遇到了困难
            </p>
          }
          {
FE committed
242
            (entryMode === 1 && !isShare) &&
FE committed
243 244 245 246 247 248 249 250
            <p className="python-user__desc">
              
              <span>{course_name}</span>的<br />
              <span>{labelName}</span>遇到了困
            </p>
          }
        </div>
        <h4 className="python-code__title">
FE committed
251
          {entryMode === 1 && isShare? `这是Ta的第${code_lines}行代码` : '运行结果'}
FE committed
252 253
        </h4>
        <div className="python-code__content">
FE committed
254
          <SelfAceEditor code={entryMode === 1 && isShare? code : result} />
FE committed
255
        </div>
FE committed
256 257 258
        <h4 className="python-code__title">
          {entryMode === 1 && isShare? '运行结果' : '代码'}
        </h4>
FE committed
259 260
        <div className="python-code__content">
          {
FE committed
261
            entryMode === 1 && isShare
FE committed
262 263 264 265
            ? <SelfAceEditor code={isExecute? result : ''} />
            : <SelfAceEditor code={code} />
          }
          {
FE committed
266
            (entryMode === 1 && isShare && !isExecute) &&
FE committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
            <button className="python-button python-button__execute" onClick={handleToExecute}>运行看看</button>
          }
        </div>
      </div>

      {
        (entryMode === 0 && isWechat) &&
        <button className="python-button python-button__study" onClick={handleToSend}>
          {isShare? '分享给好友' : '发给好友求助'}
        </button>
      }

      {
        (entryMode === 0 && !isWechat && !isCopy) &&
        <CopyToClipboard
          text={command}
          onCopy={copyToSuccess}
        >
          <button className="python-button python-button__study">
            {isShare? '分享给好友' : '发给好友求助'}
          </button>
        </CopyToClipboard>
      }

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

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

zhanghaozhe committed
304
export default PythonClass;