| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- <?php
- namespace mini\controllers;
- use biz\user\services\UserAssetService;
- use biz\user\services\UserOldDataService;
- use biz\user\services\UserService;
- use common\components\stringUtil;
- use common\components\util;
- use common\services\xhMerchantExtendService;
- use common\services\xhMerchantService;
- use common\services\xhUserService;
- use Yii;
- use yii\web\Controller;
- use yii\helpers\Json;
- use linslin\yii2\curl;
- class UserController extends BaseController
- {
-
- public $enableCsrfValidation = false;
-
- public $secretKey = 'wx3hj5a26af5c17fb86921f3';
-
- public $withoutLogin = ['get-info'];
-
- //首次进入小程序,通过code获取用户的openid
- public function actionGetInfo()
- {
- $code = Yii::$app->request->get('code', '');
- if (empty($code)) {
- util::failInfo('没有CODE信息');
- }
- $merchant = $this->merchant;
- $account = $this->merchantId;
- $appId = $merchant['miniAppId'];
- $appSecret = $merchant['miniAppSecret'];
- $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appId}&secret={$appSecret}&js_code={$code}&grant_type=authorization_code";
-
- $curl = new curl\Curl();
- $result = $curl->get($url);
- $arr = Json::decode($result);
- $sessionKey = isset($arr['session_key']) ? $arr['session_key'] : '';
- $openid = isset($arr['openid']) ? $arr['openid'] : '';
- //用户来源
- $userSource = UserService::$userSourceId['mini']['name'];
- $user = UserService::getByMiniOpenId($openid, $account);
- if (empty($user)) {
- $userInfo = ['miniOpenId' => $openid, 'merchantId' => $account];
- $user = UserService::replaceUser($userInfo, $userSource, $account);
- }
-
- $cacheKey = 'sessionKey_' . $openid;
- Yii::$app->redis->executeCommand('SET', [$cacheKey, $sessionKey]);
-
- $userId = $user['id'];
-
- $asset = UserAssetService::getByUserId($userId);
-
- //敏感信息不输出
- unset($user['password']);
- unset($user['payPassword']);
- unset($user['openId']);
- unset($user['miniOpenId']);
- unset($user['unionId']);
- unset($user['alipayId']);
-
- $merchantExtend = xhMerchantExtendService::getByMerchantId($account);
- $memberIntegral = xhMerchantExtendService::getGradeList($merchantExtend, 'desc');
- $level = $asset['memberLevel'];
- $memberLevelName = '普通会员';
- if (!empty($level)) {
- $memberLevelName = $level . "级会员";
- }
- $discountName = '';
- if (isset($memberIntegral[$level]['discount'])) {
- $currentDiscount = $memberIntegral[$level]['discount'];
- $discountName = '享' . $currentDiscount . '折优惠';
- }
- $asset['memberLevelName'] = $memberLevelName;
- $asset['discountName'] = $discountName;
-
- //token对应的就是userId
- $token = 'SID_MINI_' . $account . '_' . $userId . '_' . date("mdHis") . '_' . stringUtil::charsShuffle(8);
- Yii::$app->redis->executeCommand('SET', [$token, $userId]);
- Yii::$app->redis->executeCommand('EXPIRE', [$token, 30 * 86400]);//过期
-
- $return = ['user' => $user, 'token' => $token, 'userAsset' => $asset, 'merchant' => $this->merchant];
- util::sendJson($return);
- }
-
- //获取用户信息 shish 2019.7.25
- public function actionGetUserInfo()
- {
- $asset = UserAssetService::getByUserId($this->userId);
- $asset['discountShow'] = $asset['discount'] == 100 ? 0 : str_replace('0', '', $asset['discount']);
- util::sendJson(['user' => $this->userInfo, 'userAsset' => $asset]);
- }
-
- //授权获取用户完整信息 shish 2019.7.25
- public function actionGetFullInfo()
- {
- $post = Yii::$app->request->post();
- $iv = $post['iv'];
- $encryptedData = $post['encryptedData'];
- $appId = $this->merchant['miniAppId'];
- $merchantId = $this->merchantId;
- $miniOpenId = $this->userInfo['miniOpenId'];
- if (empty($miniOpenId)) {
- util::failInfo('mini_open_id empty');
- }
- $cacheKey = 'sessionKey_' . $miniOpenId;
- $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
- $weixinMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
- require_once($weixinMiniSecret . '/wxBizDataCrypt.php');
- $pc = new \WXBizDataCrypt($appId, $sessionKey);
- $errCode = $pc->decryptData($encryptedData, $iv, $result);
- if ($errCode != 0) {
- Yii::info($result . ' ' . $errCode);
- util::failInfo('获取用户信息失败');
- }
- Yii::info('小程序获取用户信息:' . $result);
- $originalInfo = Json::decode($result);
- $source = UserService::$userSourceId['mini']['name'];
- $user = UserService::replaceUser($originalInfo, $source, $merchantId);
-
- unset($user['password']);
- unset($user['payPassword']);
- unset($user['openId']);
- unset($user['miniOpenId']);
- unset($user['unionId']);
- unset($user['alipayId']);
-
- $userId = $user['id'];
- $asset = UserAssetService::getByUserId($userId);
-
- //更新下访问时间
- UserService::updateById($userId, ['visitTime' => time()]);
-
- $merchantExtend = xhMerchantExtendService::getByMerchantId($merchantId);
- $memberIntegral = xhMerchantExtendService::getGradeList($merchantExtend, 'desc');
- $level = $asset['memberLevel'];
- $memberLevelName = '普通会员';
- if (!empty($level)) {
- $memberLevelName = $level . "级会员";
- }
- $discountName = '';
- if (isset($memberIntegral[$level]['discount'])) {
- $currentDiscount = $memberIntegral[$level]['discount'];
- $discountName = '享' . $currentDiscount . '折优惠';
- }
- $asset['memberLevelName'] = $memberLevelName;
- $asset['discountName'] = $discountName;
-
- util::successInfo('操作成功', 'A0001', ['user' => $user, 'userAsset' => $asset]);
- }
-
- public function actionGetMobile()
- {
- $post = Yii::$app->request->post();
- $iv = $post['iv'];
- $encryptedData = $post['encryptedData'];
- $merchantId = $this->merchantId;
-
- $merchant = $this->merchant;
- $appId = $merchant['miniAppId'];
- $miniOpenId = $this->userInfo['miniOpenId'];
- $user = $this->userInfo;
- if (!empty($user) && !empty($user['mobile'])) {
- $userId = $user['id'];
- UserService::updateById($userId, ['isMember' => 1]);
- UserAssetService::updateByUserId($userId, ['isMember' => 1]);
- util::successInfo('操作成功', 'A0001', ['mobile' => $user['mobile']]);
- }
- $cacheKey = 'sessionKey_' . $miniOpenId;
- $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
- if (empty($sessionKey)) {
- util::failInfo('sesstion key empty');
- }
- $weixinMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
- require_once($weixinMiniSecret . '/wxBizDataCrypt.php');
-
- $pc = new \WXBizDataCrypt($appId, $sessionKey);
- $errCode = $pc->decryptData($encryptedData, $iv, $result);
- if ($errCode != 0) {
- util::failInfo();
- }
- $arr = Json::decode($result);
- $mobile = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
- $userId = $user['id'];
- /**升级成为会员**/
- UserService::upgradeToMember($userId, $merchantId, ['mobile' => $mobile]);
- $user = UserService::getById($userId);
- util::successInfo('操作成功', 'A0001', ['mobile' => $mobile, 'user' => $user]);
- }
-
- }
|