search

Home  >  Q&A  >  body text

javascript - What design pattern or optimization should be used for many different operations in js status?

There are ten levels, and different levels open different functions; it needs to be written in js. I use if or switch, or it feels messy and difficult to manage!!!
There is also a background to directly determine the user's level. I am asked to use js to retrieve the output in html. Isn’t this very unsafe? How should I do this? Do I need to send a request to verify the user level every time?

给我你的怀抱给我你的怀抱2822 days ago623

reply all(2)I'll reply

  • 世界只因有你

    世界只因有你2017-05-16 13:07:10

    There are many ways to do it. Let the background send you Json. If you want to upgrade in time based on points, just load the array of [level, points] asynchronously when the page is initialized, and then calculate it based on the cached array to reach a certain level. The points are upgraded and related resources are displayed. If you want to display different pages according to different levels, you can directly transfer the json of the corresponding level to the background. For example, {username:xxx,lv:3,....} can display different functions according to the value of lv, or simply Let him send you the content to be displayed as well.

    reply
    0
  • 高洛峰

    高洛峰2017-05-16 13:07:10

    How to store the user level and store it in HTML? After the user is authenticated, the server can store the user level in session or nosql, and the client can store it directly in the cookie. Every time a request is made, there is no need to verify the user level unless the request involves user level and permission functions.
    The client obtains the user level information in the cookie. Since you are designing into the level and permission categories, it is recommended to write all the permission functions into a prototype first, and then declare a method class and object. This method will judge the user by Level and permission, that is, if the user meets this permission, copy the function prototype and instantiate it (similar to inheritance), so that you don’t have to make judgments with if statements

    reply
    0
  • Cancelreply