企业微信小程序中企业微信侧边栏授权获取userid
参考链接https://segmentfault.com/a/1190000041195921
1.在index.html中引入微信的JS SDK,还要引入一个 jwxwork sdk
<!-- 需要调用JS接口的页面引入如下JS文件 --> <!-- <script src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js" type="text/javascript"></script> --> <!-- <script src="https://res.wx.qq.com/wwopen/js/jsapi/jweixin-1.0.0.js" type="text/javascript"></script> --> <script charset="utf-8"> !function(e,t){"function"!=typeof define||!define.amd&&!define.cmd||window.requirejs?t(e,!0):define("js/jsapi/jweixin-1.0.0",[],function(){return t(e)})}(this,(function(e,t){if(!e.jWeixin){var n,i={config:"preVerifyJSAPI",onMenuShareTimeline:"menu:share:timeline",onMenuShareAppMessage:"menu:share:appmessage",onMenuShareWechat:"menu:share:wechat",onMenuShareQQ:"menu:share:qq",onMenuShareWeibo:"menu:share:weiboApp",onMenuShareQZone:"menu:share:QZone",previewImage:"imagePreview",previewFile:"previewFile",getLocation:"geoLocation",openProductSpecificView:"openProductViewWithPid",addCard:"batchAddCard",openCard:"batchViewCard",chooseWXPay:"getBrandWCPayRequest",openEnterpriseRedPacket:"getRecevieBizHongBaoRequest",startSearchBeacons:"startMonitoringBeacons",stopSearchBeacons:"stopMonitoringBeacons",onSearchBeacons:"onBeaconsInRange",consumeAndShareCard:"consumedShareCard",openAddress:"editAddress",getUserOpenID:"getUserOpenID",getBrandWCPayRequest:"getBrandWCPayRequest",notifyNativeEvent:"notifyNativeEvent"},o=function(){var e={};for(var t in i)e[i[t]]=t;return e}(),r=e.document,a=r.title,c=navigator.userAgent.toLowerCase(),s=navigator.platform.toLowerCase(),d=!(!s.match("mac")&&!s.match("win")),u=-1!=c.indexOf("wxdebugger"),f=-1!=c.indexOf("wxwork"),p=-1!=c.indexOf("android"),l=-1!=c.indexOf("iphone")||-1!=c.indexOf("ipad"),g=(n=c.match(/wxwork\/(\d+\.\d+\.\d+)/)||c.match(/wxwork\/(\d+\.\d+)/))?n[1]:"",m={initStartTime:x(),initEndTime:0,preVerifyStartTime:0,preVerifyEndTime:0},v={version:"1.0.0",appId:"",initTime:0,preVerifyTime:0,networkType:"",isPreVerifyOk:1,systemType:l?1:p?2:-1,clientVersion:g,url:encodeURIComponent(location.href)},h={},S={},y={_completes:[]},I={state:0,data:{}};M((function(){m.initEndTime=x()}));var w={config:function(e){h=e,L("config",e);var t=!1!==h.check;M((function(){if(t)C(i.config,{verifyJsApiList:W(h.jsApiList)},function(){y._complete=function(e){m.preVerifyEndTime=x(),I.state=1,I.data=e},y.success=function(e){v.isPreVerifyOk=0},y.fail=function(e){y._fail?y._fail(e):I.state=-1};var e=y._completes;return e.push((function(){!function(e){if(!u&&!h.debug){var t=new Image;if(v.appId=h.appId,v.initTime=m.initEndTime-m.initStartTime,v.preVerifyTime=m.preVerifyEndTime-m.preVerifyStartTime,d){var n="https://open.work.weixin.qq.com/wwopen/report/sdk?v="+v.version+"&o="+v.isPreVerifyOk+"&cv="+v.clientVersion+"&appid="+v.appId+"&nt=pc&it="+v.initTime+"&pt="+v.preVerifyTime+"&url="+v.url;t.src=n}else w.getNetworkType({isInnerInvoke:!0,success:function(e){v.networkType=e.networkType;var n="https://open.work.weixin.qq.com/wwopen/report/sdk?v="+v.version+"&o="+v.isPreVerifyOk+"&cv="+v.clientVersion+"&appid="+v.appId+"&nt="+v.networkType+"&it="+v.initTime+"&pt="+v.preVerifyTime+"&url="+v.url;t.src=n}})}}()})),y.complete=function(t){for(var n=0,i=e.length;n<i;++n)e[n]();y._completes=[]},y}()),m.preVerifyStartTime=x();else{I.state=1;for(var e=y._completes,n=0,o=e.length;n<o;++n)e[n]();y._completes=[]}})),h.beta&&D()},agentConfig:function(t){M((function(){if(t=t||{},e.WeixinJSBridge){D();var n={corpid:t.corpid+"",agentid:t.agentid+"",timestamp:t.timestamp+"",nonceStr:t.nonceStr+"",signature:t.signature+"",jsApiList:W(t.jsApiList)};WeixinJSBridge.invoke("agentConfig",_(n),(function(i){S=n,e.WWOpenData?T("agentConfig",i,t):e.WeixinSandBox||s.match("win")?WeixinJSBridge.invoke("wwapp.initWwOpenData",_({}),(function(){T("agentConfig",i,t)})):T("agentConfig",i,t)}))}else L("agentConfig",t)}))},ready:function(e){0!=I.state?e():(y._completes.push(e),!f&&h.debug&&e())},error:function(e){-1==I.state?e(I.data):y._fail=e},checkJsApi:function(e){C("checkJsApi",{jsApiList:W(e.jsApiList)},(e._complete=function(e){if(p){var t=e.checkResult;t&&(e.checkResult=JSON.parse(t))}e=function(e){var t=e.checkResult;for(var n in t){var i=o[n];i&&(t[i]=t[n],delete t[n])}return e}(e)},e))},onMenuShareTimeline:function(e){B(i.onMenuShareTimeline,{complete:function(){C("shareTimeline",{title:e.title||a,desc:e.title||a,img_url:e.imgUrl||"",link:e.link||location.href,type:e.type||"link",data_url:e.dataUrl||""},e)}},e)},onMenuShareAppMessage:function(e){B(i.onMenuShareAppMessage,{complete:function(){C("sendAppMessage",{title:e.title||a,desc:e.desc||"",link:e.link||location.href,img_url:e.imgUrl||"",type:e.type||"link",data_url:e.dataUrl||"",finder_feed:e.finder_feed,finder_topic:e.finder_topic,finder_profile:e.finder_profile},e)}},e)},onMenuShareWechat:function(e){B(i.onMenuShareWechat,{complete:function(){C("shareWechat",{title:e.title||a,desc:e.desc||"",link:e.link||location.href,img_url:e.imgUrl||"",type:e.type||"link",data_url:e.dataUrl||""},e)}},e)},onMenuShareQQ:function(e){B(i.onMenuShareQQ,{complete:function(){C("shareQQ",{title:e.title||a,desc:e.desc||"",img_url:e.imgUrl||"",link:e.link||location.href},e)}},e)},onMenuShareWeibo:function(e){B(i.onMenuShareWeibo,{complete:function(){C("shareWeiboApp",{title:e.title||a,desc:e.desc||"",img_url:e.imgUrl||"",link:e.link||location.href},e)}},e)},onMenuShareQZone:function(e){B(i.onMenuShareQZone,{complete:function(){C("shareQZone",{title:e.title||a,desc:e.desc||"",img_url:e.imgUrl||"",link:e.link||location.href},e)}},e)},startRecord:function(e){C("startRecord",{},e)},stopRecord:function(e){C("stopRecord",{},e)},onVoiceRecordEnd:function(e){B("onVoiceRecordEnd",e)},playVoice:function(e){C("playVoice",{localId:e.localId},e)},pauseVoice:function(e){C("pauseVoice",{localId:e.localId},e)},stopVoice:function(e){C("stopVoice",{localId:e.localId},e)},onVoicePlayEnd:function(e){B("onVoicePlayEnd",e)},uploadVoice:function(e){C("uploadVoice",{localId:e.localId,isShowProgressTips:0==e.isShowProgressTips?0:1},e)},downloadVoice:function(e){C("downloadVoice",{serverId:e.serverId,isShowProgressTips:0==e.isShowProgressTips?0:1},e)},translateVoice:function(e){C("translateVoice",{localId:e.localId,isShowProgressTips:0==e.isShowProgressTips?0:1},e)},chooseImage:function(e){C("chooseImage",{scene:"1|2",count:e.count||9,sizeType:e.sizeType||["original","compressed"],sourceType:e.sourceType||["album","camera"],defaultCameraMode:e.defaultCameraMode||"normal",isSaveToAlbum:0==e.isSaveToAlbum?0:1},(e._complete=function(e){if(p){var t=e.localIds;t&&(e.localIds=JSON.parse(t))}},e))},previewImage:function(e){C(i.previewImage,{current:e.current,urls:e.urls},e)},uploadImage:function(e){C("uploadImage",{localId:e.localId,isShowProgressTips:0==e.isShowProgressTips?0:1},e)},downloadImage:function(e){C("downloadImage",{serverId:e.serverId,isShowProgressTips:0==e.isShowProgressTips?0:1},e)},getLocalImgData:function(e){C("getLocalImgData",{localId:e.localId,success:e.success},e)},previewFile:function(e){C("previewFile",{url:e.url,name:e.name,size:e.size},e)},getNetworkType:function(e){C("getNetworkType",{},(e._complete=function(e){e=function(e){var t=e.errMsg;e.errMsg="getNetworkType:ok";var n=e.subtype;if(delete e.subtype,n)e.networkType=n;else{var i=t.indexOf(":"),o=t.substring(i+1);switch(o){case"wifi":case"edge":case"wwan":e.networkType=o;break;default:e.errMsg="getNetworkType:fail"}}return e}(e)},e))},openLocation:function(e){C("openLocation",{latitude:e.latitude,longitude:e.longitude,name:e.name||"",address:e.address||"",scale:e.scale||28,infoUrl:e.infoUrl||""},e)},getLocation:function(e){e=e||{},C(i.getLocation,{type:e.type||"wgs84"},(e._complete=function(e){delete e.type},e))},hideOptionMenu:function(e){C("hideOptionMenu",{},e)},showOptionMenu:function(e){C("showOptionMenu",{},e)},closeWindow:function(e){C("closeWindow",{},e=e||{})},hideMenuItems:function(e){C("hideMenuItems",{menuList:e.menuList},e)},showMenuItems:function(e){C("showMenuItems",{menuList:e.menuList},e)},hideAllNonBaseMenuItem:function(e){C("hideAllNonBaseMenuItem",{},e)},showAllNonBaseMenuItem:function(e){C("showAllNonBaseMenuItem",{},e)},scanQRCode:function(e){C("scanQRCode",{needResult:(e=e||{}).needResult||0,scanType:e.scanType||["qrCode","barCode"]},(e._complete=function(e){if(l){var t=e.resultStr;if(t){var n=JSON.parse(t);e.resultStr=n&&n.scan_code&&n.scan_code.scan_result}}},e))},openAddress:function(e){C(i.openAddress,{},(e._complete=function(e){e=function(e){return e.postalCode=e.addressPostalCode,delete e.addressPostalCode,e.provinceName=e.proviceFirstStageName,delete e.proviceFirstStageName,e.cityName=e.addressCitySecondStageName,delete e.addressCitySecondStageName,e.countryName=e.addressCountiesThirdStageName,delete e.addressCountiesThirdStageName,e.detailInfo=e.addressDetailInfo,delete e.addressDetailInfo,e}(e)},e))},openProductSpecificView:function(e){C(i.openProductSpecificView,{pid:e.productId,view_type:e.viewType||0,ext_info:e.extInfo},e)},addCard:function(e){for(var t=e.cardList,n=[],o=0,r=t.length;o<r;++o){var a=t[o],c={card_id:a.cardId,card_ext:a.cardExt};n.push(c)}C(i.addCard,{card_list:n},(e._complete=function(e){var t=e.card_list;if(t){for(var n=0,i=(t=JSON.parse(t)).length;n<i;++n){var o=t[n];o.cardId=o.card_id,o.cardExt=o.card_ext,o.isSuccess=!!o.is_succ,delete o.card_id,delete o.card_ext,delete o.is_succ}e.cardList=t,delete e.card_list}},e))},chooseCard:function(e){C("chooseCard",{app_id:h.appId,location_id:e.shopId||"",sign_type:e.signType||"SHA1",card_id:e.cardId||"",card_type:e.cardType||"",card_sign:e.cardSign,time_stamp:e.timestamp+"",nonce_str:e.nonceStr},(e._complete=function(e){e.cardList=e.choose_card_info,delete e.choose_card_info},e))},openCard:function(e){for(var t=e.cardList,n=[],o=0,r=t.length;o<r;++o){var a=t[o],c={card_id:a.cardId,code:a.code};n.push(c)}C(i.openCard,{card_list:n},e)},consumeAndShareCard:function(e){C(i.consumeAndShareCard,{consumedCardId:e.cardId,consumedCode:e.code},e)},chooseWXPay:function(e){C(i.chooseWXPay,k(e),e)},openEnterpriseRedPacket:function(e){C(i.openEnterpriseRedPacket,k(e),e)},startSearchBeacons:function(e){C(i.startSearchBeacons,{ticket:e.ticket},e)},stopSearchBeacons:function(e){C(i.stopSearchBeacons,{},e)},onSearchBeacons:function(e){B(i.onSearchBeacons,e)},openEnterpriseChat:function(e){C("openEnterpriseChat",{useridlist:e.userIds,chatname:e.groupName,userIds:e.userIds,groupName:e.groupName,openIds:e.openIds,chatId:e.chatId,externalUserIds:e.externalUserIds},e)},getUserOpenID:function(e){C("fetchWXOpenIdRequest",{},e)},getBrandWCPayRequest:function(e){C("getBrandWCPayRequest",{appId:e.appId,partnerId:e.partnerId,openId:e.openId,timeStamp:e.timeStamp,nonceStr:e.nonceStr,package:e.package,signType:e.signType,paySign:e.paySign},e)},onHistoryBack:function(e){B("onHistoryBack",{complete:function(){if("function"==typeof e)try{var t=e();if(!1===t||0===t)return!1}catch(e){}return C("historyBack"),!1}})},notifyNativeEvent:function(e){C("notifyNativeEvent",{event:e.event,resultCode:e.resultCode,data:e.data},e)},openBluetoothAdapter:function(e){C("openBluetoothAdapter",{},e)},closeBluetoothAdapter:function(e){C("closeBluetoothAdapter",{},e)},getBluetoothAdapterState:function(e){C("getBluetoothAdapterState",{},e)},onBluetoothAdapterStateChange:function(e){B("onBluetoothAdapterStateChange",{complete:function(t){"function"==typeof e&&e(t)}})},startBluetoothDevicesDiscovery:function(e){C("startBluetoothDevicesDiscovery",{services:e.services||[],allowDuplicatesKey:e.allowDuplicatesKey||!1,interval:e.interval||0},e)},stopBluetoothDevicesDiscovery:function(e){C("stopBluetoothDevicesDiscovery",{},e)},getBluetoothDevices:function(e){C("getBluetoothDevices",{},{success:function(t){if(t&&t.devices)for(var n=0;n<t.devices.length;n++)t.devices[n].advertisData=E(t.devices[n].advertisData),t.devices[n].serviceData=E(t.devices[n].serviceData);"function"==typeof e.success&&e.success(t)},fail:e.fail,complete:e.complete})},onBluetoothDeviceFound:function(e){B("onBluetoothDeviceFound",{complete:function(t){if(t&&t.devices)for(var n=0;n<t.devices.length;n++)t.devices[n].advertisData=E(t.devices[n].advertisData);"function"==typeof e&&e(t?t.devices:t)}})},getConnectedBluetoothDevices:function(e){C("getConnectedBluetoothDevices",{services:e.services},e)},createBLEConnection:function(e){C("createBLEConnection",{deviceId:e.deviceId},e)},closeBLEConnection:function(e){C("closeBLEConnection",{deviceId:e.deviceId},e)},onBLEConnectionStateChange:function(e){B("onBLEConnectionStateChange",{complete:function(t){"function"==typeof e&&e(t)}})},getBLEDeviceServices:function(e){C("getBLEDeviceServices",{deviceId:e.deviceId},e)},readBLECharacteristicValue:function(e){C("readBLECharacteristicValue",{deviceId:e.deviceId,serviceId:e.serviceId,characteristicId:e.characteristicId},e)},getBLEDeviceCharacteristics:function(e){C("getBLEDeviceCharacteristics",{deviceId:e.deviceId,serviceId:e.serviceId},e)},writeBLECharacteristicValue:function(t){"object"==typeof t&&t.value&&(t.value=function(t){if(void 0===t)return t;for(var n="",i=new Uint8Array(t),o=i.byteLength,r=0;r<o;r++)n+=String.fromCharCode(i[r]);return e.btoa(n)}(t.value)),C("writeBLECharacteristicValue",{deviceId:t.deviceId,serviceId:t.serviceId,characteristicId:t.characteristicId,value:t.value},t)},notifyBLECharacteristicValueChange:function(e){C("notifyBLECharacteristicValueChange",{deviceId:e.deviceId,serviceId:e.serviceId,characteristicId:e.characteristicId,state:e.state},e)},onBLECharacteristicValueChange:function(e){B("onBLECharacteristicValueChange",{complete:function(t){t&&t.value&&(t.value=E(t.value)),"function"==typeof e&&e(t)}})},startBeaconDiscovery:function(e){C("startBeaconDiscovery",{uuids:e.uuids},e)},stopBeaconDiscovery:function(e){C("stopBeaconDiscovery",{},e)},getBeacons:function(e){C("getBeacons",{},e)},onBeaconUpdate:function(e){B("onBeaconUpdate",{complete:function(t){"function"==typeof e&&e(t)}})},onBeaconServiceChange:function(e){B("onBeaconServiceChange",{complete:function(t){"function"==typeof e&&e(t)}})},startWifi:function(e){C("startWifi",{},e)},stopWifi:function(e){C("stopWifi",{},e)},connectWifi:function(e){var t={SSID:e.SSID,BSSID:e.BSSID};e.hasOwnProperty("password")&&(t.password=e.password),C("connectWifi",t,e)},getWifiList:function(e){C("getWifiList",{},e)},onGetWifiList:function(e){B("onGetWifiList",{complete:function(t){"function"==typeof e&&e(t)}})},onWifiConnected:function(e){B("onWifiConnected",{complete:function(t){"function"==typeof e&&e(t)}})},getConnectedWifi:function(e){C("getConnectedWifi",{},e)},setClipboardData:function(e){C("setClipboardData",{data:e.data},e)},getClipboardData:function(e){C("getClipboardData",{},e)},onNetworkStatusChange:function(e){B("onNetworkStatusChange",{complete:function(t){"function"==typeof e&&e(t)}})},onLocationChange:function(e){B("auto:location:report",{complete:function(t){"function"==typeof e&&e(t)}})},onUserCaptureScreen:function(e){B("onUserCaptureScreen",{complete:function(t){"function"==typeof e&&e(t)}})},onKeyboardChange:function(e){B("onKeyboardChange",{complete:function(t){"function"==typeof e&&e(t)}})}};return t&&(e.wx=e.jWeixin=w),w}function C(t,n,i){e.WeixinJSBridge?WeixinJSBridge.invoke(t,_(n),(function(e){T(t,e,i)})):L(t,i)}function B(t,n,i){e.WeixinJSBridge?WeixinJSBridge.on(t,(function(e){i&&i.trigger&&i.trigger(e),T(t,e,n)})):L(t,i||n)}function _(e){e=e||{};var t={};return h.appId?t=h:S.corpid&&((t=S).appId=S.corpid),e.appId=t.appId,e.verifyAppId=t.appId,e.verifySignType="sha1",e.verifyTimestamp=t.timestamp+"",e.verifyNonceStr=t.nonceStr,e.verifySignature=t.signature,e}function k(e){return{timeStamp:e.timestamp+"",nonceStr:e.nonceStr,package:e.package,paySign:e.paySign,signType:e.signType||"SHA1"}}function T(e,t,n){"openEnterpriseChat"==e&&(t.errCode=t.err_code),delete t.err_code,delete t.err_desc,delete t.err_detail;var i=t.errMsg;i||(i=t.err_msg,delete t.err_msg,i=function(e,t){var n=e,i=o[n];i&&(n=i);var r="ok";if(t){var a=t.indexOf(":");"confirm"==(r=t.substring(a+1))&&(r="ok"),"failed"==r&&(r="fail"),-1!=r.indexOf("failed_")&&(r=r.substring(7)),-1!=r.indexOf("fail_")&&(r=r.substring(5)),"access denied"!=(r=(r=r.replace(/_/g," ")).toLowerCase())&&"no permission to execute"!=r||(r="permission denied"),"config"==n&&"function not exist"==r&&(r="ok"),""==r&&(r="fail")}return t=n+":"+r}(e,i),t.errMsg=i),(n=n||{})._complete&&(n._complete(t),delete n._complete),i=t.errMsg||"",h.debug&&!n.isInnerInvoke&&alert(JSON.stringify(t));var r=i.indexOf(":");switch(i.substring(r+1)){case"ok":n.success&&n.success(t);break;case"cancel":n.cancel&&n.cancel(t);break;default:n.fail&&n.fail(t)}n.complete&&n.complete(t)}function W(e){if(e){for(var t=0,n=e.length;t<n;++t){var o=e[t],r=i[o];r&&(e[t]=r)}return e}}function L(e,t){if(!(!h.debug||t&&t.isInnerInvoke)){var n=o[e];n&&(e=n),t&&t._complete&&delete t._complete,console.log('"'+e+'",',t||"")}}function x(){return(new Date).getTime()}function M(t){f&&(e.WeixinJSBridge?t():r.addEventListener&&r.addEventListener("WeixinJSBridgeReady",t,!1))}function D(){w.invoke||(w.invoke=function(t,n,i){e.WeixinJSBridge&&WeixinJSBridge.invoke(t,_(n),i)},w.on=function(t,n){e.WeixinJSBridge&&WeixinJSBridge.on(t,n)})}function E(t){if(void 0===t)return t;for(var n=e.atob(t),i=n.length,o=new Uint8Array(i),r=0;r<i;r++)o[r]=n.charCodeAt(r);return o.buffer}})),function(e){var t=e.encodeURIComponent;try{e.wwperf={config:function(t){var i=e.performance,o=e.document,r=i.timing,a=!1;try{if(!e.navigator.userAgent.toLowerCase().match("wxwork"))return;e.WeixinJSBridge?c():o.addEventListener("WeixinJSBridgeReady",c,!1),"complete"===o.readyState?c():e.addEventListener("load",c,!1)}catch(e){return n(e,t)}function c(){try{e.WeixinJSBridge&&"complete"===o.readyState&&e.setTimeout(s,0)}catch(e){return n(e,t)}}function s(){try{if(a)return;a=!0;var o=-1;i.navigation&&null!=i.navigation.redirectCount&&(o=i.navigation.redirectCount);var c=-1;if("function"==typeof i.getEntries){var s=i.getEntries();c=0;for(var d=s.length-1;d>=0;d--)"resource"==s[d].entryType&&(c+=1)}e.WeixinJSBridge.invoke("innerSaveWebPerformance",{perf_data:JSON.stringify({env:t,url:e.location.href,redirect_count:o,resource_count:c,dns_time:Math.max(0,r.domainLookupEnd-r.domainLookupStart),connect_time:Math.max(0,r.connectEnd-r.connectStart),request_time:Math.max(0,r.responseEnd-r.requestStart),loading_time:Math.max(0,r.domLoading-r.navigationStart),interactive_time:Math.max(0,r.domInteractive-r.navigationStart),complete_time:Math.max(0,r.domComplete-r.navigationStart),log_time:Math.floor(+new Date/1e3)})},(function(){}))}catch(e){return n(e,t)}}}}}catch(e){return n(e)}function n(n,i){try{var o=new e.Image,r=JSON.stringify({msg:n.message,stack:n.stack,env:i});o.src="//badjs2.qq.com/badjs?id=1385&uin=1008&from="+t(e.location.href)+"&msg[0]="+t(r)+"&target[0]=wwperf.js&level[0]=4&count=1&_t="+ +new Date}catch(n){}}}(this); </script> <!--调用 wx.agentConfig需要引入 jwxwork sdk--> <script src="https://open.work.weixin.qq.com/wwopen/js/jwxwork-1.0.0.js"></script>
2.主要认证企业微信的组件,重点是 wx.config和wx.agentConfig在app.vue中
<script> import { AppModel } from '@/models/qyApp.js'; const appModel = new AppModel(); export default { onLaunch: function() { this.getLogin(); }, onShow: function() { wx.showShareMenu({ menus: ['shareAppMessage', 'shareTimeline'], success(res) { console.log(res); }, fail(e) { console.log(e); } }); }, onHide: function() {}, methods: { async getLogin() { // 判断是企业微信小程序 还是 微信小程序 const sinfo = uni.getSystemInfoSync(); // 微信认证 // this.getCurLogin() this.wxConfigAuth(); }, // 进行微信认证 wxConfigAuth() { let that = this; console.log(that,"that") let params = { // 获取当前网页url url: that.getProjectPath().split('#')[0] }; console.log(that.getProjectPath().split('#')[0],"url") // 调用后台获取签名,以及wx.config需要的参数。 appModel .getAgentTicket({ url: that.getProjectPath().split('#')[0], agentId: '1000017' }) .then(res => { console.log('jWeixin',jWeixin); console.log('wx',wx); jWeixin.agentConfig({ corpid: 'wxe12f61e3d2a93b72', // 必填,企业微信的corpid,必须与当前登录的企业一致 agentid: '1000017', // 必填,企业微信的应用id (e.g. 1000247) timestamp: res.data.timestamp, // 必填,生成签名的时间戳 nonceStr: res.data.nonceStr, // 必填,生成签名的随机串 signature: res.data.signature, // 必填,签名,见附录-JS-SDK使用权限签名算法 jsApiList: ['getContext', 'getCurExternalContact','sendChatMessage'], //必填,传入需要使用的接口名称 success: () => { that.getContent(); }, fail: function(res) { console.log("微信认证失败,联系管理3员!agentConfig") console.log(res) // that.errPageShow('微信认证失败,联系管理员!'); if (res.errMsg.indexOf('function not exist') > -1) { alert('版本过低请升级'); } } }); }).catch(err => { console.log(err); //错误处理 console.log("获取签名失败,联系管理员!catch") // that.errPageShow('获取签名失败,联系管理员!'); }); }, // 获取进入H5页面的入口环境 getContent() { let that = this; console.log(that,"that") jWeixin.invoke('getContext', {}, function(res) { if (res.err_msg === 'getContext:ok') { // 从单聊会话的工具栏进入 single_chat_tools that.entry = res.entry; if (that.entry === 'single_chat_tools') { // that.getCurExternalContact(); that.getCurLogin() } else if(that.entry ==='group_chat_tools'){ // that.getCurExternalContact(); that.getCurLogin() }else { //错误处理 console.log("请在企业微信中打开该网址getContent") // that.errPageShow('请在企业微信中打开该网址!'); } // shareTicket = res.shareTicket; //可用于调用getShareInfo接口 } else { console.log("获取环境失败,联系管理员") // that.errPageShow('获取环境失败,联系管理员!'); return false; } }); }, getCurLogin(){ let that=this console.log(that.getProjectPath()) let urlTag=encodeURIComponent(that.getProjectPath().split('//')[1].split('#/')[0]) if(that.getProjectPath().indexOf('code=')!=-1){ console.log('CUNZAI') console.log('CODE',that.getProjectPath().split('code=')[1].split('&')[0]) appModel.getUserId({ auth_code:that.getProjectPath().split('code=')[1].split('&')[0], agentId:"1000017" }).then(res=>{ console.log("userid",res) uni.setStorageSync('userId', res.data.userId); }) }else{ console.log('BUCUNZAI') location.href='https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe12f61e3d2a93b72&redirect_uri='+urlTag+'&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect' } console.log(that.getProjectPath()) }, // 企业微信获取当前客户userId getCurExternalContact() { let that = this; jWeixin.invoke('getCurExternalContact', {}, function(res) { if (res.err_msg == 'getCurExternalContact:ok') { //返回当前外部联系人userId let userId = res.userId; that.wxUserId = userId; uni.setStorageSync('userId', userId); sessionStorage.setItem('wxUserId', userId); console.log('wxUserId', userId); // 授权后,根据scrmAuthStatus 判断接下来的操作 that.getWxUserIdAfter(userId); } else { that.$message.warning('获取企业微信userID失败,请联系管理员'); } }); }, // 得到当前项目的url getProjectPath() { let url = window.self.location.toString(); // console.log("getProjectPath",url) return url; } } }; </script> <style lang="scss"> @import '@/uni_modules/uview-ui/index.scss'; /*每个页面公共css */ @font-face { font-family: texticons; font-weight: normal; font-style: normal; src: url('https://at.alicdn.com/t/font_2782227_0dlgp26jspm.ttf?t=1642559133296') format('truetype'); } .icon { width: 24rpx; height: 24rpx; font-size: 30rpx; color: #808080; font-weight: 800; text-align: center; font-family: texticons; } .searchBar .u-icon__icon, .u-icon { font-size: 40rpx !important; color: #909399 !important; } </style>
标签:
微信小程序
posted on 2022-04-20 17:34 樱桃小王子yummy 阅读(703) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY