add.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // pages/user/address/add/add.js
  2. var app = getApp();
  3. import {
  4. add
  5. } from 'lodash';
  6. import {
  7. address
  8. } from './../../../../utils/address.js';
  9. Page({
  10. /**
  11. * 页面的初始数据
  12. */
  13. data: {
  14. addressList: address,
  15. addressRange: [],
  16. addressValue: [],
  17. name: "",
  18. addrText: "",
  19. renderText: "",
  20. index0: 0,
  21. loading: true,
  22. addrId: null,
  23. addr: {
  24. }
  25. },
  26. /**
  27. * 生命周期函数--监听页面加载
  28. */
  29. async onLoad(options) {
  30. this.setData({
  31. addrId: options.addrId || null,
  32. loading: false
  33. })
  34. },
  35. /**
  36. * 生命周期函数--监听页面初次渲染完成
  37. */
  38. onReady() {
  39. },
  40. /**
  41. * 生命周期函数--监听页面显示
  42. */
  43. async onShow() {
  44. let index0 = 0;
  45. let index1 = 0;
  46. if (this.data.addrId) {
  47. let addr = await app.ajax.get({
  48. url: `/admin/bxdClientAddress/${this.data.addrId}`,
  49. })
  50. this.setData({
  51. addr: addr.data.data,
  52. loading: false
  53. })
  54. }
  55. let addr = this.data.addr;
  56. if (addr && addr.addrId) {
  57. try {
  58. let province = this.data.addressList.findIndex(v => v.name === addr.province);
  59. let city = this.data.addressList[province].city.findIndex(v => v.name === addr.city);
  60. let area = this.data.addressList[province].city[city].area.findIndex(v => v === addr.area);
  61. index0 = province;
  62. index1 = city;
  63. this.setData({
  64. addrText: [addr.province, addr.city, addr.area],
  65. addressValue: [province, city, area]
  66. })
  67. } catch (e) {
  68. //console.log(e);
  69. }
  70. }
  71. let addressRange = [
  72. [...this.data.addressList.map(v => v.name)],
  73. [...this.data.addressList[index0].city.map(v => v.name)],
  74. [...this.data.addressList[index0].city[index1].area],
  75. ]
  76. this.setData({
  77. addressRange
  78. })
  79. },
  80. async submit(e) {
  81. let addr = e.detail.value;
  82. if (addr.receiver == "") {
  83. wx.showToast({
  84. title: '收货人不可为空',
  85. icon: "error"
  86. });
  87. return;
  88. }
  89. if (addr.mobile == "") {
  90. wx.showToast({
  91. title: '手机号不可为空',
  92. icon: "error"
  93. });
  94. return;
  95. }
  96. if (!app.isMobile(addr.mobile)) {
  97. wx.showToast({
  98. title: '手机号格式有误',
  99. icon: "error"
  100. })
  101. return;
  102. }
  103. if (addr.tel != "" && !app.isPhone(addr.tel)) {
  104. wx.showToast({
  105. title: '座机格式有误',
  106. icon: "error"
  107. });
  108. return;
  109. }
  110. if (this.data.addressValue.length != 3 || this.data.addrText.length == 0) {
  111. wx.showToast({
  112. title: '请选择所在区域',
  113. icon: "error"
  114. });
  115. return;
  116. }
  117. if (addr.addr == "") {
  118. wx.showToast({
  119. title: '请输入详细地址',
  120. icon: "error"
  121. });
  122. return;
  123. }
  124. let data = {
  125. ...addr,
  126. province: this.data.addrText[0],
  127. city: this.data.addrText[1],
  128. area: this.data.addrText[2],
  129. addrId: this.data.addrId
  130. }
  131. await app.ajax.post({
  132. url: "/admin/bxdClientAddress",
  133. method: this.data.addrId ? "PUT" : "POST",
  134. data: data
  135. })
  136. setTimeout(() => {
  137. wx.showToast({
  138. title: this.data.addrId ? '更新成功' : '新增成功',
  139. mask: true
  140. });
  141. }, 200);
  142. setTimeout(() => {
  143. wx.navigateBack({
  144. delta: 1
  145. })
  146. }, 1200);
  147. },
  148. bindchange(e) {
  149. let selectAddr = this.data.addressList[e.detail.value[0]];
  150. this.setData({
  151. addressValue: e.detail.value,
  152. addrText: [selectAddr.name, selectAddr.city[e.detail.value[1]].name, selectAddr.city[e.detail.value[1]].area[e.detail.value[2]]]
  153. })
  154. },
  155. bindcolumnchange(e) {
  156. let column = e.detail.column;
  157. let value = e.detail.value;
  158. let index0 = this.data.index0;
  159. //console.log(e)
  160. let addressRange = [];
  161. if (column == 0) {
  162. addressRange = [
  163. [...this.data.addressList.map(v => v.name)],
  164. [...this.data.addressList[value].city.map(v => v.name)],
  165. [...this.data.addressList[value].city[0].area],
  166. ];
  167. this.setData({
  168. index0: value
  169. })
  170. } else if (column == 1) {
  171. addressRange = [
  172. [...this.data.addressList.map(v => v.name)],
  173. [...this.data.addressList[index0].city.map(v => v.name)],
  174. [...this.data.addressList[index0].city[value].area],
  175. ]
  176. } else {
  177. return;
  178. }
  179. this.setData({
  180. addressRange
  181. })
  182. },
  183. async renderTextInput(e) {
  184. if (!e.detail.value.renderText) {
  185. let text = await wx.getClipboardData();
  186. this.setData({
  187. renderText: text.data
  188. });
  189. } else {
  190. this.setData({
  191. renderText: e.detail.value.renderText
  192. });
  193. }
  194. await this.renderAddr()
  195. },
  196. async renderAddr() {
  197. let {
  198. data
  199. } = await app.ajax.gets(`/admin/bxdClientAddress/addressParse?address=${this.data.renderText}`);
  200. this.setData({
  201. 'addr.mobile': data.data.mobile || '',
  202. 'addr.receiver': data.data.name || '',
  203. 'addr.addr': data.data.detail || '',
  204. addrText: [data.data.province, data.data.city, data.data.area]
  205. });
  206. let province = this.data.addressList.findIndex(v => v.name === data.data.province);
  207. let city = this.data.addressList[province].city.findIndex(v => v.name === data.data.city);
  208. let area = this.data.addressList[province].city[city].area.findIndex(v => v === data.data.area);
  209. this.setData({
  210. addressValue: [province, city, area],
  211. })
  212. let addressRange = [
  213. [...this.data.addressList.map(v => v.name)],
  214. [...this.data.addressList[province].city.map(v => v.name)],
  215. [...this.data.addressList[province].city[city].area],
  216. ]
  217. this.setData({
  218. addressRange
  219. })
  220. },
  221. /**
  222. * 生命周期函数--监听页面隐藏
  223. */
  224. onHide() {
  225. },
  226. /**
  227. * 生命周期函数--监听页面卸载
  228. */
  229. onUnload() {
  230. },
  231. /**
  232. * 页面相关事件处理函数--监听用户下拉动作
  233. */
  234. onPullDownRefresh() {
  235. },
  236. /**
  237. * 页面上拉触底事件的处理函数
  238. */
  239. onReachBottom() {
  240. },
  241. /**
  242. * 用户点击右上角分享
  243. */
  244. onShareAppMessage() {
  245. }
  246. })