wxh.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import {
  2. CACHE_LONGITUDE,
  3. CACHE_LATITUDE
  4. } from '../config.js';
  5. //购物车减
  6. var carmin = function (that) {
  7. var num = that.data.num;
  8. // 如果大于1时,才可以减
  9. if (num > 1) {
  10. num--;
  11. }
  12. // 只有大于一件的时候,才能normal状态,否则disable状态
  13. var minusStatus = num <= 1 ? 'disabled' : 'normal';
  14. // 将数值与状态写回
  15. that.setData({
  16. num: num,
  17. minusStatus: minusStatus
  18. });
  19. }
  20. //返回首页
  21. var home = function (that, e) {
  22. if (e.touches[0].clientY < 500 && e.touches[0].clientY > 0) {
  23. that.setData({
  24. top: e.touches[0].clientY
  25. })
  26. }
  27. }
  28. //购物车加
  29. var carjia = function (that) {
  30. var num = that.data.num;
  31. // 不作过多考虑自增1
  32. num++;
  33. // 只有大于一件的时候,才能normal状态,否则disable状态
  34. var minusStatus = num < 1 ? 'disabled' : 'normal';
  35. // 将数值与状态写回
  36. that.setData({
  37. num: num,
  38. minusStatus: minusStatus
  39. });
  40. }
  41. //倒计时;
  42. var time = function (timeStamp, that) {
  43. var interval = null,
  44. totalSecond = timeStamp - Date.parse(new Date()) / 1000;
  45. interval = setInterval(function () {
  46. // 秒数
  47. var second = totalSecond;
  48. // 小时位
  49. var hr = Math.floor(second / 3600);
  50. var hrStr = hr.toString();
  51. if (hrStr.length == 1) hrStr = '0' + hrStr;
  52. // 分钟位
  53. var min = Math.floor((second - hr * 3600) / 60);
  54. var minStr = min.toString();
  55. if (minStr.length == 1) minStr = '0' + minStr;
  56. // 秒位
  57. var sec = second - hr * 3600 - min * 60;
  58. var secStr = sec.toString();
  59. if (secStr.length == 1) secStr = '0' + secStr;
  60. that.setData({
  61. countDownHour: hrStr,
  62. countDownMinute: minStr,
  63. countDownSecond: secStr,
  64. });
  65. totalSecond--;
  66. if (totalSecond <= 0) {
  67. clearInterval(interval);
  68. wx.showToast({
  69. title: '活动已结束',
  70. icon: 'none',
  71. duration: 1000,
  72. mask: true,
  73. })
  74. that.setData({
  75. countDownHour: '00',
  76. countDownMinute: '00',
  77. countDownSecond: '00',
  78. });
  79. }
  80. }.bind(that), 1000);
  81. that.setData({
  82. interval: interval
  83. });
  84. }
  85. //倒计时2;
  86. var time2 = function (timeStamp, that) {
  87. var totalSecond = timeStamp - Date.parse(new Date()) / 1000;
  88. var interval = setInterval(function () {
  89. // 秒数
  90. var second = totalSecond;
  91. // // 天数位
  92. var day = Math.floor(second / 3600 / 24);
  93. var dayStr = day.toString();
  94. if (dayStr.length == 1) dayStr = '0' + dayStr;
  95. // 小时位
  96. var hr = Math.floor((second - day * 3600 * 24) / (60 * 60));
  97. var hrStr = hr.toString();
  98. if (hrStr.length == 1) hrStr = '0' + hrStr;
  99. // 分钟位
  100. // var min = Math.floor((second - hr * 3600) / 60);
  101. var min = Math.floor((second - day * 3600 * 24 - hr * 3600) / 60);
  102. var minStr = min.toString();
  103. if (minStr.length == 1) minStr = '0' + minStr;
  104. // 秒位
  105. // var sec = second - hrNew * 3600 - min * 60;
  106. var sec = Math.floor(second - day * 3600 * 24 - hr * 3600 - min * 60);
  107. var secStr = sec.toString();
  108. if (secStr.length == 1) secStr = '0' + secStr;
  109. that.setData({
  110. countDownDay: dayStr,
  111. countDownHour: hrStr,
  112. countDownMinute: minStr,
  113. countDownSecond: secStr,
  114. });
  115. totalSecond--;
  116. if (totalSecond <= 0) {
  117. clearInterval(interval);
  118. wx.showToast({
  119. title: '活动已结束',
  120. });
  121. that.setData({
  122. countDownDay: '00',
  123. countDownHour: '00',
  124. countDownMinute: '00',
  125. countDownSecond: '00',
  126. });
  127. }
  128. }.bind(that), 1000);
  129. that.setData({
  130. interval: interval
  131. });
  132. }
  133. var footan = function (that) {
  134. that.setData({
  135. prostatus: true,
  136. show: false
  137. })
  138. }
  139. var tapsize = function (that, e) {
  140. var $indexs = e.target.dataset.indexs; //内
  141. var $index = e.target.dataset.index; //外
  142. that.setData({
  143. taberindexs: $indexs,
  144. taberindex: $index
  145. })
  146. }
  147. var selfLocation = function (showMode) {
  148. const that = this;
  149. return new Promise((resolve, reject) => {
  150. let longitude = wx.getStorageSync(CACHE_LONGITUDE); //经度
  151. let latitude = wx.getStorageSync(CACHE_LATITUDE); //纬度
  152. if (longitude && latitude) {
  153. return resolve({
  154. longitude: longitude,
  155. latitude: latitude
  156. });
  157. }
  158. wx.getSetting({
  159. success: (res) => {
  160. if (!res.authSetting['scope.userLocation']) {
  161. wx.removeStorageSync(CACHE_LONGITUDE);
  162. wx.removeStorageSync(CACHE_LATITUDE);
  163. wx.getLocation({
  164. success: function (res) {
  165. var latitude = res.latitude;
  166. var longitude = res.longitude;
  167. wx.setStorageSync(CACHE_LONGITUDE, longitude);
  168. wx.setStorageSync(CACHE_LATITUDE, latitude);
  169. resolve(res);
  170. },
  171. fail(res) {
  172. if (res.errMsg == "getLocation:fail auth deny" && showMode == undefined) {
  173. wx.showModal({
  174. title: '您已经拒绝授权地理位置',
  175. content: '是否需要开启权限',
  176. success: function (res) {
  177. if (res.cancel) {
  178. reject(res);
  179. } else if (res.confirm) {
  180. wx.openSetting({
  181. success: function (res) {
  182. if (res.authSetting["scope.userLocation"] == true) {
  183. wx.showToast({
  184. title: '授权成功',
  185. icon: 'success',
  186. duration: 1000
  187. })
  188. wx.getLocation({
  189. success: function (res) {
  190. var latitude = res.latitude;
  191. var longitude = res.longitude;
  192. wx.setStorageSync(CACHE_LONGITUDE, longitude);
  193. wx.setStorageSync(CACHE_LATITUDE, latitude);
  194. resolve(res);
  195. }
  196. })
  197. } else {
  198. wx.showToast({
  199. title: '授权失败',
  200. icon: 'none',
  201. duration: 1000
  202. })
  203. }
  204. }
  205. })
  206. }
  207. },
  208. })
  209. }
  210. },
  211. });
  212. } else {
  213. wx.getLocation({
  214. success: function (res) {
  215. let latitude = res.latitude;
  216. let longitude = res.longitude;
  217. wx.setStorageSync(CACHE_LONGITUDE, longitude);
  218. wx.setStorageSync(CACHE_LATITUDE, latitude);
  219. resolve(res);
  220. }
  221. })
  222. }
  223. }
  224. })
  225. })
  226. }
  227. module.exports = {
  228. carmin: carmin,
  229. carjia: carjia,
  230. time: time,
  231. footan: footan,
  232. tapsize: tapsize,
  233. home: home,
  234. time2: time2,
  235. selfLocation: selfLocation
  236. }