index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. // #ifdef APP
  126. app.globalData.webSocket.ttClose();
  127. // #endif
  128. // #ifndef APP
  129. app.webSocket.ttClose();
  130. // #endif
  131. },
  132. setSys(store, data) {
  133. // console.log(store,data,'data')
  134. store.sysData = data;
  135. },
  136. /**
  137. * 设置定位信息
  138. * @param {Object} store
  139. * @param {Object} lbs
  140. */
  141. setLbs(store, lbs) {
  142. store.lbs = lbs;
  143. uni.setStorage({
  144. key: store.uuid + '_lbs',
  145. data: JSON.stringify(lbs)
  146. });
  147. },
  148. /**
  149. * 获取GPS信息
  150. */
  151. getGps: function(store, data) {
  152. var $page = data.page;
  153. var fn = data.fn || function() {};
  154. var err = data.err || function() {};
  155. var backCall = (gps) => {
  156. this.commit('setLbs', gps);
  157. fn(gps);
  158. };
  159. // #ifdef H5
  160. uni.getLocation({
  161. type: 'wgs84',
  162. success: (res) => {
  163. backCall(res);
  164. },
  165. fail: () => {
  166. err();
  167. console.log('err');
  168. }
  169. });
  170. //#endif
  171. // #ifdef APP
  172. let p = uni.getSystemInfoSync().platform;
  173. if (p === "ios") {
  174. this.dispatch('permission/requestPermissions', 'ACCESS_FINE_LOCATION').then(res => {
  175. if (res == 1) {
  176. uni.getLocation({
  177. type: 'wgs84',
  178. success: (res) => {
  179. backCall(res);
  180. },
  181. fail: (er) => {
  182. err();
  183. }
  184. });
  185. }
  186. });
  187. }
  188. // #endif
  189. // #ifdef MP-WEIXIN
  190. uni.getSetting({
  191. success(res) {
  192. if (res.authSetting['scope.userLocation'] == null) {
  193. uni.getLocation({
  194. type: 'wgs84',
  195. success: (res) => {
  196. console.log(res);
  197. backCall(res);
  198. },
  199. fail: (er) => {
  200. console.log(er);
  201. err();
  202. }
  203. });
  204. return;
  205. }
  206. if (!res.authSetting['scope.userLocation']) {
  207. uni.showModal({
  208. title: '系统提示',
  209. content: "你的位置信息将用于检索附近车行信息",
  210. success: res => {
  211. if (res.confirm) {
  212. uni.openSetting({
  213. success: () => {
  214. uni.getLocation({
  215. type: 'wgs84',
  216. success: (res) => {
  217. backCall(
  218. res);
  219. },
  220. fail: () => {
  221. err();
  222. }
  223. });
  224. }
  225. });
  226. } else {
  227. err();
  228. }
  229. }
  230. });
  231. return;
  232. }
  233. uni.getLocation({
  234. type: 'wgs84',
  235. success: (res) => {
  236. backCall(res);
  237. },
  238. fail: (er) => {
  239. err();
  240. }
  241. });
  242. }
  243. })
  244. // #endif
  245. },
  246. /**
  247. * 判断是否登录
  248. * @param {Object} store
  249. * @param {Object} $page
  250. */
  251. checkUserLogin(store, data) {
  252. var that = this;
  253. var $page = data.page;
  254. var fn = data['fn'] || null;
  255. var errFn = data['err'] || null;
  256. var isLogin = data['isLogin'] || null;
  257. if (store.user == null || store.user.token == null) {
  258. if (isLogin != null) {
  259. this.commit('login', 'app');
  260. } else {
  261. if (errFn != null) errFn(res);
  262. }
  263. this.commit('loginOut', '');
  264. return;
  265. }
  266. request
  267. .post("userInit")
  268. .then(res => {
  269. if (res.code == 200) {
  270. that.commit('setUser', res.data);
  271. if (fn != null) fn();
  272. }
  273. if (res.code == -66) {
  274. uni.showModal({
  275. title: '系统提示',
  276. content: res.msg,
  277. showCancel: false
  278. });
  279. return;
  280. }
  281. if (res.code == -99) {
  282. this.commit('loginOut', '');
  283. if (isLogin != null) {
  284. this.commit('login', 'app');
  285. } else {
  286. if (errFn != null) errFn(res);
  287. }
  288. }
  289. })
  290. .catch(e => {
  291. console.log(e);
  292. });
  293. return true;
  294. },
  295. /**
  296. * 支付类型
  297. * @param {Object} store
  298. * @param {Object} data
  299. */
  300. payPayment: function(store, data) {
  301. var $page = data.page;
  302. var isBalance = data.isBalance || 0;
  303. var isClose = data.isClose || false;
  304. if (data.afterFn != null) {
  305. data.afterFn();
  306. }
  307. if (data.payOk != null) {
  308. data.payOk = null;
  309. }
  310. uni.showLoading({
  311. title: "跳转支付中.."
  312. });
  313. var post = {
  314. order_id: data.orderId,
  315. buy_type: data.buyType,
  316. pay_type: data.payType,
  317. device_type: $page.$deviceType,
  318. bankId: data.bankId == null ? '' : data.bankId,
  319. value: 0
  320. };
  321. request
  322. .post("payPayment", post)
  323. .then(res => {
  324. uni.hideLoading();
  325. if (res.code == 200) {
  326. //支付宝
  327. if (data.payType == 'ailpay') {
  328. if (res.data.openType == 'app') {
  329. uni.requestPayment({
  330. provider: 'alipay',
  331. orderInfo: res.data.url,
  332. success: (res) => {
  333. uni.showToast({
  334. title: "支付成功",
  335. mask: true,
  336. icon: "none"
  337. });
  338. setTimeout(() => {
  339. uni.redirectTo({
  340. url: '/pages/user/pay/order_ok?orderId=' +
  341. data.orderId +
  342. "&but_type=" + data
  343. .buyType
  344. });
  345. }, 500);
  346. },
  347. fail: (err) => {
  348. console.log(err);
  349. uni.showToast({
  350. title: '你已取消支付'
  351. });
  352. }
  353. });
  354. }
  355. if (res.data.openType == 'web') {
  356. var authUrl = "alipays://platformapi/startapp?appId=20000067&url=" +
  357. encodeURIComponent(res.data.url);
  358. if ($page.$device.platform == 'ios') {
  359. authUrl = ("alipay://platformapi/startapp?appId=20000067&url=" +
  360. encodeURIComponent(res.data.url));
  361. }
  362. // #ifdef APP-PLUS
  363. $page.$refs['uipay'].show(data.orderId, data.payOk);
  364. plus.runtime.openURL(authUrl, function(res) {
  365. uni.showToast({
  366. icon: "none",
  367. title: $page.$device.platform +
  368. "请安装支付宝之后再进行支付!" + JSON.stringify(res)
  369. });
  370. });
  371. // #endif
  372. // #ifndef APP-PLUS
  373. location.href = authUrl;
  374. // #endif
  375. }
  376. }
  377. if (data.payType == 'unionpay') {
  378. var tts = base64.encode(res.data.url);
  379. uni.navigateTo({
  380. url: "/pages/web/index?title=银联支付&url=" + encodeURIComponent(
  381. tts) + "&type=base64"
  382. });
  383. $page.$refs['uipay'].show(data.orderId, data.fn, true);
  384. }
  385. if (data.payType == 'wxpay') {
  386. // #ifdef APP-PLUS
  387. $page.$refs['uipay'].show(data.orderId, data.payOk);
  388. plus.runtime.openURL(res.data.url, function(res) {
  389. uni.showToast({
  390. icon: "none",
  391. title: $page.$device.platform + "请安装微信支付!" + JSON
  392. .stringify(res)
  393. });
  394. });
  395. // #endif
  396. // #ifndef APP-PLUS
  397. location.href = res.data.url;
  398. // #endif
  399. }
  400. //银行卡
  401. if (data.payType == 'bank') {
  402. if (isClose) {
  403. uni.redirectTo({
  404. url: "/pages/user/buy/order_ok?payType=" + data.payType +
  405. "&orderId=" + data.orderId +
  406. "&time=" + res.data.time +
  407. "&pay_card=" + res.data.pay_card
  408. });
  409. } else {
  410. uni.navigateTo({
  411. url: "/pages/user/buy/order_ok?payType=" + data.payType +
  412. "&orderId=" + data.orderId +
  413. "&time=" + res.data.time + "&pay_card=" + res.data
  414. .pay_card
  415. });
  416. }
  417. }
  418. } else {
  419. uni.showToast({
  420. title: res.msg
  421. });
  422. }
  423. })
  424. .catch(err => {
  425. console.log(err);
  426. uni.hideLoading();
  427. uni.showModal({
  428. title: '系统提示',
  429. content: "支付错误:" + err,
  430. showCancel: false
  431. });
  432. });
  433. },
  434. setTxbagenum(store, data) {
  435. let count = data.chatCount + data.applyCount;
  436. if (count <= 0) {
  437. uni.removeTabBarBadge({
  438. index: 2
  439. });
  440. } else {
  441. uni.setTabBarBadge({
  442. index: 2,
  443. text: count + ""
  444. });
  445. }
  446. store.txbagenum = data;
  447. },
  448. /**
  449. * 登录数据 |
  450. * @param {Object} store
  451. * @param {Object} type
  452. */
  453. login(store, type) {
  454. //手机app
  455. if (type == 'app') {
  456. uni.redirectTo({
  457. url: "/pages/login/login"
  458. });
  459. }
  460. }
  461. },
  462. modules: {
  463. permission: {
  464. namespaced,
  465. ...permission
  466. },
  467. }
  468. });
  469. export default store