app.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. //var bmap = require('/mapjs/bmap-wx.min.js');
  2. var QQMapWX = require("./utils/qqmap-wx-jssdk.js");
  3. var map;
  4. App({
  5. /**
  6. * 全局变量
  7. */
  8. // apiRoot: "https://wj.songlanyun.com/wjxy/api/", // 正式环境api地址
  9. apiRoot: 'http://192.168.0.78:8687/qyh/api/',
  10. // apiRoot: 'https://wjxy.songlanyun.com/wjxy/api/',
  11. // apiRoot: 'http://47.104.208.8:8802/wjxy/api/',
  12. // apiRoot: 'http://192.168.0.232:8687/wjxy/api/', //黄理志
  13. // apiRoot: 'http://192.168.0.186:8687/wjxy/api/',
  14. siteInfo: require("siteinfo.js"),
  15. token: "",
  16. // appAssetsUrl: 'https://pm.lanzhongrenli.com/qmjz/app', //静态资源服务器
  17. appAssetsUrl: "https://wj.songlanyun.com/wjxy/statics/images/app", //静态资源服务器
  18. // appAssetsUrl2: 'http://192.168.0.101:8687/wjxy/statics/images/app/images/', //静态资源服务器
  19. appAssetsUrl2: "https://wj.songlanyun.com/wjxy/statics/images/app/images/", //静态资源服务器
  20. //自定义参数
  21. params: {
  22. pePageToIndex: "",
  23. },
  24. bottomLeft: 0,
  25. sexList: [],
  26. /**
  27. * 生命周期函数--监听小程序初始化
  28. */
  29. onLaunch: function () {
  30. let that = this;
  31. map = new QQMapWX({
  32. key: "RA3BZ-PSGW4-GZUUX-DDAU7-6B54E-KJFQ7", // 必填
  33. });
  34. console.log("首次启动");
  35. const updateManager = wx.getUpdateManager();
  36. updateManager.onCheckForUpdate(function (res) {
  37. // 请求完新版本信息的回调
  38. console.log(res.hasUpdate);
  39. });
  40. updateManager.onUpdateReady(function () {
  41. wx.showModal({
  42. title: "更新提示",
  43. content: "新版本已经准备好,是否重启应用?",
  44. success(res) {
  45. if (res.confirm) {
  46. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  47. updateManager.applyUpdate();
  48. }
  49. },
  50. });
  51. });
  52. updateManager.onUpdateFailed(function () {
  53. // 新版本下载失败
  54. wx.showToast({
  55. title: "版本更新失败,请检查网络!",
  56. icon: "none",
  57. duration: 1500,
  58. });
  59. });
  60. /* setInterval(function(){
  61. wx.removeStorageSync('Weather');
  62. that.getWeaTher();
  63. console.log("天气已更新");
  64. },7200000);*/
  65. //that.getWeaTher();
  66. // http://zqgj.vaiwan.com
  67. // this.setApiRoot("http://zqgj.vaiwan.com/zqgj");
  68. /* wx.getNetworkType({
  69. success(res) {
  70. const networkType = res.networkType
  71. wx.showToast({
  72. title: '当前网络为' + networkType + '',
  73. icon: 'none'
  74. })
  75. }
  76. })*/
  77. //this.getLocal();
  78. //ios安全区域
  79. wx.getSystemInfo({
  80. success: (res) => {
  81. this.bottomLeft = res.screenHeight - res.safeArea.bottom;
  82. },
  83. });
  84. wx.request({
  85. url: this.apiRoot + "bizlibrary/infoList/gender",
  86. method: "get",
  87. success: function (res) {
  88. console.log(res);
  89. if (res.data.code == 0) {
  90. that.sexList = res.data.dictList;
  91. }
  92. },
  93. });
  94. // that.font();
  95. },
  96. font() {
  97. wx.loadFontFace({
  98. family: "FZZhunYuan-M02S",
  99. global: true,
  100. source:
  101. 'url("https://wujiexiaoyuan-wechat.oss-cn-hangzhou.aliyuncs.com/font/FZZhunYuan-M02S.woff2")',
  102. // source: 'url("https://wujiexiaoyuan-wechat.oss-cn-hangzhou.aliyuncs.com/font/FZZhunYuan-M02S.ttf")',
  103. success: (msg) => {
  104. console.log(msg);
  105. },
  106. fail: (err) => {
  107. console.log(err);
  108. },
  109. complete: (res) => {
  110. console.log(res);
  111. },
  112. });
  113. wx.loadFontFace({
  114. family: "FZCuYuan-M03",
  115. global: true,
  116. source:
  117. 'url("https://wujiexiaoyuan-wechat.oss-cn-hangzhou.aliyuncs.com/font/yuanGBK.woff2")',
  118. // source: 'url("https://wujiexiaoyuan-wechat.oss-cn-hangzhou.aliyuncs.com/font/yuanGBK.ttf")',
  119. success: (msg) => {
  120. console.log(msg);
  121. },
  122. fail: (err) => {
  123. console.log(err);
  124. },
  125. complete: (res) => {
  126. console.log(res);
  127. },
  128. });
  129. },
  130. getLocal() {
  131. const _this = this;
  132. wx.getLocation({
  133. type: "gcj02",
  134. success(res) {
  135. const latitude = res.latitude;
  136. const longitude = res.longitude;
  137. //通过【小程序定位】获取经纬度,在通过第三方sdk获取的地址信息
  138. _this.pointToAddress(latitude, longitude, function (address) {
  139. if (address) {
  140. wx.setStorageSync("CHOOSECITY", {
  141. cityName: address.city,
  142. areaName: address.district,
  143. });
  144. let pages = getCurrentPages();
  145. let prevpage = pages[pages.length - 1];
  146. if (prevpage.hasOwnProperty("loadCity")) {
  147. prevpage.loadCity();
  148. }
  149. }
  150. });
  151. },
  152. fail() {
  153. let pages = getCurrentPages();
  154. let prevpage = pages[pages.length - 1];
  155. if (prevpage.hasOwnProperty("loadCity")) {
  156. prevpage.loadCity();
  157. }
  158. },
  159. });
  160. },
  161. // 定义 pointToAddress 方法
  162. pointToAddress: function (latitude, longitude, callback) {
  163. var _this = this;
  164. // 调用接口
  165. map.reverseGeocoder({
  166. location: {
  167. latitude: latitude,
  168. longitude: longitude,
  169. },
  170. success: function (res) {
  171. // 解析成功返回地址
  172. callback(res.result.ad_info);
  173. },
  174. fail: function (res) {
  175. console.log(res);
  176. },
  177. complete: function (res) {},
  178. });
  179. },
  180. /**
  181. * 当小程序启动,或从后台进入前台显示,会触发 onShow
  182. */
  183. /*
  184. onShow: function (options) {
  185. // 获取小程序基础信息
  186. this.getWxappBase(function (wxapp) {
  187. // 设置navbar标题、颜色
  188. wx.setNavigationBarColor({
  189. frontColor: wxapp.navbar.top_text_color.text,
  190. backgroundColor: wxapp.navbar.top_background_color
  191. })
  192. });
  193. },*/
  194. onShow: function (ops) {
  195. var that = this;
  196. wx.checkSession({
  197. success: function () {
  198. console.log("当前登录状态:有效");
  199. },
  200. fail: function () {},
  201. });
  202. wx.onNetworkStatusChange(function (res) {
  203. if (res.isConnected) {
  204. if (res.networkType !== "wifi") {
  205. wx.showToast({
  206. title: "注意!您正在使用" + res.networkType + "网络",
  207. icon: "none",
  208. });
  209. }
  210. }
  211. });
  212. },
  213. getWeaTher: function () {
  214. if (!wx.getStorageSync("Weather")) {
  215. // 新建百度地图对象
  216. var BMap = new bmap.BMapWX({
  217. ak: "pSzxYKshUcuo1Y9pPrbFs6r7LGvx8sxg",
  218. });
  219. var fail = function (data) {
  220. // console.log(data)
  221. };
  222. var success = function (data) {
  223. var weatherData = data.currentWeather[0];
  224. /* weatherData = '城市:' + weatherData.currentCity + '\n' + 'PM2.5:' + weatherData.pm25 + '\n' + '日期:' + weatherData.date + '\n' + '温度:' + weatherData.temperature + '\n' + '天气:' + weatherData.weatherDesc + '\n' + '风力:' + weatherData.wind + '\n';*/
  225. //Weather(weatherData.weatherDesc, _this, weatherData);
  226. wx.setStorageSync("Weather", weatherData);
  227. };
  228. // 发起weather请求
  229. BMap.weather({
  230. fail: fail,
  231. success: success,
  232. });
  233. }
  234. },
  235. /**
  236. * 设置api地址
  237. */
  238. setApiRoot: function () {
  239. //this.apiRoot = this.siteInfo.siteroot + '/api/';
  240. },
  241. /**
  242. * 获取小程序基础信息
  243. */
  244. /*
  245. getWxappBase: function (callback) {
  246. let App = this;
  247. App._get('wxapp/base', {}, function (result) {
  248. // 记录小程序基础信息
  249. wx.setStorageSync('wxapp', result.data.wxapp);
  250. callback && callback(result.data.wxapp);
  251. }, false, false);
  252. },*/
  253. /**
  254. * 显示成功提示框
  255. */
  256. showSuccess: function (msg, callback) {
  257. wx.showToast({
  258. title: msg,
  259. icon: "success",
  260. success: function () {
  261. callback &&
  262. setTimeout(function () {
  263. callback();
  264. }, 1500);
  265. },
  266. });
  267. },
  268. /**
  269. * 显示失败提示框
  270. */
  271. showError: function (msg, callback) {
  272. wx.showToast({
  273. title: msg,
  274. icon: "none",
  275. duration: 2000,
  276. success: function (res) {
  277. callback && callback();
  278. },
  279. });
  280. },
  281. /**
  282. * get请求
  283. */
  284. _get: function (url, data, success, fail, complete) {
  285. wx.showNavigationBarLoading();
  286. let App = this;
  287. // 构造请求参数
  288. data = data || {};
  289. wx.request({
  290. url: App.apiRoot + url,
  291. data: data,
  292. method: "get",
  293. success: function (res) {
  294. wx.hideNavigationBarLoading();
  295. wx.hideLoading();
  296. if (res.statusCode !== 200 || typeof res.data !== "object") {
  297. return false;
  298. }
  299. if (res.data.code === -1) {
  300. // 登录态失效, 重新登录
  301. return false;
  302. } else {
  303. success && success(res.data);
  304. }
  305. },
  306. fail: function (res) {
  307. wx.hideNavigationBarLoading();
  308. // console.log(res);
  309. App.showError(res.errMsg, function () {
  310. fail && fail(res);
  311. });
  312. },
  313. complete: function (res) {
  314. wx.hideNavigationBarLoading();
  315. complete && complete(res);
  316. },
  317. });
  318. },
  319. /**
  320. * post提交
  321. */
  322. _post_form: function (url, header, data, success, fail, complete) {
  323. wx.showNavigationBarLoading();
  324. let App = this;
  325. wx.request({
  326. url: App.apiRoot + url,
  327. header: {
  328. "content-type": header,
  329. },
  330. method: "POST",
  331. data: data,
  332. success: function (res) {
  333. wx.showNavigationBarLoading();
  334. wx.hideLoading();
  335. if (res.statusCode !== 200 && typeof res.data.code !== 1) {
  336. App.showError("网络请求出错");
  337. return false;
  338. }
  339. if (res.data.code === -1) {
  340. // 登录态失效, 重新登录
  341. App.doLogin(function () {
  342. App._post_form(url, data, success, fail);
  343. });
  344. return false;
  345. } else if (res.data.code === 0) {
  346. success && success(res.data);
  347. } else if (res.data.retCode === "000000") {
  348. success && success(res.data);
  349. return;
  350. } else {
  351. var msg = "";
  352. if (res.data.message) {
  353. msg = res.data.message;
  354. } else if (res.data.msg) {
  355. msg = res.data.msg;
  356. }
  357. App.showError(msg);
  358. success && success(res.data);
  359. return false;
  360. }
  361. },
  362. fail: function (res) {
  363. wx.showNavigationBarLoading();
  364. App.showError(res.errMsg, function () {
  365. fail && fail(res);
  366. });
  367. },
  368. complete: function (res) {
  369. complete && complete(res);
  370. wx.hideNavigationBarLoading();
  371. },
  372. });
  373. },
  374. // put 请求
  375. _put_form: function (url, header, data, success, fail, complete) {
  376. wx.showNavigationBarLoading();
  377. let App = this;
  378. wx.request({
  379. url: App.apiRoot + url,
  380. header: {
  381. "content-type": header,
  382. },
  383. method: "PUT",
  384. data: data,
  385. success: function (res) {
  386. wx.showNavigationBarLoading();
  387. wx.hideLoading();
  388. if (res.statusCode !== 200 && typeof res.data.code !== 1) {
  389. App.showError("网络请求出错");
  390. return false;
  391. }
  392. if (res.data.code === -1) {
  393. // 登录态失效, 重新登录
  394. App.doLogin(function () {
  395. App._post_form(url, data, success, fail);
  396. });
  397. return false;
  398. } else if (res.data.code === 0) {
  399. success && success(res.data);
  400. } else if (res.data.retCode === "000000") {
  401. success && success(res.data);
  402. return;
  403. } else {
  404. var msg = "";
  405. if (res.data.message) {
  406. msg = res.data.message;
  407. } else if (res.data.msg) {
  408. msg = res.data.msg;
  409. }
  410. // App.showError(msg);
  411. console.log(msg, 11111111);
  412. success && success(res.data);
  413. wx.showToast({
  414. title: msg,
  415. icon: "none",
  416. duration: 2000,
  417. });
  418. return false;
  419. }
  420. },
  421. fail: function (res) {
  422. wx.showNavigationBarLoading();
  423. App.showError(res.errMsg, function () {
  424. fail && fail(res);
  425. });
  426. },
  427. complete: function (res) {
  428. complete && complete(res);
  429. wx.hideNavigationBarLoading();
  430. },
  431. });
  432. },
  433. /**
  434. * 对象转URL
  435. */
  436. urlEncode: function urlencode(data) {
  437. var _result = [];
  438. for (var key in data) {
  439. var value = data[key];
  440. if (value.constructor == Array) {
  441. value.forEach(function (_value) {
  442. _result.push(key + "=" + _value);
  443. });
  444. } else {
  445. _result.push(key + "=" + value);
  446. }
  447. }
  448. return _result.join("&");
  449. },
  450. city: function () {
  451. return cityCode;
  452. },
  453. /**
  454. * 设置当前页面标题
  455. */
  456. /*
  457. setTitle: function() {
  458. let App = this,
  459. wxapp;
  460. if (wxapp = wx.getStorageSync('wxapp')) {
  461. wx.setNavigationBarTitle({
  462. title: wxapp.navbar.wxapp_title
  463. });
  464. } else {
  465. App.getWxappBase(function() {
  466. App.setTitle();
  467. });
  468. }
  469. },
  470. */
  471. });