index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <template>
  2. <view>
  3. <!-- index.wxml -->
  4. <view class="wrap">
  5. <view class="form_wrap">
  6. <view class="form_item">
  7. <view class="item_l">联系人</view>
  8. <view class="item_r">
  9. <van-field :value="name" placeholder="请填写收货人姓名" :border="false" @change="onChangeName" />
  10. </view>
  11. </view>
  12. <view class="form_item">
  13. <view class="item_l">性别</view>
  14. <view class="item_r">
  15. <van-radio-group :value="sex" direction="horizontal" @change="onChangeSex">
  16. <van-radio :name="1" checked-color="#295C56" style="margin-right: 60rpx">男</van-radio>
  17. <van-radio :name="2" checked-color="#295C56">女</van-radio>
  18. </van-radio-group>
  19. </view>
  20. </view>
  21. <view class="form_item">
  22. <view class="item_l">手机号</view>
  23. <view class="item_r">
  24. <van-field :value="phone" placeholder="请填写收货人手机号码" :border="false" @change="onChangePhone" />
  25. </view>
  26. </view>
  27. <view class="form_item">
  28. <view class="item_l">收货地址</view>
  29. <view class="item_r" @tap="toChooseLocation">
  30. <van-field :value="area_address" placeholder="点击选择" :border="false" readonly />
  31. <van-icon name="arrow" color="#333333" />
  32. </view>
  33. </view>
  34. <view class="form_item">
  35. <view class="item_l">门牌号</view>
  36. <view class="item_r">
  37. <van-field :value="address" placeholder="例:B座10楼1003室" :border="false" @change="onChangeAddress" />
  38. </view>
  39. </view>
  40. <view class="form_item">
  41. <van-checkbox :value="checked" checked-color="#295C56" @change="onChangeDefault">设为默认地址</van-checkbox>
  42. </view>
  43. </view>
  44. <view class="btn_wrap">
  45. <view class="btn" @tap="saveAddress">保存</view>
  46. </view>
  47. </view>
  48. <!-- loading -->
  49. <van-overlay :show="loading" :z-index="100">
  50. <van-loading custom-class="custom_loading" />
  51. </van-overlay>
  52. </view>
  53. </template>
  54. <script>
  55. // index.js
  56. let myPro = require('../../../../utils/wxRequest.js');
  57. let util = require('../../../../utils/util.js');
  58. export default {
  59. data() {
  60. return {
  61. id: null,
  62. // 地址id 用于编辑信息
  63. name: '',
  64. sex: 1,
  65. phone: '',
  66. area_address: '',
  67. address: '',
  68. checked: false,
  69. // 是否默认
  70. // 全局loading
  71. loading: false,
  72. address_lat: '',
  73. address_lng: ''
  74. };
  75. },
  76. onLoad: function (options) {
  77. // Do some initialize when page load.
  78. let that = this; // 编辑地址id
  79. if (options.id) {
  80. that.setData({
  81. id: options.id
  82. });
  83. that.getAddressInfo();
  84. } // 新增地址 来自微信
  85. if (options.type && options.type == 'wechat') {
  86. uni.getStorage({
  87. key: 'wechatAddress',
  88. success: function (res) {
  89. let obj = JSON.parse(res.data);
  90. that.setData({
  91. name: obj.name,
  92. phone: obj.phone,
  93. address: obj.address
  94. });
  95. uni.removeStorageSync('wechatAddress');
  96. }
  97. });
  98. }
  99. },
  100. onShow: function () {
  101. // Do something when page show.
  102. let that = this;
  103. },
  104. onReady: function () {
  105. // Do something when page ready.
  106. },
  107. onHide: function () {
  108. // Do something when page hide.
  109. },
  110. onUnload: function () {
  111. // Do something when page close.
  112. },
  113. onPullDownRefresh: function () {
  114. // Do something when pull down.
  115. },
  116. onReachBottom: function () {
  117. // Do something when page reach bottom.
  118. },
  119. onPageScroll: function () {
  120. // Do something when page scroll
  121. },
  122. onResize: function () {
  123. // Do something when page resize
  124. },
  125. methods: {
  126. // 获取地址详情
  127. getAddressInfo() {
  128. let that = this;
  129. let params = {
  130. id: that.id
  131. };
  132. myPro
  133. .wxRequest('user/address/detail', 'GET', params)
  134. .then((res) => {
  135. let info = res.result;
  136. that.setData({
  137. name: info.name,
  138. sex: info.sex,
  139. phone: info.phone,
  140. area_address: info.area_address,
  141. address_lat: info.lat,
  142. address_lng: info.lnt,
  143. address: info.address,
  144. checked: info.is_default ? true : false
  145. });
  146. })
  147. .catch((err) => {
  148. console.log('报错信息', err);
  149. uni.showToast({
  150. title: err,
  151. icon: 'none'
  152. });
  153. });
  154. },
  155. // 收货人性别
  156. onChangeName(event) {
  157. let that = this;
  158. that.setData({
  159. name: event.detail
  160. });
  161. },
  162. // 选择性别
  163. onChangeSex(event) {
  164. let that = this;
  165. that.setData({
  166. sex: event.detail
  167. });
  168. },
  169. // 获取输入的手机号
  170. onChangePhone(event) {
  171. let that = this;
  172. that.setData({
  173. phone: event.detail
  174. });
  175. },
  176. // 获取输入的地址
  177. onChangeAddress(event) {
  178. let that = this;
  179. that.setData({
  180. address: event.detail
  181. });
  182. },
  183. // 设置默认
  184. onChangeDefault(event) {
  185. let that = this;
  186. that.setData({
  187. checked: event.detail
  188. });
  189. },
  190. // 选点
  191. toChooseLocation() {
  192. var that = this; // 选点
  193. uni.chooseLocation({
  194. success: function (res) {
  195. // console.log('成功',res)
  196. that.setData({
  197. area_address: res.address,
  198. address_lat: res.latitude,
  199. address_lng: res.longitude
  200. });
  201. },
  202. fail: function (res) {
  203. console.log('失败', res);
  204. }
  205. });
  206. },
  207. // 提交
  208. saveAddress() {
  209. let that = this;
  210. if (that.name == null || that.name == '') {
  211. uni.showToast({
  212. title: '请输入联系人',
  213. icon: 'none'
  214. });
  215. return;
  216. }
  217. if (that.phone == null || that.phone == '' || !/^1[3456789]\d{9}$/.test(that.phone)) {
  218. uni.showToast({
  219. title: '请检查手机号',
  220. icon: 'none'
  221. });
  222. return;
  223. }
  224. if (!that.area_address) {
  225. uni.showToast({
  226. title: '请选择所在地区',
  227. icon: 'none'
  228. });
  229. return;
  230. }
  231. if (that.address == null || that.address == '') {
  232. uni.showToast({
  233. title: '请输入详细地址',
  234. icon: 'none'
  235. });
  236. return;
  237. }
  238. let params = {
  239. name: that.name,
  240. phone: that.phone,
  241. area_address: that.area_address,
  242. address: that.address,
  243. lnt: that.address_lng,
  244. lat: that.address_lat,
  245. is_default: that.checked ? 1 : 0 // int 是否默认(1是 0否)
  246. }; // 性别
  247. if (that.sex) {
  248. params.sex = that.sex;
  249. } // 编辑
  250. if (that.id) {
  251. params.id = that.id;
  252. myPro
  253. .wxRequest('user/address/edit', 'POST', params)
  254. .then((res) => {
  255. uni.showToast({
  256. title: res.msg,
  257. icon: 'none',
  258. success: function () {
  259. uni.navigateBack();
  260. }
  261. });
  262. })
  263. .catch((err) => {
  264. console.log('报错信息', err);
  265. uni.showToast({
  266. title: err,
  267. icon: 'none'
  268. });
  269. });
  270. } else {
  271. // 新增
  272. myPro
  273. .wxRequest('user/address/create', 'POST', params)
  274. .then((res) => {
  275. uni.showToast({
  276. title: res.msg,
  277. icon: 'none',
  278. success: function () {
  279. uni.navigateBack();
  280. }
  281. });
  282. })
  283. .catch((err) => {
  284. console.log('报错信息', err);
  285. uni.showToast({
  286. title: err,
  287. icon: 'none'
  288. });
  289. });
  290. }
  291. }
  292. }
  293. };
  294. </script>
  295. <style>
  296. /**index.wxss**/
  297. .form_wrap {
  298. padding: 0 30rpx;
  299. }
  300. .form_item {
  301. font-size: 28rpx;
  302. color: #333333;
  303. min-height: 98rpx;
  304. display: flex;
  305. align-items: center;
  306. border-bottom: 1rpx solid rgba(0, 0, 0, 0.1);
  307. }
  308. .form_item:last-child {
  309. border-bottom: 0 none;
  310. }
  311. .form_item .item_l {
  312. width: 150rpx;
  313. }
  314. .form_item .item_r {
  315. flex: 1;
  316. display: flex;
  317. align-items: center;
  318. justify-content: space-between;
  319. }
  320. .form_item van-field {
  321. width: 100%;
  322. }
  323. .form_item .van-cell {
  324. padding: 0 !important;
  325. }
  326. .btn_wrap {
  327. margin-top: 30rpx;
  328. }
  329. .btn_wrap .btn {
  330. width: 690rpx;
  331. height: 100rpx;
  332. margin: 0 auto;
  333. background: #295c56;
  334. border-radius: 50rpx;
  335. font-size: 34rpx;
  336. color: #ffffff;
  337. display: flex;
  338. align-items: center;
  339. justify-content: center;
  340. }
  341. </style>