addAddress.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // pages/addAddress/addAddress.js
  2. const getReq = require('./../../config.js').getReq
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. checked: true,
  9. areas: [],
  10. tree: [],
  11. value: [0, 0, 0],
  12. oldval: [0, 0, 0],
  13. citys: [],
  14. districts: [],
  15. animation_flag: false,
  16. province: '',
  17. city: '',
  18. district: '',
  19. name: '',
  20. mob_phone: '',
  21. address: ''
  22. },
  23. /**
  24. * 生命周期函数--监听页面加载
  25. */
  26. onLoad: function (options) {
  27. this.getDatas()
  28. },
  29. toggleChecked() {
  30. this.setData({
  31. checked: !this.data.checked
  32. })
  33. },
  34. bindChange: function (e) {
  35. const val = e.detail.value
  36. if (this.data.oldval[0] != val[0]) {
  37. val[1] = 0
  38. val[2] = 0
  39. } else { //若省份column未做滑动,地级市做了滑动则定位区县第一位
  40. if (this.data.oldval[1] != val[1]) {
  41. val[2] = 0
  42. }
  43. }
  44. this.setData({
  45. value: val,
  46. citys: this.data.tree[val[0]].children,
  47. districts: this.data.tree[val[0]].children[val[1]].children
  48. })
  49. },
  50. getDatas() {
  51. wx.showLoading({
  52. title: '加载中',
  53. })
  54. var self = this
  55. getReq({
  56. act: 'app_update',
  57. op: 'area',
  58. curpage: 1
  59. }, function (res) {
  60. wx.hideLoading()
  61. if (res.code == 200) {
  62. let tree = self.arrayToTree(res.datas.areas)
  63. self.setData({
  64. areas: res.datas.areas,
  65. tree,
  66. citys: tree[0].children,
  67. districts: tree[0].children[0].children
  68. })
  69. }
  70. else {
  71. wx.showToast({
  72. icon: 'none',
  73. title: res.message,
  74. duration: 2000
  75. })
  76. }
  77. })
  78. },
  79. arrayToTree(array) {
  80. let result = [], hash = {}, children = 'children'
  81. array.forEach((item, index) => {
  82. hash[item.aid] = item
  83. })
  84. array.forEach((item, index) => {
  85. let pid = item.pid
  86. if (pid == 0) {
  87. result.push(item)
  88. }
  89. else {
  90. let hashvp = hash[item.pid]
  91. if (!hashvp[children]) {
  92. hashvp[children] = []
  93. }
  94. hashvp[children].push(item)
  95. }
  96. })
  97. return result
  98. },
  99. animation_flag(e) {
  100. let flag = e.currentTarget.dataset.flag
  101. let animation_flag = flag == 'true' ? true : false
  102. let val = this.data.oldval
  103. this.setData({
  104. animation_flag,
  105. value: val,
  106. citys: this.data.tree[val[0]].children,
  107. districts: this.data.tree[val[0]].children[val[1]].children
  108. })
  109. },
  110. setAddress() {
  111. let val = this.data.value
  112. let province = this.data.tree[val[0]]
  113. let city = province.children[val[1]]
  114. let district = province.children[val[1]].children[val[2]]
  115. this.setData({
  116. animation_flag: false,
  117. oldval: val,
  118. province,
  119. city,
  120. district
  121. })
  122. },
  123. commmitAddress() {
  124. let self = this
  125. let name = this.trim(this.data.name)
  126. let phone = this.trim(this.data.mob_phone)
  127. let address = this.trim(this.data.address)
  128. let province = this.data.province
  129. if (!name) {
  130. wx.showToast({
  131. icon: 'none',
  132. title: "请填写您的收货人名称",
  133. duration: 1500
  134. })
  135. return
  136. }
  137. if (!(/^1(3|4|5|7|8)\d{9}$/.test(phone))) {
  138. wx.showToast({
  139. icon: 'none',
  140. title: "请填写您的手机号",
  141. duration: 1500
  142. })
  143. return
  144. }
  145. if (!province) {
  146. wx.showToast({
  147. icon: 'none',
  148. title: "请选择您的收货地址",
  149. duration: 1500
  150. })
  151. return
  152. }
  153. if (!address) {
  154. wx.showToast({
  155. icon: 'none',
  156. title: "请填写的您的详细收货地址",
  157. duration: 1500
  158. })
  159. return
  160. }
  161. getReq({
  162. act: 'member_address',
  163. op: 'address_add',
  164. curpage: 1,
  165. true_name: name,
  166. address,
  167. mob_phone: phone,
  168. area_id: this.data.district.aid
  169. }, function (res) {
  170. wx.hideLoading()
  171. if (res.code == 200) {
  172. let address_id = res.datas.address_id
  173. let is_default = self.data.checked ? 1 : 0
  174. getReq({
  175. act: 'member_address',
  176. op: 'set_default',
  177. curpage: 1,
  178. address_id,
  179. is_default
  180. }, function (response) {
  181. wx.hideLoading()
  182. if (response.code == 200) {
  183. wx.navigateBack()
  184. }
  185. else {
  186. wx.showToast({
  187. icon: 'none',
  188. title: response.message,
  189. duration: 2000
  190. })
  191. }
  192. })
  193. }
  194. else {
  195. wx.showToast({
  196. icon: 'none',
  197. title: res.message,
  198. duration: 2000
  199. })
  200. }
  201. })
  202. },
  203. trim(str) {
  204. str = str.replace(/\s+/g, "")
  205. return str
  206. },
  207. bindKeyInput(e) {
  208. let valueType = e.currentTarget.dataset.value
  209. let value = e.detail.value
  210. switch (valueType) {
  211. case 'name':
  212. this.setData({
  213. name: value
  214. })
  215. break;
  216. case 'phone':
  217. this.setData({
  218. mob_phone: value
  219. })
  220. break;
  221. case 'address':
  222. this.setData({
  223. address: value
  224. })
  225. break;
  226. default:
  227. break;
  228. }
  229. },
  230. /**
  231. * 生命周期函数--监听页面初次渲染完成
  232. */
  233. onReady: function () {
  234. },
  235. /**
  236. * 生命周期函数--监听页面显示
  237. */
  238. onShow: function () {
  239. },
  240. /**
  241. * 生命周期函数--监听页面隐藏
  242. */
  243. onHide: function () {
  244. },
  245. /**
  246. * 生命周期函数--监听页面卸载
  247. */
  248. onUnload: function () {
  249. },
  250. /**
  251. * 页面相关事件处理函数--监听用户下拉动作
  252. */
  253. onPullDownRefresh: function () {
  254. },
  255. /**
  256. * 页面上拉触底事件的处理函数
  257. */
  258. onReachBottom: function () {
  259. },
  260. /**
  261. * 用户点击右上角分享
  262. */
  263. onShareAppMessage: function () {
  264. }
  265. })