控制器(给前端接口):
/** * 新增&编辑店鋪 * @param $data * @param $user_id * @return array */ public function editstore(){ $userId = input('param.user_id'); if (empty($userId)){ return error_code(11067); } $data['shop_name'] = input('param.shop_name'); $data['shop_logo'] = input('param.shop_logo'); $data['shop_introduce'] = input('param.shop_introduce'); $data['id'] = input('param.id'); $model = new StoreManagementModel(); return $model->editstore($data, $userId); }
模型层:
/** * 新增&编辑店鋪 * @param $data * @param $user_id * @return array */ public function editstore($data, $user_id){ $result = [ 'status' => false, 'data' => '', 'msg' => '' ]; $store_data = [ 'user_id' => $user_id, 'shop_name' => $data['shop_name'], 'shop_logo' => _sImage($data['shop_logo']), 'shop_introduce' => $data['shop_introduce'] ]; if (isset($data['id']) && !empty($data['id'])) { //编辑 $where[] = ['id', 'eq', $data['id']]; $this->where($where)->find(); if ($this->allowField(true)->save($store_data, ['id' => $data['id']])) { $result['data'] = $data; $result['status'] = true; $result['msg'] = '成功'; } else { //保存失败 return error_code(10004); } } else { //新增 if ($this->allowField(true)->save($store_data)) { $result['data'] = $data; $result['status'] = true; $result['msg'] = '成功'; } else { return error_code(10004); } } return $result; }
成功的秘诀,在永不改变既定的目的~