AuthController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. $account = '12362';
  71. if (empty($account)) {
  72. util::fail('没有商家帐号');
  73. }
  74. $merchant = MerchantService::getById($account);
  75. if (empty($merchant)) {
  76. util::fail('商家无效');
  77. }
  78. /**
  79. * authScope 参数的说明
  80. * 1.用户通过公众号菜单打开网页,因为已经关注过了公众号,已经生成过用户信息,所以只要使用snsapi_base静默方式拿到openid就可以
  81. * 2.为了保证付款页的访问速度,暂时只要通过snsapi_base静默方式拿到openid先生成可以用的用户信息即可
  82. * 3.其它情况需要获取用户完整信息的,使用snsapi_userinfo让用户授权获取信息即可
  83. * 4.snsapi_base静默方式使用的场景更多,所以参数authScope默认使用snsapi_base
  84. */
  85. $authScope = isset($get['authScope']) ? $get['authScope'] : 'snsapi_base';
  86. $urlEncode = stringUtil::urlSafeB64Encode($url);
  87. //微信授权获取code shish 2019.11.24
  88. wxUtil::getCode($urlEncode, $merchant, $authScope);
  89. util::end();
  90. }
  91. //微信授权获取用户信息 shish 2019.11.20
  92. public function actionGetUserInfo()
  93. {
  94. $get = Yii::$app->request->get();
  95. $code = isset($get['code']) ? $get['code'] : '';
  96. if (empty($code)) {
  97. util::fail('没有获取用户code');
  98. }
  99. if (isset($get['state']) && $get['state'] == 'authdeny') {
  100. util::fail('auth fail');
  101. }
  102. $urlEncode = $get['url'];
  103. $url = stringUtil::urlSafeBase64Decode($urlEncode);
  104. $account = httpUtil::getAccount($url);
  105. if (empty($account)) {
  106. util::stop('商店ID无效!');
  107. }
  108. $merchant = xhMerchantService::getByAccount($account);
  109. if (empty($merchant)) {
  110. util::stop('商店无效');
  111. }
  112. $merchantId = $merchant['id'];
  113. $authScope = isset($get['authScope']) ? $get['authScope'] : '';
  114. if (empty($authScope)) {
  115. util::stop('没有授权类型');
  116. }
  117. $data = wxUtil::getOpenId($code, $merchant);
  118. if ($data === false) {
  119. //微信授权获取code shish 2019.11.24
  120. wxUtil::getCode($urlEncode, $merchant, $authScope);
  121. util::end();
  122. }
  123. $openId = $data['openid'];
  124. $access_token = $data['access_token'];
  125. $user = UserService::getByOpenId($openId, $merchantId);
  126. //用户来源
  127. $userSource = Yii::$app->dict->getValue('userSourceGetId', 'official');
  128. $source = $userSource['name'];
  129. if (empty($user)) {
  130. //$authScope 默认是 snsapi_base 公众号菜单打开网页(已经关注公众号,有完整信息)、打开付款页(有些客户只付款,没有后续可以不用登记完整信息),只需要获取openid
  131. $originalInfo = [];
  132. $originalInfo['openId'] = $openId;
  133. $originalInfo['merchantId'] = $merchantId;
  134. //没有关注公众号,需要获取用户完整信息的情况,则通过弹框授权
  135. if ($authScope == 'snsapi_userinfo') {
  136. $baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
  137. $originalInfo = array_merge($baseInfo, $originalInfo);
  138. $originalInfo['isFull'] = 1;
  139. $originalInfo['unionId'] = $baseInfo['unionid'];
  140. $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
  141. $originalInfo['nickName'] = $baseInfo['nickname'];
  142. }
  143. $user = UserService::replaceUser($originalInfo, $source, $merchantId);
  144. } else {
  145. if ($user['isFull'] == 0) {
  146. if ($authScope == 'snsapi_userinfo') {
  147. $baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
  148. $originalInfo = $baseInfo;
  149. $originalInfo['isFull'] = 1;
  150. $originalInfo['unionId'] = $baseInfo['unionid'];
  151. $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
  152. $originalInfo['openId'] = $baseInfo['openid'];
  153. $originalInfo['nickName'] = $baseInfo['nickname'];
  154. $originalInfo['merchantId'] = $merchantId;
  155. $user = UserService::replaceUser($originalInfo, $source, $merchantId);
  156. }
  157. }
  158. }
  159. //这个的基类没有设置统一的全局变量,这里进行设置一次
  160. $userId = $user['id'];
  161. //获取token
  162. $token = jwt::getNewToken($userId);
  163. $url = strpos($url, '?') === false ? $url . '?token=' . $token : $url . '&token=' . $token;
  164. $this->redirect($url);
  165. }
  166. }