Multiple departments share the same form

Multiple departments share the same form:
the first level department head can view all data, the second level A department head can only view department A, the second level B department can only view department B, and ordinary members can only view their own data

try to hide the field based on role.
For example

frappe.ui.form.on('Your DocType', {
    refresh: function(frm) {
        frappe.call({
            method: 'frappe.client.get_value',
            args: {
                doctype: 'User',
                fieldname: 'role_profile_name',
                filters: { 'name': frappe.session.user }
            },
            callback: function(r) {
                if (r.message && r.message.role_profile_name) {
                    var userRole = r.message.role_profile_name;

                    if (userRole === 'Second Level A Department Head') {
                        frm.toggle_display('field2A', true);
                        frm.toggle_display('field2B', false);
                    } else if (userRole === 'Second Level B Department Head') {
                        frm.toggle_display('field2A', false);
                        frm.toggle_display('field2B', true);
                    } else {
                        frm.toggle_display('field2A', false);
                        frm.toggle_display('field2B', false);
                    }
                }
            }
        });
    }
});

Hello @haianjiyong, you might want to take a look at user restrictions and field-level permissions.

User Permissions (restrictions) can help you restrict documents across multiple companies/departments. This is used in the Leaves system in HRMS, so that you only see your own Leave Applications.

permlevel is a property of a DocField used to hide it based on a Role.
For instance, you can have level 0 fields, available for all, level 1 for accounting, level 2 for sensitive information, etc. And assign a role to each permlevel.