ShopAddress.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. class ShopAddress extends Base {
  5. public function add($userId){
  6. //查询用户是否有默认地址
  7. $address=$this
  8. ->where(['user_id'=>$userId,'is_default'=>1])
  9. ->find();
  10. $data=[
  11. 'name'=>input('name/s','','trim'),
  12. 'phone'=>input('phone/s','','trim'),
  13. 'content'=>input('content/s','','trim'),
  14. 'is_default'=>input('is_default/d',0),
  15. 'user_id'=>$userId,
  16. 'create_time'=>date('Y-m-d H:i:s'),
  17. ];
  18. $validate = new \app\common\validate\ShopAddress();
  19. $result = $validate->check($data,[]);
  20. if(true !== $result){
  21. $this->error = $validate->getError();
  22. return false;
  23. }
  24. //修改为不默认地址
  25. if($data['is_default'] == 1 && $address){
  26. $this->where('user_id',$userId)->update(['is_default'=>0]);
  27. }
  28. $ret=$this->insert($data);
  29. return $ret;
  30. }
  31. public function updates($id,$userId){
  32. $data=[
  33. 'name'=>input('name/s','','trim'),
  34. 'phone'=>input('phone/s','','trim'),
  35. 'content'=>input('content/s','','trim'),
  36. 'is_default'=>input('is_default/d'),
  37. 'update_time'=>date('Y-m-d H:i:s')
  38. ];
  39. $validate = new \app\common\validate\ShopAddress();
  40. $result = $validate->check($data,[]);
  41. if(true !== $result){
  42. $this->error = $validate->getError();
  43. return false;
  44. }
  45. if($data['is_default'] == 1){
  46. $this->where('user_id',$userId)->update(['is_default'=>0]);
  47. }
  48. $ret=$this->where('id',$id)->update($data);
  49. return $ret;
  50. }
  51. public function lists($userId){
  52. $ret=Db::name('shop_address')
  53. ->field('id,user_id,name,phone,building,content,unit,is_default')
  54. ->where('user_id',$userId)
  55. ->order('id desc')
  56. ->select();
  57. return $ret?$ret:[];
  58. }
  59. public function del($id){
  60. $ret=$this->delete($id);
  61. return $ret;
  62. }
  63. public function detail($id){
  64. $ret=$this->where('id',$id)->find();
  65. return $ret;
  66. }
  67. public function finds($userId){
  68. $map[]=['user_id','=',$userId];
  69. $map[]=['is_default','=',1];
  70. $ret=$this->where($map)->find();
  71. if(!$ret){
  72. $ret=$this->where('user_id',$userId)
  73. ->order('id','desc')->find();
  74. }
  75. return $ret;
  76. }
  77. }