sam.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. import util from '@/common/we7_js/util.js'
  2. import request from '@/common/request.js'
  3. module.exports = {
  4. /**
  5. * 跳转到指定页面
  6. * 支持tabBar页面
  7. */
  8. navigateTo: function(url) {
  9. console.log(getCurrentPages().length);
  10. if (getCurrentPages().length < 6) {
  11. uni.navigateTo({
  12. url: url
  13. });
  14. } else {
  15. uni.reLaunch({
  16. url: url
  17. });
  18. }
  19. },
  20. diynavigateTo: function(e) {
  21. var link = e.currentTarget.dataset.url;
  22. if (link.ptype == 'custom') {
  23. if (link.zdyLinktype == 'wxapp') {
  24. uni.navigateToMiniProgram({
  25. appId: link.zdyappid,
  26. path: link.path
  27. })
  28. } else if (link.zdyLinktype == 'web') {
  29. this.navigateTo("/pages/webview/h5?url=" + link.path)
  30. } else {
  31. this.navigateTo(link.path)
  32. }
  33. } else {
  34. this.navigateTo(link.path)
  35. }
  36. },
  37. geturli: function() {
  38. let url = window.location.href;
  39. let urli = {
  40. i: 0
  41. };
  42. if (url.indexOf("?") != -1) {
  43. var str = url.split('?')[1];
  44. var strs = str.split("&");
  45. for (var i = 0; i < strs.length; i++) {
  46. if (strs[i].split("=")[0] && unescape(strs[i].split("=")[1])) {
  47. if (strs[i].split("=")[0] == "i") {
  48. urli.i = unescape(strs[i].split("=")[1]);
  49. }
  50. }
  51. }
  52. }
  53. return urli.i;
  54. },
  55. //设置缓存 (单位为秒)
  56. setStorage: function(key = ACCESS_TOKEN, value) {
  57. const params = {
  58. date: new Date().getTime(),
  59. value
  60. };
  61. uni.setStorageSync(key, JSON.stringify(params));
  62. },
  63. getStorage: function(key = ACCESS_TOKEN, day = 0.5) {
  64. let obj = uni.getStorageSync(key);
  65. if (!obj) return null;
  66. obj = JSON.parse(obj);
  67. const date = new Date().getTime();
  68. if (date - obj.date > 86400000 * day) return null;
  69. return obj.value;
  70. },
  71. /**
  72. * 判断变量是否为空,
  73. * @param {[type]} param 变量
  74. * @return {Boolean} 为空返回true,否则返回false。
  75. */
  76. isEmpty: function(param) {
  77. if (param) {
  78. var param_type = typeof(param);
  79. if (param_type == 'object') {
  80. //要判断的是【对象】或【数组】或【null】等
  81. if (typeof(param.length) == 'undefined') {
  82. if (JSON.stringify(param) == "{}") {
  83. return true; //空值,空对象
  84. }
  85. } else if (param.length == 0) {
  86. return true; //空值,空数组
  87. }
  88. } else if (param_type == 'string') {
  89. //如果要过滤空格等字符
  90. var new_param = param.trim();
  91. if (new_param.length == 0) {
  92. //空值,例如:带有空格的字符串" "。
  93. return true;
  94. }
  95. } else if (param_type == 'boolean') {
  96. if (!param) {
  97. return true;
  98. }
  99. } else if (param_type == 'number') {
  100. if (!param) {
  101. return true;
  102. }
  103. }
  104. return false; //非空值
  105. } else {
  106. //空值,例如:
  107. //(1)null
  108. //(2)可能使用了js的内置的名称,例如:var name=[],这个打印类型是字符串类型。
  109. //(3)空字符串''、""。
  110. //(4)数字0、00等,如果可以只输入0,则需要另外判断。
  111. return true;
  112. }
  113. },
  114. setUserGlobalData: function(param) {
  115. if (param) {
  116. getApp().globalData.memberInfo = param;
  117. getApp().globalData.uid = param.uid;
  118. }
  119. },
  120. onShowlogin: function() {
  121. if (uni.getStorageSync('memberInfo')) {
  122. this.login();
  123. }
  124. },
  125. checktelephone: function() {
  126. return new Promise((resolve, reject) => {
  127. var _this = this;
  128. util.getUserInfo(function(userInfo) {
  129. request.post('member.checktelephone', {
  130. samkey: (new Date()).valueOf()
  131. }).then(function(res) {
  132. if (res.data.is_gettelephone == 0) {
  133. uni.showToast({
  134. title: '您还未登录!',
  135. icon: 'success',
  136. duration: 1500
  137. });
  138. uni.reLaunch({
  139. url: "/pages/login/login?ptype=member",
  140. })
  141. } else {
  142. resolve(res.data);
  143. }
  144. })
  145. })
  146. // #ifdef APP-PLUS
  147. resolve({
  148. "uid": ''
  149. })
  150. // #endif
  151. })
  152. },
  153. login: function() {
  154. return new Promise((resolve, reject) => {
  155. var _this = this;
  156. var memberInfo = _this.getStorage("memberInfo", 0.001);
  157. if (memberInfo) {
  158. _this.setUserGlobalData(memberInfo);
  159. //console.log('m1');
  160. resolve(memberInfo)
  161. } else {
  162. //console.log('m2');
  163. util.getUserInfo(function(userInfo) {
  164. request.post('member.login', {
  165. samkey: (new Date()).valueOf()
  166. }).then(function(res) {
  167. if (res.data.errno == 0) {
  168. //console.log(res.data);
  169. _this.setUserGlobalData(res.data);
  170. _this.setStorage("memberInfo", res.data)
  171. resolve(res.data)
  172. } else if (res.data.errno == 20001) {
  173. uni.showToast({
  174. title: '账号审核中!',
  175. icon: 'success',
  176. duration: 1500
  177. });
  178. uni.redirectTo({
  179. url: "/pages/login/success",
  180. })
  181. } else if (res.data.errno == 10001) {
  182. uni.showToast({
  183. title: '您还未登录!',
  184. icon: 'success',
  185. duration: 1500
  186. });
  187. uni.reLaunch({
  188. url: "/pages/login/login?ptype=member",
  189. })
  190. }
  191. })
  192. })
  193. }
  194. // #ifdef APP-PLUS
  195. resolve({
  196. "uid": ''
  197. })
  198. // #endif
  199. })
  200. },
  201. //获取定位信息
  202. getCityPosition: function(param) {
  203. return new Promise((resolve, reject) => {
  204. var _this = this;
  205. if (!param) {
  206. param = {}
  207. }
  208. param.samkey = (new Date()).valueOf();
  209. util.getUserInfo(function(userInfo) {
  210. request.post('operatingcity.getcity', param).then(function(res) {
  211. if (res.is_nulldate == 1) {
  212. console.log('is_store');
  213. console.log(res.is_nulldate);
  214. resolve(res.data);
  215. } else {
  216. // #ifdef MP-WEIXIN
  217. if (res.is_close_getposition != 1) {
  218. wx.authorize({
  219. scope: 'scope.userFuzzyLocation',
  220. success: res => {
  221. //console.log(res)
  222. wx.getFuzzyLocation({
  223. type: 'wgs84',
  224. success(res) {
  225. uni.setStorageSync(
  226. 'latitude', res
  227. .latitude);
  228. uni.setStorageSync(
  229. 'longitude', res
  230. .longitude);
  231. //console.log(res);
  232. request.post(
  233. 'operatingcity.getcity', {
  234. samkey: (
  235. new Date()
  236. )
  237. .valueOf(),
  238. latitude: res
  239. .latitude,
  240. longitude: res
  241. .longitude
  242. }).then(res => {
  243. resolve(res
  244. .data
  245. );
  246. });
  247. }
  248. });
  249. },
  250. fail: res => {
  251. //console.log('失败:', res);
  252. resolve(res);
  253. }
  254. });
  255. }
  256. // #endif
  257. //#ifdef H5 || APP-PLUS
  258. uni.getLocation({
  259. type: 'wgs84',
  260. success: res => {
  261. //alert(res.latitude);
  262. uni.setStorageSync('latitude', res
  263. .latitude);
  264. uni.setStorageSync('longitude', res
  265. .longitude);
  266. //console.log(res);
  267. request.post('operatingcity.getcity', {
  268. samkey: (new Date()).valueOf(),
  269. latitude: res.latitude,
  270. longitude: res.longitude
  271. }).then(res => {
  272. resolve(res.data);
  273. });
  274. },
  275. fail: res => {
  276. //console.log('失败:', res);
  277. resolve(res);
  278. }
  279. })
  280. //#endif
  281. }
  282. })
  283. })
  284. })
  285. },
  286. /**
  287. * 保存图片
  288. */
  289. saveImage(url) {
  290. let that = this;
  291. // 向用户发起授权请求
  292. uni.authorize({
  293. scope: 'scope.writePhotosAlbum',
  294. success: () => {
  295. // 已授权
  296. that.downLoadImg(url);
  297. },
  298. fail: () => {
  299. // 拒绝授权,获取当前设置
  300. uni.getSetting({
  301. success: (result) => {
  302. if (!result.authSetting['scope.writePhotosAlbum']) {
  303. that.isAuth()
  304. }
  305. }
  306. });
  307. }
  308. })
  309. },
  310. /**
  311. * 下载资源,保存图片到系统相册
  312. */
  313. downLoadImg(url) {
  314. uni.showLoading({
  315. title: '加载中'
  316. });
  317. uni.downloadFile({
  318. url: url,
  319. success: (res) => {
  320. uni.hideLoading();
  321. if (res.statusCode === 200) {
  322. uni.saveImageToPhotosAlbum({
  323. filePath: res.tempFilePath,
  324. success: function() {
  325. uni.showToast({
  326. title: "保存成功",
  327. icon: "none"
  328. });
  329. },
  330. fail: function() {
  331. uni.showToast({
  332. title: "保存失败,请稍后重试",
  333. icon: "none"
  334. });
  335. }
  336. });
  337. }
  338. },
  339. fail: (err) => {
  340. uni.showToast({
  341. title: "失败啦",
  342. icon: "none"
  343. });
  344. }
  345. })
  346. },
  347. /*
  348. * 引导用户开启权限
  349. */
  350. isAuth() {
  351. uni.showModal({
  352. content: '由于您还没有允许保存图片到您相册里,无法进行保存,请点击确定允许授权',
  353. success: (res) => {
  354. if (res.confirm) {
  355. uni.openSetting({
  356. success: (result) => {
  357. console.log(result.authSetting);
  358. }
  359. });
  360. }
  361. }
  362. });
  363. },
  364. }