index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. import {
  2. editAddress,
  3. getAddressDetail
  4. } from '../../api/user.js';
  5. import {
  6. getCity
  7. } from '../../api/api.js';
  8. const app = getApp();
  9. Page({
  10. /**
  11. * 页面的初始数据
  12. */
  13. data: {
  14. parameter: {
  15. 'navbar': '1',
  16. 'return': '1',
  17. 'title': '添加地址'
  18. },
  19. region: ['省', '市', '区'],
  20. valueRegion: [0, 0, 0],
  21. cartId: '', //购物车id
  22. pinkId: 0, //拼团id
  23. couponId: 0, //优惠券id
  24. id: 0, //地址id
  25. userAddress: {
  26. is_default: false
  27. }, //地址详情
  28. cityId: 0,
  29. district: [],
  30. multiArray: [],
  31. multiIndex: [0, 0, 0],
  32. },
  33. /**
  34. * 授权回调
  35. */
  36. onLoadFun: function () {
  37. this.getUserAddress();
  38. },
  39. /**
  40. * 生命周期函数--监听页面加载
  41. */
  42. onLoad: function (options) {
  43. this.setData({
  44. cartId: options.cartId || '',
  45. pinkId: options.pinkId || 0,
  46. couponId: options.couponId || 0,
  47. id: options.id || 0,
  48. 'parameter.title': options.id ? '修改地址' : '添加地址'
  49. });
  50. this.getCityList();
  51. },
  52. getCityList: function () {
  53. let that = this;
  54. getCity().then(res => {
  55. that.setData({
  56. district: res.data
  57. });
  58. that.initialize();
  59. })
  60. },
  61. initialize: function () {
  62. let that = this,
  63. province = [],
  64. city = [],
  65. area = [];
  66. if (that.data.district.length) {
  67. let cityChildren = that.data.district[0].c || [];
  68. let areaChildren = cityChildren.length ? (cityChildren[0].c || []) : [];
  69. that.data.district.forEach(function (item) {
  70. province.push(item.n);
  71. });
  72. cityChildren.forEach(function (item) {
  73. city.push(item.n);
  74. });
  75. areaChildren.forEach(function (item) {
  76. area.push(item.n);
  77. });
  78. that.setData({
  79. multiArray: [province, city, area],
  80. });
  81. }
  82. },
  83. bindRegionChange: function (e) {
  84. let multiIndex = this.data.multiIndex,
  85. province = this.data.district[multiIndex[0]] || {
  86. c: []
  87. },
  88. city = province.c[multiIndex[1]] || {
  89. v: 0
  90. },
  91. multiArray = this.data.multiArray,
  92. value = e.detail.value;
  93. console.log(value);
  94. console.log(province);
  95. this.setData({
  96. region: [multiArray[0][value[0]], multiArray[1][value[1]], multiArray[2][value[2]]],
  97. cityId: city.v,
  98. valueRegion: [0, 0, 0]
  99. });
  100. this.initialize();
  101. },
  102. bindMultiPickerColumnChange: function (e) {
  103. let that = this,
  104. column = e.detail.column,
  105. value = e.detail.value,
  106. currentCity = this.data.district[value] || {
  107. c: []
  108. },
  109. multiArray = that.data.multiArray,
  110. multiIndex = that.data.multiIndex;
  111. multiIndex[column] = value;
  112. switch (column) {
  113. case 0:
  114. let areaList = currentCity.c[0] || {
  115. c: []
  116. };
  117. multiArray[1] = currentCity.c.map((item) => {
  118. return item.n;
  119. });
  120. multiArray[2] = areaList.c.map((item) => {
  121. return item.n;
  122. });
  123. break;
  124. case 1:
  125. let cityList = that.data.district[multiIndex[0]].c[multiIndex[1]].c || [];
  126. multiArray[2] = cityList.map((item) => {
  127. return item.n;
  128. });
  129. break;
  130. case 2:
  131. break;
  132. }
  133. this.setData({
  134. multiArray: multiArray,
  135. multiIndex: multiIndex
  136. });
  137. },
  138. getUserAddress: function () {
  139. if (!this.data.id) return false;
  140. let that = this;
  141. getAddressDetail(this.data.id).then(res => {
  142. var region = [res.data.province, res.data.city, res.data.district];
  143. that.setData({
  144. userAddress: res.data,
  145. region: region,
  146. cityId: res.data.city_id,
  147. });
  148. });
  149. },
  150. getWxAddress: function () {
  151. var that = this;
  152. wx.authorize({
  153. scope: 'scope.address',
  154. success: function (res) {
  155. wx.chooseAddress({
  156. success: function (res) {
  157. var addressP = {};
  158. addressP.province = res.provinceName;
  159. addressP.city = res.cityName;
  160. addressP.district = res.countyName;
  161. editAddress({
  162. address: addressP,
  163. is_default: 1,
  164. real_name: res.userName,
  165. post_code: res.postalCode,
  166. phone: res.telNumber,
  167. detail: res.detailInfo,
  168. id: 0,
  169. type: 1,
  170. }).then(res => {
  171. setTimeout(function () {
  172. if (that.data.cartId) {
  173. var cartId = that.data.cartId;
  174. var pinkId = that.data.pinkId;
  175. var couponId = that.data.couponId;
  176. that.setData({
  177. cartId: '',
  178. pinkId: '',
  179. couponId: ''
  180. })
  181. wx.navigateTo({
  182. url: '/pages/order_confirm/index?cartId=' + cartId + '&addressId=' + (that.data.id ? that.data.id : res.data.id) + '&pinkId=' + pinkId + '&couponId=' + couponId
  183. });
  184. } else {
  185. wx.navigateBack({
  186. delta: 1
  187. });
  188. }
  189. }, 1000);
  190. return app.Tips({
  191. title: "添加成功",
  192. icon: 'success'
  193. });
  194. }).catch(err => {
  195. return app.Tips({
  196. title: err
  197. });
  198. });
  199. },
  200. fail: function (res) {
  201. if (res.errMsg == 'chooseAddress:cancel') return app.Tips({
  202. title: '取消选择'
  203. });
  204. },
  205. })
  206. },
  207. fail: function (res) {
  208. wx.showModal({
  209. title: '您已拒绝导入微信地址权限',
  210. content: '是否进入权限管理,调整授权?',
  211. success(res) {
  212. if (res.confirm) {
  213. wx.openSetting({
  214. success: function (res) {
  215. console.log(res.authSetting)
  216. }
  217. });
  218. } else if (res.cancel) {
  219. return app.Tips({
  220. title: '已取消!'
  221. });
  222. }
  223. }
  224. })
  225. },
  226. })
  227. },
  228. /**
  229. * 提交用户添加地址
  230. */
  231. formSubmit: function (e) {
  232. var that = this,
  233. value = e.detail.value;
  234. if (!value.real_name) return app.Tips({
  235. title: '请填写收货人姓名'
  236. });
  237. if (!value.phone) return app.Tips({
  238. title: '请填写联系电话'
  239. });
  240. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(value.phone)) return app.Tips({
  241. title: '请输入正确的手机号码'
  242. });
  243. if (that.data.region[0] == '省') return app.Tips({
  244. title: '请选择所在地区'
  245. });
  246. if (!value.detail) return app.Tips({
  247. title: '请填写详细地址'
  248. });
  249. value.id = that.data.id;
  250. value.address = {
  251. province: that.data.region[0],
  252. city: that.data.region[1],
  253. district: that.data.region[2],
  254. city_id: that.data.cityId,
  255. };
  256. value.is_default = that.data.userAddress.is_default ? 1 : 0;
  257. editAddress(value).then(res => {
  258. if (that.data.id)
  259. app.Tips({
  260. title: '修改成功',
  261. icon: 'success'
  262. });
  263. else
  264. app.Tips({
  265. title: '添加成功',
  266. icon: 'success'
  267. });
  268. setTimeout(function () {
  269. if (that.data.cartId) {
  270. var cartId = that.data.cartId;
  271. var pinkId = that.data.pinkId;
  272. var couponId = that.data.couponId;
  273. that.setData({
  274. cartId: '',
  275. pinkId: '',
  276. couponId: ''
  277. })
  278. wx.navigateTo({
  279. url: '/pages/order_confirm/index?cartId=' + cartId + '&addressId=' + (that.data.id ? that.data.id : res.data.id) + '&pinkId=' + pinkId + '&couponId=' + couponId
  280. });
  281. } else {
  282. wx.navigateBack({
  283. delta: 1
  284. });
  285. }
  286. }, 1000);
  287. }).catch(err => {
  288. return app.Tips({
  289. title: err
  290. });
  291. })
  292. },
  293. ChangeIsDefault: function (e) {
  294. this.setData({
  295. 'userAddress.is_default': !this.data.userAddress.is_default
  296. });
  297. },
  298. })