Article Directory
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 WeChat
to this applet.
Processing Process
Okay, now that the problem has been clarified, let’s get started.
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.
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.
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.
Okay, it’s time for the main event.
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); } } }); }})
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
This interface is similar to the method of obtaining token in ordinary WeChat.
Among them,corpid
is in the enterprise WeChat management background->My Company->Corporate Information->Corporate ID;corpsecret
is 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}
2. Get userid
https://qyapi.weixin.qq.com/cgi-bin/miniprogram/jscode2session?access_token=xxx&js_code=xxx&grant_type=authorization_code 请求方式:GET
Among them,access_token
is what we obtained by gettoken in the previous step;js_code
is theres.code
obtained when judging the running environment;grant_type
Fixed transmissionauthorization_code
The returned content is as follows:
{ "userid": "bottle", "session_key": "xxxxx", "corpid": "xxxxxx", "deviceid": "xxxxxx", "errcode": 0, "errmsg": "ok"}
Thecorpid
here 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 whethercorpid
is the ID of a company you authorize. If not, just return no permission without proceeding to the next step.
Of coursecorpid
can 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
其中,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": []}
{ "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": []}
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!