Jamcuk
2017-12-27 3996156e8e022dce953986acd91c3045272a4adb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//DOWX app.js
 
 
var aldstat = require("utils/ald-stat.js");
const com = require("utils/common.js");
const dowx = require("./utils/DOWX.js");
 
 
var th = null;
 
App({
 
    // 相当于全局信息
    data:
    {
        info: null        //用户信息, 如头像、openid
    },
 
    // 小程序初始化完成时
    onLaunch: function () 
    {
        th = this;
    },
 
    onShow: function ()
    {
        // 获取个人信息权限
        dowx.Authorize_userInfo(function () 
        {
            if (th.data.loginhad == true) { return false; }
            // 获取用户信息
            th.Get_info(function (res) {
                // 注册用户
                th.Login_user();
            });
        });
    },
 
    // 用户在服务器端数据注册
    working_Login_user: false,
    Login_user: function()
    {
        // '注册'发送数据准备  
        var mydata = {
            openid: th.data.info.info2.openid, //openId
            encryptedData: th.data.info.info1.encryptedData, //加密信息
            iv: th.data.info.info1.iv //向量
        };
        var myurl = com.getWholeUrl("bainian/api/client/user/login", mydata);
        // 发起注册
        th.working_Login_user = true;
        dowx.AjaxPost(myurl, {}, function (res) 
        {
            th.working_Login_user = false;
            dowx.AjaxStop([th.working_Login_user, th.working_Get_info]);
            if (res.code != 0) 
            { 
                wx.showLoading({ title: '注册返回错误', });
                console.log(res); 
            }
            else
            {
                th.data.loginhad = true;
            }
        });
    },
 
    // 获取用户基本信息,参数 回调函数
    working_Get_info: false,
    Get_info:function(callback)
    {
        if (th.data.info==null)
        {    
            // 获取用户信息,openid
            th.working_Get_info = true;
            dowx.Get_userInfo('bainian/api/client/user/getopenid',function (res) 
            {
                th.working_Get_info = false;
                dowx.AjaxStop([th.working_Login_user, th.working_Get_info]);
                // 保存数据到globalData
                th.data.info=res;
                callback(th.data.info);
            });
        }
        //信息已经存在时,直接返回
        else { callback(th.data.info); }
    }
})