add.vue 7.9 KB

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