ThirdPartyController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace backend\controllers;
  3. use Yii;
  4. use yii\web\Response;
  5. use common\helpers\ArrayHelper;
  6. use common\enums\StatusEnum;
  7. use common\enums\AccessTokenGroupEnum;
  8. use common\helpers\ResultHelper;
  9. /**
  10. * 第三方授权
  11. *
  12. * Class ThirdPartyController
  13. * @package backend\controllers
  14. */
  15. class ThirdPartyController extends BaseController
  16. {
  17. public $member_id;
  18. public function init()
  19. {
  20. parent::init();
  21. $this->member_id = Yii::$app->request->get('member_id');
  22. }
  23. /**
  24. * @return string
  25. */
  26. public function actionIndex()
  27. {
  28. $thirdParty = [
  29. AccessTokenGroupEnum::WECHAT_MP => [
  30. 'name' => AccessTokenGroupEnum::WECHAT_MP,
  31. 'client' => '无',
  32. 'title' => AccessTokenGroupEnum::getValue(AccessTokenGroupEnum::WECHAT_MP),
  33. 'status' => StatusEnum::DISABLED
  34. ]
  35. ];
  36. $auth = Yii::$app->services->memberAuth->findByMemberId($this->member_id);
  37. $auth = ArrayHelper::arrayKey($auth, 'oauth_client');
  38. foreach ($thirdParty as &$item) {
  39. if (isset($auth[$item['name']])) {
  40. $item['status'] = StatusEnum::ENABLED;
  41. $item['client'] = $auth[$item['name']]['oauth_client_user_id'];
  42. }
  43. }
  44. return $this->render($this->action->id, [
  45. 'thirdParty' => $thirdParty,
  46. 'memberId' => $this->member_id,
  47. ]);
  48. }
  49. /**
  50. * 绑定
  51. *
  52. * @param $uuid
  53. * @return mixed
  54. * @throws \yii\base\InvalidConfigException
  55. */
  56. public function actionBindingWechat($member_id)
  57. {
  58. $qr = Yii::$app->get('qr');
  59. Yii::$app->response->format = Response::FORMAT_RAW;
  60. Yii::$app->response->headers->add('Content-Type', $qr->getContentType());
  61. $data = Yii::$app->wechatService->qrcode->syncCreateByData([
  62. 'name' => '账号绑定',
  63. 'model_type' => 1,
  64. 'expire_seconds' => 5 * 60,
  65. 'extend' => [
  66. 'type' => 'binding',
  67. 'member_id' => $member_id,
  68. 'remind' => [
  69. 'success' => '绑定账号: {member.username}; 绑定时间: {time}',
  70. 'error' => '账号 {member.username} 已被绑定过,请先解绑; 操作时间: {time}',
  71. ]
  72. ],
  73. ]);
  74. $data->save();
  75. return $qr->setText($data['url'])
  76. ->setSize(200)
  77. ->setMargin(7)
  78. ->writeString();
  79. }
  80. /**
  81. * 解绑
  82. *
  83. * @param $uuid
  84. * @return mixed
  85. * @throws \yii\base\InvalidConfigException
  86. */
  87. public function actionUnBind($type, $member_id)
  88. {
  89. Yii::$app->services->memberAuth->unBind($type, $member_id);
  90. return $this->message("解绑成功", $this->redirect(['index', 'member_id' => $member_id]));
  91. }
  92. /**
  93. * @param $type
  94. * @param $member_id
  95. * @return array|mixed
  96. */
  97. public function actionOauthStatus($type, $member_id)
  98. {
  99. $auth = Yii::$app->services->memberAuth->findByMemberIdAndOauthClient($member_id, $type);
  100. if ($auth) {
  101. return ResultHelper::json(200, '已授权');
  102. }
  103. return ResultHelper::json(422, '未授权');
  104. }
  105. }
粤ICP备19079148号