起步软件技术论坛
搜索
 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 2024|回复: 1

[处理中] 微信登录有问题,照着demo拷贝也解决不了

[复制链接]

16

主题

29

帖子

93

积分

初级会员

Rank: 2

积分
93
QQ
发表于 2016-5-5 11:38:02 | 显示全部楼层 |阅读模式

define(function(require) {
        var $ = require("jquery");
        var justep = require("$UI/system/lib/justep");

        require("cordova!com.justep.cordova.plugin.weixin.v3");
        require("cordova!plugin.http.request");

        // 仅作为示例用,如果正式开发,更好的方案参见takeout,从云服务器去请求微信
        var weixinApp = "wxc08fa78b0a117595";
        var weixinSecrt = "648dc0f1d889525095ed0cb2bea05720";

        var Model = function() {
                this.callParent();
        };

        // 图片路径转换
        Model.prototype.getImageUrl = function(url) {
                return require.toUrl(url);
        };

        Model.prototype.loginWeixinClicked = function(event) {
                var self = this;
                if (!navigator.weixin) {
                        justep.Util.hint("请安装最新版本(含插件)体验!");
                        return;
                }
                ;
                var weixin = navigator.weixin;
               
                weixin.auth(function(info) {
                        self.weixinCode2Token(info, self.authSuccess);
                        alert("xiaojing3-32"); //测试运行结果
                },

                function(reason) {

                        justep.Util.hint("登录失败37行: " + JSON.stringify(reason), {
                                "type" : "danger"
                        });
                });

        };

        Model.prototype.authSuccess = function(access_token, openid) {
                alert(access_token);
                //https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID
//???access_token有问题,运行到这里会提示“登录失败49行:”,去掉可以运行正常,但是没有用户名
                if (!(access_token && openid)) {
                        justep.Util.hint("登录失败49行:", {
                                "type" : "danger"
                        });
                        return;
                }

                var url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid + "lang=zh-CN";
                console.log(url);
                var httpReq = new plugin.HttpRequest();
                httpReq.getJSON(url, function(status, data) {

                        if (status) {
                                justep.Util.hint("登录失败61行:", {
                                        "type" : "danger"
                                });
                        }
                        var user = {};
                        user.userid = data.openid;
                        user.accountType = "WX";
                        user.name = data.nickname || "NONAME";
                        justep.Shell.userType.set(user.accountType);
                        justep.Shell.userName.set(user.name);
                        localStorage.setItem("userUUID", JSON.stringify(user));
                        justep.Util.hint("登录成功");

                        setTimeout(function() {
                                justep.Shell.showPage("main");
                        }, 3000);
                        // 微信的返回内容
                        // "openid":"OPENID",
                        // "nickname":"NICKNAME",
                        // "sex":1,
                        // "province":"PROVINCE",
                        // "city":"CITY",
                        // "country":"COUNTRY",
                        // "headimgurl":
                        // "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0",
                        // "privilege":[
                        // "PRIVILEGE1",
                        // "PRIVILEGE2"
                        // ],
                        // "unionid": " o6_bmasdasdsad6_2sgVt7hMZOPfL"

                });
        };

        Model.prototype.weixinCode2Token = function(info, authSuccess) {
                var code = info.code;
                var url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + weixinApp + "&secret=" + weixinSecrt + "&code=" + code + "&grant_type=authorization_code";

                var httpReq = new plugin.HttpRequest();
                httpReq.getJSON(url, function(status, data) {

                        if (status) {
                                justep.Util.hint("登录失败103行:", {
                                        "type" : "danger"
                                });
                                return;
                        }
                        var unionid = data.unionid || "none";
                        var openid = data.openid || "none";
                        alert("xiaojing1-110"); //测试运行结果
                        var access_token = data.access_token;
                        alert(access_token);  //这里输出undefined
                        authSuccess(access_token, openid);
                        alert("xiaojing2-113"); //测试运行结果

                });

        };

        return Model;
});
发表于 2016-5-6 18:54:31 | 显示全部楼层
说清楚些,具体是什么问题??打包仿网易案例试试吧!本地测试是可以正常登录的!
qq:1912779713
WeX5教程--WeX5下载
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|X3技术论坛|Justep Inc.    

GMT+8, 2024-5-13 23:45 , Processed in 0.066349 second(s), 23 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表