addOrder.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. const _Http = getApp().globalData.http,
  2. {
  3. formatTime
  4. } = require("../../utils/getTime");
  5. let pageNumber = 1,
  6. pageTotal = 1,
  7. sys_payorderid = null,
  8. currency = require("../../utils/currency"),
  9. CNY = value => currency(value, {
  10. symbol: "¥",
  11. precision: 2
  12. }).format();
  13. Page({
  14. data: {
  15. users: [],
  16. remarks: "",
  17. showAmount: "¥0.00",
  18. isDelete: true,
  19. useDiscount: 0
  20. },
  21. onLoad(options) {
  22. this.setData({
  23. sys_payinstructions: wx.getStorageSync('siteP').sys_payinstructions,
  24. sys_payincidence: 1,
  25. opUsers: options.users ? JSON.parse(options.users) : [],
  26. today: formatTime(new Date(), '-').split(' ')[0]
  27. })
  28. sys_payorderid = options.sys_payorderid;
  29. this.getVersions(options.vid || "");
  30. this.getDiscounts();
  31. },
  32. /* 获取优惠卷 */
  33. getDiscounts() {
  34. _Http.basic({
  35. "id": 20230801162402,
  36. "content": {
  37. nocache: true
  38. }
  39. }).then(res => {
  40. console.log("获取优惠卷", res)
  41. this.setData({
  42. discounts: res.data
  43. })
  44. })
  45. },
  46. /* 保存订单进度 */
  47. changeOrder() {
  48. _Http.basic({
  49. "classname": "system.payorder.payorder",
  50. "method": "insertUsers",
  51. "content": {
  52. "sys_site_systempartitionid": this.data.sys_site_systempartitionid,
  53. sys_payorderid,
  54. "remarks": this.data.remarks,
  55. "users": this.data.users.map(v => {
  56. let item = this.data.userList.find(s => {
  57. let id = this.data.sys_payincidence == 1 ? s.userid : s.sa_agentsid;
  58. return id == v
  59. })
  60. return {
  61. "isleader": item.isleader,
  62. "userid": item.userid,
  63. "sa_agentsid": item.sa_agentsid,
  64. "enddate": item.enddate[this.data.sys_site_systempartitionid] || ''
  65. }
  66. })
  67. }
  68. }).then(res => {
  69. console.log("修改订单信息", res)
  70. if (res.msg != '成功') return wx.showToast({
  71. title: res.msg,
  72. icon: "none",
  73. mask: true
  74. });
  75. this.setData({
  76. showAmount: CNY(res.data.amount),
  77. orderno: res.data.orderno
  78. })
  79. })
  80. },
  81. /* 更改备注 */
  82. onblur(e) {
  83. console.log(e)
  84. if (e.detail.value == this.data.remarks) return;
  85. this.setData({
  86. remarks: e.detail.value
  87. });
  88. this.changeOrder();
  89. },
  90. /* 获取版本 */
  91. getVersions(vid) {
  92. _Http.basic({
  93. "classname": "system.payorder.payorder",
  94. "method": "chooseSystemPartition",
  95. "content": {
  96. "pageNumber": 1,
  97. "pageSize": 9999,
  98. "where": {
  99. "condition": ""
  100. }
  101. }
  102. }).then(res => {
  103. console.log("版本列表", res)
  104. if (res.msg != '成功') {
  105. wx.showToast({
  106. title: res.msg,
  107. icon: "none",
  108. mask: true
  109. })
  110. setTimeout(() => {
  111. wx.navigateBack();
  112. }, 1000)
  113. return;
  114. };
  115. let sys_site_systempartitionid = res.data[0].sys_site_systempartitionid || '';
  116. if (vid && res.data.some(v => v.sys_site_systempartitionid == vid)) sys_site_systempartitionid = vid - 0;
  117. this.setData({
  118. versionsList: res.data,
  119. sys_site_systempartitionid
  120. })
  121. this.getUsers(true);
  122. })
  123. },
  124. /* 切换版本 */
  125. changeVer(e) {
  126. const {
  127. sys_site_systempartitionid
  128. } = e.currentTarget.dataset.item;
  129. if (this.data.sys_site_systempartitionid == sys_site_systempartitionid) return;
  130. this.setData({
  131. sys_site_systempartitionid
  132. });
  133. this.setUsers();
  134. },
  135. checkVer(e) {
  136. const {
  137. item
  138. } = e.currentTarget.dataset;
  139. this.selectComponent("#inventory").show(item.systemapp)
  140. },
  141. /* 获取可添加账号/主体 */
  142. getUsers(init = false) {
  143. if (init) {
  144. pageNumber = 1;
  145. pageTotal = 1;
  146. };
  147. if (pageNumber > pageTotal) return;
  148. _Http.basic({
  149. "classname": "system.payorder.payorder",
  150. "method": "chooseUsers",
  151. "content": {
  152. pageNumber,
  153. "pageSize": 99999,
  154. "where": {
  155. "condition": ""
  156. }
  157. }
  158. }).then(res => {
  159. console.log("用户列表", res)
  160. if (res.msg != '成功') {
  161. wx.showToast({
  162. title: res.msg,
  163. icon: "none",
  164. mask: true
  165. })
  166. setTimeout(() => {
  167. wx.navigateBack();
  168. }, 1000)
  169. return;
  170. };
  171. pageTotal = res.pageTotal;
  172. if (res.data.some(v => v.isleader == 1)) {
  173. this.setData({
  174. userList: res.pageNumber == 1 ? res.data : this.data.userList.concat(res.data)
  175. })
  176. pageNumber = res.pageNumber + 1;
  177. this.setUsers(this.data.opUsers.filter(id => this.data.userList.some(v => id == v.userid)));
  178. } else {
  179. wx.showModal({
  180. title: '提示',
  181. content: "您的团队缺少主账号无法使用,请联系客服!",
  182. showCancel: false,
  183. complete: (res) => {
  184. if (res.confirm) {
  185. wx.navigateBack()
  186. }
  187. }
  188. })
  189. }
  190. })
  191. },
  192. setUsers(users = []) {
  193. let list = this.data.userList,
  194. idname = this.data.sys_payincidence == 1 ? 'userid' : 'sa_agentsid',
  195. useDiscount = 0;
  196. list.forEach(v => {
  197. let date = v.enddate[this.data.sys_site_systempartitionid];
  198. if (v.userid == wx.getStorageSync('userMsg').userid || v.isleader == 1) {
  199. if (date) {
  200. if (formatTime(new Date(), '-').split(" ")[0] >= date) users.push(v[idname] + '');
  201. } else {
  202. if (!users.some(v => v == v[idname])) users.push(v[idname] + '');
  203. }
  204. }
  205. if (users.some(id => id == v.userid) && v.isleader == 0) useDiscount += 1
  206. });
  207. this.setData({
  208. users,
  209. useDiscount
  210. });
  211. this.changeOrder();
  212. },
  213. onReachBottom() {
  214. // this.getUsers();
  215. },
  216. onChange(e) {
  217. const {
  218. id,
  219. isleader
  220. } = e.currentTarget.dataset;
  221. let users = this.data.users,
  222. useDiscount = this.data.useDiscount;
  223. if (users.some(v => v == id)) {
  224. if (isleader) {
  225. let item = this.data.userList.find(v => v.userid == id),
  226. date = item.enddate[this.data.sys_site_systempartitionid];
  227. if (date && this.data.today >= date) {
  228. return;
  229. } else {
  230. users = users.filter(s => s != id)
  231. if (isleader == 0) useDiscount -= 1;
  232. }
  233. } else {
  234. users = users.filter(s => s != id)
  235. if (isleader == 0) useDiscount -= 1;
  236. }
  237. } else {
  238. users.push(id + "")
  239. if (isleader == 0) useDiscount += 1;
  240. }
  241. this.setData({
  242. users,
  243. useDiscount
  244. });
  245. this.changeOrder();
  246. },
  247. examine() {
  248. return new Promise((resolve, reject) => {
  249. if (currency(this.data.showAmount).value == 0) {
  250. wx.showModal({
  251. title: '提示',
  252. content: '优惠后金额为0,是否确认',
  253. complete: (res) => {
  254. if (res.cancel) resolve(false)
  255. if (res.confirm) resolve(true)
  256. }
  257. })
  258. } else {
  259. resolve(true)
  260. }
  261. })
  262. },
  263. isRenew() {
  264. return new Promise((resolve, reject) => {
  265. const idname = this.data.sys_payincidence == 1 ? 'userid' : 'sa_agentsid',
  266. renew = this.data.users.map(v => this.data.userList.find(s => s[idname] == v)).filter(v => v.enddate[this.data.sys_site_systempartitionid]).map(v => v[this.data.sys_payincidence == 1 ? 'name' : 'agentname']).join(',');
  267. if (renew) {
  268. wx.showModal({
  269. title: '提示',
  270. content: `${renew}还未到期,是否继续付费`,
  271. confirmText: "继续付费",
  272. complete: (res) => {
  273. if (res.cancel) resolve(false)
  274. if (res.confirm) resolve(true)
  275. }
  276. })
  277. } else {
  278. resolve(true)
  279. }
  280. })
  281. },
  282. /* 支付 */
  283. async payment() {
  284. let isRenew = await this.isRenew();
  285. if (!isRenew) return;
  286. let isPayment = await this.examine();
  287. if (!isPayment) return;
  288. let that = this;
  289. that.data.isDelete = false;
  290. wx.login({
  291. success: (s) => {
  292. _Http.basic({
  293. "classname": "system.payment.wechatpay",
  294. "method": "createWechatOrder",
  295. "content": {
  296. "orderno": that.data.orderno,
  297. "wechat_code": s.code,
  298. "trade_type": "JSAPI"
  299. }
  300. }).then(res => {
  301. wx.requestPayment({
  302. timeStamp: res.data.timeStamp,
  303. nonceStr: res.data.nonceStr,
  304. package: res.data.package,
  305. signType: res.data.signType,
  306. paySign: res.data.paySign,
  307. success() {
  308. wx.showToast({
  309. title: '支付成功',
  310. mask: true
  311. })
  312. },
  313. fail(err) {
  314. if (res.msg == '成功' && currency(that.data.showAmount).value == 0) {
  315. wx.showToast({
  316. title: '支付成功',
  317. mask: true
  318. })
  319. } else {
  320. wx.showToast({
  321. title: '支付失败',
  322. icon: "error",
  323. mask: true
  324. })
  325. console.error(err)
  326. }
  327. },
  328. complete(e) {
  329. console.log(e)
  330. setTimeout(() => {
  331. wx.redirectTo({
  332. url: '/pages/teams/order?id=' + sys_payorderid,
  333. })
  334. }, 1000)
  335. }
  336. })
  337. })
  338. },
  339. })
  340. },
  341. onUnload() {
  342. if (this.data.isDelete) _Http.basic({
  343. "classname": "system.payorder.payorder",
  344. "method": "delete",
  345. "content": {
  346. sys_payorderid
  347. }
  348. }).then(res => {
  349. console.log("删除订单", res)
  350. })
  351. }
  352. })