addSite.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // pages/my/address/addSite/addSite.js
  2. const app = getApp();
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. type: false,
  9. resultInfo: {
  10. name: '',
  11. phone: '',
  12. address: '',
  13. checked: false
  14. },
  15. name: '',
  16. phone: '',
  17. address: '',
  18. checked4: false,
  19. id:""
  20. },
  21. /**
  22. * 生命周期函数--监听页面加载
  23. */
  24. onLoad: function (options) {
  25. var type = options.parmise == 'add' ? true : false;
  26. console.log(options.parmise);
  27. if(!type){
  28. var parmise = JSON.parse(options.parmise)
  29. this.setData({
  30. type: type,
  31. resultInfo: {
  32. name: parmise.username,
  33. phone: parmise.phone,
  34. address: parmise.address,
  35. checked4: parmise.is_default == 1 ? true : false
  36. },
  37. name: parmise.username,
  38. phone: parmise.phone,
  39. address: parmise.address,
  40. checked4: parmise.is_default == 1 ? true : false,
  41. id:parmise.id,
  42. })
  43. }else{
  44. this.setData({type: type})
  45. }
  46. if (this.data.type) {
  47. wx.setNavigationBarTitle({
  48. title: '添加新地址',
  49. })
  50. } else {
  51. wx.setNavigationBarTitle({
  52. title: '编辑地址',
  53. })
  54. }
  55. },
  56. onChange(e) {
  57. console.log(e);
  58. this.setData({
  59. checked4: e.detail
  60. })
  61. this.data.resultInfo.checked = this.data.checked4
  62. },
  63. input1(e) {
  64. console.log(this.data.resultInfo.name);
  65. this.setData({
  66. name: e.detail
  67. })
  68. this.data.resultInfo.name = this.data.name
  69. if (this.data.resultInfo.name != '') {
  70. this.setData({
  71. checked1: true
  72. })
  73. } else {
  74. this.setData({
  75. checked1: false
  76. })
  77. }
  78. },
  79. input2(e) {
  80. this.setData({
  81. phone: e.detail
  82. })
  83. this.data.resultInfo.phone = this.data.phone
  84. if (this.data.resultInfo.phone != '') {
  85. this.setData({
  86. checked2: true
  87. })
  88. } else {
  89. this.setData({
  90. checked2: false
  91. })
  92. }
  93. },
  94. input3(e) {
  95. this.setData({
  96. address: e.detail
  97. })
  98. this.data.resultInfo.address = this.data.address
  99. if (this.data.resultInfo.address != '') {
  100. this.setData({
  101. checked3: true
  102. })
  103. } else {
  104. this.setData({
  105. checked3: false
  106. })
  107. }
  108. console.log(this.data.checked1, this.data.checked2, this.data.checked3);
  109. },
  110. addAddress() {
  111. var that = this;
  112. app.api.useApi(app.globalData.baseAppUrl + "api/address/create", {
  113. username: that.data.resultInfo.name,
  114. phone: that.data.resultInfo.phone,
  115. address: that.data.resultInfo.address,
  116. is_default: that.data.resultInfo.checked
  117. }, "post").then(function (res) {
  118. if (res.code != 200) {
  119. wx.showLoading({
  120. title: res.message.msg,
  121. })
  122. } else {
  123. wx.showLoading({
  124. title: res.message.msg,
  125. })
  126. setTimeout(() => {
  127. wx.navigateBack()
  128. }, 1000);
  129. }
  130. setTimeout(function () {
  131. wx.hideLoading({
  132. success: (res) => { },
  133. })
  134. }, 2000);
  135. }).catch(function (err) {
  136. console.log(222);
  137. })
  138. },
  139. del(e) {
  140. console.log(e);
  141. app.api.useApi(app.globalData.baseAppUrl + "api/address/del", {
  142. id:e.target.dataset.id,
  143. }, "post").then(function (res) {
  144. console.log(res)
  145. if (res.code != 200) {
  146. wx.showLoading({
  147. title: res.message.msg,
  148. })
  149. } else {
  150. wx.showLoading({
  151. title: res.message.msg,
  152. })
  153. setTimeout(() => {
  154. wx.navigateBack()
  155. }, 1000);
  156. }
  157. setTimeout(function () {
  158. wx.hideLoading({
  159. success: (res) => { },
  160. })
  161. }, 2000);
  162. }).catch(function (err) {
  163. console.log(222);
  164. })
  165. },
  166. updateaddr(e){
  167. var that = this;
  168. app.api.useApi(app.globalData.baseAppUrl + "api/address/update", {
  169. username: that.data.resultInfo.name,
  170. phone: that.data.resultInfo.phone,
  171. address: that.data.resultInfo.address,
  172. is_default: that.data.resultInfo.checked,
  173. id:e.target.dataset.id,
  174. }, "post").then(function (res) {
  175. if (res.code != 200) {
  176. wx.showLoading({
  177. title: res.message.msg,
  178. })
  179. } else {
  180. wx.showLoading({
  181. title: res.message.msg,
  182. })
  183. setTimeout(() => {
  184. wx.navigateBack()
  185. }, 1000);
  186. }
  187. setTimeout(function () {
  188. wx.hideLoading({
  189. success: (res) => { },
  190. })
  191. }, 2000);
  192. }).catch(function (err) {
  193. console.log(222);
  194. })
  195. }
  196. })