thinkphp lists todo

来由:

数据库的这个字段我想返回成:

新奇的写法如下:

逻辑层的代码:

  public function goodsDetail($goodId)
    {
        $detail = $this->good->where('id', $goodId)->hidden(['type_params','user_id'])->find();
        if (!$detail) {
            return $this->fail("获取数据失败!");
        }
        if ($detail->type === 1) {

            //特殊的写法
            $detail->award_lists = '';
        } else if ($detail->type === 2) {
            $number_group_buyer = Order::where('good_id', $goodId)->where('status',1)->count('id');
            $detail->number_group_buyers = $number_group_buyer;
        }

        return $this->success("获取数据成功", $detail);

    }

模型层的代码:

<?php

namespace app\daogou\model;

use app\common\model\TimeModel;

class Goods extends TimeModel
{
    protected $table = 'dg_goods';

    protected $type = [
        'type_params'=>'json',
    ];



    //type_params:
    //      type = 1 : [['coin_id'=>1,'award'=>"0.80000000"],......]
    //      type = 2 : ['max_number'=>10,'winner'=>1]
    //      type = 3 : null

    public function user()
    {
        return $this->hasOne('user','user_id','id')->bind(['username']);
    }

    public function getStatusTxtAttr($v,$data)
    {
        //状态(-1:下架,1:上架,2:交易中)
        $status = [-1=>"下架",1=>"上架",2=>"交易中"];
        if(array_key_exists('status',$data)){
            return array_key_exists($data['status'],$status) ? $status[$data['status']] : $data['status'];
        }
        return '';
    }


    public function getPriceAttr($v)
    {
        return delZero($v);
    }
    public function getTypeTxtAttr($v,$data){
        //(1:奖购2:团购,3:闪购)
        $status = [1=>"奖购",2=>"团购",3=>"闪购"];
        if(array_key_exists('type',$data)){
            return array_key_exists($data['type'],$status) ? $status[$data['type']] : $data['type'];
        }
        return '';
    }




// 不理解的代码
    public function getAwardListsAttr($v, $good)
    {
        $awardLists = [];
        if ($good['type'] === 1) {
            $type_params = json_decode($good['type_params'],true);
            foreach ($type_params as $propertyId => $properNumber) {
                $propertyName = Coin::where('id', $propertyId)->value('name');
                $award['asset_name'] = $propertyName;
                $award['asset_number'] = $properNumber;
                array_push($awardLists,$award);
            }
        }
        return $awardLists;
    }



}

可以通过 :

复制代码
$ba = debug_backtrace();
var_dump($ba);
die();

来查看执行的流程,,流程如下(个人感觉看起来 【展示的效果】 不是很友好 很乱的感觉 ):

