?接口
Route::middleware('auth:api')->prefix('index')->middleware('throttle:1000,1')->group(function (){
Route::get('ShowIn',[\App\Http\Controllers\show\index::class,'index']);
Route::get('ShowSel',[\App\Http\Controllers\show\index::class,'sel']);
Route::get('ShowselShow',[\App\Http\Controllers\show\index::class,'selShow']);
Route::get('InControllerCreate',[\App\Http\Controllers\show\index::class,'InControllerCreate']);
Route::get('ShowInSel',[\App\Http\Controllers\show\Showin::class,'sel']);
Route::get('ShowInDel',[\App\Http\Controllers\show\Showin::class,'del']);
});
小程序获取登录
<view class="container">
<view class="userinfo">
<block wx:if="{{!hasUserInfo}}">
<button wx:if="{{canIUseGetUserProfile}}" bindtap="getUserProfile"> 获取头像昵称 </button>
<button wx:else open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 获取头像昵称 </button>
</block>
<block wx:else>
<image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
<text class="userinfo-nickname">{{userInfo.nickName}}</text>
</block>
</view>
</view>
?小程序登录页面js
var util = require('../../utils/util');
Page({
data: {
userInfo: {},
hasUserInfo: false,
canIUseGetUserProfile: false,
avatarUrl:'',
nickName:''
},
onLoad() {
var thime=util.formatTime(new Date());
console.log(thime);
if (wx.getUserProfile) {
this.setData({
canIUseGetUserProfile: true
})
}
},
getUserProfile(e) {
wx.getUserProfile({
desc: '勇敢妞妞不怕困难',
success:(res)=>{
this.setData({
avatarUrl:res.userInfo.avatarUrl,
nickName:res.userInfo.nickName
})
wx.login({
success: (res) =>{
if (res.code) {
// 发起网络请求
wx.request({
url: 'http://www.ceshi.com/index/ShowIn',
data: {
code: res.code,
avatarUrl: this.data.avatarUrl,
nickName: this.data.nickName
},
success:function(res){
console.log(res.data.openid);
wx.setStorage({
key:"openid",
data:res.data.openid
})
wx.navigateTo({
url: '/pages/home/home',
})
},
})
} else {
console.log('登录失败!' + res.errMsg)
}
}
})
}
})
},
navigateBtn:function()
{
wx.navigateTo({
url: 'pages/home/home',
success: function(res){},
fail: function() {},
complete: function() {}
})
}
})
?小程序 列表 wxml
<!--pages/home/home.wxml-->
<l-card wx:for="{{name}}" type="primary"
full="{{true}}"
image="{{item.u_img}}"
title="{{item.u_name}}">
<view class="content">
<!-- <l-countdown bind:linend time-type="second" time="{{item.create}}" format="{%h}:{%m}:{%s}"/> -->
<l-button type="default" bindtap="bindButtonTap" id="{{item.id}}">默认按钮</l-button>
</view>
</l-card>
小程序 列表 js
// pages/home/home.js
Page({
/**
* 页面的初始数据
*/
data: {
name:'',
urname:'',
res:''
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
var that = this;
wx.getStorage({
key: 'openid',
success (res) {
that.setData({
res:res.data
})
}
})
wx.request({
url: 'http://www.ceshi.com/index/ShowSel',
data:{
openid:that.data.res,
name:that.data.name
},
success:function(date){
console.log(date);
that.setData({
name:date.data.date
})
}
})
},
bindButtonTap: function(e) {
var that = this;
var id =e.currentTarget.id;
var openid = wx.getStorageSync('openid');
console.log(id)
console.log(openid)
wx.request({
url: 'http://www.ceshi.com/index/ShowselShow',
data:{
id:id,
name:openid
},
success:function(res) {
wx.navigateTo({
url: '/pages/show/show?id='+id
})
}
})
}
})
详情页 wxml
<!--pages/show/show.wxml-->
<l-card wx:for="{{name}}" type="primary"
full="{{true}}"
image="{{item.u_img}}"
title="{{item.u_name}}">
<view class="content">
<l-countdown time-type="second" time="{{expire_time}}" format="{%h}:{%m}:{%s}" bind:linend="changeBtn" />
<l-button disabled="{{ btn_disable }}" bind:lintap="buyGoods" bindtap="bindtapChild" id="{{true}}">禁用按钮</l-button>
</view>
</l-card>
详情页 js
// pages/show/show.js
Page({
/**
* 页面的初始数据
*/
data: {
name:'',
goods:{},
expire_time:0,
id:'',
btn_disable:false
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (id) {
var that = this;
that.setData({
id:id.id
});
wx.request({
url: 'http://www.ceshi.com/index/ShowselShow?id='+id.id,
success:function(res) {
var update =res.data.name[0].create;
//当前时间
var now_time = Math.round(new Date().getTime() / 1000).toString();
var number = update - now_time;
that.setData({
name:res.data.name,
expire_time:number
})
}
})
},
changeBtn(){
// console.log(1);
this.setData({
btn_disable:false
})
},
bindtapChild:function(e){
var uren = e.currentTarget.id;
if( uren == 'false' ){
wx.showToast({
title: '正在禁用',
icon: 'error',
duration: 2000
})
}else{
TimeID:-1,
clearTimeout(this.TimeID);
this.TimeID = setTimeout(() => {
var openid = wx.getStorageSync('openid');
wx.request({
url: 'http://www.ceshi.com/index/InControllerCreate',
data:{
openid:openid,
id:this.data.id
},
success:function(res) {
console.log(res.data);
}
})
}, 1000);
}
},
onShareAppMessage: function () {
}
})
?小程序 获取 缓存值
wx.setStorage({
key:"openid",
data:res.data.openid
})
var openid = wx.getStorageSync('openid');
laravel缓存数据
缓存Cache
\Cache::put('mdopenid',$mdopenid,7200);
查询Cache
$en= \Cache::get('mdopenid');
laravel 框架 登录?
public function index(Request $request)
{
//获取code码
$code = $request['code'];
$avatarUrl = $request['avatarUrl'];
$nickName = $request['nickName'];
$ser = 'https://api.weixin.qq.com/sns/jscode2session?appid='.config('api.AppID').'&secret='.config('api.AppSecret').'&js_code='.$code.'&grant_type=authorization_code';
$dae= file_get_contents($ser);
$json = json_decode($dae,true);
$openid = $json['openid'];
$date = rentings::where('openid',$openid)->get()->ToArray();
if (empty($date)){
$name['code']=$code;
$name['avatarUrl']=$avatarUrl;
$name['nickName']=$nickName;
rentings::create($name);
}
$mdopenid=md5($openid);
\Cache::put('mdopenid',$mdopenid,7200);
return ['openid'=>$mdopenid];
}
?laravel 框架 查询数据
public function sel(Request $request)
{
$name = $request['name'];
$mdopenid = \Cache::get('mdopenid');
if ($name == $mdopenid){
return ['res'=>'未登录'];
}
$date = u_commodity::all()->ToArray();
$datetime = strtotime(date('Y-m-d h:i:s'));
foreach ($date as $i=>$value)
{
$date[$i]['create'] = $value['create']-$datetime;
}
return ['date'=>$date];
}
laravel 框架 详情页查询
public function selShow(Request $request)
{
$id = $request['id'];
$datetime = strtotime(date('Y-m-d h:i:s'));
$nameGet = u_commodity::where('id',$id)->get();
foreach ($nameGet as $value)
{
$value['create']= $value['create']-$datetime;
}
return ['name'=>$nameGet];
}
?减库存
public function InControllerCreate(Request $request)
{
$openid = $request['openid'];
$en= \Cache::get('mdopenid');
if ($openid != $en){
return ['res'=>'未登录'];
}
//数据查询是否存在
$data['id'] = $request['id'];
$user =Order::where('u_id',$data['id'])->where('openid',$openid)->get()->toarray();
if (!empty($user)){
return ['code'=>302,'msg'=>'你已抢过了'];
}
$goods = u_commodity::find($data['id'])->toarray();
for(!$i=1; $i <= $goods['number']; $i++){
Redis::lpush($goods['id'], $i);
}
$count = Redis::rpop($goods['id']);
if ($count != 0 ){
return $this->SelIn($request['openid'],$request['id']);
}else{
return ['code'=>302,'msg'=>'无库存'];
}
}
public function SelIn($openid,$id)
{
$date['openid']=$openid;
$date['u_id']=$id;
Order::create($date);
$this->orderForm($date);
return ['code'=>302,'msg'=>'购买成功'];
}
//数据库
public function orderForm($date)
{
$id['id'] = $date['u_id'];
$data = u_commodity::find($id)->ToArray();
$number['number'] = $data[0]['number']-1;
u_commodity::where('id',$id['id'])->update($number);
return ['code'=>200,'msg'=>'库存已减'];
}
<?php
namespace App\Http\Controllers\show;
use App\Http\Controllers\Controller;
use App\Models\Order;
use App\Models\rentings;
use App\Models\u_commodity;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
class index extends Controller
{
public function index(Request $request)
{
//获取code码
$code = $request['code'];
$avatarUrl = $request['avatarUrl'];
$nickName = $request['nickName'];
$ser = 'https://api.weixin.qq.com/sns/jscode2session?appid='.config('api.AppID').'&secret='.config('api.AppSecret').'&js_code='.$code.'&grant_type=authorization_code';
$dae= file_get_contents($ser);
$json = json_decode($dae,true);
$openid = $json['openid'];
$date = rentings::where('openid',$openid)->get()->ToArray();
if (empty($date)){
$name['code']=$code;
$name['avatarUrl']=$avatarUrl;
$name['nickName']=$nickName;
rentings::create($name);
}
$mdopenid=md5($openid);
\Cache::put('mdopenid',$mdopenid,7200);
return ['openid'=>$mdopenid];
}
public function sel(Request $request)
{
$name = $request['name'];
$mdopenid = \Cache::get('mdopenid');
if ($name == $mdopenid){
return ['res'=>'未登录'];
}
$date = u_commodity::all()->ToArray();
$datetime = strtotime(date('Y-m-d h:i:s'));
foreach ($date as $i=>$value)
{
$date[$i]['create'] = $value['create']-$datetime;
}
return ['date'=>$date];
}
public function selShow(Request $request)
{
$id = $request['id'];
$nameGet = u_commodity::where('id',$id)->get();
return ['name'=>$nameGet];
}
public function InControllerCreate(Request $request)
{
$openid = $request['openid'];
$en= \Cache::get('mdopenid');
if ($openid != $en){
return ['res'=>'未登录'];
}
//数据查询是否存在
$data['id'] = $request['id'];
$user =Order::where('u_id',$data['id'])->where('openid',$openid)->get()->toarray();
if (!empty($user)){
return ['code'=>302,'msg'=>'你已抢过了'];
}
$goods = u_commodity::find($data['id'])->toarray();
for(!$i=1; $i <= $goods['number']; $i++){
Redis::lpush($goods['id'], $i);
}
$count = Redis::rpop($goods['id']);
if ($count != 0 ){
return $this->SelIn($request['openid'],$request['id']);
}else{
return ['code'=>302,'msg'=>'无库存'];
}
}
public function SelIn($openid,$id)
{
$date['openid']=$openid;
$date['u_id']=$id;
Order::create($date);
$this->orderForm($date);
return ['code'=>302,'msg'=>'购买成功'];
}
//数据库
public function orderForm($date)
{
$id['id'] = $date['u_id'];
$data = u_commodity::find($id)->ToArray();
$number['number'] = $data[0]['number']-1;
u_commodity::where('id',$id['id'])->update($number);
return ['code'=>200,'msg'=>'库存已减'];
}
}
|