index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // index.js
  2. let myPro = require("../../../../utils/wxRequest.js");
  3. let util = require("../../../../utils/util.js");
  4. Page({
  5. data: {
  6. id: null, // 地址id 用于编辑信息
  7. name: '',
  8. sex: 1,
  9. phone: '',
  10. area_address: '',
  11. address: '',
  12. checked: false, // 是否默认
  13. loading: false // 全局loading
  14. },
  15. onLoad: function (options) {
  16. // Do some initialize when page load.
  17. let that = this;
  18. // 编辑地址id
  19. if(options.id){
  20. that.setData({
  21. id: options.id
  22. });
  23. that.getAddressInfo();
  24. };
  25. // 新增地址 来自微信
  26. if(options.type && options.type == 'wechat'){
  27. wx.getStorage({
  28. key: 'wechatAddress',
  29. success: function(res){
  30. let obj = JSON.parse(res.data);
  31. that.setData({
  32. name: obj.name,
  33. phone: obj.phone,
  34. address: obj.address
  35. });
  36. wx.removeStorageSync('wechatAddress');
  37. }
  38. })
  39. }
  40. },
  41. onShow: function () {
  42. // Do something when page show.
  43. let that = this;
  44. },
  45. onReady: function () {
  46. // Do something when page ready.
  47. },
  48. onHide: function () {
  49. // Do something when page hide.
  50. },
  51. onUnload: function () {
  52. // Do something when page close.
  53. },
  54. onPullDownRefresh: function () {
  55. // Do something when pull down.
  56. },
  57. onReachBottom: function () {
  58. // Do something when page reach bottom.
  59. },
  60. onPageScroll: function () {
  61. // Do something when page scroll
  62. },
  63. onResize: function () {
  64. // Do something when page resize
  65. },
  66. // 获取地址详情
  67. getAddressInfo(){
  68. let that = this;
  69. let params = {
  70. id: that.data.id
  71. };
  72. myPro.wxRequest("user/address/detail","GET",params).then(res=>{
  73. let info = res.result;
  74. that.setData({
  75. name: info.name,
  76. sex: info.sex,
  77. phone: info.phone,
  78. area_address: info.area_address,
  79. address_lat: info.lat,
  80. address_lng: info.lnt,
  81. address: info.address,
  82. checked: info.is_default ? true :false,
  83. });
  84. }).catch(err=>{
  85. console.log('报错信息',err);
  86. wx.showToast({
  87. title: err,
  88. icon: "none"
  89. })
  90. })
  91. },
  92. // 收货人性别
  93. onChangeName(event){
  94. let that = this;
  95. that.setData({
  96. name: event.detail
  97. });
  98. },
  99. // 选择性别
  100. onChangeSex(event){
  101. let that = this;
  102. that.setData({
  103. sex: event.detail
  104. });
  105. },
  106. // 获取输入的手机号
  107. onChangePhone(event){
  108. let that = this;
  109. that.setData({
  110. phone: event.detail
  111. });
  112. },
  113. // 获取输入的地址
  114. onChangeAddress(event){
  115. let that = this;
  116. that.setData({
  117. address: event.detail
  118. });
  119. },
  120. // 设置默认
  121. onChangeDefault(event){
  122. let that = this;
  123. that.setData({
  124. checked: event.detail
  125. });
  126. },
  127. // 选点
  128. toChooseLocation(){
  129. var that = this
  130. // 选点
  131. wx.chooseLocation({
  132. success: function(res){
  133. // console.log('成功',res)
  134. that.setData({
  135. area_address: res.address,
  136. address_lat: res.latitude,
  137. address_lng: res.longitude
  138. });
  139. },
  140. fail: function(res){
  141. console.log('失败',res)
  142. }
  143. })
  144. },
  145. // 提交
  146. saveAddress(){
  147. let that = this;
  148. if (that.data.name == null || that.data.name == '') {
  149. wx.showToast({
  150. title: '请输入联系人',
  151. icon: 'none'
  152. });
  153. return;
  154. };
  155. if (that.data.phone == null || that.data.phone == '' || !(/^1[3456789]\d{9}$/.test(that.data.phone))) {
  156. wx.showToast({
  157. title: '请检查手机号',
  158. icon: 'none'
  159. });
  160. return;
  161. };
  162. if ( !that.data.area_address) {
  163. wx.showToast({
  164. title: '请选择所在地区',
  165. icon: 'none'
  166. });
  167. return;
  168. };
  169. if (that.data.address == null || that.data.address == '') {
  170. wx.showToast({
  171. title: '请输入详细地址',
  172. icon: 'none'
  173. });
  174. return;
  175. };
  176. let params = {
  177. name: that.data.name,
  178. phone: that.data.phone,
  179. area_address: that.data.area_address,
  180. address: that.data.address,
  181. lnt: that.data.address_lng,
  182. lat: that.data.address_lat,
  183. is_default: that.data.checked ? 1 : 0 // int 是否默认(1是 0否)
  184. };
  185. // 性别
  186. if(that.data.sex){
  187. params.sex = that.data.sex;
  188. };
  189. // 编辑
  190. if(that.data.id){
  191. params.id = that.data.id;
  192. myPro.wxRequest("user/address/edit","POST",params).then(res=>{
  193. wx.showToast({
  194. title: res.msg,
  195. icon: 'none',
  196. success: function(){
  197. wx.navigateBack();
  198. }
  199. });
  200. }).catch(err=>{
  201. console.log('报错信息',err);
  202. wx.showToast({
  203. title: err,
  204. icon: 'none'
  205. });
  206. });
  207. }else{
  208. // 新增
  209. myPro.wxRequest("user/address/create","POST",params).then(res=>{
  210. wx.showToast({
  211. title: res.msg,
  212. icon: 'none',
  213. success: function(){
  214. wx.navigateBack();
  215. }
  216. });
  217. }).catch(err=>{
  218. console.log('报错信息',err);
  219. wx.showToast({
  220. title: err,
  221. icon: 'none'
  222. });
  223. });
  224. }
  225. },
  226. });