app.js 13 KB

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