东风启辰小程序端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 satır
5.2KB

  1. // pages/address/address.js
  2. const app = getApp()
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. imgUrl: app.globalData.urlStatic,//图片路径
  9. submitData:{
  10. realName:"",
  11. mobile: "",//电话
  12. province: "",//省份
  13. city: "",//城市
  14. district: "",//地区
  15. addressDetail:""//详细
  16. },
  17. isAddress: false,//是否有地址
  18. phoneInputShow: false,//是否显示电话输入框
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. onLoad: function (options) {
  24. app.globalData.nowPage = 4;
  25. if (app.globalData.openid) {
  26. this.loadFun();
  27. } else {
  28. app.globalData.openidSuccessFuc = this.loadFun;
  29. }
  30. // if (app.globalData.userMobile) {
  31. // this.data.submitData.mobile = app.globalData.userMobile;
  32. // this.setData({
  33. // phoneInputShow: true,
  34. // submitData: this.data.submitData
  35. // })
  36. // }
  37. },
  38. loadFun: function () {
  39. this.getAddress();
  40. },
  41. /**
  42. * 生命周期函数--监听页面初次渲染完成
  43. */
  44. onReady: function () {
  45. },
  46. /**
  47. * 生命周期函数--监听页面显示
  48. */
  49. onShow: function () {
  50. },
  51. /**
  52. * 生命周期函数--监听页面隐藏
  53. */
  54. onHide: function () {
  55. },
  56. /**
  57. * 生命周期函数--监听页面卸载
  58. */
  59. onUnload: function () {
  60. },
  61. /**
  62. * 页面相关事件处理函数--监听用户下拉动作
  63. */
  64. onPullDownRefresh: function () {
  65. },
  66. /**
  67. * 页面上拉触底事件的处理函数
  68. */
  69. onReachBottom: function () {
  70. },
  71. /**
  72. * 用户点击右上角分享
  73. */
  74. onShareAppMessage: function () {
  75. },
  76. addressChange:function(e){//所在地区发生改变
  77. this.data.submitData.province = e.detail.value[0];
  78. this.data.submitData.city = e.detail.value[1];
  79. this.data.submitData.district = e.detail.value[2];
  80. this.setData({
  81. submitData:this.data.submitData
  82. })
  83. },
  84. getRealName: function (e) {//获取收货人
  85. this.data.submitData.realName = e.detail.value;
  86. this.setData({
  87. submitData: this.data.submitData
  88. })
  89. },
  90. getMobile: function (e) {//获取手机号码
  91. this.data.submitData.mobile = e.detail.value;
  92. this.setData({
  93. submitData: this.data.submitData
  94. })
  95. },
  96. getAddressDetail: function (e) {//获取详细地址
  97. this.data.submitData.addressDetail = e.detail.value;
  98. this.setData({
  99. submitData: this.data.submitData
  100. })
  101. },
  102. getAddress: function () {//获取地址
  103. app.wxRequest(app.globalData.urlRoot +"address/getAddress",{},res=>{
  104. if(res.code == 200){
  105. if(res.data){
  106. this.data.isAddress = true;
  107. this.data.submitData.realName = res.data.realName;
  108. this.data.submitData.mobile = res.data.mobile;
  109. this.data.submitData.province = res.data.province;
  110. this.data.submitData.city = res.data.city;
  111. this.data.submitData.district = res.data.district;
  112. this.data.submitData.addressDetail = res.data.addressDetail;
  113. this.setData({
  114. submitData: this.data.submitData
  115. })
  116. }
  117. }else{
  118. wx.showToast({
  119. title: res.msg,
  120. icon:"none"
  121. })
  122. }
  123. },this);
  124. },
  125. addAddress:function(){//添加地址
  126. app.wxRequest(app.globalData.urlRoot + "address/addAddress", this.data.submitData,res => {
  127. wx.showToast({
  128. title: res.msg,
  129. })
  130. if (res.code == 200) {
  131. wx.navigateBack({
  132. delta: 1
  133. })
  134. }
  135. },this,"POST")
  136. },
  137. updateAddress: function () {//更新地址
  138. app.wxRequest(app.globalData.urlRoot + "address/updateAddress", this.data.submitData, res => {
  139. wx.showToast({
  140. title: res.msg,
  141. })
  142. if (res.code == 200) {
  143. wx.navigateBack({
  144. delta:1
  145. })
  146. }
  147. }, this,"POST");
  148. },
  149. getUserPhone: function (e) {//获取用户手机号
  150. this.setData({
  151. phoneInputShow: true
  152. })
  153. if (e.detail.errMsg == 'getPhoneNumber:ok') {
  154. app.getMobile(e.detail.encryptedData, e.detail.iv, res => {
  155. if (res.code == 200) {
  156. this.data.submitData.mobile = res.data.decodeData.phoneNumber;
  157. this.setData({
  158. submitData: this.data.submitData
  159. })
  160. } else {
  161. wx.showToast({
  162. title: res.msg,
  163. icon: "none"
  164. })
  165. }
  166. }, this);
  167. }
  168. },
  169. submitAddress:function(){//保存地址
  170. if (!this.data.submitData.realName){
  171. wx.showToast({
  172. title: '请输入收货人',
  173. icon:"none"
  174. })
  175. return;
  176. }
  177. if (!app.mobileVerify(this.data.submitData.mobile)) {
  178. if (this.data.submitData.mobile) {
  179. wx.showToast({
  180. title: '请输入正确的电话',
  181. icon: 'none'
  182. })
  183. } else {
  184. wx.showToast({
  185. title: '请输入电话',
  186. icon: 'none'
  187. })
  188. }
  189. return;
  190. }
  191. if (!this.data.submitData.province) {
  192. wx.showToast({
  193. title: '请选择所在地区',
  194. icon: 'none'
  195. })
  196. return;
  197. }
  198. if (!this.data.submitData.addressDetail) {
  199. wx.showToast({
  200. title: '请输入详细地址',
  201. icon: "none"
  202. })
  203. return;
  204. }
  205. if(this.data.isAddress){
  206. this.updateAddress();
  207. } else {
  208. this.addAddress();
  209. }
  210. }
  211. })