Shoporder.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 支付接口
  7. */
  8. namespace app\allwin\controller\api\v4;
  9. use app\allwin\controller\api\Base;
  10. use app\allwin\model\AllwinShopOrder;
  11. use app\common\facade\Inform;
  12. class Shoporder extends Base{
  13. /**
  14. * 初始化当前应用是否登录
  15. * @return void
  16. */
  17. public function initialize() {
  18. parent::initialize();
  19. $this->isUserAuth();
  20. }
  21. /**
  22. * @param int $store_id
  23. * @return \think\response\Json
  24. * @throws \think\db\exception\DataNotFoundException
  25. * @throws \think\db\exception\ModelNotFoundException
  26. * @throws \think\exception\DbException
  27. * 扫码店铺待核销订单列表
  28. */
  29. public function index(){
  30. $param['active'] = $this->request->param('active/d',0);
  31. $param['page'] = $this->request->param('page/d',1);
  32. $param['signkey'] = $this->request->param('signkey');
  33. $param['sign'] = $this->request->param('sign');
  34. $rel = $this->apiSign($param);
  35. if($rel['code'] != 200){
  36. return enjson($rel['code'],'签名验证失败');
  37. }
  38. $condition['user_id'] = $this->user->id;
  39. $condition['is_del'] = 0;
  40. switch ($param['active']) {
  41. case 1:
  42. $condition['paid_at'] = 0;
  43. break;
  44. case 2:
  45. $condition['paid_at'] = 1;
  46. $condition['status'] = 0;
  47. break;
  48. case 3:
  49. $condition['paid_at'] = 1;
  50. $condition['status'] = 1;
  51. break;
  52. }
  53. $order = AllwinShopOrder::with(['store' => function($query) {
  54. $query->field('name,address,id');
  55. }])->field('store_id,id,order_no,paid_at,paid_time,phone,points,message,shop_id,status,shop_cache,amount,create_time,is_del')->where($condition)->paginate(10);
  56. if($order->isEmpty()){
  57. return enjson(204);
  58. }
  59. $data = [];
  60. $ids = [];
  61. foreach ($order as $key => $value) {
  62. $data[$key] = $value;
  63. $data[$key]['shop_cache'] = json_decode($value['shop_cache']);
  64. if($value['paid_at']){
  65. $data[$key]['status_text'] = $value['status']?'已核销':'待核销';
  66. $data[$key]['end_paytime'] = 0;
  67. }else{
  68. $data[$key]['status_text'] = '待支付';
  69. $data[$key]['end_paytime'] = (($value['create_time'] + 60 * 10) - time()) * 1000;
  70. }
  71. if($value['paid_at'] == 0 && $data[$key]['end_paytime'] < 0){
  72. $ids[] = $value['id'];
  73. unset($data[$key]);
  74. }
  75. }
  76. //过期了更改了状态
  77. if(!empty($ids)){
  78. AllwinShopOrder::where(['id' => $ids])->update(['is_del' => 1]);
  79. }
  80. if(empty($data)){
  81. return enjson(204);
  82. }
  83. return enjson(200,'成功',array_values($data));
  84. }
  85. /**
  86. * 统计订单
  87. */
  88. public function count(){
  89. $param['signkey'] = $this->request->param('signkey');
  90. $param['sign'] = $this->request->param('sign');
  91. $rel = $this->apiSign($param);
  92. if($rel['code'] != 200){
  93. return enjson($rel['code'],'签名验证失败');
  94. }
  95. $pending = AllwinShopOrder::where(['user_id' => $this->user->id,'is_del' => 0,'paid_at' => 0])->count();
  96. $order = AllwinShopOrder::where(['user_id' => $this->user->id,'is_del' => 0,'paid_at' => 1,'status' => 0])->count();
  97. return enjson(200,'成功',[$pending,$order]);
  98. }
  99. /**
  100. * @param int $store_id
  101. * @return \think\response\Json
  102. * @throws \think\db\exception\DataNotFoundException
  103. * @throws \think\db\exception\ModelNotFoundException
  104. * @throws \think\exception\DbException
  105. * 扫码店铺待核销订单列表
  106. */
  107. public function getViews(){
  108. $param['id'] = $this->request->param('id/d',0);
  109. $param['signkey'] = $this->request->param('signkey');
  110. $param['sign'] = $this->request->param('sign');
  111. $rel = $this->apiSign($param);
  112. if($rel['code'] != 200){
  113. return enjson($rel['code'],'签名验证失败');
  114. }
  115. $condition['user_id'] = $this->user->id;
  116. $condition['id'] = $param['id'];
  117. $order = AllwinShopOrder::with(['store' => function($query) {
  118. $query->field('name,address,id,longitude,latitude');
  119. }])->field('store_id,id,order_no,paid_at,paid_time,phone,points,message,shop_id,status,shop_cache,amount,create_time,is_del')->where($condition)->find();
  120. if(empty($order)){
  121. return enjson(204);
  122. }
  123. $data = $order->toArray();
  124. $data['shop_cache'] = json_decode($order->shop_cache,true);
  125. $data['shop_cache']['notice'] = str_replace('<img', '<img class="img" style="max-width:100%;height:auto"',dehtml($data['shop_cache']['notice']));
  126. if($data['paid_at']){
  127. $data['end_paytime'] = 0;
  128. }else{
  129. $data['end_paytime'] = (($order->create_time + 60 * 10) - time()) * 1000;
  130. }
  131. if($order->paid_at == 0 && $data['end_paytime'] < 0){
  132. $order->is_del = 1;
  133. $order->save();
  134. }
  135. $data['distance'] = getDistance($this->qqgps['lng'],$this->qqgps['lat'],$order->store->longitude,$order->store->latitude);
  136. $data['paid_time'] = date('Y-m-d H:i:s',$order->paid_time);
  137. $data['create_time'] = date('Y-m-d H:i:s',$order->create_time);
  138. return enjson(200,'成功',$data);
  139. }
  140. /**
  141. * @param int $order_id
  142. * @return \think\response\Json
  143. * @throws \think\db\exception\DataNotFoundException
  144. * @throws \think\db\exception\ModelNotFoundException
  145. * @throws \think\exception\DbException
  146. * 获取核心订单
  147. */
  148. public function orderVer(){
  149. $param['id'] = $this->request->param('id/d',0);
  150. $param['signkey'] = $this->request->param('signkey');
  151. $param['sign'] = $this->request->param('sign');
  152. $rel = $this->apiSign($param);
  153. if($rel['code'] != 200){
  154. return enjson($rel['code'],'签名验证失败');
  155. }
  156. $condition['id'] = $param['id'];
  157. $condition['paid_at'] = 1;
  158. $condition['status'] = 0;
  159. $condition['is_del'] = 0;
  160. $info = AllwinShopOrder::with(['store' => function($query) {
  161. $query->field('name,address,id,longitude,latitude');
  162. }])->where($condition)->field('store_id,user_id,id,order_no,paid_at,paid_time,phone,points,message,shop_id,status,shop_cache,amount,create_time,is_del')->find();
  163. if(empty($info)){
  164. return enjson(303,'订单不存在',['url' => '/pages/index']);
  165. }
  166. $uid = [];
  167. if(!empty($info->storeWorker)){
  168. foreach ($info->storeWorker as $key => $value) {
  169. if($value['is_cashier'] == 1){
  170. $uid[] = $value['uid'];
  171. }
  172. }
  173. $uid = array_flip($uid);
  174. }
  175. if (!isset($uid[$this->user->id])){
  176. return enjson(301,'你无权限核销订单',['url' => '/pages/store/views?store_id='.$info->store_id]);
  177. }
  178. if(request()->isPost()){
  179. Inform::sms($info->user_id,$this->miniapp_id,['title' =>'['.$info->store->name.']订单已核销','type' => '核销通知','state'=>'成功','content' =>'单号:'.$info->order_no,'url' => 'pages/index']);
  180. Inform::sms($info->user->id,$this->miniapp_id,['title' =>'你成功核销一个订单','type' => '核销通知','state'=>'成功','content' =>'单号:'.$info->order_no,'url' => 'pages/index']);
  181. $info->status = 1;
  182. $info->save();
  183. return enjson(301,'订单成功核销',['url' => '/pages/store/views?store_id='.$info->store_id]);
  184. }else{
  185. $shop_cache= json_decode($info->shop_cache,true);
  186. $shop_cache['notice'] = dehtml($shop_cache['notice']);
  187. $info->shop_cache = $shop_cache;
  188. $info->paid_time = date('Y-m-d H:i:s',$info->paid_time);
  189. $info->create_time = date('Y-m-d H:i:s',$info->create_time);
  190. return enjson(200,'成功',$info);
  191. }
  192. }
  193. }