12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- <?php
- namespace app\common\model;
- use think\db\Query;
- use think\Model;
- /**
- * 会员模型
- * @method Query|self exists($data)
- * @property string city_detail
- */
- class UserAddress extends Model
- {
- protected $append=[
- 'city_detail',
- ];
- public function user(){
- return $this->belongsTo(User::class);
- }
- protected static function init()
- {
- self::beforeWrite(function (self $address){
- /*if(empty($address['city'])) {
- $address['city'] = tm()->getLocation($address['longitude'], $address['latitude'])['city'];
- }*/
- if(!isset($address['is_default'])){
- $address['is_default']=0;
- }
- });
- self::afterWrite(function (self $address){
- if($address['is_default']==1){
- self::where('user_id',$address['user_id'])->where('id','<>',$address['id'])->update(['is_default'=>0]);
- }
- });
- self::beforeInsert(function (self $address){
- /*if(empty($address['location'])){
- $address['location']=tm()->getLocation($address['longitude'],$address['latitude'])['address'];
- }*/
- });
- }
- public function scopeExists(Query $query,$data){
- if(!empty($data['name'])){
- $query->where('name',$data['name']);
- }
- if(!empty($data['mobile'])){
- $query->where('mobile',$data['mobile']);
- }
- if(!empty($data['address'])){
- $query->where('address',$data['address']);
- }
- if(!empty($data['city'])){
- $query->where('city',$data['city']);
- }
- if(!empty($data['longitude'])){
- $query->where('longitude',$data['longitude']);
- }
- if(!empty($data['latitude'])){
- $query->where('latitude',$data['latitude']);
- }
- }
- public function getCityDetailAttr($a,$b){
- $city=$b['city'];
- if(!$city){
- return '';
- }
- $area=Area::where('id|name|shortname',$city)->find();
- return $area['mergename']??'';
- }
- }
|