index.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. const WXAPI = require('apifm-wxapi')
  2. const AUTH = require('../../utils/auth')
  3. Page({
  4. data: {
  5. logisticsType: '0', // 0 自己送货 1 快递
  6. shopIndex: -1
  7. },
  8. onLoad(e) {
  9. // e.type = 1
  10. // e.orderId = 3
  11. // e.platform = 'jd'
  12. this.setData({
  13. type: e.type,
  14. orderId: e.orderId,
  15. platform: e.platform
  16. })
  17. wx.getLocation({
  18. type: 'wgs84', //wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标
  19. success: res => {
  20. this.data.latitude = res.latitude
  21. this.data.longitude = res.longitude
  22. this.initData()
  23. },
  24. fail: err => {
  25. console.error(err)
  26. this.initData()
  27. AUTH.checkAndAuthorize('scope.userLocation')
  28. }
  29. })
  30. },
  31. onShow() {
  32. },
  33. async initData() {
  34. this.fetchShops()
  35. if (this.data.type == 1 && this.data.platform == 'jd') {
  36. this.cpsJdOrderDetail()
  37. }
  38. if (this.data.type == 1 && this.data.platform == 'pdd') {
  39. this.cpsPddOrderDetail()
  40. }
  41. },
  42. async fetchShops(){
  43. const p = {}
  44. if (this.data.latitude) {
  45. p.curlatitude = this.data.latitude
  46. }
  47. if (this.data.longitude) {
  48. p.curlongitude = this.data.longitude
  49. }
  50. const res = await WXAPI.fetchShops(p)
  51. if (res.code == 0) {
  52. res.data.forEach(ele => {
  53. if (ele.distance) {
  54. ele.distance = ele.distance.toFixed(3) // 距离保留3位小数
  55. }
  56. })
  57. this.setData({
  58. shops: res.data
  59. })
  60. }
  61. },
  62. async cpsJdOrderDetail() {
  63. wx.showLoading({
  64. title: '',
  65. })
  66. const res = await WXAPI.cpsJdOrderDetail(wx.getStorageSync('token'), this.data.orderId)
  67. wx.hideLoading()
  68. if (res.code == 0) {
  69. const orderInfo = res.data.orderInfo
  70. if (orderInfo.validCode != 17) {
  71. wx.showModal({
  72. title: '错误',
  73. content: '已完成订单才可以申请回收',
  74. showCancel: false,
  75. success: res => {
  76. wx.navigateBack()
  77. }
  78. })
  79. }
  80. if (orderInfo.recycleOrderId) {
  81. wx.showModal({
  82. title: '错误',
  83. content: '请勿重复申请回收',
  84. showCancel: false,
  85. success: res => {
  86. wx.navigateBack()
  87. }
  88. })
  89. }
  90. this.setData({
  91. orderInfo,
  92. amountRecycle: orderInfo.estimateCosPrice,
  93. name: orderInfo.skuName,
  94. pic: orderInfo.imageUrl,
  95. amount: orderInfo.actualCosPrice,
  96. })
  97. } else {
  98. wx.showModal({
  99. title: '错误',
  100. content: res.msg,
  101. showCancel: false,
  102. success: res => {
  103. wx.navigateBack()
  104. }
  105. })
  106. }
  107. },
  108. async cpsPddOrderDetail() {
  109. wx.showLoading({
  110. title: '',
  111. })
  112. const res = await WXAPI.cpsPddOrderDetail(wx.getStorageSync('token'), this.data.orderId)
  113. wx.hideLoading()
  114. if (res.code == 0) {
  115. const orderInfo = res.data.orderInfo
  116. if (orderInfo.status != 2 && orderInfo.status != 3 && orderInfo.status != 5) {
  117. wx.showModal({
  118. title: '错误',
  119. content: '已完成订单才可以申请回收',
  120. showCancel: false,
  121. success: res => {
  122. wx.navigateBack()
  123. }
  124. })
  125. }
  126. if (orderInfo.recycleOrderId) {
  127. wx.showModal({
  128. title: '错误',
  129. content: '请勿重复申请回收',
  130. showCancel: false,
  131. success: res => {
  132. wx.navigateBack()
  133. }
  134. })
  135. }
  136. this.setData({
  137. orderInfo,
  138. amountRecycle: orderInfo.orderAmount,
  139. name: orderInfo.goodsName,
  140. pic: orderInfo.imageUrl,
  141. amount: orderInfo.orderAmount,
  142. })
  143. } else {
  144. wx.showModal({
  145. title: '错误',
  146. content: res.msg,
  147. showCancel: false,
  148. success: res => {
  149. wx.navigateBack()
  150. }
  151. })
  152. }
  153. },
  154. logisticsTypeChange(e) {
  155. this.setData({
  156. logisticsType: e.detail
  157. })
  158. },
  159. logisticsTypeClick(e) {
  160. this.setData({
  161. logisticsType: e.currentTarget.dataset.name
  162. })
  163. },
  164. shopSelect(e) {
  165. this.setData({
  166. shopIndex: e.detail.value
  167. })
  168. },
  169. callMobile() {
  170. const shop = this.data.shops[this.data.shopIndex]
  171. wx.makePhoneCall({
  172. phoneNumber: shop.linkPhone,
  173. })
  174. },
  175. goMap() {
  176. const shop = this.data.shops[this.data.shopIndex]
  177. const latitude = shop.latitude
  178. const longitude = shop.longitude
  179. wx.openLocation({
  180. latitude,
  181. longitude,
  182. scale: 18
  183. })
  184. },
  185. async submit() {
  186. if (!this.data.amountRecycle) {
  187. wx.showToast({
  188. title: '填写回收价格',
  189. icon: 'none'
  190. })
  191. return
  192. }
  193. if (this.data.shopIndex == -1) {
  194. wx.showToast({
  195. title: '请选择回收点',
  196. icon: 'none'
  197. })
  198. return
  199. }
  200. const res = await WXAPI.recycleOrderApply({
  201. token: wx.getStorageSync('token'),
  202. type: this.data.type,
  203. platform: this.data.platform,
  204. buyOrderId: this.data.orderId,
  205. name: this.data.name,
  206. pic: this.data.pic,
  207. amount: this.data.amount,
  208. amountRecycle: this.data.amountRecycle,
  209. logisticsType: this.data.logisticsType,
  210. shopId: this.data.shops[this.data.shopIndex].id,
  211. remark: this.data.remark ? this.data.remark : '',
  212. })
  213. if (res.code != 0) {
  214. wx.showToast({
  215. title: res.msg,
  216. icon: 'none'
  217. })
  218. return
  219. }
  220. wx.showModal({
  221. title: '成功',
  222. content: '提交成功,耐心等待审核',
  223. showCancel: false,
  224. success: res => {
  225. wx.redirectTo({
  226. url: '/pages/recycle/orders',
  227. })
  228. }
  229. })
  230. }
  231. })