topic.js 11.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * 
 * WordPres版微信小程序
 * author: jianbo
 * organization: 守望轩  www.watch-life.net
 * github:    https://github.com/iamxjb/winxin-app-watch-life.net
 * 技术支持微信号:iamxjb
 * 开源协议:MIT
 * 
 *  *Copyright (c) 2017 https://www.watch-life.net All rights reserved.
 */
ijianbo's avatar
ijianbo 已提交
12
import config from '../../utils/config.js'
13 14
var Api = require('../../utils/api.js');
var util = require('../../utils/util.js');
ijianbo's avatar
ijianbo 已提交
15
var Auth = require('../../utils/auth.js');
ijianbo's avatar
ijianbo 已提交
16
var WxParse = require('../../wxParse/wxParse.js');
ijianbo's avatar
ijianbo 已提交
17
var wxApi = require('../../utils/wxApi.js')
ijianbo's avatar
ijianbo 已提交
18
var wxRequest = require('../../utils/wxRequest.js');
J
jianbo 已提交
19 20
var webSiteName= config.getWebsiteName;
var domain =config.getDomain
ijianbo's avatar
ijianbo 已提交
21
var app = getApp();
22 23 24 25
Page({
    data: {
        text: "Page topic",
        categoriesList: {},
ijianbo's avatar
ijianbo 已提交
26 27
        floatDisplay: "none",
        openid:"",
J
jianbo 已提交
28 29 30
        userInfo:{},
        webSiteName:webSiteName,
    domain:domain        
31 32
    },
    onLoad: function (options) {
J
jianbo 已提交
33 34
        Auth.setUserInfoData(this); 
        Auth.checkLogin(this);
35
        wx.setNavigationBarTitle({
36
            title: '专题'
37
        });
38 39 40 41 42 43 44 45 46

        wx.showShareMenu({
                  withShareTicket:true,
                  menus:['shareAppMessage','shareTimeline'],
                  success:function(e)
                  {
                    //console.log(e);
                  }
            })
47
        
48
        this.fetchCategoriesData();
J
jianbo 已提交
49
        
ijianbo's avatar
ijianbo 已提交
50 51 52
    },
    onShow:function(){            

53 54 55
    },
    //获取分类列表
    fetchCategoriesData: function () {
56
        var self = this;        
57 58 59 60
        self.setData({
            categoriesList: []
        });
        //console.log(Api.getCategories());
J
jianbo 已提交
61 62 63
        var getCategoriesIdsRequest = wxRequest.getRequest(Api.getCategoriesIds());
        getCategoriesIdsRequest.then(res=>{
            
64

J
jianbo 已提交
65 66 67 68 69
            var ids="";
            var openid= self.data.openid
            if(!res.data.Ids=="")
            {
                ids=res.data.Ids;
70
            }
J
jianbo 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
            var getCategoriesRequest = wxRequest.getRequest(Api.getCategories(ids,openid));
                getCategoriesRequest.then(response => {
                    if (response.statusCode === 200) {
                        self.setData({
                            floatDisplay: "block",
                            categoriesList: self.data.categoriesList.concat(response.data.map(function (item) {
                                if (typeof (item.category_thumbnail_image) == "undefined" || item.category_thumbnail_image == "") {
                                    item.category_thumbnail_image = "../../images/website.png";
                                
                                }
                                // item.subimg = "subscription.png";
                                return item;
                            })),
                        });
                    }
                    else {
                        console.log(response);
                    }

                    })
                    // .then(res=>{
                    //     if (self.data.openid) {                
                    //         setTimeout(function () {
                    //             self.getSubscription();
                    //         }, 500);  
                    //     }
                        
                    // })
                    .catch(function (response) {
                        console.log(response);
101

J
jianbo 已提交
102
                    }).finally(function () {
103

J
jianbo 已提交
104 105 106
                    })
        })
        
107 108 109 110 111 112 113 114 115 116 117
    },
    onShareAppMessage: function () {
        return {
            title: '分享“' + config.getWebsiteName + '”的专题栏目.',
            path: 'pages/topic/topic',
            success: function (res) {
                // 转发成功
            },
            fail: function (res) {
                // 转发失败
            }
118
        }
J
jianbo 已提交
119
    },    
120 121
    postsub: function (e) {
        var self = this;
ijianbo's avatar
ijianbo 已提交
122 123
        if (!self.data.openid) {
            Auth.checkSession(self,'isLoginNow');
124 125 126
        }
        else {
            var categoryid = e.currentTarget.dataset.id;
ijianbo's avatar
ijianbo 已提交
127
            var openid = self.data.openid;
128 129 130 131 132 133
            var url = Api.postSubscription();
            var subflag = e.currentTarget.dataset.subflag;
            var data = {
                categoryid: categoryid,
                openid: openid
            };
ijianbo's avatar
ijianbo 已提交
134

135 136 137 138 139 140 141 142 143 144
            var postSubscriptionRequest = wxRequest.postRequest(url, data);
            postSubscriptionRequest.then(response => {
                if (response.statusCode === 200) {
                    if (response.data.status == '200') {
                        setTimeout(function () {
                            wx.showToast({
                                title: '订阅成功',
                                icon: 'success',
                                duration: 900,
                                success: function () {
ijianbo's avatar
ijianbo 已提交
145

146 147 148 149 150 151 152 153 154 155 156 157 158
                                }
                            });
                        }, 900);
                        var subimg = "";
                        if (subflag == "0") {
                            subflag = "1";
                            subimg = "subscription-on.png"
                        }
                        else {
                            subflag = "0";
                            subimg = "subscription.png"
                        }
                        self.reloadData(categoryid, subflag, subimg);
ijianbo's avatar
ijianbo 已提交
159

160 161
                    }
                    else if (response.data.status == '201') {
ijianbo's avatar
ijianbo 已提交
162 163 164 165 166 167 168 169 170
                        setTimeout(function () {
                            wx.showToast({
                                title: '取消订阅成功',
                                icon: 'success',
                                duration: 900,
                                success: function () {
                                }
                            });
                        }, 900);
171
                        var subimg = "";
ijianbo's avatar
ijianbo 已提交
172 173 174 175 176 177 178 179
                        if (subflag == "0") {
                            subflag = "1";
                            subimg = "subscription-on.png"
                        }
                        else {
                            subflag = "0";
                            subimg = "subscription.png"
                        }
180
                        self.reloadData(categoryid, subflag, subimg);
ijianbo's avatar
ijianbo 已提交
181 182

                    }
183 184 185 186
                    else if (response.data.status == '501' || response.data.status == '501') {
                        console.log(response);
                    }

ijianbo's avatar
ijianbo 已提交
187 188

                }
189 190 191 192 193 194 195
                else {
                    setTimeout(function () {
                        wx.showToast({
                            title: '操作失败,请稍后重试',
                            icon: 'success',
                            duration: 900,
                            success: function () {
ijianbo's avatar
ijianbo 已提交
196

197 198 199 200
                            }
                        });
                    }, 900);
                    console.log(response);
ijianbo's avatar
ijianbo 已提交
201 202
                }

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
            }).catch(function (response) {
                setTimeout(function () {
                    wx.showToast({
                        title: '操作失败,请稍后重试',
                        icon: 'success',
                        duration: 900,
                        success: function () {
                        }
                    });
                }, 900);
                console.log(response);
            })
        }
    },
    reloadData: function (id, subflag, subimg) {
        var self = this;
        var newCategoriesList = [];
        var categoriesList = self.data.categoriesList;
        for (var i = 0; i < categoriesList.length; i++) {
            if (categoriesList[i].id == id) {
                categoriesList[i].subflag = subflag;
                categoriesList[i].subimg = subimg;
            }
            newCategoriesList.push(categoriesList[i]);
        }

        if (newCategoriesList.length > 0) {
            self.setData({
                categoriesList: newCategoriesList
            });
ijianbo's avatar
ijianbo 已提交
233

234
        }
ijianbo's avatar
ijianbo 已提交
235

236
    },
ijianbo's avatar
ijianbo 已提交
237

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    //跳转至某分类下的文章列表
    redictIndex: function (e) {
        //console.log('查看某类别下的文章');  
        var id = e.currentTarget.dataset.id;
        var name = e.currentTarget.dataset.item;
        var url = '../list/list?categoryID=' + id;
        wx.navigateTo({
            url: url
        });
    },
    userAuthorization: function () {
        var self = this;
        // 判断是否是第一次授权,非第一次授权且授权失败则进行提醒
        wx.getSetting({
            success: function success(res) {
                console.log(res.authSetting);
                var authSetting = res.authSetting;
255 256
                if (!('scope.userInfo' in authSetting)) {
                //if (util.isEmptyObject(authSetting)) {
257
                    console.log('第一次授权');
ijianbo's avatar
ijianbo 已提交
258 259
                    self.setData({ isLoginPopup: true })

260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
                } else {
                    console.log('不是第一次授权', authSetting);
                    // 没有授权的提醒
                    if (authSetting['scope.userInfo'] === false) {
                        wx.showModal({
                            title: '用户未授权',
                            content: '如需正常使用评论、点赞、赞赏等功能需授权获取用户信息。是否在授权管理中选中“用户信息”?',
                            showCancel: true,
                            cancelColor: '#296fd0',
                            confirmColor: '#296fd0',
                            confirmText: '设置权限',
                            success: function (res) {
                                if (res.confirm) {
                                    console.log('用户点击确定')
                                    wx.openSetting({
                                        success: function success(res) {
                                            console.log('打开设置', res.authSetting);
                                            var scopeUserInfo = res.authSetting["scope.userInfo"];
                                            if (scopeUserInfo) {
ijianbo's avatar
ijianbo 已提交
279
                                                self.getUsreInfo(null);
280 281 282 283 284 285 286
                                            }
                                        }
                                    });
                                }
                            }
                        })
                    }
ijianbo's avatar
ijianbo 已提交
287
                    else {
ijianbo's avatar
ijianbo 已提交
288
                        auth.getUsreInfo(null);
ijianbo's avatar
ijianbo 已提交
289
                    }
290 291 292 293
                }
            }
        });
    },
J
jianbo 已提交
294
    agreeGetUser: function (e) {        
J
jianbo 已提交
295 296 297
        let self= this;
        Auth.checkAgreeGetUser(e,app,self,'0');   

ijianbo's avatar
ijianbo 已提交
298
        setTimeout(function () {
J
jianbo 已提交
299 300 301
            self.fetchCategoriesData();             
        }, 1000);
        
ijianbo's avatar
ijianbo 已提交
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
    },
    closeLoginPopup() {
        this.setData({ isLoginPopup: false });
    },
    openLoginPopup() {
        this.setData({ isLoginPopup: true });
    },
    getOpenId(data) {
        var url = Api.getOpenidUrl();
        var self  = this;
        var postOpenidRequest = wxRequest.postRequest(url, data);
        //获取openid
        postOpenidRequest.then(response => {
            if (response.data.status == '200') {
                //console.log(response.data.openid)
                console.log("openid 获取成功");
                app.globalData.openid = response.data.openid;
                app.globalData.isGetOpenid = true;

            }
            else {
                console.log(response);
            }
        }).then(res=>{
            setTimeout(function () {
                self.getSubscription();               
            }, 500);
        })
    },
331 332 333 334 335 336 337
    confirm: function () {
        this.setData({
            'dialog.hidden': true,
            'dialog.title': '',
            'dialog.content': ''
        })
    } 
ijianbo's avatar
ijianbo 已提交
338

339
})