array(8) {
  [0]=>
  array(7) {
    ["file"]=>
    string(66) "D:\wwwroot\yyex\thinkphp\library\think\model\concern\Attribute.php"
    ["line"]=>
    int(500)
    ["function"]=>
    string(17) "getAwardListsAttr"
    ["class"]=>
    string(22) "app\daogou\model\Goods"
    ["object"]=>
    object(app\daogou\model\Goods)#58 (2) {
      ["data"]=>
      array(14) {
        ["id"]=>
        int(14)
        ["user_id"]=>
        int(2)
        ["type"]=>
        int(1)
        ["name"]=>
        string(14) "仙侠小说ss"
        ["photo"]=>
        string(20) "http://ff12.ffsky.cn"
        ["price"]=>
        string(6) "102.30"
        ["introduction"]=>
        string(5) "ccsss"
        ["phone"]=>
        string(11) "13246885231"
        ["type_params"]=>
        string(9) "{"20":22}"
        ["status"]=>
        int(1)
        ["create_time"]=>
        int(1701844614)
        ["update_time"]=>
        int(1701844614)
        ["remark"]=>
        NULL
        ["award_lists"]=>
        string(0) ""
      }
      ["relation"]=>
      array(0) {
      }
    }
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(2) {
      [0]=>
      string(0) ""
      [1]=>
      array(14) {
        ["id"]=>
        int(14)
        ["user_id"]=>
        int(2)
        ["type"]=>
        int(1)
        ["name"]=>
        string(14) "仙侠小说ss"
        ["photo"]=>
        string(20) "http://ff12.ffsky.cn"
        ["price"]=>
        string(6) "102.30"
        ["introduction"]=>
        string(5) "ccsss"
        ["phone"]=>
        string(11) "13246885231"
        ["type_params"]=>
        string(9) "{"20":22}"
        ["status"]=>
        int(1)
        ["create_time"]=>
        int(1701844614)
        ["update_time"]=>
        int(1701844614)
        ["remark"]=>
        NULL
        ["award_lists"]=>
        string(0) ""
      }
    }
  }
  [1]=>
  array(7) {
    ["file"]=>
    string(67) "D:\wwwroot\yyex\thinkphp\library\think\model\concern\Conversion.php"
    ["line"]=>
    int(179)
    ["function"]=>
    string(7) "getAttr"
    ["class"]=>
    string(11) "think\Model"
    ["object"]=>
    object(app\daogou\model\Goods)#58 (2) {
      ["data"]=>
      array(14) {
        ["id"]=>
        int(14)
        ["user_id"]=>
        int(2)
        ["type"]=>
        int(1)
        ["name"]=>
        string(14) "仙侠小说ss"
        ["photo"]=>
        string(20) "http://ff12.ffsky.cn"
        ["price"]=>
        string(6) "102.30"
        ["introduction"]=>
        string(5) "ccsss"
        ["phone"]=>
        string(11) "13246885231"
        ["type_params"]=>
        string(9) "{"20":22}"
        ["status"]=>
        int(1)
        ["create_time"]=>
        int(1701844614)
        ["update_time"]=>
        int(1701844614)
        ["remark"]=>
        NULL
        ["award_lists"]=>
        string(0) ""
      }
      ["relation"]=>
      array(0) {
      }
    }
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(1) {
      [0]=>
      string(11) "award_lists"
    }
  }
  [2]=>
  array(7) {
    ["file"]=>
    string(67) "D:\wwwroot\yyex\thinkphp\library\think\model\concern\Conversion.php"
    ["line"]=>
    int(250)
    ["function"]=>
    string(7) "toArray"
    ["class"]=>
    string(11) "think\Model"
    ["object"]=>
    object(app\daogou\model\Goods)#58 (2) {
      ["data"]=>
      array(14) {
        ["id"]=>
        int(14)
        ["user_id"]=>
        int(2)
        ["type"]=>
        int(1)
        ["name"]=>
        string(14) "仙侠小说ss"
        ["photo"]=>
        string(20) "http://ff12.ffsky.cn"
        ["price"]=>
        string(6) "102.30"
        ["introduction"]=>
        string(5) "ccsss"
        ["phone"]=>
        string(11) "13246885231"
        ["type_params"]=>
        string(9) "{"20":22}"
        ["status"]=>
        int(1)
        ["create_time"]=>
        int(1701844614)
        ["update_time"]=>
        int(1701844614)
        ["remark"]=>
        NULL
        ["award_lists"]=>
        string(0) ""
      }
      ["relation"]=>
      array(0) {
      }
    }
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(0) {
    }
  }
  [3]=>
  array(5) {
    ["function"]=>
    string(13) "jsonSerialize"
    ["class"]=>
    string(11) "think\Model"
    ["object"]=>
    object(app\daogou\model\Goods)#58 (2) {
      ["data"]=>
      array(14) {
        ["id"]=>
        int(14)
        ["user_id"]=>
        int(2)
        ["type"]=>
        int(1)
        ["name"]=>
        string(14) "仙侠小说ss"
        ["photo"]=>
        string(20) "http://ff12.ffsky.cn"
        ["price"]=>
        string(6) "102.30"
        ["introduction"]=>
        string(5) "ccsss"
        ["phone"]=>
        string(11) "13246885231"
        ["type_params"]=>
        string(9) "{"20":22}"
        ["status"]=>
        int(1)
        ["create_time"]=>
        int(1701844614)
        ["update_time"]=>
        int(1701844614)
        ["remark"]=>
        NULL
        ["award_lists"]=>
        string(0) ""
      }
      ["relation"]=>
      array(0) {
      }
    }
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(0) {
    }
  }
  [4]=>
  array(4) {
    ["file"]=>
    string(56) "D:\wwwroot\yyex\thinkphp\library\think\response\Json.php"
    ["line"]=>
    int(36)
    ["function"]=>
    string(11) "json_encode"
    ["args"]=>
    array(2) {
      [0]=>
      *RECURSION*
      [1]=>
      int(256)
    }
  }
  [5]=>
  array(7) {
    ["file"]=>
    string(51) "D:\wwwroot\yyex\thinkphp\library\think\Response.php"
    ["line"]=>
    int(396)
    ["function"]=>
    string(6) "output"
    ["class"]=>
    string(19) "think\response\Json"
    ["object"]=>
    object(think\response\Json)#57 (8) {
      ["options"]=>
      array(1) {
        ["json_encode_param"]=>
        int(256)
      }
      ["contentType"]=>
      string(16) "application/json"
      ["data"]=>
      *RECURSION*
      ["charset"]=>
      string(5) "utf-8"
      ["code"]=>
      int(200)
      ["allowCache"]=>
      bool(true)
      ["header"]=>
      array(1) {
        ["Content-Type"]=>
        string(31) "application/json; charset=utf-8"
      }
      ["content"]=>
      NULL
    }
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(1) {
      [0]=>
      *RECURSION*
    }
  }
  [6]=>
  array(7) {
    ["file"]=>
    string(51) "D:\wwwroot\yyex\thinkphp\library\think\Response.php"
    ["line"]=>
    int(128)
    ["function"]=>
    string(10) "getContent"
    ["class"]=>
    string(14) "think\Response"
    ["object"]=>
    object(think\response\Json)#57 (8) {
      ["options"]=>
      array(1) {
        ["json_encode_param"]=>
        int(256)
      }
      ["contentType"]=>
      string(16) "application/json"
      ["data"]=>
      *RECURSION*
      ["charset"]=>
      string(5) "utf-8"
      ["code"]=>
      int(200)
      ["allowCache"]=>
      bool(true)
      ["header"]=>
      array(1) {
        ["Content-Type"]=>
        string(31) "application/json; charset=utf-8"
      }
      ["content"]=>
      NULL
    }
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(0) {
    }
  }
  [7]=>
  array(7) {
    ["file"]=>
    string(32) "D:\wwwroot\yyex\public\index.php"
    ["line"]=>
    int(18)
    ["function"]=>
    string(4) "send"
    ["class"]=>
    string(14) "think\Response"
    ["object"]=>
    object(think\response\Json)#57 (8) {
      ["options"]=>
      array(1) {
        ["json_encode_param"]=>
        int(256)
      }
      ["contentType"]=>
      string(16) "application/json"
      ["data"]=>
      *RECURSION*
      ["charset"]=>
      string(5) "utf-8"
      ["code"]=>
      int(200)
      ["allowCache"]=>
      bool(true)
      ["header"]=>
      array(1) {
        ["Content-Type"]=>
        string(31) "application/json; charset=utf-8"
      }
      ["content"]=>
      NULL
    }
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(0) {
    }
  }
}
复制代码
2:异步消息队列:
复制代码
tp6+异步任务队列+发送邮件+模型监听_tp6异步下载_Upordo的博客-CSDN博客

