APP page interception method, system and device based on user-defined annotation and medium

By adopting a custom -based interceptor technology in the APP, the problem of complexity and code flexibility in multiple interception scenarios in existing technologies is solved, and the simplicity and maintenance of the code are realized.

CN119382923APending Publication Date: 2025-01-28XIAMEN LEELEN TECH CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202411275182.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-12
Publication Date
2025-01-28

AI Technical Summary

Technical Problem

The existing technology is complex in multiple interception scenarios and lacks code flexibility, resulting in increased development and maintenance complexity, reduced interface code readability, and high maintenance costs.

Method used

Adopt the APP page intercepting method based on custom annotation, through constructing a universal interface and custom annotation interceptor by constructing the interception conditions, the packaging of the first execution method, and the second execution method, reduce the code coupling and improve the internal agglomeration properties Essence

Benefits of technology

Simplify the writing of multiple interception logic, reduce the complexity and maintenance cost of code, and improve the readability and maintenance of the code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119382923A_ABST
    Figure CN119382923A_ABST
Patent Text Reader

Abstract

The invention discloses an APP page interception method, system and device based on user-defined annotations and a medium, the method comprises the following steps: constructing an interceptor universal interface, the interceptor universal interface comprises an interception condition determination method, a first execution method and a request code, and the first execution method is an execution method when conditions are not satisfied; constructing a user-defined annotation interceptor, wherein the user-defined annotation interceptor realizes an interceptor universal interface, rewrites a request code of a corresponding annotation in the user-defined annotation interceptor, and determines an interception condition and a corresponding first execution method; the user-defined annotation interceptors are added into a class, needing an interception function, of the application, a management class is added into a base class of the application, an interceptor set is created in the management class, the interceptors needing interception are added into the interceptor set, and the logic processing method of the interface serves as a second execution method. The interception method provided by the invention can effectively improve cohesion, readability and maintainability of codes.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of Internet technology, and in particular relates to an APP page interception method, system, device and medium based on custom annotations. Background Art

[0002] With the rapid development of mobile internet, various mobile applications (APPs) have become widely used in daily life. To enhance the user experience, many APPs offer a guest mode, allowing users to access some basic functions without registering or logging in. However, to protect user privacy and security, or for commercial purposes, some advanced features often require users to log in before accessing them. When a user attempts to use these advanced features, the system typically first checks the user's login status. If the user is not logged in, the system forces the user to jump to the login screen, complete identity verification, and then return to the previous operation screen.

[0003] To implement this process, lengthy code is typically required at each entry point for login verification to verify the user's status and handle the login redirect. This approach not only increases development and maintenance complexity but also introduces redundant code, reducing system maintainability and scalability. Existing technologies include message callback solutions and intent solutions. The message callback solution is essentially implemented through a message bus, using a management class to send and receive messages and executing them through callbacks. First, a message management class is defined, and the class that needs to intercept logins implements the interception interface. After a successful login, the management class sends a message to the intercepted class, which then determines whether the login was successful and executes the corresponding logic. The intent solution, on the interface requiring login interception, suspends the corresponding logic processing and then determines whether the user is logged in. If so, the subsequent logic is executed. Otherwise, the login page is opened using the startActivityForResult method. After a successful login, the login page is closed using the setResult method and the login result information is returned to the intercepted interface. The intercepted interface then uses onActivityResult to retrieve the data from the login page and determine whether the login was successful. If the login was successful, the corresponding logic code is executed.

[0004] For example, the Chinese invention patent application with publication number CN115630354A discloses a method for implementing iOS login status interception verification on a mobile terminal, which includes the following steps: S1. Create a category file for the button control UIButton, name the category file interceptor, and obtain the interceptor category file UIButton+interceptor; S2. Through the category file UIButton+interceptor, go to the category file to implement the declaration and implementation of the Boolean attribute isNeedLogin of whether login verification is required; S3. Through the category file UIButton+interceptor, go to the category file implementation class implementation interval to implement method exchange; S4. Through the category file UIButton+interceptor, go to the category file implementation class to complete the definition of the custom method sendCustomAction in the implementation interval and login status verification; S5. Complete the implementation of the interception and verification functions, and go to the project for specific use.

