Order.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\library\AliPay;
  4. use think\cache\driver\Redis;
  5. use think\Db;
  6. use think\Exception;
  7. use EasyWeChat\Factory;
  8. use think\Session;
  9. use function Sodium\add;
  10. /**
  11. * @title 订单
  12. * @controller GoodsOrder
  13. * @group base
  14. */
  15. class Order extends Base
  16. {
  17. public function initialize()
  18. {
  19. parent::initialize();
  20. parent::check_login();
  21. }
  22. /**
  23. * @title 生成订单(立即购买 )
  24. * @desc 生成订单(立即购买 )
  25. * @author Gavin
  26. * @url /api/Order/createOrder
  27. * @method POST
  28. * @header name:Authorization require:1 desc:Token
  29. * @param name:id type:int require:1 default:-- desc:藏品id
  30. * @return name:order_id type:int require:0 default:0 desc:成功时返回订单id
  31. */
  32. public function createOrder()
  33. {
  34. $id = input('id');
  35. $num = input('num');
  36. $pay_type = input('pay_type','wx');
  37. if (!$id || !$num) $this->error('参数错误');
  38. $user = getMemberInfoHash($this->uid); //获取用户信息
  39. if ($user['is_auth']==0) $this->error('请实名认证后购买!');
  40. $coll_info = getCollectionInfoHash($id);
  41. if ($coll_info['is_deleted']==1 || $coll_info['status']==0) $this->error('藏品已下架');
  42. if ($user['vip']==2){
  43. $advance_minutes = getAdvanceMinutes(); //获取vip提前抢购分钟
  44. $sell_time = strtotime($coll_info['sell_time'])-($advance_minutes*60);
  45. // if ($sell_time>time()) $this->error('商品还未开始售卖!请耐心等待');
  46. }
  47. $snap_card = getMembercard($this->uid); //获取用户抢购卡数量
  48. if ($snap_card<1) $this->error('抢购卡不足,无法抢购');
  49. if ($coll_info['buy_count']<$num) $this->error('每人限购'.$coll_info['buy_count'].'个');
  50. $inventory = getCollectionInventory($id);
  51. if ($inventory<1) $this->error('已售罄');
  52. //先减掉库存
  53. loseCollectionInventory($id,$num);
  54. $com = true;
  55. Db::startTrans();
  56. try {
  57. $order_no = get_order_sn();
  58. $total_fee = bcmul($coll_info['price'],$num,2);
  59. $order_int =[
  60. 'mid' => $this->uid,
  61. 'c_id' => $id,
  62. 'inventory' => $coll_info['inventory'],
  63. 'order_no' => $order_no,
  64. 'num' => $num,
  65. 'pro_info' => json_encode($coll_info),
  66. 'pay_type' => $pay_type,
  67. 'pay_price' => $total_fee
  68. ];
  69. Db::name('store_order')->insert($order_int);
  70. // $retrun_data['order_no'] = $order_no;
  71. // $retrun_data['pay'] = 1;
  72. // Db::commit();
  73. $body = '象寻购买藏品';
  74. switch ($pay_type){
  75. case 'wx':
  76. $config = retrunWxConfig();
  77. $total_fee = $total_fee * 100;
  78. $config['notify_url'] = $this->request->root(true).'/api/Pay/WxOrderNotify';
  79. $app = Factory::payment($config);
  80. $order = $app->order->unify([
  81. 'body' =>$body,
  82. 'out_trade_no' => $order_no,
  83. 'total_fee' => $total_fee,
  84. 'trade_type' => "APP", // JSAPI--JSAPI支付(或小程序支付)、NATIVE--Native支付、APP--app支付,MWEB--H5支付
  85. ]);
  86. if ($order['return_msg']=='OK'){
  87. if ($order['result_code']=='FAIL'){
  88. $com=false;
  89. Db::rollback();
  90. }else{
  91. $order1 = $app->jssdk->bridgeConfig($order['prepay_id']);//执行二次签名返回参数
  92. Db::name('store_order')->where('order_no',$order_no)->update(['wx_order'=>$order1]);
  93. $retrun_data['order_no'] = $order_no;
  94. $retrun_data['pay'] = json_decode($order1,true);
  95. //减少数据库库存
  96. Db::name('store_collection')->where('id',$id)->setDec('now_inventory',$num);
  97. Db::commit();
  98. }
  99. }else{
  100. $com=false;
  101. Db::rollback();
  102. }
  103. break;
  104. case 'zfb':
  105. $zfb = new AliPay();
  106. $notify_url = $this->request->root(true).'/api/Pay/alipayOrderNotify';//回调地址
  107. $order = $zfb->aliPay($body, $total_fee, $order_no, $notify_url);//调用支付宝支付的方法
  108. $retrun_data['order_no'] = $order_no;
  109. $retrun_data['pay'] = $order;
  110. //减少数据库库存
  111. Db::name('store_collection')->where('id',$id)->setDec('now_inventory',$num);
  112. Db::commit();
  113. break;
  114. }
  115. }catch (\Exception $e){
  116. $com=false;
  117. Db::rollback();
  118. }
  119. if ($com){
  120. // $this->savetest($order_no);
  121. $this->success('成功',$retrun_data);
  122. }else{
  123. //加上库存
  124. addCollectionInventory($id,$num);
  125. $this->error('抢购失败,请稍后重试');
  126. }
  127. }
  128. /**
  129. * @title 我的订单-待支付、已购买
  130. * @desc 我的订单-待支付、已购买
  131. * @author Gavin
  132. * @url /api/Order/getMyOrderList
  133. * @method POST
  134. * @header name:Authorization require:1 desc:Token
  135. * @param name:page type:int : default:1 desc:页数
  136. * @param name:page_num type:int : default:20 desc:每页数
  137. *
  138. * @return name:order_no type:string default:-- desc:订单号
  139. * @return name:num type:int default:-- desc:数量
  140. * @return name:status type:int default:-- desc:类型0:待支付1:已支付
  141. * @return name:cancel_time type:string default:-- desc:待支付自动取消时间
  142. * @return name:pro_info@name type:string default:-- desc:藏品名称
  143. * @return name:pro_info@price type:string default:-- desc:藏品价格
  144. * @return name:pro_info@cover type:string default:-- desc:藏品图片
  145. */
  146. public function getMyOrderList(){
  147. $status = input('status',0);
  148. $where = [
  149. 'is_deleted'=>0,
  150. 'status'=>$status,
  151. 'mid'=>$this->uid
  152. ];
  153. $count = Db::name('store_order')->where($where)->count();
  154. $list = Db::name('store_order')
  155. ->where($where)
  156. ->field('id,c_id,order_no,num,pro_info,status,create_at')
  157. ->order('id desc')
  158. ->limit($this->off_set,$this->page_num)
  159. ->select();
  160. //自动取消分钟数
  161. $cancel_time = getCancelTime();
  162. foreach ($list as &$v){
  163. $v['pro_info'] = json_decode($v['pro_info'],true);
  164. if ($v['status']==0){
  165. $v['cancel_time'] = date('Y-m-d H:i:s',strtotime($v['create_at'])+($cancel_time*60));
  166. }
  167. }
  168. $this->success('成功',compact('count','list'));
  169. }
  170. /**
  171. * @title 我的订单-转赠
  172. * @desc 我的订单-转赠
  173. * @author Gavin
  174. * @url /api/Order/getMySendList
  175. * @method POST
  176. * @header name:Authorization require:1 desc:Token
  177. * @param name:page type:int : default:1 desc:页数
  178. * @param name:page_num type:int : default:20 desc:每页数
  179. *
  180. * @return name:order_no type:string default:-- desc:订单号
  181. * @return name:over_time type:string default:-- desc:转赠时间
  182. * @return name:status type:int default:-- desc:类型2:赠出3:获赠
  183. * @return name:pro_info@name type:string default:-- desc:藏品名称
  184. * @return name:pro_info@price type:string default:-- desc:藏品价格
  185. * @return name:pro_info@cover type:string default:-- desc:藏品图片
  186. */
  187. public function getMySendList(){
  188. $where = [
  189. 'mid'=>$this->uid,
  190. ];
  191. $count = Db::name('store_order_info')->where($where)->whereIn('status','2,3')->count();
  192. $list = Db::name('store_order_info')
  193. ->where($where)
  194. ->whereIn('status','2,3')
  195. ->field('id,order_no,pro_info,over_time,create_at,status')
  196. ->order('id desc')
  197. ->limit($this->off_set,$this->page_num)
  198. ->select();
  199. foreach ($list as &$v){
  200. $v['pro_info'] = json_decode($v['pro_info'],true);
  201. }
  202. $this->success('成功',compact('count','list'));
  203. }
  204. function savetest($order_no){
  205. $result['out_trade_no'] = $order_no;
  206. $order = Db::name('store_order')->where('order_no',$result['out_trade_no'])->find();
  207. if ($order['status']==0){
  208. Db::startTrans();
  209. try {
  210. Db::name('store_order')
  211. ->where('order_no',$result['out_trade_no'])
  212. ->update(['status'=>1,'pay_at'=>date('Y-m-d H:i:s'),'return_success_info'=>json_encode($result,true)]);
  213. $array = [];
  214. for ($i=0;$i<$order['num'];$i++){
  215. //获取排名
  216. $rank = getRanking($order['c_id'])+1;
  217. $tag = getTag($order['c_id'],$rank,$order['inventory']);
  218. saveRanking($order['c_id']);
  219. $company = '象寻数字科技(上海)有限公司';
  220. $company_hash = '';
  221. $company_hash_time = '';
  222. $collectors_hash = '';
  223. $date = [
  224. 'order_id'=>$order['id'],
  225. 'order_no'=>get_order_sn(),
  226. 'tag'=>$tag,
  227. 'mid'=>$order['mid'],
  228. 'pro_info'=>$order['pro_info'],
  229. 'company'=>$company,
  230. 'company_hash'=>$company_hash,
  231. 'company_hash_time'=>$company_hash_time,
  232. 'collectors_hash'=>$collectors_hash,
  233. 'collectors_hash_time'=>date('Y-m-d H:i:s')
  234. ];
  235. $array[] = $date;
  236. }
  237. Db::name('store_order_info')->insertAll($array);
  238. Db::commit();
  239. // return true;
  240. } catch (\Exception $e){
  241. Db::rollback();
  242. //return false;
  243. }
  244. }
  245. }
  246. }