Order.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. class Order extends Base {
  5. // 设置数据表(不含前缀)
  6. protected $name = 'order';
  7. // 定义时间戳字段名
  8. protected $createTime = '';
  9. protected $updateTime = '';
  10. // 自动完成
  11. protected $auto = [];
  12. protected $insert = [];
  13. protected $update = [];
  14. public function listData($where,$order,$page=1,$limit=20,$start=0)
  15. {
  16. $page = $page > 0 ? (int)$page : 1;
  17. $limit = $limit ? (int)$limit : 20;
  18. $start = $start ? (int)$start : 0;
  19. if(!is_array($where)){
  20. $where = json_decode($where,true);
  21. }
  22. $limit_str = ($limit * ($page-1) + $start) .",".$limit;
  23. $total = $this->alias('o')->where($where)->count();
  24. $list = Db::name('Order')->alias('o')
  25. ->field('o.*,u.user_name')
  26. ->join('__USER__ u','o.user_id = u.user_id','left')
  27. ->where($where)
  28. ->order($order)
  29. ->limit($limit_str)
  30. ->select();
  31. return ['code'=>1,'msg'=>lang('data_list'),'page'=>$page,'pagecount'=>ceil($total/$limit),'limit'=>$limit,'total'=>$total,'list'=>$list];
  32. }
  33. public function infoData($where,$field='*')
  34. {
  35. if(empty($where) || !is_array($where)){
  36. return ['code'=>1001,'msg'=>lang('param_err')];
  37. }
  38. $info = $this->field($field)->where($where)->find();
  39. if(empty($info)){
  40. return ['code'=>1002,'msg'=>lang('obtain_err')];
  41. }
  42. $info = $info->toArray();
  43. return ['code'=>1,'msg'=>lang('obtain_ok'),'info'=>$info];
  44. }
  45. public function saveData($data)
  46. {
  47. $validate = \think\Loader::validate('Order');
  48. if(!$validate->check($data)){
  49. return ['code'=>1001,'msg'=>lang('param_err').':'.$validate->getError() ];
  50. }
  51. $data['order_time'] = time();
  52. if(!empty($data['order_id'])){
  53. $where=[];
  54. $where['order_id'] = ['eq',$data['order_id']];
  55. $res = $this->allowField(true)->where($where)->update($data);
  56. }
  57. else{
  58. $res = $this->allowField(true)->insert($data);
  59. }
  60. if(false === $res){
  61. return ['code'=>1002,'msg'=>lang('save_err').':'.$this->getError() ];
  62. }
  63. return ['code'=>1,'msg'=>lang('save_ok')];
  64. }
  65. public function delData($where)
  66. {
  67. $res = $this->where($where)->delete();
  68. if($res===false){
  69. return ['code'=>1001,'msg'=>lang('del_err').':'.$this->getError() ];
  70. }
  71. return ['code'=>1,'msg'=>lang('del_ok')];
  72. }
  73. public function fieldData($where,$col,$val)
  74. {
  75. if(!isset($col) || !isset($val)){
  76. return ['code'=>1001,'msg'=>lang('param_err')];
  77. }
  78. $data = [];
  79. $data[$col] = $val;
  80. $res = $this->allowField(true)->where($where)->update($data);
  81. if($res===false){
  82. return ['code'=>1001,'msg'=>lang('set_err').':'.$this->getError() ];
  83. }
  84. return ['code'=>1,'msg'=>lang('set_ok')];
  85. }
  86. /*
  87. * 充值回调函数接口
  88. * 任何充值接口,回调接口里直接调用该接口更新订单状态、用户积分
  89. * pay_type预留值alipay,weixin,bank,可以继续自定义最长10个字符
  90. */
  91. public function notify($order_code,$pay_type)
  92. {
  93. if(empty($order_code) || empty($pay_type)){
  94. return ['code'=>1001,'msg'=>lang('param_err')];
  95. }
  96. $where = [];
  97. $where['order_code'] = $order_code;
  98. $order = model('Order')->infoData($where);
  99. if($order['code']>1){
  100. return $order;
  101. }
  102. if($order['info']['order_status'] == 1){
  103. return ['code'=>1,'msg'=>lang('model/order/pay_over')];
  104. }
  105. $where2=[];
  106. $where2['user_id'] = $order['info']['user_id'];
  107. $user = model('User')->infoData($where2);
  108. if($user['code']>1){
  109. return $user;
  110. }
  111. $update = [];
  112. $update['order_status'] = 1;
  113. $update['order_pay_time'] = time();
  114. $update['order_pay_type'] = $pay_type;
  115. $res = $this->where($where)->update($update);
  116. if($res===false){
  117. return ['code'=>2002,'msg'=>lang('model/order/update_status_err')];
  118. }
  119. $where2 = [];
  120. $where2['user_id'] = $user['info']['user_id'];
  121. $res = model('User')->where($where2)->setInc('user_points',$order['info']['order_points']);
  122. if($res===false){
  123. return ['code'=>2003,'msg'=>lang('model/order/update_user_points_err')];
  124. }
  125. //积分日志
  126. $data = [];
  127. $data['user_id'] = $user['info']['user_id'];
  128. $data['plog_type'] = 1;
  129. $data['plog_points'] = $order['info']['order_points'];
  130. model('Plog')->saveData($data);
  131. return ['code'=>1,'msg'=>lang('model/order/pay_ok')];
  132. }
  133. }