AuthController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace www\controllers;
  3. use biz\auth\services\AuthService;
  4. use common\components\util;
  5. use Yii;
  6. use biz\merchant\services\MerchantService;
  7. use biz\user\services\UserService;
  8. use common\components\httpUtil;
  9. use common\components\jwt;
  10. use common\components\stringUtil;
  11. use common\services\xhMerchantService;
  12. use common\components\wxUtil;
  13. class AuthController extends BaseController
  14. {
  15. public $withoutLogin = ['prepare', 'get-user-info'];
  16. //获取权限树 shish 2019.11.24
  17. public function actionTree()
  18. {
  19. $get = Yii::$app->request->get();
  20. $endId = isset($get['endId']) ? $get['endId'] : 1;
  21. $tree = AuthService::getTree($endId);
  22. util::success($tree);
  23. }
  24. //添加权限 shish 2019.11.24
  25. public function actionAdd()
  26. {
  27. $post = Yii::$app->request->post();
  28. $endId = isset($post['endId']) ? $post['endId'] : 1;
  29. $authName = isset($post['authName']) ? $post['authName'] : '';
  30. $list = AuthService::getNameList($endId);
  31. if (in_array($authName, $list)) {
  32. util::fail('名称已经存在');
  33. }
  34. AuthService::add($_POST);
  35. util::ok();
  36. }
  37. //修改 shish 2019.11.24
  38. public function actionUpdate()
  39. {
  40. $post = Yii::$app->request->post();
  41. $id = isset($post['id']) ? $post['id'] : 0;
  42. $auth = AuthService::getById($id);
  43. if (empty($auth)) {
  44. util::fail('没有找到权限');
  45. }
  46. $endId = $auth['endId'];
  47. $nameList = AuthService::getNameList($endId);
  48. $authName = isset($post['authName']) ? $post['authName'] : '';
  49. if ($authName != $auth['authName'] && in_array($authName, $nameList)) {
  50. util::fail('名称已经存在');
  51. }
  52. AuthService::updateById($id, $_POST);
  53. util::ok();
  54. }
  55. //删除权限 shish 2020.1.7
  56. public function actionDelete()
  57. {
  58. $id = Yii::$app->request->get('id');
  59. util::ok('删除成功');
  60. }
  61. //准备授权 shish 2019.11.19
  62. public function actionPrepare()
  63. {
  64. $get = Yii::$app->request->get();
  65. $url = isset($get['url']) && !empty($get['url']) ? $get['url'] : '';
  66. if (empty($url)) {
  67. util::fail('没有网址');
  68. }
  69. $account = httpUtil::getAccount($url);
  70. if (empty($account)) {
  71. util::fail('没有商家帐号');
  72. }
  73. $merchant = MerchantService::getById($account);
  74. if (empty($merchant)) {
  75. util::fail('商家无效');
  76. }
  77. /**
  78. * authScope 参数的说明
  79. * 1.用户通过公众号菜单打开网页,因为已经关注过了公众号,已经生成过用户信息,所以只要使用snsapi_base静默方式拿到openid就可以
  80. * 2.为了保证付款页的访问速度,暂时只要通过snsapi_base静默方式拿到openid先生成可以用的用户信息即可
  81. * 3.其它情况需要获取用户完整信息的,使用snsapi_userinfo让用户授权获取信息即可
  82. * 4.snsapi_base静默方式使用的场景更多,所以参数authScope默认使用snsapi_base
  83. */
  84. $authScope = isset($get['authScope']) ? $get['authScope'] : 'snsapi_base';
  85. $urlEncode = stringUtil::urlSafeB64Encode($url);
  86. //微信授权获取code shish 2019.11.24
  87. wxUtil::getCode($urlEncode, $merchant, $authScope);
  88. util::end();
  89. }
  90. //微信授权获取用户信息 shish 2019.11.20
  91. public function actionGetUserInfo()
  92. {
  93. $get = Yii::$app->request->get();
  94. $code = isset($get['code']) ? $get['code'] : '';
  95. if (empty($code)) {
  96. util::fail('没有获取用户code');
  97. }
  98. if (isset($get['state']) && $get['state'] == 'authdeny') {
  99. util::fail('auth fail');
  100. }
  101. $urlEncode = $get['url'];
  102. $url = stringUtil::urlSafeBase64Decode($urlEncode);
  103. $account = httpUtil::getAccount($url);
  104. if (empty($account)) {
  105. util::stop('商店ID无效!');
  106. }
  107. $merchant = xhMerchantService::getByAccount($account);
  108. if (empty($merchant)) {
  109. util::stop('商店无效');
  110. }
  111. $merchantId = $merchant['id'];
  112. $authScope = isset($get['authScope']) ? $get['authScope'] : '';
  113. if (empty($authScope)) {
  114. util::stop('没有授权类型');
  115. }
  116. $data = wxUtil::getOpenId($code, $merchant);
  117. if ($data === false) {
  118. //微信授权获取code shish 2019.11.24
  119. wxUtil::getCode($urlEncode, $merchant, $authScope);
  120. util::end();
  121. }
  122. $openId = $data['openid'];
  123. $access_token = $data['access_token'];
  124. $user = UserService::getByOpenId($openId, $merchantId);
  125. //用户来源
  126. $userSource = Yii::$app->dict->getValue('userSourceGetId', 'official');
  127. $source = $userSource['name'];
  128. if (empty($user)) {
  129. //$authScope 默认是 snsapi_base 公众号菜单打开网页(已经关注公众号,有完整信息)、打开付款页(有些客户只付款,没有后续可以不用登记完整信息),只需要获取openid
  130. $originalInfo = [];
  131. $originalInfo['openId'] = $openId;
  132. $originalInfo['merchantId'] = $merchantId;
  133. //没有关注公众号,需要获取用户完整信息的情况,则通过弹框授权
  134. if ($authScope == 'snsapi_userinfo') {
  135. $baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
  136. $originalInfo = array_merge($baseInfo, $originalInfo);
  137. $originalInfo['isFull'] = 1;
  138. $originalInfo['unionId'] = $baseInfo['unionid'];
  139. $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
  140. $originalInfo['nickName'] = $baseInfo['nickname'];
  141. }
  142. $user = UserService::replaceUser($originalInfo, $source, $merchantId);
  143. } else {
  144. if ($user['isFull'] == 0) {
  145. if ($authScope == 'snsapi_userinfo') {
  146. $baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
  147. $originalInfo = $baseInfo;
  148. $originalInfo['isFull'] = 1;
  149. $originalInfo['unionId'] = $baseInfo['unionid'];
  150. $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
  151. $originalInfo['openId'] = $baseInfo['openid'];
  152. $originalInfo['nickName'] = $baseInfo['nickname'];
  153. $originalInfo['merchantId'] = $merchantId;
  154. $user = UserService::replaceUser($originalInfo, $source, $merchantId);
  155. }
  156. }
  157. }
  158. //这个的基类没有设置统一的全局变量,这里进行设置一次
  159. $userId = $user['id'];
  160. //获取token
  161. $token = jwt::getNewToken($userId);
  162. $url = strpos($url, '?') === false ? $url . '?token=' . $token : $url . '&token=' . $token;
  163. $this->redirect($url);
  164. }
  165. }