addAddress.js 7.6 KB

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