[0005] During the use of the APP, there are also the following scenarios: Scenario 1, if there are many relatively advanced entrances, these logics need to be written everywhere, which not only takes a lot of time to write these identical codes, but also reduces the readability of the interface. Scenario 2, if a certain interface needs to judge whether multiple advanced functions (such as whether to log in, whether to be a member) are met before entering, then it is necessary to add multiple advanced function judgments to the current page, and it is necessary to add a large amount of logical judgment and processing code to the current page. If there are multiple such pages, it will take a lot of time to generate these repeated codes. In the above scenario, it is necessary to write a large amount of logical judgment code on the page before the interception interface, which reduces the readability of the interface code and greatly increases the cost of subsequent maintenance. Summary of the Invention

[0006] The present invention provides an APP page interception method and system based on custom annotations, aiming to solve the problems of the existing technology such as the complexity of multiple interception scenarios and insufficient code flexibility.

[0007] To solve the above technical problems, the present invention proposes an APP page interception method based on custom annotations, comprising the following steps:

[0008] Constructing an interceptor general interface, the interceptor general interface includes an interception condition determination method, a first execution method and a request code, wherein the first execution method is an execution method when the condition is not met;

[0009] Construct a custom annotation interceptor, which implements the interceptor general interface and rewrites the request code, interception condition determination and corresponding first execution method of the corresponding annotation in the custom annotation interceptor;

[0010] Add the custom annotation interceptor to the class that requires interception function;

[0011] Among them, the interceptor management class is added to the BaseActivity base class of the application, an interceptor set is created in the management class, and the logic processing method of the interface is used as the second execution method, which is the execution method when the conditions are met.

[0012] Preferably, the interceptor management class scans all interceptors of the target class. If an interceptor is added to the target class, the instance of the annotation class is obtained through reflection and added to the collection; it loops to determine whether the interception condition determination method of the interceptor in the collection is met. If not, the traversal is ended, the first execution method is started, and the corresponding interface is jumped to for processing. After processing, it returns to the current interface for loop judgment until all interceptors are met and the second execution method is executed.

[0013] Preferably, one or more custom interceptors can be added to the class requiring special functions.

[0014] Preferably, the interception conditions include user login status, user permissions or roles, network status, application version, function switch, device status, session timeout, form data verification, geographic location restriction, application background service status and time limit.

[0015] Preferably, the interceptor management class saves and maintains an interceptor set in the form of a list or a mapping table, and the interceptor set includes all interceptor instances that currently need to be managed.

[0016] Preferably, the request code is a unique identifier for identifying different interceptor instances.

[0017] Preferably, the implementation method of the interceptor general interface is:

[0018] When defining a custom annotation interceptor, declare and implement the interceptor common interface;

[0019] In the custom annotation interceptor class, rewrite the interception condition determination method and the first execution method, and set the request code for each instance;

[0020] Add custom annotations to methods or classes that require login verification. The interceptor management class detects the custom annotations through the reflection mechanism and executes the corresponding interception logic.

[0021] The present invention also proposes an APP page interception system based on custom annotations, including:

[0022] The interceptor general interface includes an interception condition determination method, a first execution method, and a request code, wherein the first execution method is an execution method when the condition is not met;

[0023] Access custom annotations, adding the class name of the interceptor general interface or the subclass class name of the interceptor general interface to the required interface;

[0024] The interceptor management class is added to the BaseActivity base class, and the logic processing method of the interface is used as the second execution method to scan and manage whether all methods in the target class contain interceptor annotations.

[0025] The present invention also proposes a computer device, comprising a memory, a processor, and a computer program stored in the memory, wherein the processor executes the computer program to implement any one of the steps in the custom annotation-based APP page interception method.

[0026] The present invention also provides a computer-readable storage medium, on which a computer program is stored, characterized in that when the computer program is loaded by a processor, it can execute any one of the steps of the APP page interception method based on custom annotations.

[0027] Compared with the prior art, the present invention has the following technical effects:

[0028] The interception method proposed in the present invention adds multiple annotation interceptors to the interface to be used, and the corresponding logical processing is encapsulated in the interceptor. There is no need to make a large amount of logical code judgment on the current page and the advanced permission page, which reduces the coupling degree of the code; when the corresponding advanced permission logic changes, it is only necessary to change the logic code of the interceptor, which improves the cohesion, readability and maintainability of the code. BRIEF DESCRIPTION OF THE DRAWINGS

