接口
<?php
namespace app\api\controller\b2c;
use app\common\controller\Api;
use think\Db;
/**
*b2c/UserAddress
*
* @icon fa fa-circle-o
*/
class UserAddress extends Api
{
/**
* Address模型对象
* @var \app\common\model\user\Address
*/
protected $noNeedLogin = ['*'];
protected $model = null;
//需要获取的变量
protected $datas = null;
protected $add_datt = ['id'];
protected $edit_datt = ['id'];
protected $select_datt = ['id'];
public function _initialize() {
parent::_initialize();
$this->model = Db::name("user_address");
$this->add_datt = ['id'];
$this->edit_datt = ['id'];
}
/**
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
*/
/**
* 查看
*/
public function index() {
//当前是否为关联查询
$where = [];
$sort = [];
$order = [];
$user = $this->auth->getUser();
if (empty($user)) {
$this -> error("操作错误");
}
$where["user_id"] = $user->id;
$page = (isset($_POST["page"])&&!empty($_POST["page"]))?$_POST["page"]:1;
$limit = isset($_POST["limit"])&&!empty($_POST["limit"])?$_POST["limit"]:10;
$total = $this->model
->where($where)
->order($sort, $order)
->count();
$list = $this->model
->where($where)
->order($sort, $order)
->page($page, $limit)
->select();
// /*
// id
// user_id
// create_time
// update_time
// is_default
// status
// province
// city
// area
// address
// mobile
// recipients
// */
// $list = collection($list)->toArray();
$result = array("total" => $total, "rows" => $list);
$this->success('请求成功',$result);
}
/**
* 修改
*/
public function find() {
$post["id"] = $this->request->request("id");
if (empty($post["id"])) {
$this -> error("操作错误");
}
$user = $this->auth->getUser();
if (empty($user)) {
$this -> error("操作错误");
}
$row = $this->model->where("id","in",$post["id"])->find();
$this->success('请求成功',$row);
}
/**
* 添加
*/
public function add() {
$user = $this->auth->getUser();
if (empty($user)) {
$this -> error("操作错误");
}
$post = $this->request->request();
if (empty($post)) {
$this -> error("操作错误");
}
$data["mobile"] = empty($post["mobile"])?:$post["mobile"];
$data["recipients"] = empty($post["recipients"])?:$post["recipients"];
$data["address"] = empty($post["address"])?:$post["address"];
$data["area"] = empty($post["area"])?:$post["area"];
$data["city"] = empty($post["city"])?:$post["city"];
$data["province"] = empty($post["province"])?:$post["province"];
$data["status"] = empty($post["status"])?:$post["status"];
$data["is_default"] = $post["is_default"] ;
$data["user_id"] = $user->id;
$data["create_time"] = time();
$data["update_time"] = time();
if ($data["is_default"] == 1) {
$row = $this->model ->where("user_id",$user->id)->update(["is_default" => 0]);
}
$row = $this->model ->insert($data);
$this->success('请求成功',$row);
}
/**
* 修改
*/
public function edit() {
$post = $this->request->request();
if (empty($post)) {
$this -> error("操作错误");
}
$user = $this->auth->getUser();
if (empty($user)) {
$this -> error("操作错误");
}
$data=[];
$data["mobile"] = empty($post["mobile"])?:$post["mobile"];
$data["recipients"] = empty($post["recipients"])?:$post["recipients"];
$data["address"] = empty($post["address"])?:$post["address"];
$data["area"] = empty($post["area"])?:$post["area"];
$data["city"] = empty($post["city"])?:$post["city"];
$data["province"] = empty($post["province"])?:$post["province"];
$data["status"] = empty($post["status"])?:$post["status"];
$data["is_default"] = $post["is_default"] ;
$data["update_time"] = time();
if ($data["is_default"] == 1) {
$row = $this->model ->where("user_id",$user->id)->update(["is_default" => 0]);
}
// 启动事务
$row = $this->model->where("id",$post["id"])->update($data);
$this->success('请求成功',$row);
}
/**
* 删除
*/
public function delete() {
$ids = $this->request->request("ids");
if (empty($ids)) {
$this -> error("操作错误");
}
$row = $this->model->where("id","in",$ids)->delete();
$this->success('请求成功',$row);
}
}
数据表
/*
Navicat MySQL Data Transfer
Source Server : 47.94.156.208
Source Server Version : 50648
Source Host : 47.94.156.208:3306
Source Database : dspzbdshop
Target Server Type : MYSQL
Target Server Version : 50648
File Encoding : 65001
Date: 2020-12-20 17:26:27
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for fa_user_address
-- ----------------------------
DROP TABLE IF EXISTS `fa_user_address`;
CREATE TABLE `fa_user_address` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`user_id` int(10) NOT NULL COMMENT '用户ID',
`create_time` varchar(50) NOT NULL COMMENT '添加时间',
`update_time` varchar(50) NOT NULL COMMENT '修改时间',
`is_default` int(1) NOT NULL COMMENT '是否默认地址',
`status` int(1) DEFAULT '1' COMMENT '0删除,1正常',
`province` varchar(255) NOT NULL COMMENT '省ID',
`city` varchar(255) DEFAULT NULL COMMENT '城市ID',
`area` varchar(255) DEFAULT NULL COMMENT '区ID',
`address` text COMMENT '详细地址',
`mobile` varchar(50) DEFAULT NULL COMMENT '联系电话',
`recipients` varchar(100) DEFAULT NULL COMMENT '收件人',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8 COMMENT='我的地址';
-- ----------------------------
-- Records of fa_user_address
-- ----------------------------
INSERT INTO `fa_user_address` VALUES ('1', '4', '1591864126', '1591864126', '0', '0', 'asd1', 'asd1', 'asd1', 'asd1', '148355445611', 'asd1');
INSERT INTO `fa_user_address` VALUES ('4', '12', '1594625179', '1594625179', '0', '1', '1', '1', '1', '陈家沟', '15753848722', '杨露禅');
INSERT INTO `fa_user_address` VALUES ('5', '12', '1594625215', '1594625215', '0', '1', '1', '1', '1', '陈家沟', '15753848722', '杨露禅');
INSERT INTO `fa_user_address` VALUES ('6', '12', '1594625286', '1594625286', '0', '1', '1', '1', '1', '水泊梁山', '156824063', '圣手书生王伦');
INSERT INTO `fa_user_address` VALUES ('9', '5', '1604123950', '1604453495', '0', '1', '辽宁省', '沈阳市', '和平区', '花园小区', '13210985135', '李四');
INSERT INTO `fa_user_address` VALUES ('10', '5', '1604124047', '1604129221', '0', '1', '北京市', '市辖区', '东城区', '121212', '13131313131', '礼包');
INSERT INTO `fa_user_address` VALUES ('11', '5', '1604124277', '1604453507', '1', '1', '吉林省', '长春市', '南关区', '飒飒', '18545541212', '张三');
INSERT INTO `fa_user_address` VALUES ('15', '5', '1604128947', '1604128947', '0', '1', '北京市', '市辖区', '东城区', '1212', '13212121211', '驱蚊器');
INSERT INTO `fa_user_address` VALUES ('16', '1', '1605233241', '1608191705', '1', '1', '山东省', '济南市', '历下区', '汇能大厦', '18858412855', '张三');
INSERT INTO `fa_user_address` VALUES ('17', '1', '1605256024', '1605343735', '0', '1', '山东省', '济南市', '历城区', '1212', '13221212121', '李四');
INSERT INTO `fa_user_address` VALUES ('18', '3', '1605263537', '1605263537', '1', '1', '北京市', '市辖区', '丰台区', '大巴车', '18354393241', '王兆镇');
INSERT INTO `fa_user_address` VALUES ('19', '24', '1605344470', '1605344470', '1', '1', '山东省', '济南市', '历城区', '汇能大厦', '13511093848', '测试');
INSERT INTO `fa_user_address` VALUES ('20', '25', '1605520343', '1605520343', '1', '1', '江苏省', '徐州市', '铜山区', '万达广场三楼', '15505597371', '胡胡');
INSERT INTO `fa_user_address` VALUES ('21', '21', '1605582246', '1605582246', '1', '1', '山东省', '济南市', '历城区', '汇能大厦一楼大厅', '18302696789', '赵露思');
INSERT INTO `fa_user_address` VALUES ('22', '26', '1606182180', '1606182180', '1', '1', '江苏省', '徐州市', '铜山区', '万达广场', '18652199115', '哦哦');
INSERT INTO `fa_user_address` VALUES ('23', '23', '1606528796', '1606528796', '1', '1', '山东省', '济南市', '历城区', '刚刚好', '18354393242', '需要');
INSERT INTO `fa_user_address` VALUES ('24', '27', '1607594577', '1607594577', '1', '1', '北京市', '北京市', '东城区', 'Rwerwerw', '18800000000', '123');
INSERT INTO `fa_user_address` VALUES ('25', '44', '1608183564', '1608183564', '0', '1', '江苏省', '徐州市', '铜山区', '万达广场', '18652199223', '哈哈');
INSERT INTO `fa_user_address` VALUES ('26', '44', '1608183564', '1608183564', '1', '1', '江苏省', '徐州市', '铜山区', '万达广场', '18652199223', '哈哈');
INSERT INTO `fa_user_address` VALUES ('27', '45', '1608184342', '1608184342', '1', '1', '江苏省', '徐州市', '铜山区', '万达广场', '18652199112', '哈哈');