index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. import Vue from 'vue'
  2. import Vuex from 'vuex';
  3. import utils from "../library/utils/Comm.js";
  4. import api from "../config/api.js";
  5. import md5 from "../library/vendor/md5.js";
  6. import base64 from "@/library/vendor/base64.js";
  7. import global from '@/config/global.js';
  8. import request from '@/library/Request.js';
  9. // import {
  10. // v4 as uuidv4
  11. // } from "uuid";
  12. import permission from './modules/permission.js';
  13. const namespaced = true;
  14. var isWxLoad = false;
  15. Vue.use(Vuex)
  16. const store = new Vuex.Store({
  17. state: {
  18. lbs: null,
  19. user: null,
  20. weixin: null,
  21. tj_uid: 0,
  22. txbagenum: {
  23. chatCount: 0,
  24. applyCount: 0
  25. },
  26. uuid: "",
  27. http: "",
  28. sysData: {
  29. "bank": 1,
  30. "ailpay": 1,
  31. "wxpay": 0,
  32. "app_code": 0,
  33. "is_audit": 1,
  34. app_update: "你有新版本要更新"
  35. },
  36. // #ifdef APP
  37. showData: false,
  38. // #endif
  39. // #ifndef APP
  40. showData: true,
  41. // #endif
  42. },
  43. mutations: {
  44. // 修改显示隐藏app
  45. chnangeSshowData(store, data) {
  46. store.showData = data;
  47. },
  48. init(store, page) {
  49. // console.log(store,page,'2323424311341234')
  50. try {
  51. var uuid = uni.getStorageSync("uuid");
  52. if (!utils.isDefine(uuid)) {
  53. uuid = utils.uuidv4();
  54. uni.setStorageSync("uuid", uuid);
  55. }
  56. store.uuid = uuid;
  57. var userStr = uni.getStorageSync(uuid + "_user");
  58. var lbs = uni.getStorageSync(uuid + "_lbs");
  59. // userStr = '{"token":"M2QzNDYwM2E5NDA4Yzk2MzU5NWUzMmM0NjY3YTc0MTk="}';
  60. // console.log(page, 'page11111111111111111111')
  61. if (utils.isDefine(userStr)) {
  62. // console.log(page, 'page11111111111111111111')
  63. this.commit('setUser', JSON.parse(userStr));
  64. setTimeout(async ()=>{
  65. // #ifdef APP
  66. const {globalData} = await utils.getAppStore();
  67. globalData.webSocket.connect()
  68. // #endif
  69. // #ifndef APP
  70. Vue.prototype.webSocket.connect()
  71. // #endif
  72. },5000)
  73. }
  74. if (utils.isDefine(lbs)) {
  75. this.commit('setLbs', JSON.parse(lbs));
  76. }
  77. //#ifdef H5
  78. var wxStr = uni.getStorageSync(uuid + "_wx");
  79. if (utils.isDefine(wxStr)) {
  80. this.commit('setWeixin', JSON.parse(wxStr));
  81. }
  82. //#endif
  83. //#ifdef H5
  84. var tjUID = uni.getStorageSync(uuid + "_tj");
  85. if (utils.isDefine(tjUID)) {
  86. this.commit('setTjuid', tjUID);
  87. }
  88. //#endif
  89. // #ifdef H5
  90. this.commit("setSys", {
  91. "bank": 1,
  92. "ailpay": 1,
  93. "wxpay": 0,
  94. "app_code": 0,
  95. "is_audit": 0,
  96. app_update: "你有新版本要更新"
  97. });
  98. // #endif
  99. this.commit("setHttp", global.apiHttps[0]);
  100. } catch (e) {
  101. console.log(e, 'safasdfasdfas ');
  102. }
  103. },
  104. setHttp: function(store, http) {
  105. store.http = http;
  106. },
  107. /** 设置用户数据
  108. * @param {Object} store
  109. * @param {Object} user
  110. */
  111. setUser(store, user) {
  112. store.user = user;
  113. let app = getApp();
  114. uni.setStorage({
  115. key: store.uuid + "_user",
  116. data: JSON.stringify(user)
  117. });
  118. },
  119. loginOut(store) {
  120. store.user = null;
  121. uni.removeStorage({
  122. key: store.uuid + "_user"
  123. });
  124. let app = getApp();
  125. app.globalData.webSocket.ttClose();
  126. },
  127. setSys(store, data) {
  128. // console.log(store,data,'data')
  129. store.sysData = data;
  130. },
  131. /**
  132. * 设置定位信息
  133. * @param {Object} store
  134. * @param {Object} lbs
  135. */
  136. setLbs(store, lbs) {
  137. store.lbs = lbs;
  138. uni.setStorage({
  139. key: store.uuid + '_lbs',
  140. data: JSON.stringify(lbs)
  141. });
  142. },
  143. /**
  144. * 获取GPS信息
  145. */
  146. getGps: function(store, data) {
  147. var $page = data.page;
  148. var fn = data.fn || function() {};
  149. var err = data.err || function() {};
  150. var backCall = (gps) => {
  151. this.commit('setLbs', gps);
  152. fn(gps);
  153. };
  154. // #ifdef H5
  155. uni.getLocation({
  156. type: 'wgs84',
  157. success: (res) => {
  158. backCall(res);
  159. },
  160. fail: () => {
  161. err();
  162. console.log('err');
  163. }
  164. });
  165. //#endif
  166. // #ifdef APP
  167. let p = uni.getSystemInfoSync().platform;
  168. if (p === "ios") {
  169. this.dispatch('permission/requestPermissions', 'ACCESS_FINE_LOCATION').then(res => {
  170. if (res == 1) {
  171. uni.getLocation({
  172. type: 'wgs84',
  173. success: (res) => {
  174. backCall(res);
  175. },
  176. fail: (er) => {
  177. err();
  178. }
  179. });
  180. }
  181. });
  182. }
  183. // #endif
  184. // #ifdef MP-WEIXIN
  185. uni.getSetting({
  186. success(res) {
  187. if (res.authSetting['scope.userLocation'] == null) {
  188. uni.getLocation({
  189. type: 'wgs84',
  190. success: (res) => {
  191. console.log(res);
  192. backCall(res);
  193. },
  194. fail: (er) => {
  195. console.log(er);
  196. err();
  197. }
  198. });
  199. return;
  200. }
  201. if (!res.authSetting['scope.userLocation']) {
  202. uni.showModal({
  203. title: '系统提示',
  204. content: "你的位置信息将用于检索附近车行信息",
  205. success: res => {
  206. if (res.confirm) {
  207. uni.openSetting({
  208. success: () => {
  209. uni.getLocation({
  210. type: 'wgs84',
  211. success: (res) => {
  212. backCall(
  213. res);
  214. },
  215. fail: () => {
  216. err();
  217. }
  218. });
  219. }
  220. });
  221. } else {
  222. err();
  223. }
  224. }
  225. });
  226. return;
  227. }
  228. uni.getLocation({
  229. type: 'wgs84',
  230. success: (res) => {
  231. backCall(res);
  232. },
  233. fail: (er) => {
  234. err();
  235. }
  236. });
  237. }
  238. })
  239. // #endif
  240. },
  241. /**
  242. * 判断是否登录
  243. * @param {Object} store
  244. * @param {Object} $page
  245. */
  246. checkUserLogin(store, data) {
  247. var that = this;
  248. var $page = data.page;
  249. var fn = data['fn'] || null;
  250. var errFn = data['err'] || null;
  251. var isLogin = data['isLogin'] || null;
  252. if (store.user == null || store.user.token == null) {
  253. if (isLogin != null) {
  254. this.commit('login', 'app');
  255. } else {
  256. if (errFn != null) errFn(res);
  257. }
  258. this.commit('loginOut', '');
  259. return;
  260. }
  261. request
  262. .post("userInit")
  263. .then(res => {
  264. if (res.code == 200) {
  265. that.commit('setUser', res.data);
  266. if (fn != null) fn();
  267. }
  268. if (res.code == -66) {
  269. uni.showModal({
  270. title: '系统提示',
  271. content: res.msg,
  272. showCancel: false
  273. });
  274. return;
  275. }
  276. if (res.code == -99) {
  277. this.commit('loginOut', '');
  278. if (isLogin != null) {
  279. this.commit('login', 'app');
  280. } else {
  281. if (errFn != null) errFn(res);
  282. }
  283. }
  284. })
  285. .catch(e => {
  286. console.log(e);
  287. });
  288. return true;
  289. },
  290. /**
  291. * 支付类型
  292. * @param {Object} store
  293. * @param {Object} data
  294. */
  295. payPayment: function(store, data) {
  296. var $page = data.page;
  297. var isBalance = data.isBalance || 0;
  298. var isClose = data.isClose || false;
  299. if (data.afterFn != null) {
  300. data.afterFn();
  301. }
  302. if (data.payOk != null) {
  303. data.payOk = null;
  304. }
  305. uni.showLoading({
  306. title: "跳转支付中.."
  307. });
  308. var post = {
  309. order_id: data.orderId,
  310. buy_type: data.buyType,
  311. pay_type: data.payType,
  312. device_type: $page.$deviceType,
  313. bankId: data.bankId == null ? '' : data.bankId,
  314. value: 0
  315. };
  316. request
  317. .post("payPayment", post)
  318. .then(res => {
  319. uni.hideLoading();
  320. if (res.code == 200) {
  321. //支付宝
  322. if (data.payType == 'ailpay') {
  323. if (res.data.openType == 'app') {
  324. uni.requestPayment({
  325. provider: 'alipay',
  326. orderInfo: res.data.url,
  327. success: (res) => {
  328. uni.showToast({
  329. title: "支付成功",
  330. mask: true,
  331. icon: "none"
  332. });
  333. setTimeout(() => {
  334. uni.redirectTo({
  335. url: '/pages/user/pay/order_ok?orderId=' +
  336. data.orderId +
  337. "&but_type=" + data
  338. .buyType
  339. });
  340. }, 500);
  341. },
  342. fail: (err) => {
  343. console.log(err);
  344. uni.showToast({
  345. title: '你已取消支付'
  346. });
  347. }
  348. });
  349. }
  350. if (res.data.openType == 'web') {
  351. var authUrl = "alipays://platformapi/startapp?appId=20000067&url=" +
  352. encodeURIComponent(res.data.url);
  353. if ($page.$device.platform == 'ios') {
  354. authUrl = ("alipay://platformapi/startapp?appId=20000067&url=" +
  355. encodeURIComponent(res.data.url));
  356. }
  357. // #ifdef APP-PLUS
  358. $page.$refs['uipay'].show(data.orderId, data.payOk);
  359. plus.runtime.openURL(authUrl, function(res) {
  360. uni.showToast({
  361. icon: "none",
  362. title: $page.$device.platform +
  363. "请安装支付宝之后再进行支付!" + JSON.stringify(res)
  364. });
  365. });
  366. // #endif
  367. // #ifndef APP-PLUS
  368. location.href = authUrl;
  369. // #endif
  370. }
  371. }
  372. if (data.payType == 'unionpay') {
  373. var tts = base64.encode(res.data.url);
  374. uni.navigateTo({
  375. url: "/pages/web/index?title=银联支付&url=" + encodeURIComponent(
  376. tts) + "&type=base64"
  377. });
  378. $page.$refs['uipay'].show(data.orderId, data.fn, true);
  379. }
  380. if (data.payType == 'wxpay') {
  381. // #ifdef APP-PLUS
  382. $page.$refs['uipay'].show(data.orderId, data.payOk);
  383. plus.runtime.openURL(res.data.url, function(res) {
  384. uni.showToast({
  385. icon: "none",
  386. title: $page.$device.platform + "请安装微信支付!" + JSON
  387. .stringify(res)
  388. });
  389. });
  390. // #endif
  391. // #ifndef APP-PLUS
  392. location.href = res.data.url;
  393. // #endif
  394. }
  395. //银行卡
  396. if (data.payType == 'bank') {
  397. if (isClose) {
  398. uni.redirectTo({
  399. url: "/pages/user/buy/order_ok?payType=" + data.payType +
  400. "&orderId=" + data.orderId +
  401. "&time=" + res.data.time +
  402. "&pay_card=" + res.data.pay_card
  403. });
  404. } else {
  405. uni.navigateTo({
  406. url: "/pages/user/buy/order_ok?payType=" + data.payType +
  407. "&orderId=" + data.orderId +
  408. "&time=" + res.data.time + "&pay_card=" + res.data
  409. .pay_card
  410. });
  411. }
  412. }
  413. } else {
  414. uni.showToast({
  415. title: res.msg
  416. });
  417. }
  418. })
  419. .catch(err => {
  420. console.log(err);
  421. uni.hideLoading();
  422. uni.showModal({
  423. title: '系统提示',
  424. content: "支付错误:" + err,
  425. showCancel: false
  426. });
  427. });
  428. },
  429. setTxbagenum(store, data) {
  430. let count = data.chatCount + data.applyCount;
  431. if (count <= 0) {
  432. uni.removeTabBarBadge({
  433. index: 2
  434. });
  435. } else {
  436. uni.setTabBarBadge({
  437. index: 2,
  438. text: count + ""
  439. });
  440. }
  441. store.txbagenum = data;
  442. },
  443. /**
  444. * 登录数据 |
  445. * @param {Object} store
  446. * @param {Object} type
  447. */
  448. login(store, type) {
  449. //手机app
  450. if (type == 'app') {
  451. uni.redirectTo({
  452. url: "/pages/login/login"
  453. });
  454. }
  455. }
  456. },
  457. modules: {
  458. permission: {
  459. namespaced,
  460. ...permission
  461. },
  462. }
  463. });
  464. export default store