| 1 | import { create } from 'zustand'; |
| 2 | import { combine } from 'zustand/middleware'; |
| 3 | import { getImgFile, IImgFile } from '../../../../components/Choose/ImgChoose'; |
| 4 | import { IImageAccountItem } from './imagePage.type'; |
| 5 | import lodash from 'lodash'; |
| 6 | import { AccountInfo } from '../../../account/comment'; |
| 7 | import { useVideoPageStore } from '../videoPage/useVideoPageStore'; |
| 8 | import { PlatType } from '../../../../../commont/AccountEnum'; |
| 9 | import { IPubParams } from '../videoPage/videoPage'; |
| 10 | import { message } from 'antd'; |
| 11 | import { accountLogin } from '../../../../icp/account'; |
| 12 | import { ErrPubParamsMapType } from '../../hooks/usePubParamsVerify'; |
| 13 | import { usePubStroe } from '../../../../store/pubStroe'; |
| 14 | import { useAccountStore } from '../../../../store/account'; |
| 15 | |
| 16 | export interface IImagePageStore { |
| 17 | // 账户数据和对应参数 |
| 18 | imageAccounts: IImageAccountItem[]; |
| 19 | // 选择的图片数据 |
| 20 | images: IImgFile[]; |
| 21 | // 每个平台当前选择的账户 |
| 22 | platActiveAccountMap: Map<PlatType, IImageAccountItem>; |
| 23 | // 当前选择的平台 |
| 24 | activePlat?: PlatType; |
| 25 | // 通用参数 |
| 26 | commonPubParams: IPubParams; |
| 27 | // 图片上传上限 |
| 28 | imgUploadLimit: number; |
| 29 | /** |
| 30 | * 这个属性在 imagePage/page.tsx 中进行计算,然后实时的放到这里 |
| 31 | * 因为别的组件需要这个属性,为了使其只计算一次,特此放到store引用 |
| 32 | */ |
| 33 | errParamsMap?: ErrPubParamsMapType; |
| 34 | warnParamsMap?: ErrPubParamsMapType; |
| 35 | } |
| 36 | |
| 37 | const store: IImagePageStore = { |
| 38 | imgUploadLimit: 35, |
| 39 | images: [], |
| 40 | imageAccounts: [], |
| 41 | platActiveAccountMap: new Map<PlatType, IImageAccountItem>(), |
| 42 | activePlat: undefined, |
| 43 | commonPubParams: useVideoPageStore.getState().pubParamsInit(), |
| 44 | errParamsMap: undefined, |
| 45 | warnParamsMap: undefined, |
| 46 | }; |
| 47 | |
| 48 | const getStore = () => { |
| 49 | return lodash.cloneDeep(store); |
| 50 | }; |
| 51 | |
| 52 | // 视频发布所有组件的共享状态和方法 |
| 53 | export const useImagePageStore = create( |
| 54 | combine( |
| 55 | { |
| 56 | ...getStore(), |
| 57 | }, |
| 58 | (_set, get, storeApi) => { |
| 59 | const set = (data: Partial<IImagePageStore>) => { |
| 60 | _set(data); |
| 61 | const oldData = get(); |
| 62 | if ( |
| 63 | (data.hasOwnProperty('imageAccounts') && |
| 64 | data.imageAccounts!.length !== 0) || |
| 65 | (data.hasOwnProperty('images') && data.images!.length !== 0) || |
| 66 | (data.hasOwnProperty('commonPubParams') && |
| 67 | oldData['imageAccounts'] && |
| 68 | oldData['imageAccounts']?.length !== 0) |
| 69 | ) { |
| 70 | usePubStroe.getState().setImgTextPubSaveData(get()); |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | const methods = { |
| 75 | setPlatActiveAccountMap( |
| 76 | platActiveAccountMap: Map<PlatType, IImageAccountItem>, |
| 77 | ) { |
| 78 | set({ |
| 79 | platActiveAccountMap, |
| 80 | }); |
| 81 | }, |
| 82 | setErrParamsMap( |
| 83 | errParamsMap: ErrPubParamsMapType, |
| 84 | warnParamsMap: ErrPubParamsMapType, |
| 85 | ) { |
| 86 | set({ |
| 87 | errParamsMap, |
| 88 | warnParamsMap, |
| 89 | }); |
| 90 | }, |
| 91 | setActivePlat(activePlat: PlatType) { |
| 92 | set({ |
| 93 | activePlat, |
| 94 | }); |
| 95 | }, |
| 96 | // 设置图片 |
| 97 | setImages(images: IImgFile[]) { |
| 98 | set({ |
| 99 | images, |
| 100 | }); |
| 101 | }, |
| 102 | |
| 103 | // 恢复临时图文记录 |
| 104 | async setTempSaveParams({ |
| 105 | images, |
| 106 | commonPubParams, |
| 107 | imageAccounts, |
| 108 | }: { |
| 109 | images?: IImgFile[]; |
| 110 | commonPubParams?: IPubParams; |
| 111 | imageAccounts?: IImageAccountItem[]; |
| 112 | }) { |
| 113 | if (images) { |
| 114 | const tasks: Promise<IImgFile>[] = []; |
| 115 | for (const image of images) { |
| 116 | tasks.push(getImgFile(image.imgPath)); |
| 117 | } |
| 118 | const imgFiles = await Promise.all(tasks); |
| 119 | set({ |
| 120 | images: imgFiles, |
| 121 | }); |
| 122 | } |
| 123 | |
| 124 | if (imageAccounts) { |
| 125 | const accountList = useAccountStore.getState().accountList; |
| 126 | imageAccounts = imageAccounts.map((v) => { |
| 127 | v.account = accountList.find( |
| 128 | (account) => v.account?.id === account.id, |
| 129 | )!; |
| 130 | return v; |
| 131 | }); |
| 132 | set({ |
| 133 | imageAccounts, |
| 134 | }); |
| 135 | } |
| 136 | |
| 137 | if (commonPubParams) { |
| 138 | set({ |
| 139 | commonPubParams, |
| 140 | }); |
| 141 | } |
| 142 | }, |
| 143 | |
| 144 | /** |
| 145 | * 账户重新登录。登录成功后会自动更新该条账户数据 |
| 146 | */ |
| 147 | async accountRestart(pType: PlatType) { |
| 148 | const res = await accountLogin(pType); |
| 149 | if (!res) return; |
| 150 | message.success('登录成功!'); |
| 151 | // 更新此条账户数据 |
| 152 | methods.updateAccounts([res]); |
| 153 | }, |
| 154 | |
| 155 | // 添加图片 |
| 156 | addImages(images: IImgFile[]) { |
| 157 | if (images.length + get().images.length > get().imgUploadLimit) { |
| 158 | return message.warning(`最多上传${get().imgUploadLimit}张图片!`); |
| 159 | } |
| 160 | set({ |
| 161 | images: [...get().images, ...images], |
| 162 | }); |
| 163 | }, |
| 164 | |
| 165 | // 设置所有参数 |
| 166 | setAllPubParams(pubParmas: IPubParams) { |
| 167 | const imageAccounts = [...get().imageAccounts]; |
| 168 | |
| 169 | imageAccounts.map((v) => { |
| 170 | // 替换 |
| 171 | Object.keys(pubParmas).map((key) => { |
| 172 | if (pubParmas.hasOwnProperty(key)) { |
| 173 | v.pubParams[key as 'topics'] = pubParmas[key as 'topics']; |
| 174 | } |
| 175 | }); |
| 176 | }); |
| 177 | set({ |
| 178 | imageAccounts, |
| 179 | }); |
| 180 | }, |
| 181 | |
| 182 | // 设置通用发布参数 |
| 183 | setCommonPubParams(pubParmas: IPubParams) { |
| 184 | const commonPubParams = { ...get().commonPubParams }; |
| 185 | const imageAccounts = [...get().imageAccounts]; |
| 186 | |
| 187 | for (const key in commonPubParams) { |
| 188 | if (pubParmas.hasOwnProperty(key)) { |
| 189 | const keyType = key as 'title'; |
| 190 | const val = pubParmas[keyType]; |
| 191 | commonPubParams[keyType] = pubParmas[keyType]; |
| 192 | imageAccounts.map((v) => { |
| 193 | v.pubParams[keyType] = val; |
| 194 | }); |
| 195 | } |
| 196 | } |
| 197 | set({ |
| 198 | commonPubParams, |
| 199 | imageAccounts, |
| 200 | }); |
| 201 | }, |
| 202 | |
| 203 | // 按照账号id删除 |
| 204 | delAccountById(accountId: number) { |
| 205 | const imageAccounts = get().imageAccounts.filter( |
| 206 | (v) => v.account.id !== accountId, |
| 207 | ); |
| 208 | set({ |
| 209 | imageAccounts, |
| 210 | }); |
| 211 | }, |
| 212 | |
| 213 | // 按平台删除 |
| 214 | delAccountByPalt(accountType: PlatType) { |
| 215 | const imageAccounts = get().imageAccounts.filter( |
| 216 | (v) => v.account.type !== accountType, |
| 217 | ); |
| 218 | set({ |
| 219 | imageAccounts, |
| 220 | }); |
| 221 | }, |
| 222 | |
| 223 | // 添加账户 |
| 224 | addAccount(accounts: AccountInfo[]) { |
| 225 | let imageAccounts = [...get().imageAccounts]; |
| 226 | // 新增账户 |
| 227 | const accountSet = new Set<number>(accounts.map((v) => v.id)); |
| 228 | // 已有账户 |
| 229 | const existAccountSet = new Set<number>( |
| 230 | imageAccounts.map((v) => v.account.id), |
| 231 | ); |
| 232 | // 要添加到数据的账户 |
| 233 | const notAddAccount: AccountInfo[] = []; |
| 234 | |
| 235 | // 过滤掉新增账户中的 已有账户 |
| 236 | for (const account of accounts) { |
| 237 | if (!existAccountSet.has(account.id)) notAddAccount.push(account); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * 新增账户没有的账户 |
| 242 | * 但是已有账户有,那么过滤 |
| 243 | */ |
| 244 | imageAccounts = imageAccounts.filter((v) => |
| 245 | accountSet.has(v.account.id), |
| 246 | ); |
| 247 | |
| 248 | // 根据账户添加数据 |
| 249 | for (const account of notAddAccount) { |
| 250 | imageAccounts.push({ |
| 251 | account, |
| 252 | pubParams: lodash.cloneDeep(get().commonPubParams), |
| 253 | }); |
| 254 | } |
| 255 | |
| 256 | set({ |
| 257 | imageAccounts, |
| 258 | }); |
| 259 | }, |
| 260 | |
| 261 | // 设置单条数据的参数 |
| 262 | setOnePubParams(pubParmas: IPubParams, id: number) { |
| 263 | const imageAccounts = [...get().imageAccounts]; |
| 264 | const findedData = imageAccounts.find((v) => v.account.id === id); |
| 265 | if (!findedData) return; |
| 266 | |
| 267 | for (const key in pubParmas) { |
| 268 | if (pubParmas.hasOwnProperty(key)) { |
| 269 | findedData.pubParams[key as 'title'] = pubParmas[key as 'title']; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | set({ imageAccounts }); |
| 274 | }, |
| 275 | |
| 276 | // 更新用户数据 |
| 277 | updateAccounts(accounts: AccountInfo[]) { |
| 278 | const imageAccounts = [...get().imageAccounts]; |
| 279 | const imageTextMap = new Map<number, IImageAccountItem>(); |
| 280 | |
| 281 | imageAccounts.map((v) => { |
| 282 | imageTextMap.set(v.account.id || 0, v); |
| 283 | }); |
| 284 | |
| 285 | accounts.map((v) => { |
| 286 | imageTextMap.get(v.id)!.account = v; |
| 287 | }); |
| 288 | |
| 289 | set({ imageAccounts }); |
| 290 | }, |
| 291 | |
| 292 | // 清除本store所有数据 |
| 293 | clear() { |
| 294 | set({ |
| 295 | ...getStore(), |
| 296 | }); |
| 297 | }, |
| 298 | }; |
| 299 | return methods; |
| 300 | }, |
| 301 | ), |
| 302 | ); |
| 303 |