app.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 (that) {
  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(that);
  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: require("utils/login.js"),
  52. request: require("utils/request.js"),
  53. saveFormId: function (form_id) {
  54. this.request({
  55. url: api.user.save_form_id,
  56. data: {
  57. form_id: form_id,
  58. }
  59. });
  60. },
  61. loginBindParent: function (object) {
  62. var access_token = wx.getStorageSync("access_token");
  63. if (access_token == '') {
  64. return true;
  65. }
  66. getApp().bindParent(object);
  67. },
  68. bindParent: function (object) {
  69. if (object.parent_id == "undefined" || object.parent_id == 0)
  70. return;
  71. console.log("Try To Bind Parent With User Id:" + object.parent_id);
  72. var user_info = wx.getStorageSync("user_info");
  73. var share_setting = wx.getStorageSync("share_setting");
  74. if (share_setting.level > 0) {
  75. var parent_id = object.parent_id;
  76. if (parent_id != 0) {
  77. getApp().request({
  78. url: api.share.bind_parent,
  79. data: {parent_id: object.parent_id},
  80. success: function (res) {
  81. if (res.code == 0) {
  82. user_info.parent = res.data
  83. wx.setStorageSync('user_info', user_info);
  84. }
  85. }
  86. });
  87. }
  88. }
  89. },
  90. /**
  91. * 分享送优惠券
  92. * */
  93. shareSendCoupon: function (page) {
  94. wx.showLoading({
  95. mask: true,
  96. });
  97. if (!page.hideGetCoupon) {
  98. page.hideGetCoupon = function (e) {
  99. var url = e.currentTarget.dataset.url || false;
  100. page.setData({
  101. get_coupon_list: null,
  102. });
  103. if (url) {
  104. wx.navigateTo({
  105. url: url,
  106. });
  107. }
  108. };
  109. }
  110. this.request({
  111. url: api.coupon.share_send,
  112. success: function (res) {
  113. if (res.code == 0) {
  114. page.setData({
  115. get_coupon_list: res.data.list
  116. });
  117. }
  118. },
  119. complete: function () {
  120. wx.hideLoading();
  121. }
  122. });
  123. },
  124. getauth: function (object) {
  125. wx.showModal({
  126. title: '是否打开设置页面重新授权',
  127. content: object.content,
  128. confirmText: '去设置',
  129. success: function (e) {
  130. if (e.confirm) {
  131. wx.openSetting({
  132. success: function (res) {
  133. if (object.success) {
  134. object.success(res);
  135. }
  136. },
  137. fail: function (res) {
  138. if (object.fail) {
  139. object.fail(res);
  140. }
  141. },
  142. complete: function (res) {
  143. if (object.complete)
  144. object.complete(res);
  145. }
  146. })
  147. } else {
  148. if (object.cancel) {
  149. getApp().getauth(object);
  150. }
  151. }
  152. }
  153. })
  154. },
  155. api: require('api.js'),
  156. setApi: function () {
  157. var siteroot = this.siteInfo.siteroot;
  158. siteroot = siteroot.replace('app/index.php', '');
  159. siteroot += 'addons/zjhj_mall/core/web/index.php?store_id=-1&r=api/';
  160. function getNewApiUri(api) {
  161. for (var i in api) {
  162. if (typeof api[i] === 'string') {
  163. api[i] = api[i].replace('{$_api_root}', siteroot);
  164. } else {
  165. api[i] = getNewApiUri(api[i]);
  166. }
  167. }
  168. return api;
  169. }
  170. this.api = getNewApiUri(this.api);
  171. var _index_api_url = this.api.default.index;
  172. var _web_root = _index_api_url.substr(0, _index_api_url.indexOf('/index.php'));
  173. this.webRoot = _web_root;
  174. },
  175. webRoot: null,
  176. siteInfo : {
  177. 'uniacid': '-1',
  178. 'acid': '-1',
  179. 'version': '1.0.0',
  180. 'siteroot': 'https://u5.9026.com/app/index.php',
  181. 'siteroot': 'https://u5.9026.com/app/index.php',
  182. },
  183. globalData:{
  184. 'userInfo': null,
  185. },
  186. currentPage: null,
  187. pageOnLoad: function (page) {
  188. this.currentPage = page;
  189. console.log('--------pageOnLoad----------');
  190. if (typeof page.openWxapp === 'undefined') {
  191. page.openWxapp = this.openWxapp;
  192. }
  193. if (typeof page.showToast === 'undefined') {
  194. page.showToast = this.pageShowToast;
  195. }
  196. this.setNavigationBarColor();
  197. this.setPageNavbar(page);
  198. var app = this;
  199. this.currentPage.naveClick = function(e){
  200. var page = this;
  201. app.navigatorClick(e,page);
  202. }
  203. wx.getSystemInfo({
  204. success: res => {
  205. let modelmes = res.model;
  206. if (modelmes.search('iPhone X') != -1) {
  207. page.setData({
  208. isIphoneX: true
  209. })
  210. }
  211. }
  212. })
  213. },
  214. pageOnReady: function (page) {
  215. console.log('--------pageOnReady----------');
  216. },
  217. pageOnShow: function (page) {
  218. console.log('--------pageOnShow----------');
  219. },
  220. pageOnHide: function (page) {
  221. console.log('--------pageOnHide----------');
  222. },
  223. pageOnUnload: function (page) {
  224. console.log('--------pageOnUnload----------');
  225. },
  226. setPageNavbar: function (page) {
  227. console.log('----setPageNavbar----');
  228. console.log(page);
  229. var navbar = wx.getStorageSync('_navbar');
  230. if (navbar) {
  231. setNavbar(navbar);
  232. }
  233. this.request({
  234. url: api.default.navbar,
  235. success: function (res) {
  236. if (res.code == 0) {
  237. setNavbar(res.data);
  238. wx.setStorageSync('_navbar', res.data);
  239. }
  240. }
  241. });
  242. function setNavbar(navbar) {
  243. var in_navs = false;
  244. var route = page.route || (page.__route__ || null);
  245. for (var i in navbar.navs) {
  246. if (navbar.navs[i].url === "/" + route) {
  247. navbar.navs[i].active = true;
  248. in_navs = true;
  249. } else {
  250. navbar.navs[i].active = false;
  251. }
  252. }
  253. if (!in_navs)
  254. return;
  255. page.setData({_navbar: navbar});
  256. }
  257. },
  258. getNavigationBarColor: function () {
  259. var app = this;
  260. app.request({
  261. url: api.default.navigation_bar_color,
  262. success: function (res) {
  263. if (res.code == 0) {
  264. wx.setStorageSync('_navigation_bar_color', res.data);
  265. app.setNavigationBarColor();
  266. }
  267. }
  268. });
  269. },
  270. setNavigationBarColor: function () {
  271. var navigation_bar_color = wx.getStorageSync('_navigation_bar_color');
  272. if (navigation_bar_color) {
  273. wx.setNavigationBarColor(navigation_bar_color);
  274. }
  275. },
  276. //登录成功后不刷新的页面
  277. loginNoRefreshPage: [
  278. 'pages/index/index',
  279. //'pages/fxhb/open/open',
  280. //'pages/fxhb/detail/detail',
  281. ],
  282. openWxapp: function (e) {
  283. console.log('--openWxapp---');
  284. if (!e.currentTarget.dataset.url)
  285. return;
  286. var url = e.currentTarget.dataset.url;
  287. url = parseQueryString(url);
  288. url.path = url.path ? decodeURIComponent(url.path) : "";
  289. console.log("Open New App");
  290. console.log(url);
  291. wx.navigateToMiniProgram({
  292. appId: url.appId,
  293. path: url.path,
  294. complete: function (e) {
  295. console.log(e);
  296. }
  297. });
  298. function parseQueryString(url) {
  299. var reg_url = /^[^\?]+\?([\w\W]+)$/,
  300. reg_para = /([^&=]+)=([\w\W]*?)(&|$|#)/g,
  301. arr_url = reg_url.exec(url),
  302. ret = {};
  303. if (arr_url && arr_url[1]) {
  304. var str_para = arr_url[1], result;
  305. while ((result = reg_para.exec(str_para)) != null) {
  306. ret[result[1]] = result[2];
  307. }
  308. }
  309. return ret;
  310. }
  311. },
  312. pageShowToast: function (e) {
  313. console.log('--- pageToast ---');
  314. var page = this.currentPage;
  315. var duration = e.duration || 2500;
  316. var title = e.title || '';
  317. var success = e.success || null;
  318. var fail = e.fail || null;
  319. var complete = e.complete || null;
  320. if (page._toast_timer) {
  321. clearTimeout(page._toast_timer);
  322. }
  323. page.setData({
  324. _toast: {
  325. title: title,
  326. },
  327. });
  328. page._toast_timer = setTimeout(function () {
  329. var _toast = page.data._toast;
  330. _toast.hide = true;
  331. page.setData({
  332. _toast: _toast,
  333. });
  334. if (typeof complete == 'function') {
  335. complete();
  336. }
  337. }, duration);
  338. },
  339. navigatorClick: function (e,page) {
  340. var open_type = e.currentTarget.dataset.open_type;
  341. if(open_type == 'redirect'){
  342. return true;
  343. }
  344. if (open_type == 'wxapp') {
  345. var path = e.currentTarget.dataset.path;
  346. var str = path.substr(0,1);
  347. if(str != '/'){
  348. path = '/' + path;
  349. }
  350. wx.navigateToMiniProgram({
  351. appId: e.currentTarget.dataset.appid,
  352. path: path,
  353. complete: function (e) {
  354. console.log(e);
  355. }
  356. });
  357. }
  358. if(open_type == 'tel'){
  359. var contact_tel = e.currentTarget.dataset.tel;
  360. wx.makePhoneCall({
  361. phoneNumber: contact_tel
  362. })
  363. }
  364. return false;
  365. function parseQueryString(url) {
  366. var reg_url = /^[^\?]+\?([\w\W]+)$/,
  367. reg_para = /([^&=]+)=([\w\W]*?)(&|$|#)/g,
  368. arr_url = reg_url.exec(url),
  369. ret = {};
  370. if (arr_url && arr_url[1]) {
  371. var str_para = arr_url[1], result;
  372. while ((result = reg_para.exec(str_para)) != null) {
  373. ret[result[1]] = result[2];
  374. }
  375. }
  376. return ret;
  377. }
  378. },
  379. });