AuthController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace www\controllers;
  3. use biz\auth\services\AuthService;
  4. use biz\weixin\services\WxOpenService;
  5. use common\components\util;
  6. use Yii;
  7. use biz\merchant\services\MerchantService;
  8. use biz\user\services\UserService;
  9. use common\components\httpUtil;
  10. use common\components\jwt;
  11. use common\components\stringUtil;
  12. use common\services\xhMerchantService;
  13. use common\components\wxUtil;
  14. class AuthController extends BaseController
  15. {
  16. public $withoutLogin = ['prepare', 'get-user-info'];
  17. //获取权限树 shish 2019.11.24
  18. public function actionTree()
  19. {
  20. $get = Yii::$app->request->get();
  21. $endId = isset($get['endId']) ? $get['endId'] : 1;
  22. $tree = AuthService::getTree($endId);
  23. util::success($tree);
  24. }
  25. //添加权限 shish 2019.11.24
  26. public function actionAdd()
  27. {
  28. $post = Yii::$app->request->post();
  29. $endId = isset($post['endId']) ? $post['endId'] : 1;
  30. $authName = isset($post['authName']) ? $post['authName'] : '';
  31. $list = AuthService::getNameList($endId);
  32. if (in_array($authName, $list)) {
  33. util::fail('名称已经存在');
  34. }
  35. AuthService::add($_POST);
  36. util::complete();
  37. }
  38. //修改 shish 2019.11.24
  39. public function actionUpdate()
  40. {
  41. $post = Yii::$app->request->post();
  42. $id = isset($post['id']) ? $post['id'] : 0;
  43. $auth = AuthService::getById($id);
  44. if (empty($auth)) {
  45. util::fail('没有找到权限');
  46. }
  47. $endId = $auth['endId'];
  48. $nameList = AuthService::getNameList($endId);
  49. $authName = isset($post['authName']) ? $post['authName'] : '';
  50. if ($authName != $auth['authName'] && in_array($authName, $nameList)) {
  51. util::fail('名称已经存在');
  52. }
  53. AuthService::updateById($id, $_POST);
  54. util::complete();
  55. }
  56. //删除权限 shish 2020.1.7
  57. public function actionDelete()
  58. {
  59. $id = Yii::$app->request->get('id');
  60. util::complete('删除成功');
  61. }
  62. //准备授权 shish 2019.11.19
  63. public function actionPrepare()
  64. {
  65. $get = Yii::$app->request->get();
  66. $url = isset($get['url']) && !empty($get['url']) ? $get['url'] : '';
  67. if (empty($url)) {
  68. util::fail('没有网址');
  69. }
  70. $account = WxOpenService::getPlatformAccount();
  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. $open = WxOpenService::getById(1);
  105. if (empty($open)) {
  106. util::fail('没有找到平台信息');
  107. }
  108. $account = isset($open['merchantId']) ? $open['merchantId'] : 0;
  109. if (empty($account)) {
  110. util::stop('平台没有绑定公众号');
  111. }
  112. $merchant = xhMerchantService::getByAccount($account);
  113. if (empty($merchant)) {
  114. util::stop('商店无效');
  115. }
  116. $merchantId = $merchant['id'];
  117. $authScope = isset($get['authScope']) ? $get['authScope'] : '';
  118. if (empty($authScope)) {
  119. util::stop('没有授权类型');
  120. }
  121. $data = wxUtil::getOpenId($code, $merchant);
  122. if ($data === false) {
  123. //微信授权获取code shish 2019.11.24
  124. wxUtil::getCode($urlEncode, $merchant, $authScope);
  125. util::end();
  126. }
  127. Yii::info('abc');
  128. $openId = $data['openid'];
  129. $access_token = $data['access_token'];
  130. $user = UserService::getByOpenId($openId, $merchantId);
  131. //用户来源
  132. $userSource = Yii::$app->dict->getValue('userSourceGetId', 'official');
  133. $source = $userSource['name'];
  134. if (empty($user)) {
  135. //$authScope 默认是 snsapi_base 公众号菜单打开网页(已经关注公众号,有完整信息)、打开付款页(有些客户只付款,没有后续可以不用登记完整信息),只需要获取openid
  136. $originalInfo = [];
  137. $originalInfo['openId'] = $openId;
  138. $originalInfo['merchantId'] = $merchantId;
  139. //没有关注公众号,需要获取用户完整信息的情况,则通过弹框授权
  140. if ($authScope == 'snsapi_userinfo') {
  141. $baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
  142. $originalInfo = array_merge($baseInfo, $originalInfo);
  143. $originalInfo['isFull'] = 1;
  144. $originalInfo['unionId'] = $baseInfo['unionid'];
  145. $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
  146. $originalInfo['nickName'] = $baseInfo['nickname'];
  147. }
  148. $user = UserService::replaceUser($originalInfo, $source, $merchantId);
  149. } else {
  150. if ($user['isFull'] == 0) {
  151. if ($authScope == 'snsapi_userinfo') {
  152. $baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
  153. $originalInfo = $baseInfo;
  154. $originalInfo['isFull'] = 1;
  155. $originalInfo['unionId'] = $baseInfo['unionid'];
  156. $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
  157. $originalInfo['openId'] = $baseInfo['openid'];
  158. $originalInfo['nickName'] = $baseInfo['nickname'];
  159. $originalInfo['merchantId'] = $merchantId;
  160. $user = UserService::replaceUser($originalInfo, $source, $merchantId);
  161. }
  162. }
  163. }
  164. //这个的基类没有设置统一的全局变量,这里进行设置一次
  165. $userId = $user['id'];
  166. //获取token
  167. $token = jwt::getNewToken($userId);
  168. $url = strpos($url, '?') === false ? $url . '?token=' . $token : $url . '&token=' . $token;
  169. $this->redirect($url);
  170. }
  171. }