App.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import './App.less'
  2. import { checkRoleApi, getPackApi, getServerListApi, pushLingkLogApi, sendPhoneCodeApi } from './api'
  3. import { useAjax } from './hooks/useAjax'
  4. import { App as AntdApp, Button, Form, Input, message, Select, Typography } from 'antd'
  5. import headerImg from './assets/image/header.png'
  6. import { isMobile } from './utils'
  7. import { useEffect, useState } from 'react'
  8. import iconSucc from './assets/image/icon-succ.png'
  9. function App() {
  10. /**************************************/
  11. const [form] = Form.useForm()
  12. const serverId = Form.useWatch('serverId', form)
  13. const roleName = Form.useWatch('roleName', form)
  14. const userPhone = Form.useWatch('userPhone', form)
  15. const [linkLogId, setLinkLogId] = useState<number>()
  16. const [serverList, setServerList] = useState<{ label: string, value: any }[]>([])
  17. const [isCheckRoleSucc, setIsCheckRoleSucc] = useState<boolean>(false)
  18. const [phone, setPhone] = useState<number>()
  19. const [msg, setMsg] = useState<string | number>('发送验证码')
  20. const [succ, setSucc] = useState<boolean>(false)
  21. const [code, setCode] = useState<string>()
  22. const getServerList = useAjax((params) => getServerListApi(params))
  23. const pushLingkLog = useAjax((params) => pushLingkLogApi(params))
  24. const checkRole = useAjax((params) => checkRoleApi(params))
  25. const sendPhoneCode = useAjax((params) => sendPhoneCodeApi(params))
  26. const getPack = useAjax((params) => getPackApi(params))
  27. /**************************************/
  28. useEffect(() => {
  29. pushLingkLog.run({ url: window.location.href }).then(res => {
  30. if (res?.data) {
  31. setLinkLogId(res?.data)
  32. }
  33. })
  34. }, [])
  35. useEffect(() => {
  36. if (linkLogId) {
  37. getServerList.run({ linkLogId }).then(res => {
  38. if (res?.data) {
  39. setServerList(Object.keys(res.data).map(key => ({ label: res.data[key], value: key })))
  40. }
  41. })
  42. }
  43. }, [linkLogId])
  44. const handleCheckRole = () => {
  45. checkRole.run({ linkLogId, serverId, roleName }).then(res => {
  46. if (res?.data?.result) {
  47. setIsCheckRoleSucc(true)
  48. message.success(res?.data?.msg || '验证成功')
  49. if (res?.data?.userPhone) {
  50. setPhone(res?.data?.userPhone)
  51. form.setFieldsValue({ userPhone: res?.data?.userPhone })
  52. }
  53. } else {
  54. setIsCheckRoleSucc(false)
  55. message.error(res?.data?.msg || '验证失败')
  56. setPhone(undefined)
  57. }
  58. })
  59. }
  60. // 发送验证码
  61. const handleSendCode = () => {
  62. sendPhoneCode.run({ phoneNumber: userPhone, linkLogId }).then(res => {
  63. if (res?.data) {
  64. message.success('发送成功')
  65. setMsg(60)
  66. timeOut(60)
  67. }
  68. })
  69. }
  70. const onFinish = (values: any) => {
  71. const params = JSON.parse(JSON.stringify(values))
  72. delete params.serverId
  73. delete params.roleName
  74. getPack.run({ ...params, linkLogId }).then(res => {
  75. if (res?.data?.result) {
  76. setSucc(true)
  77. setCode(res?.data?.msg)
  78. } else {
  79. message.error(res?.data?.msg || '领取失败')
  80. }
  81. })
  82. };
  83. // 倒计时
  84. const timeOut = (num: number) => {
  85. let timer: any = null
  86. if (num > 0) {
  87. timer = setTimeout(() => {
  88. setMsg(num - 1)
  89. timeOut(num - 1)
  90. clearTimeout(timer)
  91. timer = null
  92. }, 1000)
  93. } else {
  94. setMsg('发送验证码')
  95. }
  96. }
  97. return <AntdApp>
  98. <div className='send_code'>
  99. <img src={headerImg} alt="" />
  100. <div className='cK'>
  101. <Form
  102. name="basicLq"
  103. form={form}
  104. onFinish={onFinish}
  105. autoComplete='false'
  106. >
  107. <Form.Item name="serverId" style={{ marginBottom: 20 }} rules={[{ required: true, message: '请选择区服!' }]}>
  108. <Select
  109. style={{ width: '100%' }}
  110. className='selectQf'
  111. placeholder="选择区服"
  112. showSearch
  113. options={serverList}
  114. loading={getServerList.loading || pushLingkLog.loading}
  115. filterOption={(input, option) =>
  116. (option?.label as any)?.toLowerCase().indexOf(input.toLowerCase()) >= 0
  117. }
  118. onChange={() => {
  119. setIsCheckRoleSucc(false)
  120. }}
  121. />
  122. </Form.Item>
  123. <Form.Item style={{ marginBottom: 40 }}>
  124. <div style={{ display: 'flex', gap: 10 }}>
  125. <Form.Item name="roleName" rules={[{ required: true, message: '请输入角色名!' }]} noStyle>
  126. <Input placeholder="请输入角色名" className='inputReset' onChange={() => {
  127. setIsCheckRoleSucc(false)
  128. }} />
  129. </Form.Item>
  130. <Button style={{ backgroundColor: '#DFD4C5' }} onClick={handleCheckRole} loading={checkRole.loading} disabled={!(serverId && roleName)}><strong>角色名检测</strong></Button>
  131. </div>
  132. </Form.Item>
  133. <Form.Item
  134. name="userPhone"
  135. style={{ marginBottom: 20 }}
  136. help={phone ? <span style={{ color: '#FFFBE6', fontSize: 12 }}>手机号码如果不正确,请联系客服修改</span> : undefined}
  137. rules={[
  138. {
  139. required: phone ? false : true,
  140. validator(_rule, value) {
  141. if (phone) {
  142. return Promise.resolve()
  143. }
  144. if (!value) {
  145. return Promise.reject(new Error('请输入手机号!'))
  146. } else if (!isMobile(value)) {
  147. return Promise.reject(new Error('请输入正确的手机号!'))
  148. }
  149. return Promise.resolve()
  150. }
  151. }
  152. ]}
  153. >
  154. <Input placeholder="请输入手机号" className='inputReset' disabled={!isCheckRoleSucc || !!phone} />
  155. </Form.Item>
  156. <Form.Item style={{ marginBottom: 50 }}>
  157. <div style={{ display: 'flex', gap: 10 }}>
  158. <Form.Item name="randomCode" rules={[{ required: true, message: '请输入验证码!' }]} noStyle>
  159. <Input placeholder="请输入验证码" className='inputReset' disabled={!isCheckRoleSucc} />
  160. </Form.Item>
  161. <Button style={{ backgroundColor: '#DFD4C5' }} disabled={!userPhone || msg !== '发送验证码'} onClick={handleSendCode}><strong>{msg === '发送验证码' ? msg : msg + 's'}</strong></Button>
  162. </div>
  163. </Form.Item>
  164. <Form.Item style={{ marginBottom: 10, textAlign: 'center' }}>
  165. <Button htmlType="submit" className="form_button" style={{ backgroundColor: '#1D1001', color: '#FFEBD5', width: 140 }} loading={getPack.loading} size="large"><strong>领取</strong></Button>
  166. </Form.Item>
  167. </Form>
  168. </div>
  169. {succ && <div className="pop">
  170. <div className="pop_body">
  171. <div className="payment_head">
  172. <img src={iconSucc} style={{ width: 25 }} />
  173. <span className="payment_amount" style={{ fontSize: 20 }}><b>领取成功</b></span>
  174. </div>
  175. <Typography.Title level={4} style={{ margin: '10px 6px' }} copyable={{ icon: <span>复制</span>, tooltips: ['复制', '复制成功'] }}>{code}</Typography.Title>
  176. </div>
  177. </div>}
  178. </div>
  179. </AntdApp>
  180. }
  181. export default App