index.js 9.9 KB

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