123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- /**
- * 用户相关服务
- */
- const util = require('./util.js');
- const api = require('../config/api.js');
- /**
- * Promise封装wx.checkSession
- */
- function checkSession() {
- return new Promise(function (resolve, reject) {
- uni.checkSession({
- success: function () {
- resolve(true);
- },
- fail: function () {
- reject(false);
- }
- });
- });
- }
- /**
- * 根据客户id 获取登录信息 让客户端登录
- * @param {}} userId
- */
- function getLoginInfo(orderSn) {
- return new Promise(function (resolve, reject) {
- //是否实名
- util.request(api.GetLoginInfo, {
- orderSn: orderSn
- })
- .then(function (res) {
- resolve(res.data);
- })
- .catch(() => {
- 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) {
- uni.login({
- success: function (res) {
- if (res.code) {
- resolve(res);
- } else {
- reject(res);
- }
- },
- fail: function (err) {
- reject(err);
- }
- });
- });
- }
- /**
- * 获取当前登录人的openid
- * 静默授权时下单用
- */
- function userOpenId() {
- return new Promise(function (resolve, reject) {
- uni.login({
- success: function (res) {
- if (res.code) {
- util.request(api.AuthOpenId, {
- code: res.code
- })
- .then((response) => {
- resolve(response);
- })
- .catch((ress) => {
- reject(ress);
- });
- } else {
- reject(res);
- }
- },
- fail: function (err) {
- reject(err);
- }
- });
- });
- }
- /**
- * 返回用户购物车的数量
- */
- /**
- *
- *初始化购物车数量
- */
- /**
- * 调用微信登录
- */
- function loginByWeixin(userInfo) {
- return new Promise(function (resolve, reject) {
- console.info(userInfo);
- //登录远程服务器
- util.request(api.AuthLoginByWeixin, userInfo, 'POST')
- .then((res) => {
- if (res.errno === 0) {
- //存储用户信息
- uni.setStorageSync('userInfo', res.data.userInfo);
- resolve(res);
- } else {
- reject(res);
- }
- })
- .catch((err) => {
- reject(err);
- });
- });
- }
- /**
- * 判断用户是否登录
- */
- function checkLogin() {
- return new Promise(function (resolve, reject) {
- if (uni.getStorageSync('userInfo') && uni.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,
- getLoginInfo,
- userOpenId
- };
|