index.js 10 KB

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