addOrder.js 12 KB

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