index.js 10.0 KB

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