scene('set')->validate(); $user_info = $this->checkLogin(); if (!$user_info) return json_send(['code' => 'error', 'msg' => '请先登录']); $user_id = $user_info['uid']; $open_subscribe = request('open_subscribe', 0); // 接收数据 $tmplate_id = request('tmplate_id', ''); $data['tmplate_id'] = $tmplate_id; $data['open_subscribe'] = $open_subscribe; //订阅开关0=开启1=关闭 $data['user_id'] = $user_id; $data['insert_time'] = time(); try { // 查询用户 $config_data = $SubscriptionTmpModel->where([['user_id', '=', $user_id], ['tmplate_id', '=', $tmplate_id]])->first(); // 存在的话 if ($config_data) { // 更新数据 $config_data->open_subscribe = $data['open_subscribe']; $config_data->update_time = time(); $config_data->save(); } else { //写入数据 $SubscriptionTmpModel->insert($data); } } catch (\Exception $e) { return json_send(['code' => 'error', 'msg' => '操作失败', 'data' => $e->getMessage()]); } // 返回结果 return json_send(['code' => 'success', 'msg' => '操作成功', 'data' => '']); } }