Secondary.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. <?php
  2. namespace app\api\controller;
  3. use Alipay\EasySDK\Kernel\Util\ResponseChecker;
  4. use app\common\library\AliPay;
  5. use app\common\library\AliPay3;
  6. use app\common\library\HuijuH5Pay;
  7. use app\common\library\Shande;
  8. use EasyWeChat\Factory;
  9. use think\cache\driver\Redis;
  10. use think\Db;
  11. use function EasyWeChat\Kernel\Support\get_client_ip;
  12. use function Symfony\Component\String\length;
  13. /**
  14. * @title 二级市场
  15. * @controller secondary
  16. * @package app\api\controller
  17. */
  18. class Secondary extends Base
  19. {
  20. public function initialize(){
  21. parent::initialize();
  22. parent::check_login();
  23. }
  24. /**
  25. * @title 藏品出售
  26. * @desc 藏品出售
  27. * @author Gavin
  28. * @url /api/Secondary/sell
  29. * @method POST
  30. * @header name:Authorization require:1 desc:Token
  31. *
  32. * @param name:id type:int require:1 default:1 desc:藏品ID
  33. * @param name:resale_price type:decimal require:1 default:1 desc:出售价格
  34. * @param name:second_password type:int require:1 default:-- desc:二级密码
  35. *
  36. */
  37. public function sell(){
  38. $this->checkSwitch(1);
  39. $id = input('id');
  40. $user = getMemberInfoHash($this->uid); //获取用户信息
  41. if ($user['is_auth']==0) $this->error('请先实名认证!');
  42. $resale_price = input('resale_price');
  43. $second_password = input('second_password');
  44. if (!$id || !$resale_price) $this->error('参数错误');
  45. //if ($resale_price<'1') $this->error('最低1元');
  46. if ($resale_price<'0.1') $this->error('最低0.1元');
  47. if ($resale_price>'99999') $this->error('最高99999元');
  48. $info = Db::name('store_order_info')
  49. ->where('mid',$this->uid)
  50. ->where('id',$id)
  51. ->find();
  52. if (!$info || $info['status']==2) $this->error('藏品不存在');
  53. if ($info['resale_status']!=1) $this->error('挂售状态错误');
  54. $is_sell = Db::name('store_collection')->where('id',$info['c_id'])->value('is_sell');
  55. if (!$is_sell) $this->error('该藏品不支持寄售');
  56. if ($user['second_password']!=md5($second_password)) $this->error('密码错误');
  57. $update_data = [
  58. 'resale_status'=>2,
  59. 'resale_time'=>date('Y-m-d H:i:s'),
  60. 'resale_price'=>$resale_price
  61. ];
  62. if (Db::name('store_order_info')->where('id',$id)->update($update_data)){
  63. $this->success('成功');
  64. }
  65. $this->error('失败');
  66. }
  67. /**
  68. * @title 藏品取消出售
  69. * @desc 藏品取消出售
  70. * @author Gavin
  71. * @url /api/Secondary/cancel_sell
  72. * @method POST
  73. * @header name:Authorization require:1 desc:Token
  74. *
  75. * @param name:id type:int require:1 default:1 desc:藏品ID
  76. */
  77. public function cancel_sell(){
  78. $this->checkSwitch(1);
  79. $id = input('id');
  80. if (!$id) $this->error('参数错误');
  81. $info = Db::name('store_order_info')
  82. ->where('mid',$this->uid)
  83. ->where('id',$id)
  84. ->find();
  85. if (!$info || $info['status']==2) $this->error('藏品不存在');
  86. if ($info['resale_status']!=2) $this->error('挂售状态错误');
  87. //判断是否有待支付订单
  88. $count = Db::name('store_order_info_order')->where('info_id',$id)->where('status',0)->count();
  89. if ($count) $this->error('支付中,无法取消');
  90. $update_data = [
  91. 'resale_status'=>1,
  92. ];
  93. if (Db::name('store_order_info')->where('id',$id)->update($update_data)){
  94. $this->success('成功');
  95. }
  96. $this->error('失败');
  97. }
  98. /**
  99. * @title 标签列表
  100. * @desc 标签列表
  101. * @author Gavin
  102. * @url /api/Secondary/label_list
  103. * @method POST
  104. * @header name:Authorization require:1 desc:Token
  105. */
  106. public function label_list(){
  107. $list = Db::name('store_collection')->whereNotIn('label','测试,测试勿拍')->group('label')->column('label');
  108. $list = array_merge(['全部'],$list);
  109. $this->success('成功',['label_list'=>$list]);
  110. }
  111. /**
  112. * @title 二级市场列表
  113. * @desc 二级市场列表
  114. * @author Gavin
  115. * @url /api/Secondary/sell_list
  116. * @method POST
  117. * @header name:Authorization require:1 desc:Token
  118. * @param name:page type:int : default:1 desc:页数
  119. * @param name:page_num type:int : default:20 desc:每页数
  120. *
  121. * @param name:keyword type:string require:0 default: desc:关键词
  122. * @param name:label type:string require:0 default: desc:标签
  123. * @param name:time_order type:string require:0 default: desc:时间排序(asc:正序desc:倒序)
  124. * @param name:price_order type:string require:0 default: desc:价格排序(asc:正序desc:倒序)
  125. *
  126. * @return name:name type:string require:0 default:0 desc:藏品名称
  127. * @return name:cover type:string require:0 default:0 desc:图片
  128. * @return name:member_name type:string require:0 default:0 desc:出售人名称
  129. * @return name:member_headimg type:string require:0 default:0 desc:出售人头像
  130. * @return name:resale_price type:string require:0 default:0 desc:出售价格
  131. */
  132. public function sell_list(){
  133. $keyword = input('keyword');
  134. $label = input('label');
  135. $time_order = input('time_order');
  136. $price_order = input('price_order');
  137. $count = Db::name('store_order_info')
  138. ->where('status','neq',2)
  139. ->where('resale_status',2)
  140. ->where('is_destruction',1)
  141. ->when($keyword,function ($query) use ($keyword){
  142. $query->whereLike('name','%'.$keyword.'%');
  143. })
  144. ->when($label,function ($query) use ($label){
  145. if ($label!='全部'){
  146. $ids = Db::name('store_collection')->where('label',$label)->column('id');
  147. $query->whereIn('c_id',$ids);
  148. }
  149. })
  150. ->count();
  151. $list = Db::name('store_order_info')
  152. ->where('status','neq',2)
  153. ->where('resale_status',2)
  154. ->where('is_destruction',1)
  155. ->when($keyword,function ($query) use ($keyword){
  156. $query->whereLike('name','%'.$keyword.'%');
  157. })
  158. ->when($label,function ($query) use ($label){
  159. if ($label!='全部'){
  160. $ids = Db::name('store_collection')->where('label',$label)->column('id');
  161. $query->whereIn('c_id',$ids);
  162. }
  163. })
  164. ->when($price_order,function ($query) use ($price_order){
  165. $query->order('resale_price '.$price_order);
  166. })
  167. ->when($time_order,function ($query) use ($time_order){
  168. $query->order('resale_time '.$time_order);
  169. })
  170. ->limit($this->off_set,$this->page_num)
  171. ->select();
  172. foreach ($list as &$v){
  173. $member = getMemberInfoHash($v['mid']); //获取用户信息
  174. $v['member_name'] = $member['name'];
  175. $v['member_headimg'] = $member['headimg'];
  176. $v['pro_info'] = json_decode($v['pro_info'],true);
  177. $count = Db::name('store_order_info_order')->where('info_id',$v['id'])->where('status',0)->count();
  178. $v['is_buy'] = $count>0 ? 1 : 0;
  179. }
  180. $this->success('成功',compact('count','list'));
  181. }
  182. /**
  183. * @title 二级市场详情
  184. * @desc 二级市场详情
  185. * @author Gavin
  186. * @url /api/Secondary/sell_list_detail
  187. * @method POST
  188. * @header name:Authorization require:1 desc:Token
  189. * @param name:id type:int : default: desc:id
  190. */
  191. public function sell_list_detail(){
  192. $id = input('id');
  193. if (!$id) $this->error('参数错误');
  194. $info = Db::name('store_order_info')
  195. ->where('status','neq',2)
  196. ->where('resale_status',2)
  197. ->where('is_destruction',1)
  198. ->where('id',$id)
  199. ->find();
  200. if (!$info) $this->error('藏品不存在');
  201. $member = getMemberInfoHash($info['mid']); //获取用户信息
  202. $info['member_name'] = $member['name'];
  203. $info['member_headimg'] = $member['headimg'];
  204. $info['pro_info'] = json_decode($info['pro_info'],true);
  205. //判断是否有待支付订单
  206. $count = Db::name('store_order_info_order')->where('info_id',$id)->where('status',0)->count();
  207. $info['is_buy'] = $count>0 ? 1 : 0;
  208. $this->success('成功',$info);
  209. }
  210. /**
  211. * @title 购买
  212. * @desc 购买
  213. * @author Gavin
  214. * @url /api/Secondary/createOrder
  215. * @method POST
  216. * @header name:Authorization require:1 desc:Token
  217. * @param name:id type:int require:1 default: desc:主键ID
  218. * @param name:pay_type type:string require:1 default:wx desc:wx:微信zfb:支付宝sd:杉德h5
  219. * @param name:from type:string require:1 default:wx desc:wx:微信公众号h5:网页
  220. *
  221. * @return name:order_no type:int require:0 default:0 desc:订单号
  222. * @return name:pay type:string require:0 default:0 desc:支付信息
  223. */
  224. public function createOrder(){
  225. $this->checkSwitch(3);
  226. $redis = new Redis([ 'select'=> 2]);
  227. $redis_value = $redis->get('secondary_buy'.$this->uid);
  228. if ($redis_value){
  229. $this->error('请求过快,请稍后重试');
  230. }else{
  231. $redis->set('secondary_buy'.$this->uid,1,1);
  232. }
  233. $id = input('id');
  234. $pay_type = input('pay_type','wx');
  235. $this->checkSwitch(2,$pay_type);
  236. $from = input('from','wx');
  237. $user = getMemberInfoHash($this->uid); //获取用户信息
  238. if (!$id) $this->error('参数错误');
  239. if ($user['is_auth']==0) $this->error('请先实名认证!');
  240. $info = Db::name('store_order_info')->where('id',$id)->find();
  241. if (!$info) $this->error('藏品不存在');
  242. if ($info['resale_status']==3) $this->error('藏品已出售');
  243. if ($info['resale_status']==1) $this->error('藏品已撤销出售');
  244. if ($info['mid']==$this->uid) $this->error('不能购买自己出售的藏品');
  245. // if (isset($user['buy_time']) && $user['buy_time']>date('Y-m-d H:i:s')) $this->error('一小时取消3次以上,24小时内禁止下单');
  246. //判断是否有未支付订单
  247. $order_count = Db::name('store_order_info_order')->where('mid',$this->uid)->where('status',0)->count();
  248. if ($order_count) $this->error('有未支付订单,无法下单');
  249. //判断是否有待支付订单
  250. $count = Db::name('store_order_info_order')->where('info_id',$id)->where('status',0)->count();
  251. if ($count) $this->error('支付中,无法下单');
  252. $service_fee = getConfigValue('service_fee');
  253. $royalties = getConfigValue('royalties');
  254. $com = true;
  255. Db::startTrans();
  256. try {
  257. $order_no = get_order_sn();
  258. //获取价格
  259. $price = $info['resale_price'];
  260. $num = 1;
  261. $proportion = sprintf("%.2f",$price *($service_fee/100)); //四舍五入保留两位小数点
  262. $roya = sprintf("%.2f",$price *($royalties/100)); //四舍五入保留两位小数点
  263. $total_fee = bcmul($price,$num,2);
  264. $real_money = $price-$proportion-$roya;
  265. $data = [
  266. 'order_no'=>$order_no,
  267. 'mid'=>$this->uid,
  268. 'info_id'=>$id,
  269. 'num'=>$num,
  270. 'pro_info'=>json_encode($info,true),
  271. 'pay_price'=>$total_fee,
  272. 'service_fee'=>$service_fee,
  273. 'royalties'=>$royalties,
  274. 'to_account'=>$real_money,
  275. 'pay_type'=>$pay_type
  276. ];
  277. $id = Db::name('store_order_info_order')->insertGetId($data);
  278. $retrun_data['order_no'] = $order_no;
  279. $retrun_data['id'] = $id;
  280. $retrun_data['pay'] = '';
  281. Db::commit();
  282. // $body = 'top艺术购买二级市场藏品';
  283. // switch ($pay_type){
  284. // case 'wx':
  285. // $config = retrunWxConfig();
  286. // $total_fee = $total_fee * 100;
  287. // $config['notify_url'] = 'https://'.$_SERVER['SERVER_NAME'].'/api/Pay/SecondaryWxOrderNotify';
  288. // $app = Factory::payment($config);
  289. //
  290. // $post_data = [
  291. // 'body' => $body,
  292. // 'out_trade_no' => $order_no,
  293. // 'total_fee' => $total_fee,
  294. // 'attach'=>$this->uid, //自定义传值
  295. // ];
  296. // //trade_type SAPI--JSAPI支付(或小程序支付)、NATIVE--Native支付、APP--app支付,MWEB--H5支付
  297. // if ($from=='wx'){
  298. // $post_data['openid'] = $user['openid'];
  299. // $post_data['trade_type'] = 'JSAPI';
  300. // }elseif ($from=='h5'){
  301. // $post_data['trade_type'] = 'MWEB';
  302. // }
  303. // $result = $app->order->unify($post_data);
  304. //
  305. // if ($result['return_msg']=='OK'){
  306. // if ($result['result_code']=='FAIL'){
  307. // $com = false;
  308. // Db::rollback();
  309. // }else{
  310. // $order1 = $app->jssdk->bridgeConfig($result['prepay_id']);//执行二次签名返回参数
  311. // $retrun_data['order_no'] = $order_no;
  312. // $retrun_data['id'] = $id;
  313. // $retrun_data['pay'] = json_decode($order1,true);
  314. // Db::commit();
  315. // }
  316. // }else{
  317. // $com = false;
  318. // Db::rollback();
  319. // }
  320. // break;
  321. // case 'zfb':
  322. //
  323. // $zfb = new AliPay3();
  324. // $notify_url = $this->request->root(true).'/index.php/api/Pay/alipaySecondaryNotify';//回调地址
  325. // $callback_url = 'https://'.$_SERVER['SERVER_NAME'].'/web/h5/pages/shop/order';
  326. // $order = $zfb->aliPay($body,$total_fee,$order_no,$notify_url,$this->uid,$callback_url);
  327. // $retrun_data['order_no'] = $order_no;
  328. // $retrun_data['id'] = $id;
  329. // $retrun_data['pay'] = $order['pay_url'];
  330. // Db::commit();
  331. // break;
  332. // case 'sd':
  333. // $client = new Shande();
  334. // $notify_url = 'https://'.$_SERVER['SERVER_NAME'].'/index.php/api/Pay/shandeSecondaryNotify';//回调地址
  335. // $total_fee = $total_fee*100;
  336. // $lenth = strlen($total_fee);
  337. // $total_fee = get0number($lenth).$total_fee;
  338. // $result = $client->orderPay($order_no,$total_fee,$body,$notify_url,'https://'.$_SERVER['SERVER_NAME'].'/web/h5/pages/shop/order');
  339. // $retrun_data['order_no'] = $order_no;
  340. // $retrun_data['id'] = $id;
  341. // $retrun_data['pay'] = json_decode($result['data'],true);
  342. // Db::commit();
  343. // break;
  344. // default:
  345. // $retrun_data['order_no'] = $order_no;
  346. // $retrun_data['id'] = $id;
  347. // $retrun_data['pay'] = '';
  348. // Db::commit();
  349. // break;
  350. //
  351. // }
  352. }catch (\Exception $e){
  353. $com=false;
  354. Db::rollback();
  355. }
  356. if ($com){
  357. $this->success('成功',$retrun_data);
  358. }
  359. $this->error('失败,请稍后重试');
  360. }
  361. /**
  362. * @title 二级市场我的转售列表
  363. * @desc 二级市场我的转售列表
  364. * @author Gavin
  365. * @url /api/Secondary/my_sell_list
  366. * @method POST
  367. * @header name:Authorization require:1 desc:Token
  368. * @param name:page type:int : default:1 desc:页数
  369. * @param name:page_num type:int : default:20 desc:每页数
  370. *
  371. * @param name:keyword type:string require:0 default: desc:关键词
  372. * @param name:time_order type:string require:0 default: desc:时间排序(asc:正序desc:倒序)
  373. * @param name:price_order type:string require:0 default: desc:价格排序(asc:正序desc:倒序)
  374. *
  375. * @return name:name type:string require:0 default:0 desc:藏品名称
  376. * @return name:cover type:string require:0 default:0 desc:图片
  377. * @return name:member_name type:string require:0 default:0 desc:出售人名称
  378. * @return name:member_headimg type:string require:0 default:0 desc:出售人头像
  379. * @return name:resale_price type:string require:0 default:0 desc:出售价格
  380. */
  381. public function my_sell_list(){
  382. $keyword = input('keyword');
  383. $time_order = input('time_order');
  384. $price_order = input('price_order');
  385. $count = Db::name('store_order_info')
  386. ->where('mid',$this->uid)
  387. ->where('resale_status','neq',1)
  388. ->when($keyword,function ($query) use ($keyword){
  389. $query->whereLike('name','%'.$keyword.'%');
  390. })
  391. ->count();
  392. $list = Db::name('store_order_info')
  393. ->where('mid',$this->uid)
  394. ->where('resale_status','neq',1)
  395. ->when($keyword,function ($query) use ($keyword){
  396. $query->whereLike('name','%'.$keyword.'%');
  397. })
  398. ->when($price_order,function ($query) use ($price_order){
  399. $query->order('resale_price '.$price_order);
  400. })
  401. ->when($time_order,function ($query) use ($time_order){
  402. $query->order('resale_time '.$time_order);
  403. })
  404. ->order('resale_status asc')
  405. ->limit($this->off_set,$this->page_num)
  406. ->select();
  407. foreach ($list as &$v){
  408. $member = getMemberInfoHash($v['mid']); //获取用户信息
  409. $v['member_name'] = $member['name'];
  410. $v['member_headimg'] = $member['headimg'];
  411. $v['pro_info'] = json_decode($v['pro_info'],true);
  412. }
  413. $this->success('成功',compact('count','list'));
  414. }
  415. /**
  416. * @title 二级市场订单
  417. * @desc 二级市场订单
  418. * @author Gavin
  419. * @url /api/Secondary/secondaryOrderList
  420. * @method POST
  421. * @header name:Authorization require:1 desc:Token
  422. * @param name:page type:int : default:1 desc:页数
  423. * @param name:page_num type:int : default:20 desc:每页数
  424. *
  425. * @param name:status type:string require:0 default: desc:0:待支付1:已购买2:转售中3:已转售
  426. *
  427. * @return name:name type:string require:0 default:0 desc:藏品名称
  428. * @return name:cover type:string require:0 default:0 desc:图片
  429. * @return name:member_name type:string require:0 default:0 desc:出售人名称
  430. * @return name:member_headimg type:string require:0 default:0 desc:出售人头像
  431. * @return name:pay_price type:string require:0 default:0 desc:出售价格(已购买、待支付使用)
  432. * @return name:resale_price type:string require:0 default:0 desc:出售价格(转售中、已转售使用)
  433. */
  434. public function secondaryOrderList(){
  435. $status = input('status',0);
  436. switch ($status){
  437. case 0:case 1:
  438. $count = Db::name('store_order_info_order')
  439. ->where('mid',$this->uid)
  440. ->where('status',$status)
  441. ->count();
  442. $list = Db::name('store_order_info_order')
  443. ->where('mid',$this->uid)
  444. ->where('status',$status)
  445. ->limit($this->off_set,$this->page_num)
  446. ->select();
  447. //自动取消分钟数
  448. $cancel_time = getCancelTime();
  449. foreach ($list as &$v){
  450. $v['pro_info'] = json_decode($v['pro_info'],true);
  451. $member = getMemberInfoHash($v['mid']); //获取用户信息
  452. $v['member_name'] = $member['name'];
  453. $v['member_headimg'] = $member['headimg'];
  454. if ($v['status']==0){
  455. $v['cancel_time'] = date('Y-m-d H:i:s',strtotime($v['create_at'])+($cancel_time*60));
  456. }
  457. }
  458. break;
  459. case 2:case 3:
  460. $count = Db::name('store_order_info')
  461. ->where('mid',$this->uid)
  462. ->where('resale_status',$status)
  463. ->where('is_destruction',1)
  464. ->count();
  465. $list = Db::name('store_order_info')
  466. ->where('mid',$this->uid)
  467. ->where('resale_status',$status)
  468. ->where('is_destruction',1)
  469. ->limit($this->off_set,$this->page_num)
  470. ->select();
  471. foreach ($list as &$v){
  472. $v['pro_info'] = json_decode($v['pro_info'],true);
  473. $member = getMemberInfoHash($v['mid']); //获取用户信息
  474. $v['member_name'] = $member['name'];
  475. $v['member_headimg'] = $member['headimg'];
  476. }
  477. break;
  478. }
  479. $this->success('成功',compact('count','list'));
  480. }
  481. /**
  482. * @title 订单详情
  483. * @desc 订单详情
  484. * @author Gavin
  485. * @url /api/Secondary/orderDetail
  486. * @method POST
  487. * @header name:Authorization require:1 desc:Token
  488. * @param name:status type:string require:0 default: desc:0:待支付1:已购买2:转售中3:已转售
  489. * @param name:order_no type:string default:0 desc:订单号
  490. */
  491. public function orderDetail(){
  492. $order_no = input('order_no');
  493. $status = input('status',0);
  494. if (!$order_no) $this->error('参数错误');
  495. switch ($status){
  496. case 0:case 1:
  497. $order = Db::name('store_order_info_order')
  498. ->where('mid',$this->uid)
  499. ->where('status',$status)
  500. ->where('order_no',$order_no)
  501. ->find();
  502. //自动取消分钟数
  503. $cancel_time = getCancelTime();
  504. $order['pro_info'] = json_decode($order['pro_info'],true);
  505. $member = getMemberInfoHash($order['mid']); //获取用户信息
  506. $order['member_name'] = $member['name'];
  507. $order['member_headimg'] = $member['headimg'];
  508. if ($order['status']==0){
  509. $order['cancel_time'] = date('Y-m-d H:i:s',strtotime($order['create_at'])+($cancel_time*60));
  510. }
  511. break;
  512. case 2:case 3:
  513. $order = Db::name('store_order_info')
  514. ->where('mid',$this->uid)
  515. ->where('order_no',$order_no)
  516. ->where('resale_status',$status)
  517. ->where('is_destruction',1)
  518. ->find();
  519. $order['pro_info'] = json_decode($order['pro_info'],true);
  520. $member = getMemberInfoHash($order['mid']); //获取用户信息
  521. $v['member_name'] = $member['name'];
  522. $v['member_headimg'] = $member['headimg'];
  523. break;
  524. }
  525. $this->success('成功',$order);
  526. }
  527. /**
  528. * @title 取消订单
  529. * @desc 取消订单
  530. * @author Gavin
  531. * @url /api/Secondary/cancelOrder
  532. * @method POST
  533. * @header name:Authorization require:1 desc:Token
  534. * @param name:order_no type:string require:1 default:-- desc:订单号
  535. */
  536. public function cancelOrder(){
  537. $order_no = input('order_no');
  538. if (!$order_no) $this->error('参数错误');
  539. $order = Db::name('store_order_info_order')
  540. ->where('order_no',$order_no)
  541. ->where('mid',$this->uid)
  542. ->find();
  543. if (!$order) $this->error('订单不存在');
  544. if ($order['status']!=0) $this->error('订单已支付或已取消');
  545. $com = true;
  546. Db::startTrans();
  547. try {
  548. $up_data = [
  549. 'status'=>2,
  550. 'cancel_at'=>date('Y-m-d H:i:s'),
  551. 'cancel_state'=>2
  552. ];
  553. Db::name('store_order_info_order')->where('order_no',$order_no)->update($up_data);
  554. $cancle = [
  555. 'mid'=>$this->uid,
  556. 'order_id'=>$order['id']
  557. ];
  558. Db::name('store_order_info_cancel_log')->insert($cancle);
  559. $time = date('Y-m-d H:i:s',time()-(60*60));
  560. $count = Db::name('store_order_info_cancel_log')->where('mid',$this->uid)->where('create_at','gt',$time)->count();
  561. if ($count>2){
  562. $buy_time = date('Y-m-d H:i:s',time()+(24*60*60));
  563. Db::name('store_member')->where('id',$this->uid)->update(['buy_time'=>$buy_time]);
  564. }
  565. Db::commit();
  566. }catch (\Exception $e){
  567. $com=false;
  568. Db::rollback();
  569. }
  570. if ($com){
  571. setMemberInfoHash($this->uid);
  572. $this->success('取消成功');
  573. }else{
  574. $this->error('取消失败,请稍后重试');
  575. }
  576. }
  577. /**
  578. * @title 待支付订单支付
  579. * @desc 待支付订单支付
  580. * @author Gavin
  581. * @url /api/Secondary/payOrder
  582. * @method POST
  583. * @header name:Authorization require:1 desc:Token
  584. * @param name:order_no type:string require:1 default:-- desc:订单号
  585. * @param name:from type:string require:1 default:wx desc:wx:微信公众号h5:网页
  586. *
  587. * @return name:order_no type:int require:0 default:0 desc:订单号
  588. * @return name:pay type:string require:0 default:0 desc:支付信息
  589. */
  590. public function payOrder(){
  591. $this->checkSwitch(1);
  592. $user = getMemberInfoHash($this->uid); //获取用户信息
  593. $order_no = input('order_no'); //订单号
  594. $id = input('id');
  595. $from = input('from','wx');
  596. if (!$order_no) $this->error('参数错误');
  597. if (!$id) $this->error('参数错误');
  598. $order = Db::name('store_order_info_order')
  599. // ->where('order_no',$order_no)
  600. ->where('id',$id)
  601. ->where('mid',$this->uid)
  602. ->find();
  603. $order_no = $order['order_no'];
  604. $pay_type = $order['pay_type'];
  605. $this->checkSwitch(2,$pay_type);
  606. if (!$order) $this->error('订单不存在');
  607. if ($order['status']!=0) $this->error('订单已支付或已取消');
  608. $info = Db::name('store_order_info')->where('id',$order['info_id'])->find();
  609. if ($info['resale_status']!=2) $this->error('藏品已出售或已撤销出售');
  610. $com = true;
  611. Db::startTrans();
  612. try {
  613. //获取价格
  614. $total_fee = $order['pay_price'];
  615. $body = 'top艺术购买二级市场藏品';
  616. switch ($pay_type){
  617. case 'wx':
  618. $config = retrunWxConfig();
  619. $total_fee = $total_fee * 100;
  620. $config['notify_url'] = 'https://'.$_SERVER['SERVER_NAME'].'/api/Pay/SecondaryWxOrderNotify';
  621. $app = Factory::payment($config);
  622. $post_data = [
  623. 'body' => $body,
  624. 'out_trade_no' => $order_no,
  625. 'total_fee' => $total_fee,
  626. 'attach'=>$this->uid, //自定义传值
  627. ];
  628. //trade_type SAPI--JSAPI支付(或小程序支付)、NATIVE--Native支付、APP--app支付,MWEB--H5支付
  629. if ($from=='wx'){
  630. $post_data['openid'] = $user['openid'];
  631. $post_data['trade_type'] = 'JSAPI';
  632. }elseif ($from=='h5'){
  633. $post_data['trade_type'] = 'MWEB';
  634. }
  635. $result = $app->order->unify($post_data);
  636. if ($result['return_msg']=='OK'){
  637. if ($result['result_code']=='FAIL'){
  638. $com = false;
  639. Db::rollback();
  640. }else{
  641. $order1 = $app->jssdk->bridgeConfig($result['prepay_id']);//执行二次签名返回参数
  642. $retrun_data['order_no'] = $order_no;
  643. $retrun_data['id'] = $order['id'];
  644. $retrun_data['pay'] = json_decode($order1,true);
  645. Db::commit();
  646. }
  647. }else{
  648. $com = false;
  649. Db::rollback();
  650. }
  651. break;
  652. case 'zfb':
  653. $order_nos = get_order_sn();
  654. if (Db::name('store_order_info_order')
  655. ->where('order_no',$order_no)
  656. ->where('mid',$this->uid)
  657. ->update(['order_no'=>$order_nos])){
  658. $zfb = new AliPay3();
  659. $notify_url = $this->request->root(true).'/index.php/api/Pay/alipaySecondaryNotify';//回调地址
  660. $callback_url = 'https://'.$_SERVER['SERVER_NAME'].'/web/h5/pages/shop/order';
  661. $order_result = $zfb->aliPay($body,$total_fee,$order_nos,$notify_url,$this->uid,$callback_url);
  662. $retrun_data['order_no'] = $order_nos;
  663. $retrun_data['id'] = $order['id'];
  664. $retrun_data['pay'] = $order_result['pay_url'];
  665. Db::commit();
  666. }else{
  667. $com=false;
  668. Db::rollback();
  669. }
  670. break;
  671. case 'sd':
  672. $client = new Shande();
  673. $notify_url = 'https://'.$_SERVER['SERVER_NAME'].'/index.php/api/Pay/shandeSecondaryNotify';//回调地址
  674. $total_fee = $total_fee*100;
  675. $lenth = strlen($total_fee);
  676. $total_fee = get0number($lenth).$total_fee;
  677. $order_nos = get_order_sn();
  678. if (Db::name('store_order_info_order')
  679. ->where('order_no',$order_no)
  680. ->where('mid',$this->uid)
  681. ->update(['order_no'=>$order_nos])){
  682. $result = $client->orderPay($order_nos,$total_fee,$body,$notify_url,'https://'.$_SERVER['SERVER_NAME'].'/web/h5/pages/shop/order');
  683. $retrun_data['order_no'] = $order_nos;
  684. $retrun_data['id'] = $order['id'];
  685. $retrun_data['pay'] = json_decode($result['data'],true);
  686. Db::commit();
  687. }else{
  688. $com=false;
  689. Db::rollback();
  690. }
  691. break;
  692. case 'ylh5':
  693. $order_nos = get_order_sn();
  694. if (Db::name('store_order_info_order')->where('order_no',$order_no)->where('mid',$this->uid)->update(['order_no'=>$order_nos])){
  695. $callback_url = 'https://'.$_SERVER['SERVER_NAME'].'/pages/shop/order';
  696. $huiju = new HuijuH5Pay();
  697. $notify_url = 'https://'.$_SERVER['SERVER_NAME'].'/index.php/api/Pay/ylSecondaryNotify';//回调地址
  698. $result = $huiju->ylh5Pay($order_nos,$callback_url,$notify_url,$info['name'],$total_fee,$this->uid,2);
  699. if ($result['ra_Code']=='100'){
  700. $retrun_data['order_no'] = $order_nos;
  701. $retrun_data['id'] = $order['id'];
  702. $retrun_data['pay'] = $result['rc_Result'];
  703. Db::commit();
  704. }else{
  705. $com = false;
  706. Db::rollback();
  707. }
  708. }else{
  709. $com = false;
  710. Db::rollback();
  711. }
  712. break;
  713. case 'zfbh5':
  714. $order_nos = get_order_sn();
  715. if (Db::name('store_order_info_order')
  716. ->where('order_no',$order_no)
  717. ->where('mid',$this->uid)
  718. ->update(['order_no'=>$order_nos])){
  719. $callback_url = 'https://'.$_SERVER['SERVER_NAME'].'/pages/shop/order';
  720. $huiju = new HuijuH5Pay();
  721. $notify_url = 'https://'.$_SERVER['SERVER_NAME'].'/index.php/api/Pay/ylSecondaryNotify';//回调地址
  722. $result = $huiju->ylh5Pay($order_nos,$callback_url,$notify_url,$info['name'],$total_fee,$this->uid,1);
  723. if ($result['ra_Code']=='100'){
  724. $retrun_data['order_no'] = $order_nos;
  725. $retrun_data['id'] = $order['id'];
  726. $retrun_data['pay'] = $result['rc_Result'];
  727. Db::commit();
  728. }else{
  729. $com = false;
  730. Db::rollback();
  731. }
  732. }else{
  733. $com = false;
  734. Db::rollback();
  735. }
  736. break;
  737. }
  738. }catch (\Exception $e){
  739. $com=false;
  740. Db::rollback();
  741. }
  742. if ($com){
  743. $this->success('成功',$retrun_data);
  744. }
  745. $this->error('失败,请稍后重试');
  746. }
  747. /**
  748. * @param 判断开关
  749. * @param string $pay_type
  750. * @return bool
  751. * @throws \think\db\exception\DataNotFoundException
  752. * @throws \think\db\exception\ModelNotFoundException
  753. * @throws \think\exception\DbException
  754. */
  755. public function checkSwitch($type,$pay_type=''){
  756. if ($type==1){
  757. $v = getConfigValue('secondary_sell_switch');
  758. if (!$v){
  759. $this->error('功能暂时关闭');
  760. }else{
  761. $start = getConfigValue('shelves_start_time');
  762. $end = getConfigValue('shelves_end_time');
  763. if (time()>strtotime($start) && time()<=strtotime($end)){
  764. }else{
  765. $this->error('出售时间为:'.$start.'-'.$end);
  766. }
  767. }
  768. }elseif ($type==2){
  769. $nameArray = ['secondary_wx_switch','secondary_zfb_switch','secondary_sd_switch'];
  770. $values = getConfig($nameArray);
  771. if ($pay_type=='wx'){
  772. if (!$values['secondary_wx_switch']) $this->error('微信支付暂时关闭');
  773. }elseif ($pay_type=='zfb'){
  774. if (!$values['secondary_zfb_switch']) $this->error('支付宝支付暂时关闭');
  775. }elseif ($pay_type=='sd'){
  776. if (!$values['secondary_sd_switch']) $this->error('杉德支付暂时关闭');
  777. }
  778. }elseif ($type==3){
  779. $v = getConfigValue('secondary_buy_switch');
  780. if (!$v){
  781. $this->error('功能暂时关闭');
  782. }else{
  783. $start = getConfigValue('buy_start_time');
  784. $end = getConfigValue('buy_end_time');
  785. if (time()>strtotime($start) && time()<=strtotime($end)){
  786. }else{
  787. $this->error('时间时间为:'.$start.'-'.$end);
  788. }
  789. }
  790. }
  791. return true;
  792. }
  793. }