fmode-ng 0.0.218 → 0.0.219

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,6 @@
5
5
  * 保留所有权利 All Rights Reserved.
6
6
  * /home/ryan/workspace/nova/nova-admin/dist/fmode-ng/esm2022/lib/core/parse/fmode.user.mjs
7
7
  */
8
- import{FmodeObject}from"./fmode.object";export class FmodeUser extends FmodeObject{static{this._instance=null}static{this.instanceKey=()=>{if(!this._instance)throw new Error("FmodeParse not initialized before using User");return"Parse/"+this._instance.appId+"/currentUser"}}static bindInstance(e){this._instance=e}static get instance(){if(!this._instance)throw new Error("FmodeParse not initialized");return this._instance}get username(){return this.get("username")}set username(e){this.set("username",e)}get email(){return this.get("email")}set email(e){this.set("email",e)}get password(){return this.get("password")}set password(e){this.set("password",e)}constructor(e){super("_User"),e&&(this.id=e?.objectId,this.createdAt=new Date(e?.createdAt),this.updatedAt=new Date(e?.updatedAt),e.sessionToken&&(this.sessionToken=e.sessionToken,delete e.sessionToken),this.set(e))}static async logIn(e,s){const t=`${this.instance.serverURL}/login`,r=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json","X-Parse-Application-Id":this.instance.appId},body:JSON.stringify({username:e,password:s})}),n=await r.json();if(n.error)throw new Error(n.error);const i=new this({...n,username:e,password:s});return i._saveToCache(),i}static async become(e){const s=`${this.instance.serverURL}/users/me`;let t={"X-Parse-Session-Token":e,"X-Parse-Application-Id":this.instance.appId};const r=await fetch(s,{headers:t}),n=await r.json();if(n.error)throw new Error(n.error);const i=new this({...n,sessionToken:e});return i._saveToCache(),i}static async signUp(e,s,t={}){return new this({...t,username:e,password:s}).signUp()}static async logOut(){if(this.current())try{await fetch(`${this.instance.serverURL}/logout`,{method:"POST",headers:{"X-Parse-Session-Token":this.current().sessionToken,"X-Parse-Application-Id":this.instance.appId}})}finally{this._clearCurrentUser()}}static async requestPasswordReset(e){const s=`${this.instance.serverURL}/requestPasswordReset`,t=await fetch(s,{method:"POST",headers:{"Content-Type":"application/json","X-Parse-Application-Id":this.instance.appId},body:JSON.stringify({email:e})}),r=await t.json();if(r.error)throw new Error(r.error)}static{this._currentUser=null}static current(){if(this._currentUser)return this._currentUser;try{const e=localStorage.getItem(this.instanceKey());if(!e)return null;const s=JSON.parse(e);if(!s.sessionToken)return null;const t=new this(s);return this._currentUser=t,t.sessionToken&&this._instance&&(this._instance.sessionToken=t.sessionToken),t}catch(e){return console.warn("Failed to restore user from cache",e),null}}static _clearCurrentUser(){this._currentUser=null,this._instance&&(this._instance.sessionToken=void 0),localStorage.removeItem(this.instanceKey())}async signUp(e){if(e&&this.set(e),!this.username)throw new Error("Username is required");if(!this.password)throw new Error("Password is required");let s=this.toJSON();delete s.__type,delete s.className;const t=`${FmodeUser.instance.serverURL}/users`,r=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json","X-Parse-Application-Id":FmodeUser.instance.appId},body:JSON.stringify(s)}),n=await r.json();if(n.error)throw new Error(n.error);return this.id=n.objectId,this.createdAt=new Date(n.createdAt),this.sessionToken=n.sessionToken,this._saveToCache(),this}async save(){const e=`${FmodeUser.instance.serverURL}/users/${this.id||this.data.objectId}`,s={"Content-Type":"application/json","X-Parse-Application-Id":FmodeUser.instance.appId,"X-Parse-Session-Token":this.sessionToken},t=this.toJSON();delete t.__type,delete t.objectId,delete t.className,delete t.sessionToken,delete t.username,delete t.createdAt,delete t.updatedAt;const r=await fetch(e,{method:"PUT",headers:s,body:JSON.stringify(t)}),n=await r.json();if(n.error)throw 209===n.code&&FmodeUser._clearCurrentUser(),new Error(n.error);return this.set("updatedAt",new Date(n.updatedAt)),this._saveToCache(),this}_saveToCache(){this.sessionToken&&(FmodeUser._currentUser=this,FmodeUser._instance&&(FmodeUser._instance.sessionToken=this.sessionToken),localStorage.setItem(FmodeUser.instanceKey(),JSON.stringify(this.toJSONForCache())))}toJSONForCache(){return{...super.toJSON(),sessionToken:this.sessionToken}}getSessionToken(){return this.sessionToken}async destroy(e){if(!this.id)throw new Error("Cannot destroy unsaved user");const s=`${FmodeUser.instance.serverURL}/users/${this.id}`,t={"X-Parse-Session-Token":this.sessionToken,"X-Parse-Application-Id":FmodeUser.instance.appId};e?.useMasterKey&&FmodeUser.instance.config.masterKey&&(t["X-Parse-Master-Key"]=FmodeUser.instance.config.masterKey);const r=await fetch(s,{method:"DELETE",headers:t});if(!r.ok){const e=await r.json();throw new Error(e.error||"Failed to delete user")}FmodeUser._clearCurrentUser()}}
8
+ import{FmodeObject}from"./fmode.object";export class FmodeUser extends FmodeObject{static{this._instance=null}static{this.instanceKey=()=>{if(!this._instance)throw new Error("FmodeParse not initialized before using User");return"Parse/"+this._instance.appId+"/currentUser"}}static bindInstance(e){this._instance=e}static get instance(){if(!this._instance)throw new Error("FmodeParse not initialized");return this._instance}get username(){return this.get("username")}set username(e){this.set("username",e)}get email(){return this.get("email")}set email(e){this.set("email",e)}get password(){return this.get("password")}set password(e){this.set("password",e)}constructor(e){super("_User"),e&&(this.id=e?.objectId,this.createdAt=new Date(e?.createdAt),this.updatedAt=new Date(e?.updatedAt),e.sessionToken&&(this.sessionToken=e.sessionToken,delete e.sessionToken),this.set(e))}static async logIn(e,s){const t=`${this.instance.serverURL}/login`,r=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json","X-Parse-Application-Id":this.instance.appId},body:JSON.stringify({username:e,password:s})}),n=await r.json();if(n.error)throw new Error(n);const i=new this({...n,username:e,password:s});return i._saveToCache(),i}static async become(e){const s=`${this.instance.serverURL}/users/me`;let t={"X-Parse-Session-Token":e,"X-Parse-Application-Id":this.instance.appId};const r=await fetch(s,{headers:t}),n=await r.json();if(n.error)throw new Error(n.error);const i=new this({...n,sessionToken:e});return i._saveToCache(),i}static async signUp(e,s,t={}){return new this({...t,username:e,password:s}).signUp()}static async logOut(){if(this.current())try{await fetch(`${this.instance.serverURL}/logout`,{method:"POST",headers:{"X-Parse-Session-Token":this.current().sessionToken,"X-Parse-Application-Id":this.instance.appId}})}finally{this._clearCurrentUser()}}static async requestPasswordReset(e){const s=`${this.instance.serverURL}/requestPasswordReset`,t=await fetch(s,{method:"POST",headers:{"Content-Type":"application/json","X-Parse-Application-Id":this.instance.appId},body:JSON.stringify({email:e})}),r=await t.json();if(r.error)throw new Error(r.error)}static{this._currentUser=null}static current(){if(this._currentUser)return this._currentUser;try{const e=localStorage.getItem(this.instanceKey());if(!e)return null;const s=JSON.parse(e);if(!s.sessionToken)return null;const t=new this(s);return this._currentUser=t,t.sessionToken&&this._instance&&(this._instance.sessionToken=t.sessionToken),t}catch(e){return console.warn("Failed to restore user from cache",e),null}}static _clearCurrentUser(){this._currentUser=null,this._instance&&(this._instance.sessionToken=void 0),localStorage.removeItem(this.instanceKey())}async signUp(e){if(e&&this.set(e),!this.username)throw new Error("Username is required");if(!this.password)throw new Error("Password is required");let s=this.toJSON();delete s.__type,delete s.className;const t=`${FmodeUser.instance.serverURL}/users`,r=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json","X-Parse-Application-Id":FmodeUser.instance.appId},body:JSON.stringify(s)}),n=await r.json();if(n.error)throw new Error(n.error);return this.id=n.objectId,this.createdAt=new Date(n.createdAt),this.sessionToken=n.sessionToken,this._saveToCache(),this}async save(){const e=`${FmodeUser.instance.serverURL}/users/${this.id||this.data.objectId}`,s={"Content-Type":"application/json","X-Parse-Application-Id":FmodeUser.instance.appId,"X-Parse-Session-Token":this.sessionToken},t=this.toJSON();delete t.__type,delete t.objectId,delete t.className,delete t.sessionToken,delete t.username,delete t.createdAt,delete t.updatedAt;const r=await fetch(e,{method:"PUT",headers:s,body:JSON.stringify(t)}),n=await r.json();if(n.error)throw 209===n.code&&FmodeUser._clearCurrentUser(),new Error(n.error);return this.set("updatedAt",new Date(n.updatedAt)),this._saveToCache(),this}_saveToCache(){this.sessionToken&&(FmodeUser._currentUser=this,FmodeUser._instance&&(FmodeUser._instance.sessionToken=this.sessionToken),localStorage.setItem(FmodeUser.instanceKey(),JSON.stringify(this.toJSONForCache())))}toJSONForCache(){return{...super.toJSON(),sessionToken:this.sessionToken}}getSessionToken(){return this.sessionToken}async destroy(e){if(!this.id)throw new Error("Cannot destroy unsaved user");const s=`${FmodeUser.instance.serverURL}/users/${this.id}`,t={"X-Parse-Session-Token":this.sessionToken,"X-Parse-Application-Id":FmodeUser.instance.appId};e?.useMasterKey&&FmodeUser.instance.config.masterKey&&(t["X-Parse-Master-Key"]=FmodeUser.instance.config.masterKey);const r=await fetch(s,{method:"DELETE",headers:t});if(!r.ok){const e=await r.json();throw new Error(e.error||"Failed to delete user")}FmodeUser._clearCurrentUser()}}
9
9
  var MODULE_PATH_NEED = `6K+l5paH5Lu25piv5pys6aG555uu55qE5LiA6YOo5YiGIFRoaXMgZmlsZSBpcyBwYXJ0IG9mIHRoZSBDb21wb25lbnRzIGluIEZtb2RlIEluYy4KICAgIOeJiOadg+aJgOaciSDCqSDmnKrmnaXpo57pqawgwqkg5rGf6KW/6ISR5o6n56eR5oqA5pyJ6ZmQ5YWs5Y+4IENvcHlyaWdodCDCqSBGbW9kZSBUZWNobm9sb2d5IENvLiwgTHRkLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCBSaWdodHMgUmVzZXJ2ZWQuCiAgICDkuKXnpoHlnKjmnKrnu4/mjojmnYPnmoTmg4XlhrXkuIvvvIzpgJrov4fku7vkvZXlqpLku4vlpI3liLbmraTmlofku7YgVW5hdXRob3JpemVkIGNvcHlpbmcgb2YgdGhpcyBmaWxlLCB2aWEgYW55IG1lZGl1bSBpcyBzdHJpY3RseSBwcm9oaWJpdGVkCiAgICDor6Xmlofku7bmmK/kuJPmnInnmoTmnLrlr4bmlofku7YgUHJvcHJpZXRhcnkgYW5kIGNvbmZpZGVudGlhbAogICAKICAgIENvcHlyaWdodCAyMDIxLW5vdyBGbW9kZSBJbmMuIHN1cHBvcnRAZm1vZGUuY24uIDE4NjA3MDA3MDczLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCByaWdodHMgcmVzZXJ2ZWQuCgogICAgUEFUSDovaG9tZS9yeWFuL3dvcmtzcGFjZS9ub3ZhL25vdmEtYWRtaW4vZGlzdC9mbW9kZS1uZy9lc20yMDIyL2xpYi9jb3JlL3BhcnNlL2Ztb2RlLnVzZXIubWpz`
