app.js 13 KB

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