005--笔记:微信小程序实现购物车功能(下)--后端代码
前端小白一只,选了PHP做后端,代码能跑的通,,但是很多细节处理还不到位,可根据具体需要再做处理吧。。。
practice/api/xxx.php文件:
注: ---在practice文件夹中配置环境
////////////////////////////////////////////////////////////////////////////////////////////////////////////
文件目录:
inc.setToken.php //生成token方法代码
dbconn.php //链接数据库
login.php //登录
getCar.php //购物车中的商品
updateGoodsNum.php // 更新购物车商品数据库中num的值---加、减
delCarGoods.php //删除购物车中的商品
inc.setToken.php文件 //生成token方法代码
<?php
function setToken(){
// microtime(true): 返回当前 Unix 时间戳的微秒数:
// md5(): 加密函数
// uniqid():以字符串的形式返回唯一标识符。
// sha1(): 加密算法。 函数计算字符串的 SHA-1 散列。
// $str = microtime(true);
// $str = md5(microtime(true));
// $str = uniqid(1);
// $str = sha1('hello');
$str = uniqid(md5(microtime(true)),true);
$str = sha1($str);
return $str;
}
?>
dbconn.php文件 //链接数据库
<?php
// 链接数据库
header("content-type:text/html;charset=utf8");
date_default_timezone_set('PRC');
$serverName = "localhost";
$userName = "root";
$pwd = "root";
$dbName = "smallroutine";
//如果连接上数据库后,改变了数据库的结构,会造成下次连接的时候数据库连接不上,解决方法就是重启服务器
$conn = new mysqli($serverName,$userName,$pwd,$dbName);
//保证查询出来的中文不会出现乱码
mysqli_set_charset($conn, "utf8");
if($conn->connect_error){
die("连接失败:".$conn->connect_error);
}
?>
login.php文件 //登录
<?php
// 登录
include "./dbconn.php";//引入dbconn.php文件,链接数据库
include "./inc.setToken.php";//引入inc.setToken.php文件,为生成token
// 接收微信小程序登录后得到的code,code码五分钟内有效
$code = $_GET["code"];
$appid="这里是你的小程序的appid";
$secret="这里是你的// 小程序秘钥";// 小程序秘钥
//向微信服务器发起请求
$url="https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code";
// file_get_contents()是php服务器向微信服务器发起http请求的AIP
// $res=file_get_contents($url);报错,解决办法:
// windows系统下的PHP,只需要到php.ini中把extension=php_openssl.dll前面的;删掉,重启服务就可以了。 、
//操作步骤:PHPstudy---其他选项菜单---php.ini---搜索extension=php_openssl.dll---将其前面的;删掉
$res=file_get_contents($url);
// echo $res;// 输出查看一下,得到以下三项信息
// expires_in: 7200 //toekn过期时间(我自己的电脑输不出这项,不知为何。公司的电脑就可以输出,所以我将关于token过期时间的代码注释掉了,打开即可使用)
// openid: "o5mf70GYb0r8-txAAA1BO5aoUmp8"
// session_key: "M5XZSBjX+Gz9fr7YG/ysnw=="
$res = json_decode($res,true);
$openid = $res["openid"];
$sessionkey = $res["session_key"];
// $timeout = $res["expires_in"];
// 判断用户是否注册
$sql = "select * from login where openid='{$openid}'";
$result = $conn->query($sql);
//如果用户已经注册,则生成token,并存入session_key,过期时间,token
if ($result->num_rows > 0){
//生成Token
$token = setToken();
//过期时间
// $time_out = strtotime("+{$timeout} seconds");
// 更新token
$sql2="update login set token='{$token}',sessionkey='{$sessionkey}' where openid='{$openid}'";
if ($conn->query($sql2) === TRUE) {
// 登录成功
$data['success'] = 1;
$data["token"] = $token;
$data["msg"] = '登录成功';
}
}else{
// 如果用户没有注册
//生成Token和过期日期
$token = setToken();
//过期
// $time_out = strtotime("+{$timeout} seconds");
// 将用户信息存入数据库,这里我只存了openid,sessionkey,token,可根据自己的需要在数据库中创建字段,自行存入
$sql2="insert into login (openid,sessionkey,token) values ('{$openid}','{$sessionkey}','{$token}')";
if ($conn->query($sql2) === TRUE) {
// 登录成功
$data['success'] = 1;
$data["token"] = $token;
$data["msg"] = '登录成功';
}
}
echo json_encode($data);
?>
getCar.php文件 //购物车中的商品
<?php
// 购物车中的商品
header("Content-type:text/html;charset=utf-8");
$mysqli = new mysqli("localhost","root","root","smallroutine");
if($mysqli->connect_errno != 0){
die();
};
$token = $_GET["token"];
$sql="select * from shop_car where token='{$token}'";
$res = $mysqli->query($sql);
if ($res->num_rows > 0){
//查询到数据
for($i = 0; $i < $res->num_rows;$i++){
$tabData[] = $res->fetch_assoc();
}
$data["list"] = $tabData;
$data["code"] = 1;
}else{
//没有数据
$data["code"] = 0;
}
print_r(json_encode($data));
?>
updateGoodsNum.php文件// 更新购物车商品数据库中num的值---加、减
<?php
// 更新数据库中购物车商品的num的值
header("Content-type:text/html;charset=utf-8");
$mysqli = new mysqli("localhost","root","root","smallroutine");
if($mysqli->connect_errno != 0){
die();
};
$token = $_GET["token"];
$clickGoodsId = $_GET["clickGoodsId"];
$num = $_GET["num"];
$sql = "select * from shop_car where Id='{$clickGoodsId}' and token='{$token}'";
$res = $mysqli->query($sql);
// 如果能查询到数据---说明有这条数据,就更改num的值
if($res->num_rows > 0){
$sql2="update shop_car set num='{$num}' where Id='{$clickGoodsId}'";
if ($mysqli->query($sql2) === TRUE) {
// 数目更改成功
$data["code"] = 1;
}
}else{
$data["code"] = 0;
}
print_r(json_encode($data));
?>
delCarGoods.php文件 //删除购物车中的商品
<?php
// 删除购物车中的商品
header("Content-type:text/html;charset=utf-8");
$mysqli = new mysqli("localhost","root","root","smallroutine");
if($mysqli->connect_errno != 0){
die();
};
$token = $_GET["token"];
$clickGoodsId = $_GET["clickGoodsId"];
$sql = "select * from shop_car where Id='{$clickGoodsId}' and token='{$token}'";
$res = $mysqli->query($sql);
// 如果能查询到数据---说明有这条数据,就删除这条数据
if($res->num_rows > 0){
$sql2="delete from shop_car where Id='{$clickGoodsId}' and token='{$token}'";
if ($mysqli->query($sql2) === TRUE) {
// 数目更改成功
$data["code"] = 1;
}
}else{
$data["code"] = 0;
}
print_r(json_encode($data));
?>
基本就完了,再有完善,再来更新笔记叭