流程:
1.用户关注公众号 -> 2.公众号给出绑定链接 -> 3.用户点链接进入项目登录页面 -> 4.绑定完成
wechat_user表里至少存2个属性, 1.uid(项目的用户id) 2.openid(微信用户关注公众号时生成的对公众号唯一的id)
通过openid可以准确的向单个用户发送消息.
逻辑图如下(3步到4步):
isdel属性用来判断状态,0表示已绑定,1表示未绑定或者解绑了(最后附上我的数据表)
ps:这只是我的个人思路,有不足或错误的地方,望赐教.
==============================================8-30更新
根据上述,我写了一下php页面
从第二步开始,点链接
//链接代码
"<a href='http://xx.xxxx.top/Weixin/WeChat_login.php?cz=bd&fromUsername=$fromUsername'>
绑定链接
</a>";
//cz=操作,bd=绑定
第三步,点链接进入的页面(html界面简陋,只是用来测试)
接收两个参数1.$fromUsername 2.$cz
<?php
/**
* 微信用户登录验证
* User: rtyu
* Date: 2018/8/27
* Time: 11:49
*/
header("Content-type: text/html; charset=utf-8");
$fromUsername = $_GET['fromUsername'];//获取用户的openid
$cz = $_GET['cz'];//执行的操作 bd绑定,jb解绑
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<div style="margin: auto;width: 40%;margin-top: 20%">
<form action="http://lin.admincms.top/Weixin/WeChat_binding.php" method="post">
<input name="cz" id="cz" type="text" value="<?=$cz; ?>" style="display: none"/>
<input name="openid" id="openid" type="text" value="<?=$fromUsername; ?>"style="display: none"/>
<p>用户名 :
<input type="text" name="logusername" id="logusername"/>
</p>
<p>密 码 :
<input type="password" name="loguserpass" id="loguserpass"/>
</p>
<p style="margin: auto;width: 100%">
<input type="submit" value="登录" style="width: 100%;height: 50px"/>
</p>
</form>
</div>
</body>
</html>
登录后跳转页面,一堆逻辑判断 ,判断的逻辑图就是上面的图
<?php
/**
* 微信用户绑定
* User: rtyu
* Date: 2018/8/27
* Time: 11:49
*/
include "WeChat_util.php";
$wx = new WeChat_util();//微信帮助类对象
header("Content-type: text/html;charset=utf-8");
$openid = $_POST['openid'];//获取用户的openid
$cz = $_POST['cz'];//执行的操作 bd绑定,jb解绑
$logusername = $_POST['logusername'];//登录用户名
$loguserpass = $_POST['loguserpass'];//登录密码
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "数据库名";
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
//1.验证是否能登录
$sql="SELECT id,loginname FROM login_users WHERE loginname='".$logusername."' AND userpass='".base64_encode($loguserpass)."'";
$result = $conn->query($sql);//执行
if ($result->num_rows > 0) { //表示能登录 isdel属性 0表示绑定,1表示解绑
$row = $result->fetch_assoc();
$uid = $row["id"];//拿到id
$loginname = $row["loginname"];
switch ($cz){
case "bd"://绑定
$sql = "SELECT COUNT(1) row FROM `wechat_user` WHERE uid = '". $uid."' AND isdel=1";
$result = $conn->query($sql);//执行
$row = $result->fetch_assoc();
if ($row["row"] > 0) { //表示已经存在
//已有记录(解绑后再次绑定)
//进行解绑操作 isdel的1表示已删除
$sql = "UPDATE wechat_user SET isdel = 0 WHERE openid = '". $openid."' AND uid = '". $uid."'";
if ($conn->query($sql) === TRUE) {
echo "绑定成功";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
exit();
}else{
//还没有记录(全新)
//判断wechat_user里有没有该uid的信息
$sql = "SELECT COUNT(1) row FROM `wechat_user` WHERE uid = '". $uid."' AND isdel=0";
$result = $conn->query($sql);//执行
$row = $result->fetch_assoc();
if ($row["row"] > 0) { //表示已经存在
echo "该账户已经绑定";
exit();
}else{
//根据openid获取微信用户信息
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$wx->getAccessToken()."&openid=".$openid."&lang=zh_CN";
$out = json_decode($wx->https_curl_json($url));//获取到用户的信息(json格式)
//往wechat_user表里增加一条数据
$nickname = $out->nickname;//昵称
$avatar_src = $out->headimgurl;//头像地址
$sex = $out->sex;//用户的性别
$city = $out->city;//城市
$subscribe_time = $out->subscribe_time;//用户关注时间
$subscribe_time=date("Y-m-d H:i",$subscribe_time);//时间戳转换成时间
//绑定操作(插入一条数据
$sql = "INSERT INTO `wechat_user` (`openid`,`uid`,`nickname`,`avatar_src`,`sex`,`city`,`update_time`,`subscribe_time`,`isdel`)
VALUES('".$openid."','".$uid."','".$nickname."','".$avatar_src."','".$sex."','".$city."',NOW(),'".$subscribe_time."','0') ;";
$utf8 = "set names utf8";
$conn->query($utf8);
if ($conn->query($sql) === TRUE) {
echo "绑定成功";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
exit();
}
}
break;
case "jb"://解绑
//判断wechat_user里有没有该uid的信息
$sql = "SELECT COUNT(1) row FROM `wechat_user` WHERE uid = '". $uid."'AND openid = '". $openid."' AND isdel=0";
$result = $conn->query($sql);//执行
$row = $result->fetch_assoc();
if ($row["row"] == 0) {
echo "该账户未绑定";
exit();
}else{
//进行解绑操作 isdel的1表示已删除
$sql = "UPDATE wechat_user SET isdel = 1 WHERE openid = '". $openid."' AND uid = '". $uid."'";
if ($conn->query($sql) === TRUE) {
echo "解绑成功";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
exit();
}
break;
default:
echo "出现错误";
break;
}
}else{
echo "登录失败";
exit();
}
//关闭连接
$conn->close();
?>
WeChat_util类在我之前的文章里有,链接:https://blog.csdn.net/HOLYLANCES/article/details/82018065
我的数据表
/*
SQLyog Professional v12.09 (64 bit)
MySQL - 5.7.15-log : Database - pms
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=''*/;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`pms` /*!40100 DEFAULT CHARACTER SET gbk */;
USE `你自己的数据表名`;
/*Table structure for table `wechat_user` */
DROP TABLE IF EXISTS `wechat_user`;
CREATE TABLE `wechat_user` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`openid` CHAR(32) CHARACTER SET utf8 DEFAULT NULL COMMENT '微信用户唯一编号',
`uid` INT(11) DEFAULT NULL COMMENT '对应用户表的编号(外键)',
`nickname` VARCHAR(50) CHARACTER SET utf8 DEFAULT NULL COMMENT '昵称',
`avatar_src` VARCHAR(150) CHARACTER SET utf8 DEFAULT NULL COMMENT '头像地址',
`sex` INT(11) DEFAULT NULL COMMENT '性别',
`city` VARCHAR(32) CHARACTER SET utf8 DEFAULT NULL COMMENT '城市',
`update_time` DATETIME DEFAULT NULL COMMENT '更新时间',
`subscribe_time` DATETIME DEFAULT NULL COMMENT '订阅时间',
`isdel` INT(2) NOT NULL COMMENT '删除标识 0表示绑定 1表示已解绑或未绑',
PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;