Commit af75f958 authored by clone's avatar clone

微信绑定

parent e67e7d01
......@@ -25,12 +25,16 @@ class WxSdk extends Basic
$this->wxInfoModel = new UWxInfo();
}
/**
* wx授权拉取数据
* @return \think\Response
*/
public function saveWxInfo()
{
$params = $this->params;
/* $params = array(
/* $params = array(
"wx_open_id" => "123132123123123",
"buyer_nick" => "123123123",
"buyer_nick" => "222222222",
"buyer_img" => "123123123",
"sex" => 1,
"province" => "123123123",
......@@ -42,15 +46,63 @@ class WxSdk extends Basic
return $this->response("101", $checkResult);
}
try{
$wx_open_id = $params["wx_open_id"];
// 验证open_id是否已经存在,存在则更新
$wxInfoObj = $this->wxInfoModel->getWxInfoByOpenId($wx_open_id);
try {
if ($wxInfoObj && count($wxInfoObj) > 0) {
$params["id"] = $wxInfoObj[0]["id"];
$id = $this->wxInfoModel->updateWxInfo($params);
} else {
$id = $this->wxInfoModel->addWxInfo($params);
if($id > 0){
return $this->response("200","request success",["id"=>$id]);
}
}catch (Exception $exception){
return $this->response("101","request error:".$exception);
} catch (Exception $exception) {
return $this->response("101", "request error:" . $exception);
}
if ($id > 0) {
return $this->response("200", "request success", [ "id" => $id ]);
} else {
return $this->response("101", "request exception");
}
}
/**
* 绑定用户 记录最后一次绑定
* @return \think\Response
*/
public function bindUserId()
{
$params = $this->params;
/* $params = array(
"wx_open_id" => "123132123123123",
"user_id" => 2
);*/
$checkResult = $this->validate($params, "UWxInfoValidate.bind");
if (true !== $checkResult) {
return $this->response("101", $checkResult);
}
$wx_open_id = $params["wx_open_id"];
// 验证open_id是否已经存在,存在则更新
$wxInfoObj = $this->wxInfoModel->getWxInfoByOpenId($wx_open_id);
if ($wxInfoObj && count($wxInfoObj) > 0) {
$params["id"] = $wxInfoObj[0]["id"];
try {
$id = $this->wxInfoModel->updateWxInfo($params);
if ($id > 0) {
return $this->response("200", "request success", [ "id" => $id ]);
} else {
return $this->response("101", "绑定失败");
}
} catch (Exception $exception) {
return $this->response("101", "request error:" . $exception);
}
}else{
return $this->response("101", "没有找到用户对应微信记录" );
}
}
......
<?php
namespace app\api\service;
/**
* Created by PhpStorm.
* User : zw
* Date : 2018/6/7
* Time : 14:06
* Intro:
*/
class WxSdkService{
}
\ No newline at end of file
......@@ -17,6 +17,7 @@ class UWxInfoValidate extends Validate
'wx_open_id' => 'require|length:10,50',
'sex' => 'require|number',
'source' => 'require|number',
'user_id' => 'require|number|gt:0',
];
protected $message = [
......@@ -26,10 +27,14 @@ class UWxInfoValidate extends Validate
'sex.number' => '性别必须是数字',
'source.require' => '来源为必须字段',
'source.number' => '来源必须是数字',
'user_id.require' => 'user_id为必须字段',
'user_id.number' => 'user_id必须是数字',
'user_id.gt' => 'user_id必须大于0',
];
protected $scene = [
'add' => [ 'wx_open_id', 'sex','source' ],
'add' => [ 'wx_open_id', 'sex', 'source' ],
'select' => [ 'wx_open_id' ],
'bind' => [ 'wx_open_id', 'user_id' ],
];
}
\ No newline at end of file
......@@ -18,45 +18,101 @@ class UWxInfo extends Model
// 设置当前模型对应的完整数据表名称
protected $table = 'u_wx_info';
public function __construct()
{
}
/**
* 新增微信拉取用户信息
* @param $params
* @return int|string
* @throws Exception
*/
public function addWxInfo($params)
public function addWxInfo(array $params): int
{
$wxBin = $this->wxInfoBin($params);
Db::startTrans();
try{
try {
$id = $this->insertGetId($wxBin);
Db::commit();
return $id;
}catch (Exception $exception){
} catch (Exception $exception) {
Db::rollback();
throw $exception;
}
}
/**
* 修改微信拉取用户信息
* @param array $params
* @return int
* @throws Exception
*/
public function updateWxInfo(array $params): int
{
$wxBin = $this->wxInfoBin($params);
Db::startTrans();
try {
$this->where([ "id" => $wxBin["id"] ])->update($wxBin);
Db::commit();
return $params["id"];
} catch (Exception $exception) {
Db::rollback();
throw $exception;
}
}
/**
* 根据微信open_id获取id
* @param string $open_id
* @return false|\PDOStatement|string|\think\Collection
*/
public function getWxInfoByOpenId(string $open_id)
{
$where_["wx_open_id"] = $open_id;
$where_["is_del"] = 0;
return $this
->field("id")
->where($where_)
->select();
}
/**
* bin
* @param $params
* @return array
*/
private function wxInfoBin($params)
{
$arr = [];
if (isset($params["id"])) {
$arr["wx_open_id"] = $params["id"];
$arr["id"] = $params["id"];
} else {
$arr["create_time"] = date("Y-m-d H:i:s", time());
}
if (isset($params["user_id"])) {
$arr["user_id"] = $params["user_id"];
}
if (isset($params["wx_open_id"])) {
$arr["wx_open_id"] = $params["wx_open_id"];
}
if (isset($params["buyer_nick"])) {
$arr["buyer_nick"] = $params["buyer_nick"];
}
if (isset($params["buyer_img"])) {
$arr["buyer_img"] = $params["buyer_img"];
}
if (isset($params["sex"])) {
$arr["sex"] = $params["sex"];
}
if (isset($params["province"])) {
$arr["province"] = $params["province"];
}
if (isset($params["city"])) {
$arr["city"] = $params["city"];
}
if (isset($params["source"])) {
$arr["source"] = $params["source"];
$arr["create_time"] = date("Y-m-d H:i:s", time());
}
$arr["update_time"] = date("Y-m-d H:i:s", time());
return $arr;
}
......
......@@ -320,6 +320,7 @@ Route::group('api', [
'saveWxInfo' => ['api/WxSdk/saveWxInfo', [ 'method' => 'POST|GET' ] ], //wx
'bindUserId' => ['api/WxSdk/bindUserId', [ 'method' => 'POST|GET' ] ], //wx
]);
Route::group('chat', [
......
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