webApi.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import request from '@/utils/axiosReq'
  2. import { CommonQueryResult } from '~/common'
  3. export function Query<T = any>(params): Promise<CommonQueryResult<T>> {
  4. if (NEW_DATACONTENT_ID.includes(params.id)) {
  5. return NewQuery<T>(params)
  6. }
  7. return request({
  8. url: '/openApi/query',
  9. method: 'post',
  10. data: params,
  11. })
  12. }
  13. function NewQuery<T = any>(params): Promise<CommonQueryResult<T>> {
  14. return request({
  15. url: '/newOpenApi/search',
  16. method: 'post',
  17. data: params,
  18. })
  19. }
  20. export async function myQuery(id, ...dataContent) {
  21. const params = {
  22. id,
  23. dataContent,
  24. }
  25. try {
  26. const result = await Query(params)
  27. if (Number(result.code) === 0) {
  28. return result.returnData.listValues
  29. } else {
  30. return Promise.reject(result.message)
  31. }
  32. } catch (error) {
  33. return Promise.reject(error)
  34. }
  35. }
  36. export async function htQuery(id, ...dataContent) {
  37. const params = {
  38. id,
  39. dataContent,
  40. }
  41. try {
  42. const result = await Query(params)
  43. if (Number(result.code) === 0) {
  44. return result.returnData
  45. } else {
  46. return []
  47. }
  48. } catch (error) {
  49. return []
  50. }
  51. }
  52. function NewGeneralDataReception(func: any) {
  53. return func
  54. }
  55. export function GeneralDataReception(params): Promise<CommonQueryResult> {
  56. const { serviceId, dataContent } = params
  57. if (NEW_SERVICE_ID.includes(serviceId)) {
  58. const newData = JSON.parse(dataContent)
  59. const { event } = newData
  60. const eventType = event ?? params.event
  61. let key: any = null
  62. if (newData?.event) {
  63. delete newData.event
  64. }
  65. if (eventType == 2) {
  66. if (newData.hasOwnProperty('IDKey')) {
  67. key = newData['IDKey']
  68. } else {
  69. for (const item in newData) {
  70. if (
  71. item.includes('id') ||
  72. item.includes('Id') ||
  73. item.includes('ID')
  74. ) {
  75. key = item
  76. }
  77. }
  78. }
  79. return NewGeneralDataReception(
  80. dataToChange(serviceId, newData, eventType, key)
  81. )
  82. } else {
  83. if (newData?.IDKey) {
  84. delete newData.IDKey
  85. }
  86. return NewGeneralDataReception(
  87. dataToChange(serviceId, newData, eventType)
  88. )
  89. }
  90. }
  91. return request({
  92. url: '/openApi/generalDataReception',
  93. method: 'post',
  94. data: params,
  95. })
  96. }
  97. //增/删/改
  98. export async function dataToChange(
  99. id: number,
  100. data: any,
  101. event: number,
  102. key?: string
  103. ) {
  104. try {
  105. const params = {
  106. serviceId: id,
  107. dataContent: formatChange(data, event, key),
  108. event: `${event}`,
  109. // authId: this.AauthId
  110. }
  111. const { code, message } = await sendData(params)
  112. return { code, message }
  113. } catch (error) {
  114. return { code: 1, message: error }
  115. }
  116. }
  117. function sendData(params) {
  118. return request({
  119. url: '/externalApi/openApi/sendData',
  120. method: 'post',
  121. data: params,
  122. })
  123. }
  124. //新增
  125. export function newData(params) {
  126. return request({
  127. url: '/externalApi/openApi/sendData',
  128. method: 'post',
  129. data: params,
  130. })
  131. }
  132. //修改
  133. export function modifyData(params) {
  134. return request({
  135. url: '/externalApi/openApi/sendData',
  136. method: 'post',
  137. data: params,
  138. })
  139. }
  140. //删除movedata
  141. export function moveData(params) {
  142. return request({
  143. url: '/externalApi/openApi/sendData',
  144. method: 'post',
  145. data: params,
  146. })
  147. }
  148. function formatChange(data, event, key) {
  149. const datas: any = []
  150. const flag = Array.isArray(data)
  151. if (flag) {
  152. for (let i = 0; i < data.length; i++) {
  153. let obj: any = {}
  154. if (event == 1) {
  155. obj.Value = data[i]
  156. } else if (event == 2) {
  157. obj.Value = data[i]
  158. obj.filter = {}
  159. if (Array.isArray(key)) {
  160. const keys = <any>[]
  161. const vals = <any>[]
  162. obj.filter = []
  163. for (let j = 0; j < key.length; j++) {
  164. const dep = key[i]
  165. keys.push(Object.keys(dep))
  166. vals.push(Object.values(dep))
  167. }
  168. const nobj = {
  169. left: '(',
  170. column: `${keys[i]}`,
  171. comparator: '=',
  172. value: `${vals[i]}`,
  173. right: ')',
  174. connector: 'and',
  175. }
  176. obj.filter.push(nobj)
  177. if (obj.Value.hasOwnProperty(keys[i])) {
  178. delete obj.Value[keys[i]]
  179. }
  180. } else {
  181. obj.filter[key] = data[i][key]
  182. if (obj.Value[key]) {
  183. delete obj.Value[key]
  184. }
  185. }
  186. } else {
  187. obj = data[i]
  188. }
  189. datas.push(obj)
  190. }
  191. } else {
  192. let obj: any = {}
  193. if (event == 1) {
  194. obj.Value = data
  195. } else if (event == 2) {
  196. obj.Value = data
  197. obj.filter = [
  198. {
  199. left: '(',
  200. column: `${key}`,
  201. comparator: '=',
  202. value: `${data[key]}`,
  203. right: ')',
  204. connector: 'and',
  205. },
  206. ]
  207. if (obj.Value[key]) {
  208. delete obj.Value[key]
  209. }
  210. } else {
  211. obj = data
  212. }
  213. datas.push(obj)
  214. }
  215. return datas
  216. }