溯源码文件报错
<?php
namespace app\api\controller;
use app\api\model\Chagoodslist;
use app\api\model\Chagoodscode;
use app\common\controller\Api;
/**
* 扫码app 接口
*/
class Client extends Api
{
protected $noNeedLogin = ['*'];//不需要登录可访问方法
protected $noNeedRight = '*';
protected $file = __DIR__.'/../../../随机数.txt';
/**
* 应用首页
* 要求:1. 后期添加商品时,商品顺序需要遵从当前cha_goods_list里的顺序、id
*/
public function index(){
// 当前已绑定了多少个茶罐
$pot_bind_num = 0;
$pot_bind_num = Chagoodscode::where('id','<=',45040)
->where('goods_id','>',0)->count();
// 所有产品大类
$field = ['id, good_name, good_age, good_type, good_count as format, num, box_bind_num'];
$cha_goods_list = Chagoodslist::field($field)->where('good_count','>',0)->select();
foreach($cha_goods_list as &$item){
$item->append(['pot_bind_num']); // 添加一些参数
$item->hidden(['num']); // 暂时隐藏该系列产品 总数
}
$this->success('查询用户茶碗信息',[
'pot_bind_num' => $pot_bind_num,
'list'=>$cha_goods_list,
]);
}
/**
* 查询茶罐码状态
* @params $_POST['pcode']
*/
public function potStatus(){
$params = $this->request->post();
// 验证数据
$this->ApiValidate($params, 'app\api\validate\Chagoodslist', 'potStatus');
// 查询
$code = Chagoodscode::get(['code'=>$params['pcode']]);
// 不存在
is_null($code) && $this->error('无效code');
// 已被添加
$code->goods_id !==0 && $this->error('code已被使用');
// 是否被绑定
$code->top_gid !==0 && $this->error('code已被使用');
$this->success('code未被绑定,可以添加');
}
/**
* 准备扫码,添加茶罐
* @params['id','codes'=>['dpxxxx'....]]
*/
public function bindPots(){
$params = $this->request->post();
// 验证参数
$this->ApiValidate($params, 'app\api\validate\Chagoodslist', 'bindPots');
// 验证茶罐码
$string = str_replace('"','"', $params['codes']);
// $codes = json_decode(htmlspecialchars_decode($params['codes']),true);
$codes = json_decode($string,true);
// 不是数组
!is_array($codes) && $this->error('错误的codes');
// 是否有重复
$count_1 = count($codes);
$codes = array_unique($codes);
$count = count($codes);
$count !== $count_1 && $this->error('含有重复茶罐数据');
// 数量是否在规定内
!($count<17 && $count>7) && $this->error('罐码数量区间为8~16');
// 验证每一个code码是否符合规定
$pots_obj = [];
foreach ($codes as $key=>$pcode){
$c['pcode'] = $pcode;
// 正则验证
$this->ApiValidate($c, 'app\api\validate\Chagoodslist', 'potStatus');
// 是否可用
$res = Chagoodscode::where(['code'=>$pcode, 'goods_id&top_gid'=>0])->find();
if(!$res){
$this->error('第'.++$key.'个产品码无效,删除后再试');
break;
}
array_push($pots_obj,$res);
}
// 验证id是否存在
$field = ['id, good_name, good_age, good_type, good_count as format, num, box_bind_num'];
$product = Chagoodslist::field($field)->where('good_count','>',0)->where('id',$params['id'])->cache(true)->find();
!$product && $this->error('参数(id)错误,无法找到对应数据');
// 茶罐数量是否达到要求
$count !== $product->format && $this->error('该款产品只含有'.$product->format.'罐茶,你添加了'.$count.'罐');
// 添加绑定
if($this->bind($pots_obj,$product))
// 规格是否满足要求
$this->success('绑定成功');
else
$this->error('出现了一些错误,请联系开发人员处理');
}
// 绑定
private function bind($pots_obj,$product){
\think\Db::startTrans();
try{
// 生成盒码
$str = $this->generate_box_code();
// 保存生成的盒码
$top_gid = Chagoodscode::insertGetId([
'code'=>$str,
'goods_id'=>$product->id,
'status'=>0,
'top_gid'=>0,
'create_time'=>$_SERVER['REQUEST_TIME']
]);
// 更新茶罐码
foreach ($pots_obj as $item){
$item->goods_id = $product->id;
$item->top_gid = $top_gid;
$item->save();
}
// 更新产品表
$product->box_bind_num = ['inc',1];
$product->save();
\think\Db::commit();
}catch(\Exception $e){
\think\Db::rollback();
// var_dump($e);
\think\Log::error('【扫码app】:'.$e->getFile().' line:'.$e->getLine().' ErrorInfo=>'.$e->getMessage());
return false;
}
return true;
}
// 生成随机茶盒码
private function generate_box_code(){
if(!file_exists($this->file)){
$myfile = fopen($this->file, "w") or die('Unable to open '.$this->file.'!');
fclose($myfile);
}
return $this->rand_check(); // bzxxxxxx
}
// 开始检查随机数
private function rand_check(){
$str = 'bz'.$this->rand_(6);
if($this->each_($str)){
file_put_contents($this->file, $str.PHP_EOL, FILE_APPEND);
return $str;
}else{
$this->rand_check();
}
}
// 生成随机数
private function rand_($len){
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$string=$_SERVER['REQUEST_TIME'];
for(;$len>=1;$len--) {
$position=rand()%strlen($chars);
$position2=rand()%strlen($string);
$string=substr_replace($string,substr($chars,$position,1),$position2,0);
}
return $string;
}
// 读取txt文件
private function readTxt(){
$handle = fopen($this->file, 'rb');
while (feof($handle)===false) {
yield fgets($handle);
}
fclose($handle);
}
// 遍历是否有重复的内容
private function each_($str){
foreach ($this->readTxt() as $key => $value) {
if($value === $str)
return false;
else
return true;
}
}
}
报错的结果:
G:\phpstudy_pro\Extensions\php\php7.3.4nts\php.exe G:\phpstudy_pro\WWW\success_tea_nPhD3f\application\api\controller\Client.php
Fatal error: Uncaught Error: Class 'app\common\controller\Api' not found in G:\phpstudy_pro\WWW\success_tea_nPhD3f\application\api\controller\Client.php:12
Stack trace:
#0 {main}
thrown in G:\phpstudy_pro\WWW\success_tea_nPhD3f\application\api\controller\Client.php on line 12
PHP Fatal error: Uncaught Error: Class 'app\common\controller\Api' not found in G:\phpstudy_pro\WWW\success_tea_nPhD3f\application\api\controller\Client.php:12
Stack trace:
#0 {main}
thrown in G:\phpstudy_pro\WWW\success_tea_nPhD3f\application\api\controller\Client.php on line 12
进程已结束,退出代码为 255