WechatController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\api\controller\v2\wechat;
  12. use app\Request;
  13. use app\services\wechat\WechatServices;
  14. use crmeb\exceptions\ApiException;
  15. use crmeb\services\CacheService;
  16. use crmeb\services\oauth\OAuth;
  17. /**
  18. * Class WechatController
  19. * @package app\api\controller\v2\wechat
  20. */
  21. class WechatController
  22. {
  23. protected $services = NUll;
  24. /**
  25. * WechatController constructor.
  26. * @param WechatServices $services
  27. */
  28. public function __construct(WechatServices $services)
  29. {
  30. $this->services = $services;
  31. }
  32. /**
  33. * 公众号授权登录,返回token
  34. * @param $spread
  35. * @return \think\Response
  36. * @throws \Psr\SimpleCache\InvalidArgumentException
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. * @author: 吴汐
  41. * @email: 442384644@qq.com
  42. * @date: 2023/8/12
  43. */
  44. public function authLogin($spread = '')
  45. {
  46. $data = $this->services->authLogin($spread);
  47. return app('json')->success($data);
  48. }
  49. public function authOpenId()
  50. {
  51. /** @var OAuth $oauth */
  52. $oauth = app()->make(OAuth::class);
  53. $wechatInfo = $oauth->oauth();
  54. if (!isset($wechatInfo['nickname'])) {
  55. $wechatInfo = $oauth->getUserInfo($wechatInfo['openid']);
  56. if (!isset($wechatInfo['nickname']))
  57. throw new ApiException(410131);
  58. if (isset($wechatInfo['tagid_list']))
  59. $wechatInfo['tagid_list'] = implode(',', $wechatInfo['tagid_list']);
  60. } else {
  61. if (isset($wechatInfo['privilege'])) unset($wechatInfo['privilege']);
  62. }
  63. $wechatInfo['user_type'] = 'wechat';
  64. $openid = $wechatInfo['openid'];
  65. return app('json')->success($openid);
  66. }
  67. /**
  68. * 公众号授权绑定手机号
  69. * @param string $key
  70. * @param string $phone
  71. * @param string $captcha
  72. * @return \think\Response
  73. * @throws \Psr\SimpleCache\InvalidArgumentException
  74. * @throws \think\db\exception\DataNotFoundException
  75. * @throws \think\db\exception\DbException
  76. * @throws \think\db\exception\ModelNotFoundException
  77. * @author: 吴汐
  78. * @email: 442384644@qq.com
  79. * @date: 2023/8/12
  80. */
  81. public function authBindingPhone($key = '', $phone = '', $captcha = '')
  82. {
  83. //验证验证码
  84. // $verifyCode = CacheService::get('code_' . $phone);
  85. // if (!$verifyCode)
  86. // return app('json')->fail(410009);
  87. // $verifyCode = substr($verifyCode, 0, 6);
  88. // if ($verifyCode != $captcha) {
  89. // CacheService::delete('code_' . $phone);
  90. // return app('json')->fail(410010);
  91. // }
  92. CacheService::delete('code_' . $phone);
  93. $data = $this->services->authBindingPhone($key, $phone);
  94. return app('json')->success($data);
  95. }
  96. }