123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- 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,
- };
|