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 = { code: res.code, appid: accountInfo.miniProgram.appId }; http.getRequest(config.API_USER_LOGIN, postData, function(res) { if (res.data.code == 200) { storage.setUserInfo(res.data.data.baseInfo); storage.setUserToken(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) { storage.setUserToken(res.data.data.token); // wx.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) { // 上报 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) { common.alert('请授权', '商城需要获取您的用户信息', function() { uni.openSetting({ success: function(rs) {} }); }); }, complete: function(res) {} }); } login(callBack); } // 支付 function wxPay(order_sn, payParams, callBack) { common.loading(); const pData = 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) { // wx.hideLoading(); // if (rs2.data.code == 200) { // // 跳转到订单页面 // callBack(true); // } else { // common.simpleToast(rs2.data.msg); // } // }) }, fail: function(res) { // 跳转到订单页面 common.simpleToast('未完成支付'); callBack(false); }, complete: function(res) { uni.hideLoading(); } }); } // 取消租赁支付 function cancelHirePay(order_sn) { http.postApi( config.API_DAY_HIRE_CABINRT_CANCEL_CABINRT_CHANGE_BATTERY, { order_sn: order_sn }, function(res) { if (res.data.code == 200) {} } ); } // 取消门店换电支付 function cancelShopExPay(order_sn) { http.postApi( config.API_CANCEL_SHOP_EX, { exchange_sn: order_sn }, function(res) { if (res.data.code == 200) {} } ); } // 取消电柜换电支付 function cancelCabExPay(order_sn) { http.postApi( config.API_CANCEL_EX_PAY, { order_sn: order_sn }, function(res) { if (res.data.code == 200) {} } ); } function cancelExtractPay(stash_sn, target_id) { http.postApi( config.API_CANCEL_EXTRACT_PAY, { stash_sn: stash_sn, target_id: target_id }, function(res) { if (res.data.code == 200) {} } ); } //取消充电柜换电支付 function cancelChargeExPay(order_sn) { http.postApi( config.API_CANCEL_CHARGE_EX_PAY, { order_sn: order_sn }, function(res) { if (res.data.code == 200) {} } ); } // 取消充电柜支付 function cancelChargePay(order_sn) { http.postApi( config.API_CANCEL_CHARGE_PAY, { order_sn: order_sn }, function(res) { if (res.data.code == 200) {} } ); } //取消充电柜取电支付 function cancelChargeExtractPay(order_sn) { http.postApi( config.API_CHARGE_EXTRACT_CANCEL, { order_sn: order_sn }, function(res) { if (res.data.code == 200) {} } ); } // 取消订单 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.simpleToast(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 }); } } }); } function aliPay(trade_no, callBack) { my.tradePay({ tradeNO: trade_no, success: function(res) { if (res.resultCode == 9000) { uni.showToast({ title: '支付成功', icon: 'success', duration: 1500 }); callBack(true) // 支付成功后执行的回调函数,传入true表示支付成功,false表示支付失败 } else { my.alert({ content: '支付失败', }); callBack(false) } }, fail: function(res) { callBack(false) }, }) } function totalPay(order_sn, payParams, trade_no, callBack) { //#ifdef MP-WEIXIN wxPay(order_sn, payParams, function(isSuccess) { if (isSuccess) { callBack(true) } else { callBack(false) } }) //#endif //#ifdef MP-ALIPAY aliPay(trade_no, function(isaliPaySuccess) { if (isaliPaySuccess) { callBack(true) } else { callBack(false) } }) //#endif } // 取消租车/购车支付 function cancelPayTakeCar(order_sn) { http.postApi( config.API_FLK_ORDER_CANCEL_HIRE, { order_sn: order_sn }, function(res) { if (res.data.code == 200) {} } ); } module.exports = { login: login, getUserInfo: getUserInfo, wxPay: wxPay, cancelOrder: cancelOrder, loadCarNum: loadCarNum, cancelHirePay: cancelHirePay, cancelShopExPay: cancelShopExPay, cancelCabExPay: cancelCabExPay, cancelChargePay: cancelChargePay, cancelChargeExtractPay: cancelChargeExtractPay, cancelExtractPay: cancelExtractPay, cancelChargeExPay: cancelChargeExPay, totalPay: totalPay, getUserOpenId: getUserOpenId, cancelPayTakeCar: cancelPayTakeCar, };