addAddress.js 5.9 KB

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