MiniProgramController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace api\modules\v1\controllers;
  3. use Yii;
  4. use api\controllers\OnAuthController;
  5. use api\modules\v1\forms\MiniProgramLoginForm;
  6. use common\models\member\Member;
  7. use common\enums\AccessTokenGroupEnum;
  8. use common\helpers\ResultHelper;
  9. /**
  10. * 小程序授权验证
  11. *
  12. * Class MiniProgramController
  13. * @package api\modules\v1\controllers
  14. * @author jianyan74 <751393839@qq.com>
  15. */
  16. class MiniProgramController extends OnAuthController
  17. {
  18. public $modelClass = '';
  19. /**
  20. * 不用进行登录验证的方法
  21. * 例如: ['index', 'update', 'create', 'view', 'delete']
  22. * 默认全部需要验证
  23. *
  24. * @var array
  25. */
  26. protected $authOptional = ['login'];
  27. /**
  28. * 登录认证
  29. *
  30. * @return array|mixed
  31. * @throws \EasyWeChat\Kernel\Exceptions\DecryptException
  32. * @throws \yii\base\Exception
  33. * @throws \Exception
  34. */
  35. public function actionLogin()
  36. {
  37. $model = new MiniProgramLoginForm();
  38. $model->attributes = Yii::$app->request->post();
  39. if (!$model->validate()) {
  40. return ResultHelper::json(422, $this->getError($model));
  41. }
  42. $userinfo = $model->getUser();
  43. // 插入到用户授权表
  44. if (!($memberAuthInfo = Yii::$app->services->memberAuth->findOauthClient(AccessTokenGroupEnum::WECHAT_MINI, $model->getOpenid()))) {
  45. $memberAuthInfo = Yii::$app->services->memberAuth->create([
  46. 'unionid' => $userinfo['unionId'] ?? '',
  47. 'oauth_client' => AccessTokenGroupEnum::WECHAT_MINI,
  48. 'oauth_client_user_id' => $model->getOpenid(),
  49. 'gender' => $userinfo['gender'],
  50. 'nickname' => $userinfo['nickName'],
  51. 'head_portrait' => $userinfo['avatarUrl'],
  52. 'country' => $userinfo['country'],
  53. 'province' => $userinfo['province'],
  54. 'city' => $userinfo['city'],
  55. 'language' => $userinfo['language'],
  56. ]);
  57. }
  58. // TODO 查询自己关联的用户信息并处理自己的登录请求,并返回用户数据
  59. // TODO 以下代码都可以替换
  60. // 判断是否有管理信息 数据也可以后续在绑定
  61. if (!($member = $memberAuthInfo->member)) {
  62. $member = new Member();
  63. $member->loadDefaultValues();
  64. $member->pid = 0;
  65. $member->attributes = [
  66. 'gender' => $userinfo['gender'],
  67. 'nickname' => $userinfo['nickName'],
  68. 'head_portrait' => $userinfo['avatarUrl'],
  69. ];
  70. $member->save();
  71. // 关联用户
  72. $memberAuthInfo->member_id = $member['id'];
  73. $memberAuthInfo->save();
  74. }
  75. return Yii::$app->services->apiAccessToken->getAccessToken($member, AccessTokenGroupEnum::WECHAT_MINI);
  76. }
  77. /**
  78. * 生成小程序码
  79. *
  80. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  81. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  82. */
  83. public function actionQrCode()
  84. {
  85. // $response = $app->app_code->get('path/to/page');
  86. // 指定颜色
  87. $response = Yii::$app->wechat->miniProgram->app_code->get('path/to/page', [
  88. 'width' => 600,
  89. 'line_color' => [
  90. 'r' => 105,
  91. 'g' => 166,
  92. 'b' => 134,
  93. ],
  94. ]);
  95. // $response 成功时为 EasyWeChat\Kernel\Http\StreamResponse 实例,失败时为数组或者你指定的 API 返回格式
  96. $directory = Yii::getAlias('@attachment');
  97. // 保存小程序码到文件
  98. if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  99. $filename = $response->save($directory);
  100. }
  101. // 或
  102. if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  103. $filename = $response->saveAs($directory, 'appcode.png');
  104. }
  105. }
  106. /**
  107. * 权限验证
  108. *
  109. * @param string $action 当前的方法
  110. * @param null $model 当前的模型类
  111. * @param array $params $_GET变量
  112. * @throws \yii\web\BadRequestHttpException
  113. */
  114. public function checkAccess($action, $model = null, $params = [])
  115. {
  116. // 方法名称
  117. if (in_array($action, ['index', 'view', 'update', 'create', 'delete'])) {
  118. throw new \yii\web\BadRequestHttpException('权限不足');
  119. }
  120. }
  121. }
粤ICP备19079148号