123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- <template>
- <mobile-frame>
- <view class="main">
- <view class="one">
- <uni-forms ref="form" v-model="form" :rules="rules" label-width="auto">
- <uni-forms-item label="收货人" name="name">
- <uni-easyinput type="text" v-model="form.name" placeholder="请输入收货人" />
- </uni-forms-item>
- <uni-forms-item label="联系电话" name="phone">
- <uni-easyinput type="number" v-model="form.phone" placeholder="请输入联系电话" />
- </uni-forms-item>
- <uni-forms-item label="地图选点">
- <button type="default" size="mini" class="toLoacl" @click="toLocaltion()">地图选点</button>
- </uni-forms-item>
- <uni-forms-item label="所在省份" name="province">
- <picker class="picker" mode="selector" :range="provinceList" @change="proChange" range-key="value">
- <view>{{form.province||'请选择所在省份'}}</view>
- </picker>
- </uni-forms-item>
- <uni-forms-item label="所在市" name="city">
- <picker class="picker" mode="selector" :range="cityList" @change="cityChange" range-key="value">
- <view>{{form.city||'请选择所在市'}}</view>
- </picker>
- </uni-forms-item>
- <uni-forms-item label="所在区" name="area">
- <picker class="picker" mode="selector" :range="areaList" @change="areaChange" range-key="value">
- <view>{{form.area||'请选择所在区'}}</view>
- </picker>
- </uni-forms-item>
- <uni-forms-item label="详细地址" name="address">
- <uni-easyinput type="textarea" v-model="form.address" placeholder="请输入详细地址" />
- </uni-forms-item>
- <uni-forms-item label="是否默认" name="is_default">
- <switch color="#FB1438" :checked="form.is_default=='1'?true:false" @change="defaChange" />
- </uni-forms-item>
- </uni-forms>
- <view class="btn">
- <button type="primary" size="mini" @click="onSubmit('form')">提交保存</button>
- </view>
- </view>
- </view>
- </mobile-frame>
- </template>
- <script>
- export default {
- data() {
- return {
- user: {},
- id: '',
- form: {},
- rules: {
- name: {
- rules: [{
- required: true,
- errorMessage: '请输入收货人',
- }]
- },
- phone: {
- rules: [{
- required: true,
- errorMessage: '请输入联系电话',
- tel: true
- }]
- },
- province: {
- rules: [{
- required: true,
- errorMessage: '请选择所在省'
- }]
- },
- city: {
- rules: [{
- required: true,
- errorMessage: '请选择所在市'
- }]
- },
- area: {
- rules: [{
- required: false,
- errorMessage: '请选择所在区'
- }]
- },
- address: {
- rules: [{
- required: true,
- errorMessage: '请输入详细地址'
- }]
- },
- },
- provinceList: [ //所在省份
- {
- code: '1',
- value: '吉林省',
- },
- {
- code: '2',
- value: '辽宁省',
- },
- {
- code: '3',
- value: '黑龙江省',
- },
- ],
- cityList: [ //所在市
- {
- code: '1-1',
- value: '长春市',
- },
- {
- code: '2-1',
- value: '大连市',
- },
- {
- code: '3-1',
- value: '哈尔滨市',
- },
- ],
- areaList: [ //所在区
- {
- code: '1-1-1',
- value: '朝阳区'
- },
- {
- code: '2-1-1',
- value: '开发区'
- },
- {
- code: '3-1-1',
- value: '发展区'
- },
- ]
- };
- },
- onLoad: function(e) {
- const that = this;
- that.$set(that, `id`, e && e.id || '');
- that.watchLogin();
- },
- onShow: function() {
- const that = this;
- },
- onUnload: function() {},
- methods: {
- // 监听用户是否登录
- watchLogin() {
- const that = this;
- uni.getStorage({
- key: 'token',
- success: async function(res) {
- let user = that.$jwt(res.data);
- if (user) {
- that.$set(that, `user`, user);
- if (that.id) {
- let arr = await that.$api(`/address/${that.id}`, 'GET')
- if (arr.errcode == '0') {
- that.$set(that, `form`, arr.data)
- }
- } else {
- that.$set(that, `form`, {
- name: user.name || '',
- phone: user.phone || ''
- })
- }
- }
- },
- fail: function(err) {
- uni.reLaunch({
- url: '/pages/login/index'
- })
- }
- })
- },
- // 定位
- toLocaltion() {
- const that = this;
- uni.chooseLocation({
- success: function(res) {
- if (res) {
- that.$set(that.form, `address`, res.address || '')
- }
- },
- fail: function(err) {
- console.log(err);
- }
- })
- // const key = that.$config.txlocalKey;
- // const referer = that.$config.txlocalReferer;
- // uni.navigateTo({
- // url: `plugin://chooseLocation/index?key=${key}&referer=${referer}`
- // });
- },
- // 选择省份
- proChange(e) {
- const that = this;
- let data = that.provinceList[e.detail.value];
- if (data) {
- that.$set(that.form, `province`, data.value)
- }
- },
- // 选择区
- areaChange(e) {
- const that = this;
- let data = that.areaList[e.detail.value];
- if (data) {
- that.$set(that.form, `area`, data.value)
- }
- },
- // 选择市
- cityChange(e) {
- const that = this;
- let data = that.cityList[e.detail.value];
- if (data) {
- that.$set(that.form, `city`, data.value)
- }
- },
- // 是否默认
- defaChange(e) {
- const that = this;
- let value = e.detail.value == true ? '1' : '0';
- that.$set(that.form, `is_default`, value)
- },
- // 提交保存
- onSubmit(ref) {
- const that = this;
- let id = that.id;
- let user = that.user;
- that.$refs[ref].validate().then(async params => {
- params.customer = user.id;
- let res;
- if (id) res = await that.$api(`/address/${id}`, 'POST', params)
- else res = await that.$api(`/address`, 'POST', params);
- if (res.errcode == '0') {
- uni.showToast({
- title: '维护信息成功',
- icon: 'none'
- })
- uni.navigateBack({
- delta: 1
- })
- } else {
- uni.showToast({
- title: res.errmsg,
- icon: 'none'
- })
- }
- })
- },
- }
- }
- </script>
- <style lang="scss">
- .main {
- display: flex;
- flex-direction: column;
- width: 100vw;
- height: 100vh;
- .one {
- padding: 2vw;
- .toLoacl {
- margin: 1vw 0 0 0;
- }
- .picker {
- border: 1px solid #3333;
- border-radius: 5px;
- padding: 2vw;
- }
- .btn {
- text-align: center;
- button {
- width: 30%;
- font-size: 14px;
- background-color: var(--f35BColor);
- }
- }
- }
- }
- </style>
|