[0029] Figure 1 It is a schematic diagram of a single interceptor flow chart of the interception method of the present invention;

[0030] Figure 2 It is a schematic diagram of the multiple interceptor flow of the interception method described in the present invention. DETAILED DESCRIPTION

[0031] In order to make the objectives, technical solutions and advantages of the present invention more clear, the technical solutions of the present invention will be clearly and completely described below in combination with specific embodiments of the present application and with reference to the accompanying drawings.

[0032] In an embodiment of the present invention, an interceptor is a software design pattern used to intercept or capture specific operations or events during program execution. It can perform customized processing before, after, or instead of the operation itself. Advanced permissions refer to special permissions, such as login permissions that require a logged-in user to access, VIP permissions that require VIP users to use, or resources that require other set conditions to be met before access.

[0033] Example 1

[0034] like Figure 1 、 2 As shown, this embodiment is a method for intercepting an APP page based on custom annotations, including the following steps 1 to 3:

[0035] Step 1: Construct a universal interface for the interceptor. The universal interface for the interceptor includes an interception condition determination method, a first execution method, and a request code. The first execution method is an execution method when the condition is not met.

[0036] The implementation method of the interceptor general interface is:

[0037] When defining a custom annotation interceptor, declare and implement the interceptor common interface;

[0038] In the custom annotation interceptor class, rewrite the interception condition determination method and the first execution method, and set the request code for each instance;

[0039] Add custom annotations to methods or classes that require login verification. The interceptor management class detects the custom annotations through the reflection mechanism and executes the corresponding interception logic.

[0040] When building a common interceptor interface for Android applications, you need to define an interface that includes the key methods of the interceptor: the interception condition judgment method (used to determine whether a certain condition is met), the first execution method (used for processing logic when the condition is not met), and the request code (used to distinguish different interceptors). First, define the common interceptor interface. Define an interceptor common interface Interceptor. This interface contains three main elements: isConditionMet(), a method for determining whether a condition is met, returning a Boolean value; onConditionNotMet(), an execution method when the condition is not met; getRequestCode(), a method for obtaining a request code, used to identify different interceptor instances. Then you can implement a specific interceptor. For example, a user login status interceptor LoginInterceptor is used to jump to the login page if the user is not logged in.

[0041] The interceptor common interface provides a unified interception logic specification for easy implementation and maintenance. The isConditionMet() method is used to determine whether interception is required. Returning true indicates that the condition is met and no interception is required; returning false indicates that the condition is not met and interception is required and onConditionNotMet() is executed. The onConditionNotMet() method handles the specific logic when the condition is not met, such as jumping to the login page. The getRequestCode() method provides an identifier to distinguish different interceptors after processing is completed.

[0042] Step 2: Build a custom annotation interceptor that implements the interceptor general interface and rewrites the request code, interception condition determination, and corresponding first execution method of the corresponding annotation in the custom annotation interceptor. The interceptor management class scans all interceptors of the target class. If an interceptor is added to the target class, the instance of the annotation class is obtained through reflection and added to the collection; it loops to determine whether the interception condition determination method of the interceptor in the collection is satisfied. If not, the traversal ends and the first execution method is executed. Jump to the corresponding interface for processing. After processing, return to the current interface and loop until all interceptors are satisfied and execute the second execution method.

[0043] Among them, the interceptor management class is added to the BaseActivity base class of the application, and the logical processing method of the interface is used as the second execution method. The second execution method is the execution method when the conditions are met. By adding the interceptor management class to BaseActivity, a unified interception mechanism can be provided for all subclasses inherited from BaseActivity. The logic common to all interfaces (such as user login status verification) is concentrated in BaseActivity to reduce duplicate code and improve the maintainability and readability of the code. Add the interceptor management class to the BaseActivity base class of the application, and the implementation steps are as follows:

[0044] Define the interceptor management class;

[0045] Declare and initialize the interceptor management class in the BaseActivity base class. Specifically, define an instance of the interceptor management class in the BaseActivity base class and initialize it in the onCreate method;

[0046] Implement the interceptor interface and add it to the management class. Specifically, each specific interceptor needs to implement the interceptor common interface and define the condition judgment and execution logic;

