appletapi.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. 'use strict';
  2. const Controller = require('egg').Controller;
  3. class AppletapiController extends Controller {
  4. async get_openid({ code }) {
  5. const token = await this.get_accessToken();
  6. const url = `https://api.weixin.qq.com/wxa/getpluginopenpid?access_token=${token}`;
  7. const openid = await this.ctx.curl({
  8. model: 'post',
  9. url,
  10. data: { code },
  11. });
  12. return openid;
  13. }
  14. async get_phone({ code }) {
  15. const token = await this.get_accessToken();
  16. const url = `https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=${token}`;
  17. const data = await this.ctx.curl({
  18. model: 'post',
  19. url,
  20. data: { code },
  21. });
  22. return data;
  23. }
  24. // 获取access_token
  25. async get_accessToken() {
  26. const { appid, appsecret } = this.app.config.wxconfig;
  27. const to_uri = `https://api.weixin.qq.com/cgi-bin/token?appid=${appid}&secret=${appsecret}&grant_type=client_credential`;
  28. const { access_token } = await this.ctx.curl(to_uri);
  29. return access_token;
  30. }
  31. }
  32. module.exports = AppletapiController;