addAddress.js 7.7 KB

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