index.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. console.log("values",values)
  74. // 遍历 firstBackRate 数组,并将 backRate 转换为字符串
  75. if (values.firstBackRate) {
  76. values.firstBackRate = values.firstBackRate.map((item: any) => {
  77. if (Array.isArray(item.backRate)) {
  78. item.backRate = item.backRate.join(":"); // 转换数组为 'min:max' 字符串
  79. }
  80. return item;
  81. });
  82. }
  83. if (values.otherBackRate) {
  84. values.otherBackRate = values.otherBackRate.map((item: any) => {
  85. if (Array.isArray(item.backRate)) {
  86. item.backRate = item.backRate.join(":"); // 转换数组为 'min:max' 字符串
  87. }
  88. return item;
  89. });
  90. }
  91. if (values.markUpBackRate) {
  92. values.markUpBackRate = values.markUpBackRate.map((item: any) => {
  93. if (Array.isArray(item.backRate)) {
  94. item.backRate = item.backRate.join(":"); // 转换数组为 'min:max' 字符串
  95. }
  96. return item;
  97. });
  98. }
  99. // 打印转换后的数据
  100. console.log("处理后的表单数据: ", values);
  101. AppAccountConfig.run({
  102. appType: initialState?.selectApp?.appType,
  103. appId: initialState?.selectApp?.id,
  104. advertisingChannel: key,
  105. policyType: 1,
  106. policyContentOfDefault: values
  107. }).then(res => {
  108. if (res.code === 200) {
  109. message.success("配置成功!")
  110. GetAppAccountConfig.refresh()
  111. setIsEdit(false)
  112. }
  113. })
  114. }
  115. // lock or edit
  116. const edit = () => {
  117. setIsEdit(true)
  118. if (GetAppAccountConfig?.data?.data[key]?.distributorAppAdBackPolicy?.policyContentOfDefault) {
  119. setTimeout(() => {
  120. formRef?.current?.setFieldsValue(GetAppAccountConfig?.data?.data[key]?.distributorAppAdBackPolicy?.policyContentOfDefault)
  121. }, 100)
  122. }
  123. }
  124. // 广告单元授权
  125. const ad = () => {
  126. doAuthOfBUnit({ state: accountData?.state, accountId }).then(res => {
  127. if (res.code === 200) {
  128. message.success("授权成功")
  129. setAccountData(null)
  130. setAccountId(null)
  131. }
  132. setTimeout(() => {
  133. location.href = location.origin + '/miniApp/adBack'
  134. }, 1000)
  135. })
  136. }
  137. return <PageContainer
  138. tabList={getEnum("ADVERTISING_CHANNEL", "arr")?.map((item: { label: any; key: any }, index: number) => {
  139. if (index === 0 && !key) {
  140. setKey(item.key)
  141. }
  142. return { tab: item.label, key: item.key }
  143. })}
  144. tabProps={{
  145. type: 'card',
  146. hideAdd: true,
  147. onChange: (e) => setKey(e),
  148. }}
  149. >
  150. <ProCard direction="column" ghost gutter={8} loading={GetAppAccountConfig?.loading} >
  151. <ProCard
  152. bordered
  153. title="归因设置"
  154. actions={
  155. !GetAppAccountConfig?.data?.data[key]?.tencentAdAccount && <Space style={{ margin: 20 }}> <Button type="primary" onClick={auth}>授权</Button></Space>
  156. }
  157. >
  158. <div>
  159. <div style={{ marginBottom: 5 }}>
  160. <span style={{ width: 100, display: 'inline-block', textAlign: 'right' }}>归因方式:</span>
  161. <span style={{ color: "#999" }}>数据源ID</span>
  162. </div>
  163. <div style={{ marginBottom: 5 }}>
  164. <span style={{ width: 100, display: 'inline-block', textAlign: 'right' }}>数据源类型:</span>
  165. <span style={{ color: "#999" }}>{getEnum("TENCENT_USER_ACTION_TYPE", 'map').get(GetAppAccountConfig?.data?.data[key]?.tencentAdAccount?.userActionSets?.type) || "-"}</span>
  166. </div>
  167. <div style={{ marginBottom: 5 }}>
  168. <span style={{ width: 100, display: 'inline-block', textAlign: 'right' }}>数据源ID:</span>
  169. <span style={{ color: "#999" }}>{GetAppAccountConfig?.data?.data[key]?.tencentAdAccount?.userActionSets?.userActionSetId || "-"}</span>
  170. </div>
  171. <div style={{ marginBottom: 5 }}>
  172. <span style={{ width: 100, display: 'inline-block', textAlign: 'right' }}>已授权账号ID:</span>
  173. <span style={{ color: "#999" }}> {GetAppAccountConfig?.data?.data[key]?.tencentAdAccount?.userActionSets?.accountId || "-"}</span>
  174. </div>
  175. {GetAppAccountConfig?.data?.data[key]?.tencentAdAccount && <div style={{ marginBottom: 5 }}>
  176. <span style={{ width: 100, display: 'inline-block', textAlign: 'right' }}>授权:</span>
  177. <span style={{ color: "#999" }}> <Button type="primary" onClick={auth} size="small" danger>更换授权</Button></span>
  178. </div>}
  179. </div>
  180. </ProCard>
  181. <ProCard
  182. bordered
  183. title="回传设置"
  184. tooltip="回传“下单”“付费”两个行为事件,直投小程序链路额外支持“注册”(纯新+染色回流用户进入小程序)事件回传。"
  185. actions={<Space style={{ margin: 20 }}>
  186. <Button type="primary" onClick={edit}>编辑</Button>
  187. </Space>}
  188. >
  189. <div>
  190. <div style={{ marginBottom: 5 }}>
  191. <span style={{ width: 200, display: 'inline-block', textAlign: 'right' }}>回传类型:</span>
  192. <span style={{ color: "#999" }}> {getEnum("CALLBACK_TYPE", "map")?.get(GetAppAccountConfig?.data?.data[key]?.distributorAppAdBackPolicy?.policyContentOfDefault?.backUnit) || "-"}</span>
  193. </div>
  194. <div style={{ marginBottom: 5 }}>
  195. <span style={{ width: 200, display: 'inline-block', textAlign: 'right' }}>VIP回传策略:</span>
  196. <span style={{ color: "#999" }}>{getEnum("VIP_CALLBACK", "map")?.get(GetAppAccountConfig?.data?.data[key]?.distributorAppAdBackPolicy?.policyContentOfDefault?.vipOrderBackType) || "-"}</span>
  197. </div>
  198. <div style={{ marginBottom: 5 }}>
  199. <span style={{ width: 200, display: 'inline-block', textAlign: 'right' }}>是否补单:</span>
  200. <span style={{ color: "#999" }}>{new Map([true, false].map(key => [key, key ? "是" : "否"])).get(GetAppAccountConfig?.data?.data[key]?.distributorAppAdBackPolicy?.policyContentOfDefault?.markUpOrder) || "-"}</span>
  201. </div>
  202. <div style={{ marginBottom: 5 }}>
  203. <span style={{ width: 200, display: 'inline-block', textAlign: 'right' }}>单用户最大回传订单数:</span>
  204. <span style={{ color: "#999" }}> {GetAppAccountConfig?.data?.data[key]?.distributorAppAdBackPolicy?.policyContentOfDefault?.maxBackCountOfUser || "-"}</span>
  205. </div>
  206. <div style={{ marginBottom: 5 }}>
  207. <span style={{ width: 200, display: 'inline-block', textAlign: 'right' }}>注册充值回传的最大间隔时间:</span>
  208. <span style={{ color: "#999" }}> {GetAppAccountConfig?.data?.data[key]?.distributorAppAdBackPolicy?.policyContentOfDefault?.regPayIntervalTime ? GetAppAccountConfig?.data?.data[key]?.distributorAppAdBackPolicy?.policyContentOfDefault?.regPayIntervalTime + "分钟" : "-"}</span>
  209. </div>
  210. {GetAppAccountConfig?.data?.data[key]?.distributorAppAdBackPolicy?.policyContentOfDefault && <div style={{ marginBottom: 5 }}>
  211. <span style={{ width: 200, display: 'inline-block', textAlign: 'right' }}>更多内容:</span>
  212. <span style={{ color: "#999" }}><Button size='small' type='link' onClick={() => { setReadonly(true); edit() }}>查看</Button></span>
  213. </div>}
  214. </div>
  215. </ProCard>
  216. </ProCard>
  217. {/* 回传配置 */}
  218. <BetaSchemaForm<any>
  219. title={"回传策略配置"}
  220. formRef={formRef}
  221. open={isEdit}
  222. onOpenChange={(b) => {
  223. if (!b) {
  224. setIsEdit(b)
  225. setReadonly(false)
  226. }
  227. }}
  228. layoutType={"ModalForm"}
  229. labelCol={{ span: 24 }}
  230. colProps={{ span: 8 }}
  231. grid={true}
  232. layout="vertical"
  233. onFinish={submit}
  234. columns={formConfig()}
  235. submitter={readonly ? false : { render: (props, dom) => [...dom] }}
  236. modalProps={{
  237. destroyOnClose: true
  238. }}
  239. />
  240. {/* 业务单元授权 */}
  241. <Modal
  242. open={!!accountData}
  243. title={"选择广告账号"}
  244. destroyOnClose
  245. onCancel={() => { setAccountData(null); setAccountId(null) }}
  246. onOk={ad}
  247. >
  248. <Radio.Group onChange={(e) => {
  249. let value = e.target.value
  250. setAccountId(value)
  251. }} value={accountId} >
  252. {
  253. accountData?.accountIdList?.map((id: any) => {
  254. return <Radio key={id} value={id}>{id}</Radio>
  255. })
  256. }
  257. </Radio.Group>
  258. </Modal>
  259. </PageContainer>
  260. }
  261. export default Auth