ShopOrder.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <?php
  2. namespace app\api\controller\v1;
  3. use AlibabaCloud\SDK\Cloudauth\V20190307\Models\CompareFacesResponseBody\data;
  4. use app\common\model\BoxHandleModel;
  5. use app\common\model\ShopGoodsModel;
  6. use app\common\model\ShopOrderModel;
  7. use app\common\model\UserAddressModel as UserAddress;
  8. use app\Request;
  9. use think\Exception;
  10. use think\facade\Db;
  11. use think\Response;
  12. class ShopOrder
  13. {
  14. /**
  15. * 订单列表
  16. */
  17. public function orderList (Request $request)
  18. {
  19. try {
  20. return api_succ_return(['msg'=>'获取成功', 'data'=>ShopOrderModel::list($request->only(['status', 'order_type']), $request->only(['page', 'limit']), $request->uid)]);
  21. } catch (\Exception $e) {
  22. return api_error_return('获取失败' . $e->getMessage());
  23. }
  24. }
  25. /**
  26. * 订单详情
  27. */
  28. public function orderDetail (Request $request): Response
  29. {
  30. try {
  31. return api_succ_return(['msg'=>'获取成功', 'data'=>ShopOrderModel::details($request->only(['order_sn']), $request->uid)]);
  32. } catch (\Exception $e) {
  33. return api_error_return('获取失败' . $e->getMessage());
  34. }
  35. }
  36. /**
  37. * 下单商品详情
  38. */
  39. public function buyDetail (Request $request)
  40. {
  41. $buy_goods = $request->post('buy_goods');
  42. $address = $request->post('address_id');
  43. if (empty($buy_goods)) {
  44. return api_error_return('参数错误11');
  45. }
  46. try {
  47. $buy_goods = json_decode(html_entity_decode($buy_goods), true);
  48. if (empty($buy_goods)) {
  49. return api_error_return('参数错误22');
  50. }
  51. // $hasMoney = false;
  52. // $hasScore = false;
  53. // $hasLockScore = false;
  54. // $hasMachine = false;
  55. // $hasXz = false;
  56. // $order_type = 1;
  57. foreach ($buy_goods as $k=>$v) {
  58. $goodsInfo = ShopGoodsModel::where(['goods_sn' => $v['goods_sn']])->field('goods_type')->find();
  59. if (!$goodsInfo) {
  60. return api_error_return('下单失败,请返回刷新列表1');
  61. }
  62. }
  63. // if ($goodsInfo['goods_type'] == 1){$hasMoney=true;}
  64. // if ($goodsInfo['goods_type'] == 2){$hasScore=true;$order_type=2;}
  65. // if ($goodsInfo['goods_type'] == 3){$hasMachine=true;$order_type=3;}
  66. // if ($goodsInfo['goods_type'] == 4){$hasLockScore=true;$order_type=4;}
  67. // if ($goodsInfo['goods_type'] == 5){$hasXz=true;$order_type=5;}
  68. // }
  69. // if ($hasMoney && ($hasScore || $hasLockScore || $hasMachine || $hasXz)){
  70. // return api_error_return('自营商品不能和其他商品下单');
  71. // }
  72. //
  73. // if ($hasScore && ($hasMoney || $hasLockScore || $hasMachine || $hasXz)){
  74. // return api_error_return('积分商品不能和其他商品下单');
  75. // }
  76. // if ($hasLockScore && ($hasScore || $hasMoney || $hasMachine || $hasXz)){
  77. // return api_error_return('返利积分不能和其他商品下单');
  78. // }
  79. // if ($hasMachine && ($hasScore || $hasLockScore || $hasMoney || $hasXz)){
  80. // return api_error_return('兑换券商品不能和其他商品下单');
  81. // }
  82. //
  83. // if ($hasXz && ($hasScore || $hasLockScore || $hasMoney || $hasMachine)){
  84. // return api_error_return('星钻商品不能和其他商品下单');
  85. // }
  86. if (empty($address)) {
  87. //用户默认收货地址
  88. $address = UserAddress::where(['uid' => $request->uid, 'is_default' => 1])->field('id,province,city,county,name,mobile,remark,is_default,mergename')->findOrEmpty()->toArray();
  89. } else {
  90. $address = UserAddress::where(['uid' => $request->uid, 'id' => $address])->field('id,province,city,county,name,mobile,remark,is_default,mergename')->findOrEmpty()->toArray();
  91. }
  92. // if (!$address){
  93. // return api_error_return('地址获取失败');
  94. // }
  95. $data = ShopOrderModel::buyDetails($buy_goods, $address);
  96. if (empty($data)) {
  97. return api_error_return('获取失败');
  98. }
  99. $cur_xzmoney = Db::name('system_config')->where('name', 'xz_cur_money')->where('group', 'xzconfig')->value('value');
  100. foreach ($data['details'] as $key=>$val){
  101. foreach ($val['goods'] as $k1=>$v1){
  102. $goods_name = $v1['goods_name'];
  103. $goods_name = str_replace(' ','',$goods_name);
  104. if (strlen($goods_name) > 20){
  105. $goods_name = substr($goods_name, 0, 19);
  106. }
  107. // $data['details'][$key]['goods'][$k1]['goods_name'] = mb_convert_encoding($goods_name, 'UTF-8', 'UTF-8');
  108. // $data['details'][$key]['goods'][$k1]['goods_name'] = '云德商品购买';
  109. if ($v1['goods_type'] == 5){
  110. $data['details'][$key]['goods'][$k1]['price'] = getXzPirceWithPrice($cur_xzmoney, $data['details'][$key]['goods'][$k1]['price']);
  111. $data['details'][$key]['goods'][$k1]['total_fee'] = getXzPirceWithPrice($cur_xzmoney, $data['details'][$key]['goods'][$k1]['total_fee']);
  112. }
  113. }
  114. }
  115. // if ($order_type == 5){
  116. //
  117. // foreach ($data['details'] as $key=>$val){
  118. //// ['total_price'] = getXzPirceWithPrice($cur_xzmoney, $data['total_price']);
  119. // $data['details'][$key]['total_price']= getXzPirceWithPrice($cur_xzmoney, $val['total_price']);
  120. // $data['details'][$key]['payment']= getXzPirceWithPrice($cur_xzmoney, $val['payment']);
  121. //
  122. // }
  123. //
  124. // $data['total']['count_payment'] = getXzPirceWithPrice($cur_xzmoney, $data['total']['count_payment']);
  125. //
  126. // }
  127. return api_succ_return(['msg'=>'获取成功', 'data'=>['address' => (object)$address, 'buyDetail' => $data['details'], 'total' => $data['total']]]);
  128. } catch (\Exception $e) {
  129. return api_error_return($e->getMessage());
  130. }
  131. }
  132. /**
  133. * 创建订单
  134. */
  135. public function createOrder(Request $request)
  136. {
  137. // 这是测试 上线要干掉
  138. // return api_error_return('暂未开放');
  139. // $curandroid = env('app.CUR_ANDROID_VERSION', '');
  140. // $curios = env('app.CUR_IOS_VERSION', '');;
  141. //
  142. // $curAndroidVersion = str_replace('.', '', $curandroid);
  143. // $curAndroidVersion = dispRepair($curAndroidVersion, 4, '0', '0');
  144. // $curIosVersion = str_replace('.', '', $curios);
  145. // $curIosVersion = dispRepair($curIosVersion, 4, '0', '0');
  146. // $app_sources = $request->param('app_sources');
  147. // $app_version = $request->param('app_version');
  148. // $app_version = str_replace('.', '', $app_version);
  149. // $app_version = dispRepair($app_version, 4, '0', '0');
  150. //
  151. // if ($app_sources == 'ios'){
  152. // if ($curIosVersion > $app_version){
  153. // return api_error_return('app版本号过低,请更新');
  154. // }
  155. // }
  156. // if ($app_sources == 'android'){
  157. // if ($curAndroidVersion > $app_version){
  158. // return api_error_return('app版本号过低,请更新');
  159. // }
  160. // }
  161. //
  162. // if (empty($app_sources) || empty($app_version)){
  163. // if ($curAndroidVersion > $app_version){
  164. // return api_error_return('app版本号过低,请更新');
  165. // }
  166. // }
  167. $address_id = $request->post('address', '');
  168. $pay_type = $request->post('pay_type', 0);
  169. // 后面加了收银台 pay_type 去掉
  170. if ($pay_type != 3){
  171. $pay_type = 99;
  172. }
  173. $remark = $request->post('order_remark', '');
  174. $buy_goods = json_decode(html_entity_decode($request->post('buy_goods', '')), true);
  175. $hasMoney = false;
  176. $hasScore = false;
  177. $hasLockScore = false;
  178. $hasMachine = false;
  179. $hasXz = false;
  180. $order_type = 1; // 1 买商品送积分 2积分兑换 3任务包兑换券 4提货券 5 星钻兑换
  181. foreach ($buy_goods as $k=>$v){
  182. if ($v['num'] < 1){
  183. return api_error_return("哈哈,你不能买负数哇");
  184. }
  185. $goodsInfo = ShopGoodsModel::where('goods_sn', $v['goods_sn'])->field('goods_type,restrictions_num')->find();
  186. if (!$goodsInfo){
  187. return api_error_return('下单失败,请返回刷新列表');
  188. }
  189. if ($goodsInfo['goods_type'] == 2){
  190. return api_error_return('福袋商品不能购买');
  191. }
  192. if ($goodsInfo['restrictions_num']>0){
  193. $count = Db::name('shop_order_goods')
  194. ->alias('og')
  195. ->leftJoin('shop_order o', 'o.order_id = og.order_id')
  196. ->where('o.status', 'in', [1,2,4])
  197. ->where('og.uid', $request->uid)
  198. ->count('og.og_id');
  199. if ($count > $goodsInfo['restrictions_num']){
  200. return api_error_return('超过限购');
  201. }
  202. if ($v['num'] + $count > $goodsInfo['restrictions_num']){
  203. return api_error_return('超过限购数量,还可购买:'.($goodsInfo['restrictions_num']-$count).'件');
  204. }
  205. }
  206. }
  207. if ($request->user['status'] == 0){
  208. return api_error_return('用户被禁用');
  209. }
  210. if ($request->user['is_reward'] == 0) {
  211. return api_error_return('用户已被禁止交易');
  212. }
  213. try {
  214. if (!$userAddress = UserAddress::getAddressIdDetails((int)$address_id)) {
  215. return api_error_return('获取收货地址失败');
  216. }
  217. if ($userAddress['uid'] != $request->user['id']) {
  218. return api_error_return('收货地址信息异常');
  219. }
  220. if (empty($buy_goods)) {
  221. return api_error_return('参数错误2');
  222. }
  223. foreach ($buy_goods as $key => $value) {
  224. $result[$value['goods_sn'] . '_' . $value['spec_id']][] = $value;
  225. }
  226. if (!ShopOrderModel::checkStock($result)) {
  227. return api_error_return('下单失败,所选商品含有库存不足的商品');
  228. }
  229. try {
  230. $data = ShopOrderModel::buyDetails($buy_goods, $userAddress);
  231. $supplier_name = $data['details'][0]['supplier_name'];
  232. $paymoney = $data['total']['count_payment'];
  233. // // 如果是余额支付 先判断余额
  234. // if (in_array($order_type, [1,3]) && $pay_type == 3){
  235. //// return json_encode($request->user);
  236. // if ($request->user['money'] < $paymoney){
  237. // return api_error_return('下单失败,用户余额不足');
  238. // }
  239. // }
  240. //
  241. // if ($order_type == 2){
  242. // $paymoney = $data['total']['count_payment'];
  243. // if ($request->user['score'] < $paymoney){
  244. // return api_error_return('下单失败,用户积分不足');
  245. // }
  246. // }
  247. //
  248. // if ($order_type == 4){
  249. // $paymoney = $data['total']['count_payment'];
  250. // if ($request->user['lock_score'] < $paymoney){
  251. // return api_error_return('下单失败,用户提货券不足');
  252. // }
  253. // }
  254. // $cur_xzmoney = Db::name('system_config')->where('name', 'xz_cur_money')->where('group', 'xzconfig')->value('value');
  255. // if ($order_type == 5){
  256. // $paymoney = getXzPirceWithPrice($cur_xzmoney, $data['total']['count_payment']);
  257. // $shouxu_scale = get_user_shouxufei($request->uid);
  258. // $shouxu = number_format($paymoney*$shouxu_scale/100, '2', '.', '');
  259. // $coupon_num = $data['total']['total_coupon_num'];
  260. // if ($request->user['xz_num'] < $paymoney+$shouxu){
  261. // return api_error_return('下单失败,用户星钻不足(总共需支付:'.($paymoney+$shouxu).'其中手续费:'.$shouxu_scale.'%');
  262. // }
  263. //
  264. // if ($request->user['xz_num'] - $paymoney - $shouxu < 10){
  265. // throw new Exception('账户必须预留10个星钻');
  266. // }
  267. //
  268. // if ($request->user['coupon_num'] < $coupon_num){
  269. // return api_error_return('下单失败,用户抢购值不足');
  270. // }
  271. //
  272. // }
  273. $count = Db::name('shop_order')->where('user_id', $request->uid)->where('order_type', 1)->where('status', 0)->whereTime('created_time', '-2 hours')->count();
  274. if ($count > 20){
  275. return api_error_return('未支付的订单太多,创建失败');
  276. }
  277. $flag = ShopOrderModel::createOrder(['order'=>$result, 'address' => $userAddress, 'cls'=>0, 'cart_ids'=>0, 'pay_type'=>$pay_type, 'order_remark'=>$remark, 'order_type'=>$order_type, 'supplier_name'=>$supplier_name], $request->uid, $request->user, $data, $request->post('cart_id', ''));
  278. // return $flag;
  279. // return json_encode($flag);
  280. if (!$flag) {
  281. return api_error_return('下单失败');
  282. }
  283. return api_succ_return(['msg'=>'下单成功', 'data'=>['order_ids'=>$flag['data']]]);
  284. } catch (\Exception $e) {
  285. return api_error_return('下单失败 ' . $e->getMessage());
  286. }
  287. } catch (\Exception $e){
  288. return api_error_return($e->getMessage());
  289. }
  290. }
  291. /**
  292. * 取消订单
  293. */
  294. public function cancelOrder (Request $request): Response
  295. {
  296. if (empty($request->only(['order_sn']))) {
  297. return api_error_return('订单号不能为空');
  298. }
  299. Db::startTrans();
  300. try {
  301. ShopOrderModel::cancelOrder($request->only(['order_sn']), $request->uid);
  302. Db::commit();
  303. }catch (\Exception $e){
  304. Db::rollback();
  305. return api_error_return('取消失败'.$e->getMessage());
  306. }
  307. return api_succ_return('取消成功');
  308. }
  309. /**
  310. * 确认订单
  311. */
  312. public function receiveOrder (Request $request): Response
  313. {
  314. if (empty($request->only(['order_sn']))) {
  315. return api_error_return('订单号不能为空');
  316. }
  317. Db::startTrans();
  318. try {
  319. ShopOrderModel::receiveOrder($request->only(['order_sn']), $request->uid);
  320. Db::commit();
  321. }catch (\Exception $e){
  322. Db::rollback();
  323. return api_error_return('确认失败'.$e->getMessage());
  324. }
  325. return api_succ_return('确认成功');
  326. }
  327. }