add.vue 7.7 KB

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