Order.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace app\models\store;
  3. use app\models\user\User;
  4. use app\models\user\UserBill;
  5. use crmeb\basic\BaseModel;
  6. use crmeb\traits\ModelTrait;
  7. use think\Exception;
  8. class Order extends BaseModel
  9. {
  10. use ModelTrait;
  11. private static $appid ="jnz_formal_0001";
  12. private static $appsecret = "mxtw58mxbexwjk40ghin";
  13. private static $url = "https://new.juniuzu.com";
  14. public function setSpecAttr($value)
  15. {
  16. return json_encode($value);
  17. }
  18. public function getSpecAttr($value)
  19. {
  20. return json_decode($value,true);
  21. }
  22. /**
  23. * 生成订单唯一id
  24. * @param $uid 用户uid
  25. * @return string
  26. */
  27. public static function getNewOrderId()
  28. {
  29. list($msec, $sec) = explode(' ', microtime());
  30. $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
  31. $orderId = 'pk' . $msectime . mt_rand(10000, 99999);
  32. while (self::be(['order_id' => $orderId])) $orderId = 'pk' . $msectime . mt_rand(10000, 99999);
  33. return $orderId;
  34. }
  35. /**
  36. * 创建订单
  37. * @param $data
  38. * @return bool
  39. */
  40. public static function order_create($data)
  41. {
  42. try {
  43. $data['order_id'] = self::getNewOrderId();
  44. $data['pay_time'] = time();
  45. $data['pay_type'] = 'integral';
  46. $data['paid'] = 1;
  47. $rs = self::create($data);
  48. if ($rs) {
  49. User::where('uid', $data['uid'])->dec('integral', $data['pay_price'])->update();
  50. UserBill::expend('积分兑换', $data['uid'], 'integral', 'pay', $data['pay_price'], $rs['id'], User::where('uid', $data['uid'])->value('integral'), "兑换商品,支付" . $data['pay_price'] . "积分!");
  51. }
  52. return $rs;
  53. }catch (Exception $e)
  54. {
  55. return self::setErrorInfo('创建订单出错!'.$e->getMessage());
  56. }
  57. }
  58. /**
  59. * 订单列表
  60. * @param $where
  61. * @return array
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\DbException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. */
  66. public static function lst($where)
  67. {
  68. $model = new self;
  69. if(isset($where['uid']) && $where['uid'] !=0) $model = $model->where('uid',$where['uid']);
  70. if(isset($where['data']) && $where['data'] !='') $model = $model->getModelTime($where,$model,"pay_time");
  71. if(isset($where['paid']) && $where['paid'] >-1) $model = $model->where('paid',$where['paid']);
  72. if(isset($where['status']) && $where['status'] >-4) $model = $model->where('status',$where['status']);
  73. if(isset($where['key']) && $where['key'] !='') $model = $model->where('order_id','like',"%".$where['key']."%");
  74. $model = $model->order('id desc');
  75. $count = $model->value('count(id)')?:0;
  76. $data = $model->page($where['page'],$where['limit'])->select()->toArray();
  77. return compact('count','data');
  78. }
  79. /**
  80. * 获取系统订单列表
  81. * @param $where
  82. * @return array
  83. */
  84. public static function sys_lst($where)
  85. {
  86. $model = new self;
  87. if(isset($where['data']) && $where['data'] !='') $model = $model->getModelTime($where,$model,"add_time");
  88. if(isset($where['paid']) && $where['paid'] >-1) $model = $model->where('paid',$where['paid']);
  89. if(isset($where['status']) && $where['status'] >-4) $model = $model->where('status',$where['status']);
  90. if(isset($where['key']) && $where['key'] !='') $model = $model->where('order_id','like',"%".$where['key']."%");
  91. $model = $model->order('id desc');
  92. return self::page($model, function ($v){
  93. }, $where);
  94. }
  95. public static function sign($param)
  96. {
  97. $param['appid'] = self::$appid;
  98. $param['appsecret'] = self::$appsecret;
  99. ksort($param);
  100. $string = self::toUrlParams($param);
  101. $string = md5( $string );
  102. //签名步骤四:所有字符转为大写
  103. $param['sign'] = strtoupper( $string );
  104. unset($param['appsecret']);
  105. return $param;
  106. }
  107. /**
  108. * @param $data
  109. *
  110. * @return string
  111. */
  112. protected static function toUrlParams( $data ) {
  113. $str = '';
  114. foreach( $data as $key => $value ) {
  115. if( $key != "sign" && !is_array( $value ) ) {
  116. $str .= '&' . $key . '=' . rawurldecode( $value );
  117. }
  118. }
  119. $str = ltrim( $str, '&' );
  120. return $str;
  121. }
  122. }