|
@@ -0,0 +1,196 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace app\api\controller;
|
|
|
+
|
|
|
+use app\api\model\Commoditycolor;
|
|
|
+use app\api\model\OrderModel;
|
|
|
+use app\common\controller\Api;
|
|
|
+use think\Db;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 订单接口
|
|
|
+ */
|
|
|
+class Order extends Api
|
|
|
+{
|
|
|
+ protected $noNeedLogin = '*';
|
|
|
+ protected $noNeedRight = '*';
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 购物车总价统计
|
|
|
+ * @ApiMethod (POST)
|
|
|
+ * @param string $list p_id colorid
|
|
|
+ */
|
|
|
+ public function carMoney()
|
|
|
+ {
|
|
|
+ $params = $this->request->post();
|
|
|
+ if (!isset($params['list'])) {
|
|
|
+ return $this->result('网络错误', [], 100);
|
|
|
+ }
|
|
|
+ $list = $params['list'];
|
|
|
+ foreach ($list as $v) {
|
|
|
+ $commoditycolor[] = Commoditycolor::where('colorid', $v['colorid'])->find();
|
|
|
+ }
|
|
|
+ if ($commoditycolor) {
|
|
|
+ $money = 0;
|
|
|
+ foreach ($commoditycolor as $v) {
|
|
|
+ $money = $money + $v['money'];
|
|
|
+ }
|
|
|
+ return $this->result('', $money, 200);
|
|
|
+ } else {
|
|
|
+ return $this->result('网络错误', [], 100);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 订单先提交
|
|
|
+ * @ApiMethod (POST)
|
|
|
+ * @param string $user_id 用户id
|
|
|
+ * @param string $list 商品参数 c_id buy_number p_id colorid
|
|
|
+ */
|
|
|
+ public function orderGenerate()
|
|
|
+ {
|
|
|
+ $params = $this->request->post();
|
|
|
+ if (!isset($params['user_id'])) {
|
|
|
+ return $this->result('网络错误', [], 100);
|
|
|
+ }
|
|
|
+ if (!isset($params['list'])) {
|
|
|
+ return $this->result('网络错误', [], 100);
|
|
|
+ }
|
|
|
+ $list = $params['list'];
|
|
|
+ $rules = [
|
|
|
+ 'buy_number' => "require|number",
|
|
|
+ 'colorid' => "require|number",
|
|
|
+ ];
|
|
|
+ $msg = [
|
|
|
+ 'buy_number.require' => '未选择购买数量',
|
|
|
+ 'colorid.require' => '未选择颜色',
|
|
|
+ 'colorid.number' => '网络错误',
|
|
|
+ 'buy_number.number' => '网络错误',
|
|
|
+ ];
|
|
|
+ foreach ($list as $v) {
|
|
|
+ $validata = $this->validate($v, $rules, $msg);
|
|
|
+ if (is_string($validata)) {
|
|
|
+ return $this->result($validata, [], 100);
|
|
|
+ }
|
|
|
+ $colorids[] = $v['colorid']; //颜色id放在同一个数组里
|
|
|
+ $buy_numbers[] = $v['buy_number']; // 购买数量放在同一个数组里
|
|
|
+ }
|
|
|
+ $colorid = implode(',', $colorids); // 颜色数组拆分成为字符串
|
|
|
+ $buy_number = implode(',', $buy_numbers); //数量数组拆分成为字符串
|
|
|
+ $data = array(
|
|
|
+ 'user_id' => $params['user_id'],
|
|
|
+ 'colorid' => $colorid,
|
|
|
+ 'buy_number' => $buy_number,
|
|
|
+ 'create_time' => date('Y-m-d H:i:s', time()),
|
|
|
+ );
|
|
|
+ $addPre = Db::name('order_pre')->insertGetId($data);
|
|
|
+ if ($addPre) {
|
|
|
+ return $this->result('', $addPre, 200);
|
|
|
+ } else {
|
|
|
+ return $this->result('请求失败,请重新购买', [], 100);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 支付订单显示
|
|
|
+ * @ApiMethod (POST)
|
|
|
+ * @param string $pre_id 订单提交返回的值
|
|
|
+ */
|
|
|
+ public function preOrderIndex()
|
|
|
+ {
|
|
|
+ $pre_id = $this->request->post('pre_id');
|
|
|
+ if (!$pre_id) {
|
|
|
+ return $this->result('网络错误', [], 100);
|
|
|
+ }
|
|
|
+ $order_pre = Db::name('order_pre')->where('pre_id', $pre_id)->find(); //查出预存订单
|
|
|
+ $order_pre['colorid'] = explode(',', $order_pre['colorid']); // 批量拆分id
|
|
|
+ $order_pre['buy_number'] = explode(',', $order_pre['buy_number']); // 批量拆分购买数量
|
|
|
+ $count = count($order_pre['buy_number']);
|
|
|
+ $commoditycolor = new Commoditycolor();
|
|
|
+ for ($i = 0; $i < $count; $i++) {
|
|
|
+ $data[] = $commoditycolor->alias('co')
|
|
|
+ ->join('parameter p', 'co.p_id = p.p_id', 'left')
|
|
|
+ ->join('commodity c', 'p.c_id = c.c_id', 'left')
|
|
|
+ ->where('co.colorid', $order_pre['colorid'][$i])
|
|
|
+ ->find(); // 循环查出购买的商品
|
|
|
+ }
|
|
|
+ $data['money'] = 0; // 总价
|
|
|
+ $data['freight'] = 0; // 运费
|
|
|
+ $data['number'] = "yxj" . rand(1000, 9999) . time(); // 订单编号
|
|
|
+ $data['create_time'] = $order_pre['create_time']; // 创建时间
|
|
|
+ $data['whitebean'] = 0; // 白豆个数
|
|
|
+ for ($i = 0; $i < $count; $i++) {
|
|
|
+ $data[$i]['buy_number'] = $order_pre['buy_number'][$i]; // 循环写入购买数量
|
|
|
+ $data['money'] = $data['money'] + $data[$i]['c_freight'] + $data[$i]['money']; // 总费用
|
|
|
+ $data['freight'] = $data['freight'] + $data[$i]['c_freight']; // 总运费
|
|
|
+ $data['whitebean'] = $data['whitebean'] + $data[$i]['c_whitebean']; // 总白豆数
|
|
|
+ }
|
|
|
+ $preAddMoney = Db::name('order_pre')->where('pre_id', $pre_id)->setInc('money', $data['money']);
|
|
|
+ if ($data && $preAddMoney) {
|
|
|
+ return $this->result('', $data, 200);
|
|
|
+ } else {
|
|
|
+ return $this->result('网络错误', [], 100);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 支付订单
|
|
|
+ * @ApiMethod (POST)
|
|
|
+ * @param string $pre_id 预存id
|
|
|
+ * @param string $list 商品参数 c_id buy_number p_id colorid name(颜色名) p_name(规格名) color(图片)
|
|
|
+ * @param string $user_id 用户id
|
|
|
+ * @param string $money 总价
|
|
|
+ * @param string $a_id 地址id
|
|
|
+ * @param string $a_tel 收货手机号
|
|
|
+ * @param string $a_name 收货姓名
|
|
|
+ * @param string $area 收货地址
|
|
|
+ * @param string $city 城市
|
|
|
+ * @param string $freight 运费
|
|
|
+ * @param string $whitebean 总白豆数
|
|
|
+ * @param string $create_time 创建时间
|
|
|
+ * @param string $number 编号
|
|
|
+ * @param string $type 0余额支付1第四方支付
|
|
|
+ */
|
|
|
+ public function orderPay()
|
|
|
+ {
|
|
|
+ $parames = $this->request->post();
|
|
|
+ $rules = [
|
|
|
+ 'pre_id' => 'require|number',
|
|
|
+ 'user_id' => 'require',
|
|
|
+ 'money' => 'require',
|
|
|
+ 'a_id' => 'require',
|
|
|
+ 'a_tel' => 'require',
|
|
|
+ 'a_name' => 'require',
|
|
|
+ 'area' => 'require',
|
|
|
+ 'city' => 'require',
|
|
|
+ 'freight' => 'require',
|
|
|
+ 'whitebean' => 'require',
|
|
|
+ 'type' => 'require|max:1',
|
|
|
+ ];
|
|
|
+ $msg = [
|
|
|
+ 'pre_id.require' => '网络错误1',
|
|
|
+ 'user_id.require' => '网络错误2',
|
|
|
+ 'money.require' => '网络错误3',
|
|
|
+ 'a_id.require' => '网络错误4',
|
|
|
+ 'a_tel.require' => '网络错误5',
|
|
|
+ 'a_name.require' => '网络错误6',
|
|
|
+ 'area.require' => '网络错误7',
|
|
|
+ 'city.require' => '网络错误8',
|
|
|
+ 'freight.require' => '网络错误9',
|
|
|
+ 'whitebean.require' => '网络错误10',
|
|
|
+ 'type.require' => '网络错误11',
|
|
|
+ 'type.max' => '网络错误12',
|
|
|
+ 'pre_id.number' => '网络错误13',
|
|
|
+ ];
|
|
|
+ $validata = $this->validate($parames,$rules,$msg);
|
|
|
+ if (is_string($validata)) {
|
|
|
+ return $this->result($validata,[],100);
|
|
|
+ }
|
|
|
+ $order = new OrderModel();
|
|
|
+ if ($parames['type'] == 0) {
|
|
|
+ $res = $order->userMoneyPay($parames);
|
|
|
+ halt($res);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|