app.js 13 KB

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