confirmOreder.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // pages/order/confirmOreder/confirmOreder.js
  2. const app = getApp();
  3. const addr = require('../../my/address/addSite/addSite.js');
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. isonline: 1,
  10. currentList: [],
  11. mode: 1,
  12. checked: true,
  13. show1: false,
  14. show2: false,
  15. code: "",
  16. order_no: "",
  17. orderinfo: "",
  18. address: "",
  19. useraddr: "",
  20. remark: "",
  21. address_id:0,
  22. freight:0,
  23. total_price:0,
  24. isphone: false,
  25. isphonenum: false,
  26. phonenum: '',
  27. couponinfo: ''
  28. },
  29. onLoad(opticon) {
  30. let order_no = opticon.order_no;
  31. this.detail(order_no);
  32. this.setData({
  33. order_no
  34. })
  35. },
  36. onShow(){
  37. this.setData({couponinfo: this.data.citem})
  38. if(this.data.couponinfo){
  39. if(this.data.couponinfo.type==1){
  40. if((parseFloat(this.data.freight)-parseFloat(this.data.couponinfo.money))>0){
  41. this.setData({total_price: (parseFloat(this.data.total_price) - parseFloat(this.data.couponinfo.money)).toFixed(2)})
  42. }else{
  43. this.setData({total_price: (parseFloat(this.data.total_price) - parseFloat(this.data.freight)).toFixed(2)})
  44. }
  45. }else if(this.data.couponinfo.type==2){
  46. if((parseFloat(this.data.total_price) - parseFloat(this.data.couponinfo.money))>0){
  47. this.setData({total_price: (parseFloat(this.data.total_price) - parseFloat(this.data.couponinfo.money)).toFixed(2)})
  48. }else{
  49. this.setData({total_price: 0})
  50. }
  51. }
  52. }
  53. },
  54. goyhq(){
  55. if(this.data.couponinfo){
  56. this.setData({
  57. total_price: parseFloat(parseFloat(this.data.total_price) + parseFloat(this.data.couponinfo.money)).toFixed(2)
  58. })
  59. }
  60. console.log(this.data.total_price)
  61. wx.navigateTo({
  62. url: '../coupon/coupon?totalprice='+this.data.total_price+'&freight='+this.data.freight,
  63. })
  64. },
  65. changeisphone(){
  66. this.setData({isphone: !this.data.isphone})
  67. },
  68. //详情
  69. detail(order) {
  70. var that = this;
  71. app.api.useApi(app.globalData.baseAppUrl + "api/order_detail", {
  72. order_no: order,
  73. }, "get").then(function (res) {
  74. if (res.code != 200) {
  75. wx.showLoading({
  76. title: res.message.msg,
  77. })
  78. setTimeout(function () {
  79. wx.hideLoading({
  80. success: (res) => { },
  81. })
  82. }, 2000);
  83. } else {
  84. that.setData({isonline: res.is_online})
  85. if(res.message.address!=' '&&res.message.address!=''){
  86. console.log('不为空')
  87. that.setData({isphone: false})
  88. }else{
  89. that.setData({isphonenum: true,isphone:true})
  90. }
  91. var unit=res.message.data.network.unit[res.message.data.network.unit.length-1];
  92. var total_price=parseFloat(res.message.data.total_price);
  93. var price=parseFloat(unit.price);
  94. that.setData({
  95. orderinfo: res.message.data,
  96. address: res.message.address.address,
  97. code: res.message.address.phone,
  98. name: res.message.address.username,
  99. address_id:res.message.address.id,
  100. useraddr: res.message.address,
  101. mode:unit.type,
  102. order_type:unit.type,
  103. freight:unit.price,
  104. total_price:(total_price+price).toFixed(2),
  105. currentList: res.message.data.services
  106. })
  107. let currentList = that.data.currentList
  108. currentList.map(item => {
  109. item.active = false
  110. })
  111. }
  112. }).catch(function (err) {
  113. console.log(err);
  114. })
  115. },
  116. changeClose1(e) {
  117. this.setData({
  118. show1: !this.data.show1
  119. })
  120. },
  121. changeClose2(e) {
  122. // console.log(e)
  123. this.setData({
  124. show2: !this.data.show2,
  125. remark: this.data.remark
  126. })
  127. },
  128. onChange(event) {
  129. console.log(event);
  130. this.setData({
  131. checked: event.detail,
  132. });
  133. },
  134. chios(e) {
  135. let that = this
  136. let { item } = e.currentTarget.dataset
  137. let currentList = that.data.currentList
  138. currentList.map((it, index) => {
  139. if (item.id == it.id) {
  140. // it.active = !it.active
  141. if(it.active){
  142. it.active = false
  143. that.setData({total_price: (parseFloat(that.data.total_price) - parseFloat((it.price.start_time && it.price.end_time)?it.price.sale_price*it.stock:it.price.price*it.stock)).toFixed(2)})
  144. }else{
  145. it.active = true
  146. that.setData({total_price: (parseFloat(that.data.total_price) + parseFloat((it.price.start_time && it.price.end_time)?it.price.sale_price*it.stock:it.price.price*it.stock)).toFixed(2)})
  147. }
  148. }
  149. })
  150. that.setData({
  151. currentList
  152. })
  153. // total_price
  154. },
  155. changeMode(e) {
  156. var id = e.currentTarget.dataset.id
  157. var key=e.currentTarget.dataset.indx;
  158. var unit=this.data.orderinfo.network.unit[key];
  159. var total_price=parseFloat(this.data.total_price)
  160. var price=parseFloat(unit.price)
  161. console.log(total_price,price)
  162. this.setData({
  163. mode: id,
  164. freight:unit.price,
  165. total_price:parseFloat(total_price+price).toFixed(2)
  166. })
  167. },
  168. goto() {
  169. let that = this
  170. if(!that.data.checked){
  171. wx.showLoading({
  172. title: '请阅读并同意洗护协议',
  173. })
  174. setTimeout(function () {
  175. wx.hideLoading({
  176. success: (res) => { },
  177. })
  178. }, 1000);
  179. }else{
  180. wx.requestSubscribeMessage({
  181. tmplIds: ['VGlAo-fOukIVSjdmOS5_-9IldrNplzrbKzZzWI2rFuc'],
  182. success (res) {
  183. console.log('成功-'+res);
  184. console.log(res)
  185. that.orderConfirm();
  186. }
  187. })
  188. }
  189. },
  190. getPhoneNumber(e) {
  191. app.login();
  192. console.log(e)
  193. let that = this
  194. var data = {
  195. 'mds': wx.getStorageSync('seisson_key'),
  196. 'encryptedData': e.detail.encryptedData,
  197. 'errMsg': e.detail.errMsg,
  198. 'iv': e.detail.iv,
  199. 'appid':wx.getAccountInfoSync().miniProgram.appId,
  200. }
  201. app.api.useApi(app.globalData.baseAppUrl+"api/user/getPhoneNumber",data,"post").then((res)=>{
  202. if (res.code == 200) {
  203. var phone = res.message.data.phone
  204. console.log(phone);
  205. if (phone) {
  206. if(that.data.isphonenum){
  207. that.setData({
  208. code: phone,
  209. })
  210. }
  211. that.changeisphone()
  212. }
  213. }
  214. }).catch((err)=>{
  215. console.log(err)
  216. })
  217. // console.log("fetch cloudfunction success", cloudResult.result)
  218. },
  219. input1(e) {
  220. console.log(e.detail);
  221. this.setData({
  222. name: e.detail.value
  223. })
  224. },
  225. input2(e) {
  226. this.setData({
  227. code: e.detail.value
  228. })
  229. },
  230. input3(e) {
  231. this.setData({
  232. address: e.detail.value
  233. })
  234. },
  235. input4(e) {
  236. this.setData({
  237. remark: e.detail.value
  238. })
  239. },
  240. addAddress() {
  241. var that = this;
  242. app.api.useApi(app.globalData.baseAppUrl + "api/address/create", {
  243. username: that.data.name,
  244. phone: that.data.code,
  245. address: that.data.address,
  246. }, "post").then(function (res) {
  247. if (res.code != 200) {
  248. wx.showLoading({
  249. title: res.original.message.msg,
  250. })
  251. setTimeout(function () {
  252. wx.hideLoading({
  253. success: (res) => { },
  254. })
  255. }, 1000);
  256. } else {
  257. that.changeClose1();
  258. that.setData({
  259. address_id: res.message.data.address.id,
  260. })
  261. }
  262. }).catch(function (err) {
  263. console.log(err);
  264. })
  265. },
  266. updateaddr(e) {
  267. var that = this;
  268. app.api.useApi(app.globalData.baseAppUrl + "api/address/update", {
  269. username: that.data.name,
  270. phone: that.data.code,
  271. address: that.data.address,
  272. id: e.target.dataset.id,
  273. }, "post").then(function (res) {
  274. if (res.code != 200) {
  275. wx.showLoading({
  276. title: res.message.msg,
  277. })
  278. } else {
  279. that.changeClose1();
  280. // wx.navigateTo({
  281. // url: '/pages/my/address/address',
  282. // })
  283. }
  284. setTimeout(function () {
  285. wx.hideLoading({
  286. success: (res) => { },
  287. })
  288. }, 2000);
  289. }).catch(function (err) {
  290. console.log(err.message);
  291. })
  292. },
  293. orderConfirm() {
  294. var that=this;
  295. let cid = 0
  296. if(that.data.couponinfo!=''&&that.data.couponinfo!=undefined){
  297. cid = that.data.couponinfo.pivot.id
  298. }
  299. let services = []
  300. that.data.currentList.forEach((item,index)=>{
  301. if(item.active){
  302. services.push({
  303. id: item.id,
  304. price: parseFloat((item.price.start_time && item.price.end_time)?item.price.sale_price*item.stock:item.price.price*item.stock).toFixed(2),
  305. price_id: item.price.id,
  306. stock: item.stock
  307. })
  308. }
  309. })
  310. console.log(that.data.currentList)
  311. console.log(that.data.currentList[0].price.id)
  312. console.log(services)
  313. app.api.useApi(app.globalData.baseAppUrl + "api/orders", {
  314. order_no: that.data.order_no,
  315. order_type: that.data.mode,
  316. user_remark: that.data.remark,
  317. address_id:that.data.address_id,
  318. coupon_id: cid,
  319. services: services
  320. }, "post").then(function (res) {
  321. console.log(res)
  322. if (res.code != 200) {
  323. wx.showLoading({
  324. title: res.message.msg,
  325. })
  326. if(res.code == 1020){
  327. // that.changeisphone()
  328. that.changeClose1()
  329. }
  330. setTimeout(function () {
  331. wx.hideLoading({
  332. success: (res) => { },
  333. })
  334. }, 1000);
  335. } else {
  336. wx.navigateTo({
  337. url: '/pages/my/cashier/cashier?order_no='+that.data.order_no
  338. })
  339. }
  340. }).catch(function (err) {
  341. // console.log(222);
  342. console.log(err)
  343. })
  344. },
  345. })