UserAddress.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace app\common\model;
  3. use think\db\Query;
  4. use think\Model;
  5. /**
  6. * 会员模型
  7. * @method Query|self exists($data)
  8. * @property string city_detail
  9. */
  10. class UserAddress extends Model
  11. {
  12. protected $append=[
  13. 'city_detail',
  14. ];
  15. public function user(){
  16. return $this->belongsTo(User::class);
  17. }
  18. protected static function init()
  19. {
  20. self::beforeWrite(function (self $address){
  21. /*if(empty($address['city'])) {
  22. $address['city'] = tm()->getLocation($address['longitude'], $address['latitude'])['city'];
  23. }*/
  24. if(!isset($address['is_default'])){
  25. $address['is_default']=0;
  26. }
  27. });
  28. self::afterWrite(function (self $address){
  29. if($address['is_default']==1){
  30. self::where('user_id',$address['user_id'])->where('id','<>',$address['id'])->update(['is_default'=>0]);
  31. }
  32. });
  33. self::beforeInsert(function (self $address){
  34. /*if(empty($address['location'])){
  35. $address['location']=tm()->getLocation($address['longitude'],$address['latitude'])['address'];
  36. }*/
  37. });
  38. }
  39. public function scopeExists(Query $query,$data){
  40. if(!empty($data['name'])){
  41. $query->where('name',$data['name']);
  42. }
  43. if(!empty($data['mobile'])){
  44. $query->where('mobile',$data['mobile']);
  45. }
  46. if(!empty($data['address'])){
  47. $query->where('address',$data['address']);
  48. }
  49. if(!empty($data['city'])){
  50. $query->where('city',$data['city']);
  51. }
  52. if(!empty($data['longitude'])){
  53. $query->where('longitude',$data['longitude']);
  54. }
  55. if(!empty($data['latitude'])){
  56. $query->where('latitude',$data['latitude']);
  57. }
  58. }
  59. public function getCityDetailAttr($a,$b){
  60. $city=$b['city'];
  61. if(!$city){
  62. return '';
  63. }
  64. $area=Area::where('id|name|shortname',$city)->find();
  65. return $area['mergename']??'';
  66. }
  67. }