index.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import { useAjax } from "@/Hook/useAjax"
  2. import { appAccountConfig, doAuthOfBUnit, getAppAccountConfig, tencentAdAccountDoAuth, tencentAdAccountToAuth } from "@/services/miniApp/adBack"
  3. import { BetaSchemaForm, PageContainer, ProCard, ProFormInstance} from "@ant-design/pro-components"
  4. import { history, useModel } from "@umijs/max"
  5. import { Button, message, Modal, Radio, Space } from "antd"
  6. import { useEffect, useRef, useState } from "react"
  7. import formConfig from "./formConfig"
  8. import { searchToObj } from "@/utils"
  9. function Auth() {
  10. let { getEnum } = useModel('global')
  11. let { initialState } = useModel("@@initialState")
  12. let [key, setKey] = useState<any>()
  13. let [isEdit, setIsEdit] = useState(false)
  14. const [readonly, setReadonly] = useState(false);
  15. const formRef = useRef<ProFormInstance>();
  16. const [accountData, setAccountData] = useState<any>(null)
  17. const [accountId, setAccountId] = useState(null)
  18. let TencentAdAccountToAuth = useAjax((params) => tencentAdAccountToAuth(params))//获取授权地址
  19. let TencentAdAccountDoAuth = useAjax((params) => tencentAdAccountDoAuth(params))//开始授权
  20. let GetAppAccountConfig = useAjax((params) => getAppAccountConfig(params))//获取回传配置
  21. let AppAccountConfig = useAjax((params) => appAccountConfig(params))//设置配置
  22. let DoAuthOfBUnit = useAjax((params) => doAuthOfBUnit(params))//广告业务单元授权
  23. // 获取广告回传列表
  24. useEffect(() => {
  25. // 授权回调
  26. if (history.location.search) {
  27. let obj = searchToObj(history.location.search)
  28. if (obj?.state && obj?.authorization_code) {
  29. TencentAdAccountDoAuth.run({
  30. "state": obj.state,
  31. "authorizationCode": obj.authorization_code,
  32. "callbackPage": "https://testdistribution.zanxiangwl.com/miniApp/adBack"
  33. }).then(res => {
  34. if (res.code === 200) {
  35. // 授权失败
  36. if (res.data.authorizationStatus == 1) {
  37. setAccountData(res.data)
  38. } else {
  39. message.success("授权成功")
  40. setTimeout(() => {
  41. location.href = location.origin + '/miniApp/adBack'
  42. }, 1000)
  43. }
  44. } else {
  45. setTimeout(() => {
  46. history.push("/miniApp/adBack")
  47. }, 1000)
  48. }
  49. })
  50. }
  51. }
  52. getConfig()
  53. }, [])
  54. //获取回传配置
  55. const getConfig = () => {
  56. GetAppAccountConfig.run({ appType: initialState?.selectApp?.appType, appId: initialState?.selectApp?.id })
  57. }
  58. // 授权
  59. const auth = () => {
  60. TencentAdAccountToAuth.run({
  61. appType: initialState?.selectApp?.appType,
  62. appId: initialState?.selectApp?.id,
  63. advertisingChannel: key,
  64. callbackPage: "https://testdistribution.zanxiangwl.com/miniApp/adBack"
  65. }).then(res => {
  66. if (res.code === 200) {
  67. window.open(res.data)
  68. }
  69. })
  70. }
  71. // submit
  72. const submit = async (values: any) => {
  73. // 遍历 firstBackRate 数组,并将 backRate 转换为字符串
  74. if (values.firstBackRate) {
  75. values.firstBackRate = values.firstBackRate.map((item: any) => {
  76. if (Array.isArray(item.backRate)) {
  77. item.backRate = item.backRate.join(":"); // 转换数组为 'min:max' 字符串
  78. }
  79. return item;
  80. });
  81. }
  82. if (values.otherBackRate) {
  83. values.otherBackRate = values.otherBackRate.map((item: any) => {
  84. if (Array.isArray(item.backRate)) {
  85. item.backRate = item.backRate.join(":"); // 转换数组为 'min:max' 字符串
  86. }
  87. return item;
  88. });
  89. }
  90. if (values.markUpBackRate) {
  91. values.markUpBackRate = values.markUpBackRate.map((item: any) => {
  92. if (Array.isArray(item.backRate)) {
  93. item.backRate = item.backRate.join(":"); // 转换数组为 'min:max' 字符串
  94. }
  95. return item;
  96. });
  97. }
  98. // 打印转换后的数据
  99. console.log("处理后的表单数据: ", values);
  100. AppAccountConfig.run({
  101. appType: initialState?.selectApp?.appType,
  102. appId: initialState?.selectApp?.id,
  103. advertisingChannel: key,
  104. policyType: 1,
  105. policyContentOfDefault: values
  106. }).then(res => {
  107. if (res.code === 200) {
  108. message.success("配置成功!")
  109. GetAppAccountConfig.refresh()
  110. setIsEdit(false)
  111. }
  112. })
  113. }
  114. // lock or edit
  115. const edit = () => {
  116. setIsEdit(true)
  117. if (GetAppAccountConfig?.data?.data[key]?.distributorAppAdBackPolicy?.policyContentOfDefault) {
  118. setTimeout(() => {
  119. formRef?.current?.setFieldsValue(GetAppAccountConfig?.data?.data[key]?.distributorAppAdBackPolicy?.policyContentOfDefault)
  120. }, 100)
  121. }
  122. }
  123. // 广告单元授权
  124. const ad = () => {
  125. doAuthOfBUnit({ state: accountData?.state, accountId }).then(res => {
  126. if (res.code === 200) {
  127. message.success("授权成功")
  128. setAccountData(null)
  129. setAccountId(null)
  130. }
  131. setTimeout(() => {
  132. location.href = location.origin + '/miniApp/adBack'
  133. }, 1000)
  134. })
  135. }
  136. return <PageContainer
  137. tabList={getEnum("ADVERTISING_CHANNEL", "arr")?.map((item: { label: any; key: any }, index: number) => {
  138. if (index === 0 && !key) {
  139. setKey(item.key)
  140. }
  141. return { tab: item.label, key: item.key }
  142. })}
  143. tabProps={{
  144. type: 'card',
  145. hideAdd: true,
  146. onChange: (e) => setKey(e),
  147. }}
  148. >
  149. <ProCard direction="column" ghost gutter={8} loading={GetAppAccountConfig?.loading} >
  150. <ProCard
  151. bordered
  152. title="归因设置"
  153. actions={
  154. !GetAppAccountConfig?.data?.data[key]?.tencentAdAccount && <Space style={{ margin: 20 }}> <Button type="primary" onClick={auth}>授权</Button></Space>
  155. }
  156. >
  157. <div>
  158. <div style={{ marginBottom: 5 }}>
  159. <span style={{ width: 100, display: 'inline-block', textAlign: 'right' }}>归因方式:</span>
  160. <span style={{ color: "#999" }}>数据源ID</span>
  161. </div>
  162. <div style={{ marginBottom: 5 }}>
  163. <span style={{ width: 100, display: 'inline-block', textAlign: 'right' }}>数据源类型:</span>
  164. <span style={{ color: "#999" }}>{getEnum("TENCENT_USER_ACTION_TYPE", 'map').get(GetAppAccountConfig?.data?.data[key]?.tencentAdAccount?.userActionSets?.type) || "-"}</span>
  165. </div>
  166. <div style={{ marginBottom: 5 }}>
  167. <span style={{ width: 100, display: 'inline-block', textAlign: 'right' }}>数据源ID:</span>
  168. <span style={{ color: "#999" }}>{GetAppAccountConfig?.data?.data[key]?.tencentAdAccount?.userActionSets?.userActionSetId || "-"}</span>
  169. </div>
  170. <div style={{ marginBottom: 5 }}>
  171. <span style={{ width: 100, display: 'inline-block', textAlign: 'right' }}>已授权账号ID:</span>
  172. <span style={{ color: "#999" }}> {GetAppAccountConfig?.data?.data[key]?.tencentAdAccount?.userActionSets?.accountId || "-"}</span>
  173. </div>
  174. {GetAppAccountConfig?.data?.data[key]?.tencentAdAccount && <div style={{ marginBottom: 5 }}>
  175. <span style={{ width: 100, display: 'inline-block', textAlign: 'right' }}>授权:</span>
  176. <span style={{ color: "#999" }}> <Button type="primary" onClick={auth} size="small" danger>更换授权</Button></span>
  177. </div>}
  178. </div>
  179. </ProCard>
  180. <ProCard
  181. bordered
  182. title="回传设置"
  183. tooltip="回传“下单”“付费”两个行为事件,直投小程序链路额外支持“注册”(纯新+染色回流用户进入小程序)事件回传。"
  184. actions={<Space style={{ margin: 20 }}>
  185. <Button type="primary" onClick={edit}>编辑</Button>
  186. </Space>}
  187. >
  188. <div>
  189. <div style={{ marginBottom: 5 }}>
  190. <span style={{ width: 200, display: 'inline-block', textAlign: 'right' }}>回传类型:</span>
  191. <span style={{ color: "#999" }}> {getEnum("CALLBACK_TYPE", "map")?.get(GetAppAccountConfig?.data?.data[key]?.distributorAppAdBackPolicy?.policyContentOfDefault?.backUnit) || "-"}</span>
  192. </div>
  193. <div style={{ marginBottom: 5 }}>
  194. <span style={{ width: 200, display: 'inline-block', textAlign: 'right' }}>VIP回传策略:</span>
  195. <span style={{ color: "#999" }}>{getEnum("VIP_CALLBACK", "map")?.get(GetAppAccountConfig?.data?.data[key]?.distributorAppAdBackPolicy?.policyContentOfDefault?.vipOrderBackType) || "-"}</span>
  196. </div>
  197. <div style={{ marginBottom: 5 }}>
  198. <span style={{ width: 200, display: 'inline-block', textAlign: 'right' }}>是否补单:</span>
  199. <span style={{ color: "#999" }}>{new Map([true, false].map(key => [key, key ? "是" : "否"])).get(GetAppAccountConfig?.data?.data[key]?.distributorAppAdBackPolicy?.policyContentOfDefault?.markUpOrder) || "-"}</span>
  200. </div>
  201. <div style={{ marginBottom: 5 }}>
  202. <span style={{ width: 200, display: 'inline-block', textAlign: 'right' }}>单用户最大回传订单数:</span>
  203. <span style={{ color: "#999" }}> {GetAppAccountConfig?.data?.data[key]?.distributorAppAdBackPolicy?.policyContentOfDefault?.maxBackCountOfUser || "-"}</span>
  204. </div>
  205. <div style={{ marginBottom: 5 }}>
  206. <span style={{ width: 200, display: 'inline-block', textAlign: 'right' }}>注册充值回传的最大间隔时间:</span>
  207. <span style={{ color: "#999" }}> {GetAppAccountConfig?.data?.data[key]?.distributorAppAdBackPolicy?.policyContentOfDefault?.regPayIntervalTime ? GetAppAccountConfig?.data?.data[key]?.distributorAppAdBackPolicy?.policyContentOfDefault?.regPayIntervalTime + "分钟" : "-"}</span>
  208. </div>
  209. {GetAppAccountConfig?.data?.data[key]?.distributorAppAdBackPolicy?.policyContentOfDefault && <div style={{ marginBottom: 5 }}>
  210. <span style={{ width: 200, display: 'inline-block', textAlign: 'right' }}>更多内容:</span>
  211. <span style={{ color: "#999" }}><Button size='small' type='link' onClick={() => { setReadonly(true); edit() }}>查看</Button></span>
  212. </div>}
  213. </div>
  214. </ProCard>
  215. </ProCard>
  216. {/* 回传配置 */}
  217. <BetaSchemaForm<any>
  218. title={"回传策略配置"}
  219. formRef={formRef}
  220. open={isEdit}
  221. onOpenChange={(b) => {
  222. if (!b) {
  223. setIsEdit(b)
  224. setReadonly(false)
  225. }
  226. }}
  227. layoutType={"ModalForm"}
  228. labelCol={{ span: 24 }}
  229. colProps={{ span: 8 }}
  230. grid={true}
  231. layout="vertical"
  232. onFinish={submit}
  233. columns={formConfig()}
  234. submitter={readonly ? false : { render: (props, dom) => [...dom] }}
  235. modalProps={{
  236. destroyOnClose: true
  237. }}
  238. />
  239. {/* 业务单元授权 */}
  240. <Modal
  241. open={!!accountData}
  242. title={"选择广告账号"}
  243. destroyOnClose
  244. onCancel={() => { setAccountData(null); setAccountId(null) }}
  245. onOk={ad}
  246. >
  247. <Radio.Group onChange={(e) => {
  248. let value = e.target.value
  249. setAccountId(value)
  250. }} value={accountId} >
  251. {
  252. accountData?.accountIdList?.map((id: any) => {
  253. return <Radio key={id} value={id}>{id}</Radio>
  254. })
  255. }
  256. </Radio.Group>
  257. </Modal>
  258. </PageContainer>
  259. }
  260. export default Auth