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 @@ ...@@ -3,6 +3,7 @@
namespace App\Services\user; namespace App\Services\user;
use Api\PhpUtils\Mon\MonUtil;
use App\Models\user\mysql\UserWechatBind; use App\Models\user\mysql\UserWechatBind;
use Api\PhpUtils\Http\Request; use Api\PhpUtils\Http\Request;
use App\Services\user\Weixin\WxBizDataCrypt;; use App\Services\user\Weixin\WxBizDataCrypt;;
...@@ -29,36 +30,42 @@ class UserService ...@@ -29,36 +30,42 @@ class UserService
$code = !empty($params['code']) ? $params['code'] : '';//小程序授权code $code = !empty($params['code']) ? $params['code'] : '';//小程序授权code
$openid = !empty($params['openid']) ? $params['openid'] : '';//小程序授权code $openid = !empty($params['openid']) ? $params['openid'] : '';//小程序授权code
if (empty($code) && empty($openid)) { 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]); throw new UserException(['cus' => 0]);
} }
//需要授权微信 //需要授权微信
//if (empty($openid)) { //if (empty($openid)) {
$appid = \Yaf\Registry::get('config')->wechat->appid; $appid = \Yaf\Registry::get('config')->wechat->appid;
$secret = \Yaf\Registry::get('config')->wechat->secret; $secret = \Yaf\Registry::get('config')->wechat->secret;
$grant_type = 'authorization_code'; $grant_type = 'authorization_code';
$loginUrl = 'https://api.weixin.qq.com/sns/jscode2session'; $loginUrl = 'https://api.weixin.qq.com/sns/jscode2session';
//请求接口,获取用户openid //请求接口,获取用户openid
$params = [ $params = [
'appid' => $appid, 'appid' => $appid,
'secret' => $secret, 'secret' => $secret,
'js_code' => $code, 'js_code' => $code,
'grant_type' => $grant_type, 'grant_type' => $grant_type,
]; ];
$response = (new Request())->post($loginUrl, $params); $begin = microtime(true);
$response = (new Request())->post($loginUrl, $params);
if ($response['code'] == 0) {
$response = $response['response'];
}
if (empty($response) || array_key_exists('errcode', $response)) { $end = microtime(true);
//throw new \Exception('获取openid失败:' . $response['errcode']); $totalTime = round(($end - $begin), 4) * 1000;
FileLog::error("wechatLogin:获取openid失败", json_encode($params, JSON_UNESCAPED_UNICODE),'','jianghaiming@yidian-inc.com'); if ($response['code'] == 0) {
throw new UserException(['cus' => 1]); $response = $response['response'];
} }
//检查系统是已注册 //mon打点
$openid = !empty($response['openid']) ? $response['openid'] : ''; $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 ...@@ -75,34 +82,34 @@ class UserService
$sessionKey = !empty($response['session_key']) ? $response['session_key'] : ''; $sessionKey = !empty($response['session_key']) ? $response['session_key'] : '';
//$newWechatId = UserWechatBind::save($insert); //$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) { if (!$newWechatId) {
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');
FileLog::error("wechatLogin:登陆创建用户失败--insert", json_encode($insert, 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]); throw new UserException(['cus' => 2]);
} }
$user['wechat_id'] = $newWechatId; $user['wechat_id'] = $newWechatId;
$user['openid'] = $response['openid']; $user['openid'] = $response['openid'];
$user['session_key'] = $response['session_key']; $user['session_key'] = $response['session_key'];
}else{ } else {
if (empty($user)) { 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]); throw new UserException(['cus' => 2]);
} }
if (empty($response)){ if (empty($response)) {
FileLog::error("wechatLogin:登陆创建用户失败-- 微信response 获取失败", json_encode($params, JSON_UNESCAPED_UNICODE),'','jianghaiming@yidian-inc.com'); FileLog::error("wechatLogin:登陆创建用户失败-- 微信response 获取失败", json_encode($params, JSON_UNESCAPED_UNICODE), '', 'jianghaiming@yidian-inc.com');
throw new UserException(['cus' => 2]); throw new UserException(['cus' => 2]);
} }
$insert = [ $insert = [
'session_key' => !empty($response['session_key']) ? $response['session_key'] : '', '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); $user['third_session'] = self::generate3rdSession($openid);
return $user; return $user;
} }
/** /**
...@@ -117,9 +124,9 @@ class UserService ...@@ -117,9 +124,9 @@ class UserService
{ {
$openid = !empty($params['openid']) ? $params['openid'] : ''; $openid = !empty($params['openid']) ? $params['openid'] : '';
$encryptedData = !empty($params['encryptedData']) ? $params['encryptedData'] : ''; $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; $appid = \Yaf\Registry::get('config')->wechat->appid;
if (!$encryptedData) { if (!$encryptedData) {
FileLog::error("bindPhone:encryptedData 为空", json_encode($params, JSON_UNESCAPED_UNICODE),'','jianghaiming@yidian-inc.com'); FileLog::error("bindPhone:encryptedData 为空", json_encode($params, JSON_UNESCAPED_UNICODE),'','jianghaiming@yidian-inc.com');
throw new UserException(['cus' => 3]); throw new UserException(['cus' => 3]);
...@@ -204,7 +211,7 @@ class UserService ...@@ -204,7 +211,7 @@ class UserService
public static function userWechatBind($params) public static function userWechatBind($params)
{ {
$data['wechat_id[>]'] = 0; $data['wechat_id[>]'] = 0;
if (!empty($params['openid'])) { if (!empty($params['openid'])) {
...@@ -218,7 +225,7 @@ class UserService ...@@ -218,7 +225,7 @@ class UserService
if (!empty($params['phone'])) { if (!empty($params['phone'])) {
$data['phone'] = $params['phone']; $data['phone'] = $params['phone'];
} }
$userList = UserWechatBind::getRecords($data); $userList = UserWechatBind::getRecords($data);
return $userList; 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