Commit 3df47e9d authored by zhuwei's avatar zhuwei

小程序获取oppenid等

parent 95e0ef04
......@@ -13,6 +13,7 @@ namespace app\api\controller;
use app\api\extend\Basic;
use app\api\service\RegisterService;
use app\api\service\WxSdkService;
use app\api\untils\JwtUntils;
use app\model\Users;
use app\model\UWxInfo;
......@@ -147,7 +148,75 @@ class WxSdk extends Basic
return $this->response("101", "request error:" . $exception);
}
} else {
return $this->response("101", "没有找到用户对应微信记录");
}
}
/**
* 获取小程序openid、unionid等数据
* @return \think\Response
*/
public function getOpenidByCode()
{
$params = $this->params;
/* $params = array(
"code" => "123132123123123",
);*/
$checkResult = $this->validate($params, "UWxInfoValidate.getOpenidByCode");
if (true !== $checkResult) {
return $this->response("101", $checkResult);
}
$s_wx = new WxSdkService();
$res = $s_wx->getOpenidByCode($params['code']);
if($res['errcode'] == 0){
return $this->response("200", "request success", $res);
}else{
return $this->response("101", $res['errmsg']);
}
}
/**
* 保存微信登录信息
* @return \think\Response
*/
public function saveWXInfoByPortableProgram()
{
$params = $this->params;
/* $params = array(
"wx_open_id" => "123132123123123",
"wx_union_id" => "123132123123123",
"buyer_nick" => "222222222",
"buyer_img" => "123123123",
"sex" => 1,
"province" => "123123123",
"city" => "123123123",
"source" => 1,// 0:h5邀请 1安卓 2苹果',
);*/
$checkResult = $this->validate($params, "UWxInfoValidate.add");
if (true !== $checkResult) {
return $this->response("101", $checkResult);
}
$wx_union_id = $params["wx_union_id"];
// 验证open_id是否已经存在,存在则更新
$wxInfoObj = $this->wxInfoModel->getWxInfoByOpenId($wx_union_id);
try {
if ($wxInfoObj && count($wxInfoObj) > 0) {
$params["id"] = $wxInfoObj[0]["id"];
$id = $this->wxInfoModel->updateWxInfo($params);
} else {
$id = $this->wxInfoModel->addWxInfo($params);
}
} catch (Exception $exception) {
return $this->response("101", "request error:" . $exception);
}
if ($id > 0 ) {
return $this->response("200", "request success");
} else{
return $this->response("101", "request exception");
}
}
......
......@@ -78,6 +78,9 @@ class Basic extends Controller
"office_api/getBuildingRoomList",
"office_api/filtrateConditionRoom",
"office_api/getOfficeBuildingInfoShare",
"api/getOpenidByCode",
"api/saveWXInfoByPortableProgram",
);
/**
......
<?php
namespace app\api\service;
use app\chat\utils\CurlUtil;
/**
* Created by PhpStorm.
* User : zw
......@@ -8,5 +10,43 @@ namespace app\api\service;
* Intro:
*/
class WxSdkService{
private $appId;
private $appSecret;
public function __construct()
{
// $this->appId = "wxbbeb8cd2f4b72aea";
// $this->appSecret = "9ac6d1680553f4b554f3b46d263fbfb3";
if (strpos($this->http_host(), 'api') !== false) {
$this->appId = "wxbbeb8cd2f4b72aea";
$this->appSecret = "9ac6d1680553f4b554f3b46d263fbfb3";
} else {
$this->appId = "wxd30d74d1126d3278";//测试
$this->appSecret = "f29c8c502549020d2193a464be9632dc";//测试
}
}
/**
* 获取小程序openid、unionid等数据
* @param $code
* @return \app\chat\utils\CurlResponse|mixed
*/
public function getOpenidByCode($code)
{
$curl = new CurlUtil();
$url = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->appId}&secret={$this->appSecret}&js_code={$code}&grant_type=authorization_code";
$result = $curl->get($url);
$result = json_decode($result, "true");
return $result;
}
public function http_host()
{
$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
$host=$http_type.$_SERVER['HTTP_HOST'];
return $host;
}
}
\ No newline at end of file
......@@ -19,6 +19,8 @@ class UWxInfoValidate extends Validate
'sex' => 'require|number',
'source' => 'require|number',
'user_id' => 'require|number|gt:0',
'code' => 'require',
];
protected $message = [
......@@ -33,11 +35,13 @@ class UWxInfoValidate extends Validate
'user_id.require' => 'user_id为必须字段',
'user_id.number' => 'user_id必须是数字',
'user_id.gt' => 'user_id必须大于0',
'code.require' => 'code不能为空',
];
protected $scene = [
'add' => [ 'wx_open_id','wx_union_id', 'sex', 'source' ],
'select' => [ 'wx_open_id' ],
'bind' => [ 'wx_open_id', 'user_id' ],
'add' => [ 'wx_open_id', 'wx_union_id', 'sex', 'source' ],
'select' => [ 'wx_open_id' ],
'bind' => [ 'wx_open_id', 'user_id' ],
'getOpenidByCode' => [ 'code' ],
];
}
\ No newline at end of file
......@@ -655,6 +655,9 @@ Route::group('api', [
'saveWxInfo' => ['api/WxSdk/saveWxInfo', ['method' => 'POST']], //wx
'bindUserId' => ['api/WxSdk/bindUserId', ['method' => 'POST|GET']], //wx
'getOpenidByCode' => ['api/WxSdk/getOpenidByCode', ['method' => 'POST']], //wx
'saveWXInfoByPortableProgram' => ['api/WxSdk/saveWXInfoByPortableProgram', ['method' => 'POST']],
'sendCode' => ['api/Register/registerSendCode', ['method' => 'POST']],
'userVerify' => ['api/Register/registerVerify', ['method' => 'POST']],
......
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