add.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <template>
  2. <mobile-frame>
  3. <view class="main">
  4. <view class="one">
  5. <uni-forms ref="form" v-model="form" :rules="rules" label-width="auto">
  6. <uni-forms-item label="收货人" name="name">
  7. <uni-easyinput type="text" v-model="form.name" placeholder="请输入收货人" />
  8. </uni-forms-item>
  9. <uni-forms-item label="联系电话" name="phone">
  10. <uni-easyinput type="number" v-model="form.phone" placeholder="请输入联系电话" />
  11. </uni-forms-item>
  12. <uni-forms-item label="地图选点">
  13. <button type="default" size="mini" class="toLoacl" @click="toLocaltion()">地图选点</button>
  14. </uni-forms-item>
  15. <uni-forms-item label="所在省份" name="province">
  16. <picker class="picker" mode="selector" :range="provinceList" @change="proChange" range-key="name">
  17. <view>{{form.province||'请选择所在省份'}}</view>
  18. </picker>
  19. </uni-forms-item>
  20. <uni-forms-item label="所在市" name="city">
  21. <picker class="picker" mode="selector" :range="cityList" @change="cityChange" range-key="name">
  22. <view>{{form.city||'请选择所在市'}}</view>
  23. </picker>
  24. </uni-forms-item>
  25. <uni-forms-item label="所在区" name="area">
  26. <picker class="picker" mode="selector" :range="areaList" @change="areaChange">
  27. <view>{{form.area||'请选择所在区'}}</view>
  28. </picker>
  29. </uni-forms-item>
  30. <uni-forms-item label="详细地址" name="address">
  31. <uni-easyinput type="textarea" v-model="form.address" placeholder="请输入详细地址" />
  32. </uni-forms-item>
  33. <uni-forms-item label="是否默认" name="is_default">
  34. <switch color="#FB1438" :checked="form.is_default=='1'?true:false" @change="defaChange" />
  35. </uni-forms-item>
  36. </uni-forms>
  37. <view class="btn">
  38. <button type="primary" size="mini" @click="onSubmit('form')">提交保存</button>
  39. </view>
  40. </view>
  41. </view>
  42. </mobile-frame>
  43. </template>
  44. <script>
  45. import {
  46. getAddress
  47. } from '@/common/tool.js';
  48. export default {
  49. data() {
  50. return {
  51. user: {},
  52. id: '',
  53. form: {},
  54. rules: {
  55. name: {
  56. rules: [{
  57. required: true,
  58. errorMessage: '请输入收货人',
  59. }]
  60. },
  61. phone: {
  62. rules: [{
  63. required: true,
  64. errorMessage: '请输入联系电话',
  65. tel: true
  66. }]
  67. },
  68. province: {
  69. rules: [{
  70. required: true,
  71. errorMessage: '请选择所在省'
  72. }]
  73. },
  74. city: {
  75. rules: [{
  76. required: true,
  77. errorMessage: '请选择所在市'
  78. }]
  79. },
  80. area: {
  81. rules: [{
  82. required: false,
  83. errorMessage: '请选择所在区'
  84. }]
  85. },
  86. address: {
  87. rules: [{
  88. required: true,
  89. errorMessage: '请输入详细地址'
  90. }]
  91. },
  92. },
  93. provinceList: [ //所在省份
  94. ],
  95. cityList: [ //所在市
  96. ],
  97. areaList: [ //所在区
  98. ],
  99. paltForm: ''
  100. };
  101. },
  102. onLoad: function(e) {
  103. const that = this;
  104. that.$set(that, `id`, e && e.id || '');
  105. that.watchLogin();
  106. },
  107. onShow: function() {
  108. const that = this;
  109. uni.getStorage({
  110. key: 'system',
  111. success: function(res) {
  112. that.$set(that, `paltForm`, res.data.uniPlatform)
  113. }
  114. })
  115. },
  116. onUnload: function() {},
  117. methods: {
  118. // 监听用户是否登录
  119. watchLogin() {
  120. const that = this;
  121. uni.getStorage({
  122. key: 'token',
  123. success: async function(res) {
  124. let user = that.$jwt(res.data);
  125. if (user) {
  126. that.$set(that, `user`, user);
  127. if (that.id) {
  128. let arr = await that.$api(`/address/${that.id}`, 'GET')
  129. if (arr.errcode == '0') {
  130. that.$set(that, `form`, arr.data)
  131. }
  132. } else {
  133. that.$set(that, `form`, {
  134. name: user.name || '',
  135. phone: user.phone || ''
  136. })
  137. }
  138. }
  139. that.searchPor();
  140. },
  141. fail: function(err) {
  142. uni.reLaunch({
  143. url: '/pages/login/index'
  144. })
  145. }
  146. })
  147. },
  148. // 查找省
  149. searchPor() {
  150. const that = this;
  151. let list = that.$config.china;
  152. that.$set(that, `provinceList`, list)
  153. },
  154. // 定位
  155. toLocaltion() {
  156. const that = this;
  157. uni.chooseLocation({
  158. success: function(res) {
  159. that.searchAddress(res);
  160. }
  161. })
  162. },
  163. // 解析位置详细信息
  164. searchAddress(e) {
  165. const that = this;
  166. that.$set(that.form, `address`, e.address);
  167. if (that.paltForm == 'app') {
  168. var point = new plus.maps.Point(e.longitude, e.latitude);
  169. plus.maps.Map.reverseGeocode(point, {}, function(event) {
  170. var address = event.address;
  171. var reg = /.+?(省|市|自治区|自治州|县|区)/g;
  172. let addressList = address.match(reg).toString().split(",");
  173. let province = addressList[0] || '';
  174. let city = addressList[1] || '';
  175. let area = addressList[2] || '';
  176. that.$set(that.form, `province`, province);
  177. that.$set(that.form, `city`, city);
  178. that.$set(that.form, `area`, area);
  179. })
  180. } else if (that.paltForm == 'mp-weixin') {
  181. getAddress(e.longitude, e.latitude).then((res) => {
  182. if (res.status == 0) {
  183. let province = res.result.address_component.province;
  184. let city = res.result.address_component.city;
  185. let area = res.result.address_component.district;
  186. that.$set(that.form, `province`, province);
  187. that.$set(that.form, `city`, city);
  188. that.$set(that.form, `area`, area);
  189. } else {
  190. uni.showToast({
  191. title: '解析位置失败,请重新选择!'
  192. });
  193. }
  194. })
  195. }
  196. },
  197. // 选择省份
  198. proChange(e) {
  199. const that = this;
  200. let data = that.provinceList[e.detail.value];
  201. if (data) {
  202. that.$set(that.form, `province`, data.name);
  203. that.$set(that, `cityList`, data.city)
  204. }
  205. },
  206. // 选择市
  207. cityChange(e) {
  208. const that = this;
  209. let data = that.cityList[e.detail.value];
  210. if (data) {
  211. that.$set(that.form, `city`, data.name);
  212. that.$set(that, `areaList`, data.area);
  213. }
  214. },
  215. // 选择区
  216. areaChange(e) {
  217. const that = this;
  218. let data = that.areaList[e.detail.value];
  219. if (data) {
  220. that.$set(that.form, `area`, data)
  221. }
  222. },
  223. // 是否默认
  224. defaChange(e) {
  225. const that = this;
  226. let value = e.detail.value == true ? '1' : '0';
  227. that.$set(that.form, `is_default`, value)
  228. },
  229. // 提交保存
  230. onSubmit(ref) {
  231. const that = this;
  232. let id = that.id;
  233. let user = that.user;
  234. that.$refs[ref].validate().then(async params => {
  235. params.customer = user.id;
  236. let res;
  237. if (id) res = await that.$api(`/address/${id}`, 'POST', params)
  238. else res = await that.$api(`/address`, 'POST', params);
  239. if (res.errcode == '0') {
  240. uni.showToast({
  241. title: '维护信息成功',
  242. icon: 'none'
  243. })
  244. uni.navigateBack({
  245. delta: 1
  246. })
  247. } else {
  248. uni.showToast({
  249. title: res.errmsg,
  250. icon: 'none'
  251. })
  252. }
  253. })
  254. },
  255. }
  256. }
  257. </script>
  258. <style lang="scss">
  259. .main {
  260. display: flex;
  261. flex-direction: column;
  262. width: 100vw;
  263. height: 100vh;
  264. .one {
  265. padding: 2vw;
  266. .toLoacl {
  267. margin: 1vw 0 0 0;
  268. }
  269. .picker {
  270. border: 1px solid #3333;
  271. border-radius: 5px;
  272. padding: 2vw;
  273. }
  274. .btn {
  275. text-align: center;
  276. button {
  277. width: 30%;
  278. font-size: 14px;
  279. background-color: var(--f35BColor);
  280. }
  281. }
  282. }
  283. }
  284. </style>