fmode-ng 0.0.220 → 0.0.221
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/social/wxwork/wxwork.auth.mjs
|
|
7
7
|
*/
|
|
8
|
-
import{WxworkSDK}from"./wxwork.sdk";import{FmodeParse}from"../../parse";const Parse=FmodeParse.with("nova");export async function checkWeworkLogin(r){console.log("WxworkAuthGuard");let o=r.paramMap.get("cid")||r.queryParamMap.get("cid");o&&localStorage.setItem("company",o);let t,e=r.paramMap.get("appId")||"crm",a=new WxworkAuth({cid:o,appId:e});console.log("init wxwork");try{if(t=await a.getUserInfo(),console.log("guard userInfo"+JSON.stringify(t)),t)return await a.syncUserInfo(t),await a.autoLogin(t),!0}catch(r){console.error(r),console.log("guierr"+JSON.stringify(r))}let s=new URL(location?.href).searchParams.get("code");if(console.log("WxworkAuthGuard"+o+s),!s){let r=navigator.userAgent.toLowerCase();if(console.log("ua",r),-1!==r.indexOf("micromessenger")){let r="snsapi_base";"wechat"==a.platform()&&(r="snsapi_base"),"wxwork"==a.platform()&&(r="snsapi_privateinfo");let o=await a.oauth(r);return o&&(s=o),!1}s||(s=await a.loginPC())}try{t=await a.getUserInfo(s)}catch(r){console.log("codeinfoerr:"+JSON.stringify(r))}return console.log("before sync"+t?.mobile),await a.syncUserInfo(t),await a.autoLogin(t),!0}export class WxworkAuth{constructor(r){this.cid=r.cid||localStorage.getItem("company"),this.wxwork=new WxworkSDK({cid:r.cid,appId:r.appId||"crm"}),this.platform=this.wxwork.platform}async currentProfile(){let r=await this.getUserInfo(),o=await this.wxwork.getContactOrProfile(r);return"Profile"==o?.className?o:null}async currentContact(){let r=await this.getUserInfo(),o=await this.wxwork.getContactOrProfile(r);return"ContactInfo"==o?.className?o:null}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{r||(r=await this.getUserInfo());const o=Parse.User.current();if(o?.id){console.log("用户已登录:",o.get("username"));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 a=await this.wxwork.getContactOrProfile(r);return a?.id&&!a.get("user")?.id&&(a.set("user",o.toPointer()),await a.save(),console.log("已同步 user 指针到 Profile/UserSocial")),o}catch(o){if(console.log("loginError",o),!("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 a=await this.wxwork.getContactOrProfile(r);return a?.id&&(a.set("user",e.toPointer()),await a.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.autoLogin(o);return{userInfo:o,profile:await this.syncUserInfo(o),user:t}}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 async function checkWeworkLogin(r){console.log("WxworkAuthGuard");let o=r.paramMap.get("cid")||r.queryParamMap.get("cid")||localStorage.getItem("cid");o&&localStorage.setItem("company",o);let t,e=r.paramMap.get("appId")||"crm",a=new WxworkAuth({cid:o,appId:e});console.log("init wxwork");try{if(t=await a.getUserInfo(),console.log("guard userInfo"+JSON.stringify(t)),t)return await a.syncUserInfo(t),await a.autoLogin(t),!0}catch(r){console.error(r),console.log("guierr"+JSON.stringify(r))}let s=new URL(location?.href).searchParams.get("code");if(console.log("WxworkAuthGuard"+o+s),!s){let r=navigator.userAgent.toLowerCase();if(console.log("ua",r),-1!==r.indexOf("micromessenger")){let r="snsapi_base";"wechat"==a.platform()&&(r="snsapi_base"),"wxwork"==a.platform()&&(r="snsapi_privateinfo");let o=await a.oauth(r);return o&&(s=o),!1}s||(s=await a.loginPC())}try{t=await a.getUserInfo(s)}catch(r){console.log("codeinfoerr:"+JSON.stringify(r))}return console.log("before sync"+t?.mobile),await a.syncUserInfo(t),await a.autoLogin(t),!0}export class WxworkAuth{constructor(r){this.cid=r.cid||localStorage.getItem("company"),this.wxwork=new WxworkSDK({cid:r.cid,appId:r.appId||"crm"}),this.platform=this.wxwork.platform}async currentProfile(){let r=await this.getUserInfo(),o=await this.wxwork.getContactOrProfile(r);return"Profile"==o?.className?o:null}async currentContact(){let r=await this.getUserInfo(),o=await this.wxwork.getContactOrProfile(r);return"ContactInfo"==o?.className?o:null}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{r||(r=await this.getUserInfo());const o=Parse.User.current();if(o?.id){console.log("用户已登录:",o.get("username"));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 a=await this.wxwork.getContactOrProfile(r);return a?.id&&!a.get("user")?.id&&(a.set("user",o.toPointer()),await a.save(),console.log("已同步 user 指针到 Profile/UserSocial")),o}catch(o){if(console.log("loginError",o),!("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 a=await this.wxwork.getContactOrProfile(r);return a?.id&&(a.set("user",e.toPointer()),await a.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.autoLogin(o);return{userInfo:o,profile:await this.syncUserInfo(o),user:t}}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
|
|
|
@@ -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.sdk.mjs
|
|
7
7
|
*/
|
|
8
|
-
import*as ww from"@wecom/jssdk";import{FmodeParse}from"../../parse";const Parse=FmodeParse.with("nova");import{WxworkCorp}from"./wxwork.corp";import{wxdebug}from"../wxdebug";import{createLoginContainer,removeLoginContainer}from"./login-modal";export class WxworkSDK{constructor(e){this.companyMap={E4KpGvTEto:{corpResId:"vsVIWbd1i9"},uRiDGwEYLi:{corpResId:"JxfFXBr0ly"},cDL6R1hgSi:{corpResId:"SpL6gyD1Gu"}},this.suiteMap={crm:{suiteId:"dk2559ba758f33d8f5"}},this.ww=ww,this.groupChat={createGroupChat:async e=>{try{return wxdebug("createGroupChat"+JSON.stringify(e)),await this.registerCorpWithSuite()?new Promise(((t,r)=>{e.success=e=>{wxdebug(JSON.stringify(e)),t(e)},e.fail=e=>{wxdebug(JSON.stringify(e)),r(e)},ww.createCorpGroupChat(e)})):null}catch(e){wxdebug(JSON.stringify(e))}},addUserInfoGroup:async e=>{wxdebug("addUserInfoGroup"+JSON.stringify(e));try{let t=await this.registerCorpWithSuite();return wxdebug("isRegister"+t),t?(wxdebug(JSON.stringify(e)),new Promise(((t,r)=>{e.success=e=>{wxdebug(JSON.stringify(e)),t(e)},e.fail=e=>{wxdebug(JSON.stringify(e)),r(e)},ww.updateCorpGroupChat(e)}))):null}catch(e){wxdebug(JSON.stringify(e))}}},this.cid=e?.cid,this.appId=e?.appId,this.wecorp=new WxworkCorp(this.cid)}async syncGroupChat(e){let t=new Parse.Query("GroupChat");t.equalTo("chat_id",e?.chat_id);let r=await t.first();if(r?.id||(r=new Parse.Object("GroupChat")),!r?.get("joinUrl")){let t=(await this.wecorp.externalContact.groupChat.addJoinWay({scene:1,chat_id_list:[e.chat_id]}))?.config_id,o=(await this.wecorp.externalContact.groupChat.getJoinWay(t))?.join_way;r.set("joinUrl",o)}if(!r?.get("joinQrcode")){let t=(await this.wecorp.externalContact.groupChat.addJoinWay({scene:2,chat_id_list:[e.chat_id]}))?.config_id,o=(await this.wecorp.externalContact.groupChat.getJoinWay(t))?.join_way;r.set("joinQrcode",o)}let o={chat_id:e.chat_id,name:e.name,owner:e.owner,notice:e.notice},i=!1;return r.get("chat_id")!=e.chat_id&&(i=!0),r.get("name")!=e.name&&(i=!0),r.get("owner")!=e.owner&&(i=!0),r.get("notice")!=e.notice&&(i=!0),r.get("member_version")!=e.member_version&&(i=!0,o.member_list=e.member_list,o.member_version=e.member_version),r.set(o),this.cid&&r.set("company",{__type:"Pointer",className:"Company",objectId:this.cid}),i&&(r=await r.save()),r}async syncContact(e){const t=e.external_contact||e,r=t.external_userid;let o=new Parse.Query("ContactInfo");o.equalTo("external_userid",r);const i=new Parse.Object("Company");i.id=this.cid,o.equalTo("company",i);let a=await o.first();a?.id||(a=new Parse.Object("ContactInfo"),i?.id&&a.set("company",i.toPointer()));const n=t.name||"",s=t.mobile||"",c={...t,follow_user:e.follow_user||t.follow_user||[]};let l=!1;a.get("external_userid")!=r&&(l=!0),a.get("name")!=n&&n&&(l=!0),a.get("mobile")!=s&&s&&(l=!0);const d=a.get("data");return JSON.stringify(d)!==JSON.stringify(c)&&(l=!0),a.set("external_userid",r),a.set("name",n),a.set("mobile",s),a.set("data",c),wxdebug("syncContact",a.toJSON()),l&&(a=await a.save()),a}async getCurrentChatObject(){const e=await this.getCurrentChat();if(!e)return{currentChat:null};let t,r;try{if("chatId"===e.type&&e.group)t=await this.syncGroupChat(e.group);else if("userId"===e.type&&e.id){const t=await this.wecorp.externalContact.get(e.id);r=await this.syncContact(t)}}catch(e){console.error("getCurrentChatObject error:",e)}return{GroupChat:t,Contact:r,currentChat:e}}async syncUserInfo(e){let t=null;1==e.gender&&(t="男"),2==e.gender&&(t="女");let r=await this.getContactOrProfile(e);console.log("thisUser",r),console.log("userInfo",e),"ContactInfo"==r.className&&r.set({data:{openid:e.openid,userid:e.userid,external_userid:e.external_userid,type:e.type&&String(e.type)||null,name:e.name,avatar:e.avatar,gender:t,qr_code:e.qr_code,mobile:e.mobile,email:e.email,biz_mail:e.biz_mail,address:e.address,position:e.position,corp_name:e.corp_name},type:"wecom-agency",company:{__type:"Pointer",className:"Company",objectId:this.cid}}),"Profile"==r.className&&(r.set({openid:e.openid,userid:e.userid,external_userid:e.external_userid,type:e.type&&String(e.type)||null,name:e.name,avatar:e.avatar,gender:t,qr_code:e.qr_code,mobile:e.mobile,email:e.email,biz_mail:e.biz_mail,address:e.address,position:e.position,corp_name:e.corp_name,company:{__type:"Pointer",className:"Company",objectId:this.cid}}),r.set("data",e));try{if(r?.id&&!r.get("user")?.id){let e=Parse.User.current();e?.id&&r.set("user",e?.toPointer())}r=await r.save()}catch(e){wxdebug(e)}return r}async getUserinfo(e){let t,r;wxdebug("getUserinfo");let o=localStorage.getItem(`${this.cid}/USERINFO`);if(!e){t=JSON.parse(o),t.cid=this.cid,wxdebug("getContactOrProfile");try{r=await this.getContactOrProfile(t)}catch(e){}if(wxdebug("getUserinfo:"+o),t&&r)return t}if(!(e=new URL(location.href).searchParams.get("code")||e))return null;let i=await this.wecorp.auth.getuserinfo(e);if(wxdebug("result"+JSON.stringify(i)),i?.errcode)return console.error(i?.errmsg),null;let a=i?.external_userid;if(a){let e=await this.wecorp.externalContact.get(a);e?.external_contact&&Object.keys(e?.external_contact).forEach((t=>{i[t]=e?.external_contact[t]}))}return i.cid=this.cid,t=i,r=await this.getContactOrProfile(t),r?.get("mobile")&&(t.mobile=r?.get("mobile")),o=JSON.stringify(i),localStorage.setItem(`${this.cid}/USERINFO`,o),t}async getCurrentUser(){const e=await this.getUserinfo();if(!e)return null;return await this.getContactOrProfile(e)}async getContactOrProfile(e){let t;(e.openid||e.external_userid)&&(t="ContactInfo"),e.userid&&(t="Profile");let r=[];"Profile"==t&&(e.openid&&r.push({openid:{$regex:e.openid}}),e.userid&&r.push({userid:{$regex:e.userid}}),e.mobile&&r.push({mobile:{$regex:e.mobile}}),e.email&&r.push({email:{$regex:e.email}}),e.external_userid&&r.push({external_userid:{$regex:e.external_userid}})),"ContactInfo"==t&&(e.openid&&r.push({"data.openid":{$regex:e.openid}}),e.userid&&r.push({"data.userid":{$regex:e.userid}}),e.mobile&&r.push({"data.mobile":{$regex:e.mobile}}),e.email&&r.push({"data.email":{$regex:e.email}}),e.external_userid&&r.push({"data.external_userid":{$regex:e.external_userid}}));let o,i=Parse.Query.fromJSON(t,{where:{$or:r}});i.equalTo("company",this.cid),i.equalTo("userid",e.userid);try{o=await i.first()}catch(e){console.error(e)}o?.id||(o=new Parse.Object(t));let a=Parse.User.current();return a?.id&&(o?.get("user")?.id||o.set("user",a.toPointer())),o}async loginPC(){let e=await this.getCorpByCid(this.cid);console.log(this.cid,e);const t=e?.corpId,r=e?.agentId,o=new URL("https://app.fmode.cn"+location.pathname+location?.search);o.searchParams.delete("code"),o.searchParams.delete("state");const i=o.href,a=ww.WWLoginType.corpApp,n=ww.WWLoginRedirectType.callback;return console.log("loginPC1"),createLoginContainer(),new Promise(((e,o)=>{console.log("loginPC2");const s=this.ww.createWWLoginPanel({el:"#ww_login",params:{login_type:a,appid:t,agentid:r,redirect_uri:i,state:"loginState",redirect_type:n},onCheckWeComLogin({isWeComLogin:e}){console.log(e)},onLoginSuccess({code:t}){console.log({code:t}),removeLoginContainer(),e(t)},onLoginFail(e){console.error("lpc",e),removeLoginContainer(),o(e)}});console.log(s,s.el)}))}async oauth(e="snsapi_base",t=!1){t&&localStorage.removeItem(`${this.cid}/USERINFO`),wxdebug("oauth");let r=await this.getCorpByCid(this.cid);console.log(this.cid,r);let o=r?.corpId,i=r?.agentId,a=new URL("https://app.fmode.cn"+location.pathname+location?.search);a.searchParams.delete("code"),a.searchParams.delete("state");let n=encodeURIComponent(a.href),s=`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${o}&redirect_uri=${n}&response_type=code&scope=${e}&state=STATE&agentid=${i}#wechat_redirect`;if("h5"==this.platform()){if(location.host?.startsWith("localhost"))return"";s=`https://login.work.weixin.qq.com/wwlogin/sso/login?login_type=CorpApp&appid=${o}&agentid=${i}&redirect_uri=${n}&state=WWLogin`,console.log("createWWLoginPanel")}return location.href=s,null}async getCorpByCid(e){if(console.log("cid",e),this.corpId)return this.corpId;let t=new Parse.Query("CloudResource"),r=(await t.get(this.companyMap[e]?.corpResId)).get("config");return console.log("config",r),r}async registerCorpWithSuite(e,t,r){if("wxwork"!=this.platform())return!1;if(!r?.length&&this.registerUrl==location.href)return!0;r=r||jsApiList;try{e=e||this.cid,t=t||this.appId;let o=await this.getCorpByCid(e);console.log(o);let i=o?.corpId,a=this.suiteMap[t]?.suiteId;console.log(i,a);let n=new Date,s=new Promise((e=>{ww.register({corpId:o.corpId,suiteId:a,agentId:o.agentId,jsApiList:r,getAgentConfigSignature:async()=>{let e=await this.wecorp.ticket.get();return ww.getSignature({ticket:e,nonceStr:"666",timestamp:(n.getTime()/1e3).toFixed(0),url:location.href})},onAgentConfigSuccess:()=>{this.registerUrl=location.href,e(!0)},onAgentConfigFail:t=>{e(!1)},onConfigFail:t=>{e(!1)}})}));return await s}catch(e){return wxdebug(JSON.stringify(e)),!1}}getUA(){return this.ua=navigator.userAgent.toLowerCase(),this.ua}platform(){return this.ua=navigator.userAgent.toLowerCase(),this.ua.indexOf("wxwork")>-1?"wxwork":this.ua.indexOf("wechat")>-1?"wechat":"h5"}async getContext(){await this.registerCorpWithSuite();try{let e=await ww.getContext();return e?.entry&&(this.entry=e?.entry),e}catch(e){this.entryError="请先注册JSAPI"+e.errCode+e.errMsg,console.error(e)}return null}async getCurrentChat(){let e;if(!await this.registerCorpWithSuite())return null;try{e=await ww.getContext()}catch(e){console.error(e)}let t,r,o,i,a=e?.entry;try{return"group_chat_tools"==a&&(t="chatId",r=(await ww.getCurExternalChat())?.chatId,i=await this.wecorp.externalContact.groupChat.get(r)),"contact_profile"!=a&&"single_chat_tools"!=a||(t="userId",r=(await ww.getCurExternalContact())?.userId,o=await this.wecorp.externalContact.get(r)),this.currentChat={type:t,id:r,group:i?.group_chat,contact:o?.external_contact,follow_user:o?.follow_user},this.currentChat}catch(e){return wxdebug(JSON.stringify(e)),null}}version(){return wxdebug(ww.SDK_VERSION),ww.SDK_VERSION}register(){ww.register({corpId:"ww7ca4776b2a70000",jsApiList:["getExternalContact"],getConfigSignature:this.getConfigSignature})}async getConfigSignature(e){return{timestamp:"",nonceStr:"",signature:""}}config(){}async selectEnterpriseContact(){let e=await ww.selectEnterpriseContact({fromDepartmentId:-1,mode:"multi",type:["department","user"]});console.log(e),wxdebug(e)}}export const jsApiList=["getContext","getCurExternalChat","getCurExternalContact","createCorpGroupChat","updateCorpGroupChat","checkJsApi","onMenuShareAppMessage","onMenuShareWechat","onMenuShareTimeline","shareAppMessage","shareWechatMessage","startRecord","stopRecord","onVoiceRecordEnd","playVoice","pauseVoice","stopVoice","uploadVoice","downloadVoice","chooseImage","previewImage","uploadImage","downloadImage","getNetworkType","openLocation","getLocation","hideOptionMenu","showOptionMenu","hideMenuItems","showMenuItems","hideAllNonBaseMenuItem","showAllNonBaseMenuItem","closeWindow","scanQRCode","previewFile","openEnterpriseChat","selectEnterpriseContact","onHistoryBack","openDefaultBrowser"];
|
|
8
|
+
import*as ww from"@wecom/jssdk";import{FmodeParse}from"../../parse";const Parse=FmodeParse.with("nova");import{WxworkCorp}from"./wxwork.corp";import{wxdebug}from"../wxdebug";import{createLoginContainer,removeLoginContainer}from"./login-modal";export class WxworkSDK{constructor(e){this.companyMap={E4KpGvTEto:{corpResId:"vsVIWbd1i9"},uRiDGwEYLi:{corpResId:"JxfFXBr0ly"},cDL6R1hgSi:{corpResId:"SpL6gyD1Gu"}},this.suiteMap={crm:{suiteId:"dk2559ba758f33d8f5"}},this.ww=ww,this.groupChat={createGroupChat:async e=>{try{return wxdebug("createGroupChat"+JSON.stringify(e)),await this.registerCorpWithSuite()?new Promise(((t,r)=>{e.success=e=>{wxdebug(JSON.stringify(e)),t(e)},e.fail=e=>{wxdebug(JSON.stringify(e)),r(e)},ww.createCorpGroupChat(e)})):null}catch(e){wxdebug(JSON.stringify(e))}},addUserInfoGroup:async e=>{wxdebug("addUserInfoGroup"+JSON.stringify(e));try{let t=await this.registerCorpWithSuite();return wxdebug("isRegister"+t),t?(wxdebug(JSON.stringify(e)),new Promise(((t,r)=>{e.success=e=>{wxdebug(JSON.stringify(e)),t(e)},e.fail=e=>{wxdebug(JSON.stringify(e)),r(e)},ww.updateCorpGroupChat(e)}))):null}catch(e){wxdebug(JSON.stringify(e))}}},this.cid=e?.cid,this.appId=e?.appId,this.wecorp=new WxworkCorp(this.cid)}async syncGroupChat(e){let t=new Parse.Query("GroupChat");t.equalTo("chat_id",e?.chat_id);let r=await t.first();if(r?.id||(r=new Parse.Object("GroupChat")),!r?.get("joinUrl")){let t=(await this.wecorp.externalContact.groupChat.addJoinWay({scene:1,chat_id_list:[e.chat_id]}))?.config_id,o=(await this.wecorp.externalContact.groupChat.getJoinWay(t))?.join_way;r.set("joinUrl",o)}if(!r?.get("joinQrcode")){let t=(await this.wecorp.externalContact.groupChat.addJoinWay({scene:2,chat_id_list:[e.chat_id]}))?.config_id,o=(await this.wecorp.externalContact.groupChat.getJoinWay(t))?.join_way;r.set("joinQrcode",o)}let o={chat_id:e.chat_id,name:e.name,owner:e.owner,notice:e.notice},i=!1;return r.get("chat_id")!=e.chat_id&&(i=!0),r.get("name")!=e.name&&(i=!0),r.get("owner")!=e.owner&&(i=!0),r.get("notice")!=e.notice&&(i=!0),r.get("member_version")!=e.member_version&&(i=!0,o.member_list=e.member_list,o.member_version=e.member_version),r.set(o),this.cid&&r.set("company",{__type:"Pointer",className:"Company",objectId:this.cid}),i&&(r=await r.save()),r}async syncContact(e){const t=e.external_contact||e,r=t.external_userid;let o=new Parse.Query("ContactInfo");o.equalTo("external_userid",r);const i=new Parse.Object("Company");i.id=this.cid,o.equalTo("company",i);let a=await o.first();a?.id||(a=new Parse.Object("ContactInfo"),i?.id&&a.set("company",i.toPointer()));const n=t.name||"",s=t.mobile||"",c={...t,follow_user:e.follow_user||t.follow_user||[]};let l=!1;a.get("external_userid")!=r&&(l=!0),a.get("name")!=n&&n&&(l=!0),a.get("mobile")!=s&&s&&(l=!0);const d=a.get("data");return JSON.stringify(d)!==JSON.stringify(c)&&(l=!0),a.set("external_userid",r),a.set("name",n),a.set("mobile",s),a.set("data",c),wxdebug("syncContact",a.toJSON()),l&&(a=await a.save()),a}async getCurrentChatObject(){const e=await this.getCurrentChat();if(!e)return{currentChat:null};let t,r;try{if("chatId"===e.type&&e.group)t=await this.syncGroupChat(e.group);else if("userId"===e.type&&e.id){const t=await this.wecorp.externalContact.get(e.id);r=await this.syncContact(t)}}catch(e){console.error("getCurrentChatObject error:",e)}return{GroupChat:t,Contact:r,currentChat:e}}async syncUserInfo(e){let t=null;1==e.gender&&(t="男"),2==e.gender&&(t="女");let r=await this.getContactOrProfile(e);console.log("thisUser",r),console.log("userInfo",e),"ContactInfo"==r.className&&r.set({data:{openid:e.openid,userid:e.userid,external_userid:e.external_userid,type:e.type&&String(e.type)||null,name:e.name,avatar:e.avatar,gender:t,qr_code:e.qr_code,mobile:e.mobile,email:e.email,biz_mail:e.biz_mail,address:e.address,position:e.position,corp_name:e.corp_name},type:"wecom-agency",company:{__type:"Pointer",className:"Company",objectId:this.cid}}),"Profile"==r.className&&(r.set({openid:e.openid,userid:e.userid,external_userid:e.external_userid,type:e.type&&String(e.type)||null,name:e.name,avatar:e.avatar,gender:t,qr_code:e.qr_code,mobile:e.mobile,email:e.email,biz_mail:e.biz_mail,address:e.address,position:e.position,corp_name:e.corp_name,company:{__type:"Pointer",className:"Company",objectId:this.cid}}),r.set("data",e));try{if(r?.id&&!r.get("user")?.id){let e=Parse.User.current();e?.id&&r.set("user",e?.toPointer())}r=await r.save()}catch(e){wxdebug(e)}return r}async getUserinfo(e){let t,r;wxdebug("getUserinfo");let o=localStorage.getItem(`${this.cid}/USERINFO`);if(!e){t=JSON.parse(o),t.cid=this.cid,wxdebug("getContactOrProfile");try{r=await this.getContactOrProfile(t)}catch(e){}if(wxdebug("getUserinfo:"+o),t&&r)return t}if(!(e=new URL(location.href).searchParams.get("code")||e))return null;let i=await this.wecorp.auth.getuserinfo(e);if(wxdebug("result"+JSON.stringify(i)),i?.errcode)return console.error(i?.errmsg),null;let a=i?.external_userid;if(a){let e=await this.wecorp.externalContact.get(a);e?.external_contact&&Object.keys(e?.external_contact).forEach((t=>{i[t]=e?.external_contact[t]}))}return i.cid=this.cid,t=i,r=await this.getContactOrProfile(t),r?.get("mobile")&&(t.mobile=r?.get("mobile")),o=JSON.stringify(i),localStorage.setItem(`${this.cid}/USERINFO`,o),t}async getCurrentUser(){const e=await this.getUserinfo();if(!e)return null;return await this.getContactOrProfile(e)}async getContactOrProfile(e){let t;(e.openid||e.external_userid)&&(t="ContactInfo"),e.userid&&(t="Profile");let r=[];"Profile"==t&&(e.openid&&r.push({openid:{$regex:e.openid}}),e.userid&&r.push({userid:{$regex:e.userid}}),e.mobile&&r.push({mobile:{$regex:e.mobile}}),e.email&&r.push({email:{$regex:e.email}}),e.external_userid&&r.push({external_userid:{$regex:e.external_userid}})),"ContactInfo"==t&&(e.openid&&r.push({"data.openid":{$regex:e.openid}}),e.userid&&r.push({"data.userid":{$regex:e.userid}}),e.mobile&&r.push({"data.mobile":{$regex:e.mobile}}),e.email&&r.push({"data.email":{$regex:e.email}}),e.external_userid&&r.push({"data.external_userid":{$regex:e.external_userid}}));let o,i=Parse.Query.fromJSON(t,{where:{$or:r}});i.equalTo("company",this.cid),i.equalTo("userid",e.userid);try{o=await i.first()}catch(e){console.error(e)}o?.id||(o=new Parse.Object(t));let a=Parse.User.current();return a?.id&&(o?.get("user")?.id||o.set("user",a.toPointer())),o}async loginPC(){let e=await this.getCorpByCid(this.cid);console.log(this.cid,e);const t=e?.corpId,r=e?.agentId,o=new URL("https://app.fmode.cn"+location.pathname+location?.search);o.searchParams.delete("code"),o.searchParams.delete("state");const i=o.href,a=ww.WWLoginType.corpApp,n=ww.WWLoginRedirectType.callback;console.log("loginPC1");return document.getElementById("ww_login")||createLoginContainer(),new Promise(((e,o)=>{console.log("loginPC2");const s=this.ww.createWWLoginPanel({el:"#ww_login",params:{login_type:a,appid:t,agentid:r,redirect_uri:i,state:"loginState",redirect_type:n},onCheckWeComLogin({isWeComLogin:e}){console.log(e)},onLoginSuccess({code:t}){console.log({code:t}),removeLoginContainer(),e(t)},onLoginFail(e){console.error("lpc",e),removeLoginContainer(),o(e)}});console.log(s,s.el)}))}async oauth(e="snsapi_base",t=!1){t&&localStorage.removeItem(`${this.cid}/USERINFO`),wxdebug("oauth");let r=await this.getCorpByCid(this.cid);console.log(this.cid,r);let o=r?.corpId,i=r?.agentId,a=new URL("https://app.fmode.cn"+location.pathname+location?.search);a.searchParams.delete("code"),a.searchParams.delete("state");let n=encodeURIComponent(a.href),s=`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${o}&redirect_uri=${n}&response_type=code&scope=${e}&state=STATE&agentid=${i}#wechat_redirect`;if("h5"==this.platform()){if(location.host?.startsWith("localhost"))return"";s=`https://login.work.weixin.qq.com/wwlogin/sso/login?login_type=CorpApp&appid=${o}&agentid=${i}&redirect_uri=${n}&state=WWLogin`,console.log("createWWLoginPanel")}return location.href=s,null}async getCorpByCid(e){if(console.log("cid",e),this.corpId)return this.corpId;let t=new Parse.Query("CloudResource"),r=(await t.get(this.companyMap[e]?.corpResId)).get("config");return console.log("config",r),r}async registerCorpWithSuite(e,t,r){if("wxwork"!=this.platform())return!1;if(!r?.length&&this.registerUrl==location.href)return!0;r=r||jsApiList;try{e=e||this.cid,t=t||this.appId;let o=await this.getCorpByCid(e);console.log(o);let i=o?.corpId,a=this.suiteMap[t]?.suiteId;console.log(i,a);let n=new Date,s=new Promise((e=>{ww.register({corpId:o.corpId,suiteId:a,agentId:o.agentId,jsApiList:r,getAgentConfigSignature:async()=>{let e=await this.wecorp.ticket.get();return ww.getSignature({ticket:e,nonceStr:"666",timestamp:(n.getTime()/1e3).toFixed(0),url:location.href})},onAgentConfigSuccess:()=>{this.registerUrl=location.href,e(!0)},onAgentConfigFail:t=>{e(!1)},onConfigFail:t=>{e(!1)}})}));return await s}catch(e){return wxdebug(JSON.stringify(e)),!1}}getUA(){return this.ua=navigator.userAgent.toLowerCase(),this.ua}platform(){return this.ua=navigator.userAgent.toLowerCase(),this.ua.indexOf("wxwork")>-1?"wxwork":this.ua.indexOf("wechat")>-1?"wechat":"h5"}async getContext(){await this.registerCorpWithSuite();try{let e=await ww.getContext();return e?.entry&&(this.entry=e?.entry),e}catch(e){this.entryError="请先注册JSAPI"+e.errCode+e.errMsg,console.error(e)}return null}async getCurrentChat(){let e;if(!await this.registerCorpWithSuite())return null;try{e=await ww.getContext()}catch(e){console.error(e)}let t,r,o,i,a=e?.entry;try{return"group_chat_tools"==a&&(t="chatId",r=(await ww.getCurExternalChat())?.chatId,i=await this.wecorp.externalContact.groupChat.get(r)),"contact_profile"!=a&&"single_chat_tools"!=a||(t="userId",r=(await ww.getCurExternalContact())?.userId,o=await this.wecorp.externalContact.get(r)),this.currentChat={type:t,id:r,group:i?.group_chat,contact:o?.external_contact,follow_user:o?.follow_user},this.currentChat}catch(e){return wxdebug(JSON.stringify(e)),null}}version(){return wxdebug(ww.SDK_VERSION),ww.SDK_VERSION}register(){ww.register({corpId:"ww7ca4776b2a70000",jsApiList:["getExternalContact"],getConfigSignature:this.getConfigSignature})}async getConfigSignature(e){return{timestamp:"",nonceStr:"",signature:""}}config(){}async selectEnterpriseContact(){let e=await ww.selectEnterpriseContact({fromDepartmentId:-1,mode:"multi",type:["department","user"]});console.log(e),wxdebug(e)}}export const jsApiList=["getContext","getCurExternalChat","getCurExternalContact","createCorpGroupChat","updateCorpGroupChat","checkJsApi","onMenuShareAppMessage","onMenuShareWechat","onMenuShareTimeline","shareAppMessage","shareWechatMessage","startRecord","stopRecord","onVoiceRecordEnd","playVoice","pauseVoice","stopVoice","uploadVoice","downloadVoice","chooseImage","previewImage","uploadImage","downloadImage","getNetworkType","openLocation","getLocation","hideOptionMenu","showOptionMenu","hideMenuItems","showMenuItems","hideAllNonBaseMenuItem","showAllNonBaseMenuItem","closeWindow","scanQRCode","previewFile","openEnterpriseChat","selectEnterpriseContact","onHistoryBack","openDefaultBrowser"];
|
|
9
9
|
var MODULE_PATH_NEED = `6K+l5paH5Lu25piv5pys6aG555uu55qE5LiA6YOo5YiGIFRoaXMgZmlsZSBpcyBwYXJ0IG9mIHRoZSBDb21wb25lbnRzIGluIEZtb2RlIEluYy4KICAgIOeJiOadg+aJgOaciSDCqSDmnKrmnaXpo57pqawgwqkg5rGf6KW/6ISR5o6n56eR5oqA5pyJ6ZmQ5YWs5Y+4IENvcHlyaWdodCDCqSBGbW9kZSBUZWNobm9sb2d5IENvLiwgTHRkLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCBSaWdodHMgUmVzZXJ2ZWQuCiAgICDkuKXnpoHlnKjmnKrnu4/mjojmnYPnmoTmg4XlhrXkuIvvvIzpgJrov4fku7vkvZXlqpLku4vlpI3liLbmraTmlofku7YgVW5hdXRob3JpemVkIGNvcHlpbmcgb2YgdGhpcyBmaWxlLCB2aWEgYW55IG1lZGl1bSBpcyBzdHJpY3RseSBwcm9oaWJpdGVkCiAgICDor6Xmlofku7bmmK/kuJPmnInnmoTmnLrlr4bmlofku7YgUHJvcHJpZXRhcnkgYW5kIGNvbmZpZGVudGlhbAogICAKICAgIENvcHlyaWdodCAyMDIxLW5vdyBGbW9kZSBJbmMuIHN1cHBvcnRAZm1vZGUuY24uIDE4NjA3MDA3MDczLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCByaWdodHMgcmVzZXJ2ZWQuCgogICAgUEFUSDovaG9tZS9yeWFuL3dvcmtzcGFjZS9ub3ZhL25vdmEtYWRtaW4vZGlzdC9mbW9kZS1uZy9lc20yMDIyL2xpYi9jb3JlL3NvY2lhbC93eHdvcmsvd3h3b3JrLnNkay5tanM=`
|
|
10
10
|
|