index.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // index.js
  2. let myPro = require("../../utils/wxRequest.js");
  3. let util = require("../../utils/util.js");
  4. Page({
  5. data: {
  6. imgUrl: getApp().globalData.imgUrl,
  7. loading: false, // 全局loading
  8. page: 1,
  9. size: 10,
  10. finished: false,
  11. storeList: [], // 店列表
  12. fare_type: 1, // 0外卖 1自提 默认自提
  13. showOpenLocation: false, // 开启定位提醒
  14. showTips: false,
  15. checkedAddress: null,
  16. lat: null,
  17. lng: null
  18. },
  19. onLoad: function (options) {
  20. // Do some initialize when page load.
  21. let that = this;
  22. // 拒绝定位时,先提醒去开启
  23. if(getApp().globalData.location_auth == -1){
  24. that.setData({
  25. showOpenLocation: true
  26. });
  27. }
  28. },
  29. onShow: function () {
  30. // Do something when page show.
  31. let that = this;
  32. // 已选的店
  33. if(wx.getStorageSync("store")){
  34. that.setData({
  35. store: JSON.parse(wx.getStorageSync("store"))
  36. })
  37. };
  38. // 获取配送流程
  39. if(wx.getStorageSync("fare_type")){
  40. that.setData({
  41. fare_type: wx.getStorageSync("fare_type")
  42. });
  43. };
  44. // 获取配送地址
  45. if(wx.getStorageSync("checkedAddress")){
  46. that.setData({
  47. checkedAddress: JSON.parse(wx.getStorageSync("checkedAddress"))
  48. });
  49. };
  50. // 经纬度
  51. if(that.data.fare_type == 0 && that.data.checkedAddress){
  52. // 外卖 按收货地址的经纬度
  53. that.setData({
  54. lat: that.data.checkedAddress.lat,
  55. lng: that.data.checkedAddress.lnt
  56. })
  57. }else{
  58. // 自取 按定位的经纬度
  59. that.setData({
  60. lat: getApp().globalData.lat,
  61. lng: getApp().globalData.lng
  62. })
  63. }
  64. // 已定位
  65. if(getApp().globalData.location_auth == 1){
  66. // 已定位
  67. that.setData({
  68. page: 1,
  69. storeList: [],
  70. finished: false
  71. });
  72. that.getStoreList();
  73. }
  74. },
  75. onReady: function () {
  76. // Do something when page ready.
  77. },
  78. onHide: function () {
  79. // Do something when page hide.
  80. },
  81. onUnload: function () {
  82. // Do something when page close.
  83. },
  84. onPullDownRefresh: function () {
  85. },
  86. onReachBottom: function () {
  87. // Do something when page reach bottom.
  88. let that = this;
  89. if(!that.data.finished){
  90. that.getStoreList();
  91. };
  92. },
  93. onPageScroll: function () {
  94. // Do something when page scroll
  95. },
  96. onResize: function () {
  97. // Do something when page resize
  98. },
  99. // 获取店列表
  100. getStoreList(){
  101. let that = this;
  102. let data = {
  103. lat: that.data.lat,
  104. lng: that.data.lng,
  105. page: that.data.page,
  106. size: that.data.size
  107. };
  108. // console.log('参数的经纬度',that.data.lat,that.data.lng);
  109. myPro.wxRequest("user/store/list","GET",data).then(res=>{
  110. let list = res.result;
  111. // 无数据
  112. if(that.data.fare_type == 0 && that.data.page == 1 && list.length == 0 ){
  113. that.setData({
  114. finished: true,
  115. showTips: true,
  116. tipsTitle: "抱歉,您的配送地址内暂无可用门店",
  117. tipsMessage: "可前往自取或更换地址,不便之处敬请见谅"
  118. });
  119. // 清掉所选地址
  120. wx.removeStorageSync('checkedAddress');
  121. }else if(list.length == 0){
  122. that.setData({
  123. finished: true
  124. });
  125. wx.showToast({
  126. title: "暂无更多数据",
  127. icon: "none"
  128. });
  129. }else{
  130. // 有数据
  131. for(let i in list){
  132. if(i == 0 && that.data.page == 1){
  133. list[i].is_tuijian = 1; // 推荐距离最近的店
  134. };
  135. if(that.data.store){
  136. if(that.data.store.id == list[i].id){
  137. list[i].is_selected = 1;
  138. }
  139. }else{
  140. // if(i == 0){
  141. // list[i].is_selected = 1;
  142. // }else{
  143. // list[i].is_selected = 0;
  144. // }
  145. list[i].is_selected = 0;
  146. }
  147. };
  148. that.setData({
  149. storeList: that.data.storeList.concat(list),
  150. page: that.data.page + 1
  151. });
  152. }
  153. }).catch(err=>{
  154. console.log('报错信息',err)
  155. wx.showToast({
  156. title: err,
  157. icon: "none",
  158. });
  159. });
  160. },
  161. // 主动切换门店
  162. onSelectStore(event){
  163. let that = this;
  164. let item = event.currentTarget.dataset.item;
  165. let list = that.data.storeList;
  166. let currentObj = null;
  167. // 判断点选店铺是否支持所选配送方式
  168. if(that.data.fare_type == 0){
  169. if(!item.is_delivery){
  170. that.setData({
  171. showTips: true,
  172. tipsTitle: "抱歉,此门店暂不支持外卖",
  173. tipsMessage: "可前往自取或更换地址,不便之处敬请见谅"
  174. })
  175. return;
  176. }
  177. }else{
  178. if(!item.is_ziti){
  179. that.setData({
  180. showTips: true,
  181. tipsTitle: "抱歉,此门店暂不支持自取",
  182. tipsMessage: "可更换门店,不便之处敬请见谅"
  183. })
  184. return;
  185. }
  186. };
  187. for(let i in list){
  188. if(list[i].id == item.id){
  189. list[i].is_selected = 1;
  190. currentObj = list[i];
  191. }else{
  192. list[i].is_selected = 0;
  193. }
  194. };
  195. that.setData({
  196. storeList: list,
  197. selectStore: currentObj
  198. });
  199. // 更新选中门店
  200. wx.setStorage({
  201. key: "store",
  202. data: JSON.stringify(that.data.selectStore)
  203. });
  204. // 判断是否切换门店,如有需清空购物车
  205. if(that.data.store && that.data.store.id != that.data.selectStore.id){
  206. wx.removeStorage({
  207. key: 'localCart',
  208. });
  209. };
  210. // 进入商品选购
  211. wx.navigateTo({
  212. url: "/pages/goods/index"
  213. });
  214. },
  215. // 取消授权提醒
  216. onCloseLocation(){
  217. let that = this;
  218. that.setData({
  219. showOpenLocation: false
  220. });
  221. // 当是外卖是,进首页
  222. if(that.data.fare_type == 0){
  223. wx.reLaunch({
  224. url: '/pages/index/index'
  225. })
  226. }else{
  227. // 这时只能调取到默认的
  228. getApp().globalData.lat = 0;
  229. getApp().globalData.lng = 0;
  230. that.setData({
  231. page: 1,
  232. storeList: [],
  233. finished: false,
  234. lat: getApp().globalData.lat,
  235. lng: getApp().globalData.lng
  236. });
  237. that.getStoreList();
  238. };
  239. },
  240. // 同意去打开地理位置
  241. onConfirmLocation(){
  242. let that = this;
  243. wx.openSetting({
  244. success: function (res) {
  245. console.log(res)
  246. let authSetting = res.authSetting;
  247. if(authSetting["scope.userLocation"]){
  248. // 开启
  249. getApp().getLocal(function(){
  250. that.setData({
  251. page: 1,
  252. storeList: [],
  253. finished: false
  254. });
  255. that.setData({
  256. lat: getApp().globalData.lat,
  257. lng: getApp().globalData.lng
  258. })
  259. that.getStoreList();
  260. });
  261. }else{
  262. // 未开启
  263. that.setData({
  264. showOpenLocation: true
  265. });
  266. }
  267. }
  268. })
  269. },
  270. // 关闭提醒
  271. closeShowTips(){
  272. let that = this;
  273. that.setData({
  274. showTips: false
  275. })
  276. }
  277. });