index.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import { editAddress, getAddressDetail} from '../../api/user.js';
  2. import { getCity } from '../../api/api.js';
  3. const app = getApp();
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. parameter: {
  10. 'navbar': '1',
  11. 'return': '1',
  12. 'title': '添加地址'
  13. },
  14. region: ['省', '市', '区'],
  15. valueRegion: [0, 0, 0],
  16. cartId:'',//购物车id
  17. pinkId:0,//拼团id
  18. couponId:0,//优惠券id
  19. id:0,//地址id
  20. userAddress: { is_default:false},//地址详情
  21. cityId:0,
  22. district:[],
  23. multiArray:[],
  24. multiIndex: [0, 0, 0],
  25. },
  26. /**
  27. * 授权回调
  28. *
  29. */
  30. onLoadFun:function(){
  31. this.getUserAddress();
  32. },
  33. /**
  34. * 生命周期函数--监听页面加载
  35. */
  36. onLoad: function (options) {
  37. this.setData({
  38. cartId: options.cartId || '',
  39. pinkId: options.pinkId || 0,
  40. couponId: options.couponId || 0,
  41. id: options.id || 0,
  42. 'parameter.title': options.id ? '修改地址' : '添加地址'
  43. });
  44. this.getCityList();
  45. },
  46. getCityList:function(){
  47. let that = this;
  48. getCity().then(res=>{
  49. that.setData({ district:res.data});
  50. that.initialize();
  51. })
  52. },
  53. initialize:function(){
  54. let that = this, province = [], city = [], area = [];
  55. if (that.data.district.length) {
  56. let cityChildren = that.data.district[0].c || [];
  57. let areaChildren = cityChildren.length ? (cityChildren[0].c || []) : [];
  58. that.data.district.forEach(function (item) {
  59. province.push(item.n);
  60. });
  61. cityChildren.forEach(function (item) {
  62. city.push(item.n);
  63. });
  64. areaChildren.forEach(function (item) {
  65. area.push(item.n);
  66. });
  67. that.setData({
  68. multiArray: [province, city, area],
  69. });
  70. }
  71. },
  72. bindRegionChange: function (e) {
  73. let multiIndex = this.data.multiIndex, province = this.data.district[multiIndex[0]] || { c: [] }, city = province.c[multiIndex[1]] || { v: 0 }, multiArray = this.data.multiArray, value = e.detail.value;
  74. console.log(value);
  75. console.log(province);
  76. this.setData({
  77. region: [multiArray[0][value[0]], multiArray[1][value[1]], multiArray[2][value[2]]],
  78. cityId: city.v,
  79. valueRegion: [0,0,0]
  80. });
  81. this.initialize();
  82. },
  83. bindMultiPickerColumnChange:function(e){
  84. let that = this, column = e.detail.column, value = e.detail.value, currentCity = this.data.district[value] || { c: [] }, multiArray = that.data.multiArray, multiIndex = that.data.multiIndex;
  85. multiIndex[column] = value;
  86. switch (column){
  87. case 0:
  88. let areaList = currentCity.c[0] || { c: [] };
  89. multiArray[1] = currentCity.c.map((item)=>{
  90. return item.n;
  91. });
  92. multiArray[2] = areaList.c.map((item)=>{
  93. return item.n;
  94. });
  95. break;
  96. case 1:
  97. let cityList = that.data.district[multiIndex[0]].c[multiIndex[1]].c || [];
  98. multiArray[2] = cityList.map((item)=>{
  99. return item.n;
  100. });
  101. break;
  102. case 2:
  103. break;
  104. }
  105. this.setData({ multiArray: multiArray, multiIndex: multiIndex});
  106. },
  107. getUserAddress:function(){
  108. if(!this.data.id) return false;
  109. let that = this;
  110. getAddressDetail(this.data.id).then(res=>{
  111. var region = [res.data.province, res.data.city, res.data.district];
  112. that.setData({
  113. userAddress: res.data,
  114. region: region,
  115. cityId: res.data.city_id,
  116. });
  117. });
  118. },
  119. getWxAddress:function(){
  120. var that = this;
  121. wx.authorize({
  122. scope: 'scope.address',
  123. success: function (res) {
  124. wx.chooseAddress({
  125. success: function (res) {
  126. var addressP = {};
  127. addressP.province = res.provinceName;
  128. addressP.city = res.cityName;
  129. addressP.district = res.countyName;
  130. editAddress({
  131. address: addressP,
  132. is_default: 1,
  133. real_name: res.userName,
  134. post_code: res.postalCode,
  135. phone: res.telNumber,
  136. detail: res.detailInfo,
  137. id: 0,
  138. type: 1,
  139. }).then(res => {
  140. setTimeout(function () {
  141. if (that.data.cartId) {
  142. var cartId = that.data.cartId;
  143. var pinkId = that.data.pinkId;
  144. var couponId = that.data.couponId;
  145. that.setData({ cartId: '', pinkId: '', couponId: '' })
  146. wx.navigateTo({
  147. url: '/pages/order_confirm/index?cartId=' + cartId + '&addressId=' + (that.data.id ? that.data.id : res.data.id) + '&pinkId=' + pinkId + '&couponId=' + couponId
  148. });
  149. } else {
  150. wx.navigateBack({ delta: 1 });
  151. }
  152. }, 1000);
  153. return app.Tips({ title: "添加成功", icon: 'success' });
  154. }).catch(err => {
  155. return app.Tips({ title: err });
  156. });
  157. },
  158. fail: function (res) {
  159. if (res.errMsg == 'chooseAddress:cancel') return app.Tips({ title: '取消选择' });
  160. },
  161. })
  162. },
  163. fail: function (res) {
  164. wx.showModal({
  165. title: '您已拒绝导入微信地址权限',
  166. content: '是否进入权限管理,调整授权?',
  167. success(res) {
  168. if (res.confirm) {
  169. wx.openSetting({
  170. success: function (res) {
  171. console.log(res.authSetting)
  172. }
  173. });
  174. } else if (res.cancel) {
  175. return app.Tips({ title: '已取消!' });
  176. }
  177. }
  178. })
  179. },
  180. })
  181. },
  182. /**
  183. * 提交用户添加地址
  184. *
  185. */
  186. formSubmit:function(e){
  187. var that = this, value = e.detail.value;
  188. if (!value.real_name) return app.Tips({title:'请填写收货人姓名'});
  189. if (!value.phone) return app.Tips({title:'请填写联系电话'});
  190. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(value.phone)) return app.Tips({title:'请输入正确的手机号码'});
  191. if (that.data.region[0] =='省') return app.Tips({title:'请选择所在地区'});
  192. if (!value.detail) return app.Tips({title:'请填写详细地址'});
  193. value.id=that.data.id;
  194. value.address={
  195. province:that.data.region[0],
  196. city: that.data.region[1],
  197. district: that.data.region[2],
  198. city_id: that.data.cityId,
  199. };
  200. value.is_default = that.data.userAddress.is_default ? 1 : 0;
  201. editAddress(value).then(res=>{
  202. if (that.data.id)
  203. app.Tips({ title: '修改成功', icon: 'success' });
  204. else
  205. app.Tips({ title: '添加成功', icon: 'success' });
  206. setTimeout(function () {
  207. if (that.data.cartId) {
  208. var cartId = that.data.cartId;
  209. var pinkId = that.data.pinkId;
  210. var couponId = that.data.couponId;
  211. that.setData({ cartId: '', pinkId: '', couponId: '' })
  212. wx.navigateTo({
  213. url: '/pages/order_confirm/index?cartId=' + cartId + '&addressId=' + (that.data.id ? that.data.id : res.data.id) + '&pinkId=' + pinkId + '&couponId=' + couponId
  214. });
  215. } else {
  216. wx.navigateBack({ delta: 1 });
  217. }
  218. }, 1000);
  219. }).catch(err=>{
  220. return app.Tips({title:err});
  221. })
  222. },
  223. ChangeIsDefault:function(e){
  224. this.setData({ 'userAddress.is_default': !this.data.userAddress.is_default});
  225. },
  226. })