ServeOrderRepository.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace app\common\repositories\system\serve;
  3. use app\common\dao\system\serve\ServeOrderDao;
  4. use app\common\repositories\BaseRepository;
  5. use app\common\repositories\store\product\ProductCopyRepository;
  6. use ln\services\PayService;
  7. use think\exception\ValidateException;
  8. use think\facade\Cache;
  9. use think\facade\Db;
  10. class ServeOrderRepository extends BaseRepository
  11. {
  12. protected $dao;
  13. public function __construct(ServeOrderDao $dao)
  14. {
  15. $this->dao = $dao;
  16. }
  17. public function QrCode(int $merId, array $data)
  18. {
  19. $ret = app()->make(ServeMealRepository::class)->get($data['meal_id']);
  20. if(!$ret) throw new ValidateException('数据不存在');
  21. $key = 'Meal_'.$merId.'_'.$data['meal_id'].'_'.date('YmdH',time());
  22. $arr = [
  23. 'meal_id' => $ret['meal_id'],
  24. 'name' => $ret['name'],
  25. 'num' => $ret['num'],
  26. 'price' => $ret['price'],
  27. 'type' => $ret['type'],
  28. ];
  29. if(!$result = Cache::store('file')->get($key)){
  30. $order_sn = $this->setOrderSn();
  31. $param = [
  32. 'status' => 0,
  33. 'is_del' => 0,
  34. 'mer_id' => $merId,
  35. 'type' => $ret['type'],
  36. 'meal_id'=> $ret['meal_id'],
  37. 'pay_type' => $data['pay_type'],
  38. 'order_sn' => $order_sn,
  39. 'attach' => 'meal',
  40. 'order_info' => json_encode($arr,JSON_UNESCAPED_UNICODE),
  41. 'pay_price' => $ret['price'],
  42. 'body' => $order_sn,
  43. ];
  44. $type = $data['pay_type'] == 1 ? 'weixinQr' : 'alipayQr';
  45. $service = new PayService($type,$param);
  46. $code = $service->pay(null);
  47. $endtime = time() + 1800 ;
  48. $result = [
  49. 'config' => $code['config'],
  50. 'endtime'=> date('Y-m-d H:i:s',$endtime),
  51. ];
  52. Cache::store('file')->set($key,$result,30);
  53. $param['key'] = $key;
  54. Cache::store('file')->set($order_sn,$param,60 * 24);
  55. }
  56. return $result;
  57. }
  58. public function paySuccess($data)
  59. {
  60. $dat = Cache::store('file')->get($data['order_sn']);
  61. $get = $this->dao->getWhere(['order_sn' => $data['order_sn']]);
  62. if(!$get){
  63. Db::transaction(function () use($data,$dat){
  64. $key = $dat['key'];
  65. unset($dat['attach'],$dat['body'],$dat['key']);
  66. $dat['status'] = 1;
  67. $this->dao->create($dat);
  68. $info = json_decode($dat['order_info']);
  69. app()->make(ProductCopyRepository::class)->add([
  70. 'type' => $dat['type'] == 1 ? 'pay_copy' : 'pay_dump',
  71. 'num' => $info->num,
  72. 'info' => $dat['order_info'],
  73. 'message' => $dat['type'] == 1 ? '购买复制商品套餐' : '购买电子面单套餐',
  74. ],$dat['mer_id']);
  75. Cache::store('file')->delete($data['order_sn']);
  76. Cache::store('file')->delete($key);
  77. });
  78. }
  79. }
  80. public function setOrderSn()
  81. {
  82. list($msec, $sec) = explode(' ', microtime());
  83. $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
  84. $orderId = 'cs' . $msectime . mt_rand(10000, max(intval($msec * 10000) + 10000, 98369));
  85. return $orderId;
  86. }
  87. public function getList(array $where, int $page, int $limit)
  88. {
  89. $where['is_del'] = 0;
  90. $query = $this->dao->getSearch($where)->with([
  91. 'merchant' => function($query){
  92. $query->field('mer_id,mer_name');
  93. }
  94. ])->order('create_time DESC');
  95. $count = $query->count();
  96. $list = $query->page($page, $limit)->select();
  97. return compact('count','list');
  98. }
  99. }