client_modal.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. {{define "clientsModal"}}
  2. <a-modal id="client-modal" v-model="clientModal.visible" :title="clientModal.title" @ok="clientModal.ok"
  3. :confirm-loading="clientModal.confirmLoading" :closable="true" :mask-closable="false"
  4. :class="themeSwitcher.darkCardClass"
  5. :ok-text="clientModal.okText" cancel-text='{{ i18n "close" }}'>
  6. {{template "form/client"}}
  7. </a-modal>
  8. <script>
  9. const clientModal = {
  10. visible: false,
  11. confirmLoading: false,
  12. title: '',
  13. okText: '',
  14. isEdit: false,
  15. dbInbound: new DBInbound(),
  16. inbound: new Inbound(),
  17. clients: [],
  18. clientStats: [],
  19. oldClientId: "",
  20. index: null,
  21. clientIps: null,
  22. delayedStart: false,
  23. ok() {
  24. if (clientModal.isEdit) {
  25. ObjectUtil.execute(clientModal.confirm, clientModalApp.client, clientModal.dbInbound.id, clientModal.oldClientId);
  26. } else {
  27. ObjectUtil.execute(clientModal.confirm, clientModalApp.client, clientModal.dbInbound.id);
  28. }
  29. },
  30. show({ title = '', okText = '{{ i18n "sure" }}', index = null, dbInbound = null, confirm = () => { }, isEdit = false }) {
  31. this.visible = true;
  32. this.title = title;
  33. this.okText = okText;
  34. this.isEdit = isEdit;
  35. this.dbInbound = new DBInbound(dbInbound);
  36. this.inbound = dbInbound.toInbound();
  37. this.clients = this.getClients(this.inbound.protocol, this.inbound.settings);
  38. this.index = index === null ? this.clients.length : index;
  39. this.delayedStart = false;
  40. if (isEdit) {
  41. if (this.clients[index].expiryTime < 0) {
  42. this.delayedStart = true;
  43. }
  44. this.oldClientId = this.getClientId(dbInbound.protocol, clients[index]);
  45. } else {
  46. this.addClient(this.inbound.protocol, this.clients);
  47. }
  48. this.clientStats = this.dbInbound.clientStats.find(row => row.email === this.clients[this.index].email);
  49. this.confirm = confirm;
  50. },
  51. getClients(protocol, clientSettings) {
  52. switch (protocol) {
  53. case Protocols.VMESS: return clientSettings.vmesses;
  54. case Protocols.VLESS: return clientSettings.vlesses;
  55. case Protocols.TROJAN: return clientSettings.trojans;
  56. case Protocols.SHADOWSOCKS: return clientSettings.shadowsockses;
  57. default: return null;
  58. }
  59. },
  60. getClientId(protocol, client) {
  61. switch (protocol) {
  62. case Protocols.TROJAN: return client.password;
  63. case Protocols.SHADOWSOCKS: return client.email;
  64. default: return client.id;
  65. }
  66. },
  67. addClient(protocol, clients) {
  68. switch (protocol) {
  69. case Protocols.VMESS: return clients.push(new Inbound.VmessSettings.Vmess());
  70. case Protocols.VLESS: return clients.push(new Inbound.VLESSSettings.VLESS());
  71. case Protocols.TROJAN: return clients.push(new Inbound.TrojanSettings.Trojan());
  72. case Protocols.SHADOWSOCKS: return clients.push(new Inbound.ShadowsocksSettings.Shadowsocks(clients[0].method));
  73. default: return null;
  74. }
  75. },
  76. close() {
  77. clientModal.visible = false;
  78. clientModal.loading(false);
  79. },
  80. loading(loading) {
  81. clientModal.confirmLoading = loading;
  82. },
  83. };
  84. const clientModalApp = new Vue({
  85. delimiters: ['[[', ']]'],
  86. el: '#client-modal',
  87. data: {
  88. clientModal,
  89. get inbound() {
  90. return this.clientModal.inbound;
  91. },
  92. get client() {
  93. return this.clientModal.clients[this.clientModal.index];
  94. },
  95. get clientStats() {
  96. return this.clientModal.clientStats;
  97. },
  98. get isEdit() {
  99. return this.clientModal.isEdit;
  100. },
  101. get isTrafficExhausted() {
  102. if (!clientStats) return false
  103. if (clientStats.total <= 0) return false
  104. if (clientStats.up + clientStats.down < clientStats.total) return false
  105. return true
  106. },
  107. get isExpiry() {
  108. return this.clientModal.isEdit && this.client.expiryTime >0 ? (this.client.expiryTime < new Date().getTime()) : false;
  109. },
  110. get statsColor() {
  111. return usageColor(clientStats.up + clientStats.down, app.trafficDiff, this.client.totalGB);
  112. },
  113. get delayedExpireDays() {
  114. return this.client && this.client.expiryTime < 0 ? this.client.expiryTime / -86400000 : 0;
  115. },
  116. set delayedExpireDays(days) {
  117. this.client.expiryTime = -86400000 * days;
  118. },
  119. },
  120. methods: {
  121. async getDBClientIps(email) {
  122. const msg = await HttpUtil.post(`/panel/inbound/clientIps/${email}`);
  123. if (!msg.success) {
  124. document.getElementById("clientIPs").value = msg.obj;
  125. return;
  126. }
  127. let ips = msg.obj;
  128. if (typeof ips === 'string' && ips.startsWith('[') && ips.endsWith(']')) {
  129. try {
  130. ips = JSON.parse(ips);
  131. ips = Array.isArray(ips) ? ips.join("\n") : ips;
  132. } catch (e) {
  133. console.error('Error parsing JSON:', e);
  134. }
  135. }
  136. document.getElementById("clientIPs").value = ips;
  137. },
  138. async clearDBClientIps(email) {
  139. try {
  140. const msg = await HttpUtil.post(`/panel/inbound/clearClientIps/${email}`);
  141. if (!msg.success) {
  142. return;
  143. }
  144. document.getElementById("clientIPs").value = "";
  145. } catch (error) {
  146. }
  147. },
  148. resetClientTraffic(email, dbInboundId, iconElement) {
  149. this.$confirm({
  150. title: '{{ i18n "pages.inbounds.resetTraffic"}}',
  151. content: '{{ i18n "pages.inbounds.resetTrafficContent"}}',
  152. class: themeSwitcher.darkCardClass,
  153. okText: '{{ i18n "reset"}}',
  154. cancelText: '{{ i18n "cancel"}}',
  155. onOk: async () => {
  156. iconElement.disabled = true;
  157. const msg = await HttpUtil.postWithModal('/panel/inbound/' + dbInboundId + '/resetClientTraffic/' + email);
  158. if (msg.success) {
  159. this.clientModal.clientStats.up = 0;
  160. this.clientModal.clientStats.down = 0;
  161. }
  162. iconElement.disabled = false;
  163. },
  164. })
  165. },
  166. },
  167. });
  168. </script>
  169. {{end}}