index.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. //@ts-ignore
  2. import Toast from 'tdesign-miniprogram/toast/index'
  3. import AddressApi from '../../../../services/address'
  4. import { areaData } from '../../../../utils/area'
  5. import checkauth from '../../../../utils/checkauth'
  6. const innerPhoneReg = '^1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[0-8]|8\\d|9\\d)\\d{8}$'
  7. const innerNameReg = '^[a-zA-Z\\d\\u4e00-\\u9fa5]+$'
  8. const labelsOptions = [
  9. { id: 0, name: '家' },
  10. { id: 1, name: '公司' },
  11. ]
  12. let privateData = {
  13. verifyTips: '',
  14. }
  15. Page({
  16. options: {
  17. multipleSlots: true,
  18. },
  19. externalClasses: ['theme-wrapper-class'],
  20. behaviors: [checkauth],
  21. /**
  22. * 页面的初始数据
  23. */
  24. data: {
  25. wait: false,
  26. locationState: {
  27. id: null,
  28. addressTag: '',
  29. cityCode: '',
  30. cityName: '',
  31. detailAddress: '',
  32. districtCode: '',
  33. districtName: '',
  34. isDefault: false,
  35. name: '',
  36. phone: '',
  37. provinceCode: '',
  38. provinceName: '',
  39. isEdit: false,
  40. isOrderDetail: false,
  41. isOrderSure: false,
  42. labelIndex: null,
  43. },
  44. areaData: areaData,
  45. labels: labelsOptions,
  46. areaPickerVisible: false,
  47. submitActive: false,
  48. visible: false,
  49. labelValue: '',
  50. columns: 3,
  51. },
  52. init() {
  53. const id = this.data.locationState.id
  54. if (id && Number(id) > 0) {
  55. this.setData({
  56. wait: true,
  57. })
  58. AddressApi.GetForEdit({
  59. id,
  60. })
  61. .then((rsp) => {
  62. const { result } = rsp
  63. if (result && result.wxAddress) {
  64. const addr = result.wxAddress
  65. const tag = addr.addressTag
  66. let labelIndex = null
  67. if (tag && tag.length > 0) {
  68. const { labels } = this.data
  69. const find = labels.filter((o) => o.name === tag)
  70. console.log('find', find)
  71. if (!find || find.length === 0) {
  72. console.log('create')
  73. labelIndex = labels[labels.length - 1].id + 1
  74. this.setData({
  75. labels: [...labels, { id: labelIndex, name: tag }],
  76. })
  77. } else {
  78. labelIndex = find[0].id
  79. }
  80. }
  81. this.setData(
  82. {
  83. wait: false,
  84. locationState: {
  85. ...addr,
  86. labelIndex,
  87. },
  88. },
  89. () => {
  90. const { isLegal, tips } = this.onVerifyInputLegal()
  91. this.setData({
  92. submitActive: isLegal,
  93. })
  94. privateData.verifyTips = tips
  95. },
  96. )
  97. }
  98. })
  99. .catch(() => {
  100. this.setData({
  101. wait: false,
  102. })
  103. })
  104. }
  105. },
  106. /**
  107. * 生命周期函数--监听页面加载
  108. */
  109. //@ts-ignore
  110. onLoad(query: { id: number }) {
  111. const { id = -1 } = query
  112. this.setData({
  113. 'locationState.id': id,
  114. })
  115. },
  116. /**
  117. * 生命周期函数--监听页面初次渲染完成
  118. */
  119. onReady() {},
  120. /**
  121. * 生命周期函数--监听页面显示
  122. */
  123. onShow() {
  124. const self = this
  125. //@ts-ignore
  126. this.afterAuth(() => {
  127. self.init()
  128. })
  129. },
  130. /**
  131. * 生命周期函数--监听页面隐藏
  132. */
  133. onHide() {},
  134. /**
  135. * 生命周期函数--监听页面卸载
  136. */
  137. onUnload() {},
  138. /**
  139. * 页面相关事件处理函数--监听用户下拉动作
  140. */
  141. onPullDownRefresh() {},
  142. /**
  143. * 页面上拉触底事件的处理函数
  144. */
  145. onReachBottom() {},
  146. /**
  147. * 用户点击右上角分享
  148. */
  149. onShareAppMessage() {},
  150. onPickArea() {
  151. this.setData({ areaPickerVisible: true })
  152. },
  153. onPickLabels(e: WechatMiniprogram.CustomEvent<{}, {}, { item: any }>) {
  154. const { item } = e.currentTarget.dataset
  155. const {
  156. locationState: { labelIndex = undefined },
  157. labels = [],
  158. } = this.data
  159. let payload = {
  160. labelIndex: item,
  161. addressTag: labels[item].name,
  162. }
  163. if (item === labelIndex) {
  164. payload = { labelIndex: null, addressTag: '' }
  165. }
  166. this.setData({
  167. 'locationState.labelIndex': payload.labelIndex,
  168. 'locationState.addressTag': payload.addressTag,
  169. })
  170. this.triggerEvent('triggerUpdateValue', payload)
  171. },
  172. addLabels() {
  173. this.setData({
  174. visible: true,
  175. })
  176. },
  177. confirmHandle() {
  178. const { labels, labelValue } = this.data
  179. this.setData({
  180. visible: false,
  181. labels: [...labels, { id: labels[labels.length - 1].id + 1, name: labelValue }],
  182. labelValue: '',
  183. })
  184. },
  185. cancelHandle() {
  186. this.setData({
  187. visible: false,
  188. labelValue: '',
  189. })
  190. },
  191. onCheckDefaultAddress({ detail }: { detail: { value: boolean } }) {
  192. const { value } = detail
  193. this.setData({
  194. 'locationState.isDefault': value,
  195. })
  196. },
  197. onInputValue(
  198. e: WechatMiniprogram.CustomEvent<
  199. { value: string; areas: { code: string; name: string }[] },
  200. {},
  201. { item: string }
  202. >,
  203. ) {
  204. const { item } = e.currentTarget.dataset
  205. const { value = '', areas = [] } = e.detail
  206. if (item === 'address') {
  207. this.setData(
  208. {
  209. 'locationState.provinceCode': areas[0].code,
  210. 'locationState.provinceName': areas[0].name,
  211. 'locationState.cityName': areas[1].name,
  212. 'locationState.cityCode': areas[1].code,
  213. 'locationState.districtCode': areas[2].code,
  214. 'locationState.districtName': areas[2].name,
  215. areaPickerVisible: false,
  216. },
  217. () => {
  218. const { isLegal, tips } = this.onVerifyInputLegal()
  219. this.setData({
  220. submitActive: isLegal,
  221. })
  222. privateData.verifyTips = tips
  223. },
  224. )
  225. } else {
  226. this.setData(
  227. {
  228. [`locationState.${item}`]: value,
  229. },
  230. () => {
  231. const { isLegal, tips } = this.onVerifyInputLegal()
  232. this.setData({
  233. submitActive: isLegal,
  234. })
  235. privateData.verifyTips = tips
  236. },
  237. )
  238. }
  239. },
  240. onVerifyInputLegal() {
  241. const { name, phone, detailAddress, districtName } = this.data.locationState
  242. const nameRegExp = new RegExp(innerNameReg)
  243. const phoneRegExp = new RegExp(innerPhoneReg)
  244. if (!name || !name.trim()) {
  245. return {
  246. isLegal: false,
  247. tips: '请填写收货人',
  248. }
  249. }
  250. if (!nameRegExp.test(name)) {
  251. return {
  252. isLegal: false,
  253. tips: '收货人仅支持输入中文、英文(区分大小写)、数字',
  254. }
  255. }
  256. if (!phone || !phone.trim()) {
  257. return {
  258. isLegal: false,
  259. tips: '请填写手机号',
  260. }
  261. }
  262. if (!phoneRegExp.test(phone)) {
  263. return {
  264. isLegal: false,
  265. tips: '请填写正确的手机号',
  266. }
  267. }
  268. if (!districtName || !districtName.trim()) {
  269. return {
  270. isLegal: false,
  271. tips: '请选择省市区信息',
  272. }
  273. }
  274. if (!detailAddress || !detailAddress.trim()) {
  275. return {
  276. isLegal: false,
  277. tips: '请完善详细地址',
  278. }
  279. }
  280. if (detailAddress && detailAddress.trim().length > 50) {
  281. return {
  282. isLegal: false,
  283. tips: '详细地址不能超过50个字符',
  284. }
  285. }
  286. return {
  287. isLegal: true,
  288. tips: '',
  289. }
  290. },
  291. formSubmit() {
  292. const { submitActive } = this.data
  293. if (!submitActive) {
  294. Toast({
  295. context: this,
  296. selector: '#t-toast',
  297. message: privateData.verifyTips,
  298. icon: '',
  299. duration: 1000,
  300. })
  301. return
  302. }
  303. const { locationState } = this.data
  304. const bag = {
  305. id: null,
  306. phone: locationState.phone,
  307. name: locationState.name,
  308. provinceName: locationState.provinceName,
  309. provinceCode: locationState.provinceCode,
  310. cityName: locationState.cityName,
  311. cityCode: locationState.cityCode,
  312. districtName: locationState.districtName,
  313. districtCode: locationState.districtCode,
  314. detailAddress: locationState.detailAddress,
  315. isDefault: locationState.isDefault,
  316. addressTag: locationState.addressTag,
  317. // latitude: locationState.latitude,
  318. // longitude: locationState.longitude,
  319. }
  320. if (Number(locationState.id) > 0) {
  321. bag.id = locationState.id
  322. }
  323. this.setData({
  324. wait: true,
  325. })
  326. AddressApi.CreateOrUpdate({
  327. wxAddress: bag,
  328. })
  329. .then((rsp) => {
  330. this.setData({
  331. wait: false,
  332. })
  333. if (rsp.result > 0) {
  334. wx.navigateBack({ delta: 1 })
  335. }
  336. })
  337. .catch(() => {
  338. this.setData({
  339. wait: false,
  340. })
  341. })
  342. },
  343. })