add.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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.id || '');
  105. if (e.id) {
  106. uni.setNavigationBarTitle({
  107. title: '修改地址'
  108. });
  109. }
  110. that.searchPor();
  111. that.watchLogin();
  112. },
  113. onShow: function() {
  114. const that = this;
  115. uni.getStorage({
  116. key: 'system',
  117. success: function(res) {
  118. that.$set(that, `paltForm`, res.data.uniPlatform)
  119. }
  120. })
  121. },
  122. methods: {
  123. // 监听用户是否登录
  124. watchLogin() {
  125. const that = this;
  126. uni.getStorage({
  127. key: 'token',
  128. success: async function(res) {
  129. let user = that.$jwt(res.data);
  130. if (user) {
  131. that.$set(that, `user`, user);
  132. if (that.id) {
  133. let arr = await that.$api(`/address/${that.id}`, 'GET')
  134. if (arr.errcode == '0') {
  135. let index = that.provinceList.findIndex((i) => i.name == arr.data.province);
  136. if (index) {
  137. that.proChange(parseFloat(index));
  138. }
  139. that.$set(that, `form`, arr.data)
  140. }
  141. } else {
  142. that.$set(that, `form`, {
  143. name: user.name || '',
  144. phone: user.phone || ''
  145. })
  146. }
  147. }
  148. },
  149. fail: function(err) {
  150. uni.reLaunch({
  151. url: '/pages/login/index'
  152. })
  153. }
  154. })
  155. },
  156. // 查找省
  157. searchPor() {
  158. const that = this;
  159. let list = that.$config.china;
  160. that.$set(that, `provinceList`, list)
  161. },
  162. // 定位
  163. toLocaltion() {
  164. const that = this;
  165. uni.chooseLocation({
  166. success: function(res) {
  167. that.searchAddress(res);
  168. }
  169. })
  170. },
  171. // 解析位置详细信息
  172. searchAddress(e) {
  173. const that = this;
  174. that.$set(that.form, `address`, e.address);
  175. if (that.paltForm == 'app') {
  176. var point = new plus.maps.Point(e.longitude, e.latitude);
  177. plus.maps.Map.reverseGeocode(point, {}, function(event) {
  178. var address = event.address;
  179. var reg = /.+?(省|市|自治区|自治州|县|区)/g;
  180. let addressList = address.match(reg).toString().split(",");
  181. let province = addressList[0] || '';
  182. let city = addressList[1] || '';
  183. let area = addressList[2] || '';
  184. that.$set(that.form, `province`, province);
  185. that.$set(that.form, `city`, city);
  186. that.$set(that.form, `area`, area);
  187. })
  188. } else if (that.paltForm == 'mp-weixin') {
  189. getAddress(e.longitude, e.latitude).then((res) => {
  190. if (res.status == 0) {
  191. let province = res.result.address_component.province;
  192. let city = res.result.address_component.city;
  193. let area = res.result.address_component.district;
  194. that.$set(that.form, `province`, province);
  195. that.$set(that.form, `city`, city);
  196. that.$set(that.form, `area`, area);
  197. } else {
  198. uni.showToast({
  199. title: '解析位置失败,请重新选择!'
  200. });
  201. }
  202. })
  203. }
  204. },
  205. // 选择省份
  206. proChange(e) {
  207. const that = this;
  208. let index = 0;
  209. if (e && e.detail && e.detail.value) index = e.detail.value
  210. else index = e;
  211. let data = that.provinceList[index];
  212. if (data) {
  213. if (that.id) {
  214. that.$set(that.form, `city`, '');
  215. that.$set(that.form, `area`, '');
  216. }
  217. that.$set(that.form, `province`, data.name);
  218. that.$set(that, `cityList`, data.city)
  219. }
  220. },
  221. // 选择市
  222. cityChange(e) {
  223. const that = this;
  224. let data = that.cityList[e.detail.value];
  225. if (data) {
  226. if (that.id) {
  227. that.$set(that.form, `area`, '');
  228. }
  229. that.$set(that.form, `city`, data.name);
  230. that.$set(that, `areaList`, data.area);
  231. }
  232. },
  233. // 选择区
  234. areaChange(e) {
  235. const that = this;
  236. let data = that.areaList[e.detail.value];
  237. if (data) {
  238. that.$set(that.form, `area`, data)
  239. }
  240. },
  241. // 是否默认
  242. defaChange(e) {
  243. const that = this;
  244. let value = e.detail.value == true ? '1' : '0';
  245. that.$set(that.form, `is_default`, value)
  246. },
  247. // 提交保存
  248. onSubmit(ref) {
  249. const that = this;
  250. let id = that.id;
  251. let user = that.user;
  252. that.$refs[ref].validate().then(async params => {
  253. params.customer = user.id;
  254. let res;
  255. if (id) res = await that.$api(`/address/${id}`, 'POST', params)
  256. else res = await that.$api(`/address`, 'POST', params);
  257. if (res.errcode == '0') {
  258. uni.showToast({
  259. title: '维护信息成功',
  260. icon: 'none'
  261. })
  262. uni.navigateBack({
  263. delta: 1
  264. })
  265. } else {
  266. uni.showToast({
  267. title: res.errmsg,
  268. icon: 'none'
  269. })
  270. }
  271. })
  272. },
  273. }
  274. }
  275. </script>
  276. <style lang="scss">
  277. .main {
  278. display: flex;
  279. flex-direction: column;
  280. width: 100vw;
  281. height: 100vh;
  282. .one {
  283. padding: 2vw;
  284. .toLoacl {
  285. margin: 1vw 0 0 0;
  286. }
  287. .picker {
  288. border: 1px solid #3333;
  289. border-radius: 5px;
  290. padding: 2vw;
  291. }
  292. .btn {
  293. text-align: center;
  294. button {
  295. width: 30%;
  296. font-size: 14px;
  297. background-color: var(--f35BColor);
  298. }
  299. }
  300. }
  301. }
  302. </style>