Enterprise WeChat applet development process

coldplay.xixi
Release: 2021-04-27 10:11:23
forward
2780 people have browsed it

Enterprise WeChat applet development process

Article Directory

    • Background
    • Problem Analysis
    • Processing process
      • 1. Develop the basic functions of the mini program and submit it for review
      • 2. Enterprise WeChat associated mini program
      • 3. Obtain the information of the associated mini program Secret, set the visible range
      • 4. Modify the applet
        • 4.1. Determine the operating environment
        • 4.2. Permission verification
    • Reference materials

Related free learning recommendations:WeChat applet development Tutorial

Background

Develop a small program for other companies. This small program must only be accessed in corporate WeChat and cannot be accessed through ordinary WeChat. access.

Only authorized users in Enterprise WeChat can use this mini program.

The reason why I need to organize such a process is because the documents on WeChat are too scattered and messy. It also took me a long time to straighten out this process.

Problem Analysis

First of all, there are several problems that need to be solved:

1. It can only be used in corporate WeChat and cannot be used in ordinary mini programs. Use

to determine the current operating environment

2. Only authorized users can use the applet

Permission verification

1: Ordinary WeChat, use account and password to log in, only for review, account permissions can be restricted

2: Enterprise WeChat, verify company ID, verify user ID, only those with permission are allowed to use

3. Control search

Set "Not allowed to be searched" in the mini program management background

4. Control sharing

Close sharing in the mini program

OK, after doing the above points, only authorized users in Enterprise WeChat can view it on theworkbench of Enterprise WeChatto this applet.

Processing Process

Okay, now that the problem has been clarified, let’s get started.

1. Develop the basic functions of the mini program and submit it for review

Yes, you read that right, submit it for review first, because only mini programs that pass the review can be bound to Enterprise WeChat. Therefore, first make the basic functions of the mini program, and you can limit some functions. In short, let this mini program be put on the shelves first. At the same time, set "not allowed to be searched" in the mini program's management background to avoid unnecessary trouble.

2. Associate the Mini Program with Enterprise WeChat

Enter the Enterprise WeChat backend-> Application Management-> Mini Program-> Associate the Mini Program, and then use the WeChat QR code of the Mini Program administrator to scan the code , just follow the instructions.

3. Get the Secret of the associated applet and set the visible range

The applet you just associated will appear on the applet page in the previous step. Click to enter, and then you will see the secret and visible range.

This secret is equivalent to the token used by the applet to access the company's WeChat data. Please keep it properly.

The visibility range is authorization. Which users can see this mini program. Those who are set to be visible will see the mini program on their corporate WeChat workbench.

4. Modify the mini program

Okay, it’s time for the main event.

4.1. Determine the operating environment

The mini program needs to determine the current operating environment (normal WeChat or enterprise WeChat) and whether the user using the current mini program has permission to use it.

var isWxWork = false;wx.getSystemInfo({ success(res) { console.log(res.environment); isWxWork = res.environment == 'wxwork'; if (!isWxWork) { // 当前环境不是企业微信,怎么处理你随便 return; } // 当前环境是企业微信,执行登陆,获取用户 code,用于后面的权限校验 wx.qy.login({ success: function (res) { if (res.code) { console.log(res.code); // 这里可以将 res.code 通过请求发送给后台,让后台做权限校验 } else { console.log('登录失败!' + res.errMsg); } } }); }})
Copy after login

4.2. Permission verification

The background needs to call the following interfaces to perform permission verification.

1. Obtain access_token

https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=xxxx&corpsecret=xxxx 请求方式:GET
Copy after login

This interface is similar to the method of obtaining token in ordinary WeChat.

Among them,corpidis in the enterprise WeChat management background->My Company->Corporate Information->Corporate ID;corpsecretis the small number associated with us in the previous step The secret obtained after the program.

The returned content is as follows:

{ "errcode": 0, "errmsg": "ok", "access_token": "xxxxxx", "expires_in": 7200}
Copy after login

2. Get userid

https://qyapi.weixin.qq.com/cgi-bin/miniprogram/jscode2session?access_token=xxx&js_code=xxx&grant_type=authorization_code 请求方式:GET
Copy after login

Among them,access_tokenis what we obtained by gettoken in the previous step;js_codeis theres.codeobtained when judging the running environment;grant_typeFixed transmissionauthorization_code

The returned content is as follows:

{ "userid": "bottle", "session_key": "xxxxx", "corpid": "xxxxxx", "deviceid": "xxxxxx", "errcode": 0, "errmsg": "ok"}
Copy after login

Thecorpidhere can be used to initially verify whether the current user has permissions, because no matter which company a person uses, as long as he uses Enterprise WeChat and uses this applet, such a result will be returned. You need to verify whethercorpidis the ID of a company you authorize. If not, just return no permission without proceeding to the next step.

Of coursecorpidcan also be used to handle situations where a small program is associated with multiple companies, but this is another issue. Let me briefly mention here, because it is a small program developed for other companies, our small program is also associated with two companies, one is our company and the other is the other company. This also facilitates our testing and only requires our own testers. Authorization allows them to use the exact same environment for testing.

3. Obtain user information (determine permissions)

https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=xxx&userid=xxx 请求方式:GET
Copy after login

其中,access_token是我们前一步 gettoken 获取到的;userid就是我们上一步获取到的userid

返回内容如下:

  • 有权限
{ "errcode": 0, "errmsg": "ok", "userid": "xxx", "name": "xxx", "department": [], "position": "", "mobile": "xxx", "gender": "2", "email": "", "avatar": "http://p.qlogo.cn/bizmail/xxx/0", "status": 1, "isleader": 0, "extattr": { "attrs": [] }, "telephone": "", "enable": 1, "hide_mobile": 0, "order": [], "qr_code": "https://open.work.weixin.qq.com/wwopen/userQRCode?vcode=xxx", "alias": "", "is_leader_in_dept": []}
Copy after login
  • 无权限
{ "errcode": 60011, "errmsg": "no privilege to access/modify contact/party/agent , hint: [1564556097_7_8d45297bd21be3702ff430560e1f0652], from ip: 118.113.1.217, more info at https://open.work.weixin.qq.com/devtool/query?e=60011", "department": [], "order": [], "is_leader_in_dept": []}
Copy after login

OK,后面根据有权限还是无权限,执行不同的操作就可以了,这里不再赘述。

相关免费学习推荐:微信小程序开发

The above is the detailed content of Enterprise WeChat applet development process. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!