作者 郭文星

123

... ... @@ -24,68 +24,89 @@ class Login extends Base
{
$appid = $this->AppID;
$AppSecret = $this->AppSecret;
$code = $this->request->param("code");
// $encryptedData = $this->request->param("encryptedData");
// $iv = $this->request->param("iv");
// $type = $this->request->param("type");
//
// if ($type == 1) {
// $encryptedData = urldecode($encryptedData);
// $iv = urldecode($iv);
$post = $this->request->post();
$code = $post['code'];// I('post.code');
$encryptedData = $post['encryptedData'];//I('post.encryptedData');
$iv = $post['iv'];//I('post.iv');
if ($post['type'] == 1) {
$encryptedData = urldecode($encryptedData);
$iv = urldecode($iv);
// file_put_contents("ccc.txt", "授权A:" . date("Y-m-d H:i:s") . ":" . $encryptedData . PHP_EOL, FILE_APPEND);
//// file_put_contents("ccc.txt", "授权B:" . date("Y-m-d H:i:s") . ":" . $iv . PHP_EOL, FILE_APPEND);
//
// }
// file_put_contents("ccc.txt", "授权B:" . date("Y-m-d H:i:s") . ":" . $iv . 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);
print_r($res);return;
if (!$res) {
$res = json_decode(http_request($url), true);
}
if(!empty($res['errcode'])){
$ajax['code'] = 0;
$ajax['info'] = $res['errcode'];
$errCode = $res['errcode'] == "-41001" || $res['errcode'] == "-41003" ? "授权失败,请尝试重新授权" : $res['errcode'];
$this->error($errCode);
}
$sessionKey = $res['session_key'];
$openid = $res['openid'];//获取用户openid
//$unionid = $res['unionid'];//获取用户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);
//$errCode = $pc->decryptData($encryptedData, $iv, $data);
//$data = json_decode($data, true);
$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",
];
//print_r($openid_info);return;
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)) {
if (empty($userres)) {
//如果通过小程序openid找不到会员
//注册处理
$extend_data = [
//"avatar" => $openid_info["avatarUrl"],
"nickname" => $openid_info['nickName'],
"avatar" => $openid_info["avatarUrl"],
"wx_xcx_openid" => $openid,
"unionid" => $unionid,
];
$username = $openid ? $openid : suiji_num("TY");
$ret = $this->auth->register($username, "a123456", '', "1345233948", $extend_data);
$ret = $this->auth->register($username, "a123456", '', $data['phoneNumber'], $extend_data);
if ($ret) {
$return_data = $this->auth->getUserinfo();
... ... @@ -126,6 +147,13 @@ class Login extends Base
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绑定过去
... ... @@ -138,9 +166,23 @@ class Login extends Base
}
}
}
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);
}
}
... ...