addaddress.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // pages/addaddress/addaddress.js
  2. const api = require("../../api/api");
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. navbarData: {
  9. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  10. title: '新增地址', //导航栏 中间的标题
  11. capsuleMode: 'navBack', //显示模式(navBack:返回上一页;navHome:返回首页)
  12. },
  13. genders: ['男士', '女士'],
  14. type: '', //类型
  15. /* 表单 */
  16. id: 0, //id
  17. address: '', //地址名称/门牌号
  18. receiving_name: '', //收货人姓名
  19. genderIndex: 0,
  20. receiving_phone: '', //收货人手机号
  21. },
  22. /**
  23. * 生命周期函数--监听页面加载
  24. */
  25. onLoad(options) {
  26. console.log(options.type);
  27. if (options.type === 'edit') {
  28. this.setData({
  29. type: 'edit',
  30. id: wx.getStorageSync('address').id,
  31. address: wx.getStorageSync('address').receiving_address,
  32. receiving_name: wx.getStorageSync('address').receiving_name,
  33. genderIndex: wx.getStorageSync('address').gender,
  34. receiving_phone: wx.getStorageSync('address').receiving_phone,
  35. navbarData: {
  36. showCapsule: 1,
  37. title: '修改地址',
  38. capsuleMode: 'navBack', //显示模式(navBack:返回上一页;navHome:返回首页)
  39. }
  40. })
  41. } else {
  42. this.setData({
  43. type: 'add',
  44. navbarData: {
  45. showCapsule: 1,
  46. title: '新增地址',
  47. capsuleMode: 'navBack', //显示模式(navBack:返回上一页;navHome:返回首页)
  48. }
  49. })
  50. }
  51. },
  52. /**
  53. * 生命周期函数--监听页面初次渲染完成
  54. */
  55. onReady() {
  56. },
  57. /**
  58. * 生命周期函数--监听页面显示
  59. */
  60. onShow() {
  61. },
  62. /**
  63. * 生命周期函数--监听页面隐藏
  64. */
  65. onHide() {
  66. },
  67. /**
  68. * 生命周期函数--监听页面卸载
  69. */
  70. onUnload() {
  71. },
  72. /**
  73. * 页面相关事件处理函数--监听用户下拉动作
  74. */
  75. onPullDownRefresh() {
  76. },
  77. /**
  78. * 页面上拉触底事件的处理函数
  79. */
  80. onReachBottom() {
  81. },
  82. /**
  83. * 用户点击右上角分享
  84. */
  85. onShareAppMessage() {
  86. },
  87. // 输入地址/门牌号
  88. inputAddress(e) {
  89. this.setData({
  90. address: e.detail.value
  91. })
  92. },
  93. // 输入姓名
  94. inputName(e) {
  95. this.setData({
  96. receiving_name: e.detail.value
  97. })
  98. },
  99. // 选择性别
  100. selectGender(e) {
  101. this.setData({
  102. genderIndex: e.currentTarget.dataset.index
  103. })
  104. },
  105. // 输入手机号
  106. inputPhone(e) {
  107. this.setData({
  108. receiving_phone: e.detail.value
  109. })
  110. },
  111. // 保持地址
  112. submitAdd() {
  113. let that = this;
  114. if (that.data.address == '') {
  115. wx.showToast({
  116. title: '提货点地址未录入',
  117. icon: 'none',
  118. mask: true
  119. })
  120. return
  121. }
  122. if (that.data.receiving_name == '') {
  123. wx.showToast({
  124. title: '姓名未录入',
  125. icon: 'none',
  126. mask: true
  127. })
  128. return
  129. }
  130. if (that.data.receiving_phone == '') {
  131. wx.showToast({
  132. title: '手机号未录入',
  133. icon: 'none',
  134. mask: true
  135. })
  136. return
  137. }
  138. wx.showLoading({
  139. title: '提交中',
  140. mask: true
  141. })
  142. wx.request({
  143. url: api.Address_add,
  144. header: {
  145. 'Authorization': wx.getStorageSync('token')
  146. },
  147. method: 'POST',
  148. data: {
  149. receiving_name: that.data.receiving_name,
  150. receiving_phone: that.data.receiving_phone,
  151. receiving_address: that.data.address,
  152. gender: that.data.genderIndex
  153. },
  154. success(res) {
  155. console.log(res);
  156. wx.hideLoading()
  157. if (res.data.code === 1) {
  158. wx.showToast({
  159. title: '提交成功',
  160. icon: 'success',
  161. mask: true,
  162. success() {
  163. setTimeout(() => {
  164. wx.navigateBack({
  165. delta: 1,
  166. })
  167. }, 1500)
  168. }
  169. })
  170. } else {
  171. wx.showToast({
  172. title: res.data.msg,
  173. mask: true,
  174. icon: 'none'
  175. })
  176. }
  177. },
  178. fail(err) {
  179. wx.hideLoading()
  180. wx.showToast({
  181. title: '发起网络请求失败',
  182. icon: 'none',
  183. mask: true
  184. })
  185. },
  186. complete() {
  187. // wx.hideLoading()
  188. }
  189. })
  190. },
  191. // 修改地址
  192. submitEdit() {
  193. let that = this;
  194. if (that.data.address == '') {
  195. wx.showToast({
  196. title: '地址未录入',
  197. icon: 'none',
  198. mask: true
  199. })
  200. return
  201. }
  202. if (that.data.receiving_name == '') {
  203. wx.showToast({
  204. title: '姓名未录入',
  205. icon: 'none',
  206. mask: true
  207. })
  208. return
  209. }
  210. if (that.data.receiving_phone == '') {
  211. wx.showToast({
  212. title: '手机号未录入',
  213. icon: 'none',
  214. mask: true
  215. })
  216. return
  217. }
  218. wx.showLoading({
  219. title: '提交中',
  220. mask: true
  221. })
  222. wx.request({
  223. url: api.Address_edit,
  224. header: {
  225. 'Authorization': wx.getStorageSync('token')
  226. },
  227. method: 'POST',
  228. data: {
  229. id: that.data.id,
  230. receiving_name: that.data.receiving_name,
  231. receiving_phone: that.data.receiving_phone,
  232. receiving_address: that.data.address,
  233. gender: that.data.genderIndex
  234. },
  235. success(res) {
  236. console.log(res);
  237. wx.hideLoading()
  238. if (res.data.code === 1) {
  239. wx.showToast({
  240. title: '提交成功',
  241. icon: 'success',
  242. mask: true,
  243. success() {
  244. setTimeout(() => {
  245. wx.navigateBack({
  246. delta: 1,
  247. })
  248. }, 1500)
  249. }
  250. })
  251. } else {
  252. wx.showToast({
  253. title: res.data.msg,
  254. mask: true,
  255. icon: 'none'
  256. })
  257. }
  258. },
  259. fail(err) {
  260. wx.hideLoading()
  261. wx.showToast({
  262. title: '发起网络请求失败',
  263. icon: 'none',
  264. mask: true
  265. })
  266. },
  267. complete() {
  268. // wx.hideLoading()
  269. }
  270. })
  271. }
  272. })