add.vue 7.7 KB

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