import Taro, { getCurrentPages } from "@tarojs/taro";
/**
 * 根据&转为数组
 */
export const splitStrToArr = (str: string | undefined, sign: string = ",") => {
  if (str) {
    return str.split(sign);
  }
  return [];
};

//状态的动态颜色
export const caseStutsColor: Record<string, string> = {
  20: "#49C57D", //待受理
  30: "#3E6BDA", //待立案
  40: "#3E6BDA", //待派遣
  50: "#2FC7DA", //待处置
  60: "#2FC7DA", //处置中
  70: "#FFA748", //待核查
  80: "#C456F2", //待结案
  90: "#F56F6B", //已结案
  0: "#767B89", //已作废
  10: "#45B2FE", //预处理
  15: "#458AFE", //待直达
};

export function getAuth(
  authStr: keyof Taro.AuthSetting,
  refuseHint?: string,
  refuseCb?: () => void
) {
  return new Promise<boolean>((resolve) => {
    Taro.getSetting({
      success: (res) => {
        const flag = res.authSetting[authStr];
        if (flag) {
          resolve(true);
        } else {
          Taro.authorize({
            scope: authStr,
            success: () => {
              resolve(true);
            },
            fail: () => {
              resolve(false);
              if (refuseHint) {
                Taro.showModal({
                  title: "提示",
                  content: refuseHint,
                  confirmText: "去授权",
                  success: (res) => {
                    if (res.confirm) {
                      Taro.openSetting({
                        success: (res) => {},
                      });
                    } else {
                      refuseCb && refuseCb();
                    }
                  },
                });
              }
            },
          });
        }
      },
    });
  });
}
