addAddress.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. self.setDefault(res, name, phone, area_info, address, address_id)
  208. }
  209. else {
  210. wx.showToast({
  211. icon: 'none',
  212. title: res.message,
  213. duration: 2000
  214. })
  215. }
  216. })
  217. },
  218. editAddress(true_name, address_id, city_id, area_id, area_info, address, mob_phone) {
  219. let self = this;
  220. let params = {
  221. act: 'member_address',
  222. op: 'address_edit',
  223. true_name,
  224. address_id,
  225. city_id,
  226. area_id,
  227. area_info,
  228. address,
  229. mob_phone,
  230. }
  231. getReq(params, function (res) {
  232. if (res.code == 200) {
  233. self.setDefault(res, true_name, mob_phone, area_info, address, address_id)
  234. }
  235. });
  236. },
  237. setDefault(res, true_name, mob_phone, area_info, address, address_id) {
  238. let self = this;
  239. let is_default = self.data.checked ? 1 : 0
  240. if (is_default) {
  241. let area_info = self.data.area_info
  242. appInstance.checkDefaultAddress({
  243. true_name,
  244. mob_phone,
  245. area_info,
  246. address,
  247. address_id
  248. })
  249. }
  250. let params = {
  251. act: 'member_address',
  252. op: 'set_default',
  253. is_default,
  254. address_id
  255. }
  256. getReq(params, function (response) {
  257. wx.hideLoading()
  258. console.log(response);
  259. if (response.code == 200) {
  260. wx.navigateBack()
  261. }
  262. else {
  263. wx.showToast({
  264. icon: 'none',
  265. title: response.message,
  266. duration: 2000
  267. })
  268. }
  269. })
  270. },
  271. trim(str) {
  272. str = str.replace(/\s+/g, "")
  273. return str
  274. },
  275. bindKeyInput(e) {
  276. let valueType = e.currentTarget.dataset.value
  277. let value = e.detail.value
  278. switch (valueType) {
  279. case 'name':
  280. this.setData({
  281. name: value
  282. })
  283. break;
  284. case 'phone':
  285. this.setData({
  286. mob_phone: value
  287. })
  288. break;
  289. case 'address':
  290. this.setData({
  291. address: value
  292. })
  293. break;
  294. default:
  295. break;
  296. }
  297. },
  298. /**
  299. * 生命周期函数--监听页面初次渲染完成
  300. */
  301. onReady: function () {
  302. },
  303. /**
  304. * 生命周期函数--监听页面显示
  305. */
  306. onShow: function () {
  307. },
  308. /**
  309. * 生命周期函数--监听页面隐藏
  310. */
  311. onHide: function () {
  312. },
  313. /**
  314. * 生命周期函数--监听页面卸载
  315. */
  316. onUnload: function () {
  317. },
  318. /**
  319. * 页面相关事件处理函数--监听用户下拉动作
  320. */
  321. onPullDownRefresh: function () {
  322. },
  323. /**
  324. * 页面上拉触底事件的处理函数
  325. */
  326. onReachBottom: function () {
  327. }
  328. })