Commit a760e4fd authored by pengfei's avatar pengfei

Merge branch 'colonel' of https://git.yidian-inc.com:8021/bp/goods into colonel

parents fa50faa6 eac4daa5
......@@ -3,6 +3,7 @@
namespace App\Services\user;
use Api\PhpUtils\Mon\MonUtil;
use App\Models\user\mysql\UserWechatBind;
use Api\PhpUtils\Http\Request;
use App\Services\user\Weixin\WxBizDataCrypt;;
......@@ -29,36 +30,42 @@ class UserService
$code = !empty($params['code']) ? $params['code'] : '';//小程序授权code
$openid = !empty($params['openid']) ? $params['openid'] : '';//小程序授权code
if (empty($code) && empty($openid)) {
FileLog::error("wechatLogin:获取参数为空", json_encode($params, JSON_UNESCAPED_UNICODE),'','jianghaiming@yidian-inc.com');
FileLog::error("wechatLogin:获取参数为空", json_encode($params, JSON_UNESCAPED_UNICODE), '', 'jianghaiming@yidian-inc.com');
throw new UserException(['cus' => 0]);
}
//需要授权微信
//if (empty($openid)) {
$appid = \Yaf\Registry::get('config')->wechat->appid;
$secret = \Yaf\Registry::get('config')->wechat->secret;
$grant_type = 'authorization_code';
$loginUrl = 'https://api.weixin.qq.com/sns/jscode2session';
//请求接口,获取用户openid
$params = [
'appid' => $appid,
'secret' => $secret,
'js_code' => $code,
'grant_type' => $grant_type,
];
$response = (new Request())->post($loginUrl, $params);
if ($response['code'] == 0) {
$response = $response['response'];
}
$appid = \Yaf\Registry::get('config')->wechat->appid;
$secret = \Yaf\Registry::get('config')->wechat->secret;
$grant_type = 'authorization_code';
$loginUrl = 'https://api.weixin.qq.com/sns/jscode2session';
//请求接口,获取用户openid
$params = [
'appid' => $appid,
'secret' => $secret,
'js_code' => $code,
'grant_type' => $grant_type,
];
$begin = microtime(true);
$response = (new Request())->post($loginUrl, $params);
if (empty($response) || array_key_exists('errcode', $response)) {
//throw new \Exception('获取openid失败:' . $response['errcode']);
FileLog::error("wechatLogin:获取openid失败", json_encode($params, JSON_UNESCAPED_UNICODE),'','jianghaiming@yidian-inc.com');
throw new UserException(['cus' => 1]);
}
//检查系统是已注册
$openid = !empty($response['openid']) ? $response['openid'] : '';
$end = microtime(true);
$totalTime = round(($end - $begin), 4) * 1000;
if ($response['code'] == 0) {
$response = $response['response'];
}
//mon打点
$env = Application::app()->environ() ?? "dev";
MonUtil::proxyMon($loginUrl, $response['code'], 'weixin_'.$env, $totalTime);
if (empty($response) || array_key_exists('errcode', $response)) {
//throw new \Exception('获取openid失败:' . $response['errcode']);
FileLog::error("wechatLogin:获取openid失败", json_encode($params, JSON_UNESCAPED_UNICODE), '', 'jianghaiming@yidian-inc.com');
throw new UserException(['cus' => 1]);
}
//检查系统是已注册
$openid = !empty($response['openid']) ? $response['openid'] : '';
//}
......@@ -75,34 +82,34 @@ class UserService
$sessionKey = !empty($response['session_key']) ? $response['session_key'] : '';
//$newWechatId = UserWechatBind::save($insert);
$newWechatId =UserWechatBind::insertDuplicate($insert, ['session_key' => $sessionKey,'create_time' => date("Y-m-d H:i:s")]);
$newWechatId = UserWechatBind::insertDuplicate($insert, ['session_key' => $sessionKey, 'create_time' => date("Y-m-d H:i:s")]);
if (!$newWechatId) {
FileLog::error("wechatLogin:登陆创建用户失败", json_encode($params, JSON_UNESCAPED_UNICODE),'','jianghaiming@yidian-inc.com');
FileLog::error("wechatLogin:登陆创建用户失败--insert", json_encode($insert, JSON_UNESCAPED_UNICODE),'','jianghaiming@yidian-inc.com');
FileLog::error("wechatLogin:登陆创建用户失败", json_encode($params, JSON_UNESCAPED_UNICODE), '', 'jianghaiming@yidian-inc.com');
FileLog::error("wechatLogin:登陆创建用户失败--insert", json_encode($insert, JSON_UNESCAPED_UNICODE), '', 'jianghaiming@yidian-inc.com');
throw new UserException(['cus' => 2]);
}
$user['wechat_id'] = $newWechatId;
$user['openid'] = $response['openid'];
$user['session_key'] = $response['session_key'];
}else{
} else {
if (empty($user)) {
FileLog::error("wechatLogin:登陆创建用户失败-- 微信response", json_encode($response, JSON_UNESCAPED_UNICODE),'','jianghaiming@yidian-inc.com');
FileLog::error("wechatLogin:登陆创建用户失败-- 微信response", json_encode($response, JSON_UNESCAPED_UNICODE), '', 'jianghaiming@yidian-inc.com');
throw new UserException(['cus' => 2]);
}
if (empty($response)){
FileLog::error("wechatLogin:登陆创建用户失败-- 微信response 获取失败", json_encode($params, JSON_UNESCAPED_UNICODE),'','jianghaiming@yidian-inc.com');
if (empty($response)) {
FileLog::error("wechatLogin:登陆创建用户失败-- 微信response 获取失败", json_encode($params, JSON_UNESCAPED_UNICODE), '', 'jianghaiming@yidian-inc.com');
throw new UserException(['cus' => 2]);
}
$insert = [
'session_key' => !empty($response['session_key']) ? $response['session_key'] : '',
];
$newWechatId = UserWechatBind::save($insert,['openid' => $openid]);
$newWechatId = UserWechatBind::save($insert, ['openid' => $openid]);
}
$user['third_session'] = self::generate3rdSession($openid);
return $user;
return $user;
}
/**
......@@ -117,9 +124,9 @@ class UserService
{
$openid = !empty($params['openid']) ? $params['openid'] : '';
$encryptedData = !empty($params['encryptedData']) ? $params['encryptedData'] : '';
$iv = !empty($params['iv']) ? $params['iv'] : '';
$iv = !empty($params['iv']) ? $params['iv'] : '';
$appid = \Yaf\Registry::get('config')->wechat->appid;
if (!$encryptedData) {
FileLog::error("bindPhone:encryptedData 为空", json_encode($params, JSON_UNESCAPED_UNICODE),'','jianghaiming@yidian-inc.com');
throw new UserException(['cus' => 3]);
......@@ -204,7 +211,7 @@ class UserService
public static function userWechatBind($params)
{
$data['wechat_id[>]'] = 0;
if (!empty($params['openid'])) {
......@@ -218,7 +225,7 @@ class UserService
if (!empty($params['phone'])) {
$data['phone'] = $params['phone'];
}
$userList = UserWechatBind::getRecords($data);
return $userList;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment