From 2860c33cd00aa4fde57bbac181094db2eef65b4f Mon Sep 17 00:00:00 2001
From: Anyon <zoujingli@qq.com>
Date: Fri, 11 Sep 2020 11:25:41 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BC=9A=E5=91=98=E6=94=B6?=
 =?UTF-8?q?=E8=B4=A7=E5=9C=B0=E5=9D=80=E7=AE=A1=E7=90=86?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 app/data/controller/api/auth/Address.php | 139 +++++++++++++++++++++++
 app/data/controller/api/auth/Order.php   |   8 +-
 app/data/service/UserService.php         |   8 +-
 3 files changed, 147 insertions(+), 8 deletions(-)
 create mode 100644 app/data/controller/api/auth/Address.php

diff --git a/app/data/controller/api/auth/Address.php b/app/data/controller/api/auth/Address.php
new file mode 100644
index 000000000..4019fa2c9
--- /dev/null
+++ b/app/data/controller/api/auth/Address.php
@@ -0,0 +1,139 @@
+<?php
+
+namespace app\data\controller\api\auth;
+
+use app\data\controller\api\Auth;
+use think\admin\extend\CodeExtend;
+
+/**
+ * 会员收货地址管理
+ * Class Address
+ * @package app\data\controller\api\auth
+ */
+class Address extends Auth
+{
+    /**
+     * 绑定数据表
+     * @var string
+     */
+    private $table = 'DataMemberAddress';
+
+    /**
+     * 添加收货地址
+     * @throws \think\db\exception\DbException
+     */
+    public function set()
+    {
+        $data = $this->_vali([
+            'mid.value'        => $this->mid,
+            'code.default'     => '',
+            'type.default'     => 0,
+            'type.in:0,1'      => '地址状态不在范围!',
+            'name.require'     => '收货人姓名不能为空!',
+            'phone.mobile'     => '收货人手机格式错误!',
+            'phone.require'    => '收货人手机不能为空!',
+            'province.require' => '地址省份不能为空!',
+            'city.require'     => '地址城市不能为空!',
+            'area.require'     => '地址区域不能为空!',
+            'address.require'  => '详情地址不能为空!',
+            'deleted.value'    => 0,
+        ]);
+        if (empty($data['code'])) {
+            unset($data['code']);
+            $count = $this->app->db->name($this->table)->where($data)->count();
+            if ($count > 0) $this->error('抱歉,该地址已经存在!');
+            $data['code'] = CodeExtend::uniqidDate(12, 'A');
+            if ($this->app->db->name($this->table)->insert($data) === false) {
+                $this->error('添加收货地址失败!');
+            }
+        } else {
+            $map = ['mid' => $this->mid, 'code' => $data['code']];
+            $address = $this->app->db->name($this->table)->where($map)->find();
+            if (empty($address)) $this->error('修改收货地址不存在!');
+            $this->app->db->name($this->table)->where($map)->update($data);
+        }
+        // 去除其它默认选项
+        if (isset($data['type']) && $data['type'] > 0) {
+            $map = [['mid', '=', $this->mid], ['code', '<>', $data['code']]];
+            $this->app->db->name($this->table)->where($map)->update(['type' => 0]);
+        }
+        $this->success('添加收货地址成功!', $this->_getAddress($data['code']));
+    }
+
+
+    /**
+     * 获取收货地址
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function get()
+    {
+        $query = $this->_query($this->table)->withoutField('deleted');
+        $query->equal('code')->where(['mid' => $this->mid, 'deleted' => 0]);
+        $result = $query->order('type desc,id desc')->page(false, false, false, 15);
+        $this->success('获取收货地址数据!', $result);
+    }
+
+    /**
+     * 修改收货地址状态
+     * @throws \think\db\exception\DbException
+     */
+    public function state()
+    {
+        $data = $this->_vali([
+            'mid.value'    => $this->mid,
+            'code.require' => '地址编号不能为空!',
+            'type.require' => '地址状态不能为空!',
+            'type.in:0,1'  => '地址状态不在范围!',
+        ]);
+        // 检查地址是否存在
+        $map = ['mid' => $data['mid'], 'code' => $data['code']];
+        if ($this->app->db->name($this->table)->where($map)->count() < 1) {
+            $this->error('修改的地址不存在!');
+        }
+        // 更新默认地址状态
+        $data['type'] = intval($data['type']);
+        $this->app->db->name($this->table)->where($map)->update(['type' => $data['type']]);
+        // 去除其它默认选项
+        if ($data['type'] > 0) {
+            $map = [['mid', '=', $this->mid], ['code', '<>', $data['code']]];
+            $this->app->db->name($this->table)->where($map)->update(['type' => 0]);
+        }
+        $this->success('默认设置成功!', $this->_getAddress($data['code']));
+    }
+
+    /**
+     * 删除收货地址
+     * @throws \think\db\exception\DbException
+     */
+    public function remove()
+    {
+        $map = $this->_vali([
+            'mid.value'    => $this->mid,
+            'code.require' => '地址编号不能为空!',
+        ]);
+        $address = $this->app->db->name($this->table)->where($map)->find();
+        if (empty($address)) $this->error('需要删除的地址不存在!');
+        if ($this->app->db->name($this->table)->where($map)->update(['deleted' => 1]) !== false) {
+            $this->success('删除地址成功!');
+        } else {
+            $this->error('删除地址失败!');
+        }
+    }
+
+    /**
+     * 获取指定的收货地址
+     * @param string $code
+     * @return array|\think\Model|null
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    private function _getAddress($code)
+    {
+        $map = ['code' => $code, 'mid' => $this->mid, 'deleted' => 0];
+        return $this->app->db->name($this->table)->withoutField('deleted')->where($map)->find();
+    }
+
+}
\ No newline at end of file
diff --git a/app/data/controller/api/auth/Order.php b/app/data/controller/api/auth/Order.php
index ab3e1f366..cd63811eb 100644
--- a/app/data/controller/api/auth/Order.php
+++ b/app/data/controller/api/auth/Order.php
@@ -22,6 +22,7 @@ class Order extends Auth
      */
     protected function initialize()
     {
+        parent::initialize();
         if (empty($this->member['status'])) {
             $this->error('账户已被冻结,不能操作订单数据哦!');
         }
@@ -36,7 +37,7 @@ class Order extends Auth
     public function add()
     {
         // 商品规则
-        $rules = $this->request->post('rule', '');
+        $rules = $this->request->post('items', '');
         if (empty($rules)) $this->error('商品规则不能为空!');
         // 订单数据
         [$codes, $items] = [[], []];
@@ -95,9 +96,8 @@ class Order extends Auth
             // 同步商品库存及销量
             foreach ($codes as $code) GoodsService::instance()->syncStock($code);
             // 返回前端订单编号
-            $this->success('订单创建成功,请补全收货地址后支付!', [
-                'order_no' => $order['order_no'], 'reduct' => $order['amount_reduct'],
-            ]);
+            $order['items'] = $items;
+            $this->success('预购订单创建成功,请补全收货地址', $order);
         } catch (HttpResponseException $exception) {
             throw $exception;
         } catch (\Exception $exception) {
diff --git a/app/data/service/UserService.php b/app/data/service/UserService.php
index 645c03b58..2794530d9 100644
--- a/app/data/service/UserService.php
+++ b/app/data/service/UserService.php
@@ -31,13 +31,13 @@ class UserService extends Service
     {
         $map = ['token' => $token, 'deleted' => 0];
         $query = $this->app->db->name($this->table)->where($map);
-        $member = $query->withoutField('status,deleted')->find();
+        $member = $query->withoutField('tokenv,deleted')->find();
         if (empty($member)) {
             throw new \think\Exception('登录授权失败');
         }
-        if ($member['tokenv'] !== $this->buildTokenVerify()) {
-            throw new \think\Exception('请重新登录授权');
-        }
+//        if ($member['tokenv'] !== $this->buildTokenVerify()) {
+//            throw new \think\Exception('请重新登录授权');
+//        }
         return array_merge($member, $data);
     }