Lave.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace app\common\model;
  3. use liuniu\BaseModel;
  4. use think\Exception;
  5. use think\Model;
  6. class Lave extends BaseModel
  7. {
  8. // 表名
  9. protected $name = 'lave';
  10. // 自动写入时间戳字段
  11. protected $autoWriteTimestamp = 'int';
  12. // 定义时间戳字段名
  13. protected $createTime = 'createtime';
  14. protected $updateTime = false;
  15. protected $deleteTime = false;
  16. // 追加属性
  17. protected $append = [
  18. 'is_open_text',
  19. 'is_ticket_text',
  20. 'paid_text',
  21. 'pay_type_text',
  22. 'paytime_text',
  23. 'type_text'
  24. ];
  25. public function getIsOpenList()
  26. {
  27. return ['0' => __('Is_open 0'), '1' => __('Is_open 1')];
  28. }
  29. public function getIsTicketList()
  30. {
  31. return ['0' => __('Is_ticket 0'), '1' => __('Is_ticket 1')];
  32. }
  33. public function getPaidList()
  34. {
  35. return ['0' => __('Paid 0'), '1' => __('Paid 1')];
  36. }
  37. public function getPayTypeList()
  38. {
  39. return ['0' => __('Pay_type 0'), '1' => __('Pay_type 1'), '2' => __('Pay_type 2'), '3' => __('Pay_type 3'), '4' => __('Pay_type 4')];
  40. }
  41. public function getTypeList()
  42. {
  43. return ['0' => __('Type 0'), '1' => __('Type 1')];
  44. }
  45. public function getIsOpenTextAttr($value, $data)
  46. {
  47. $value = $value ? $value : (isset($data['is_open']) ? $data['is_open'] : '');
  48. $list = $this->getIsOpenList();
  49. return isset($list[$value]) ? $list[$value] : '';
  50. }
  51. public function getIsTicketTextAttr($value, $data)
  52. {
  53. $value = $value ? $value : (isset($data['is_ticket']) ? $data['is_ticket'] : '');
  54. $list = $this->getIsTicketList();
  55. return isset($list[$value]) ? $list[$value] : '';
  56. }
  57. public function getPaidTextAttr($value, $data)
  58. {
  59. $value = $value ? $value : (isset($data['paid']) ? $data['paid'] : '');
  60. $list = $this->getPaidList();
  61. return isset($list[$value]) ? $list[$value] : '';
  62. }
  63. public function getPayTypeTextAttr($value, $data)
  64. {
  65. $value = $value ? $value : (isset($data['pay_type']) ? $data['pay_type'] : '');
  66. $list = $this->getPayTypeList();
  67. return isset($list[$value]) ? $list[$value] : '';
  68. }
  69. public function getPaytimeTextAttr($value, $data)
  70. {
  71. $value = $value ? $value : (isset($data['paytime']) ? $data['paytime'] : '');
  72. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  73. }
  74. public function getTypeTextAttr($value, $data)
  75. {
  76. $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
  77. $list = $this->getTypeList();
  78. return isset($list[$value]) ? $list[$value] : '';
  79. }
  80. protected function setPaytimeAttr($value)
  81. {
  82. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  83. }
  84. public static function lst($where)
  85. {
  86. $model = new self;
  87. $xwhere = null;
  88. $order = "id desc";
  89. if (isset($where['cid']) && $where['cid'] > 0) $xwhere['cid'] = $where['cid'];
  90. if (isset($where['user_id']) && $where['user_id'] > 0) $xwhere['user_id'] = $where['user_id'];
  91. if (isset($where['category_id']) && $where['category_id'] > 0) $xwhere['category_id'] = $where['category_id'];
  92. if (isset($where['paid']) && $where['paid'] > -1) $xwhere['paid'] = $where['paid'];
  93. if (isset($where['help_id']) && $where['help_id'] > -1) $xwhere['help_id'] = $where['help_id'];
  94. if (isset($where['order']) && $where['order'] != '') $order = $where['order'];
  95. if (isset($where['key']) && $where['key'] != '') $xwhere['name|contact|tel'] = ['like', "%{$where['key']}%"];
  96. $data = $model->where($xwhere)->order($order)->page($where['page'], $where['limit'])->select();
  97. $count = $model->where($xwhere)->count();
  98. return compact('count', 'data');
  99. }
  100. public static function lave_create($where)
  101. {
  102. try {
  103. if ($where['cid'] == 0 || $where['user_id'] == 0 || $where['amount'] <= 0 || empty($where['name']) || empty($where['tel'])) return self::setErrorInfo('请核对信息');
  104. if ($where['type'] == 1 && $where['contact'] == '') return self::setErrorInfo('请核对信息');
  105. $where['order_id'] = self::getNewOrderId();
  106. $rs = self::create($where);
  107. return $rs;
  108. } catch (Exception $e) {
  109. return self::setErrorInfo($e->getMessage());
  110. }
  111. }
  112. public static function getNewOrderId()
  113. {
  114. do {
  115. list($msec, $sec) = explode(' ', microtime());
  116. $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
  117. $orderId = 'wx' . $msectime . mt_rand(10000, 99999);
  118. } while (self::where(['order_id' => $orderId])->find());
  119. return $orderId;
  120. }
  121. public static function paySuccess($cid, $order_id)
  122. {
  123. $max_sn = self::where('cid', $cid)->order('SN DESC')->value('SN');
  124. if ($max_sn) {
  125. $no = intval(explode("_", $max_sn)[1]) + 1;
  126. } else {
  127. $no = 1;
  128. }
  129. $SN = $cid . "_" . str_pad($no, 8, '0', STR_PAD_LEFT);
  130. $res = self::where('order_id', $order_id)->where('cid', $cid)->update(['paid' => 1, 'paytime' => time(), "SN" => $SN]);//订单改为支付
  131. return false !== $res;
  132. }
  133. }