UNPKG

h5-cli

Version:

hello

85 lines (74 loc) 3.27 kB
"use strict"; /** \u5fae\u4fe1OpenId\u6a21\u5757 .isOpenId() //\u662f\u5426\u5df2\u5b58\u5728openId .getOpenId() //\u65b9\u6cd5\u5185\u76f4\u63a5\u4ece\u7f13\u5b58\u4e2d\u83b7\u53d6openId */ define("mods/wxOpenId/index", function (require, exports, module) { var cache = require("../../libs/cache"); var PageModule = require("component/PageModule"); var cachePer = "wx_openid_by_corpid_"; var weixin = PageModule.render({ /** openId = coprId + \u4e1a\u52a1id \u624d\u80fd\u51b3\u5b9a\u4e00\u4e2aopenId \u56e0\u4e3a\u4e00\u5bb6\u533b\u9662\u53ef\u80fd\u5bf9\u5e94\u4e24\u4e2a\u516c\u4f17\u53f7 \u6bd4\u5982\u5145\u503c\u5230\u533b\u8054\u4f53\u7684\u516c\u4f17\u53f7\uff0c\u7f34\u8d39\u662f\u5230\u533b\u9662\u7684\u516c\u4f17\u53f7 */ getOpenIdByCode: function getOpenIdByCode(code, corpId, optType) { this.corpId = corpId; var key = this.getPre(corpId, optType); //\u83b7\u53d6openId PageModule.render({ init: function init(code, corpId, optType) { this.get("/user-web/restapi/wx/getOpenId", { code: code, corpId: corpId, optType: optType }); }, onSuccess: function onSuccess(result) { //\u7f13\u5b58openId if (result.data.openId) { cache.set(key, result.data.openId); location.reload(); } else { this.util.alert("\u65e0\u6cd5\u83b7\u5f97OpenId\uff0c\u6682\u65f6\u65e0\u6cd5\u4f7f\u7528\u5fae\u4fe1\u5145\u503c"); } // self.openId = result.data.openId; }, onError: function onError(result) { this.util.alert(result.msg || ''); } }).init(code, this.corpId, 1); }, getOpenId: function getOpenId(corpId, optType) { var openId = cache.get(this.getPre(corpId, optType)); // console.log( this.query.code, openId ) //\u6ca1\u6709openId if (!openId) { //\u9875\u9762\u6ca1\u6709\u6388\u6743code, \u5c31\u53bb\u5fae\u4fe1\u6388\u6743 if (this.query.code) { this.getOpenIdByCode(this.query.code, corpId, optType); } else { this.goAuth(corpId, optType); } return null; } return openId; //cache.get(cachePer+corpId) }, //\u53bb\u6388\u6743\u4e00\u53d8\u83b7\u5f97openId goAuth: function goAuth(corpId, optType) { this.get("/user-web/restapi/wx/getOpenIdParas", { corpId: corpId, optType: optType }); }, onSuccess: function onSuccess(result) { if (result && result.data && result.data.wxAppId) { var appid = result.data.wxAppId; //"wx664b4dee4fbd814c";//\u5357\u9633 var redirect_uri = window.location.href; var url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=" + redirect_uri + "&response_type=code&scope=snsapi_base&state=261_1_jsonp4203#wechat_redirect"; // window.location.replace(url);// = ; window.location.href = url; } else { this.util.alert("\u533b\u9662\u672a\u914d\u7f6e\u516c\u4f17\u53f7\uff0c\u65e0\u6cd5\u4f7f\u7528\u5fae\u4fe1\u652f\u4ed8"); } }, getPre: function getPre(corpId, optType) { return cachePer + optType + "_" + corpId; } }); module.exports = weixin; });