/** * 用户相关服务 */ const util = require('../utils/util.js'); const api = require('../config/api.js'); /** * Promise封装wx.checkSession */ function checkSession() { return new Promise(function(resolve, reject) { wx.checkSession({ success: function() { resolve(true); }, fail: function() { reject(false); } }) }); } /** * 判断是否实名注册过 */ function checkRegisted(){ return new Promise(function(resolve, reject) { //是否实名 util.request(api.AuthIsCert).then(function (res) { resolve(res.data.isnid); }).catch(()=>{ reject(false); }); }); } /** * Promise封装wx.login */ function login() { return new Promise(function(resolve, reject) { wx.login({ success: function(res) { if (res.code) { resolve(res); } else { reject(res); } }, fail: function(err) { reject(err); } }); }); } /** * 返回用户购物车的数量 */ function cartcount(){ util.request(api.CartGoodsCount, 'POST').then(res => { if (res.errno === 0) { wx.setTabBarBadge({ index: 2, text: ''+res.data+'' }) wx.setStorageSync('cartcount', res.data); } }) } /** * *初始化购物车数量 */ function cartshow(){ // let count=wx.getStorageSync('cartcount'); // if(count==0){ // wx.hideTabBarRedDot({ // index: 2, // }); // }else{ // wx.setTabBarBadge({ // index: 2, // text: ''+count+'' // }) // } } function cartadd(num){ let count=wx.getStorageSync('cartcount'); console.info('count'+count); if(count==null) count=0; count=count+num; wx.setStorageSync('cartcount',count); wx.setTabBarBadge({ index: 2, text: ''+count+'' }) } /** * 调用微信登录 */ function loginByWeixin(userInfo) { return new Promise(function(resolve, reject) { console.info(userInfo); //登录远程服务器 util.request(api.AuthLoginByWeixin, userInfo , 'POST').then(res => { if (res.errno === 0) { //存储用户信息 wx.setStorageSync('userInfo', res.data.userInfo); resolve(res); } else { reject(res); } }).catch((err) => { reject(err); }); }); } /** * 判断用户是否登录 */ function checkLogin() { return new Promise(function(resolve, reject) { if (wx.getStorageSync('userInfo') && wx.getStorageSync('token')) { checkSession().then(() => { resolve(true); }).catch(() => { reject(false); }); } else { reject(false); } }); } /**微信手机号码解密 */ function denPhone(e){ return new Promise(function(resolve, reject) { //登录远程服务器 util.request(api.AuthDenPhone, { iv: e.iv, encryptedData:e.encryptedData }, 'POST').then(res => { if (res.errno === 0) { resolve(res); } else { reject(res); console.log(res); } }).catch((err) => { reject(err); }); }) } module.exports = { loginByWeixin, checkLogin, denPhone, login, checkRegisted, cartcount, cartadd, cartshow };