10
10
 
@@ -5,6 +5,6 @@
5
5
  * 保留所有权利 All Rights Reserved.
6
6
  * /home/ryan/workspace/nova/nova-admin/dist/fmode-ng/esm2022/lib/core/social/wxwork/wxwork.auth.mjs
7
7
  */
8
- import{WxworkSDK}from"./wxwork.sdk";import{FmodeParse}from"../../parse";const Parse=FmodeParse.with("nova");export class WxworkAuth{constructor(r){this.cid=r.cid,this.wxwork=new WxworkSDK({cid:r.cid,appId:r.appId||"crm"})}async getUserInfo(r){try{return await this.wxwork.getUserinfo(r)}catch(r){throw console.error("WxworkAuth.getUserInfo error:",r),r}}async syncUserInfo(r){try{if(r||(r=await this.getUserInfo()),!r)throw new Error("无法获取用户信息");return await this.wxwork.syncUserInfo(r)}catch(r){throw console.error("WxworkAuth.syncUserInfo error:",r),r}}async autoLogin(r){try{const o=Parse.User.current();if(o?.id){console.log("用户已登录:",o.get("username")),r||(r=await this.getUserInfo());const t=await this.wxwork.getContactOrProfile(r);return t?.id&&!t.get("user")?.id&&(t.set("user",o.toPointer()),await t.save(),console.log("已同步 user 指针到 Profile/UserSocial")),o}if(r||(r=await this.getUserInfo()),!r)throw new Error("无法获取用户信息");const t=r.userid||r.external_userid||r.openid;if(!t||t.length<6)return console.warn("用户名太短,无法自动登录:",t),null;const e=t.slice(-6);try{const o=await Parse.User.logIn(t,e);console.log("自动登录成功:",t);const s=await this.wxwork.getContactOrProfile(r);return s?.id&&!s.get("user")?.id&&(s.set("user",o.toPointer()),await s.save(),console.log("已同步 user 指针到 Profile/UserSocial")),o}catch(o){if(101!==o.code)throw console.error("自动登录失败:",o),o;{console.log("用户不存在,创建新用户:",t);const o=new Parse.User;o.set("username",t),o.set("password",e),r.mobile&&o.set("mobilePhoneNumber",r.mobile),r.email&&o.set("email",r.email);try{const e=await o.signUp();console.log("自动注册成功:",t);const s=await this.wxwork.getContactOrProfile(r);return s?.id&&(s.set("user",e.toPointer()),await s.save(),console.log("已同步 user 指针到 Profile/UserSocial")),e}catch(r){throw console.error("自动注册失败:",r),r}}}}catch(r){throw console.error("WxworkAuth.autoLogin error:",r),r}}async authenticateAndLogin(r){try{const o=await this.getUserInfo(r),t=await this.syncUserInfo(o);return{userInfo:o,profile:t,user:await this.autoLogin(o)}}catch(r){throw console.error("WxworkAuth.authenticateAndLogin error:",r),r}}async oauth(r="snsapi_base",o=!1){try{return await this.wxwork.oauth(r,o)}catch(r){throw console.error("WxworkAuth.oauth error:",r),r}}async loginPC(){try{return await this.wxwork.loginPC()}catch(r){throw console.error("WxworkAuth.loginPC error:",r),r}}getSDK(){return this.wxwork}}
8
+ import{WxworkSDK}from"./wxwork.sdk";import{FmodeParse}from"../../parse";const Parse=FmodeParse.with("nova");export class WxworkAuth{constructor(r){this.cid=r.cid,this.wxwork=new WxworkSDK({cid:r.cid,appId:r.appId||"crm"})}async getUserInfo(r){try{return await this.wxwork.getUserinfo(r)}catch(r){throw console.error("WxworkAuth.getUserInfo error:",r),r}}async syncUserInfo(r){try{if(r||(r=await this.getUserInfo()),!r)throw new Error("无法获取用户信息");return await this.wxwork.syncUserInfo(r)}catch(r){throw console.error("WxworkAuth.syncUserInfo error:",r),r}}async autoLogin(r){try{const o=Parse.User.current();if(o?.id){console.log("用户已登录:",o.get("username")),r||(r=await this.getUserInfo());const t=await this.wxwork.getContactOrProfile(r);return t?.id&&!t.get("user")?.id&&(t.set("user",o.toPointer()),await t.save(),console.log("已同步 user 指针到 Profile/UserSocial")),o}if(r||(r=await this.getUserInfo()),!r)throw new Error("无法获取用户信息");const t=r.userid||r.external_userid||r.openid;if(!t||t.length<6)return console.warn("用户名太短,无法自动登录:",t),null;const e=t.slice(-6);try{let o;o=await Parse.User.logIn(t,e);const s=await this.wxwork.getContactOrProfile(r);return s?.id&&!s.get("user")?.id&&(s.set("user",o.toPointer()),await s.save(),console.log("已同步 user 指针到 Profile/UserSocial")),o}catch(o){if(!("string"==typeof o&&o.indexOf("Invalid")>-1||101===o?.code))throw console.error("自动登录失败:",o),o;{console.log("用户不存在,创建新用户:",t);const o=new Parse.User;o.set("username",t),o.set("password",e),r.mobile&&o.set("mobile",r.mobile),r.email&&o.set("email",r.email);try{const e=await o.signUp();console.log("自动注册成功:",t);const s=await this.wxwork.getContactOrProfile(r);return s?.id&&(s.set("user",e.toPointer()),await s.save(),console.log("已同步 user 指针到 Profile/UserSocial")),e}catch(r){throw console.error("自动注册失败:",r),r}}}}catch(r){throw console.error("WxworkAuth.autoLogin error:",r),r}}async authenticateAndLogin(r){try{const o=await this.getUserInfo(r),t=await this.syncUserInfo(o);return{userInfo:o,profile:t,user:await this.autoLogin(o)}}catch(r){throw console.error("WxworkAuth.authenticateAndLogin error:",r),r}}async oauth(r="snsapi_base",o=!1){try{return await this.wxwork.oauth(r,o)}catch(r){throw console.error("WxworkAuth.oauth error:",r),r}}async loginPC(){try{return await this.wxwork.loginPC()}catch(r){throw console.error("WxworkAuth.loginPC error:",r),r}}getSDK(){return this.wxwork}}
9
9
  var MODULE_PATH_NEED = `6K+l5paH5Lu25piv5pys6aG555uu55qE5LiA6YOo5YiGIFRoaXMgZmlsZSBpcyBwYXJ0IG9mIHRoZSBDb21wb25lbnRzIGluIEZtb2RlIEluYy4KICAgIOeJiOadg+aJgOaciSDCqSDmnKrmnaXpo57pqawgwqkg5rGf6KW/6ISR5o6n56eR5oqA5pyJ6ZmQ5YWs5Y+4IENvcHlyaWdodCDCqSBGbW9kZSBUZWNobm9sb2d5IENvLiwgTHRkLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCBSaWdodHMgUmVzZXJ2ZWQuCiAgICDkuKXnpoHlnKjmnKrnu4/mjojmnYPnmoTmg4XlhrXkuIvvvIzpgJrov4fku7vkvZXlqpLku4vlpI3liLbmraTmlofku7YgVW5hdXRob3JpemVkIGNvcHlpbmcgb2YgdGhpcyBmaWxlLCB2aWEgYW55IG1lZGl1bSBpcyBzdHJpY3RseSBwcm9oaWJpdGVkCiAgICDor6Xmlofku7bmmK/kuJPmnInnmoTmnLrlr4bmlofku7YgUHJvcHJpZXRhcnkgYW5kIGNvbmZpZGVudGlhbAogICAKICAgIENvcHlyaWdodCAyMDIxLW5vdyBGbW9kZSBJbmMuIHN1cHBvcnRAZm1vZGUuY24uIDE4NjA3MDA3MDczLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCByaWdodHMgcmVzZXJ2ZWQuCgogICAgUEFUSDovaG9tZS9yeWFuL3dvcmtzcGFjZS9ub3ZhL25vdmEtYWRtaW4vZGlzdC9mbW9kZS1uZy9lc20yMDIyL2xpYi9jb3JlL3NvY2lhbC93eHdvcmsvd3h3b3JrLmF1dGgubWpz`
10
10