wxh.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. // 取得授权地址
  148. var selfLocation = function (showMode) {
  149. const that = this;
  150. return new Promise((resolve, reject) => {
  151. let longitude = wx.getStorageSync(CACHE_LONGITUDE); //经度
  152. let latitude = wx.getStorageSync(CACHE_LATITUDE); //纬度
  153. if (longitude && latitude) {
  154. return resolve({
  155. longitude: longitude,
  156. latitude: latitude
  157. });
  158. }
  159. wx.getSetting({
  160. success: (res) => {
  161. if (!res.authSetting['scope.userLocation']) {
  162. wx.removeStorageSync(CACHE_LONGITUDE);
  163. wx.removeStorageSync(CACHE_LATITUDE);
  164. wx.getLocation({
  165. success: function (res) {
  166. var latitude = res.latitude;
  167. var longitude = res.longitude;
  168. wx.setStorageSync(CACHE_LONGITUDE, longitude);
  169. wx.setStorageSync(CACHE_LATITUDE, latitude);
  170. resolve(res);
  171. },
  172. fail(res) {
  173. if (res.errMsg == "getLocation:fail auth deny" && showMode == undefined) {
  174. wx.showModal({
  175. title: '您已经拒绝授权地理位置',
  176. content: '是否需要开启权限',
  177. success: function (res) {
  178. if (res.cancel) {
  179. reject(res);
  180. } else if (res.confirm) {
  181. wx.openSetting({
  182. success: function (res) {
  183. if (res.authSetting["scope.userLocation"] == true) {
  184. wx.showToast({
  185. title: '授权成功',
  186. icon: 'success',
  187. duration: 1000
  188. })
  189. wx.getLocation({
  190. success: function (res) {
  191. var latitude = res.latitude;
  192. var longitude = res.longitude;
  193. wx.setStorageSync(CACHE_LONGITUDE, longitude);
  194. wx.setStorageSync(CACHE_LATITUDE, latitude);
  195. resolve(res);
  196. }
  197. })
  198. } else {
  199. wx.showToast({
  200. title: '授权失败',
  201. icon: 'none',
  202. duration: 1000
  203. })
  204. }
  205. }
  206. })
  207. }
  208. },
  209. })
  210. }
  211. },
  212. });
  213. } else {
  214. wx.getLocation({
  215. success: function (res) {
  216. let latitude = res.latitude;
  217. let longitude = res.longitude;
  218. wx.setStorageSync(CACHE_LONGITUDE, longitude);
  219. wx.setStorageSync(CACHE_LATITUDE, latitude);
  220. resolve(res);
  221. }
  222. })
  223. }
  224. }
  225. })
  226. })
  227. }
  228. module.exports = {
  229. carmin: carmin,
  230. carjia: carjia,
  231. time: time,
  232. footan: footan,
  233. tapsize: tapsize,
  234. home: home,
  235. time2: time2,
  236. selfLocation: selfLocation
  237. }