微信小程序登录
登录后台操作
public function create()
{
$code = input('code');//前台传过来的
$AppID = 'wxa1f79edf42175e02';//微信公众平台获取
$AppSecret = '5e7b75c47168b51084e4b9f63ec78495';
$url = 'https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code';
$url = sprintf($url,$AppID,$AppSecret,$code);//sprinty()把百分号(%)符号替换成一个作为参数进行传递的变量:
$data=curlGet($url);
$user=UserModel::where('openid',$data['openid'])->find();
if (empty($user)){
$user = UserModel::create([
'openid'=>$data['openid']
]);
}
$token=(new TokenServer())->generateToken($user->id);
return json(['token'=>$token,'error_code'=>0,'msg'=>'登录成功']);
}
public function phone()
{
$tel = input();
if (!$tel)
{
return UserModel::create(['error_code'=>500,'msg'=>'手机号不能为空!'],'json');
}
$statusStr = array(
"0" => "短信发送成功",
"-1" => "参数不全",
"-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!",
"30" => "密码错误",
"40" => "账号不存在",
"41" => "余额不足",
"42" => "帐户已过期",
"43" => "IP地址限制",
"50" => "内容含有敏感词"
);
$code = rand(1111,9999);
$smsapi = "http://api.smsbao.com/";
$user = "1048551941"; //短信平台帐号
$pass = md5("1048551941"); //短信平台密码
$content="梦开始的地方:您的短信验证码为$code,请不要告诉其他人";//要发送的短信内容
$phone = $tel['phone'];//要发送短信的手机号码
$sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
$result =file_get_contents($sendurl) ;
if ($result == 0) {
Cache::set($phone,$code);
return json(['error_code'=>0,'data'=>$code,'msg'=>'发送验证码成功']);
}else{
return json(['error_code'=>999,'data'=>$result,'msg'=>'网络异常 稍后重试']);
}
}
public function login()
{
$data = input();
$code = session('datas');
if ($data['code'] != $code){
$res = UserModel::where('phone',$data['phone'])->find()->toArray();
if ($res){
$token=(new TokenServer())->generateToken($res['id']);
return json(['token'=>$token,'msg'=>'登录成功','code'=>0,]);
}else{
return json(['data'=>$res,'msg'=>'没有','code'=>1]);
}
}else{
return json(['data'=>'','msg'=>'验证码错误','code'=>2]);
}
}
登录前台操作
登录页面
登录wxml页面
<view class='content'>
<form bindsubmit="formSubmit">
<view class='phone-box'>
<text class='phone'>手机号</text>
<input name="phone" type='number' placeholder="请输入手机号" maxlength='11' name="phone" class='number' bindinput='lovePhone' />
</view>
<view class='phone-box'>
<text class='phone'>验证码</text>
<input name="phoneCode" placeholder="请输入验证码" class='number' placeholder-style='color:#bbb' bindinput="yanLoveInput" />
<view bindtap='yanLoveBtn' class='getNum'>{{getText2}}</view>
</view>
<button formType="submit" class='submit'>登录</button>
</form>
</view>
wxss页面
.content {
width: 100%;
height: auto;
padding: 0 50rpx;
box-sizing: border-box;
}
.phone-box {
width: 100%;
height: 89rpx;
border-bottom: 1rpx solid #efefef;
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: center;
}
.phone {
color: #333;
margin-right: 60rpx;
font-size: 28rpx;
}
.number {
color: #333;
font-size: 28rpx;
width: 200rpx;
}
.getNum {
width:210rpx;
height:48rpx;
background:rgba(248, 112, 57, 1);
border-radius:8rpx;
font-size:28rpx;
font-family:PingFang-SC-Medium;
color:rgba(255, 255, 255, 1);
line-height:48rpx;
margin-right:36rpx;
text-align:center;
}
.submit {
width: 480rpx;
height: 80rpx;
background: rgba(248, 112, 57, 1);
border-radius: 8rpx;
margin-top: 80rpx;
color: #fff;
font-size: 32rpx;
}
js页面
Page({
/**
* 页面的初始数据
*/
data: {
//验证手机号
loginPhone:false,
loginPwd:false,
loveChange: true,
hongyzphone: '',
// 验证码是否正确
zhengLove: true,
huoLove: '',
getText2: '获取验证码',
},
// 手机验证
lovePhone: function (e) {
let phone = e.detail.value;
this.setData({ hongyzphone: phone })
if (!(/^1[34578]\d{9}$/.test(phone))) {
this.setData({
lovePhone: false
})
console.log(phone.length)
if (phone.length >= 11) {
wx.showToast({
title: '手机号有误',
icon: 'none',
duration: 1000
})
}
} else {
this.setData({
lovePhone: true
})
}
},
// 验证码输入
yanLoveInput: function (e) {
let that = this;
let yanLove = e.detail.value;
let huoLove = this.data.huoLove;
that.setData({
yanLove: yanLove,
zhengLove: false,
})
if (yanLove.length >= 4) {
if (yanLove == huoLove) {
that.setData({
zhengLove: true,
})
} else {
that.setData({
zhengLove: false,
})
wx.showModal({
content: '输入验证码有误',
showCancel: false,
success: function (res) { }
})
}
}
},
// 验证码按钮
yanLoveBtn: function () {
let loveChange = this.data.loveChange;
console.log(loveChange)
let lovePhone = this.data.lovePhone;
console.log(lovePhone)
let phone = this.data.hongyzphone;
console.log(phone)
let n = 59;
let that = this;
if (!lovePhone) {
wx.showToast({
title: '手机号有误',
icon: 'success',
duration: 1000
})
} else {
if (loveChange) {
this.setData({
loveChange: false
})
let lovetime = setInterval(function () {
let str = '(' + n + ')' + '重新获取'
that.setData({
getText2: str
})
if (n <= 0) {
that.setData({
loveChange: true,
getText2: '重新获取'
})
clearInterval(lovetime);
}
n--;
}, 1000);
console.log(phone)
wx.request({
url: 'http://www.easyadmin2.com/api/phone',
data:{phone:phone},
success(res){
console.log(res)
}
})
}
}
},
//form表单提交
formSubmit(e){
let val = e.detail.value
// console.log('val', val)
var phone = val.phone //电话
var phoneCode = val.phoneCode //验证码
var openid = wx.getStorageSync('openid')
console.log(openid)
wx.request({
url: 'http://www.easyadmin2.com/api/login',
data:{phone:phone,code:phoneCode,openid:openid},
success(res){
console.log(res)
if(res){
wx.navigateTo({
url: '/pages/index2/index2',
})
}
}
})
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
wx.login({
success (res) {
console.log(res.code)
if (res.code) {
wx.request({
url: 'http://www.easyadmin2.com/api/create',
data:{code:res.code},
header:{
'token': wx.getStorageSync('token')
},
success(res){
wx.setStorageSync('token', res.data.token)
}
})
} else {
console.log('登录失败!' + res.errMsg)
}
}
})
},
})
列表操作
列表后台
/**
*注释: 列表展示分页加载
*用户名:过客
*日期:2021/9/28
*时间:10:21
* @param Request $request
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function singList(Request $request)
{
$params=$request->get();
$offset = ($params['page']-1)*$params['limit']=3;
$result=SingModel::limit($offset,$params['limit'])->order('watch_number','desc')->select();
$count=SingModel::count();
$pageCount =($count+$params['page']-1)/$params['limit'];
if ($result){
return json(['data'=>$result,'pageCount'=>$pageCount,'msg'=>'查询成功','code'=>200]);
}else{
return json(['data'=>'','msg'=>'查询失败','code'=>400]);
}
}
/**
*注释:列表详情页
*用户名:过客
*日期:2021/9/28
*时间:10:21
* @param Request $request
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function singInfo(Request $request)
{
$id=$request->get('id');
$info=SingModel::where('id',$id)->find();
if ($info){
return json(['code'=>200,'msg'=>'查询成功','data'=>$info]);
}else{
return json(['code'=>400,'msg'=>'查询失败','data'=>'']);
}
}
/**
*注释: 搜索数据
*用户名:过客
*日期:2021/9/28
*时间:14:32
* @param Request $request
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function singSelect(Request $request)
{
$startime = $request['start_time'];
$stoptime = $request['end_time'];
$where[] = ['start_time','between',"$startime,$stoptime"];
$data=SingModel::where($where)->select()->toArray();
return json(['code' => 200, 'data' => $data, 'msg' => 'ok']);
}
列表前台微信小程序
wxml页面
<view class="section">
<picker mode="date" value="{{start_time}}" start="2019-09-01" end="2022-09-01" bindchange="startTimeChange">
<view class="picker">
开始日期: {{start_time}}
</view>
</picker>
</view>
<view class="section">
<picker mode="date" value="{{end_time}}" start="2019-09-01" end="2022-09-01" bindchange="stopTimeChange">
<view class="picker">
结束日期: {{end_time}}
</view>
</picker>
</view>
<button bindtap="show">搜索</button>
<view class="box" wx:for="{{list}}" wx:key="key">
<navigator url="/pages/index3/index3?id={{item.id}}">
<image src="{{item.image}}" class="img"></image>
<view class="div">
<view class="title">{{item.name}}</view>
<view class="desc">{{item.desc}}</view>
<view class="address">{{item.address}}</view>
<view class="time">{{item.start_time}}-{{item.end_time}}</view>
<view class="number">观看人数{{item.watch_number}}</view>
</view>
</navigator>
</view>
wxss页面
.box{
float: left;
width: 90%;
height: 375rpx;
margin-top: 20rpx;
margin-left: 5%;
}
.img{
float: left;
width: 40%;
height: 310rpx;
margin-left: 20rpx;
margin-top: 30rpx;
border-radius: 10rpx;
}
.div{
float: left;
width: 350rpx;
height: 400rpx;
margin-left: 30rpx;
}
.title{
font-size: 14px;
margin-top: 40rpx;
}
.desc{
float: left;
font-size: 13px;
margin-top: 10rpx;
color: #CCCCCC;
}
.address{
float: left;
font-size: 13px;
margin-top: 10rpx;
color: #CCCCCC;
}
.time{
float: left;
font-size: 13px;
margin-top: 10rpx;
color: #CCCCCC;
}
.number{
float: left;
font-size: 14px;
font-weight: bold;
}
js页面
Page({
/**
* 页面的初始数据
*/
data: {
list:'',
page:1,
limit:5,
start_time:'',
end_time:''
},
startTimeChange(e){
console.log(e);
this.setData({
start_time:e.detail.value
})
},
stopTimeChange(e){
console.log(e);
this.setData({
end_time:e.detail.value
})
},
//搜索
show(e){
console.log(e);
wx.request({
url: 'http://www.easyadmin2.com/api/singSelect',
data:{
start_time:this.data.start_time,
end_time:this.data.end_time
},
success:res=>{
this.setData({
list:res.data.data
})
}
})
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
wx.request({
url: 'http://www.easyadmin2.com/api/list',
data:{page:this.data.page},
success:res=>{
this.setData({
list:res.data.data,
page:this.data.page+1
})
}
})
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
wx.request({
url: 'http://www.easyadmin2.com/api/list',
data:{page:this.data.page},
success:res=>{
if(res.data.pageCount>=this.data.page)
this.setData({
list:this.data.list.concat(res.data.data),
page:this.data.page+1
})
}
})
},
})
列表详情页面
wxml页面
<view class="box">
<image src="{{info.image}}" class="img"></image>
<view class="div">
<view class="title">{{info.name}}</view>
<view class="time">{{info.start_time}}-{{info.end_time}}</view>
<view class="money">¥{{info.price}}</view>
</view>
</view>
<view class="area">
<view class="address">{{info.address}}</view>
</view>
<view class="show">演出简介</view>
<text class="desc">{{info.desc}}</text>
<button type="primary" open-type="contact" class="shareBox">在线咨询</button>
<button type="primary" class="btn" bindtap="buyTicket">购买</button>
wxss页面
.box{
float: left;
width: 90%;
height: 350rpx;
margin-top: 30rpx;
margin-left: 5%;
}
.img{
float: left;
width: 40%;
height: 322rpx;
margin-left: 20rpx;
margin-top: 15rpx;
border-radius: 10rpx;
}
.div{
float: left;
width: 350rpx;
height: 330rpx;
margin-left: 30rpx;
}
.title{
float: left;
font-size: 18px;
margin-top: 20rpx;
}
.time{
float: left;
margin-top: 50rpx;
color: #cccccc;
font-size: 13px;
}
.money{
float: left;
margin-top: 60rpx;
font-size: 17px;
font-weight: bold;
}
.area{
float: left;
width: 90%;
height: 120rpx;
margin-top: 30rpx;
margin-left: 5%;
}
.address{
float: left;
font-size: 20px;
margin-top: 30rpx;
margin-left: 20rpx;
}
.show{
float: left;
width: 170rpx;
height: 60rpx;
font-size: 20px;
margin-top: 100rpx;
margin-left: 35rpx;
}
.desc{
float: left;
width: 90%;
height: 300rpx;
margin-top: 30rpx;
margin-left: 5%;
}
.btn{
float: right;
}
.shareBox{
float: left;
}
js页面
Page({
/**
* 页面的初始数据
*/
data: {
info:''
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
let id=options.id
wx.request({
url: 'http://www.easyadmin2.com/api/getInfo',
data:{id:id},
success:res=>{
this.setData({
info:res.data.data
})
}
})
},
buyTicket(){
let token=wx.getStorageSync('token')
let price=this.data.info.price;
let name=this.data.info.name
if(!token){
wx.showToast({
title: '未登录',
icon:"error"
})
wx.navigateTo({
url: '/pages/phone/phone',
})
}else{
wx.showToast({
title: '已登录',
icon:'success'
})
wx.navigateTo({
url: '/pages/index4/index4?price='+price+'&name='+name,
})
}
},
})
订单操作
订单后台操作
/**
*注释: 生成订单数据
*用户名:过客
*日期:2021/9/28
*时间:15:41
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function orderCreate()
{
$id=input('id');
//开启事物
\think\facade\Db::startTrans();
try {
//查询座位
$data=SeatModel::where('status','=',1)->select()->toArray();
//随机生成一个座位
$res=array_rand($data,1);
//座位id
$arr['seat_id']=$res;
$arr['price']=input('price');
//演唱会名称
$arr['name']=input('name');
//用户id
$arr['uid']=request()->uid;
//生成订单号
$arr['order_id']=time();
$arr['status']=2;
//提交事物
\think\facade\Db::commit();
$res=OrderModel::create($arr);
$data=OrderModel::select();
if ($data){
return json(['code'=>200,'msg'=>'订单生成成功','data'=>$data]);
}
}catch (ErrorException $e){
//回滚
\think\facade\Db::rollback();
print_r($e->getMessage());
return json(['code'=>400,'msg'=>'订单生成失败','data'=>'']);
}
}
/**
*注释: 订单支付
*用户名:过客
*日期:2021/9/28
*时间:16:53
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function orderPay()
{
$phone=input('phone');
//获取订单编号
$order_id=input('order_id');
//开启事物
\think\facade\Db::startTrans();
try {
//接收参数
$price=input('price');
//查询这个用户的金额
$res=UserModel::where('phone','13782121370')->find()->toArray();
//取出金额
$money=$res['money'];
//提交事物
\think\facade\Db::commit();
//减钱
$res=UserModel::where('phone','13782121370')->update(['money'=>$money-$price]);
if ($res){
//接收参数
$tel=input('phone');
//实例化redis
$redis=new Redis();
$result=$redis->lpush('phone',$tel);
$data=$redis->lrange('phone',1,1);
$statusStr = array(
"0" => "短信发送成功",
"-1" => "参数不全",
"-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!",
"30" => "密码错误",
"40" => "账号不存在",
"41" => "余额不足",
"42" => "帐户已过期",
"43" => "IP地址限制",
"50" => "内容含有敏感词"
);
$code = rand(1111,9999);
$smsapi = "http://api.smsbao.com/";
$user = "1048551941"; //短信平台帐号
$pass = md5("1048551941"); //短信平台密码
$content="梦开始的地方:您的短信验证码为$code,请不要告诉其他人";//要发送的短信内容
$phone = "13782121370";//要发送短信的手机号码
$sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
$result =file_get_contents($sendurl) ;
$redis->rpop('phone');
$result =OrderModel::where('order_id',$order_id)->update(['status'=>1]);
return json(['code'=>200,'msg'=>'发送验证码成功','data'=>$code]);
}else{
return json(['code'=>400,'msg'=>'余额不足','data'=>null]);
}
}catch (ErrorException $e){
//事物回滚
\think\facade\Db::rollback();
print_r($e->getMessage());
return json(['code'=>401,'msg'=>'支付失败','data'=>null]);
}
}
/**
*注释: 查询已支付的订单
*用户名:过客
*日期:2021/9/28
*时间:17:49
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function orderSelect()
{
$arr=OrderModel::where('status',1)->select();
if ($arr){
return json(['code'=>200,'data'=>$arr,'msg'=>'查询成功']);
}
}
/**
*注释:退款订单
*用户名:过客
*日期:2021/9/28
*时间:18:42
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function orderPayEnd()
{
//开启事物
\think\facade\Db::startTrans();
try {
$order_id=input('order_id');
$price=input('price');
$res=OrderModel::where('order_id',$order_id)->update(['status'=>2]);
$data=UserModel::where('phone','13782121370')->find()->toArray();
$money=$data['money'];
$data=UserModel::where('phone','13782121370')->update(['money'=>$money+$price]);
//提交事物
\think\facade\Db::commit();
return json(['code'=>200,'msg'=>'退票成功','data'=>$data]);
}catch (ErrorException $e){
//事物回滚
\think\facade\Db::rollback();
print_r($e->getMessage());
return json(['code'=>400,'msg'=>'退票失败','data'=>null]);
}
}
/**
*注释:退款订单查询
*用户名:过客
*日期:2021/9/28
*时间:18:42
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function orderSelectEnd()
{
$data=OrderModel::where('status',2)->select();
if ($data){
return json(['code'=>200,'msg'=>'查询成功','data'=>$data]);
}else{
return json(['code'=>400,'msg'=>'查询失败','data'=>null]);
}
}
订单前台操作
wxml页面
<l-card wx:for="{{order}}" type="primary"
plaintext="{{true}}" wx:key="item">
<view>
订单号 {{item.order_id}}
</view>
<view>
用户 {{item.uid}}
</view>
<view>
座位 {{item.seat_id}}
</view>
<view>
演唱会名称 {{item.name}}
</view>
<view>
价格 {{item.price}}
</view>
<view wx:if="{{item.status==2}}">
<l-button type="default" bindtap="pay">立即支付</l-button>
</view>
<view wx:else>
<l-button type="default" bindtap="end">退款</l-button>
</view>
</l-card>
js页面
// pages/index4/index4.js
Page({
/**
* 页面的初始数据
*/
data: {
order:''
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
//生成订单数据
let price=options.price;
let name=options.name
wx.request({
url: 'http://www.easyadmin2.com/api/orderCreate',
data:{
price:price,
name:name
},
header:{
'token':wx.getStorageSync('token')
},
method:"POST",
success:res=>{
this.setData({
order:res.data.data
})
}
})
},
//退票
end:function()
{
let price=this.data.order[0].price;
let order_id=this.data.order[0].order_id;
wx.request({
url: 'http://www.easyadmin2.com/api/orderPayEnd',
method:"POST",
data:{
price:price,
order_id:order_id,
},
header:{
'token':wx.getStorageSync('token')
},
success:res=>{
if(res.code==200){
wx.showToast({
title: '退款成功',
})
}
}
})
},
//购买票
pay()
{
let price=this.data.order[0].price;
let order_id=this.data.order[0].order_id;
wx.request({
url: 'http://www.easyadmin2.com/api/orderPay',
method:"POST",
data:{
price:price,
order_id:order_id,
'phone':'13782121370'
},
header:{
'token':wx.getStorageSync('token')
},
success:res=>{
if(res.code==200){
wx.showToast({
title: '支付成功',
})
}
}
})
},
})
创建后台数据表