index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // index.js
  2. let myPro = require("../../../../utils/wxRequest.js");
  3. let util = require("../../../../utils/util.js");
  4. Page({
  5. data: {
  6. showConfirmAdd: false, // 确认地址弹层
  7. page: 1,
  8. size: 10,
  9. dataList: [],
  10. finished: false, // 加载完毕
  11. loading: false, // 全局loading
  12. checkedAddress: null, // 选中地址
  13. showDelDialog: false, // 删除确认框
  14. is_back: null // 当用户主动切换地址时,支持回退
  15. },
  16. onLoad: function (options) {
  17. // Do some initialize when page load.
  18. let that = this;
  19. // 路由回退标识
  20. if(options.is_back){
  21. that.setData({
  22. is_back: options.is_back
  23. });
  24. };
  25. },
  26. onShow: function () {
  27. // Do something when page show.
  28. let that = this;
  29. that.setData({
  30. page: 1,
  31. size: 10,
  32. dataList: [],
  33. finished: false,
  34. });
  35. that.getData();
  36. },
  37. onReady: function () {
  38. // Do something when page ready.
  39. },
  40. onHide: function () {
  41. // Do something when page hide.
  42. },
  43. onUnload: function () {
  44. // Do something when page close.
  45. },
  46. onPullDownRefresh: function () {
  47. // Do something when pull down.
  48. },
  49. onReachBottom: function () {
  50. // Do something when page reach bottom.
  51. let that = this;
  52. if(!that.data.finished){
  53. that.getData();
  54. };
  55. },
  56. onPageScroll: function () {
  57. // Do something when page scroll
  58. },
  59. onResize: function () {
  60. // Do something when page resize
  61. },
  62. // 获取地址列表
  63. getData(){
  64. let that = this;
  65. let params = {
  66. page: that.data.page,
  67. size: that.data.size
  68. };
  69. myPro.wxRequest("user/address/list","GET",params).then(res=>{
  70. let list = res.result;
  71. // 无数据
  72. if(list.length == 0){
  73. wx.showToast({
  74. title: "暂无更多数据",
  75. icon: "none"
  76. });
  77. that.setData({
  78. finished: true
  79. });
  80. }else{
  81. // 有数据
  82. that.setData({
  83. dataList: that.data.dataList.concat(list),
  84. page: that.data.page + 1
  85. });
  86. }
  87. }).catch(err=>{
  88. console.log('报错信息',err);
  89. wx.showToast({
  90. title: err,
  91. icon: "none"
  92. });
  93. })
  94. },
  95. // 去详情
  96. goAddressDetail(event){
  97. let that = this;
  98. let url = '';
  99. if(event.currentTarget.dataset.item){
  100. let item = event.currentTarget.dataset.item;
  101. url = '/pages/user/address/detail/index?id='+item.id
  102. }else{
  103. url = '/pages/user/address/detail/index'
  104. }
  105. wx.navigateTo({
  106. url: url
  107. });
  108. },
  109. // 获取微信地址
  110. getAddressFromWechat(){
  111. let that = this;
  112. wx.chooseAddress({
  113. success: function(res){
  114. console.log('获取成功',res)
  115. let obj = {
  116. name: res.userName,
  117. phone: res.telNumber,
  118. area: res.provinceName+res.cityName+res.countyName,
  119. address: res.detailInfo
  120. };
  121. // 让用户主动选一遍位置
  122. wx.navigateTo({
  123. url: '/pages/user/address/detail/index?type=wechat'
  124. });
  125. // 存一下
  126. wx.setStorage({
  127. key: "wechatAddress",
  128. data: JSON.stringify(obj)
  129. });
  130. },
  131. fail: function(res){
  132. console.log('获取失败',res)
  133. }
  134. })
  135. },
  136. // 选择地址
  137. selecteAddress(event){
  138. let that = this;
  139. // 先把上一次的值置空(避免出问题)
  140. that.setData({
  141. checkedAddress: null
  142. });
  143. // 重新赋值
  144. let checkedAddress = event.currentTarget.dataset.item;
  145. that.setData({
  146. showConfirmAdd: true,
  147. checkedAddress: checkedAddress
  148. });
  149. },
  150. // 更换地址
  151. cancelCheck(){
  152. let that = this;
  153. that.setData({
  154. showConfirmAdd: false,
  155. });
  156. },
  157. // 确认地址
  158. confirmCheck(){
  159. let that = this;
  160. that.setData({
  161. showConfirmAdd: false
  162. });
  163. // 拿地址,匹配下门店,如果未返回,就是超距离了
  164. let params = {
  165. lat: that.data.checkedAddress.lat,
  166. lng: that.data.checkedAddress.lnt,
  167. page: 1,
  168. size: 10
  169. };
  170. myPro.wxRequest("user/store/list","GET",params).then(res=>{
  171. let storeList = res.result;
  172. if(storeList.length != 0){
  173. wx.setStorage({
  174. key: "checkedAddress",
  175. data: JSON.stringify(that.data.checkedAddress),
  176. success: function(){
  177. if(that.data.is_back){
  178. wx.navigateBack();
  179. }else{
  180. // 去购商品
  181. wx.redirectTo({
  182. url: "/pages/goods/index"
  183. });
  184. }
  185. }
  186. });
  187. }else{
  188. // 超配送距离
  189. wx.showToast({
  190. title: "门店尚未营业", // 单店改为门店尚未营业 多店已超配送距离,请更换地址
  191. icon: "none"
  192. });
  193. };
  194. }).catch(err=>{
  195. console.log('报错信息',err)
  196. wx.showToast({
  197. title: err,
  198. icon: "none",
  199. });
  200. });
  201. },
  202. // 打开删除确认框
  203. doDeleteAddress(event){
  204. let that = this;
  205. that.setData({
  206. id: event.currentTarget.dataset.id,
  207. showDelDialog: true
  208. });
  209. },
  210. // 取消删除地址
  211. onCloseDel(){
  212. let that = this;
  213. that.setData({
  214. showDelDialog: false
  215. });
  216. },
  217. // 确认删除
  218. onConfirmDel: function(){
  219. let that = this;
  220. let params = {
  221. id: that.data.id
  222. };
  223. myPro.wxRequest("user/address/remove","POST",params).then(res=>{
  224. wx.showToast({
  225. title: res.msg,
  226. icon: "none"
  227. });
  228. that.setData({
  229. showDelDialog: false,
  230. page: 1,
  231. dataList: [],
  232. finished: false
  233. });
  234. that.getData();
  235. }).catch(err=>{
  236. that.setData({
  237. showDelDialog: false
  238. });
  239. console.log("报错信息",err);
  240. wx.showToast({
  241. title: err,
  242. icon: "none"
  243. });
  244. })
  245. },
  246. });