东风启辰小程序端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

placingOrder.js 10.0KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // pages/placingOrder/placingOrder.js
  2. const app = getApp()
  3. Component({
  4. /**
  5. * 组件的属性列表
  6. */
  7. properties: {
  8. },
  9. /**
  10. * 组件的初始数据
  11. */
  12. data: {
  13. imgUrl: app.globalData.urlStatic,//图片路径
  14. agreement:false,
  15. isAgreement: true,//是否同意协议
  16. siteSelect: false,//是否显示位置选择框
  17. phoneInputShow: false,//是否显示电话输入框
  18. provinceArr: [],//省
  19. provinceValue: 0,//选中的省下标
  20. storeArr: [],//店铺
  21. storeValue: 0,//选中的店铺下标
  22. verificationCode: '获取验证码',//验证码文案
  23. sendCode: true,
  24. appointment: true,
  25. subscribeData: {
  26. realname: "",//姓名
  27. mobile: "",//手机号
  28. captcha: "",//验证码
  29. province: "",//省份
  30. agent_code: "",//经销商编码
  31. agentDetail: "",//经销商详细信息
  32. parentOpenid: app.globalData.friendOpenid,//好友openid
  33. },
  34. getBuyState:false
  35. },
  36. attached: function () {
  37. if (app.globalData.openid) {
  38. this.loadFun();
  39. } else {
  40. app.globalData.openidSuccessFuc = this.loadFun;
  41. }
  42. },
  43. /**
  44. * 组件的方法列表
  45. */
  46. methods: {
  47. closeXieyi: function () {
  48. this.triggerEvent('placing')
  49. },
  50. getUserPhone: function (e) {//获取用户手机号
  51. this.setData({
  52. phoneInputShow: true
  53. })
  54. if (e.detail.errMsg == 'getPhoneNumber:ok') {
  55. app.getMobile(e.detail.encryptedData, e.detail.iv, res => {
  56. if (res.code == 200) {
  57. if (res.data && res.data.decodeData) {
  58. this.data.subscribeData.mobile = res.data.decodeData.phoneNumber;
  59. this.setData({
  60. subscribeData: this.data.subscribeData
  61. })
  62. }
  63. } else {
  64. wx.showToast({
  65. title: res.msg,
  66. icon: "none"
  67. })
  68. }
  69. }, this);
  70. }
  71. },
  72. loadFun: function () {
  73. if (app.globalData.myCenterData) {
  74. this.setData({
  75. provinceArr: app.globalData.myCenterData.provinceArr,
  76. storeArr: app.globalData.myCenterData.storeArr,
  77. provinceValue: app.globalData.myCenterData.provinceValue,
  78. storeValue: app.globalData.myCenterData.storeValue,
  79. })
  80. this.data.subscribeData.province = this.data.provinceArr[this.data.provinceValue].province;
  81. this.data.subscribeData.agentDetail = this.data.storeArr[this.data.storeValue].agent_detail;
  82. this.data.subscribeData.agent_code = this.data.storeArr[this.data.storeValue].agent_code;
  83. }
  84. if (app.globalData.userMobile) {
  85. this.data.subscribeData.mobile = app.globalData.userMobile;
  86. this.setData({
  87. phoneInputShow: true,
  88. subscribeData: this.data.subscribeData
  89. })
  90. }
  91. },
  92. getRealname: function (e) {//获取用户输入的姓名
  93. this.data.subscribeData.realname = e.detail.value;
  94. },
  95. getMobile: function (e) {//获取用户输入的电话
  96. this.data.subscribeData.mobile = e.detail.value;
  97. },
  98. getCaptcha: function (e) {//获取用户输入的验证码
  99. this.data.subscribeData.captcha = e.detail.value;
  100. },
  101. getDistributorList: function (longitude, latitude) {//获取经销商列表
  102. app.wxRequest(app.globalData.urlRoot + "agent/getAgentList", { longitude: longitude, latitude: latitude }, res => {
  103. if (res.code == 200) {
  104. app.globalData.myCenterData = {};
  105. this.setData({
  106. provinceArr: res.data.list,
  107. storeArr: res.data.list[res.data.nearData.provinceIndex].children,
  108. provinceValue: res.data.nearData.provinceIndex,
  109. storeValue: res.data.nearData.cityIndex
  110. })
  111. this.data.subscribeData.province = this.data.provinceArr[this.data.provinceValue].province;
  112. this.data.subscribeData.agentDetail = this.data.storeArr[this.data.storeValue].agent_detail;
  113. this.data.subscribeData.agent_code = this.data.storeArr[this.data.storeValue].agent_code;
  114. app.globalData.myCenterData.provinceArr = res.data.list;
  115. app.globalData.myCenterData.storeArr = res.data.list[res.data.nearData.provinceIndex].children;
  116. app.globalData.myCenterData.provinceValue = res.data.nearData.provinceIndex;
  117. app.globalData.myCenterData.storeValue = res.data.nearData.cityIndex;
  118. } else {
  119. wx.showToast({
  120. title: res.msg,
  121. icon: "none"
  122. })
  123. }
  124. }, this);
  125. },
  126. provinceChane: function (e) {//选中省
  127. this.setData({
  128. provinceValue: e.detail.value,
  129. storeArr: this.data.provinceArr[e.detail.value].children,
  130. storeValue: 0
  131. })
  132. this.data.subscribeData.province = this.data.provinceArr[this.data.provinceValue].province;
  133. this.data.subscribeData.agentDetail = this.data.storeArr[this.data.storeValue].agent_detail;
  134. this.data.subscribeData.agent_code = this.data.storeArr[this.data.storeValue].agent_code;
  135. },
  136. storeChane: function (e) {//选中店铺
  137. this.setData({
  138. storeValue: e.detail.value,
  139. })
  140. this.data.subscribeData.agentDetail = this.data.storeArr[this.data.storeValue].agent_detail;
  141. this.data.subscribeData.agent_code = this.data.storeArr[this.data.storeValue].agent_code;
  142. },
  143. getCode: function (e) {//获取验证码
  144. if (!app.mobileVerify(this.data.subscribeData.mobile)) {
  145. if (this.data.subscribeData.mobile) {
  146. wx.showToast({
  147. title: '请输入正确的电话',
  148. icon: 'none'
  149. })
  150. } else {
  151. wx.showToast({
  152. title: '请输入电话',
  153. icon: 'none'
  154. })
  155. }
  156. return;
  157. }
  158. if (!this.data.sendCode) {
  159. return;
  160. }
  161. this.data.sendCode = false;
  162. app.wxRequest(app.globalData.urlRoot + "captcha/sendCaptcha", { mobile: this.data.subscribeData.mobile }, res => {
  163. if (res.code == 200) {
  164. this.countDown();
  165. wx.showToast({
  166. title: '验证码获取成功',
  167. icon: "none"
  168. })
  169. this.setData({
  170. verificationCode: 60
  171. })
  172. } else {
  173. this.data.sendCode = true;
  174. wx.showToast({
  175. title: res.msg,
  176. icon: "none"
  177. })
  178. }
  179. }, this)
  180. },
  181. countDown: function () {//倒计时
  182. setTimeout(() => {
  183. this.setData({
  184. verificationCode: this.data.verificationCode - 1
  185. })
  186. if (this.data.verificationCode > 0) {
  187. this.countDown();
  188. } else {
  189. this.setData({
  190. verificationCode: "获取验证码"
  191. })
  192. this.data.sendCode = true;
  193. }
  194. }, 1000);
  195. },
  196. getUserLocation: function (e) {
  197. wx.getLocation({
  198. type: 'gcj02', //wgs84
  199. success: (res) => {
  200. this.getDistributorList(res.longitude, res.latitude);
  201. },
  202. fail: (res) => {
  203. this.getDistributorList("", "");
  204. },
  205. complete: (res) => {
  206. this.setData({
  207. siteSelect: true,
  208. })
  209. }
  210. })
  211. },
  212. showSite: function () {//显示地址选择框
  213. if (!this.data.siteSelect) {
  214. if (!app.globalData.myCenterData) {
  215. this.getUserLocation();//获取用户当前位置
  216. } else {
  217. this.setData({
  218. siteSelect: true
  219. })
  220. }
  221. }
  222. },
  223. agreementState: function () {//协议
  224. this.setData({
  225. isAgreement: !this.data.isAgreement
  226. })
  227. },
  228. agreementControl: function () {
  229. this.setData({
  230. agreement: !this.data.agreement
  231. })
  232. },
  233. preBuy: function () {//下订支付
  234. if (!this.data.subscribeData.realname) {
  235. wx.showToast({
  236. title: '请输入姓名',
  237. icon: "none"
  238. })
  239. return;
  240. }
  241. if (!this.data.subscribeData.mobile) {
  242. wx.showToast({
  243. title: '请输入电话',
  244. icon: "none"
  245. })
  246. return;
  247. }
  248. if (!this.data.subscribeData.captcha) {
  249. wx.showToast({
  250. title: '请输入验证码',
  251. icon: "none"
  252. })
  253. return;
  254. }
  255. if (!this.data.isAgreement) {
  256. wx.showToast({
  257. title: '请同意协议',
  258. icon: 'none'
  259. })
  260. return;
  261. }
  262. if (!this.data.appointment) {
  263. return;
  264. }
  265. this.data.appointment = false;
  266. app.wxRequest(app.globalData.urlRoot + "wxPay/preBuy", this.data.subscribeData, res => {
  267. if (res.code == 200) {
  268. wx.requestPayment({
  269. timeStamp: res.data.jsSubmit.timeStamp,
  270. nonceStr: res.data.jsSubmit.nonceStr,
  271. signType: res.data.jsSubmit.signType,
  272. package: res.data.jsSubmit.package,
  273. paySign: res.data.jsSubmit.paySign,
  274. success:(option)=>{
  275. this.paySuccess(res.data.trade_id);
  276. },
  277. fail: (option) => {
  278. this.data.appointment = true;
  279. wx.showToast({
  280. title: '支付失败',
  281. icon:"none"
  282. })
  283. }
  284. })
  285. } else {
  286. this.data.appointment = true;
  287. wx.showToast({
  288. title: res.msg,
  289. icon: "none"
  290. })
  291. }
  292. }, this, "POST")
  293. },
  294. paySuccess: function (e) {//支付成功
  295. app.wxRequest(app.globalData.urlRoot + "wxPay/paySuccess", { trade_id:e},res=>{
  296. if(res.code==200){
  297. // wx.showToast({
  298. // title: '下订成功',
  299. // mask:true
  300. // })
  301. this.setData({
  302. getBuyState:true
  303. })
  304. app.globalData.getBuyState = {};
  305. app.globalData.getBuyState.success = true;
  306. app.globalData.getBuyState.cdate = res.data.cdate;
  307. // setTimeout(()=>{
  308. // this.closeXieyi();
  309. // },1500)
  310. } else {
  311. this.data.appointment = true;
  312. wx.showToast({
  313. title: res.msg,
  314. icon:'none'
  315. })
  316. }
  317. },this,"POST");
  318. },
  319. closePage:function(){
  320. this.closeXieyi();
  321. }
  322. }
  323. })