Order.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. * 商城小程序公共API服务
  7. */
  8. namespace app\fastshop\controller\api\v3;
  9. use app\fastshop\controller\api\Base;
  10. use app\common\facade\WechatPay;
  11. use app\common\model\SystemUserAddress;
  12. use app\common\model\SystemMemberPayment;
  13. use app\common\model\SystemUserLevel;
  14. use app\common\model\SystemMemberBank;
  15. use app\fastshop\model\Sale;
  16. use app\fastshop\model\Order as AppOrder;
  17. use app\fastshop\model\OrderCache;
  18. use app\fastshop\model\Vip;
  19. use app\fastshop\model\Entrust;
  20. use app\fastshop\model\EntrustList;
  21. use app\fastshop\model\Item;
  22. use app\fastshop\model\Store;
  23. use app\fastshop\model\Config;
  24. use app\fastshop\model\Fare;
  25. use util\Util;
  26. class Order extends Base{
  27. public function initialize() {
  28. parent::initialize();
  29. $this->isUserAuth();
  30. }
  31. /**
  32. * 查看是否有订单
  33. * @param integer 读取ID
  34. * @return json
  35. */
  36. public function isOrder(){
  37. $param['id'] = $this->request->param('id/d',0);
  38. $param['sign'] = $this->request->param('sign');
  39. $rel = $this->apiSign($param);
  40. if($rel['code'] != 200){
  41. return enjson(204,'签名失败');
  42. }
  43. $where['id'] = $param['id'];
  44. $where['types'] = 1;
  45. $sale = Sale::where($where)->find();
  46. if (empty($sale)) {
  47. return json(['code'=>403,'msg'=>'活动已下架']);
  48. }
  49. if($sale->types == 0){
  50. return json(['code'=>403,'msg'=>'活动已下架']);
  51. }
  52. if($sale->sale_nums <= 0){
  53. return json(['code'=>403,'msg'=>'请下次抢购再来']);
  54. }
  55. if($sale->start_time >= time() || time() >= $sale->end_time){
  56. return json(['code'=>403,'msg'=>'抢购未开始']);
  57. }
  58. return json(['code'=>200,'msg'=>'成功']);
  59. }
  60. /**
  61. * 读取购物车产品列表
  62. */
  63. public function cartItem(){
  64. if(request()->isPost()){
  65. $param['cart'] = $this->request->param('cart',0);
  66. $param['sign'] = $this->request->param('sign');
  67. $rel = $this->apiSign($param);
  68. if($rel['code'] != 200){
  69. return enjson(204,'签名失败');
  70. }
  71. if(empty($param['cart'])){
  72. return json(['code'=>204,'msg'=>'购物车中没有宝贝','url'=>'/pages/market/index']);
  73. }
  74. $rel = Sale::where(['member_miniapp_id' => $this->miniapp_id,'id' => $param['cart']])->find();
  75. if(empty($rel)){
  76. return json(['code'=>403,'msg'=>'活动已下架']);
  77. }
  78. if($rel['sale_nums'] <= 0){
  79. return json(['code'=>403,'msg'=>'活动宝贝已抢完了']);
  80. }
  81. $rel['start_time'] = date('Y-m-d',$rel->start_time);
  82. $rel['end_time'] = date('Y-m-d',$rel->end_time);
  83. $rel['market_price'] = money($rel->market_price/100);
  84. $rel['sale_price'] = money($rel->sale_price/100);
  85. $rel['gift'] = AppOrder::gift(json_decode($rel->gift,true));
  86. //计算产品价格
  87. $price = [];
  88. foreach ($rel['gift'] as $key => $value) {
  89. $price[$key] = ['amount' => 0,'weight' =>$value['weight'],'num' =>1];
  90. }
  91. $item_price = [['amount' => $rel['sale_price'],'weight' =>$rel->item->weight,'num' =>1]];
  92. $price = array_merge($item_price,$price);
  93. $amount = Fare::realAmount($price,$this->miniapp_id);
  94. if(empty($rel)){
  95. return json(['code'=>204,'msg'=>'没有内容了','url'=>'/pages/market/index']);
  96. }else{
  97. return json(['data'=>['item'=>$rel,'amount' => $amount],'code'=>200,'msg'=>'成功']);
  98. }
  99. }
  100. }
  101. /**
  102. * 发起支付并通知支付接口
  103. * @param string $no
  104. * @return void
  105. */
  106. public function doPay(){
  107. if (request()->isPost()) {
  108. $param['address'] = $this->request->param('address/d',0);
  109. $param['ids'] = $this->request->param('ids/d',0);
  110. $param['ucode'] = $this->request->param('ucode');
  111. $param['buytype'] = $this->request->param('buytype','wepay');
  112. $param['sign'] = $this->request->param('sign');
  113. $rel = $this->apiSign($param);
  114. if($rel['code'] != 200){
  115. return enjson(403,'参数有误');
  116. }
  117. $validate = $this->validate($param, 'Cart.add_order');
  118. if (true !== $validate) {
  119. return enjson(403,$validate);
  120. }
  121. //判断提交商品(是否购买过)
  122. $sale = model('Sale')->where(['id' => $param['ids']])->find(); //价格是分
  123. if (empty($sale)) {
  124. return enjson(403,"活动不存在");
  125. }
  126. if ($sale->sale_nums <= 0) {
  127. return enjson(403,"商品库存不足");
  128. }
  129. if ($sale->types == 0) {
  130. return enjson(403,"活动已下架");
  131. }
  132. if ($sale->start_time >= time() || time() >= $sale->end_time) {
  133. return enjson(403,"活动暂未开始");
  134. }
  135. //读取配置
  136. $config = Config::where(['member_miniapp_id' => $this->miniapp_id])->find();
  137. if($param['buytype'] == 'point' && $config->payment_type == 0){
  138. return json(['code'=>403,'msg'=>"未开通余额支付功能"]);
  139. }
  140. //增加下单次数限制
  141. if($config->num_referee_people > 0){
  142. //下单数量
  143. $entrust_list = EntrustList::where(['member_miniapp_id' => $this->miniapp_id,'user_id' => $this->user->id,'is_rebate' => 0])->field('user_id,order_no,is_rebate')->select();
  144. $entrust_num = count(Util::unique_array($entrust_list->toArray()));
  145. //统计推荐人数
  146. $peple_num = SystemUserLevel::where(['parent_id' => $this->user->id,'level'=>1])->count();
  147. if($peple_num <= 0){
  148. return enjson(403,"最少邀请['.$config->num_referee_people.']朋友才允许抢购");
  149. }else{
  150. $num = intval($peple_num/$config->num_referee_people);
  151. if($num){
  152. if($entrust_num >= $num){
  153. $people_num = $config->num_referee_people*($num+1)-$peple_num;
  154. return enjson(403,"还有[{$entrust_num}]单未成交,再邀请[".$people_num."]个朋友,就可以增加一单");
  155. }
  156. }else{
  157. $people_num = $config->num_referee_people-$peple_num;
  158. return enjson(403,"已邀[{$peple_num}]人,再邀请[".$people_num."]个朋友,就可以抢购");
  159. }
  160. }
  161. }
  162. //判断下单的是否会员
  163. $vip = Vip::where(['user_id' => $this->user->id,'state' => 1])->find();
  164. if ($config['shop_types'] == 0) {
  165. if ($sale->is_vip && empty($vip)) {
  166. return enjson(302,"本活动仅会员可参与,请先开通会员",['url' =>'/pages/user/vip']);
  167. }
  168. } else {
  169. if (empty($vip)) {
  170. return enjson(302,"仅会员可参与,请先开通会员",['url' =>'/pages/user/vip']);
  171. }
  172. }
  173. //判断订单
  174. $condition['member_miniapp_id'] = $this->miniapp_id;
  175. $condition['user_id'] = $this->user->id;
  176. $condition['is_del'] = 0;
  177. $condition['paid_at'] = 1;
  178. if($config->old_users > 0){
  179. $order_num = AppOrder::where($condition)->count();
  180. if ($sale->is_newuser == 1) {
  181. if ($order_num >= $config->old_users) {
  182. return enjson(403,"活动只允许新用户抢购");
  183. }
  184. }
  185. }
  186. if($config->day_ordernum > 0){
  187. $today = date('Y-m-d');
  188. $today_num = AppOrder::where($condition)->whereBetweenTime('order_starttime',$today)->count();
  189. if ($today_num >= $config->day_ordernum) {
  190. return enjson(403,"今天已没有抢购机会,请明天再来");
  191. }
  192. }
  193. if($config->sale_ordernum > 0){
  194. $condition['sale_id'] = $sale->id;
  195. $orderNum = AppOrder::where($condition)->count();
  196. if ($orderNum >= $config->sale_ordernum) {
  197. return enjson(403,"每个活动最多只允许抢购{$config->sale_ordernum}次");
  198. }
  199. }
  200. //读取发货地址
  201. $address = SystemUserAddress::where(['user_id'=>$this->user->id,'id' => $this->request->param('address/d',0)])->find();
  202. if (empty($address)) {
  203. return enjson(403,'请重新选择收货地址');
  204. }
  205. //支付接口
  206. $payment = SystemMemberPayment::where(['apiname' => 'wepay','member_miniapp_id'=>$this->miniapp_id])->find();
  207. if (empty($payment)) {
  208. return enjson(403,'未开通微信支付功能');
  209. }
  210. //计算商品价格(从上面的分换算成 元)
  211. $item = Item::field('weight,img,name')->where(['id' => $sale->item_id])->find();
  212. //计算产品价格(包含主商品和赠品的重量计算)
  213. $price = [];
  214. $gift = AppOrder::gift(json_decode($sale->gift,true));
  215. foreach ($gift as $key => $value) {
  216. $price[$key] = ['amount' => 0,'weight' =>$value['weight'],'num' =>1];
  217. }
  218. $item_price = [['amount' => money($sale->sale_price/100),'weight' =>$item->weight,'num' =>1]];
  219. $price = array_merge($item_price,$price);
  220. $amount = Fare::realAmount($price,$this->miniapp_id);
  221. if($param['buytype'] == 'point'){
  222. if($config->payment_type == 0){
  223. return enjson(403,'未开通余额支付功能');
  224. }
  225. $point_fee = money($amount['order_amount']*($config->payment_point/100)); //积分付款
  226. if ($point_fee <= 0) {
  227. $param['buytype'] = 'wepay'; //如果积分付款为零0转换为正常的微信全额支付
  228. $order_amount = money($amount['order_amount']);
  229. }else{
  230. $order_amount = money($amount['order_amount'] - $point_fee);
  231. $order_amount = $order_amount <= 0 ? 1 : $order_amount; //如果是100%积分设置最低付款金额
  232. //判断积分够不够
  233. $rel = model('Bank')->isPay($this->user->id,$point_fee,$config->payment_type);
  234. if(!$rel){
  235. return enjson(403,'余额不足,请选择其它支付渠道');
  236. }
  237. }
  238. }else{
  239. $order_amount = $amount['order_amount'];
  240. }
  241. $order_amount = $order_amount <= 0 ? 1 :$order_amount;
  242. //判断云收银台
  243. if($config->is_pay_types == 1 && $config->goodpay_tax > 0){
  244. $goodpay_tax = $order_amount*$config->goodpay_tax/100;
  245. $bank_rel = SystemMemberBank::moneyJudge($this->miniapp->member_id,$goodpay_tax);
  246. if($bank_rel){
  247. return enjson(403,'账户余额不足,暂停交易');
  248. }
  249. }
  250. $order_no = $this->user->invite_code.order_no(); //生成的订单号
  251. //计算最后付款金额
  252. $order['order_amount'] = $order_amount; //元
  253. $order['real_amount'] = $amount['real_amount']; //元
  254. $order['real_freight'] = $amount['real_freight']; //元
  255. $order['payment_id'] = $payment->id; //支付ID
  256. $order['express_name'] = $address->name;
  257. $order['express_phone'] = $address->telphone;
  258. $order['express_address'] = $address->address;
  259. $order['sale_id'] = $sale->id;
  260. $order['is_fusion'] = $sale->is_fusion;
  261. $order['member_miniapp_id'] = $this->miniapp_id;
  262. $order['user_id'] = $this->user->id;
  263. $order['order_no'] = $order_no;
  264. $order['paid_at'] = 0;
  265. $order['order_starttime'] = time();
  266. $rel = AppOrder::insert($order);
  267. if (empty($rel)) {
  268. return enjson(403,'创建订单失败');
  269. }
  270. //保存订单产品到缓存数据表
  271. $item_data['order_no'] = $order_no;
  272. $item_data['item_id'] = $sale->item_id;
  273. $item_data['sale_price'] = $sale->sale_price;//分
  274. $item_data['name'] = $sale->title;
  275. $item_data['img'] = $item->img;
  276. $item_data['gift'] = $sale->gift;
  277. OrderCache::insert($item_data);
  278. //支付方式
  279. if($config->is_pay_types == 1){
  280. $pay_coinfig = json_decode($payment->config);
  281. //云收银台
  282. $ispay = [
  283. 'name' => $sale->title,
  284. 'mchid' => json_decode($payment->config)->mch_id,
  285. 'order_no' => $order_no,
  286. 'note' => $this->miniapp_id,
  287. 'total_fee' => $order_amount*100,
  288. 'notify_url' => $param['buytype'] == 'wepay'? api(3,'fastshop/goodpay/sale',$this->miniapp_id):api(3,'fastshop/goodpay/salePoint',$this->miniapp_id),
  289. 'publickey' => uuid(1)
  290. ];
  291. $paydata = $this->makeSign($ispay,$pay_coinfig->key);
  292. }else{
  293. //去请求微信支付接口
  294. $payparm = [
  295. 'name' => $sale->title,
  296. 'openid' => $this->user->miniapp_uid,
  297. 'miniapp_id' => $this->miniapp_id,
  298. 'order_no' => $order_no,
  299. 'total_fee' => $order_amount*100,
  300. 'notify_url' => $param['buytype'] == 'wepay'? api(3,'fastshop/notify/sale',$this->miniapp_id) : api(3,'fastshop/notify/salePoint',$this->miniapp_id),
  301. ];
  302. $ispay = WechatPay::orderPay($payparm);
  303. if($ispay['code'] == 0){
  304. return enjson(403,$ispay['msg']);
  305. }
  306. $paydata = $ispay['data'];
  307. }
  308. return enjson(200,'成功',['type' =>$config->is_pay_types,'order' =>$paydata]);
  309. }
  310. }
  311. /**
  312. * 我的寄卖
  313. * @return void
  314. */
  315. public function gift(){
  316. $rule = [
  317. 'types' => $this->request->param('types',0),
  318. 'page' => $this->request->param('page',0),
  319. 'sign' => $this->request->param('sign')
  320. ];
  321. $rel = $this->apiSign($rule);
  322. if($rel['code'] != 200){
  323. return enjson(403,'签名失败');
  324. }
  325. $condition['user_id'] = $this->user->id;
  326. $condition['is_del'] = 0;
  327. switch ($rule['types']) {
  328. case 1:
  329. $condition['paid_at'] = 1;
  330. $condition['is_entrust'] = 1;
  331. $condition['express_status'] = 0;
  332. break;
  333. case 2:
  334. $condition['paid_at'] = 1;
  335. $condition['express_status'] = 1;
  336. $condition['is_entrust'] = 1;
  337. break;
  338. default:
  339. $condition['paid_at'] = 1;
  340. $condition['is_entrust'] = 0;
  341. break;
  342. }
  343. $order = AppOrder::where($condition)->order('id desc')->paginate(10);
  344. if(empty($order)) {
  345. return enjson(204,'没有订单');
  346. }
  347. $data = [];
  348. foreach ($order as $key => $value) {
  349. $data[$key] = $value;
  350. $data[$key]['status_text'] = AppOrder::statuText($value->status,$value->paid_at,$value->is_entrust,$value->express_status);
  351. $data[$key]['order_starttime'] = empty($value->order_starttime) ? '' : date('Y-m-d H:i:s',$value->order_starttime);
  352. $data[$key]['order_endtime'] = empty($value->order_endtime) ? '' : date('Y-m-d H:i',$value->order_endtime);
  353. $data[$key]['item'] = $value->orderItem;
  354. $gift_data = [];
  355. $gift = array_column(json_decode($value->orderItem->gift,true),'item_id');
  356. foreach ($gift as $k => $item_id) {
  357. $items = Item::field('id,name,img')->where(['id' => $item_id])->find();
  358. $gift_data[$k]['name'] = $items->name;
  359. $gift_data[$k]['img'] = $items->img."?x-oss-process = style/auto";
  360. }
  361. $data[$key]['gift']= $gift_data;
  362. }
  363. return enjson(200,'成功',$data);
  364. }
  365. /**
  366. * 读取购物车预览
  367. */
  368. public function review(){
  369. $param['order_no'] = $this->request->param('order_no');
  370. $param['sign'] = $this->request->param('sign');
  371. $rel = $this->apiSign($param);
  372. if($rel['code'] != 200){
  373. return enjson(204,'签名失败');
  374. }
  375. $condition['order_no'] = $param['order_no'];
  376. $condition['user_id'] = $this->user->id;
  377. $order = AppOrder::where($condition)->find();
  378. if(empty($order)){
  379. return enjson(204);
  380. }
  381. $data = $order;
  382. $data['status_text'] = AppOrder::statuText($order->status,$order->paid_at,$order->is_entrust,$order->express_status);
  383. $data['order_starttime'] = empty($order->order_starttime) ? '' : date('Y-m-d H:i:s',$order->order_starttime);
  384. $data['order_endtime'] = empty($order->order_endtime) ? '' : date('Y-m-d H:i',$order->order_endtime);
  385. $data['item'] = $order->orderItem;
  386. $gift = array_column(json_decode($order->orderItem->gift,true),'item_id');
  387. $entrust_state = json_decode($order->orderItem->entrust);
  388. $gift_data = [];
  389. foreach ($gift as $k => $item_id) {
  390. $items = Item::field('id,name,img')->where(['id' => $item_id])->find();
  391. $gift_data[$k]['id'] = $items->id;
  392. $gift_data[$k]['name'] = $items->name;
  393. $gift_data[$k]['img'] = $items->img."?x-oss-process = style/auto";
  394. $gift_data[$k]['entrust_state'] = empty($entrust_state[$k]) ? false : $entrust_state[$k];
  395. }
  396. $data['gift'] = $gift_data;
  397. return enjson(200,'成功',$data);
  398. }
  399. /**
  400. * 签收订单
  401. */
  402. public function signorder(){
  403. $param['order_no'] = $this->request->param('order_no');
  404. $param['sign'] = $this->request->param('sign');
  405. $rel = $this->apiSign($param);
  406. if($rel['code'] != 200){
  407. return enjson(204,'签名失败');
  408. }
  409. $condition['order_no'] = $param['order_no'];
  410. $condition['user_id'] = $this->user->id;
  411. $condition['is_del'] = 0;
  412. $condition['paid_at'] = 1;
  413. $condition['express_status'] = 1;
  414. $rel = AppOrder::where($condition)->data(['status' => 1])->update();
  415. if(empty($rel)){
  416. return json(['code'=>403,'msg'=>'未找到当前订单']);
  417. }
  418. return json(['code'=>200,'msg'=>'订单签收成功']);
  419. }
  420. /**
  421. * 确定是否寄卖商品
  422. */
  423. public function giftAction(){
  424. $param['order_no'] = $this->request->param('order_no');
  425. $param['service'] = $this->request->param('service',0);
  426. $param['item_checkbox'] = $this->request->param('item_checkbox',0);
  427. $param['gift'] = $this->request->param('gift');
  428. $param['sign'] = $this->request->param('sign');
  429. $rel = $this->apiSign($param);
  430. if($rel['code'] != 200){
  431. return enjson(204,'签名失败');
  432. }
  433. if (!$param['service']) {
  434. return enjson(403,'必须接受用户服务协议');
  435. }
  436. $item_checkbox = $param['item_checkbox'];
  437. $is_gift = (array)ids(json_decode($param['gift']),true);
  438. if(empty($is_gift)){
  439. return enjson(403,'委托商品选择异常');
  440. }
  441. $store = Store::where(['uid' => $this->user->id])->find();
  442. if(empty($store)){
  443. return json(['code'=>302,'msg'=>'未开通小店,请先开通小店','url'=>'/pages/store/index']);
  444. }
  445. //确认提货
  446. $condition['order_no'] = $param['order_no'];
  447. $condition['user_id'] = $this->user->id;
  448. $condition['paid_at'] = 1;
  449. $condition['is_del'] = 0;
  450. $condition['is_entrust'] = 0;
  451. $rel = AppOrder::where($condition)->find();
  452. if (empty($rel)) {
  453. return json(['code'=>403,'msg'=>'订单未付款']);
  454. }
  455. //读取配置
  456. $config = Config::where(['member_miniapp_id' => $this->miniapp_id])->field('lock_sale_day,amountlimit')->find();
  457. //读取当前用户的收益(是否允许委托)
  458. if($config->amountlimit){
  459. $bank = model('Bank')->where(['user_id' => $this->user->id])->field('profit')->find();
  460. if(!empty($bank)){
  461. $profit = intval($bank->profit/100);
  462. if($profit >= $config->amountlimit){
  463. $amountlimit = false;
  464. if($rel->is_fusion && $item_checkbox){
  465. $amountlimit = true;
  466. }else{
  467. if(in_array(1,$is_gift)){
  468. $amountlimit = true;
  469. }
  470. }
  471. if($amountlimit){
  472. return json(['code'=>403,'msg'=>'收益已达上限'.$config->amountlimit.'禁止委托,本单必须提货.']);
  473. }else{
  474. model('Bank')->isProfit($this->user->id,0); //清空收益
  475. }
  476. }
  477. }
  478. }
  479. $is_under = $config->lock_sale_day > 0 ? 1 : 0; //是否立即委托销售
  480. $fusion_state = 0;
  481. //读取委托商品(并计算寄卖数量)
  482. $info = OrderCache::where(['order_no' => $rel->order_no])->find();
  483. $gift = AppOrder::gift(json_decode($info['gift'],true)); //分已转化成元
  484. $entrust_data = [];
  485. $entrust_state = [];
  486. if($rel->is_fusion){
  487. foreach ($gift as $key => $value) {
  488. $entrust_state[$key] = false;
  489. }
  490. if($item_checkbox){
  491. //增加通一个商品的委托库存
  492. $entrust = Entrust::where(['member_miniapp_id'=>$this->miniapp_id,'item_id' => $rel->sale->item_id])->find();
  493. if (empty($entrust)) {
  494. $data['item_id'] = $rel->sale->item_id;
  495. $data['gite_count'] = 1;
  496. $data['entrust_price'] = $rel->sale->sale_price; //分
  497. $data['member_miniapp_id'] = $this->miniapp_id;
  498. $entrust_id = Entrust::insertGetId($data);
  499. } else {
  500. Entrust::where(['member_miniapp_id'=>$this->miniapp_id,'item_id' => $rel->sale->item_id])->setInc('gite_count',1);
  501. $entrust_id = $entrust->id;
  502. }
  503. $fusion_state = 1;
  504. //增加托买数据
  505. $entrust_data[] = [
  506. 'member_miniapp_id' => $this->miniapp_id,
  507. 'order_no' => $rel->order_no,
  508. 'entrust_id' => $entrust_id,
  509. 'user_id' => $this->user->id,
  510. 'order_amount' => $rel->order_amount*100, //元=>分
  511. 'entrust_price' => $rel->sale->sale_price, //分
  512. 'item_id' => $rel->sale->item_id,
  513. 'is_under' => $is_under,
  514. 'create_time' => time(),
  515. 'is_fusion' => 1
  516. ];
  517. }
  518. }else{
  519. foreach ($gift as $key => $value) {
  520. if ($is_gift[$key] == 1) { //委托
  521. //增加通一个商品的委托库存
  522. $entrust = Entrust::where(['member_miniapp_id'=>$this->miniapp_id,'item_id' => $value['item_id']])->find();
  523. if (empty($entrust)) {
  524. $data['item_id'] = $value['item_id'];
  525. $data['gite_count'] = 1;
  526. $data['entrust_price'] = $value['sale_price']*100;
  527. $data['member_miniapp_id'] = $this->miniapp_id;
  528. $entrust_id = Entrust::insertGetId($data);
  529. } else {
  530. Entrust::where(['member_miniapp_id'=>$this->miniapp_id,'item_id' => $value['item_id']])->setInc('gite_count',1);
  531. $entrust_id = $entrust->id;
  532. }
  533. //增加托买数据
  534. $entrust_data[$key]['member_miniapp_id'] = $this->miniapp_id;
  535. $entrust_data[$key]['order_no'] = $rel->order_no;
  536. $entrust_data[$key]['entrust_id'] = $entrust_id;
  537. $entrust_data[$key]['user_id'] = $this->user->id;
  538. $entrust_data[$key]['order_amount'] = $rel->order_amount*100; //分
  539. $entrust_data[$key]['entrust_price'] = $value['sale_price']*100; //分
  540. $entrust_data[$key]['item_id'] = $value['item_id'];
  541. $entrust_data[$key]['is_under'] = $is_under;
  542. $entrust_data[$key]['create_time'] = time();
  543. $entrust_data[$key]['is_fusion'] = 0;
  544. $entrust_state[$key] = true;
  545. } else {
  546. //提货
  547. $entrust_state[$key] = false;
  548. }
  549. }
  550. }
  551. if (!empty($entrust_data)) {
  552. EntrustList::insertAll($entrust_data);
  553. }
  554. OrderCache::where(['order_no' => $rel->order_no])->update(['entrust' => json_encode($entrust_state),'fusion_state' => $fusion_state]);
  555. $rel->is_entrust = 1; //修改已确认订单状态
  556. $rel->save();
  557. return json(['code'=>200,'msg'=>'订单已确认,在小店中可管理寄卖的产品']);
  558. }
  559. }