123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- var config = require('./config.js');
- var http = require('./http.js');
- var common = require('./common.js');
- var storage = require('./storage.js');
- function login(callBack) {
- if (!callBack) {
- callBack = function () {};
- }
- var userInfo = storage.getUserInfo();
- if (!userInfo) {
- // uni.login({
- // success: (res) => {
- //const accountInfo = uni.getAccountInfoSync();
- // 发送 res.code 到后台换取 openId, sessionKey, unionId
- var postData = {
- appid: "wxddbcc3709026525e"
- };
- http.getRequest(config.API_USER_LOGIN, postData, function (res) {
- console.log(res);
- if (res.data.code == 200) {
- storage.setUserInfo(res.data.data.baseInfo);
- uni.setStorage({
- key: config.STORAGE_USER_TOKEN,
- data: res.data.data.baseInfo.token
- });
- callBack(res.data.data.baseInfo);
- } else {
- common.alert('登录提示', res.data.msg);
- callBack(null);
- }
- });
- // }
- // });
- }
- callBack(userInfo);
- }
- // 登录从本地找openid 没有的话从本地加载
- function getUserOpenId() {
- if (!uni.getStorageSync(config.STORAGE_OPEN_ID)) {
- uni.login({
- success: (res) => {
- // 发送 res.code 到后台换取 openId, sessionKey, unionId
- var postData = {
- wx_code: res.code
- };
- http.postApi(config.API_GET_OPEN_ID, postData, function (res) {
- if (res.data.code == 200) {
- uni.setStorageSync(config.STORAGE_OPEN_ID, res.data.data.wx_session_msg.openid);
- if (res.data.data.token) {
- uni.setStorage({
- key: config.STORAGE_USER_TOKEN,
- data: res.data.data.token
- });
- }
- }
- });
- }
- });
- }
- }
- function getUserInfo(callBack) {
- var userInfo = storage.getUserInfo();
- if (userInfo) {
- if (userInfo.is_new == 0) {
- if (callBack) {
- callBack(userInfo);
- }
- return;
- }
- uni.getUserInfo({
- success: function (res) {
- console.log(res);
- // 上报
- http.postApi(config.API_REPORT_USER_INFO, res.userInfo, function (rs2) {
- if (rs2.data.code == 200) {
- storage.setUserInfo(rs2.data.data.baseInfo);
- callBack(rs2.data.data.baseInfo);
- } else {
- common.alert('登录提示', rs2.data.msg);
- }
- });
- },
- fail: function (res) {
- console.log(res);
- common.alert('请授权', '商城需要获取您的用户信息', function () {
- uni.openSetting({
- success: function (rs) {}
- });
- });
- },
- complete: function (res) {}
- });
- }
- login(callBack);
- }
- // 支付
- function wxPay(order_sn, payParams, callBack) {
- common.loading();
- uni.requestPayment({
- timeStamp: payParams.timeStamp,
- nonceStr: payParams.nonceStr,
- package: payParams.package,
- signType: payParams.signType,
- paySign: payParams.paySign,
- success: function (rs) {
- uni.showToast({
- title: '支付成功',
- icon: 'success',
- duration: 1500
- });
- callBack(true);
- // http.postApi(config.API_USER_PAY_SUCCESS, { order_sn: order_sn }, function (rs2) {
- // console.log(rs2);
- // wx.hideLoading();
- // if (rs2.data.code == 200) {
- // // 跳转到订单页面
- // callBack(true);
- // } else {
- // common.alert('提示', rs2.data.msg);
- // }
- // })
- },
- fail: function (res) {
- console.log(res);
- // 支付失败 / 未完成支付
- // 跳转到订单页面
- common.simpleToast('未完成支付');
- callBack(false);
- },
- complete: function (res) {
- uni.hideLoading();
- }
- });
- }
- // 取消订单
- function cancelOrder(order_sn, callBack) {
- http.postApi(
- config.API_USER_CANCEL_ORDER,
- {
- order_sn: order_sn
- },
- function (res) {
- if (res.data.code == 200) {
- callBack();
- } else {
- common.alert('提示', res.data.msg);
- }
- }
- );
- }
- function loadCarNum() {
- http.postApi(config.API_USER_CART_NUM, {}, function (response) {
- if (response.data.code === 200) {
- if (response.data.data.cart_num) {
- uni.setTabBarBadge({
- index: 2,
- text: response.data.data.cart_num
- });
- } else {
- uni.removeTabBarBadge({
- index: 2
- });
- }
- }
- });
- }
- module.exports = {
- login: login,
- getUserInfo: getUserInfo,
- wxPay: wxPay,
- cancelOrder: cancelOrder,
- loadCarNum: loadCarNum,
- getUserOpenId: getUserOpenId
- };
|