You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
5.1 KiB

7 years ago
4 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Model;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Support\Facades\Log;
  5. //短信发送记录
  6. class SmsLog extends BaseModel
  7. {
  8. protected $table = 'sms_log';
  9. public $timestamps = false;
  10. protected $hidden = array();
  11. protected $guarded = array(); //$guarded包含你不想被赋值的字段数组。
  12. const SUCCESS = 1;
  13. const FAIL = 2;
  14. public function getDb()
  15. {
  16. return DB::table($this->table);
  17. }
  18. public static function success($mobile, $text, $result)
  19. {
  20. self::create([
  21. 'mobile' => $mobile,
  22. 'text' => $text,
  23. 'status' => self::SUCCESS,
  24. 'result' => json_encode($result)
  25. ]);
  26. }
  27. public static function fail($mobile, $text, $result)
  28. {
  29. self::create([
  30. 'mobile' => $mobile,
  31. 'text' => $text,
  32. 'status' => self::FAIL,
  33. 'result' => json_encode($result)
  34. ]);
  35. }
  36. /**
  37. * 列表
  38. * @param array $where 查询条件
  39. * @param string $order 排序
  40. * @param string $field 字段
  41. * @param int $offset 偏移量
  42. * @param int $limit 取多少条
  43. * @return array
  44. */
  45. public function getList($where = array(), $order = '', $field = '*', $offset = 0, $limit = 15)
  46. {
  47. $model = $this->getDb();
  48. if($where){$model = $model->where($where);}
  49. $res['count'] = $model->count();
  50. $res['list'] = array();
  51. if($res['count'] > 0)
  52. {
  53. if($field){if(is_array($field)){$model = $model->select($field);}else{$model = $model->select(\DB::raw($field));}}
  54. if($order){$model = parent::getOrderByData($model, $order);}
  55. if($offset){}else{$offset = 0;}
  56. if($limit){}else{$limit = 15;}
  57. $res['list'] = $model->skip($offset)->take($limit)->get();
  58. }
  59. return $res;
  60. }
  61. /**
  62. * 分页,用于前端html输出
  63. * @param array $where 查询条件
  64. * @param string $order 排序
  65. * @param string $field 字段
  66. * @param int $limit 每页几条
  67. * @param int $page 当前第几页
  68. * @return array
  69. */
  70. public function getPaginate($where = array(), $order = '', $field = '*', $limit = 15)
  71. {
  72. $res = $this->getDb();
  73. if($where){$res = $res->where($where);}
  74. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  75. if($order){$res = parent::getOrderByData($res, $order);}
  76. if($limit){}else{$limit = 15;}
  77. return $res->paginate($limit);
  78. }
  79. /**
  80. * 查询全部
  81. * @param array $where 查询条件
  82. * @param string $order 排序
  83. * @param string $field 字段
  84. * @param int $limit 取多少条
  85. * @return array
  86. */
  87. public function getAll($where = array(), $order = '', $field = '*', $limit = '', $offset = '')
  88. {
  89. $res = $this->getDb();
  90. if($where){$res = $res->where($where);}
  91. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  92. if($order){$res = parent::getOrderByData($res, $order);}
  93. if($offset){$res = $res->skip($offset);}
  94. if($limit){$res = $res->take($limit);}
  95. $res = $res->get();
  96. return $res;
  97. }
  98. /**
  99. * 获取一条
  100. * @param array $where 条件
  101. * @param string $field 字段
  102. * @return array
  103. */
  104. public function getOne($where, $field = '*')
  105. {
  106. $res = $this->getDb();
  107. if($where){$res = $res->where($where);}
  108. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  109. $res = $res->first();
  110. return $res;
  111. }
  112. /**
  113. * 添加
  114. * @param array $data 数据
  115. * @return int
  116. */
  117. public function add(array $data,$type = 0)
  118. {
  119. if($type==0)
  120. {
  121. // 新增单条数据并返回主键值
  122. return self::insertGetId(parent::filterTableColumn($data,$this->table));
  123. }
  124. elseif($type==1)
  125. {
  126. /**
  127. * 添加单条数据
  128. * $data = ['foo' => 'bar', 'bar' => 'foo'];
  129. * 添加多条数据
  130. * $data = [
  131. * ['foo' => 'bar', 'bar' => 'foo'],
  132. * ['foo' => 'bar1', 'bar' => 'foo1'],
  133. * ['foo' => 'bar2', 'bar' => 'foo2']
  134. * ];
  135. */
  136. return self::insert($data);
  137. }
  138. }
  139. /**
  140. * 修改
  141. * @param array $data 数据
  142. * @param array $where 条件
  143. * @return int
  144. */
  145. public function edit($data, $where = array())
  146. {
  147. $res = $this->getDb();
  148. return $res->where($where)->update(parent::filterTableColumn($data, $this->table));
  149. }
  150. /**
  151. * 删除
  152. * @param array $where 条件
  153. * @return bool
  154. */
  155. public function del($where)
  156. {
  157. $res = $this->getDb();
  158. $res = $res->where($where)->delete();
  159. return $res;
  160. }
  161. }