import Vue from 'vue' import Vuex from 'vuex'; import utils from "../library/utils/Comm.js"; import api from "../config/api.js"; import md5 from "../library/vendor/md5.js"; import base64 from "@/library/vendor/base64.js"; import { v4 as uuidv4 } from "uuid"; import permission from './modules/permission.js'; const namespaced = true; var isWxLoad = false; Vue.use(Vuex) const store = new Vuex.Store({ state: { lbs: null, user: null, weixin: null, tj_uid: 0, txbagenum: { chatCount: 0, applyCount: 0 }, uuid: "", http: "", sysData: { "bank": 1, "ailpay": 1, "wxpay": 0, "app_code": 0, "is_audit": 1, app_update: "你有新版本要更新" }, // #ifdef APP showData: false, // #endif // #ifndef APP showData: true, // #endif }, mutations: { // 修改显示隐藏app chnangeSshowData(store, data){ store.showData = data; }, init(store, page) { // try{ var uuid = uni.getStorageSync("uuid"); if (!utils.isDefine(uuid)) { uuid = uuidv4(); uni.setStorageSync("uuid", uuid); } store.uuid = uuid; var userStr = uni.getStorageSync(uuid + "_user"); var lbs = uni.getStorageSync(uuid + "_lbs"); // userStr = '{"token":"M2QzNDYwM2E5NDA4Yzk2MzU5NWUzMmM0NjY3YTc0MTk="}'; if (utils.isDefine(userStr)) { this.commit('setUser', JSON.parse(userStr)); page.webSocket.connect(); } if (utils.isDefine(lbs)) { this.commit('setLbs', JSON.parse(lbs)); } //#ifdef H5 var wxStr = uni.getStorageSync(uuid + "_wx"); if (utils.isDefine(wxStr)) { this.commit('setWeixin', JSON.parse(wxStr)); } //#endif //#ifdef H5 var tjUID = uni.getStorageSync(uuid + "_tj"); if (utils.isDefine(tjUID)) { this.commit('setTjuid', tjUID); } //#endif // #ifdef H5 this.commit("setSys", { "bank": 1, "ailpay": 1, "wxpay": 0, "app_code": 0, "is_audit": 0, app_update: "你有新版本要更新" }); // #endif this.commit("setHttp", page.global.apiHttps[0]); // } catch (e) { // console.log(e.message); // } }, setHttp: function(store, http) { store.http = http; }, /** 设置用户数据 * @param {Object} store * @param {Object} user */ setUser(store, user) { store.user = user; let app = getApp(); uni.setStorage({ key: store.uuid + "_user", data: JSON.stringify(user) }); }, loginOut(store) { store.user = null; uni.removeStorage({ key: store.uuid + "_user" }); let app = getApp(); app.webSocket.ttClose(); }, setSys(store, data) { store.sysData = data; }, /** * 设置定位信息 * @param {Object} store * @param {Object} lbs */ setLbs(store, lbs) { store.lbs = lbs; uni.setStorage({ key: store.uuid + '_lbs', data: JSON.stringify(lbs) }); }, /** * 获取GPS信息 */ getGps: function(store, data) { var $page = data.page; var fn = data.fn || function() {}; var err = data.err || function() {}; var backCall = (gps) => { this.commit('setLbs', gps); fn(gps); }; // #ifdef H5 uni.getLocation({ type: 'wgs84', success: (res) => { backCall(res); }, fail: () => { err(); console.log('err'); } }); //#endif // #ifdef APP let p = uni.getSystemInfoSync().platform; if (p === "ios") { this.dispatch('permission/requestPermissions', 'ACCESS_FINE_LOCATION').then(res => { if (res == 1) { uni.getLocation({ type: 'wgs84', success: (res) => { backCall(res); }, fail: (er) => { err(); } }); } }); } // #endif // #ifdef MP-WEIXIN uni.getSetting({ success(res) { if (res.authSetting['scope.userLocation'] == null) { uni.getLocation({ type: 'wgs84', success: (res) => { console.log(res); backCall(res); }, fail: (er) => { console.log(er); err(); } }); return; } if (!res.authSetting['scope.userLocation']) { uni.showModal({ title: '系统提示', content: "你的位置信息将用于检索附近车行信息", success: res => { if (res.confirm) { uni.openSetting({ success: () => { uni.getLocation({ type: 'wgs84', success: (res) => { backCall( res); }, fail: () => { err(); } }); } }); } else { err(); } } }); return; } uni.getLocation({ type: 'wgs84', success: (res) => { backCall(res); }, fail: (er) => { err(); } }); } }) // #endif }, /** * 判断是否登录 * @param {Object} store * @param {Object} $page */ checkUserLogin(store, data) { var that = this; var $page = data.page; var fn = data['fn'] || null; var errFn = data['err'] || null; var isLogin = data['isLogin'] || null; if (store.user == null || store.user.token == null) { if (isLogin != null) { this.commit('login', 'app'); } else { if (errFn != null) errFn(res); } this.commit('loginOut', ''); return; } $page .request .post("userInit") .then(res => { if (res.code == 200) { that.commit('setUser', res.data); if (fn != null) fn(); } if (res.code == -66) { uni.showModal({ title: '系统提示', content: res.msg, showCancel: false }); return; } if (res.code == -99) { this.commit('loginOut', ''); if (isLogin != null) { this.commit('login', 'app'); } else { if (errFn != null) errFn(res); } } }) .catch(e => { console.log(e); }); return true; }, /** * 支付类型 * @param {Object} store * @param {Object} data */ payPayment: function(store, data) { var $page = data.page; var isBalance = data.isBalance || 0; var isClose = data.isClose || false; if (data.afterFn != null) { data.afterFn(); } if (data.payOk != null) { data.payOk = null; } uni.showLoading({ title: "跳转支付中.." }); var post = { order_id: data.orderId, buy_type: data.buyType, pay_type: data.payType, device_type: $page.$deviceType, bankId: data.bankId == null ? '' : data.bankId, value: 0 }; $page .request .post("payPayment", post) .then(res => { uni.hideLoading(); if (res.code == 200) { //支付宝 if (data.payType == 'ailpay') { if (res.data.openType == 'app') { uni.requestPayment({ provider: 'alipay', orderInfo: res.data.url, success: (res) => { uni.showToast({ title: "支付成功", mask: true, icon: "none" }); setTimeout(() => { uni.redirectTo({ url: '/pages/user/pay/order_ok?orderId=' + data.orderId + "&but_type=" + data .buyType }); }, 500); }, fail: (err) => { console.log(err); uni.showToast({ title: '你已取消支付' }); } }); } if (res.data.openType == 'web') { var authUrl = "alipays://platformapi/startapp?appId=20000067&url=" + encodeURIComponent(res.data.url); if ($page.$device.platform == 'ios') { authUrl = ("alipay://platformapi/startapp?appId=20000067&url=" + encodeURIComponent(res.data.url)); } // #ifdef APP-PLUS $page.$refs['uipay'].show(data.orderId, data.payOk); plus.runtime.openURL(authUrl, function(res) { uni.showToast({ icon: "none", title: $page.$device.platform + "请安装支付宝之后再进行支付!" + JSON.stringify(res) }); }); // #endif // #ifndef APP-PLUS location.href = authUrl; // #endif } } if (data.payType == 'unionpay') { var tts = base64.encode(res.data.url); uni.navigateTo({ url: "/pages/web/index?title=银联支付&url=" + encodeURIComponent( tts) + "&type=base64" }); $page.$refs['uipay'].show(data.orderId, data.fn, true); } if (data.payType == 'wxpay') { // #ifdef APP-PLUS $page.$refs['uipay'].show(data.orderId, data.payOk); plus.runtime.openURL(res.data.url, function(res) { uni.showToast({ icon: "none", title: $page.$device.platform + "请安装微信支付!" + JSON .stringify(res) }); }); // #endif // #ifndef APP-PLUS location.href = res.data.url; // #endif } //银行卡 if (data.payType == 'bank') { if (isClose) { uni.redirectTo({ url: "/pages/user/buy/order_ok?payType=" + data.payType + "&orderId=" + data.orderId + "&time=" + res.data.time + "&pay_card=" + res.data.pay_card }); } else { uni.navigateTo({ url: "/pages/user/buy/order_ok?payType=" + data.payType + "&orderId=" + data.orderId + "&time=" + res.data.time + "&pay_card=" + res.data .pay_card }); } } } else { uni.showToast({ title: res.msg }); } }) .catch(err => { console.log(err); uni.hideLoading(); uni.showModal({ title: '系统提示', content: "支付错误:" + err, showCancel: false }); }); }, setTxbagenum(store, data) { let count = data.chatCount + data.applyCount; if (count <= 0) { uni.removeTabBarBadge({ index: 2 }); } else { uni.setTabBarBadge({ index: 2, text: count + "" }); } store.txbagenum = data; }, /** * 登录数据 | * @param {Object} store * @param {Object} type */ login(store, type) { //手机app if (type == 'app') { uni.redirectTo({ url: "/pages/login/login" }); } } }, modules: { permission: { namespaced, ...permission }, } }); export default store