0], [['gender'], 'in', 'range' => [0,1,2,3]], [['tag'], 'in', 'range' => [0,1,2,3,4]], [['model', 'user_id', 'cx_mch_id', 'province_id', 'city_id', 'district_id', 'detail', 'lat', 'lng', ], 'required'] ]; } public function attributeLabels() { return [ 'cx_mch_id' => '平台商户ID', 'user_id' => '用户ID', 'name' => '姓名', 'mobile' => '手机号', 'province_id' => '省ID', 'province' => '省', 'city_id' => '市ID', 'city' => '市', 'district_id' => '区ID', 'district' => '区', 'town_id' => '镇id', 'town' => '镇', 'detail' => '详细地址', 'formatted_addr' => '可读地址', 'is_default' => '是否默认,0=否,1=是', 'lat' => '纬度', 'lng' => '经度', 'gender' => '性别,0=未知,1=男,2=女,3=保密', 'tag' => '标签,0=未知,1=家,2=公司,3=学校,4=其他', ]; } public function save() { if(!$this->validate()){ return $this->getModelError(); } $t = \Yii::$app->db->beginTransaction(); if($this->model->isNewRecord){ $this->model->cx_mch_id = $this->cx_mch_id; $this->model->user_id = $this->user_id; $this->model->is_delete = 0; } $this->model->name = $this->name; $this->model->mobile = $this->mobile; $province = District::getDistrict($this->province_id); if(!$province){ return $this->apiReturnError("无效省ID"); } $this->model->province_id = $this->province_id; $this->model->province = $province['name']; $city = District::getDistrict($this->city_id); if(!$city){ return $this->apiReturnError("无效城市ID"); } $this->model->city_id = $this->city_id; $this->model->city = $city['name']; $district = District::getDistrict($this->district_id); if(!$district){ return $this->apiReturnError("无效区/县ID"); } $this->model->district_id = $this->district_id; $this->model->district = $district['name']; if(!empty($this->town_id)){ $town = District::find() ->where(['id' => $this->town_id, 'is_delete' => 0]) ->select('id,name,level,parent_id') ->asArray()->one(); if($town == null) return $this->apiReturnError("无效镇/街道ID"); $this->model->town_id = $this->town_id; $this->model->town = $town['name']; } $this->model->detail = $this->detail; $this->model->formatted_addr = $this->formatted_addr; if($this->is_default == 1){ //取消默认 Address::updateAll(['is_default' => 0],[ 'is_delete' => 0, 'user_id' => $this->user_id, 'cx_mch_id' => $this->cx_mch_id ]); } $this->model->is_default = $this->is_default; $this->model->lat = $this->lat; $this->model->lng = $this->lng; $this->model->gender = $this->gender; $this->model->tag = $this->tag; if(!$this->model->save()){ $t->rollBack(); return $this->getModelError($this->model); } $t->commit(); return $this->apiReturnSuccess("保存成功"); } }