true, // 经期前提醒 'ovulation' => true, // 排卵日提醒 'record' => true, // 每日记录提醒 'care' => true, // 关怀提醒(家人共护) 'time' => '21:00', ]; /** * 读取设置 */ public function index() { $setting = Db::name('user_setting')->where('user_id', $this->auth->id)->find(); if (!$setting) { $setting = [ 'app_lock' => 'none', 'local_only' => 0, 'remind_json' => json_encode($this->defaultRemind), ]; } $remind = json_decode($setting['remind_json'], true) ?: $this->defaultRemind; $remind = array_merge($this->defaultRemind, $remind); $this->success('', [ 'app_lock' => $setting['app_lock'], 'has_lock_code' => !empty($setting['app_lock_value']), 'local_only' => (int)$setting['local_only'], 'remind' => $remind, ]); } /** * 保存设置 * POST app_lock=none|password|biometric, app_lock_code=1234, local_only=0|1, remind={"period":true,...} */ public function save() { $userId = $this->auth->id; $exist = Db::name('user_setting')->where('user_id', $userId)->find(); $data = ['updatetime' => time()]; $appLock = $this->request->post('app_lock', null); if ($appLock !== null) { if (!in_array($appLock, ['none', 'password', 'biometric'])) { $this->error('应用锁类型不正确'); } $data['app_lock'] = $appLock; } $code = (string)$this->request->post('app_lock_code', ''); if ($code !== '') { if (!preg_match('/^\d{4,6}$/', $code)) { $this->error('应用锁密码需为 4-6 位数字'); } // 仅存哈希,不落明文 $data['app_lock_value'] = md5($code . 'huāqī'); } $localOnly = $this->request->post('local_only', null); if ($localOnly !== null) { $data['local_only'] = (int)!!$localOnly; } $remind = $this->readRemind(); if ($remind !== null) { $merged = array_merge($this->defaultRemind, array_intersect_key($remind, $this->defaultRemind)); if (isset($merged['time']) && !preg_match('/^\d{2}:\d{2}$/', $merged['time'])) { $merged['time'] = '21:00'; } $data['remind_json'] = json_encode($merged, JSON_UNESCAPED_UNICODE); } if ($exist) { Db::name('user_setting')->where('id', $exist['id'])->update($data); } else { $data['user_id'] = $userId; $data['createtime'] = time(); Db::name('user_setting')->insert($data); } $this->success('已保存'); } /** * 读取提醒配置:支持 remind_xxx 独立字段(推荐,兼容 request filter)与 remind JSON * @return array|null */ protected function readRemind() { $keys = ['period', 'ovulation', 'record', 'care', 'time']; $result = []; $hasField = false; foreach ($keys as $key) { $value = $this->request->post('remind_' . $key, null); if ($value !== null) { $hasField = true; $result[$key] = $key === 'time' ? (string)$value : (bool)(int)$value; } } if ($hasField) { return $result; } // 兼容 JSON 传参(注意:全局 filter 会把引号转义,需还原) $json = $this->request->post('remind', null); if ($json === null || $json === '') { return null; } $arr = is_array($json) ? $json : json_decode(htmlspecialchars_decode((string)$json, ENT_QUOTES), true); return is_array($arr) ? $arr : null; } }