addAddress.js 7.3 KB

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