[0047] When a user performs an operation that requires permission, the onUserAction method is called, and the interceptor management class in the BaseActivity base class automatically traverses and executes the logic of all interceptors.

[0048] Among them, the custom annotation interceptor is a specific interceptor implementation that uses annotations to declare the operations that need to be intercepted. The custom annotation interceptor needs to implement the interceptor common interface and define its specific interception logic. The specific implementation steps are as follows:

[0049] Define custom annotations and create a custom annotation (such as @LoginRequired) to mark methods or classes that require login verification;

[0050] Implement the interceptor general interface and the custom annotation interceptor class to implement the methods in the interceptor general interface: implement the logic to determine whether the user is logged in. If not, return false, otherwise return true; the first execution method implements the processing logic when the user is not logged in, such as jumping to the login page; set a unique request code for each custom annotation interceptor instance to distinguish different interceptors;

[0051] In the custom annotation interceptor class, override the method of the interceptor common interface to provide specific interception logic and processing methods.

[0052] Step 3: Add the custom annotation interceptor to the class that needs the interception function. One or more custom interceptors can be added to the class that needs special functions.

[0053] The interception conditions described in step 1 include user login status, user permissions or roles, network status, application version, function switch, device status, session timeout, form data verification, geographic location restrictions, application background service status and time limit.

[0054] Specifically, the login status is used to check whether the user has logged in to the application. For example, some functions or pages require the user to log in to access, such as viewing order history, personal information or other content related to the user account, which can be obtained through UserManager.isUserLoggedIn() or similar methods; the user permissions or roles are used to check whether the user has the permission to perform certain operations or access certain pages. For example, some advanced functions are only open to specific user roles (such as administrators, VIP users), which can be obtained through getUserRole() or hasPermission("PERMISSION_NAME") under UserManager; the network status is used to check the network connection status of the device to ensure that the network services that the user operations rely on are available. For example, some functions (such as data synchronization, online payment, real-time chat) require a network connection to operate normally. ConnectivityManager can be used to check the current network status; the application version and function switch are used to check whether the application version or specific functions are enabled, such as some functions may only be enabled in a specific version, or can be dynamically switched through server configuration; the device status checks the status of the device, such as battery power, storage space, GPS positioning, etc., such as some operations may require a specific device status to proceed normally, for example, GPS navigation requires positioning permission and positioning services to be turned on, the battery power is obtained through BatteryManager, and the GPS status is checked by LocationManager, etc.; the session timeout checks whether the user session has timed out. In some scenarios with higher security requirements, some applications will require the user to log in again after a long period of inactivity. By recording the last user operation time and comparing it with the current time, it is determined whether the session has expired; the form data verification is used to check before submitting the form or data. Whether the data meets the requirements, such as verifying the completeness and correctness of the input before submitting data (such as registration, login, and order information); the geographic location restriction is used to check whether the user's current geographic location meets certain specific requirements. For example, some services or functions may only be available in a specific geographic area. You can use LocationManager or FusedLocationProviderClient to obtain the current geographic location; the application background service status is used to check whether the application's background service is running or in a specific state. For example, some operations may require the application's background service (such as data synchronization service, message push service) to be running. You can check the running status of the background service and obtain the service status through ServiceManager or similar tools to achieve this; the time limit checks whether the operation is performed within the allowed time range. Some operations or activities may only be valid within a specific time, such as promotional activities, check-in functions, etc.

[0055] In step 2, the interceptor management class saves and maintains an interceptor set in the form of a list or mapping table, and the interceptor set contains all the interceptor instances that currently need to be managed. The interceptor management class is responsible for traversing and scheduling all interceptors in the interceptor set, and judging in turn whether the interception conditions of each interceptor are met. If the condition of a certain interceptor is met, the condition of the next interceptor is determined; if it is not met, the first execution method (i.e., the execution method when the condition is not met) is executed, and the satisfied condition is obtained through the first execution method; then it returns to re-circulate and judge the condition of each interceptor until the conditions of all interceptors are met, and then the second execution method (i.e., the execution method when the condition is met) is executed.

[0056] Example 2

[0057] This embodiment is an APP page interception system based on custom annotations. The interception system is applied to the interception method as described in Example 1, including:

