core.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. async function get_api_data(url, data, type = null, para_cache = false) {
  2. if (isEmpty(type)) {
  3. return await get_main_api_data(url, data, para_cache)
  4. } else {
  5. return await get_app_api_data(url, data, type, para_cache)
  6. }
  7. }
  8. async function get_strong_api_data(strong_type, url, data) {
  9. return await get_main_api_data(url, data, false, false, strong_type)
  10. }
  11. async function get_main_api_data(url, data, para_cache, encode_status = false, strong_type = false) {
  12. const is_user_page = window.location.href.includes('/dabory/erp')
  13. if (is_user_page) {
  14. $('#spinner-processing').show()
  15. }
  16. const response = await axios.post('/ajax/get-data', {
  17. url: url,
  18. data: data,
  19. is_user_page: is_user_page,
  20. para_cache: para_cache,
  21. encode_status: encode_status,
  22. strong_type: strong_type,
  23. });
  24. if (is_user_page) {
  25. $('#spinner-processing').hide()
  26. }
  27. // console.log(response)
  28. // return response
  29. // console.log(window.User)
  30. // console.log(window.Member)
  31. if (! isEmpty(response.data['apiStatus'])) {
  32. // console.log(url)
  33. // console.log(data)
  34. // console.log(response)
  35. error_response(response.data['apiStatus'], response.data['body'])
  36. }
  37. return response
  38. }
  39. async function get_app_api_data(url, data, type, para_cache) {
  40. const AppToken = await call_local_api('/find-gate-token', { app_name: type } )
  41. if (! AppToken.data) {
  42. return
  43. }
  44. const is_user_page = window.location.href.includes('/dabory/erp')
  45. if (is_user_page) {
  46. $('#spinner-processing').show()
  47. }
  48. const response = await axios.post('/ajax/get-data', {
  49. url: AppToken.data['ApiUrl'] + '/' + url,
  50. type: 'custom',
  51. data: data,
  52. is_user_page: is_user_page,
  53. headers: {
  54. GateToken: AppToken.data['GateToken']
  55. },
  56. para_cache: para_cache,
  57. encode_status: false
  58. });
  59. if (is_user_page) {
  60. $('#spinner-processing').hide()
  61. }
  62. const status_code = response.data['apiStatus']
  63. if (! isEmpty(status_code)) {
  64. if (status_code === '555') {
  65. return window.location.href = '/505'
  66. }
  67. iziToast.error({
  68. title: type + 'DB -> status code: ' + status_code,
  69. message: response.data['body'] ?? $('#api-request-failed-please-check').text(),
  70. });
  71. }
  72. // console.log(response)
  73. return response
  74. }
  75. async function call_local_api(url, data, headers = {}) {
  76. const response = await axios.post(url, data, {
  77. headers: headers
  78. })
  79. if (! isEmpty(response.data['apiStatus'])) {
  80. // console.log(url)
  81. // console.log(data)
  82. // console.log(response)
  83. error_response(response.data['apiStatus'], response.data['body'])
  84. }
  85. return response
  86. }
  87. function error_response(status_code, message) {
  88. switch ( status_code ) {
  89. // default:
  90. case 503:
  91. window.location.href = '/503'
  92. break
  93. case 505:
  94. case 555:
  95. window.location.href = '/505'
  96. break
  97. case 506:
  98. window.location.href = '/506'
  99. break
  100. case 600:
  101. window.location.href = '/600'
  102. break
  103. }
  104. // iziToast.error({
  105. // title: 'status code: ' + status_code,
  106. // message: message,
  107. // });
  108. return true
  109. }
  110. function get_cache_api_data(api_name, callback, menu_code, query_name = false) {
  111. $.ajaxSetup({
  112. headers: {
  113. 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  114. }
  115. });
  116. $.ajax({
  117. url: "/cache-api",
  118. type:'POST',
  119. data: {
  120. menu_code: menu_code,
  121. api_name: api_name,
  122. query_name: query_name,
  123. },
  124. cache: false,
  125. success: function(cacheData) {
  126. callback(cacheData)
  127. }
  128. });
  129. }
  130. function get_blades_html(path_to_blade, data, callback,
  131. key = 'moealSetFile', class_name = []) {
  132. $.ajaxSetup({
  133. headers: {
  134. 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  135. }
  136. });
  137. $.ajax({
  138. url: "/blades",
  139. type:'POST',
  140. data: {
  141. path_to_blade: path_to_blade,
  142. data: data,
  143. key: key,
  144. class_name: class_name,
  145. },
  146. cache: false,
  147. success: function(html) {
  148. // let array = path_to_blade.split('.');
  149. // let id = '#modal-' + array[array.length - 1];
  150. // if ( ! isEmpty( $(id).html() ) ) return;
  151. // console.log(html)
  152. callback(html)
  153. }
  154. });
  155. }
  156. async function get_para_data(para_type, path_to_para,
  157. bpa = undefined, theme_dir = undefined) {
  158. return await axios.post('/paras', {
  159. para_type: para_type,
  160. path_to_para: path_to_para,
  161. bpa: bpa,
  162. theme_dir: theme_dir
  163. });
  164. }
  165. async function call_slip_form_book(url, query_name, filter_value, menuCode) {
  166. let response = await get_api_data(url,
  167. {
  168. QueryVars: {
  169. QueryName: query_name,
  170. FilterValue: filter_value
  171. }
  172. }, null, menuCode);
  173. return response
  174. }
  175. async function get_slip_common_setup_for(brand_code) {
  176. return await get_api_data('setup-get', {
  177. 'SetupCode': 'slip-common',
  178. 'BrandCode': brand_code,
  179. })
  180. }
  181. async function make_slip_no(brand_code, table_name) {
  182. const slip_common = await get_slip_common_setup_for(brand_code)
  183. if (slip_common.data['IsNewRecAutoSlipNo']) {
  184. const last_slip_no_get = await get_api_data('last-slip-no-get', {
  185. 'TableName': 'item-yw',
  186. 'YYMMDD': moment(new Date()).format('YYMMDD'),
  187. })
  188. return slip_common.data['SlipPrefix'] +
  189. moment(new Date()).format(slip_common.data['YymmddFormat']) + '-'
  190. + last_slip_no_get.data.LastSlipNo.padStart(slip_common.data['SerialDigit'], '0')
  191. }
  192. return false;
  193. }
  194. function route(url) {
  195. if (self !== top) {
  196. window.top.location.href = url
  197. } else {
  198. window.location.href = url
  199. }
  200. }