"use strict";const e=require("../vendor.js"),n=require("../../gen/Apis.js"),o=require("../libraries/apiLoading.js"),i="wx04d2d27ca198367b",l=e.defineStore("we_app_auth",()=>{const l=e.ref(!1),a=e.ref(!1),t=e.ref({user:{id:0,name:"",phone:""},work_info:{session_key:"",openid:""},permissions:[],environment:""}),s=e.ref(null),d=()=>{s.value&&(clearInterval(s.value),s.value=null),s.value=setInterval(()=>{u()},3e5)},u=()=>{e.index.getLocation({type:"wgs84",altitude:!0,isHighAccuracy:!0,success(e){n.Apis.Attendance.AttendanceEmployeeTracks.Track({latitude:e.latitude,longitude:e.longitude}),console.log(e,"经纬度")},fail(n){e.index.hideLoading(),e.index.showModal({title:"提示",content:"定位失败，员工轨迹失效！请打开定位！",success:function(n){n.confirm?(e.index.openSetting(),console.log("用户点击确定")):n.cancel&&console.log("用户点击取消")}}),console.log(n,"获取经纬度失败")}})};const r=(o,i)=>{n.Apis.Login.Auth.WorkLogin({code:i,app_id:"ww96c806131a95c739"}).then(n=>{var i,a,s,r,v,c,g,p;console.log("企业微信登录",null==n?void 0:n.data),t.value.user=null==(i=n.data)?void 0:i.user,t.value.work_info=null==(a=n.data)?void 0:a.work_info,t.value.permissions=null==(r=null==(s=n.data)?void 0:s.permissions[0])?void 0:r.children,l.value=!1,e.index.setStorageSync("NCHL_PROD_ACCESS_TOKEN_CUSTOMER",null==(c=null==(v=null==n?void 0:n.data)?void 0:v.token)?void 0:c.token),(null==(p=null==(g=null==n?void 0:n.data)?void 0:g.user)?void 0:p.phone)&&(u(),d()),null==o||o.appContext.config.globalProperties.$isResolve()}).catch(()=>{l.value=!1,a.value=!0})},v=(o,s)=>{n.Apis.Login.Auth.Login({code:s,app_id:i}).then(n=>{var i,a,s,r,v,c,g;console.log("登录",null==n?void 0:n.data),t.value.user=null==(i=n.data)?void 0:i.user,t.value.permissions=null==(s=null==(a=n.data)?void 0:a.permissions[0])?void 0:s.children,l.value=!1,e.index.setStorageSync("NCHL_PROD_ACCESS_TOKEN_CUSTOMER",null==(v=null==(r=null==n?void 0:n.data)?void 0:r.token)?void 0:v.token),(null==(g=null==(c=null==n?void 0:n.data)?void 0:c.user)?void 0:g.phone)&&(u(),d()),null==o||o.appContext.config.globalProperties.$isResolve()}).catch(()=>{l.value=!1,a.value=!0})};function c(e){o.getApiLoading(n.Apis.Login.Auth.Me,{}).then(n=>{var o,i;return t.value.user=n.data.user,t.value.permissions=null==(i=null==(o=n.data)?void 0:o.permissions[0])?void 0:i.children,console.log("me",n.data),l.value=!1,a.value=!1,e()})}return{loading:l,hasError:a,data:t,login:function(n){const o=e.index.getSystemInfoSync();t.value.environment=null==o?void 0:o.environment,console.log(null==o?void 0:o.environment,"res?.environment"),"wxwork"===(null==o?void 0:o.environment)?e.wx$1.qy.login({success:function(e){console.log(e),r(n,e.code)}}):e.index.login({provider:"weixin",success:function(e){console.log(e),v(n,e.code)}})},me:c,handleUserLogin:async l=>{o.getApiLoading(n.Apis.Login.Auth.BindPhoneNumber,{app_id:i,...l}).then(n=>{c(()=>{e.index.navigateBack({delta:1})})})},handleUserLoginWxwork:async l=>{var a,s;o.getApiLoading(n.Apis.Login.Auth.BindWork,{app_id:i,session_key:null==(s=null==(a=null==t?void 0:t.value)?void 0:a.work_info)?void 0:s.session_key,iv:null==l?void 0:l.iv,encrypted_data:null==l?void 0:l.encryptedData}).then(n=>{c(()=>{e.index.navigateBack({delta:1})})})}}});exports.useWeAppAuthStore=l;
