...
|
...
|
@@ -2,11 +2,11 @@ |
|
|
/**
|
|
|
* Created by PhpStorm.
|
|
|
* Login: Kevin
|
|
|
* Date: 2022/06/12
|
|
|
* Time: 15:34
|
|
|
* Date: 2023/03/22
|
|
|
* Time: 14:10
|
|
|
*/
|
|
|
|
|
|
namespace app\api\controller\v1;
|
|
|
namespace app\api\controller\v2;
|
|
|
|
|
|
use lib\WXBizDataCrypt;
|
|
|
use think\Db;
|
...
|
...
|
@@ -17,204 +17,66 @@ header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Ac |
|
|
header('Access-Control-Allow-Methods: GET, POST, PUT');
|
|
|
|
|
|
class Login extends Base
|
|
|
{ /**
|
|
|
* 12.小程序授权注册用户、返回用户信息
|
|
|
{
|
|
|
/**
|
|
|
* 1、小程序授权注册用户、返回用户信息
|
|
|
*/
|
|
|
public function get_user_by_shouquan()
|
|
|
{
|
|
|
print_r(123);return;
|
|
|
$appid = $this->AppID;
|
|
|
$AppSecret = $this->AppSecret;
|
|
|
$post = $this->request->post();
|
|
|
$code = $post['code'];// I('post.code');
|
|
|
$encryptedData = $post['encryptedData'];//I('post.encryptedData');
|
|
|
$iv = $post['iv'];//I('post.iv');
|
|
|
|
|
|
|
|
|
$encryptedData = urldecode($encryptedData);
|
|
|
$iv = urldecode($iv);
|
|
|
//file_put_contents("ccc_v2.txt", "授权0-提交的内容:" . date("Y-m-d H:i:s") . json_encode($post) . PHP_EOL, FILE_APPEND);
|
|
|
$url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $AppSecret . "&js_code=" . $code . "&grant_type=authorization_code";
|
|
|
$res = json_decode(http_request($url), true);
|
|
|
file_put_contents("1.txt","8-1. ".($code)."\r\n",FILE_APPEND);
|
|
|
$result = http_request($url);
|
|
|
file_put_contents("111111111111222.txt","3-1. ".($result)."\r\n",FILE_APPEND);
|
|
|
$res = json_decode($result, true);
|
|
|
print_r($res);return;
|
|
|
if (!$res) {
|
|
|
$res = json_decode(http_request($url), true);
|
|
|
}
|
|
|
|
|
|
$sessionKey = $res['session_key'];
|
|
|
$openid = $res['openid'];//获取用户openid
|
|
|
//$unionid = $res['unionid'];//获取用户openid
|
|
|
// file_put_contents("ccc.txt", "授权1:" . date("Y-m-d H:i:s") . ":" . json_encode($res, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
|
|
|
|
|
|
require_once '../extend/lib/WXBizDataCrypt.class.php';
|
|
|
|
|
|
$pc = new WXBizDataCrypt($appid, $sessionKey);
|
|
|
$errCode = $pc->decryptData($encryptedData, $iv, $data);
|
|
|
|
|
|
$data = json_decode($data, true);
|
|
|
// file_put_contents("ccc.txt", "授权2:" . date("Y-m-d H:i:s") . ":" . $errCode . PHP_EOL, FILE_APPEND);
|
|
|
// file_put_contents("ccc.txt", "授权3:" . date("Y-m-d H:i:s") . ":" . json_encode($data, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
|
|
|
|
|
|
if ($errCode != 0) {
|
|
|
$ajax['code'] = 0;
|
|
|
$ajax['info'] = $errCode;
|
|
|
$errCode = $errCode == "-41001" || $errCode == "-41003" ? "授权失败,请尝试重新授权" : $errCode;
|
|
|
$this->error($errCode);
|
|
|
}
|
|
|
if ($openid) {
|
|
|
$openid_info = [
|
|
|
"openid" => $openid,
|
|
|
//"unionid" => $unionid,
|
|
|
"from" => "wx",
|
|
|
];
|
|
|
if (!empty($data['nickName'])) {
|
|
|
$openid_info['nickName'] = $data['nickName'];
|
|
|
}
|
|
|
if (!empty($data['gender'])) {
|
|
|
$openid_info['gender'] = $data['gender'];
|
|
|
}
|
|
|
if (!empty($data['avatarUrl'])) {
|
|
|
$openid_info['avatarUrl'] = $data['avatarUrl'];
|
|
|
}
|
|
|
if (!empty($data['country'])) {
|
|
|
$openid_info['country'] = $data['country'];
|
|
|
}
|
|
|
if (!empty($data['province'])) {
|
|
|
$openid_info['province'] = $data['province'];
|
|
|
}
|
|
|
if (!empty($data['city'])) {
|
|
|
$openid_info['city'] = $data['city'];
|
|
|
}
|
|
|
if (!empty($data['phoneNumber'])) {
|
|
|
$openid_info['phoneNumber'] = $data['phoneNumber'];
|
|
|
}
|
|
|
insert_openid_info($openid_info);//更新下微信用户信息到数据库
|
|
|
}
|
|
|
|
|
|
//直接通过unionid 查找用户信息
|
|
|
$userres = Db::name("user")->where("wx_xcx_openid", $openid)->order("id desc")->find();
|
|
|
|
|
|
if (empty($userres)) {
|
|
|
//如果通过小程序openid找不到会员
|
|
|
//注册处理
|
|
|
$this->error("授权失败,请尝试重新授权");
|
|
|
//$res = json_decode(send_post($url), true);
|
|
|
}else{
|
|
|
//查询到openid 通过openid查询到用户
|
|
|
$user=Db::name("user")->where('wx_xcx_openid',$res['openid'])->find();
|
|
|
if($user){
|
|
|
//查询到了用户信息
|
|
|
$this->bind($user['id'],$user['moblie']);
|
|
|
}else{
|
|
|
//未查询到用户信息 添加用户信息
|
|
|
$username = $res['openid'] ? $res['openid'] : suiji_num("TY");
|
|
|
$extend_data = [
|
|
|
"wx_xcx_openid" => $openid,
|
|
|
"avatar" => "/default.png",
|
|
|
"wx_xcx_openid" => $res['openid'],
|
|
|
];
|
|
|
$username = $openid ? $openid : suiji_num("TY");
|
|
|
$ret = $this->auth->register($username, "a123456", '', $data['phoneNumber'], $extend_data);
|
|
|
if ($ret) {
|
|
|
$ret = $this->auth->register($username, "a123456", '','', $extend_data);
|
|
|
if($ret){
|
|
|
$return_data = $this->auth->getUserinfo();
|
|
|
|
|
|
$is_mng_user = 0;//是否是社区管理员 0不是 1是
|
|
|
$mng_area = [];//管辖区域(多个)
|
|
|
$area = Db::name("hc_area_code_mng")->alias("a")
|
|
|
->join("hc_area_code b", "b.code=a.mng_code")
|
|
|
->where("a.mng_user_id", $return_data['id'])->field("b.code,b.name")->select();
|
|
|
if (!empty($area)) {
|
|
|
$is_mng_user = 1;
|
|
|
$mng_area = $area;
|
|
|
}
|
|
|
$return_data['is_mng_user'] = $is_mng_user;
|
|
|
$return_data['mng_area'] = $mng_area;
|
|
|
|
|
|
if (empty($return_data['mobile'])) {
|
|
|
$this->success("授权成功,请继续认证手机号后才能正常使用", $return_data, 2);
|
|
|
} else {
|
|
|
//这里是新注册用户,有手机号,需要判断下手机号有没有在区域管理员中存在,若存在就将id绑定过去
|
|
|
$bind = Db::name("hc_area_code_mng")->where("mng_admin_phone", $return_data['mobile'])->order("id desc")->find();
|
|
|
if (!empty($bind)) {
|
|
|
Db::name("hc_area_code_mng")->where("mng_admin_phone", $return_data['mobile'])->update(["mng_user_id" => $return_data['id']]);
|
|
|
if ($bind['mng_admin_name']) {
|
|
|
//将管理员姓名更新到用户表中
|
|
|
Db::name("user")->where("id", $return_data['id'])->update(["username" => $bind['mng_admin_name']]);
|
|
|
$this->success('查询用户信息成功2',$return_data);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
$this->success("登录成功!", $return_data);
|
|
|
} else {
|
|
|
$this->error($this->auth->getError());
|
|
|
}
|
|
|
} else {
|
|
|
//小程序openid找到了会员
|
|
|
$update_data = [];
|
|
|
$update_data['logintime'] = time();
|
|
|
if (empty($userres['mobile']) && !empty($openid_info['phoneNumber'])) {
|
|
|
$update_data['mobile'] = $openid_info['phoneNumber'];
|
|
|
}
|
|
|
// if (!empty($openid_info['phoneNumber'])) {
|
|
|
// $update_data['username'] = $openid_info['phoneNumber'];
|
|
|
// }
|
|
|
// if (!empty($openid_info['nickName']) && $openid_info['nickName']!="微信用户") {
|
|
|
// $update_data['nickname'] = $openid_info['nickName'];
|
|
|
// }
|
|
|
|
|
|
|
|
|
if (!empty($openid_info['phoneNumber'])) {
|
|
|
//这里是已注册过的用户,有手机号,需要判断下手机号有没有在区域管理员中存在,若存在就将id绑定过去
|
|
|
$bind = Db::name("hc_area_code_mng")->where("mng_admin_phone", $openid_info['phoneNumber'])->order("id desc")->find();
|
|
|
if (!empty($bind)) {
|
|
|
Db::name("hc_area_code_mng")->where("mng_admin_phone", $openid_info['phoneNumber'])->update(["mng_user_id" => $userres['id']]);
|
|
|
if ($bind['mng_admin_name']) {
|
|
|
//将管理员姓名更新到用户表中
|
|
|
Db::name("user")->where("id", $userres['id'])->update(["username" => $bind['mng_admin_name']]);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
Db::name("user")->where("wx_xcx_openid", $openid)->update($update_data);
|
|
|
$this->auth->direct($userres['id']);
|
|
|
$return_data = $this->auth->getUserinfo();
|
|
|
|
|
|
$is_mng_user = 0;//是否是部门管理员 0不是 1是
|
|
|
$mng_area = [];//管辖部门(多个)
|
|
|
$area = Db::name("hc_area_code_mng")->alias("a")
|
|
|
->join("hc_area_code b", "b.code=a.mng_code")
|
|
|
->where("a.mng_user_id", $return_data['id'])->field("b.code,b.name")->select();
|
|
|
if (!empty($area)) {
|
|
|
$is_mng_user = 1;
|
|
|
$mng_area = $area;
|
|
|
}
|
|
|
$return_data['is_mng_user'] = $is_mng_user;
|
|
|
$return_data['mng_area'] = $mng_area;
|
|
|
|
|
|
$this->success(__('登录成功'), $return_data);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 给后台添加用户时自动注册用
|
|
|
*/
|
|
|
public function auto_regist_user($username, $password = "a123456", $email = "", $mobile, $extend_data)
|
|
|
{
|
|
|
$ret = $this->auth->register($username, $password, '', $mobile, $extend_data);
|
|
|
if ($ret) {
|
|
|
$return_data = $this->auth->getUserinfo();
|
|
|
return array_callback(true, "用户添加成功", $return_data['id']);
|
|
|
} else {
|
|
|
return array_callback(false, $this->auth->getError());
|
|
|
}
|
|
|
}
|
|
|
public function get_user_by_phone()
|
|
|
public function getuserphonenumber()
|
|
|
{
|
|
|
$user_id=$this->auth->id;
|
|
|
$appid = $this->AppID;
|
|
|
$AppSecret = $this->AppSecret;
|
|
|
$post = $this->request->post();
|
|
|
$code = $post['code'];
|
|
|
$code = $post['code'];// I('post.code');
|
|
|
$AccessToken=$this->getwxAccessToken($appid,$AppSecret);
|
|
|
$phone=$this->getwxPhoneNumber($code,$AccessToken);
|
|
|
$phone=json_decode($phone,true);
|
|
|
$moblie=$phone['phoneNumber'];
|
|
|
if($moblie){
|
|
|
$res=Db::name("user")->where("id", $user_id)->update(["mobile" => $moblie]);
|
|
|
$this->auth->direct($user_id);
|
|
|
$return_data = $this->auth->getUserinfo();
|
|
|
|
|
|
if($res){
|
|
|
$this->success("登录成功!",$return_data);
|
|
|
|
|
|
}else{
|
|
|
$this->error("无法获取手机号,登录失败");
|
|
|
|
|
|
}
|
|
|
Db::name("user")->where("id", $user_id)->update(["mobile" => $moblie]);
|
|
|
$this->bind($user_id,$moblie);
|
|
|
}else{
|
|
|
$this->error("无法获取手机号,登录失败");
|
|
|
}
|
...
|
...
|
@@ -270,4 +132,64 @@ class Login extends Base |
|
|
curl_close($curl);
|
|
|
return $return_str;
|
|
|
}
|
|
|
|
|
|
//查询权限
|
|
|
public function bind($user_id,$phoneNumber){
|
|
|
//跟新手机号
|
|
|
Db::name("user")->where("id", $user_id)->update(["mobile" => $phoneNumber]);
|
|
|
|
|
|
//[农场]这里是已注册过的用户,有手机号,需要判断下手机号有没有在食堂中存在,若存在就将id绑定过去
|
|
|
$bind = Db::name("farm_manager")->where("mng_admin_phone", $phoneNumber)->order("id desc")->find();
|
|
|
|
|
|
if (!empty($bind)) {
|
|
|
Db::name("farm_manager")->where("mng_admin_phone", $phoneNumber)->update(["mng_user_id" =>$user_id]);
|
|
|
if ($bind['mng_admin_name']) {
|
|
|
//将管理员姓名更新到用户表中
|
|
|
Db::name("user")->where("id", $user_id)->update(["username" => $bind['mng_admin_name']]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//[食堂]这里是已注册过的用户,有手机号,需要判断下手机号有没有在食堂管理员中存在,若存在就将id绑定过去
|
|
|
$bind2 = Db::name("farm_canteenmanager")->where("mng_admin_phone", $phoneNumber)->order("id desc")->find();
|
|
|
|
|
|
if (!empty($bind2)) {
|
|
|
Db::name("farm_canteenmanager")->where("mng_admin_phone", $phoneNumber)->update(["mng_user_id" => $user_id]);
|
|
|
if ($bind2['mng_admin_name']) {
|
|
|
//将管理员姓名更新到用户表中
|
|
|
Db::name("user")->where("id", $user_id)->update(["username" => $bind2['mng_admin_name']]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$is_mng_user = 0;//是否是农场管理员 0不是 1是
|
|
|
$mng_area = [];//管辖区域(多个)
|
|
|
$area = Db::name("farm_manager")->alias("a")
|
|
|
->join("farm_farm_list b", "b.id=a.farm_farm_list_id")
|
|
|
->where("a.mng_user_id", $user_id)->field("b.id as code,b.name")->select();
|
|
|
if (!empty($area)) {
|
|
|
$is_mng_user = 1;
|
|
|
$mng_area = $area;
|
|
|
}
|
|
|
$return_data['is_mng_user'] = $is_mng_user;
|
|
|
$return_data['mng_area'] = $mng_area;
|
|
|
|
|
|
$is_mng_user2 = 0;//是否是食堂管理员 0不是 1是
|
|
|
$mng_area2 = [];//管辖区域(多个)
|
|
|
$area2 = Db::name("farm_canteenmanager")->alias("a")
|
|
|
->join("farm_canteen b", "b.id=a.farm_canteen_id")
|
|
|
->where("a.mng_user_id", $user_id)->field("b.id as code,b.name")->select();
|
|
|
if (!empty($area2)) {
|
|
|
$is_mng_user2 = 1;
|
|
|
$mng_area2 = $area2;
|
|
|
}
|
|
|
$return_data['is_mng_user2'] = $is_mng_user2;
|
|
|
$return_data['mng_area2'] = $mng_area2;
|
|
|
|
|
|
$return_data['avatar'] =full_image($return_data['avatar']);
|
|
|
$res=$this->auth->direct($user_id);
|
|
|
$return_data = $this->auth->getUserinfo();
|
|
|
$this->success(__('登录成功'), $return_data);
|
|
|
}
|
|
|
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|