[0058] The interceptor general interface includes an interception condition determination method, a first execution method, and a request code, wherein the first execution method is an execution method when the condition is not met;

[0059] Access custom annotations, adding the class name of the interceptor general interface or the subclass class name of the interceptor general interface to the required interface;

[0060] The interceptor management class is added to the BaseActivity base class, and the logic processing method of the interface is used as the second execution method; the interceptor management class is used to scan and manage the interceptors in the target class.

[0061] Example 3

[0062] This embodiment is a computer device, including a memory, a processor, and a computer program stored in the memory. The processor executes the computer program to implement the APP page interception method based on custom annotations as described in Example 1.

[0063] Example 4

[0064] This embodiment is a computer-readable storage medium, on which a computer program is stored. The characteristic of this embodiment is that when the computer program is loaded by a processor, it can execute the APP page interception method based on custom annotations as described in the first embodiment.

[0065] The above description is only a preferred embodiment of the present invention. It should be pointed out that for ordinary technicians in this field, several variations and improvements can be made without departing from the creative concept of the present invention, which all fall within the scope of protection of the present invention.

Claims

1. The APP page interception method based on custom annotations is characterized by: The following steps are involved: Constructing an interceptor general interface, the interceptor general interface includes an interception condition determination method, a first execution method and a request code, the first execution method being an execution method when the condition is not met; Construct a custom annotation interceptor, which implements the interceptor general interface, and rewrites the request code, interception condition determination and corresponding first execution method of the corresponding annotation in the custom annotation interceptor; Add the custom annotation interceptor to the class that needs the interception function in the application; The interceptor management class is added to the BaseActivity base class of the application, an interceptor set is created in the management class, and the logic processing method of the interface is used as the second execution method, which is the execution method when the conditions are met.

2. The APP page interception method based on custom annotations according to claim 1 is characterized in that: The interceptor management class scans all interceptors of the target class. If an interceptor is added to the target class, the instance of the annotation class is obtained through reflection and added to the collection; a loop is performed to determine whether the interception condition determination method of the interceptors in the collection is satisfied. If not, the traversal is terminated, the first execution method is started, and the corresponding interface is jumped to for processing. After processing, the current interface is returned to for loop determination until all interceptors are satisfied and the second execution method is executed.

3. According to the method for intercepting APP pages based on custom annotations according to claim 1, it is characterized in that: One or more custom interceptors may be added to the class that requires special functions.

4. The APP page interception method based on custom annotations according to claim 1 is characterized in that: The interception conditions include user login status, user permissions or roles, network status, application version, function switch, device status, session timeout, form data verification, geographic location restrictions, application background service status and time limit.

5. The APP page interception method based on custom annotations according to claim 1 is characterized in that: The interceptor management class saves and maintains an interceptor set in the form of a list or a mapping table, and the interceptor set includes all interceptor instances that currently need to be managed.

6. The APP page interception method based on custom annotations according to claim 1 is characterized in that: The request code is a unique identifier used to identify different interceptor instances.

7. The APP page interception method based on custom annotations according to claim 1 is characterized in that: The implementation method of the interceptor general interface is: When defining a custom annotation interceptor, declare and implement the interceptor common interface; In the custom annotation interceptor class, rewrite the interception condition determination method and the first execution method, and set the request code for each instance; Add custom annotations to methods or classes that require login verification. The interceptor management class detects the custom annotations through the reflection mechanism and executes the corresponding interception logic.

8. An APP page interception system applied to the APP page interception method based on custom annotations as described in claim 1, characterized in that: include: The interceptor general interface includes an interception condition determination method, a first execution method and a request code, wherein the first execution method is an execution method when the condition is not met; A custom annotation, in which the class name of the interceptor common interface or the subclass class name of the interceptor common interface is added; The interceptor management class is added to the BaseActivity base class, and the logic processing method of the interface is used as the second execution method to scan and manage whether all methods in the target class contain interceptor annotations.

9. A computer device comprising a memory, a processor and a computer program stored in the memory, characterized in that: The processor executes the computer program to implement the steps of the method according to any one of claims 1 to 7.

10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is loaded into a processor, the computer program can execute the steps of the method according to any one of claims 1 to 7.

Citation Information

Patent Citations

  • Implementation method of mobile terminal iOS login state interception verification

    CN115630354A