app.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. //app.js
  2. var util = require('./utils/utils.js');
  3. var api;
  4. App({
  5. data: {
  6. deviceInfo: {}
  7. },
  8. is_on_launch: true,
  9. onLaunch: function () {
  10. let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
  11. var uniacid = JSON.stringify(extConfig) != "{}" ? extConfig.uniacid : '20236';
  12. this.siteInfo.uniacid = uniacid;
  13. this.siteInfo.acid = uniacid;
  14. wx.setStorageSync("store_id", uniacid);
  15. console.log('uniacid:'+uniacid);
  16. this.setApi();
  17. api = this.api;
  18. this.getNavigationBarColor();
  19. this.getStoreData();
  20. this.getCatList();
  21. },
  22. getStoreData: function () {
  23. var page = this;
  24. this.request({
  25. url: api.default.store,
  26. success: function (res) {
  27. if (res.code == 0) {
  28. wx.setStorageSync("store", res.data.store);
  29. wx.setStorageSync("store_name", res.data.store_name);
  30. wx.setStorageSync("show_customer_service", res.data.show_customer_service);
  31. wx.setStorageSync("contact_tel", res.data.contact_tel);
  32. wx.setStorageSync("share_setting", res.data.share_setting);
  33. }
  34. },
  35. complete: function () {
  36. page.login();
  37. }
  38. });
  39. },
  40. getCatList: function () {
  41. this.request({
  42. url: api.default.cat_list,
  43. success: function (res) {
  44. if (res.code == 0) {
  45. var cat_list = res.data.list || [];
  46. wx.setStorageSync("cat_list", cat_list);
  47. }
  48. }
  49. });
  50. },
  51. login: function () {
  52. var pages = getCurrentPages();
  53. var page = pages[(pages.length - 1)];
  54. wx.showLoading({
  55. title: "正在登录",
  56. mask: true,
  57. });
  58. wx.login({
  59. success: function (res) {
  60. if (res.code) {
  61. var code = res.code;
  62. wx.getUserInfo({
  63. success: function (res) {
  64. getApp().request({
  65. url: api.passport.login,
  66. method: "post",
  67. data: {
  68. code: code,
  69. user_info: res.rawData,
  70. encrypted_data: res.encryptedData,
  71. iv: res.iv,
  72. signature: res.signature
  73. },
  74. success: function (res) {
  75. wx.hideLoading();
  76. if (res.code == 0) {
  77. wx.setStorageSync("access_token", res.data.access_token);
  78. wx.setStorageSync("user_info", res.data);
  79. var p = getCurrentPages();
  80. var parent_id = 0;
  81. if (p[0].options.user_id != undefined) {
  82. var parent_id = p[0].options.user_id;
  83. }
  84. else if (p[0].options.scene != undefined) {
  85. var parent_id = p[0].options.scene;
  86. }
  87. getApp().bindParent({
  88. parent_id: parent_id || 0
  89. });
  90. if (page == undefined) {
  91. return;
  92. }
  93. var loginNoRefreshPage = getApp().loginNoRefreshPage;
  94. for (var i in loginNoRefreshPage) {
  95. if (loginNoRefreshPage[i] === page.route)
  96. return;
  97. }
  98. wx.redirectTo({
  99. url: "/" + page.route + "?" + util.objectToUrlParams(page.options),
  100. fail: function () {
  101. wx.switchTab({
  102. url: "/" + page.route,
  103. });
  104. },
  105. });
  106. } else {
  107. wx.showToast({
  108. title: res.msg
  109. });
  110. }
  111. }
  112. });
  113. },
  114. fail: function (res) {
  115. wx.hideLoading();
  116. getApp().getauth({
  117. content: '需要获取您的用户信息授权,请到小程序设置中打开授权',
  118. cancel: true,
  119. success: function (e) {
  120. if (e) {
  121. getApp().login();
  122. }
  123. },
  124. });
  125. }
  126. });
  127. } else {
  128. //console.log(res);
  129. }
  130. }
  131. });
  132. },
  133. request: function (object) {
  134. if (!object.data)
  135. object.data = {};
  136. var access_token = wx.getStorageSync("access_token");
  137. if (access_token) {
  138. object.data.access_token = access_token;
  139. }
  140. object.data._uniacid = this.siteInfo.uniacid;
  141. object.data._acid = this.siteInfo.acid;
  142. wx.request({
  143. url: object.url,
  144. header: object.header || {
  145. 'content-type': 'application/x-www-form-urlencoded'
  146. },
  147. data: object.data || {},
  148. method: object.method || "GET",
  149. dataType: object.dataType || "json",
  150. success: function (res) {
  151. if (res.data.code == -1) {
  152. getApp().login();
  153. } else {
  154. if (object.success)
  155. object.success(res.data);
  156. }
  157. },
  158. fail: function (res) {
  159. console.warn('--- request fail >>>');
  160. console.warn(res);
  161. console.warn('<<< request fail ---');
  162. var app = getApp();
  163. if (app.is_on_launch) {
  164. app.is_on_launch = false;
  165. wx.showModal({
  166. title: "网络请求出错",
  167. content: res.errMsg,
  168. showCancel: false,
  169. success: function (res) {
  170. if (res.confirm) {
  171. if (object.fail)
  172. object.fail(res);
  173. }
  174. }
  175. });
  176. } else {
  177. wx.showToast({
  178. title: res.errMsg,
  179. image: "/images/icon-warning.png",
  180. });
  181. if (object.fail)
  182. object.fail(res);
  183. }
  184. },
  185. complete: function (res) {
  186. if (res.statusCode != 200) {
  187. console.log('--- request http error >>>');
  188. console.log(res.statusCode);
  189. console.log(res.data);
  190. console.log('<<< request http error ---');
  191. }
  192. if (object.complete)
  193. object.complete(res);
  194. }
  195. });
  196. },
  197. saveFormId: function (form_id) {
  198. this.request({
  199. url: api.user.save_form_id,
  200. data: {
  201. form_id: form_id,
  202. }
  203. });
  204. },
  205. loginBindParent: function (object) {
  206. var access_token = wx.getStorageSync("access_token");
  207. if (access_token == '') {
  208. return true;
  209. }
  210. getApp().bindParent(object);
  211. },
  212. bindParent: function (object) {
  213. if (object.parent_id == "undefined" || object.parent_id == 0)
  214. return;
  215. console.log("Try To Bind Parent With User Id:" + object.parent_id);
  216. var user_info = wx.getStorageSync("user_info");
  217. var share_setting = wx.getStorageSync("share_setting");
  218. if (share_setting.level > 0) {
  219. var parent_id = object.parent_id;
  220. if (parent_id != 0) {
  221. getApp().request({
  222. url: api.share.bind_parent,
  223. data: {parent_id: object.parent_id},
  224. success: function (res) {
  225. if (res.code == 0) {
  226. user_info.parent = res.data
  227. wx.setStorageSync('user_info', user_info);
  228. }
  229. }
  230. });
  231. }
  232. }
  233. },
  234. /**
  235. * 分享送优惠券
  236. * */
  237. shareSendCoupon: function (page) {
  238. wx.showLoading({
  239. mask: true,
  240. });
  241. if (!page.hideGetCoupon) {
  242. page.hideGetCoupon = function (e) {
  243. var url = e.currentTarget.dataset.url || false;
  244. page.setData({
  245. get_coupon_list: null,
  246. });
  247. if (url) {
  248. wx.navigateTo({
  249. url: url,
  250. });
  251. }
  252. };
  253. }
  254. this.request({
  255. url: api.coupon.share_send,
  256. success: function (res) {
  257. if (res.code == 0) {
  258. page.setData({
  259. get_coupon_list: res.data.list
  260. });
  261. }
  262. },
  263. complete: function () {
  264. wx.hideLoading();
  265. }
  266. });
  267. },
  268. getauth: function (object) {
  269. wx.showModal({
  270. title: '是否打开设置页面重新授权',
  271. content: object.content,
  272. confirmText: '去设置',
  273. success: function (e) {
  274. if (e.confirm) {
  275. wx.openSetting({
  276. success: function (res) {
  277. if (object.success) {
  278. object.success(res);
  279. }
  280. },
  281. fail: function (res) {
  282. if (object.fail) {
  283. object.fail(res);
  284. }
  285. },
  286. complete: function (res) {
  287. if (object.complete)
  288. object.complete(res);
  289. }
  290. })
  291. } else {
  292. if (object.cancel) {
  293. getApp().getauth(object);
  294. }
  295. }
  296. }
  297. })
  298. },
  299. api: require('api.js'),
  300. setApi: function () {
  301. var siteroot = this.siteInfo.siteroot;
  302. siteroot = siteroot.replace('app/index.php', '');
  303. siteroot += 'addons/zjhj_mall/core/web/index.php?store_id=-1&r=api/';
  304. function getNewApiUri(api) {
  305. for (var i in api) {
  306. if (typeof api[i] === 'string') {
  307. api[i] = api[i].replace('{$_api_root}', siteroot);
  308. } else {
  309. api[i] = getNewApiUri(api[i]);
  310. }
  311. }
  312. return api;
  313. }
  314. this.api = getNewApiUri(this.api);
  315. var _index_api_url = this.api.default.index;
  316. var _web_root = _index_api_url.substr(0, _index_api_url.indexOf('/index.php'));
  317. this.webRoot = _web_root;
  318. },
  319. webRoot: null,
  320. siteInfo : {
  321. 'uniacid': '-1',
  322. 'acid': '-1',
  323. 'version': '1.0.0',
  324. 'siteroot': 'https://u5.9026.com/app/index.php',
  325. },
  326. currentPage: null,
  327. pageOnLoad: function (page) {
  328. this.currentPage = page;
  329. console.log('--------pageOnLoad----------');
  330. if (typeof page.openWxapp === 'undefined') {
  331. page.openWxapp = this.openWxapp;
  332. }
  333. if (typeof page.showToast === 'undefined') {
  334. page.showToast = this.pageShowToast;
  335. }
  336. this.setNavigationBarColor();
  337. this.setPageNavbar(page);
  338. var app = this;
  339. this.currentPage.naveClick = function(e){
  340. var page = this;
  341. app.navigatorClick(e,page);
  342. }
  343. wx.getSystemInfo({
  344. success: res => {
  345. let modelmes = res.model;
  346. if (modelmes.search('iPhone X') != -1) {
  347. page.setData({
  348. isIphoneX: true
  349. })
  350. }
  351. }
  352. })
  353. },
  354. pageOnReady: function (page) {
  355. console.log('--------pageOnReady----------');
  356. },
  357. pageOnShow: function (page) {
  358. console.log('--------pageOnShow----------');
  359. },
  360. pageOnHide: function (page) {
  361. console.log('--------pageOnHide----------');
  362. },
  363. pageOnUnload: function (page) {
  364. console.log('--------pageOnUnload----------');
  365. },
  366. setPageNavbar: function (page) {
  367. console.log('----setPageNavbar----');
  368. console.log(page);
  369. var navbar = wx.getStorageSync('_navbar');
  370. if (navbar) {
  371. setNavbar(navbar);
  372. }
  373. this.request({
  374. url: api.default.navbar,
  375. success: function (res) {
  376. if (res.code == 0) {
  377. setNavbar(res.data);
  378. wx.setStorageSync('_navbar', res.data);
  379. }
  380. }
  381. });
  382. function setNavbar(navbar) {
  383. var in_navs = false;
  384. var route = page.route || (page.__route__ || null);
  385. for (var i in navbar.navs) {
  386. if (navbar.navs[i].url === "/" + route) {
  387. navbar.navs[i].active = true;
  388. in_navs = true;
  389. } else {
  390. navbar.navs[i].active = false;
  391. }
  392. }
  393. if (!in_navs)
  394. return;
  395. page.setData({_navbar: navbar});
  396. }
  397. },
  398. getNavigationBarColor: function () {
  399. var app = this;
  400. app.request({
  401. url: api.default.navigation_bar_color,
  402. success: function (res) {
  403. if (res.code == 0) {
  404. wx.setStorageSync('_navigation_bar_color', res.data);
  405. app.setNavigationBarColor();
  406. }
  407. }
  408. });
  409. },
  410. setNavigationBarColor: function () {
  411. var navigation_bar_color = wx.getStorageSync('_navigation_bar_color');
  412. if (navigation_bar_color) {
  413. wx.setNavigationBarColor(navigation_bar_color);
  414. }
  415. },
  416. //登录成功后不刷新的页面
  417. loginNoRefreshPage: [
  418. 'pages/index/index',
  419. //'pages/fxhb/open/open',
  420. //'pages/fxhb/detail/detail',
  421. ],
  422. openWxapp: function (e) {
  423. console.log('--openWxapp---');
  424. if (!e.currentTarget.dataset.url)
  425. return;
  426. var url = e.currentTarget.dataset.url;
  427. url = parseQueryString(url);
  428. url.path = url.path ? decodeURIComponent(url.path) : "";
  429. console.log("Open New App");
  430. console.log(url);
  431. wx.navigateToMiniProgram({
  432. appId: url.appId,
  433. path: url.path,
  434. complete: function (e) {
  435. console.log(e);
  436. }
  437. });
  438. function parseQueryString(url) {
  439. var reg_url = /^[^\?]+\?([\w\W]+)$/,
  440. reg_para = /([^&=]+)=([\w\W]*?)(&|$|#)/g,
  441. arr_url = reg_url.exec(url),
  442. ret = {};
  443. if (arr_url && arr_url[1]) {
  444. var str_para = arr_url[1], result;
  445. while ((result = reg_para.exec(str_para)) != null) {
  446. ret[result[1]] = result[2];
  447. }
  448. }
  449. return ret;
  450. }
  451. },
  452. pageShowToast: function (e) {
  453. console.log('--- pageToast ---');
  454. var page = this.currentPage;
  455. var duration = e.duration || 2500;
  456. var title = e.title || '';
  457. var success = e.success || null;
  458. var fail = e.fail || null;
  459. var complete = e.complete || null;
  460. if (page._toast_timer) {
  461. clearTimeout(page._toast_timer);
  462. }
  463. page.setData({
  464. _toast: {
  465. title: title,
  466. },
  467. });
  468. page._toast_timer = setTimeout(function () {
  469. var _toast = page.data._toast;
  470. _toast.hide = true;
  471. page.setData({
  472. _toast: _toast,
  473. });
  474. if (typeof complete == 'function') {
  475. complete();
  476. }
  477. }, duration);
  478. },
  479. navigatorClick: function (e,page) {
  480. var open_type = e.currentTarget.dataset.open_type;
  481. if(open_type == 'redirect'){
  482. return true;
  483. }
  484. if (open_type == 'wxapp') {
  485. var path = e.currentTarget.dataset.path;
  486. var str = path.substr(0,1);
  487. if(str != '/'){
  488. path = '/' + path;
  489. }
  490. wx.navigateToMiniProgram({
  491. appId: e.currentTarget.dataset.appid,
  492. path: path,
  493. complete: function (e) {
  494. console.log(e);
  495. }
  496. });
  497. }
  498. if(open_type == 'tel'){
  499. var contact_tel = e.currentTarget.dataset.tel;
  500. wx.makePhoneCall({
  501. phoneNumber: contact_tel
  502. })
  503. }
  504. return false;
  505. function parseQueryString(url) {
  506. var reg_url = /^[^\?]+\?([\w\W]+)$/,
  507. reg_para = /([^&=]+)=([\w\W]*?)(&|$|#)/g,
  508. arr_url = reg_url.exec(url),
  509. ret = {};
  510. if (arr_url && arr_url[1]) {
  511. var str_para = arr_url[1], result;
  512. while ((result = reg_para.exec(str_para)) != null) {
  513. ret[result[1]] = result[2];
  514. }
  515. }
  516. return ret;
  517. }
  518. },
  519. });