Admin.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace app\admin\controller\shopro\wechat;
  3. use fast\Random;
  4. use addons\shopro\facade\Wechat;
  5. use app\admin\model\shopro\ThirdOauth;
  6. use app\admin\controller\shopro\Common;
  7. class Admin extends Common
  8. {
  9. protected $wechat;
  10. protected $noNeedRight = ['getQrcode', 'checkScan', 'unbind'];
  11. public function _initialize()
  12. {
  13. parent::_initialize();
  14. $this->wechat = Wechat::officialAccountManage();
  15. }
  16. // 获取公众号二维码
  17. public function getQrcode()
  18. {
  19. $event = $this->request->param('event');
  20. if (!in_array($event, ['bind'])) {
  21. $this->error('参数错误');
  22. }
  23. $adminId = $this->auth->id;
  24. $thirdOauth = ThirdOauth::where([
  25. 'provider' => 'wechat',
  26. 'platform' => 'admin',
  27. 'admin_id' => $adminId
  28. ])->find();
  29. if ($thirdOauth) {
  30. error_stop('已绑定微信账号', -2, $thirdOauth);
  31. }
  32. // 二维码和缓存过期时间
  33. $expireTime = 1 * 60;
  34. // 事件唯一标识
  35. $eventId = Random::uuid();
  36. $cacheKey = "wechatAdmin.{$event}.{$eventId}";
  37. cache($cacheKey, ['id' => 0], $expireTime);
  38. try {
  39. $result = $this->wechat->qrcode->temporary($cacheKey, $expireTime);
  40. $qrcode = $this->wechat->qrcode->url($result['ticket']);
  41. } catch (\Exception $e) {
  42. $this->error($e->getMessage());
  43. }
  44. $this->success('', null, [
  45. 'url' => $qrcode,
  46. 'eventId' => $eventId
  47. ]);
  48. }
  49. // 检查扫码结果
  50. public function checkScan()
  51. {
  52. $event = $this->request->param('event');
  53. $eventId = $this->request->param('eventId');
  54. if (!in_array($event, ['bind'])) {
  55. error_stop('参数错误');
  56. }
  57. $cacheKey = "wechatAdmin.{$event}.{$eventId}";
  58. $cacheValue = cache($cacheKey);
  59. if (empty($cacheValue)) {
  60. error_stop('二维码已过期, 请重新扫码');
  61. }
  62. if ($cacheValue['id'] === 0) {
  63. error_stop('等待扫码', -1);
  64. }
  65. if ($cacheValue['id'] !== 0) {
  66. switch ($event) {
  67. case 'bind':
  68. $adminId = $this->auth->id;
  69. $thirdOauth = ThirdOauth::where([
  70. 'provider' => 'wechat',
  71. 'platform' => 'admin',
  72. 'openid' => $cacheValue['id'],
  73. ])->find();
  74. if ($thirdOauth && $thirdOauth->admin_id !== 0) {
  75. error_stop('该微信账号已被绑定');
  76. }
  77. if (!$thirdOauth) {
  78. $thirdOauth = ThirdOauth::create([
  79. 'provider' => 'wechat',
  80. 'platform' => 'admin',
  81. 'openid' => $cacheValue['id'],
  82. 'admin_id' => $adminId
  83. ]);
  84. } else {
  85. $thirdOauth->admin_id = $adminId;
  86. $thirdOauth->save();
  87. }
  88. break;
  89. }
  90. $this->success();
  91. }
  92. }
  93. // 解绑
  94. public function unbind()
  95. {
  96. $adminId = $this->auth->id;
  97. $thirdOauth = ThirdOauth::where([
  98. 'provider' => 'wechat',
  99. 'platform' => 'admin',
  100. 'admin_id' => $adminId
  101. ])->find();
  102. if ($thirdOauth) {
  103. $thirdOauth->admin_id = 0;
  104. $thirdOauth->save();
  105. }
  106. $this->success();
  107. }
  108. }