|
|
@@ -32,6 +32,8 @@ Component({
|
|
|
hasSelected: false,
|
|
|
leftTitle: "都没有",
|
|
|
rightTitle: "提交",
|
|
|
+ initialOptions: [] as Option[], // 保存初始状态
|
|
|
+ hasChanged: false, // 记录是否发生了变化
|
|
|
},
|
|
|
lifetimes: {
|
|
|
attached() {
|
|
|
@@ -50,18 +52,66 @@ Component({
|
|
|
},
|
|
|
},
|
|
|
observers: {
|
|
|
- "payload.options"(options) {
|
|
|
+ "payload.options"(options: any) {
|
|
|
this.setData({ options });
|
|
|
+ // 保存初始状态(深拷贝)
|
|
|
+ const initialOptions = JSON.parse(JSON.stringify(options));
|
|
|
+ this.setData({ initialOptions });
|
|
|
},
|
|
|
options(options: AnyArray) {
|
|
|
+ console.log("options", options);
|
|
|
+ const hasSelected = options
|
|
|
+ .filter((option: any) => !option.hide)
|
|
|
+ .some((option: any) => option.checked);
|
|
|
+
|
|
|
+ // 检测是否与初始状态不同
|
|
|
+ const hasChanged = this._checkOptionsChanged(options, this.data.initialOptions);
|
|
|
+
|
|
|
this.setData({
|
|
|
- hasSelected: options
|
|
|
- .filter((option) => !option.hide)
|
|
|
- .some((option) => option.checked),
|
|
|
+ hasSelected,
|
|
|
+ hasChanged,
|
|
|
});
|
|
|
},
|
|
|
},
|
|
|
methods: {
|
|
|
+ // 检测选项是否发生变化
|
|
|
+ _checkOptionsChanged(currentOptions: Option[], initialOptions: Option[]): boolean {
|
|
|
+ if (!initialOptions || initialOptions.length === 0) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 比较主选项的选中状态
|
|
|
+ for (let i = 0; i < currentOptions.length; i++) {
|
|
|
+ const current = currentOptions[i];
|
|
|
+ const initial = initialOptions[i];
|
|
|
+
|
|
|
+ if (!initial) continue;
|
|
|
+
|
|
|
+ // 比较主选项的checked状态
|
|
|
+ if (current.checked !== initial.checked) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果有子选项,比较子选项的选中状态
|
|
|
+ if (current.options && initial.options) {
|
|
|
+ for (let j = 0; j < current.options.length; j++) {
|
|
|
+ const currentSub = current.options[j];
|
|
|
+ const initialSub = initial.options[j];
|
|
|
+
|
|
|
+ if (!initialSub) continue;
|
|
|
+
|
|
|
+ if (currentSub.checked !== initialSub.checked) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ },
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
handleTop(event: HandleEvent) {
|
|
|
const {
|
|
|
mark: { item },
|
|
|
@@ -80,6 +130,7 @@ Component({
|
|
|
const hasAnySubChecked = itemInState?.options?.some(
|
|
|
(o: any) => o?.checked
|
|
|
);
|
|
|
+ console.log("itemInState", itemInState, hasSubOptions, hasAnySubChecked);
|
|
|
if (itemInState?.checked && hasSubOptions && hasAnySubChecked) {
|
|
|
this.setData({
|
|
|
subTitle: itemInState.name,
|
|
|
@@ -113,6 +164,8 @@ Component({
|
|
|
this.onSubmit();
|
|
|
}
|
|
|
},
|
|
|
+
|
|
|
+
|
|
|
handleSub(event: HandleEvent) {
|
|
|
const {
|
|
|
mark: { item },
|
|
|
@@ -218,6 +271,7 @@ Component({
|
|
|
onCancel() {},
|
|
|
onConfirm() {},
|
|
|
onSubmit() {
|
|
|
+ console.log("onSubmit", this.data.result, this.data.hasSelected);
|
|
|
if (this.data.result) return;
|
|
|
if (!this.data.hasSelected) {
|
|
|
wx.showToast({ title: "请至少选择一项", icon: "error" });
|
|
|
@@ -233,14 +287,32 @@ Component({
|
|
|
return [option.name, sub].filter(Boolean).join(": ");
|
|
|
})
|
|
|
.join("; ");
|
|
|
+ console.log("result", result);
|
|
|
this.setData({ result });
|
|
|
this.triggerEvent("next", { options: this.data.options });
|
|
|
}
|
|
|
},
|
|
|
onSkip() {
|
|
|
- if (this.data.result || this.data.hasSelected) return;
|
|
|
- if (!this.data.payload.required) {
|
|
|
- this.setData({ result: "都没有" });
|
|
|
+ console.log("onSkip", this.data.result, this.data.hasSelected);
|
|
|
+ if (this.data.result) return;
|
|
|
+
|
|
|
+ // 如果是新用户(belongNew为true),使用原来的逻辑
|
|
|
+ if (this.data.payload.belongNew) {
|
|
|
+ if (this.data.hasSelected) return;
|
|
|
+ if (!this.data.payload.required) {
|
|
|
+ this.setData({ result: "都没有" });
|
|
|
+ this.triggerEvent("next", { options: this.data.options });
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 如果是老用户(belongNew为false),检查是否发生了变化
|
|
|
+ if (this.data.hasChanged) {
|
|
|
+ // 如果发生了变化,不允许点击"无变化"
|
|
|
+ // wx.showToast({ title: "症状已发生变化,请重新选择", icon: "none" });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果没有变化,允许点击"无变化"
|
|
|
+ this.setData({ result: "无变化" });
|
|
|
this.triggerEvent("next", { options: this.data.options });
|
|
|
}
|
|
|
},
|