C # develop WeChat portals and applications (10) -- synchronize WeChat user group information in the management system

Source: Internet
Author: User

In the previous articles, we gradually transitioned from the original API encapsulation to the application platform management system, and gradually introduced the data interface design in the management system, and related processing logic and code. We hope to introduce the application development process to you at a higher level. This article describes how to synchronize user group information in the management system.

In fact, the main reason is that there is user information, so it is very important to synchronize and manage the account's attention to user data. With user data, you can connect to any of your application systems to integrate the data of the system-mobile client, and perform Marketing Management for users, for example, sending product messages and service messages that users are interested in can greatly expand the company's influence and market behavior.

In an earlier article titled C # development portal and application (5)-user group information management, I once introduced various underlying API encapsulation operations for groups, it mainly provides APIs. NET advanced grouping, for all information exchange, data exchange through entity, makes it easier for us to call APIs to process various transactions, thus laying the foundation for application platform management. This article introduces the API encapsulation process of all group management, including user group management, which includes the following aspects:

1) create a group
2) query all Groups
3) query the user group
4) modify the group name
5) Mobile User Group

1. User grouping, interface design in the management system

For the operations of the preceding groups, we can design a module in the application management system to manage group data. In this module, we can create and modify groups, basic operations such as viewing groups can also be used to synchronize group operations. A synchronization operation is mainly to add new group information to the Group, and the modified group is also implemented in the modification function, deletion is not supported currently, so you don't have to worry about it. Finally, we can synchronize the modified data from the server here. To avoid submitting unsuccessful data, we need to mark the modified records, this is the logic of the entire synchronization operation.

In the management system, the Group list management interface is designed as follows.

When creating a group, you only need to add a group name. The interface design is also simple. However, we designed the created ID to-1 as a newly added ID that is not synchronized.

The Edit Group information page is as follows. After the group is edited and saved, the system will remember the modified groups.

2. Group Synchronization operation code Display

To better implement Group Synchronization management, I encapsulate the group operation code in an MVC Controller method. The page code can be synchronized through Ajax calls, or if it fails, you will be prompted to understand the results.

During synchronization, add the locally added content to create a group on the server, modify the modified group name on the server, and then process the synchronization list. Before synchronization, the list interface may be as follows. If there is a new record ID =-1, or after modification, the record modification flag.

 

 

The synchronization button operation of the user group is to call a script code. The specific code is as follows.

// BindSyncDataEvent () {$ ("# btnSyncData "). click (function () {$. messager. confirm ("submit for confirmation", "Are you sure you want to synchronize group information with the server? ", Function (action) {if (action) {// submit data $ (" # loading "). show (); $. ajax ({url :'/Group/SyncGroup', Type: 'post', dataType: 'json', success: function (data) {if (data. success) {$ ("# grid "). datagrid ("reload"); $. messager. alert ("prompt", "synchronization successful");} else {$. messager. alert ("prompt", "synchronization failed:" + data. errorMessage) ;}}, data: ''}); $ (" # loading "). fadeOut (500 );}});});}

The above red part is the MVC Controller method called through Jquery. The specific function code is as follows.

/// <Summary> /// synchronize the group information of the server /// </summary> /// <returns> </returns> public ActionResult SyncGroup () {string accessToken = GetAccessToken (); CommonResult result = BLLFactory <Group>. instance. syncGroup (accessToken); return ToJsonContent (result );}

From the above, we didn't see much logic. In order to facilitate their further encapsulation, we put it in the business logic layer for processing. Let's take a look at its code logic. Here we use transaction operations to make all database operations faster and complete, so that we can paste the relevant code to help you understand the logic.

/// <Summary> /// synchronize the server group information /// </summary> /// <returns> </returns> public CommonResult SyncGroup (string accessToken) {CommonResult result = new CommonResult (); try {IUserApi api = new UserApi (); using (DbTransaction trans = baseDal. createTransaction () {// first upload the records with the local flag groupId =-1 not uploaded to the server, and then update string condition = string locally. format ("GroupID = '-1'"); List <GroupInfo> unSubmitList = base. find (conditi On); foreach (GroupInfo info in unSubmitList) {GroupJson groupJson = api. CreateGroup (accessToken, info. Name); if (groupJson! = Null) {info. groupID = groupJson. id; baseDal. update (info, info. ID, trans) ;}/// indicates the record whose status is modified, and modifies condition = string on the server. format ("GroupID> = 0 and Modified = 1"); List <GroupInfo> unModifyList = base. find (condition); foreach (GroupInfo info in unModifyList) {CommonResult modifyed = api. updateGroupName (accessToken, info. groupID, info. name); if (modifyed! = Null & modifyed. success) {info. modified = 0; // reset the flag baseDal. update (info, info. ID, trans) ;}/// Delete the group with the delete flag // condition = string. format ("GroupID> = 100 and Deleted = 1"); // List <GroupInfo> unDeletedList = base. find (condition); // foreach (GroupInfo info in unDeletedList) // {// CommonResult deleted = api. deleteGroup (accessToken, info. groupID, info. name); // if (deleted! = Null & deleted. success) // {// baseDal. delete (info. ID, trans); //} List <GroupJson> list = api. getGroupList (accessToken); foreach (GroupJson info in list) {UpdateGroup (info, trans);} try {trans. commit (); result. success = true;} catch {trans. rollback (); throw ;}} catch (Exception ex) {result. errorMessage = ex. message;} return result ;}

During Jquery synchronization, in order to avoid waiting for too long to judge whether the program is working normally, we 'd better add a busy prompt operation because we used Ajax calls, therefore, you can set the busy and complete status of Ajax as follows.

// Used to unify the settings for displaying busy requests $. ajaxSetup ({beforeSend: function () {$ ("# loading "). show () ;}, complete: function () {$ ("# loading "). hide ();}});

 

If you are interested in or want to experience related functions, follow my knowledge. For specific results, you can follow my portal: Guangzhou aiqidi, or scan the following QR code to learn more.

If you are interested in this series of C # development portals and applications, you can follow my other articles as follows:

C # development portal and application (9)-portal menu management and submission to server

C # development portal and application (8)-portal application management system function Introduction

C # development portal and application (7)-Multi-customer service functions and development integration

C # development portal and application (6)-portal menu management operations

C # development portal and application (5) -- User Group Information Management

C # development portal and application (4) -- Focus on the user list and detailed information management

C # development portal and application (3) -- Response to text messages and text messages

C # development portal and application (2) -- Message Processing and response

C # development portal and application (1) -- getting started with Interfaces

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.