addAddress.js 7.5 KB

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