Yorder.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?php
  2. namespace app\api\controller;
  3. use addons\epay\library\Service;
  4. use app\admin\model\Method;
  5. use app\admin\model\method\Time;
  6. use app\admin\model\Order;
  7. use app\admin\model\Ordercode;
  8. use app\admin\model\Orderitem;
  9. use app\admin\model\Pingjia;
  10. use app\admin\model\Smsorder;
  11. use app\admin\model\Tzmoneyrecharge;
  12. use app\admin\model\Usercar;
  13. use app\admin\model\UserMoneyLog;
  14. use app\common\controller\Api;
  15. use EasyWeChat\Factory;
  16. use think\Db;
  17. use Yansongda\Pay\Pay;
  18. class Yorder extends Api
  19. {
  20. protected $noNeedLogin = [];
  21. protected $noNeedRight = ['*'];
  22. public function order_no(){
  23. $order_model=new Order();
  24. $order_id= input('order_id');
  25. $pay_type = input('pay_type');
  26. $order_new_no = order_no($this->auth->id);
  27. $order_model->isUpdate('true',['id'=>$order_id])->save(['order_no'=>$order_new_no,'pay_type'=>$pay_type]);
  28. $this->success('重新生成订单号',$order_id);
  29. }
  30. /**
  31. * 钓鱼下单
  32. * @return void
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. * @throws \think\exception\DbException
  36. */
  37. public function order_create(){
  38. $method_model =new Method();
  39. $order_no = order_no($this->auth->id);
  40. $data=input();
  41. // $data['item']=[['id'=>133,'title'=>'14:00-17:00','price'=>50,'type'=>1,'number'=>10],['id'=>2,'title'=>'女钓友福利','price'=>0,'type'=>2,'number'=>1]];
  42. // $data['user']=[['name'=>1,'phone'=>1223],['name'=>2,'phone'=>122],['name'=>3,'phone'=>110]];
  43. foreach ($data['item'] as &$d){
  44. if($d['type']==1){
  45. $time_model = new Time();
  46. // print_r($d);die;
  47. $time_info = $time_model->where('id',$d['id'])->find();
  48. if($time_info['is_set']==1){
  49. if($d['number']>$time_info['sale_num']){
  50. $this->error('所选时间段座位不够了');
  51. }
  52. }
  53. // print_r($d['number']);print_r($time_info['sale_num']);die;
  54. }
  55. }
  56. $method_info = $method_model->where('id',$data['method_id'])->field('type,show_images,moshi,user_id,yutang_id,yutang_name,eat_price,yajin')->find();
  57. $images = explode(',',$method_info['show_images']);
  58. $order_data = [
  59. 'user_id'=>$this->auth->id,
  60. 'order_no'=>$order_no,
  61. 'image'=>$images[0],
  62. 'yutang'=>$method_info['yutang_name'],
  63. 't_user_id'=>$method_info['user_id'],
  64. 'yutang_id'=>$method_info['yutang_id'],
  65. 'method_id'=>$data['method_id'],
  66. 'pay_type'=>$data['type'],
  67. 'item_money'=>$data['item_money'],
  68. 'wucan_money'=>$data['wucan_money'],
  69. 'yajin_money'=>$data['yajin_money'],
  70. 'youhui_money'=>config('site.reduction'),
  71. 'order_money'=>$data['order_money'],
  72. 'status'=>1,
  73. 'create_time'=>time(),
  74. 'number'=>$data['number'],
  75. 'type'=>$method_info['type'],
  76. 'moshi'=>$method_info['moshi'],
  77. 'phone'=>$this->auth->mobile,
  78. ];
  79. //zhifu
  80. Db::startTrans();
  81. try {
  82. $order_model = new Order();
  83. $order_id = $order_model->insertGetId($order_data);
  84. foreach ($data['item'] as $v){
  85. for ($i=0;$i<$v['number'];$i++){
  86. $ruchang_number='RC'.rand('000000','999999').$order_id;
  87. $code1 = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/index.php/api/writeoff/saoma'.'?'.'ruchang'.'='.$ruchang_number;
  88. $ruchang_code= 'code/ruchang/'.$ruchang_number.'.png';//图片路径名
  89. $ruchang_price = $v['price'];
  90. $this->code($code1,$ruchang_code);
  91. if($data['wucan_money']>0) {
  92. $wucan_number = 'WC'.rand('000000', '999999') . $order_id;
  93. $code2 = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/index.php/api/writeoff/saoma'.'?'.'wucan'.'='.$wucan_number;
  94. $wucan_code= 'code/wucan/'.$wucan_number.'.png';//图片路径名
  95. $wucan_price = $method_info['eat_price'];
  96. $this->code($code2,$wucan_code);
  97. }
  98. $yajin_number='YJ'.rand('000000','999999').$order_id;
  99. $code3 = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/index.php/api/writeoff/saoma'.'?'.'yajin'.'='.$yajin_number;
  100. $yajin_code= 'code/yajin/'.$yajin_number.'.png';//图片路径名
  101. $yajin_price = $method_info['yajin'];
  102. $this->code($code3,$yajin_code);
  103. $code_data =[
  104. 'order_id'=>$order_id,
  105. 'ruchang_number'=>$ruchang_number,
  106. 'ruchang_code'=>'/'.$ruchang_code,
  107. 'ruchang_price'=>$ruchang_price,
  108. 'ruchang_status'=>1,
  109. 'yajin_number'=>$yajin_number,
  110. 'yajin_code'=>'/'.$yajin_code,
  111. 'yajin_price'=>$yajin_price,
  112. 'yajin_status'=>1,
  113. 'type'=>$v['type'],
  114. 'time_id'=>$v['id'],
  115. 'method_id'=>$data['method_id'],
  116. ];
  117. if($data['wucan_money']>0){
  118. $code_data[ 'wucan_number']=$wucan_number;
  119. $code_data[ 'wucan_code']='/'.$wucan_code;
  120. $code_data['wucan_price'] = $wucan_price;
  121. $code_data[ 'wucan_status']=1;
  122. }
  123. $order_code =new Ordercode();
  124. $order_code->insert($code_data);
  125. }
  126. $item_data[] =[
  127. 'order_id'=>$order_id,
  128. 'time_id'=>$v['id'],
  129. 'title'=>$v['title'],
  130. 'number'=>$v['number'],
  131. 'price'=>$v['price'],
  132. 'order_money'=>$v['price']*$v['number'],
  133. 'type'=>$v['type']
  134. ];
  135. if($v['type']==1){
  136. $end=explode('-',$v['title']);
  137. $order_model->isUpdate('true',['id'=>$order_id])->save(['end'=>$end[1],'sta'=>$end[0]]);
  138. }
  139. }
  140. $list_code = $order_code->where('order_id',$order_id)->select();
  141. foreach ($list_code as $kk=>$vv){
  142. $order_code->isUpdate(true,['id'=>$list_code[$kk]['id']])->save(['name'=>$data['user'][$kk]['name'],'phone'=>$data['user'][$kk]['phone']]);
  143. if($method_info['type']==2&&$method_info['moshi']!=1){
  144. $dis_model = new \app\admin\model\Distribution();
  145. $dis_model->insert(['method_id'=>$data['method_id'],'title'=>$v['title'],'phone'=>$data['user'][$kk]['phone'],'type'=>1]);
  146. }
  147. }
  148. $order_item_model = new Orderitem();
  149. $order_item_model->insertAll($item_data);
  150. //生成对应数量二维码
  151. Db::commit();
  152. $this->success('订单下单成功',$order_id);
  153. }catch (\ErrorException $e){
  154. Db::rollback();
  155. $this->error('订单失败');
  156. }
  157. }
  158. public function order_list(){
  159. $order_model = new Order();
  160. $status=input('status',0);
  161. //订单状态:1=待支付,2=待核销,3=进行中,4=待评价,5=已完成,6=退款中,7=已退款,8=已取消
  162. switch ($status){
  163. case 0:
  164. $array = [0,1,2,3,4,5,6,7,8];
  165. break;
  166. case 1:
  167. $array = [1];
  168. break;
  169. case 2:
  170. $array = [2];
  171. break;
  172. case 3:
  173. $array = [3];
  174. break;
  175. case 4:
  176. $array = [4];
  177. break;
  178. case 5:
  179. $array = [6,7];
  180. break;
  181. case 6:
  182. $array = [8];
  183. break;
  184. }
  185. $list= $order_model->where('user_id',$this->auth->id)->whereIn('status',$array)->order('id','desc')->paginate();
  186. foreach ($list as &$v){
  187. if($v['status']==1){
  188. $vtime=$v['create_time']+15*60-time();
  189. if($vtime<0){
  190. $order_model->isUpdate('true',['id'=>$v['id']])->save(['status'=>8]);
  191. }
  192. $v['time'] =$vtime;
  193. }
  194. }
  195. $this->success('我的订单列表',$list);
  196. }
  197. public function order_details(){
  198. $order_id= input('order_id');
  199. $order_model = new Order();
  200. $order_item =new Orderitem();
  201. $order_code = new Ordercode();
  202. $dis_model = new \app\admin\model\Distribution();
  203. $order_info= $order_model->where('o.id',$order_id)->alias('o')->join('method m','o.method_id=m.id')->field('o.*,m.huodong_time')->find();
  204. if(empty($order_info)){
  205. $this->error('订单信息有误');
  206. }
  207. if($order_info['status']){
  208. $order_info['time']=$order_info['create_time']+30*60-time();
  209. }
  210. $order_info['item'] = $order_item->where('order_id',$order_id)->select();
  211. $order_info['code'] = $order_code->where('order_id',$order_id)->select();
  212. foreach ($order_info['code'] as $k =>$v){
  213. $zuowei = $dis_model->where('method_id',$order_info['code'][$k]['method_id'])->where('phone',$order_info['code'][$k]['phone'])->value('number');
  214. $v['zuowei']=$zuowei;
  215. }
  216. $this->success('订单详情',$order_info);
  217. }
  218. public function order_pingjia(){
  219. $data = input();
  220. $order_model = new Order();
  221. $yutang_model = new \app\admin\model\Yutang();
  222. $order_info = $order_model->where('id',$data['order_id'])->find();
  223. if($order_info['status']==4){
  224. $pingjia_data=[
  225. 'yutang_id'=>$order_info['yutang_id'],
  226. 'user_id'=>$this->auth->id,
  227. 'score'=>$data['score'],
  228. 'content'=>$data['content'],
  229. 'images'=>$data['images'],
  230. 'create_time'=>time()
  231. ];
  232. $pingjia_model = new Pingjia();
  233. $pingjia_model->insert($pingjia_data);
  234. $sum_pingja = $pingjia_model->where('yutang_id',$order_info['yutang_id'])->sum('score');
  235. $count_pingjia = $pingjia_model->where('yutang_id',$order_info['yutang_id'])->count('id');
  236. $pingjia = floor($sum_pingja/$count_pingjia);
  237. $yutang_model->isUpdate('true',['id'=>$order_info['yutang_id']])->save(['pingjie'=>$pingjia]);
  238. $this->success('感谢你的评价');
  239. }
  240. else{
  241. $this->error('订单状态不是待评价');
  242. }
  243. }
  244. public function pingjia_list(){
  245. $yutang_id= input('yutang_id');
  246. $pingjia_model = new Pingjia();
  247. $pingjia= $pingjia_model->alias('p')->join('user u','u.id=p.user_id')->where('p.yutang_id',$yutang_id)->order('p.id','desc')->field('p.*,u.nickname,u.avatar')->paginate();
  248. $this->success('查看更多评价',$pingjia);
  249. }
  250. public function cancel_order(){
  251. $order_id= input('order_id');
  252. $order_model = new Order();
  253. $up = $order_model->isUpdate('true',['user_id'=>$this->auth->id,'id'=>$order_id,'status'=>1])->save(['status'=>8]);
  254. if($up){
  255. $this->success('订单已取消');
  256. }
  257. else{
  258. $this->error('订单信息审核不正确');
  259. }
  260. }
  261. public function reason_order(){
  262. $data = input();
  263. $order_model = new Order();
  264. $order_info = $order_model->where('id',$data['order_id'])->where('status',2)->find();
  265. if(empty($order_info)){
  266. $this->error('您的订单不支持退款了!');
  267. }
  268. $order_data = [
  269. 'reson_name'=>$data['reson_name'],
  270. 'reson_desc'=>$data['reson_desc'],
  271. 'reson_images'=>$data['reson_images'],
  272. 'reson_money'=>$order_info['order_money'],
  273. 'reson_time'=>date('Y-m-d H:i:s'),
  274. 'status'=>7,
  275. ];
  276. // 退款功能
  277. if($order_info['pay_type']=='微信支付') {
  278. $config = Service::getConfig('wechat');
  279. $app = Pay::wechat($config);
  280. $refund_data=[
  281. 'out_trade_no' => $order_info['order_no'],
  282. 'out_refund_no' => time(),
  283. 'total_fee' => $order_info['order_money']*100,
  284. 'refund_fee' => $order_info['order_money']*100,
  285. 'refund_desc' => '订单退款',
  286. 'type'=>'app'
  287. ];
  288. $result = $app->refund($refund_data);
  289. if ($result['return_code'] == 'FAIL' || $result['result_code'] == 'FAIL') {
  290. $order_model->save(['status' => 2], ['id' => $data['order_id'], 'user_id' => $this->auth->id]);
  291. $this->error('退款出错请联系商家');
  292. }
  293. }
  294. elseif ($order_info['pay_type']=='支付宝支付'){
  295. $config = Service::getConfig('alipay');
  296. $aipay = Pay::alipay($config);
  297. $refund_data=[
  298. 'out_trade_no' => $order_info['order_no'],
  299. 'refund_amount' => $order_info['order_money'],
  300. ];
  301. $ai_refund = $aipay->refund($refund_data);
  302. if ($ai_refund['return_code'] == 'FAIL' || $ai_refund['result_code'] == 'FAIL') {
  303. $order_model->save(['status' => 2], ['id' => $data['order_id'], 'user_id' => $this->auth->id]);
  304. $this->error('退款出错请联系商家');
  305. }
  306. }
  307. else{
  308. $user_money_log_model = new UserMoneyLog();
  309. $user=$this->auth->getUser();
  310. $money_log = [
  311. 'user_id' => $this->auth->id,
  312. 'type' => 2,
  313. 'memo' => '订单退款',
  314. 'money' => $order_info['order_money'],
  315. 'before' => $user->money,
  316. 'after' => $user->money + $order_info['order_money'],
  317. 'createtime' => time(),
  318. ];
  319. $user->money +=$order_info['order_money'];
  320. $user->save();
  321. $user_money_log_model->insert($money_log);
  322. }
  323. $order_model->isUpdate('true',['id'=>$data['order_id']])->save($order_data);
  324. $sms_model = new Sms();
  325. $sms_model->order_reund($order_info['phone'],$order_info['order_no'],$order_info['order_money']);
  326. $this->success('退款成功,稍后到账');
  327. }
  328. public function code($code,$image){
  329. vendor('phpqrcode.phpqrcode');//包含类库文件
  330. $a= new \QRcode();//实例化类
  331. $errorCorrectionLevel = 'L';//容错级别
  332. $matrixPointSize = 6;//生成图片大小
  333. //生成二维码图片
  334. $a->png($code,$image, $errorCorrectionLevel, $matrixPointSize, 2);//生成二
  335. }
  336. public function user_car(){
  337. $user_car_model = new Usercar();
  338. $data = input();
  339. $usercar_data=[
  340. 'user_id'=>$this->auth->id,
  341. 'name'=>$data['name'],
  342. 'phone'=>$data['phone'],
  343. ];
  344. $user_car_model->insert($usercar_data);
  345. $this->success('添加钓客信息成功');
  346. }
  347. public function user_car_del(){
  348. $user_car_model = new Usercar();
  349. $car_id = input('car_id');
  350. $user_car_model->where('id',$car_id)->delete();
  351. $this->success('钓客信息已删除');
  352. }
  353. public function user_car_list(){
  354. $user_car_model = new Usercar();
  355. $list = $user_car_model->where('user_id',$this->auth->id)->order('id','ASC')->select();
  356. $this->success('钓客信息',$list);
  357. }
  358. public function huiyu(){
  359. $sms_model = new Sms();
  360. $user_model = new \app\admin\model\User();
  361. $order_model = new Order();
  362. $order_id = input('order_id');
  363. $order_info = $order_model->where('id',$order_id)->field('order_no,t_user_id')->find();
  364. $user_info= $user_model->where('id',$order_info['t_user_id'])->field('sms_number,mobile,duanxin')->find();
  365. $user = $this->auth->getUser();
  366. if ($user_info['sms_number'] > 0 && $user_info['duanxin'] == 1) {
  367. $sms_model->order_huiyu($user_info['mobile'], $order_info['order_no'], $user->nickname);
  368. }
  369. $message_data =[
  370. 'category_id' => 2,
  371. 'user_id' => $order_info['t_user_id'],
  372. 'title' => '订单消息',
  373. 'order_id' => $order_id,
  374. 'name' => '',
  375. 'description' => '塘主您好,订单号'.$order_info['order_no'].'用户'.$user->nickname.'发起回鱼申请,请及时处理。',
  376. 'is_read' => 1,
  377. 'createtime' => time(),
  378. ];
  379. $message_model = new \app\admin\model\Message();
  380. $message_model->insert($message_data);
  381. $this->success('已通知鱼塘主');
  382. }
  383. public function sms_order(){
  384. $sms_model = new Smsorder();
  385. $data= input();
  386. $add_data = [
  387. 'user_id' => $this->auth->id,
  388. 'order_no'=>'SMS'.time().$this->auth->id,
  389. 'username'=>$this->auth->nickname,
  390. 'phone' => $this->auth->mobile,
  391. 'number'=>$data['number'],
  392. 'pay_money'=>$data['pay_money'],
  393. 'pay_type'=>$data['pay_type'],
  394. 'status'=>2,
  395. 'create_time'=>time(),
  396. ];
  397. $order_id = $sms_model->insertGetId($add_data);
  398. $this->success('下单成功',$order_id);
  399. }
  400. public function money_order(){
  401. $sms_model = new Tzmoneyrecharge();
  402. $data= input();
  403. $add_data = [
  404. 'user_id' => $this->auth->id,
  405. 'order_no'=>'Bl'.time().$this->auth->id,
  406. 'money'=>$data['money'],
  407. 'pay_type'=>$data['pay_type'],
  408. 'status'=>1,
  409. 'create_time'=>time(),
  410. ];
  411. $order_id = $sms_model->insertGetId($add_data);
  412. $this->success('下单成功',$order_id);
  413. }
  414. }