3:thinkphp6 + workman 实现即使聊天 通讯:

https://www.cnblogs.com/codehaoran/p/16817231.html

PHP聊天室框架workerman-chat

GitHub - walkor/workerman-chat: Websocket chat room written in PHP based on workerman.

前端聊天模板:lemon imui(gitee)

thinkphp :

复制代码
think PHP libary  Bootstrap类型
相关推荐
学习ing小白1 小时前
JavaWeb - 5 - 前端工程化
前端·elementui·vue
真的很上进1 小时前
【Git必看系列】—— Git巨好用的神器之git stash篇
java·前端·javascript·数据结构·git·react.js
胖虎哥er1 小时前
Html&Css 基础总结(基础好了才是最能打的)三
前端·css·html
qq_278063711 小时前
css scrollbar-width: none 隐藏默认滚动条
开发语言·前端·javascript
.ccl2 小时前
web开发 之 HTML、CSS、JavaScript、以及JavaScript的高级框架Vue(学习版2)
前端·javascript·vue.js
小徐不会写代码2 小时前
vue 实现tab菜单切换
前端·javascript·vue.js
2301_765347542 小时前
Vue3 Day7-全局组件、指令以及pinia
前端·javascript·vue.js
ch_s_t2 小时前
新峰商城之分类三级联动实现
前端·html
古猫先生2 小时前
YMTC Xtacking 4.0(Gen5)技术深度分析
服务器·人工智能·科技·云计算
辛-夷2 小时前
VUE面试题(单页应用及其首屏加载速度慢的问题)
前端·javascript·vue.js