add.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <template>
  2. <view class="content vflex jbetween">
  3. <view class="form">
  4. <view class="hflex acenter jbetween item">
  5. <view class="left">收货人</view>
  6. <u-input v-model="name" border="none" placeholder="请填写收货人姓名"></u-input>
  7. </view>
  8. <view class="hflex acenter jbetween item">
  9. <view class="left">手机号码</view>
  10. <u-input v-model="phone" border="none" placeholder="请填写收货人手机号码"></u-input>
  11. </view>
  12. <view class="hflex acenter jbetween item" @click.stop="openmap">
  13. <view class="left">所在地区</view>
  14. <u-input v-model="addressObj.province + addressObj.city + addressObj.area" border="none" disabled disabledColor="#fff" placeholder="请选择所在地区"></u-input>
  15. <u-icon name="map-fill" color="#506DFF" size="13"></u-icon>
  16. </view>
  17. <view class="hflex acenter jbetween item">
  18. <view class="left">详细地址</view>
  19. <u-input v-model="addressObj.address" border="none" placeholder="请输入街道、楼牌号"></u-input>
  20. </view>
  21. <view class="hflex acenter jbetween item">
  22. <view class="left">设为默认地址</view>
  23. <u-switch v-model="is_active" activeColor="#506dff" inactiveColor="rgb(230, 230, 230)" @change="change"></u-switch>
  24. </view>
  25. </view>
  26. <view class="btn" v-if="is_edit == 0" @click="addAddress">保存</view>
  27. <view class="bottom hflex acenter jbetween" v-else>
  28. <view class="del_btn" @click="deleteAdd">删除</view>
  29. <view class="save_btn" @click="editAddress">保存</view>
  30. </view>
  31. </view>
  32. </template>
  33. <script>
  34. import $api from '@/static/js/api.js'
  35. var that = ''
  36. export default {
  37. data() {
  38. return {
  39. name: '',
  40. phone: '',
  41. province: '',
  42. city: '',
  43. area: '',
  44. areas: '',
  45. address: '',
  46. is_active: false,
  47. is_edit: 0,
  48. id: '',
  49. addressObj: {}
  50. }
  51. },
  52. onLoad(options) {
  53. that = this
  54. if(options.index) {
  55. that.getPredata(options.index)
  56. that.id = options.id
  57. }
  58. },
  59. methods: {
  60. getPredata(index) {
  61. that.is_edit = 1
  62. var pages = getCurrentPages()
  63. var prePage = pages[pages.length - 2]
  64. var data = prePage.$vm.pageList[index]
  65. that.name = data.name
  66. that.phone = data.phone
  67. that.addressObj.province = data.province
  68. that.addressObj.city = data.city
  69. that.addressObj.area = data.area
  70. that.addressObj.areas = data.province + that.city + that.area
  71. that.addressObj.address = data.address
  72. that.id = data.id
  73. if(data.type) {
  74. that.is_active = true
  75. } else {
  76. that.is_active = false
  77. }
  78. },
  79. // 选择地区
  80. bindPickerChange(e) {
  81. console.log(e);
  82. that.province = e.detail.value[0]
  83. that.city = e.detail.value[1]
  84. that.area = e.detail.value[2]
  85. that.areas = e.detail.value[0] + e.detail.value[1] + e.detail.value[2]
  86. },
  87. openmap() {
  88. console.log('选择地址');
  89. uni.getLocation({
  90. type: 'wgs84',
  91. success: (res)=>{
  92. console.log(res);
  93. if(res.longitude && res.latitude){
  94. $api.jump('/pages/user/map?lng='+res.longitude+'&lat='+res.latitude)
  95. }
  96. },
  97. fail:()=>{
  98. uni.getSetting({
  99. success: (res) => {
  100. console.log(res);
  101. var status = res.authSetting;
  102. if(!status['scope.userLocation']){
  103. uni.showModal({
  104. title:"是否授权当前位置",
  105. content:"需要获取您的地理位置,请确认授权,否则地图功能将无法使用",
  106. success:(tip)=>{
  107. if(tip.confirm){
  108. uni.openSetting({
  109. success:(data)=>{
  110. if(data.authSetting['scope.userLocation']===true){
  111. uni.showToast({
  112. title:"授权成功",
  113. icon:"success",
  114. duration:1000
  115. })
  116. uni.getLocation({
  117. type: 'wgs84',
  118. success: (res)=>{
  119. console.log('当前位置的经度:' + res.longitude);
  120. console.log('当前位置的纬度:' + res.latitude);
  121. $api.jump('/pages/user/map?lng='+res.longitude+'&lat='+res.latitude)
  122. }
  123. })
  124. }else{
  125. uni.showToast({
  126. title:"授权失败",
  127. icon:"none",
  128. duration:1000
  129. })
  130. }
  131. }
  132. })
  133. }
  134. }
  135. })
  136. }else{
  137. uni.getLocation({
  138. type: 'wgs84',
  139. success: (res)=>{
  140. console.log('当前位置的经度:' + res.longitude);
  141. console.log('当前位置的纬度:' + res.latitude);
  142. $api.jump('/pages/user/map?lng='+res.longitude+'&lat='+res.latitude)
  143. }
  144. })
  145. }
  146. },
  147. fail: (res) => {
  148. uni.showToast({
  149. title:"调用授权窗口失败",
  150. icon:"none",
  151. duration:1000
  152. })
  153. }
  154. })
  155. }
  156. })
  157. },
  158. // 设置默认地址
  159. change(e) {
  160. that.is_active = e
  161. },
  162. // 添加收获地址
  163. addAddress() {
  164. uni.showLoading({
  165. title: '添加中',
  166. mask:true
  167. })
  168. if($api.formCheck(that.name,'required') && $api.formCheck(that.phone,'phone') && $api.formCheck(that.addressObj.address,'required')) {
  169. $api.req({
  170. url: '/data/api.User/add_address',
  171. method: 'POST',
  172. data: {
  173. type: that.is_active?1:0,
  174. name: that.name,
  175. phone: that.phone,
  176. province: that.addressObj.province,
  177. city: that.addressObj.city,
  178. area: that.addressObj.area,
  179. address: that.addressObj.address
  180. }
  181. }, function(res) {
  182. $api.info(res.info)
  183. if(res.code == 1) {
  184. uni.hideLoading()
  185. if(that.is_active) {
  186. uni.setStorageSync('address_id',that.id)
  187. }
  188. $api.jump(1,-1)
  189. }
  190. })
  191. }
  192. },
  193. // 修改地址
  194. editAddress() {
  195. if($api.formCheck(that.name,'required') && $api.formCheck(that.phone,'phone') && $api.formCheck(that.area,'required') && $api.formCheck(that.address,'required')) {
  196. $api.req({
  197. url: '/data/api.User/save_address',
  198. method: 'POST',
  199. data: {
  200. id: that.id,
  201. type: that.is_active?1:0,
  202. name: that.name,
  203. phone: that.phone,
  204. province: that.province,
  205. city: that.city,
  206. area: that.area,
  207. address: that.address
  208. }
  209. }, function(res) {
  210. $api.info(res.info)
  211. if(res.code == 1) {
  212. if(that.is_active) {
  213. uni.setStorageSync('address_id',that.id)
  214. }
  215. $api.jump(1,-1)
  216. }
  217. })
  218. }
  219. },
  220. deleteAdd() {
  221. $api.req({
  222. url: '/data/api.User/del_address',
  223. method: 'POST',
  224. data: {
  225. id: that.id
  226. }
  227. }, function(res) {
  228. if(res.code == 1) {
  229. var address_id = uni.getStorageSync('address_id')
  230. if(that.id == address_id) {
  231. uni.removeStorageSync('address_id')
  232. }
  233. $api.info(res.info)
  234. $api.jump(-1)
  235. }
  236. })
  237. }
  238. },
  239. }
  240. </script>
  241. <style lang="scss" scoped>
  242. .content {
  243. padding: 0 30rpx;
  244. .form {
  245. margin-top: 22rpx;
  246. .item {
  247. padding: 26rpx 0;
  248. }
  249. .left {
  250. width: 200rpx;
  251. font-size: 28rpx;
  252. font-weight: 400;
  253. color: #222222;
  254. line-height: 40rpx;
  255. }
  256. }
  257. .btn {
  258. width: 100%;
  259. height: 84rpx;
  260. background: #506DFF;
  261. border-radius: 42rpx;
  262. font-size: 36rpx;
  263. font-weight: 500;
  264. color: #FFFFFF;
  265. text-align: center;
  266. line-height: 84rpx;
  267. margin-bottom: 66rpx;
  268. }
  269. .bottom {
  270. width: 100%;
  271. margin-bottom: 66rpx;
  272. .del_btn {
  273. width: 230rpx;
  274. height: 88rpx;
  275. border-radius: 44rpx;
  276. border: 1px solid #506DFF;
  277. text-align: center;
  278. line-height: 88rpx;
  279. font-size: 36rpx;
  280. font-weight: 500;
  281. color: #506DFF;
  282. }
  283. .save_btn {
  284. width: 430rpx;
  285. height: 88rpx;
  286. background: #506DFF;
  287. border-radius: 44rpx;
  288. text-align: center;
  289. line-height: 88rpx;
  290. font-size: 36rpx;
  291. font-weight: 500;
  292. color: #FFFFFF;
  293. }
  294. }
  295. }
  296. </style>