Qrcode.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php namespace App\Models\WeiBan;
  2. use Illuminate\Database\Eloquent\Factories\HasFactory;
  3. use Illuminate\Database\Eloquent\Model;
  4. /**
  5. * 订单落地Banner
  6. *
  7. */
  8. class Qrcode extends Model
  9. {
  10. use HasFactory;
  11. // 与模型关联的表名
  12. protected $table = 'weiban_qrcode';
  13. // 是否主动维护时间戳
  14. public $timestamps = false;
  15. // 定义时间戳字段名
  16. // const CREATED_AT = 'insert_time';
  17. // const UPDATED_AT = 'update_time';
  18. /**
  19. * 添加数据
  20. *
  21. */
  22. public function add($data)
  23. {
  24. // 时间
  25. $data['insert_time'] = time();
  26. $data['update_time'] = time();
  27. // 写入数据表
  28. $id = $this->query()->insertGetId($data);
  29. // 失败结果
  30. if( !$id ) return 0;
  31. // 更新缓存
  32. $this->getList(true);
  33. // 返回结果
  34. return $id;
  35. }
  36. /**
  37. * 添加数据
  38. *
  39. */
  40. public function edit($id,$data)
  41. {
  42. // 更新时间
  43. $data['update_time'] = time();
  44. // 写入数据表
  45. $result = $this->query()->where(['id'=>$id])->update($data);
  46. // 失败结果
  47. if( !$result ) return 0;
  48. // 更新缓存
  49. $this->getList(true);
  50. // 返回结果
  51. return $result;
  52. }
  53. /**
  54. * 获取列表
  55. * @param Bool $force 是否强制更新
  56. *
  57. */
  58. public function getList($force = false)
  59. {
  60. // 结果数据
  61. $list = $force ? [] : cache('weiban:qrcode:list');
  62. // 不存在数据
  63. if ( !$list ) {
  64. // 从数据库获取数据
  65. $data = $this->query()->where([['status','=',0]])->get(['id','name','thumb','link_url','city_ids','status']);
  66. // 是否有数据
  67. $data = $data ? $data->toArray() : [];
  68. // 循环处理数据
  69. $list = [];
  70. // 进行更新
  71. foreach ($data as $value) {
  72. // 处理图片
  73. $value['thumb'] = $value['thumb'] ? path_compat($value['thumb']) : '';
  74. // 重组数据
  75. $list[$value['id']] = $value;
  76. }
  77. // 存起来
  78. cache(['weiban:qrcode:list'=>$list]);
  79. }
  80. // 返回结果
  81. return $list;
  82. }
  83. /**
  84. * 获取配置平台对应的应用数据
  85. *
  86. * @param int 用户ID
  87. * @param string 指定字段
  88. *
  89. */
  90. public function getOne($id,$field='')
  91. {
  92. // 获取列表数据
  93. $list = $this->getList();
  94. // 获取数据
  95. $one = isset($list[$id]) ? $list[$id] : [];
  96. // 返回值
  97. return empty($field) ? $one : ( isset($one[$field]) ? $one[$field] : null);
  98. }
  99. /**
  100. * 获取配置平台对应的应用数据
  101. *
  102. * @param int $cityId 城市ID
  103. *
  104. */
  105. public function getFollowQrcode($cityId){
  106. // 获取列表数据
  107. $list = $this->getList();
  108. // 列表数据不存在
  109. if( !$list ) return ['thumb'=>'','link_url'=>''];
  110. // 全国的码
  111. $all = [];
  112. // 城市的码
  113. $city = [];
  114. // 循环列表
  115. foreach ($list as $key => $value) {
  116. // 如果没有限制城市
  117. if( empty($value['city_ids']) ) {
  118. $all[] = $value;
  119. continue;
  120. }
  121. // 如果限制了的话,转数组
  122. $value['city_ids'] = explode(',',$value['city_ids']);
  123. // 判断用户的城市是否在内
  124. if( in_array($cityId,$value['city_ids']) ) {
  125. $city[] = $value;
  126. continue;
  127. }
  128. // 不在范围的删除
  129. unset($list[$key]);
  130. }
  131. // 先获取城市的,再获取全国的
  132. $one = $city ? array_shift($city) : array_shift($all);
  133. // 判断是否存在二维码
  134. return ['thumb'=>( empty($one['thumb']) ? '' : $one['thumb']),'link_url'=>( empty($one['link_url']) ? '' : $one['link_url'])];
  135. }
  136. }