app.js 18 KB

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