123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- <?php
- namespace app\common\model;
- use think\Db;
- class ShopAddress extends Base {
- public function add($userId){
- //查询用户是否有默认地址
- $address=$this
- ->where(['user_id'=>$userId,'is_default'=>1])
- ->find();
- $data=[
- 'name'=>input('name/s','','trim'),
- 'phone'=>input('phone/s','','trim'),
- 'content'=>input('content/s','','trim'),
- 'is_default'=>input('is_default/d',0),
- 'user_id'=>$userId,
- 'create_time'=>date('Y-m-d H:i:s'),
- ];
- $validate = new \app\common\validate\ShopAddress();
- $result = $validate->check($data,[]);
- if(true !== $result){
- $this->error = $validate->getError();
- return false;
- }
- //修改为不默认地址
- if($data['is_default'] == 1 && $address){
- $this->where('user_id',$userId)->update(['is_default'=>0]);
- }
- $ret=$this->insert($data);
- return $ret;
- }
- public function updates($id,$userId){
- $data=[
- 'name'=>input('name/s','','trim'),
- 'phone'=>input('phone/s','','trim'),
- 'content'=>input('content/s','','trim'),
- 'is_default'=>input('is_default/d'),
- 'update_time'=>date('Y-m-d H:i:s')
- ];
- $validate = new \app\common\validate\ShopAddress();
- $result = $validate->check($data,[]);
- if(true !== $result){
- $this->error = $validate->getError();
- return false;
- }
- if($data['is_default'] == 1){
- $this->where('user_id',$userId)->update(['is_default'=>0]);
- }
- $ret=$this->where('id',$id)->update($data);
- return $ret;
- }
- public function lists($userId){
- $ret=Db::name('shop_address')
- ->field('id,user_id,name,phone,building,content,unit,is_default')
- ->where('user_id',$userId)
- ->order('id desc')
- ->select();
- return $ret?$ret:[];
- }
- public function del($id){
- $ret=$this->delete($id);
- return $ret;
- }
- public function detail($id){
- $ret=$this->where('id',$id)->find();
- return $ret;
- }
- public function finds($userId){
- $map[]=['user_id','=',$userId];
- $map[]=['is_default','=',1];
- $ret=$this->where($map)->find();
- if(!$ret){
- $ret=$this->where('user_id',$userId)
- ->order('id','desc')->find();
- }
- return $ret;
- }
- }
|