index.js 10 KB

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